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