config.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import os
  2. import json
  3. import re
  4. def cmdMatlab():
  5. pwd=os.path.dirname(os.path.abspath(__file__))
  6. pwdUp=os.path.dirname(pwd)
  7. mDir=os.path.join(pwdUp,'matlab')
  8. with open(os.path.join(os.path.expanduser('~'),'.labkey','setup.json'),'r') as f:
  9. siteSetup=json.load(f)
  10. mExec=siteSetup['paths']['matlab']
  11. cmds=[mExec]
  12. cmds.append('-sd')
  13. cmds.append(mDir)
  14. #cmds.append('-batch')
  15. return cmds
  16. def getPatientId(row,xconfig):
  17. return row[getPatientField(xconfig)]
  18. def getPatientField(xconfig):
  19. return xconfig['ParticipantField']
  20. def getVisitField(xconfig):
  21. try:
  22. return xconfig['visitField']
  23. except KeyError:
  24. return 'visitName'
  25. def getVisitId(row,xconfig):
  26. return row[getVisitField(xconfig)]
  27. def getIdFilter(row,xconfig):
  28. return {'variable':getPatientField(xconfig),\
  29. 'value':getPatientId(row,xconfig),
  30. 'oper':'eq'}
  31. def getVisitFilter(row,xconfig):
  32. return {'variable':getVisitField(xconfig),\
  33. 'value':getVisitId(row,xconfig),
  34. 'oper':'eq'}
  35. def getFilter(xconfig):
  36. row={}
  37. fields={getPatientField(xconfig):getIdFilter,
  38. getVisitField(xconfig):getVisitFilter}
  39. qfilter=[]
  40. for f in fields:
  41. try:
  42. row[f]=xconfig[f]
  43. if xconfig[f]!='NONE':
  44. qfilter.append(fields[f](row,xconfig))
  45. except KeyError:
  46. continue
  47. return qfilter
  48. def getCode(row,xconfig):
  49. return '{}_{}'.format(getPatientId(row,xconfig),getVisitId(row,xconfig))
  50. def getTargetSeqNum(row,xconfig):
  51. if getVisitId(row,xconfig)=='OBR':
  52. return 2
  53. if getVisitId(row,xconfig)=='MIR1':
  54. return 3
  55. if getVisitId(row,xconfig)=='MIR2':
  56. return 4
  57. return 1
  58. def getPathList(row,xconfig):
  59. return [xconfig['baseDir'],getPatientId(row,xconfig),getVisitId(row,xconfig)]
  60. def getOutputDir(row,xconfig):
  61. return '/'.join(getPathList(row,xconfig))
  62. def getTempDir(xconfig):
  63. tempDir=xconfig['tempDir']
  64. pathList=tempDir.split('/')
  65. pathList.insert(0,os.path.expanduser('~'))
  66. return os.path.join(*pathList)
  67. def getLocalDir(row,xconfig):
  68. return os.path.join(getTempDir(xconfig),getCode(row,xconfig))
  69. def getNodeName(row,xconfig,mode,i=0):
  70. if mode=='CT':
  71. return getCode(row,xconfig)+'_CT'
  72. if mode=='NM':
  73. return '{}_Volume{}'.format(getCode(row,xconfig),i)
  74. return getCode(row,xconfig)+'_Dummy'
  75. def decode(code,xconfig):
  76. #invert code and return object equivalent to row with relevant
  77. #fields set that can be used in
  78. #getPatientId, getVisitId,getIdFilter,getVisitFilter
  79. #as an equivalent replacement for r
  80. values=code.split('_')
  81. fid=values[0]
  82. vid=values[1]
  83. return {getPatientField(xconfig):fid,getVisitField(xconfig):vid}
  84. #standardized file names
  85. def getPattern(ftype,code,nclass=0,ir=0,ic=0,fitType='global',qaName='fits',timepoint=0,iseg=0,qLambda=0,qLambdaC=0):
  86. qLambdaString='{:.3f}'.format(qLambda).replace('.','p')
  87. qLambdaCString='{:.3f}'.format(qLambdaC).replace('.','p')
  88. replacePatterns={
  89. '_code_':code,
  90. '_nclass_':nclass,
  91. '_ir_':ir,
  92. '_ic_':ic,
  93. '_fitType_':fitType,
  94. '_qaName_':qaName,
  95. '_timepoint_':timepoint,
  96. '_iseg_':iseg,
  97. '_qLambda_':qLambdaString,
  98. '_qLambdaC_':qLambdaCString}
  99. map={'CT':'_code__CT.nrrd',
  100. 'SPECT':'_code__Volume_timepoint_.nrrd',
  101. 'Dummy':'_code__Dummy.mcsv',
  102. 'centerMap':'_code___nclass___ir__centerMap.txt',
  103. 'centerWeight':'_code___nclass___ir__center_ic__centerWeight.nrrd',
  104. 'centerNRRD':'_code___nclass___ir__centerMap__qaName_.nrrd',
  105. 'center':'_code___nclass___ir__center_ic__center.txt',
  106. 'fitParFinal':'_code___nclass___ir___fitType__fitParFinal.txt',
  107. 'fitPNG':'_code___nclass___ir___fitType__centers_ic_.png',
  108. 'pixelFitParFinal':'_code___nclass___ir__Pixel__fitType__fitParFinal.txt',
  109. 'pixelFitPNG':'_code___nclass___ir__Pixel__fitType__centers_ic_.png',
  110. 'fitIVF':'_code___nclass___ir___qLambda__fitParIVF.txt',
  111. 'plotIVF':'_code___nclass___ir___qLambda__plotIVF__qaName_.png',
  112. 'fitCompartment':'_code___nclass___ir___qLambda__fitCompartment__iseg___qaName___qLambdaC_.txt',
  113. 'plotCompartment':'_code___nclass___ir___qLambda__plotCompartment__iseg___qaName___qLambdaC_.png'}
  114. w=map[ftype]
  115. for x in replacePatterns:
  116. w=re.sub(x,'{}'.format(replacePatterns[x]),w)
  117. return w
  118. #pixels corresponding to a particular c-means class
  119. def getCenterWeight(r,setup,nclass,ir,i):
  120. code=getCode(r,setup)
  121. return f'{code}_{nclass}_{ir}_center{i}_centerWeight.nrrd'
  122. #1D TAC for a particular group center
  123. def getCenter(r,setup,nclass,ir,i):
  124. code=getCode(r,setup)
  125. return f'{code}_{nclass}_{ir}_center{i}_center.txt'
  126. #all names for determination of TAC, obsolete
  127. def printRowCenterNames(r,setup):
  128. names=[]
  129. nr=setup['nr']
  130. nclass=setup['nclass']
  131. code=getCode(r,setup)
  132. #tempDir=config.getTempDir(setup)
  133. for nc in nclass:
  134. for j in range(nr):
  135. for i in range(nc):
  136. names.append(getCenter(r,setup,nc,j+1,i+1))
  137. names.append(getCenterWeight(r,setup,nc,j+1,i+1))
  138. return names
  139. #fit of a compartment
  140. def getFitParFinalName(code,nc,j,aType):
  141. fCode='{}_{}_{}'.format(code,nc,j+1)
  142. if aType!='global':
  143. fCode='{}_{}'.format(fCode,aType)
  144. return '{}_fitParFinal.txt'.format(fCode)
  145. #fit of the IVF
  146. def getFitIVFParName(r,setup,nc,j):
  147. code=getCode(r,setup)
  148. fCode='{}_{}_{}'.format(code,nc,j+1)
  149. return f'{fCode}_fitParIVF.txt'
  150. #status report of the IVF fit (three images)
  151. def getFitIVFReportPNG(r,setup,nc,j,name):
  152. code=getCode(r,setup)
  153. fCode=f'{code}_{nc}_{j+1}_fitParIVF_{name}.png'
  154. return fCode
  155. #
  156. def getFitPNGNames(code,nc,j,aType):
  157. if aType=='global':
  158. return []
  159. fCode='{}_{}_{}'.format(code,nc,j+1)
  160. #if aType!='global':
  161. fCode='{}_{}'.format(fCode,aType)
  162. names=[]
  163. for i in range(nc):
  164. names.append('{}_centers{}.png'.format(fCode,i+1))
  165. return names
  166. def printFitParFinalRowNames(r,setup,aType):
  167. names=[]
  168. nr=setup['nr']
  169. nclass=setup['nclass']
  170. code=getCode(r,setup)
  171. #tempDir=config.getTempDir(setup)
  172. for nc in nclass:
  173. for j in range(nr):
  174. names.append(getFitParFinalName(code,nc,j,aType))
  175. #add pngs of the fit (good QA)
  176. #this are only partial fits, from which global parameters are taken
  177. names+=getFitPNGNames(code,nc,j,aType)
  178. return names
  179. def getPixelFitParFinalName(code,nc,s2,mode):
  180. sigmaCode='{:.2f}'.format(s2)
  181. sigmaCode=re.sub('\.','p',sigmaCode)
  182. fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
  183. if mode!='general':
  184. fName='{}{}'.format(fName,mode)
  185. fName='{}_fitParFinal.txt'.format(fName)
  186. return fName
  187. def getPixelFitParFinalPngName(code,nc,i,s2,mode):
  188. sigmaCode='{:.2f}'.format(s2)
  189. sigmaCode=re.sub('\.','p',sigmaCode)
  190. fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
  191. if mode!='general':
  192. fName='{}{}'.format(fName,mode)
  193. fName='{}_centers{}.png'.format(fName,i)
  194. return fName
  195. def printPixelFitParFinalRowNames(r,setup,nc,s2,mode):
  196. names=[]
  197. code=getCode(r,setup)
  198. names.append(getPixelFitParFinalName(code,nc,s2,mode))
  199. for i in range(nc):
  200. names.append(getPixelFitParFinalPngName(code,nc,i+1,s2,mode))
  201. return names