addSegmentations.py 3.8 KB

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