clusterAnalysisLeftovers.ipynb 24 KB

#load required libraries
import sys
import os
import chardet
import json
import re
import datetime

#you should get nixSuite via git clone https://git0.fmf.uni-lj.si/studen/nixSuite.git
#if you don't put it to $HOME/software/src/, you should update the path
nixSuite=os.path.join(os.path.expanduser('~'),'software','src','nixSuite')
sys.path.append(os.path.join(nixSuite,'wrapper'))
import nixWrapper
nixWrapper.loadLibrary('labkeyInterface')
import labkeyInterface
import labkeyDatabaseBrowser
import labkeyFileBrowser
nixWrapper.loadLibrary('orthancInterface')
import orthancInterface
import orthancDatabaseBrowser

def connectDB(server):
    #check connectivity. This checks the configuration in $HOME/.labkey/network.json, 
    #where paths to certificates are stored
    net=labkeyInterface.labkeyInterface()
    fconfig=os.path.join(os.path.expanduser('~'),'.labkey','{}.json'.format(server))
    net.init(fconfig)
    #this reports the certificate used
    try:
        print('Using: {}'.format(net.connectionConfig['SSL']['user']))
    except KeyError:
        pass
    #This gets a deafult CSRF code; It should report user name plus a long string of random hex numbers
    net.getCSRF()
    db=labkeyDatabaseBrowser.labkeyDB(net)
    fb=labkeyFileBrowser.labkeyFileBrowser(net)
    return db,fb

def modifyRowsWithCheck(db,project,schema,query,entry,testFields):
    mode='insert'
    filters=[{'variable':x,'value':'{}'.format(entry[x]),'oper':'eq'} for x in testFields]
    ds=db.selectRows(project,schema,query,filters)
    finalEntry={}
    if len(ds['rows'])==1:
        finalEntry=ds['rows'][0]
        mode='update'
    for q in entry:
        finalEntry[q]=entry[q];
    print(db.modifyRows(mode,project,schema,query,[finalEntry]))

def connectOrthanc(server):
    net=orthancInterface.orthancInterface()
    fconfig=os.path.join(os.path.expanduser('~'),'.labkey','{}.json'.format(server))
    net.init(fconfig)
    db=orthancDatabaseBrowser.orthancDB(net)
    return db

loadLibrary
remoteSourcesURL https://git0.fmf.uni-lj.si/studen/nixSuite/raw/master/remoteResources/resources.json
{'labkeyInterface': {'url': 'https://git0.fmf.uni-lj.si/studen/labkeyInterface/archive/master.zip', 'branch': 'master', 'modules': []}, 'irAEMM': {'url': 'https://git0.fmf.uni-lj.si/studen/iraemm/archive/master.zip', 'branch': 'master', 'modules': ['iraemmBrowser']}, 'SlicerLabkeyExtension': {'url': 'https://git0.fmf.uni-lj.si/studen/SlicerLabkeyExtension/archive/SlicerExtensionIndex.zip', 'branch': 'SlicerExtensionIndex', 'modules': ['labkeyBrowser']}, 'limfomiPET': {'url': 'https://git0.fmf.uni-lj.si/studen/limfomiPET/archive/master.zip', 'branch': 'master', 'modules': ['imageBrowser', 'segmentationBrowser']}, 'parseConfig': {'url': 'https://git0.fmf.uni-lj.si/studen/parseConfig/archive/master.zip', 'branch': 'master', 'modules': []}, 'orthancInterface': {'url': 'https://git0.fmf.uni-lj.si/studen/orthancInterface/archive/master.zip', 'branch': 'master', 'modules': []}, 'dynamicSPECT': {'url': 'https://git0.fmf.uni-lj.si/studen/dynamicSPECT/archive/master.zip', 'branch': 'master', 'modules': ['imageBrowser']}}
{'url': 'https://git0.fmf.uni-lj.si/studen/labkeyInterface/archive/master.zip', 'branch': 'master', 'modules': []}
File  C:\Users\studen\temp\labkeyInterface.zip: True
loadLibrary
remoteSourcesURL https://git0.fmf.uni-lj.si/studen/nixSuite/raw/master/remoteResources/resources.json
{'labkeyInterface': {'url': 'https://git0.fmf.uni-lj.si/studen/labkeyInterface/archive/master.zip', 'branch': 'master', 'modules': []}, 'irAEMM': {'url': 'https://git0.fmf.uni-lj.si/studen/iraemm/archive/master.zip', 'branch': 'master', 'modules': ['iraemmBrowser']}, 'SlicerLabkeyExtension': {'url': 'https://git0.fmf.uni-lj.si/studen/SlicerLabkeyExtension/archive/SlicerExtensionIndex.zip', 'branch': 'SlicerExtensionIndex', 'modules': ['labkeyBrowser']}, 'limfomiPET': {'url': 'https://git0.fmf.uni-lj.si/studen/limfomiPET/archive/master.zip', 'branch': 'master', 'modules': ['imageBrowser', 'segmentationBrowser']}, 'parseConfig': {'url': 'https://git0.fmf.uni-lj.si/studen/parseConfig/archive/master.zip', 'branch': 'master', 'modules': []}, 'orthancInterface': {'url': 'https://git0.fmf.uni-lj.si/studen/orthancInterface/archive/master.zip', 'branch': 'master', 'modules': []}, 'dynamicSPECT': {'url': 'https://git0.fmf.uni-lj.si/studen/dynamicSPECT/archive/master.zip', 'branch': 'master', 'modules': ['imageBrowser']}}
{'url': 'https://git0.fmf.uni-lj.si/studen/orthancInterface/archive/master.zip', 'branch': 'master', 'modules': []}
File  C:\Users\studen\temp\orthancInterface.zip: True
setup={}
setup['server']='onko-nix'
setup['project']='limfomiPET/Study2023'
setup['query']='missingTransfers'
#setup['view']='Missing visit 1'
setup['view']='Missing pre-treatment'

