runSegmentation.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import os
  2. import json
  3. import re
  4. import subprocess
  5. import nibabel
  6. import shutil
  7. import sys
  8. #nothing gets done if you do import
  9. def getPatientLabel(row,participantField='PatientId'):
  10. return row[participantField].replace('/','_')
  11. def getVisitLabel(row):
  12. return 'VISIT_'+str(int(row['SequenceNum']))
  13. def getStudyLabel(row,participantField='PatientId'):
  14. return getPatientLabel(row,participantField)+'-'+getVisitLabel(row)
  15. def updateRow(project,dataset,row,imageResampledField,gzFileNames,\
  16. participantField='PatientId'):
  17. row['patientCode']=getPatientLabel(row,participantField)
  18. row['visitCode']=getVisitLabel(row)
  19. for im in imageResampledField:
  20. row[imageResampledField[im]]=gzFileNames[im]
  21. db.modifyRows('update',project,'study',dataset,[row])
  22. def replacePatterns(infile,outfile,replacePatterns):
  23. of=open(outfile,'w')
  24. with open(infile,'r') as f:
  25. data=f.read()
  26. for p in replacePatterns:
  27. val=replacePatterns[p]
  28. data=re.sub(p,val,data)
  29. of.write(data)
  30. of.close()
  31. def valueSubstitution(pars,val):
  32. if val.find('__home__')>-1:
  33. val=re.sub(r'__home__',os.path.expanduser('~'),val)
  34. return path
  35. def main(parameterFile):
  36. fhome=os.path.expanduser('~')
  37. with open(os.path.join(fhome,".labkey","setup.json")) as f:
  38. setup=json.load(f)
  39. sys.path.insert(0,setup["paths"]["labkeyInterface"])
  40. import labkeyInterface
  41. import labkeyDatabaseBrowser
  42. import labkeyFileBrowser
  43. sys.path.append(setup['paths']['parseConfig'])
  44. import parseConfig
  45. fconfig=os.path.join(fhome,'.labkey','network.json')
  46. net=labkeyInterface.labkeyInterface()
  47. net.init(fconfig)
  48. db=labkeyDatabaseBrowser.labkeyDB(net)
  49. fb=labkeyFileBrowser.labkeyFileBrowser(net)
  50. with open(parameterFile) as f:
  51. pars=json.load(f)
  52. pars=parseConfig.convert(pars)
  53. pars=parseConfig.convertValues(pars)
  54. hi=0
  55. project=pars['project']
  56. dataset=pars['targetQuery']
  57. schema=pars['targetSchema']
  58. tempBase=pars['tempBase']
  59. if not os.path.isdir(tempBase):
  60. os.makedirs(tempBase)
  61. participantField=pars['participantField']
  62. #all images from database
  63. ds=db.selectRows(project,schema,dataset,[])
  64. #imageSelector={"CT":"CT","PET":"PETWB_orthancId"}
  65. #input
  66. images=pars['images']
  67. #use webdav to transfer file (even though it is localhost)
  68. tempNames={im:os.path.join(tempBase,images[im]['tempFile']) for im in images}
  69. #update the config
  70. cfg=pars['deepmedic']['config']
  71. for c in cfg:
  72. replacePatterns(cfg[c]['template'],\
  73. cfg[c]['out'],\
  74. pars['replacePattern'])
  75. i=0
  76. for row in ds["rows"]:
  77. #download to temp file (could be a fixed name)
  78. baseDir=fb.formatPathURL(project,pars['imageDir']+'/'+\
  79. getPatientLabel(row,participantField)+'/'+\
  80. getVisitLabel(row))
  81. for im in images:
  82. fb.readFileToFile(baseDir+'/'+row[images[im]['queryField']],
  83. os.path.join(tempBase,images[im]['tempFile']))
  84. break
  85. i=i+1
  86. print("Done")
  87. if __name__ == '__main__':
  88. main(sys.argv[1])
  89. #sys.exit()