12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import statUtils
- import os
- import radiomics
- import SimpleITK
- import sys
- import json
- def main(parFile='../templates/statistics.json'):
- setup=statUtils.loadSetup(parFile)
- rFile='radiomics.json'
- with open(rFile,'w') as f:
- f.write(json.dumps(setup['radiomics']))
- setup['db'],setup['fb']=statUtils.connectDB('onko-nix')
- users=statUtils.getUsers(setup['db'],setup['project'])
- qFilter=[]
- try:
- vList=';'.join(setup['participants'])
- qFilter.append({'variable':'ParticipantId','value':vList,'oper':'in'})
- except KeyError:
- pass
- ds=setup['db'].selectRows(setup['project'],'study',setup['imagingDataset'],qFilter)
- if not os.path.isdir(setup['localDir']):
- os.mkdir(setup['localDir'])
- #select just the first row; debugging
- rows=ds['rows']
- setup['values']=['COM','MTV','TLG','SUVmean','SUVmax']
- #params=os.path.join('..','templates','radiomics.yaml')
- setup['featureExtractor']=radiomics.featureextractor.RadiomicsFeatureExtractor(rFile)
- for r in rows:
- print(r)
- for q in ['petResampled']:
- localPath=statUtils.getImage(setup,r,q)
- if localPath=="NONE":
- continue
- segPaths=statUtils.getSegmentations(setup,r)
- if "NONE" in segPaths.values():
- os.remove(localPath)
- continue
- segKeys=list(segPaths.keys())
- for x in segPaths:
- print('Loaded {}/{}'.format(users[x],segPaths[x]))
- pet=SimpleITK.ReadImage(localPath)
- seg={x:SimpleITK.ReadImage(segPaths[x]) for x in segPaths}
- #find labels associated with each (non-overlaping) segmentation
- ids=statUtils.getSegments(list(seg.values())[0])
- for id in ids:
- print('{} {}'.format(id,ids[id]))
- for x in seg:
- try:
- output=statUtils.getRadiomicsComponentStats(setup,pet,seg[x],ids[id])
- except ValueError:
- continue
- output.update({x:r[x] for x in ['ParticipantId','SequenceNum','patientCode','visitCode']})
- output['User']=x
- output['segment']=ids[id]
- statUtils.updateDatasetRows(setup['db'],setup['project'],setup['SUVdataset'],[output])
- #print(output)
- #cleanup
- for x in segPaths:
- os.remove(segPaths[x])
- os.remove(localPath)
- os.remove(rFile)
- if __name__=='__main__':
- main(sys.argv[1])
|