123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- import os
- import json
- import re
- def cmdMatlab():
- pwd=os.path.dirname(os.path.abspath(__file__))
- pwdUp=os.path.dirname(pwd)
- mDir=os.path.join(pwdUp,'matlab')
- with open(os.path.join(os.path.expanduser('~'),'.labkey','setup.json'),'r') as f:
- siteSetup=json.load(f)
- mExec=siteSetup['paths']['matlab']
- cmds=[mExec]
- cmds.append('-sd')
- cmds.append(mDir)
- #cmds.append('-batch')
- return cmds
-
- def getPatientId(row,xconfig):
- return row[getPatientField(xconfig)]
- def getPatientField(xconfig):
- return xconfig['ParticipantField']
- def getVisitField(xconfig):
- try:
- return xconfig['visitField']
- except KeyError:
- return 'visitName'
- def getVisitId(row,xconfig):
- return row[getVisitField(xconfig)]
- def getIdFilter(row,xconfig):
- return {'variable':getPatientField(xconfig),\
- 'value':getPatientId(row,xconfig),
- 'oper':'eq'}
- def getVisitFilter(row,xconfig):
- return {'variable':getVisitField(xconfig),\
- 'value':getVisitId(row,xconfig),
- 'oper':'eq'}
- def getFilter(xconfig):
- row={}
- fields={getPatientField(xconfig):getIdFilter,
- getVisitField(xconfig):getVisitFilter}
- qfilter=[]
- for f in fields:
- try:
- row[f]=xconfig[f]
- if xconfig[f]!='NONE':
- qfilter.append(fields[f](row,xconfig))
- except KeyError:
- continue
- return qfilter
- def getCode(row,xconfig):
- return '{}_{}'.format(getPatientId(row,xconfig),getVisitId(row,xconfig))
- def getTargetSeqNum(row,xconfig):
- if getVisitId(row,xconfig)=='OBR':
- return 2
- if getVisitId(row,xconfig)=='MIR1':
- return 3
- if getVisitId(row,xconfig)=='MIR2':
- return 4
- return 1
- def getPathList(row,xconfig):
- return [xconfig['baseDir'],getPatientId(row,xconfig),getVisitId(row,xconfig)]
- def getOutputDir(row,xconfig):
- return '/'.join(getPathList(row,xconfig))
- def getTempDir(xconfig):
- tempDir=xconfig['tempDir']
- pathList=tempDir.split('/')
- pathList.insert(0,os.path.expanduser('~'))
- return os.path.join(*pathList)
-
- def getLocalDir(row,xconfig):
- return os.path.join(getTempDir(xconfig),getCode(row,xconfig))
- def getNodeName(row,xconfig,mode,i=0):
- if mode=='CT':
- return getCode(row,xconfig)+'_CT'
- if mode=='NM':
- return '{}_Volume{}'.format(getCode(row,xconfig),i)
- return getCode(row,xconfig)+'_Dummy'
- def decode(code,xconfig):
- #invert code and return object equivalent to row with relevant
- #fields set that can be used in
- #getPatientId, getVisitId,getIdFilter,getVisitFilter
- #as an equivalent replacement for r
- values=code.split('_')
- fid=values[0]
- vid=values[1]
- return {getPatientField(xconfig):fid,getVisitField(xconfig):vid}
- #standardized file names
- def getPattern(ftype,code,nclass=0,ir=0,ic=0,fitType='global',qaName='fits',timepoint=0,iseg=0,qLambda=0,qLambdaC=0):
- qLambdaString='{:.3f}'.format(qLambda).replace('.','p')
- qLambdaCString='{:.3f}'.format(qLambdaC).replace('.','p')
- replacePatterns={
- '_code_':code,
- '_nclass_':nclass,
- '_ir_':ir,
- '_ic_':ic,
- '_fitType_':fitType,
- '_qaName_':qaName,
- '_timepoint_':timepoint,
- '_iseg_':iseg,
- '_qLambda_':qLambdaString,
- '_qLambdaC_':qLambdaCString}
- map={'CT':'_code__CT.nrrd',
- 'SPECT':'_code__Volume_timepoint_.nrrd',
- 'Dummy':'_code__Dummy.mcsv',
- 'centerMap':'_code___nclass___ir__centerMap.txt',
- 'centerWeight':'_code___nclass___ir__center_ic__centerWeight.nrrd',
- 'centerNRRD':'_code___nclass___ir__centerMap__qaName_.nrrd',
- 'center':'_code___nclass___ir__center_ic__center.txt',
- 'fitParFinal':'_code___nclass___ir___fitType__fitParFinal.txt',
- 'fitPNG':'_code___nclass___ir___fitType__centers_ic_.png',
- 'pixelFitParFinal':'_code___nclass___ir__Pixel__fitType__fitParFinal.txt',
- 'pixelFitPNG':'_code___nclass___ir__Pixel__fitType__centers_ic_.png',
- 'fitIVF':'_code___nclass___ir___qLambda__fitParIVF.txt',
- 'plotIVF':'_code___nclass___ir___qLambda__plotIVF__qaName_.png',
- 'fitCompartment':'_code___nclass___ir___qLambda__fitCompartment__iseg___qaName___qLambdaC_.txt',
- 'plotCompartment':'_code___nclass___ir___qLambda__plotCompartment__iseg___qaName___qLambdaC_.png'}
- w=map[ftype]
- for x in replacePatterns:
- w=re.sub(x,'{}'.format(replacePatterns[x]),w)
- return w
-
- #pixels corresponding to a particular c-means class
- def getCenterWeight(r,setup,nclass,ir,i):
- code=getCode(r,setup)
- return f'{code}_{nclass}_{ir}_center{i}_centerWeight.nrrd'
- #1D TAC for a particular group center
- def getCenter(r,setup,nclass,ir,i):
- code=getCode(r,setup)
- return f'{code}_{nclass}_{ir}_center{i}_center.txt'
- #all names for determination of TAC, obsolete
- def printRowCenterNames(r,setup):
- names=[]
- nr=setup['nr']
- nclass=setup['nclass']
- code=getCode(r,setup)
- #tempDir=config.getTempDir(setup)
- for nc in nclass:
- for j in range(nr):
- for i in range(nc):
- names.append(getCenter(r,setup,nc,j+1,i+1))
- names.append(getCenterWeight(r,setup,nc,j+1,i+1))
- return names
- #fit of a compartment
- def getFitParFinalName(code,nc,j,aType):
- fCode='{}_{}_{}'.format(code,nc,j+1)
- if aType!='global':
- fCode='{}_{}'.format(fCode,aType)
-
- return '{}_fitParFinal.txt'.format(fCode)
- #fit of the IVF
- def getFitIVFParName(r,setup,nc,j):
- code=getCode(r,setup)
- fCode='{}_{}_{}'.format(code,nc,j+1)
- return f'{fCode}_fitParIVF.txt'
- #status report of the IVF fit (three images)
- def getFitIVFReportPNG(r,setup,nc,j,name):
- code=getCode(r,setup)
- fCode=f'{code}_{nc}_{j+1}_fitParIVF_{name}.png'
- return fCode
- #
- def getFitPNGNames(code,nc,j,aType):
- if aType=='global':
- return []
- fCode='{}_{}_{}'.format(code,nc,j+1)
- #if aType!='global':
- fCode='{}_{}'.format(fCode,aType)
- names=[]
- for i in range(nc):
- names.append('{}_centers{}.png'.format(fCode,i+1))
- return names
- def printFitParFinalRowNames(r,setup,aType):
- names=[]
- nr=setup['nr']
- nclass=setup['nclass']
- code=getCode(r,setup)
- #tempDir=config.getTempDir(setup)
- for nc in nclass:
- for j in range(nr):
- names.append(getFitParFinalName(code,nc,j,aType))
- #add pngs of the fit (good QA)
- #this are only partial fits, from which global parameters are taken
- names+=getFitPNGNames(code,nc,j,aType)
- return names
-
- def getPixelFitParFinalName(code,nc,s2,mode):
- sigmaCode='{:.2f}'.format(s2)
- sigmaCode=re.sub('\.','p',sigmaCode)
- fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
- if mode!='general':
- fName='{}{}'.format(fName,mode)
- fName='{}_fitParFinal.txt'.format(fName)
- return fName
- def getPixelFitParFinalPngName(code,nc,i,s2,mode):
- sigmaCode='{:.2f}'.format(s2)
- sigmaCode=re.sub('\.','p',sigmaCode)
- fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
- if mode!='general':
- fName='{}{}'.format(fName,mode)
- fName='{}_centers{}.png'.format(fName,i)
- return fName
- def printPixelFitParFinalRowNames(r,setup,nc,s2,mode):
- names=[]
- code=getCode(r,setup)
- names.append(getPixelFitParFinalName(code,nc,s2,mode))
- for i in range(nc):
- names.append(getPixelFitParFinalPngName(code,nc,i+1,s2,mode))
- return names
-
|