|
@@ -0,0 +1,110 @@
|
|
|
+import os
|
|
|
+import json
|
|
|
+import re
|
|
|
+import subprocess
|
|
|
+import nibabel
|
|
|
+import shutil
|
|
|
+import sys
|
|
|
+
|
|
|
+shome=os.path.expanduser('~nixUser')
|
|
|
+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"]["labkeyInterface"])
|
|
|
+import labkeyInterface
|
|
|
+import labkeyDatabaseBrowser
|
|
|
+import labkeyFileBrowser
|
|
|
+
|
|
|
+#sys.path.insert(1,shome+'/software/src/IPNUMM/dicomUtils')
|
|
|
+#import loadDicom
|
|
|
+
|
|
|
+fconfig=os.path.join(fhome,'.labkey','network.json')
|
|
|
+
|
|
|
+net=labkeyInterface.labkeyInterface()
|
|
|
+net.init(fconfig)
|
|
|
+db=labkeyDatabaseBrowser.labkeyDB(net)
|
|
|
+fb=labkeyFileBrowser.labkeyFileBrowser(net)
|
|
|
+
|
|
|
+project='iPNUMMretro/Study'
|
|
|
+dataset='Imaging1'
|
|
|
+tempBase=os.path.join(fhome,'temp')
|
|
|
+
|
|
|
+#all images from database
|
|
|
+ds=db.selectRows(project,'study',dataset,[])
|
|
|
+#imageSelector={"CT":"CT","PET":"PETWB"};
|
|
|
+imageResampledField={"Segm":"Segmentation"}
|
|
|
+
|
|
|
+#projectNIfTIBase=os.path.join(labkeyBase,'files',project,'@files/nifti')
|
|
|
+#use webdav to transfer file (even though it is localhost)
|
|
|
+
|
|
|
+
|
|
|
+def getPatientLabel(row):
|
|
|
+ return row['PatientId'].replace('/','_')
|
|
|
+
|
|
|
+def getVisitLabel(row):
|
|
|
+ return 'VISIT_'+str(int(row['SequenceNum']))
|
|
|
+
|
|
|
+def getStudyLabel(row):
|
|
|
+ return getPatientLabel(row)+'-'+getVisitLabel(row)
|
|
|
+
|
|
|
+def updateRow(project,dataset,row,imageResampledField,gzFileNames):
|
|
|
+ for im in imageResampledField:
|
|
|
+ row[imageResampledField[im]]=gzFileNames[im]
|
|
|
+ db.modifyRows('update',project,'study',dataset,[row])
|
|
|
+
|
|
|
+i=0
|
|
|
+for row in ds["rows"]:
|
|
|
+
|
|
|
+ #interesting files are processedDir/studyName_CT_notCropped_2mmVoxel.nii
|
|
|
+ #asn processedDir/studyName_PET_notCropped_2mmVoxel.nii
|
|
|
+ gzFileNames={im:\
|
|
|
+ getStudyLabel(row)+'_'+im+'.nii.gz'\
|
|
|
+ for im in imageResampledField}
|
|
|
+
|
|
|
+ #build/check remote directory structure
|
|
|
+ remoteDir=fb.buildPathURL(project,\
|
|
|
+ ['preprocessedImages',getPatientLabel(row),getVisitLabel(row)])
|
|
|
+
|
|
|
+ gzRemoteFiles={im:remoteDir+'/'+f\
|
|
|
+ for (im,f) in gzFileNames.items()}
|
|
|
+
|
|
|
+ remoteFilePresent=[fb.entryExists(f)\
|
|
|
+ for f in gzRemoteFiles.values()]
|
|
|
+
|
|
|
+ for f in gzRemoteFiles.values():
|
|
|
+ print("[{}]: [{}]".format(f,fb.entryExists(f)))
|
|
|
+
|
|
|
+
|
|
|
+ if all(remoteFilePresent):
|
|
|
+ print("Entry for row done.")
|
|
|
+ updateRow(project,dataset,row,imageResampledField,\
|
|
|
+ gzFileNames)
|
|
|
+ continue
|
|
|
+
|
|
|
+ inputDir=fb.buildPathURL(project,['segmentations'])
|
|
|
+ inputFiles={im:inputDir+'/'+f for (im,f) in gzFileNames.items()}
|
|
|
+
|
|
|
+ for im in inputFiles:
|
|
|
+ f=inputFiles[im]
|
|
|
+ if not fb.entryExists(f):
|
|
|
+ print("Input file {} not found".format(f))
|
|
|
+ continue
|
|
|
+ print("Found {}".format(f))
|
|
|
+ localFile=os.path.join(tempBase,gzFileNames[im])
|
|
|
+ print("Local {}".format(localFile))
|
|
|
+ fb.readFileToFile(f,localFile)
|
|
|
+ fb.writeFileToFile(localFile,gzRemoteFiles[im])
|
|
|
+ print("Remote {}".format(gzRemoteFiles[im]))
|
|
|
+ os.remove(localFile)
|
|
|
+
|
|
|
+ #update row and let it know where the processed files are
|
|
|
+ updateRow(project,dataset,row,imageResampledField,gzFileNames)
|
|
|
+
|
|
|
+
|
|
|
+ if i==-1:
|
|
|
+ break
|
|
|
+ i=i+1
|
|
|
+
|
|
|
+
|
|
|
+print("Done")
|