123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- import numpy
- import config
- import SimpleITK
- import os
- import getData
- def guessPixelPosition15(sx=-1,sy=-1,sz=-1):
- #guess position of segments
- if sx<0:
- sx=12
- if sy<0:
- sy=28
- if sz<0:
- sz=32
- rz=4
- oz=0
- slc=[sx,sy,sz]
- p1=[sx,sy,sz]
- pts={
- '0':[sx-5,sy,sz],\
- '1':[sx-2,sy,sz-rz],\
- '2':[sx-2,sy,sz+rz-1],\
- '3':[sx-1,sy-rz,sz],\
- '4':[sx-1,sy+rz-1,sz],\
- '5':[sx,sy-rz+oz,sz],\
- '6':[sx,sy-0.3*rz+oz,sz-rz],\
- '7':[sx,sy-0.3*rz+oz,sz+rz],\
- '8':[sx,sy,sz],\
- '9':[sx,sy+0.3*rz+oz,sz-rz],\
- '10':[sx,sy+0.3*rz+oz,sz+rz],\
- '11':[sx,sy+rz+oz,sz],\
- '12':[sx+3,sy-rz,sz],\
- '13':[sx+3,sy,sz-rz],\
- '14':[sx+3,sy,sz+rz],\
- '15':[sx+3,sy+rz,sz]}
- slices={'0':['8','0','1','13','2','14'],\
- '1':['8','0','3','4','12','15'],\
- '2':['8','11','9','6','5','7','10']}
-
- print(slices['0'])
- fp={x:[pts[q] for q in slices[x]] for x in slices}
-
- sliceIds={x:[] for x in pts}
- for p in pts:
- for s in slices:
- if p in slices[s]:
- sliceIds[p].append(s)
-
- sliceCode={x:';'.join(sliceIds[x]) for x in sliceIds}
- print(fp)
- print(sliceCode)
- return fp
- #tip
- fp={'0':[\
- [sx,sy,sz],
- [sx-5,sy,sz],\
- [sx-2,sy,sz-rz],\
- [sx+3,sy,sz-rz],\
- [sx-2,sy,sz+rz-1],\
- [sx+3,sy,sz+rz]],\
- '1':[\
- [sx,sy,sz],
- [sx-5,sy,sz],\
- [sx-1,sy-rz,sz],\
- [sx-1,sy+rz-1,sz],\
- [sx+3,sy-rz,sz],\
- [sx+3,sy+rz,sz]],\
- '2':[\
- [sx,sy,sz],
- [sx,sy+rz+oz,sz],\
- [sx,sy+0.3*rz+oz,sz-rz],\
- [sx,sy-0.3*rz+oz,sz-rz],\
- [sx,sy-rz+oz,sz],\
- [sx,sy-0.3*rz+oz,sz+rz],\
- [sx,sy+0.3*rz+oz,sz+rz]]}
- return fp
- def guessPixelPosition4(sx=-1,sy=-1,sz=-1):
- #guess position of segments
- if sx<0:
- sx=32
- if sy<0:
- sy=31
- if sz<0:
- sz=31
- rz=4
- pts={
- '0':[sx,sy,sz],\
- '1':[sx,sy,sz-rz],\
- '2':[sx,sy,sz+rz],\
- '3':[sx,sy-rz,sz],\
- '4':[sx,sy+rz,sz]}
- slices={
- '0':['0','1','2'],\
- '1':['0','3','4'],\
- '2':['0','1','2','3','4']}
-
- print(slices['0'])
- fp={x:[pts[q] for q in slices[x]] for x in slices}
-
- sliceIds={x:[] for x in pts}
- for p in pts:
- for s in slices:
- if p in slices[s]:
- sliceIds[p].append(s)
-
- sliceCode={x:';'.join(sliceIds[x]) for x in sliceIds}
- print(fp)
- print(sliceCode)
- return [{'regionId':x,'x':pts[x][0],'y':pts[x][1],'z':pts[x][2],'sliceId':sliceCode[x]} for x in pts]
- def updateSegmentation(db,setup,r,pixels):
- copyFields=['PatientId','visitCode']
- for x in pixels:
- for c in copyFields:
- x[c]=r[c]
- x['SequenceNum']=r['SequenceNum']+0.01*int(x['regionId'])
- filterVar=['PatientId','SequenceNum']
- qFilter=[{'variable':y,'value':'{}'.format(x[y]),'oper':'eq'} for y in filterVar]
- ds=db.selectRows(setup['project'],'study','Segmentation',qFilter)
- entry={}
- mode='insert'
- if len(ds['rows'])>0:
- entry=ds['rows'][0]
- mode='update'
- for q in x:
- entry[q]=x[q]
- db.modifyRows(mode,setup['project'],'study','Segmentation',[entry])
- print('Done')
-
- def getSegmentationFileName(r,setup,db=None):
- if not db:
- db,fb=getData.connectDB(setup['network'])
- if setup['segmentationMode']=='TXT':
- return '{}_Segmentation.txt'.format(config.getCode(r,setup))
- if setup['segmentationMode']=='NRRD':
- copyFields=['PatientId','visitCode']
- qFilter=[{'variable':x,'value':r[x],'oper':'eq'} for x in copyFields]
- qFilter.append({'variable':'User','value':setup['targetUser'],'oper':'eq'})
- rows=getData.getSegmentation(db,setup,qFilter)
- r=rows[0]
- return r['latestFile']
- def getURL(fb,r,setup,name):
- remoteDir=fb.buildPathURL(setup['project'],config.getPathList(r,setup))
- return '/'.join([remoteDir,'Segmentations',name])
-
- def copyFromServer(fb,r,setup,names):
- try:
- forceReload=setup['forceReload']
- except KeyError:
- forceReload=False
- getData.getLocalDir(r,setup,createIfMissing=True)
- remoteDir=fb.buildPathURL(setup['project'],config.getPathList(r,setup))
- for n in names:
- localPath=getData.getLocalPath(r,setup,n)
- if os.path.isfile(localPath) and not forceReload:
- continue
- remotePath='/'.join([remoteDir,'Segmentations',n])
- fb.readFileToFile(remotePath,localPath)
- def copyToServer(fb,r,setup,names):
- remoteDir=fb.buildPathURL(setup['project'],config.getPathList(r,setup))
- for n in names:
- localPath=getLocalPath(r,setup,n)
- remotePath='/'.join([remoteDir,'Segmentations',n])
- fb.writeFileToFile(localPath,remotePath)
- def writeSegmentation(db,fb,r,setup):
- if setup['segmentationMode']=='NRRD':
- print('Failed to load segmentation')
- return
-
- fileName=getSegmentationFileName(db,r,setup)
- idFilter={'variable':'PatientId','value':config.getPatientId(r,setup),'oper':'eq'}
- visitFilter={'variable':'visitCode','value':config.getVisitId(r,setup),'oper':'eq'}
- rows=getData.getSegmentation(db,setup,[idFilter,visitFilter])
- v=numpy.zeros((len(rows),3))
- for qr in rows:
- region=int(qr['regionId'])
- v[region,2]=float(qr['x'])
- v[region,1]=float(qr['y'])
- v[region,0]=float(qr['z'])
- #for i in range(len(rows)):
- # print(v[i,:])
- numpy.savetxt(getData.getLocalPath(r,setup,fileName),v)
- getData.copyToServer(fb,r,setup,[fileName])
- def getNC(r,xsetup):
- if xsetup['segmentationMode']=='TXT':
- getNCTxt(r,xsetup)
- if xsetup['segmentationMode']=='NRRD':
- return xsetup['NC']
- def getNCTxt(r,xsetup):
- sName=getSegmentationFileName(db=None,r=r,setup=xsetup)
- fName=getData.getLocalPath(r,xsetup,sName)
- x=numpy.loadtxt(fName)
- nc=x.shape[0]
- return nc
-
- def loadSegmentation(db,fb,r,setup):
-
- sName=getSegmentationFileName(db,r,setup)
- print(f'Looking for {sName}')
- fName=getData.getLocalPath(r,setup,sName)
- print(f'Local {fName}')
- if not os.path.isfile(fName):
- fURL=getData.getURL(fb,r,setup,sName)
- if fb.entryExists(fURL):
- getData.copyFromServer(fb,r,setup,[sName])
- if os.path.isfile(fName):
- print(f'Copied {fURL} to {fName}')
- else:
- print(f'Failed to load {fName} from {fURL}')
- else:
- #this creates local and global file
- writeSegmentation(db,fb,r,setup)
- if setup['segmentationMode']=='TXT':
- return numpy.loadtxt(fName)
-
- def getNRRDImage(r,setup,names=None):
- if names:
- localFile=getData.getLocalPath(r,setup,names['segmentation'][0])
- else:
- localFile=getData.getLocalPath(r,setup,getSegmentationFileName(r,setup))
- segImg=SimpleITK.ReadImage(localFile)
- seg=SimpleITK.GetArrayFromImage(segImg)
- return seg
|