|
@@ -0,0 +1,123 @@
|
|
|
+import os
|
|
|
+import json
|
|
|
+import re
|
|
|
+import sys
|
|
|
+
|
|
|
+#nothing gets done if you do import
|
|
|
+
|
|
|
+def getPatientLabel(row,participantField='PatientId'):
|
|
|
+ return row[participantField].replace('/','_')
|
|
|
+
|
|
|
+def getVisitLabel(row):
|
|
|
+ return 'VISIT_'+str(int(row['SequenceNum']))
|
|
|
+
|
|
|
+def getStudyLabel(row,participantField='PatientId'):
|
|
|
+ return getPatientLabel(row,participantField)+'-'+getVisitLabel(row)
|
|
|
+
|
|
|
+def main(parameterFile):
|
|
|
+ fhome=os.path.expanduser('~')
|
|
|
+ with open(os.path.join(fhome,".labkey","setup.json")) as f:
|
|
|
+ setup=json.load(f)
|
|
|
+
|
|
|
+ sys.path.insert(0,setup["paths"]["nixWrapper"])
|
|
|
+ import nixWrapper
|
|
|
+
|
|
|
+ nixWrapper.loadLibrary("labkeyInterface")
|
|
|
+
|
|
|
+ import labkeyInterface
|
|
|
+ import labkeyDatabaseBrowser
|
|
|
+ import labkeyFileBrowser
|
|
|
+
|
|
|
+ fconfig=os.path.join(fhome,'.labkey','onko-nix.json')
|
|
|
+
|
|
|
+ net=labkeyInterface.labkeyInterface()
|
|
|
+ net.init(fconfig)
|
|
|
+ db=labkeyDatabaseBrowser.labkeyDB(net)
|
|
|
+ fb=labkeyFileBrowser.labkeyFileBrowser(net)
|
|
|
+
|
|
|
+ with open(parameterFile) as f:
|
|
|
+ pars=json.load(f)
|
|
|
+
|
|
|
+ hi=0
|
|
|
+ project=pars['Database']['project']
|
|
|
+ dataset=pars['Database']['queryName']
|
|
|
+ schema=pars['Database']['schemaName']
|
|
|
+
|
|
|
+ tempBase=os.path.join('/','data','nixUser','RIS')
|
|
|
+ if not os.path.isdir(tempBase):
|
|
|
+ os.mkdir(tempBase)
|
|
|
+
|
|
|
+
|
|
|
+ participantField=pars['Database']['participantField']
|
|
|
+ segmentation=pars['Database']['segementationQuery']
|
|
|
+
|
|
|
+ #all images from database
|
|
|
+ visitFilter={'variable':'visitCode','value':'VISIT_1','oper':'eq'}
|
|
|
+ iodineFilter={'variable':'iodineContrast','value':'0','oper':'eq'}
|
|
|
+
|
|
|
+ ds=db.selectRows(project,schema,dataset,[visitFilter,iodineFilter])
|
|
|
+ #imageSelector={"CT":"CT_orthancId","PET":"PETWB_orthancId"}
|
|
|
+ #output
|
|
|
+ imageResampledField={"CT":"ctResampled","PET":"petResampled","patientmask":"ROImask"}
|
|
|
+
|
|
|
+ #use webdav to transfer file (even though it is localhost)
|
|
|
+
|
|
|
+ i=0
|
|
|
+
|
|
|
+ n=len(ds['rows'])
|
|
|
+ keys=[r[participantField] for r in ds['rows']]
|
|
|
+ perm=numpy.random.permutation(n)
|
|
|
+ pseudo={keys[i]:perm[i] for i in range(n)}
|
|
|
+
|
|
|
+ for row in ds["rows"]:
|
|
|
+ print("Starting row id:{} seq:{}".format(row[participantField],row['SequenceNum']))
|
|
|
+ #interesting files are processedDir/studyName_CT_notCropped_2mmVoxel.nii
|
|
|
+ #asn processedDir/studyName_PET_notCropped_2mmVoxel.nii
|
|
|
+
|
|
|
+ idFilter={'variable':participantField,'value':row[participantField],'oper':'eq'}
|
|
|
+ segFilter={'variable':'SequenceNum','value':'{}'.format(row['SequenceNum']),'oper':'eq'}
|
|
|
+ ds=db.selectRows(project,schema,segmentation,[idFilter,segFilter])
|
|
|
+ nS=len(ds['rows'])
|
|
|
+ if nS==0:
|
|
|
+ print('No segmentation found')
|
|
|
+ continue
|
|
|
+ if nS>1:
|
|
|
+ print('Multiple segmentations found')
|
|
|
+ continue
|
|
|
+ maskField={'mask':'/'.join('Segmentations',ds['rows'][0]['latestFile'])}
|
|
|
+ imageResampleField.update(maskField)
|
|
|
+
|
|
|
+
|
|
|
+ #build/check remote directory structure
|
|
|
+ remoteDir=fb.buildPathURL(project,['preprocessedImages',\
|
|
|
+ getPatientLabel(row,participantField),getVisitLabel(row)])
|
|
|
+
|
|
|
+ gzRemoteFiles={x:'/'.join(remoteDir,row[imageResampleField[x]]) for x in imageResampleField}
|
|
|
+
|
|
|
+ for f in gzRemoteFiles.values():
|
|
|
+ print("[{}]: [{}]".format(f,fb.entryExists(f)))
|
|
|
+ localDir='patient{:03d}'.format(pseudo[row[participantField]])
|
|
|
+ localDir=os.path.join(tempBase,localDir)
|
|
|
+ if not os.path.isdir(localDir):
|
|
|
+ os.mkdir(localDir)
|
|
|
+ localFiles={x:os.path.join(localDir,'{}.nii.gz'.format(x)) for x in gzRemoteFiles}
|
|
|
+
|
|
|
+
|
|
|
+ if not all(remoteFilePresent):
|
|
|
+ print('Missing remote files')
|
|
|
+ continue
|
|
|
+
|
|
|
+ continue
|
|
|
+
|
|
|
+ _=[fb.readFileToFile(gzRemoteFiles[x],localFiles[x]) for x in localFiles]
|
|
|
+
|
|
|
+ if i==0:
|
|
|
+ break
|
|
|
+ i=i+1
|
|
|
+
|
|
|
+ print("Done")
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ main(sys.argv[1])
|
|
|
+
|