runStat.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import statUtils
  2. import os
  3. import radiomics
  4. import SimpleITK
  5. import sys
  6. import json
  7. def main(parFile='../templates/statistics.json'):
  8. setup=statUtils.loadSetup(parFile)
  9. rFile='radiomics.json'
  10. with open(rFile,'w') as f:
  11. f.write(json.dumps(setup['radiomics']))
  12. setup['db'],setup['fb']=statUtils.connectDB('onko-nix')
  13. users=statUtils.getUsers(setup['db'],setup['project'])
  14. qFilter=[]
  15. try:
  16. vList=';'.join(setup['participants'])
  17. qFilter.append({'variable':'ParticipantId','value':vList,'oper':'in'})
  18. except KeyError:
  19. pass
  20. ds=setup['db'].selectRows(setup['project'],'study',setup['imagingDataset'],qFilter)
  21. if not os.path.isdir(setup['localDir']):
  22. os.mkdir(setup['localDir'])
  23. #select just the first row; debugging
  24. rows=ds['rows']
  25. setup['values']=['COM','MTV','TLG','SUVmean','SUVmax']
  26. #params=os.path.join('..','templates','radiomics.yaml')
  27. setup['featureExtractor']=radiomics.featureextractor.RadiomicsFeatureExtractor(rFile)
  28. for r in rows:
  29. print(r)
  30. for q in ['petResampled']:
  31. localPath=statUtils.getImage(setup,r,q)
  32. if localPath=="NONE":
  33. continue
  34. segPaths=statUtils.getSegmentations(setup,r)
  35. if "NONE" in segPaths.values():
  36. os.remove(localPath)
  37. continue
  38. segKeys=list(segPaths.keys())
  39. for x in segPaths:
  40. print('Loaded {}/{}'.format(users[x],segPaths[x]))
  41. pet=SimpleITK.ReadImage(localPath)
  42. seg={x:SimpleITK.ReadImage(segPaths[x]) for x in segPaths}
  43. #find labels associated with each (non-overlaping) segmentation
  44. ids=statUtils.getSegments(list(seg.values())[0])
  45. for id in ids:
  46. print('{} {}'.format(id,ids[id]))
  47. for x in seg:
  48. try:
  49. output=statUtils.getRadiomicsComponentStats(setup,pet,seg[x],ids[id])
  50. except ValueError:
  51. continue
  52. output.update({x:r[x] for x in ['ParticipantId','SequenceNum','patientCode','visitCode']})
  53. output['User']=x
  54. output['segment']=ids[id]
  55. statUtils.updateDatasetRows(setup['db'],setup['project'],setup['SUVdataset'],[output])
  56. #print(output)
  57. #cleanup
  58. for x in segPaths:
  59. os.remove(segPaths[x])
  60. os.remove(localPath)
  61. os.remove(rFile)
  62. if __name__=='__main__':
  63. main(sys.argv[1])