123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import os
- import json
- import re
- import subprocess
- import nibabel
- import shutil
- import sys
- #nothing gets done if you do import
- def getPatientLabel(row,participantField='PatientId'):
- return row[participantField].replace('/','_')
- def getVisitLabel(row):
- return 'VISIT_'+str(int(row['SequenceNum']))
- def getStudyLabel(row,participantField='PatientId'):
- return getPatientLabel(row,participantField)+'-'+getVisitLabel(row)
- def updateRow(project,dataset,row,imageResampledField,gzFileNames,\
- participantField='PatientId'):
- row['patientCode']=getPatientLabel(row,participantField)
- row['visitCode']=getVisitLabel(row)
- for im in imageResampledField:
- row[imageResampledField[im]]=gzFileNames[im]
- db.modifyRows('update',project,'study',dataset,[row])
-
- def replacePatterns(infile,outfile,replacePatterns):
- of=open(outfile,'w')
- with open(infile,'r') as f:
- data=f.read()
- for p in replacePatterns:
- val=replacePatterns[p]
- data=re.sub(p,val,data)
- of.write(data)
- of.close()
-
- def valueSubstitution(pars,val):
- if val.find('__home__')>-1:
- val=re.sub(r'__home__',os.path.expanduser('~'),val)
- return path
- def main(parameterFile):
-
- fhome=os.path.expanduser('~')
- with open(os.path.join(fhome,".labkey","setup.json")) as f:
- setup=json.load(f)
- sys.path.insert(0,setup["paths"]["labkeyInterface"])
- import labkeyInterface
- import labkeyDatabaseBrowser
- import labkeyFileBrowser
- sys.path.append(setup['paths']['parseConfig'])
- import parseConfig
- fconfig=os.path.join(fhome,'.labkey','network.json')
- net=labkeyInterface.labkeyInterface()
- net.init(fconfig)
- db=labkeyDatabaseBrowser.labkeyDB(net)
- fb=labkeyFileBrowser.labkeyFileBrowser(net)
- with open(parameterFile) as f:
- pars=json.load(f)
- pars=parseConfig.convert(pars)
- pars=parseConfig.convertValues(pars)
- hi=0
- project=pars['project']
- dataset=pars['targetQuery']
- schema=pars['targetSchema']
- tempBase=pars['tempBase']
- if not os.path.isdir(tempBase):
- os.makedirs(tempBase)
- participantField=pars['participantField']
- #all images from database
- ds=db.selectRows(project,schema,dataset,[])
-
- #imageSelector={"CT":"CT","PET":"PETWB_orthancId"}
- #input
- images=pars['images']
- #use webdav to transfer file (even though it is localhost)
- tempNames={im:os.path.join(tempBase,images[im]['tempFile']) for im in images}
-
- #update the config
- cfg=pars['deepmedic']['config']
- for c in cfg:
- replacePatterns(cfg[c]['template'],\
- cfg[c]['out'],\
- pars['replacePattern'])
- i=0
- for row in ds["rows"]:
-
- #download to temp file (could be a fixed name)
- baseDir=fb.formatPathURL(project,pars['imageDir']+'/'+\
- getPatientLabel(row,participantField)+'/'+\
- getVisitLabel(row))
- for im in images:
- fb.readFileToFile(baseDir+'/'+row[images[im]['queryField']],
- os.path.join(tempBase,images[im]['tempFile']))
-
- break
- i=i+1
- print("Done")
- if __name__ == '__main__':
- main(sys.argv[1])
- #sys.exit()
|