def listOrthanc(setup):
    db,fb=connectDB(setup['server'])
    ds=db.selectRows(setup['project'],'study',setup['query'],[],setup['view'])
    rows=ds['rows']
    for r in rows:
        imgs={}
        idFilter={'variable':'PatientId','value':r['ParticipantId'],'oper':'eq'}
        ds=db.selectRows('Orthanc/Database','study','Imaging',[idFilter])
        for x in ds['rows']:
            date=datetime.datetime.strptime(x['studyDate'],'%Y/%m/%d %H:%M:%S')
            try:
                imgs[date].append(x)
            except KeyError:
                imgs[date]=[x]
            #print('{} {}'.format(date.strftime('%Y-%m-%d'),x['seriesDescription']))
        #print(r['ParticipantId'])
        dates=[d.strftime('%Y-%m-%d') for d in imgs]
        print('{}: {}'.format(r['ParticipantId'],','.join(dates)))
        
            

listOrthanc(setup)
User: andrej studen CSRF: ea2151487c571c34c2860833e3685f60
8294/16: 2017-07-07
def copyDataset(modify=False):
    db,fb=connectDB('onko-nix')
    sourceProject='limfomiPET/Study'
    sourceQuery='SUVanalysis'
    targetProject='limfomiPET/Study'
    targetQuery='SUVanalysisBackup'
    
    #prepare (and clear) target
    ds=db.selectRows(targetProject,'study',targetQuery,[])
    rows=ds['rows']
    print('Deleting {} rows'.format(len(rows)))
    if modify:
        db.modifyRows('delete',targetProject,'study',targetQuery,rows)
        
    #copy from source
    ds=db.selectRows(sourceProject,'study',sourceQuery,[])
    rows=ds['rows']
    #insert to target
    print('Inserting {} rows'.format(len(rows)))
    if modify:
        db.modifyRows('insert',targetProject,'study',targetQuery,rows)
        
copyDataset(True)
    
User: andrej studen CSRF: 3fd61436be2b1f4f01f9c5be197d96eb
Deleting 0 rows
User: andrej studen CSRF: 4bb37bc5125387f1a054d520f1a6333e
Inserting 1400 rows
User: andrej studen CSRF: 9099dbaacb01d92900e4eb39b899a398
import SimpleITK
import numpy
def getSegmentation():
    db,fb=connectDB('onko-nix')
    p='1965/16'
    p='2763/07'
    #p='4464/16'
    pcode=p.replace('/','_')
    idFilter={'variable':'ParticipantId','value':p,'oper':'eq'}
    v='VISIT_1'
    visitFilter={'variable':'visitCode','value':v,'oper':'eq'}
    user='1037'
    userFilter={'variable':'User','value':user,'oper':'eq'}
    project='limfomiPET/Study'
    ds=db.selectRows(project,'study','Segmentations',[idFilter,visitFilter,userFilter])
    for r in ds['rows']:
        url=fb.buildPathURL(project,['preprocessedImages',pcode,v,'Segmentations'])
        remoteURL='/'.join([url,r['latestFile']])
        print(fb.entryExists(remoteURL))
        localPath=os.path.join(os.path.expanduser('~'),'Downloads',r['latestFile'])
        fb.readFileToFile(remoteURL,localPath)
        im=SimpleITK.ReadImage(localPath)
        ar=SimpleITK.GetArrayFromImage(im)
        for seg in [1,2,4]:
            print('{} {}: {}'.format(r['latestFile'],seg,numpy.sum(ar==seg)))
        
getSegmentation()
User: andrej studen CSRF: 4762143c71425ffc69605ce515e80d93
True
Segmentation_2763_07-VISIT_1_adoma_220.nrrd 1: 5394
Segmentation_2763_07-VISIT_1_adoma_220.nrrd 2: 0
Segmentation_2763_07-VISIT_1_adoma_220.nrrd 4: 86125