addSegmentations.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import os
  2. import json
  3. import re
  4. import subprocess
  5. import nibabel
  6. import shutil
  7. import sys
  8. shome=os.path.expanduser('~nixUser')
  9. fhome=os.path.expanduser('~')
  10. with open(os.path.join(fhome,".labkey","setup.json")) as f:
  11. setup=json.load(f)
  12. sys.path.insert(0,setup["paths"]["labkeyInterface"])
  13. import labkeyInterface
  14. import labkeyDatabaseBrowser
  15. import labkeyFileBrowser
  16. #sys.path.insert(1,shome+'/software/src/IPNUMM/dicomUtils')
  17. #import loadDicom
  18. fconfig=os.path.join(fhome,'.labkey','network.json')
  19. net=labkeyInterface.labkeyInterface()
  20. net.init(fconfig)
  21. db=labkeyDatabaseBrowser.labkeyDB(net)
  22. fb=labkeyFileBrowser.labkeyFileBrowser(net)
  23. project='iPNUMMretro/Study'
  24. dataset='Imaging1'
  25. tempBase=os.path.join(fhome,'temp')
  26. #all images from database
  27. ds=db.selectRows(project,'study',dataset,[])
  28. #imageSelector={"CT":"CT","PET":"PETWB"};
  29. imageResampledField={"Segm":"Segmentation"}
  30. #projectNIfTIBase=os.path.join(labkeyBase,'files',project,'@files/nifti')
  31. #use webdav to transfer file (even though it is localhost)
  32. def getPatientLabel(row):
  33. return row['PatientId'].replace('/','_')
  34. def getVisitLabel(row):
  35. return 'VISIT_'+str(int(row['SequenceNum']))
  36. def getStudyLabel(row):
  37. return getPatientLabel(row)+'-'+getVisitLabel(row)
  38. def updateRow(project,dataset,row,imageResampledField,gzFileNames):
  39. for im in imageResampledField:
  40. row[imageResampledField[im]]=gzFileNames[im]
  41. db.modifyRows('update',project,'study',dataset,[row])
  42. i=0
  43. for row in ds["rows"]:
  44. #interesting files are processedDir/studyName_CT_notCropped_2mmVoxel.nii
  45. #asn processedDir/studyName_PET_notCropped_2mmVoxel.nii
  46. gzFileNames={im:\
  47. getStudyLabel(row)+'_'+im+'.nii.gz'\
  48. for im in imageResampledField}
  49. #build/check remote directory structure
  50. remoteDir=fb.buildPathURL(project,\
  51. ['preprocessedImages',getPatientLabel(row),getVisitLabel(row)])
  52. gzRemoteFiles={im:remoteDir+'/'+f\
  53. for (im,f) in gzFileNames.items()}
  54. remoteFilePresent=[fb.entryExists(f)\
  55. for f in gzRemoteFiles.values()]
  56. for f in gzRemoteFiles.values():
  57. print("[{}]: [{}]".format(f,fb.entryExists(f)))
  58. if all(remoteFilePresent):
  59. print("Entry for row done.")
  60. updateRow(project,dataset,row,imageResampledField,\
  61. gzFileNames)
  62. continue
  63. inputDir=fb.buildPathURL(project,['segmentations'])
  64. inputFiles={im:inputDir+'/'+f for (im,f) in gzFileNames.items()}
  65. for im in inputFiles:
  66. f=inputFiles[im]
  67. if not fb.entryExists(f):
  68. print("Input file {} not found".format(f))
  69. continue
  70. print("Found {}".format(f))
  71. localFile=os.path.join(tempBase,gzFileNames[im])
  72. print("Local {}".format(localFile))
  73. fb.readFileToFile(f,localFile)
  74. fb.writeFileToFile(localFile,gzRemoteFiles[im])
  75. print("Remote {}".format(gzRemoteFiles[im]))
  76. os.remove(localFile)
  77. #update row and let it know where the processed files are
  78. updateRow(project,dataset,row,imageResampledField,gzFileNames)
  79. if i==-1:
  80. break
  81. i=i+1
  82. print("Done")