|
@@ -0,0 +1,407 @@
|
|
|
+import config
|
|
|
+import getData
|
|
|
+import loadData
|
|
|
+import fitData
|
|
|
+import numpy
|
|
|
+import segmentation
|
|
|
+import plotData
|
|
|
+import os
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def listRequiredFiles(stage,r,setup):
|
|
|
+ code=config.getCode(r,setup)
|
|
|
+ nclass=setup['nclass'][0]
|
|
|
+ nr=setup['nr']
|
|
|
+ nt=20
|
|
|
+ if stage=='setCenters':
|
|
|
+ names={x:[config.getPattern(x,code)] for x in ['CT','Dummy']}
|
|
|
+ names['SPECT']=[config.getPattern('SPECT',code=code,timepoint=i) for i in range(nt)]
|
|
|
+ return names
|
|
|
+ if stage=='fitIVF':
|
|
|
+ names={x:[config.getPattern(x,code)] for x in ['Dummy']}
|
|
|
+ names['center']=[]
|
|
|
+ for ir in range(nr):
|
|
|
+ rel=[config.getPattern('center',code=code,nclass=nclass,ir=ir,ic=i) for i in range(nclass)]
|
|
|
+ names['center'].extend(rel)
|
|
|
+ return names
|
|
|
+ if stage=='plotIVF':
|
|
|
+ names={x:[config.getPattern(x,code)] for x in ['Dummy']}
|
|
|
+ names['center']=[]
|
|
|
+ names['fitIVF']=[]
|
|
|
+ qLambda=setup['qLambda']
|
|
|
+ for ir in range(nr):
|
|
|
+ rel=[config.getPattern('center',code=code,nclass=nclass,ir=ir,ic=i) for i in range(nclass)]
|
|
|
+ names['center'].extend(rel)
|
|
|
+ names['fitIVF'].append(config.getPattern('fitIVF',code=code,nclass=nclass,ir=ir,qLambda=qLambda))
|
|
|
+
|
|
|
+ rel=[config.getPattern('centerNRRD',code=code,nclass=nclass,ir=ir,qaName=x) for x in ['CT','SPECT']]
|
|
|
+ names['center'].extend(rel)
|
|
|
+
|
|
|
+
|
|
|
+ names.update({x:[config.getPattern(x,code)] for x in ['CT']})
|
|
|
+ names['SPECT']=[config.getPattern('SPECT',code=code,timepoint=i) for i in range(nt)]
|
|
|
+ return names
|
|
|
+
|
|
|
+ if stage=='fitCompartment':
|
|
|
+ names={}
|
|
|
+ names['center']=[]
|
|
|
+ names['fitIVF']=[]
|
|
|
+ qLambda=setup['qLambda']
|
|
|
+ for ir in range(nr):
|
|
|
+ rel=[config.getPattern('center',code=code,nclass=nclass,ir=ir,ic=i) for i in range(nclass)]
|
|
|
+ names['center'].extend(rel)
|
|
|
+ names['fitIVF'].append(config.getPattern('fitIVF',code=code,nclass=nclass,ir=ir,qLambda=qLambda))
|
|
|
+ names['center'].append(config.getPattern('centerMap',code=code,nclass=nclass,ir=ir))
|
|
|
+ names['segmentation']=[segmentation.getSegmentationFileName(r,setup)]
|
|
|
+ return names
|
|
|
+
|
|
|
+ if stage=='plotCompartment':
|
|
|
+ names={}
|
|
|
+ names['center']=[]
|
|
|
+ names['fitIVF']=[]
|
|
|
+ names['fitCompartment']=[]
|
|
|
+ nseg=setup['nseg']
|
|
|
+ qLambda=setup['qLambda']
|
|
|
+ for ir in range(nr):
|
|
|
+ rel=[config.getPattern('center',code=code,nclass=nclass,ir=ir,ic=i) for i in range(nclass)]
|
|
|
+ names['center'].extend(rel)
|
|
|
+ names['fitIVF'].append(config.getPattern('fitIVF',code=code,nclass=nclass,ir=ir,qLambda=qLambda))
|
|
|
+ names['center'].append(config.getPattern('centerMap',code=code,nclass=nclass,ir=ir))
|
|
|
+ for iseg in range(nseg):
|
|
|
+ rel=[config.getPattern(xc,code=code,nclass=nclass,ir=ir,qaName=qn,iseg=iseg,qLambda=qLambda) for qn in sNames]
|
|
|
+ names['fitCompartment'].extend(rel)
|
|
|
+ names['segmentation']=[segmentation.getSegmentationFileName(r,setup)]
|
|
|
+ names.update({x:[config.getPattern(x,code)] for x in ['CT','Dummy']})
|
|
|
+ names['SPECT']=[config.getPattern('SPECT',code=code,timepoint=i) for i in range(nt)]
|
|
|
+ return names
|
|
|
+
|
|
|
+ return {}
|
|
|
+
|
|
|
+def listCreatedFiles(stage,r,setup):
|
|
|
+ code=config.getCode(r,setup)
|
|
|
+ nclass=setup['nclass'][0]
|
|
|
+ qLambda=setup['qLambda']
|
|
|
+ nr=setup['nr']
|
|
|
+ try:
|
|
|
+ nseg=setup['nseg']
|
|
|
+ except KeyError:
|
|
|
+ nseg=0
|
|
|
+ names={}
|
|
|
+
|
|
|
+ if stage=='setCenters':
|
|
|
+ names['center']=[]
|
|
|
+ for ir in range(nr):
|
|
|
+ rel=[config.getPattern('center',code=code,nclass=nclass,ir=ir,ic=i) for i in range(nclass)]
|
|
|
+ names['center'].extend(rel)
|
|
|
+
|
|
|
+
|
|
|
+ names['center'].append(config.getPattern('centerMap',code=code,nclass=nclass,ir=ir))
|
|
|
+ rel=[config.getPattern('centerNRRD',code=code,nclass=nclass,ir=ir,qaName=x) for x in ['CT','SPECT']]
|
|
|
+ names['center'].extend(rel)
|
|
|
+
|
|
|
+ return names
|
|
|
+ if stage=='fitIVF':
|
|
|
+ names['fitIVF']=[]
|
|
|
+ for ir in range(nr):
|
|
|
+ names['fitIVF'].append(config.getPattern('fitIVF',code=code,nclass=nclass,ir=ir,qLambda=qLambda))
|
|
|
+ return names
|
|
|
+ if stage=='plotIVF':
|
|
|
+ names['plotIVF']=[]
|
|
|
+ for ir in range(nr):
|
|
|
+ x=[config.getPattern('plotIVF',code=code,nclass=nclass,ir=ir,qaName=y,qLambda=qLambda)
|
|
|
+ for y in ['fits','diff','generatedIVF','centerIVFSPECT','centerIVFCT']]
|
|
|
+ names['plotIVF'].extend(x)
|
|
|
+ return names
|
|
|
+ if stage=='fitCompartment':
|
|
|
+ xc='fitCompartment'
|
|
|
+ names[xc]=[]
|
|
|
+ sNames=['kmeansFit','localFit','kmeansTAC','localTAC']
|
|
|
+ for ir in range(nr):
|
|
|
+ for iseg in range(nseg):
|
|
|
+ rel=[config.getPattern(xc,code=code,nclass=nclass,ir=ir,qaName=qn,iseg=iseg,qLambda=qLambda) for qn in sNames]
|
|
|
+ names[xc].extend(rel)
|
|
|
+ if stage=='plotCompartment':
|
|
|
+ xc='plotCompartment'
|
|
|
+ names[xc]=[]
|
|
|
+ sNames=['realizations','diff']
|
|
|
+ for ir in range(nr):
|
|
|
+ for iseg in range(nseg):
|
|
|
+ rel=[config.getPattern(xc,code=code,nclass=nclass,ir=ir,qaName=qn,iseg=iseg,qLambda=qLambda) for qn in sNames]
|
|
|
+ names[xc].extend(rel)
|
|
|
+
|
|
|
+ return []
|
|
|
+
|
|
|
+
|
|
|
+def getRequiredFiles(stage,r,setup,fb,names=None):
|
|
|
+
|
|
|
+ if not names:
|
|
|
+ names=listRequiredFiles(stage,r,setup)
|
|
|
+ for f in names:
|
|
|
+ _copyFromServer=getData.copyFromServer
|
|
|
+ if f=='segmentation':
|
|
|
+ _copyFromServer=segmentation.copyFromServer
|
|
|
+ _copyFromServer(fb,r,setup,names[f])
|
|
|
+ return fb,r
|
|
|
+
|
|
|
+def checkRequiredFiles(stage,r,setup,names=None,fb=None,doPrint=False):
|
|
|
+ ok=True
|
|
|
+ if not names:
|
|
|
+ names=listRequiredFiles(stage,r,setup)
|
|
|
+ for f in names:
|
|
|
+ nm=names[f]
|
|
|
+ for x in nm:
|
|
|
+ avail=os.path.isfile(getData.getLocalPath(r,setup,x))
|
|
|
+ if not avail:
|
|
|
+ print(f'Missing {x}')
|
|
|
+ if fb:
|
|
|
+ _getURL=getData.getURL
|
|
|
+ if f=='segmentation':
|
|
|
+ _getURL=segmentation.getURL
|
|
|
+ availRemote=fb.entryExists(_getURL(fb,r,setup,x))
|
|
|
+ print(f'Available remote: {availRemote}')
|
|
|
+ ok=False
|
|
|
+ if doPrint:
|
|
|
+ print(f'[{avail}] {x}')
|
|
|
+ return ok
|
|
|
+
|
|
|
+def uploadCreatedFiles(stage,fb,r,setup,names=None):
|
|
|
+ if not names:
|
|
|
+ names=listCreatedFiles(stage,r,setup)
|
|
|
+ for f in names:
|
|
|
+ _copyToServer=getData.copyToServer
|
|
|
+ _getURL=getData.getURL
|
|
|
+ if f=='segmentation':
|
|
|
+ _copyToServer=segmentation.copyToServer
|
|
|
+ _getURL=segmentation.getURL
|
|
|
+ _copyToServer(fb,r,setup,names[f])
|
|
|
+ for x in names[f]:
|
|
|
+ print('[{}] Uploaded {}'.format(fb.entryExists(_getURL(fb,r,setup,x)),x))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def makeMap(segs,kClass,tac):
|
|
|
+ map={}
|
|
|
+ vals=[(kClass[i],tac[:,i]) for i in range(len(kClass))]
|
|
|
+
|
|
|
+ for (i,v) in zip(segs,vals):
|
|
|
+ try:
|
|
|
+ map[i].append(v)
|
|
|
+ except KeyError:
|
|
|
+ map[i]=[v]
|
|
|
+ return map
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def updateDatabase(r,setup,stage,fb=None,db=None,categories=[]):
|
|
|
+
|
|
|
+ try:
|
|
|
+ qLam=setup['qLambda']
|
|
|
+ except KeyError:
|
|
|
+ qLam=0
|
|
|
+
|
|
|
+ nclass=setup['nclass'][0]
|
|
|
+ code=config.getCode(r,setup)
|
|
|
+ if stage=='plotIVF':
|
|
|
+ m,samples=loadData.readIVF(r,setup,qLambda=qLam)
|
|
|
+ chi2=samples[0,:]
|
|
|
+ threshold=numpy.median(chi2)
|
|
|
+ fit=fitData.getFit(samples,threshold)
|
|
|
+ row={x:r[x] for x in ['PatientId','visitCode']}
|
|
|
+ row['nclass']=nclass
|
|
|
+ row['mean']=fit.mu[0]
|
|
|
+ row['std']=fit.cov[0,0]
|
|
|
+ row['qLambda']=qLam
|
|
|
+
|
|
|
+ fNames={x:config.getPattern('plotIVF',code=code,ir=0,nclass=nclass,qaName=x,qLambda=qLam) for x in categories}
|
|
|
+ row.update(fNames)
|
|
|
+ if db:
|
|
|
+ db.modifyRows('insert',setup['project'],'lists','SummaryIVF',[row])
|
|
|
+
|
|
|
+
|
|
|
+def workflow(r,setup,stage,fb=None,db=None):
|
|
|
+ setCenters=False
|
|
|
+ setIVF=False
|
|
|
+ plotIVF=False
|
|
|
+ setC=True
|
|
|
+ try:
|
|
|
+ qLambda=setup['qLambda']
|
|
|
+ except KeyError:
|
|
|
+ qLambda=0
|
|
|
+
|
|
|
+ if stage=='setCenters':
|
|
|
+ names=listRequiredFiles(stage,r,setup)
|
|
|
+ if fb:
|
|
|
+ getRequiredFiles(stage,r,setup,fb,names=names)
|
|
|
+ if not checkRequiredFiles(stage,r,setup,names=names,fb=fb,doPrint=True):
|
|
|
+ return
|
|
|
+
|
|
|
+ loadData.saveCenters(r,setup)
|
|
|
+
|
|
|
+ if stage=='fitIVF':
|
|
|
+
|
|
|
+
|
|
|
+ if not checkRequiredFiles(stage,r,setup,fb=fb,doPrint=True):
|
|
|
+ return
|
|
|
+
|
|
|
+ loadData.saveIVF(r,setup,nfit=30,qLambda=qLambda)
|
|
|
+
|
|
|
+
|
|
|
+ if stage=='plotIVF':
|
|
|
+ ir=0
|
|
|
+ names=listRequiredFiles(stage,r,setup)
|
|
|
+ if fb:
|
|
|
+ getRequiredFiles(stage,r,setup,fb,names=names)
|
|
|
+ if not checkRequiredFiles(stage,r,setup,fb=fb,doPrint=True,names=names):
|
|
|
+ return
|
|
|
+
|
|
|
+ print('Loading files to memory')
|
|
|
+ m,samples=loadData.readIVF(r,setup,qLambda=qLambda)
|
|
|
+
|
|
|
+ data=loadData.loadData(r,setup)
|
|
|
+ ct=loadData.loadCT(r,setup)
|
|
|
+ centerMapSPECT,centerMapCT=loadData.loadCenterMapNRRD(r,setup,ir=ir)
|
|
|
+
|
|
|
+ t,dt=loadData.loadTime(r,setup)
|
|
|
+ centers=loadData.loadCenters(r,setup,ir=ir)
|
|
|
+ ivf=centers[m]
|
|
|
+ chi2=samples[0,:]
|
|
|
+ threshold=numpy.median(chi2)
|
|
|
+ code=config.getCode(r,setup)
|
|
|
+ ir=0
|
|
|
+
|
|
|
+ categories=['fits','diff','generatedIVF','centerIVFSPECT','centerIVFCT']
|
|
|
+
|
|
|
+ fNames={x:config.getPattern('plotIVF',code=code,ir=0,nclass=nclass,qaName=x,qLambda=qLambda) for x in categories}
|
|
|
+ files={x:getData.getLocalPath(r,setup,fNames[x]) for x in fNames}
|
|
|
+
|
|
|
+
|
|
|
+ plotData.plotIVF(t,ivf,samples,threshold,file0=files['fits'],file1=files['diff'])
|
|
|
+
|
|
|
+ plotData.plotIVFRealizations(t,ivf,samples,threshold,file=files['generatedIVF'])
|
|
|
+
|
|
|
+
|
|
|
+ plotData.plotIVFCenter(centerMapSPECT,centerMapCT,m,data,ct,file0=files['centerIVFSPECT'],
|
|
|
+ file1=files['centerIVFCT'])
|
|
|
+
|
|
|
+ updateDatabase(r,setup,stage,db=db,fb=fb,categories=categories)
|
|
|
+
|
|
|
+ if stage=='fitCompartment':
|
|
|
+ ir=0
|
|
|
+ names=listRequiredFiles(stage,r,setup)
|
|
|
+ if fb:
|
|
|
+ getRequiredFiles(stage,r,setup,fb,names=names)
|
|
|
+ if not checkRequiredFiles(stage,r,setup,fb=fb,doPrint=True,names=names):
|
|
|
+ return
|
|
|
+
|
|
|
+
|
|
|
+ u=loadData.loadCenterMap(r,setup)
|
|
|
+ print(u.shape)
|
|
|
+
|
|
|
+
|
|
|
+ seg=segmentation.getNRRDImage(r,setup,names)
|
|
|
+ loc=numpy.nonzero(seg)
|
|
|
+ vClass=[int(x) for x in u[loc]]
|
|
|
+ segments=[int(x) for x in seg[loc]]
|
|
|
+ print(segments)
|
|
|
+ data=loadData.loadData(r,setup)
|
|
|
+ tac=loadData.getTACAtPixels(data,loc)
|
|
|
+
|
|
|
+ segMap=makeMap(segments,vClass,tac)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ m1,samples=loadData.readIVF(r,setup,qLambda=qLambda)
|
|
|
+ chi2=samples[0,:]
|
|
|
+ threshold=numpy.median(chi2)
|
|
|
+ ivfFit=fitData.getFit(samples,threshold)
|
|
|
+ t,dt=loadData.loadTime(r,setup)
|
|
|
+ centers=loadData.loadCenters(r,setup)
|
|
|
+
|
|
|
+ setup['nseg']=len(segMap.keys())
|
|
|
+ for x in segMap:
|
|
|
+ mArray=segMap[x]
|
|
|
+ qCenter=numpy.zeros(t.shape[0])
|
|
|
+ qData=numpy.zeros(t.shape[0])
|
|
|
+ s=0
|
|
|
+
|
|
|
+ kCenters=[]
|
|
|
+ for m in mArray:
|
|
|
+
|
|
|
+ kCenters.append(m[0])
|
|
|
+ qCenter+=centers[m[0]]
|
|
|
+ qData+=m[1]
|
|
|
+ s+=1
|
|
|
+ qCenter/=s
|
|
|
+ qData/=s
|
|
|
+ samplesC=fitData.fitCompartmentGlobal(ivfFit,t,qCenter,useJac=True,nfit=20)
|
|
|
+ samplesC1=fitData.fitCompartmentGlobal(ivfFit,t,qData,nfit=20,useJac=True)
|
|
|
+
|
|
|
+ loadData.saveSamples(r,setup,samplesC,kCenters,'kmeansFit',iseg=x,ir=ir,qLambda=qLambda)
|
|
|
+ loadData.saveSamples(r,setup,samplesC1,[-1],'localFit',iseg=x,ir=ir,qLambda=qLambda)
|
|
|
+ loadData.saveTAC(r,setup,qCenter,'kmeansTAC',iseg=x,ir=ir,qLambda=qLambda)
|
|
|
+ loadData.saveTAC(r,setup,qData,'localTAC',iseg=x,ir=ir,qLambda=qLambda)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if stage=='plotCompartment':
|
|
|
+ ir=0
|
|
|
+ names=listRequiredFiles(stage,r,setup)
|
|
|
+ if fb:
|
|
|
+ getRequiredFiles(stage,r,setup,fb,names=names)
|
|
|
+ if not checkRequiredFiles(stage,r,setup,fb=fb,doPrint=True,names=names):
|
|
|
+ return
|
|
|
+
|
|
|
+ tag='plotCompartment'
|
|
|
+ seg=segmentation.getNRRDImage(r,setup,names)
|
|
|
+ loc=numpy.nonzero(seg)
|
|
|
+ segmentIds=list(set([int(x) for x in seg[loc]]))
|
|
|
+ nclass=setup['nclass'][0]
|
|
|
+ code=config.getCode(r,setup)
|
|
|
+ setup['nseg']=len(segmentIds)
|
|
|
+ t,dt=loadData.loadTime(r,setup)
|
|
|
+ for iseg in segmentIds:
|
|
|
+ m,samplesC=loadData.readSamples(r,setup,'kmeansFit',ir=ir,iseg=iseg,qLambda=qLambda)
|
|
|
+ m1,samplesC1=loadData.readSamples(r,setup,'localFit',ir=ir,iseg=iseg,qLambda=qLambda)
|
|
|
+ qCenter=loadData.readTAC(r,setup,'kmeansTAC',ir=ir,iseg=iseg,qLambda=qLambda)
|
|
|
+ qData=loadData.readTAC(r,setup,'localTAC',ir=ir,iseg=iseg,qLambda=qLambda)
|
|
|
+ chi2C=samplesC[0,:]
|
|
|
+ threshold=numpy.median(chi2C)
|
|
|
+ chi2C1=samplesC1[0,:]
|
|
|
+ threshold1=numpy.median(chi2C1)
|
|
|
+ fit=fitData.getFit(samplesC,threshold)
|
|
|
+ fit1=fitData.getFit(samplesC1,threshold1)
|
|
|
+ k1=fit.mu[0]
|
|
|
+ stdK1=fit.cov[0,0]
|
|
|
+ k11=fit1.mu[0]
|
|
|
+ stdK11=fit1.cov[0,0]
|
|
|
+
|
|
|
+ row={x:r[x] for x in ['PatientId','visitCode']}
|
|
|
+ row['Date']=datetime.datetime.now().isoformat()
|
|
|
+ row['nclass']=nclass
|
|
|
+ row['option']='kmeansFit'
|
|
|
+ row['mean']=k1
|
|
|
+ row['std']=stdK1
|
|
|
+ row['regionId']=iseg
|
|
|
+ row['fitPlot']=config.getPattern(tag,code=code,ir=0,nclass=nclass,qaName='realizations',iseg=iseg,qLambda=qLambda)
|
|
|
+ row['diffPlot']=config.getPattern(tag,code=code,ir=0,nclass=nclass,qaName='diff',iseg=iseg,qLambda=qLambda)
|
|
|
+ row1={x:row[x] for x in row}
|
|
|
+ row1['option']='localFit'
|
|
|
+ row1['mean']=k11
|
|
|
+ row1['std']=stdK11
|
|
|
+ row['qLambda']=qLambda
|
|
|
+ if db:
|
|
|
+ db.modifyRows('insert',setup['project'],'lists','Summary',[row,row1])
|
|
|
+
|
|
|
+
|
|
|
+ evalArray=[(samplesC,qCenter,'blue'),
|
|
|
+ (samplesC1,qData,'orange')]
|
|
|
+
|
|
|
+ file0=getData.getLocalPath(r,setup,row['fitPlot'])
|
|
|
+ file1=getData.getLocalPath(r,setup,row['diffPlot'])
|
|
|
+ plotData.plotSamples(t,evalArray,file0=file0,file1=file1)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ uploadCreatedFiles(stage,fb,r,setup)
|