addSegmentations.py 3.7 KB

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