config.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. return 'visitName'
  22. def getVisitId(row,xconfig):
  23. return row[getVisitField(xconfig)]
  24. def getIdFilter(row,xconfig):
  25. return {'variable':getPatientField(xconfig),\
  26. 'value':getPatientId(row,xconfig),
  27. 'oper':'eq'}
  28. def getVisitFilter(row,xconfig):
  29. return {'variable':getVisitField(xconfig),\
  30. 'value':getVisitId(row,xconfig),
  31. 'oper':'eq'}
  32. def getFilter(xconfig):
  33. row={}
  34. fields={getPatientField(xconfig):getIdFilter,
  35. getVisitField(xconfig):getVisitFilter}
  36. qfilter=[]
  37. for f in fields:
  38. try:
  39. row[f]=xconfig[f]
  40. qfilter.append(fields[f](row,xconfig))
  41. except KeyError:
  42. continue
  43. return qfilter
  44. def getCode(row,xconfig):
  45. return '{}_{}'.format(getPatientId(row,xconfig),getVisitId(row,xconfig))
  46. def getTargetSeqNum(row,xconfig):
  47. if getVisitId(row,xconfig)=='OBR':
  48. return 2
  49. if getVisitId(row,xconfig)=='MIR1':
  50. return 3
  51. return 1
  52. def getPathList(row,xconfig):
  53. return [xconfig['baseDir'],getPatientId(row,xconfig),getVisitId(row,xconfig)]
  54. def getOutputDir(row,xconfig):
  55. return '/'.join(getPathList(row,xconfig))
  56. def getTempDir(xconfig):
  57. tempDir=xconfig['tempDir']
  58. pathList=tempDir.split('/')
  59. pathList.insert(0,os.path.expanduser('~'))
  60. return os.path.join(*pathList)
  61. def getLocalDir(row,xconfig):
  62. return os.path.join(getTempDir(xconfig),getCode(row,xconfig))
  63. def getNodeName(row,xconfig,mode,i=0):
  64. if mode=='CT':
  65. return getCode(row,xconfig)+'_CT'
  66. if mode=='NM':
  67. return '{}_Volume{}'.format(getCode(row,xconfig),i)
  68. return getCode(row,xconfig)+'_Dummy'
  69. def decode(code,xconfig):
  70. #invert code and return object equivalent to row with relevant
  71. #fields set that can be used in
  72. #getPatientId, getVisitId,getIdFilter,getVisitFilter
  73. #as an equivalent replacement for r
  74. values=code.split('_')
  75. fid=values[0]
  76. vid=values[1]
  77. return {getPatientField(xconfig):fid,getVisitField(xconfig):vid}
  78. def printRowCenterNames(r,setup):
  79. names=[]
  80. nr=setup['nr']
  81. nclass=setup['nclass']
  82. code=getCode(r,setup)
  83. #tempDir=config.getTempDir(setup)
  84. for nc in nclass:
  85. for j in range(nr):
  86. for i in range(nc):
  87. fCode='{}_{}_{}_center{}'.format(code,nc,j+1,i+1)
  88. names.append('{}_center.txt'.format(fCode))
  89. names.append('{}_centerWeight.nrrd'.format(fCode))
  90. return names
  91. def getFitParFinalName(code,nc,j,aType):
  92. fCode='{}_{}_{}'.format(code,nc,j+1)
  93. if aType!='global':
  94. fCode='{}_{}'.format(fCode,aType)
  95. return '{}_fitParFinal.txt'.format(fCode)
  96. def getFitPNGNames(code,nc,j,aType):
  97. fCode='{}_{}_{}'.format(code,nc,j+1)
  98. if aType!='global':
  99. fCode='{}_{}'.format(fCode,aType)
  100. names=[]
  101. for i in range(nc):
  102. names.append('{}_centers{}.txt'.format(fCode,i+1))
  103. return names
  104. def printFitParFinalRowNames(r,setup,aType):
  105. names=[]
  106. nr=setup['nr']
  107. nclass=setup['nclass']
  108. code=getCode(r,setup)
  109. #tempDir=config.getTempDir(setup)
  110. for nc in nclass:
  111. for j in range(nr):
  112. names.append(getFitParFinalName(code,nc,j,aType))
  113. #add pngs of the fit (good QA)
  114. #this are only partial fits, from which global parameters are taken
  115. #names+=getFitPNGNames(code,nc,j,aType)
  116. return names
  117. def getPixelFitParFinalName(code,nc,s2,mode):
  118. sigmaCode='{:.2f}'.format(s2)
  119. sigmaCode=re.sub('\.','p',sigmaCode)
  120. fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
  121. if mode!='general':
  122. fName='{}{}'.format(fName,mode)
  123. fName='{}_fitParFinal.txt'.format(fName)
  124. return fName
  125. def getPixelFitParFinalPngName(code,nc,i,s2,mode):
  126. sigmaCode='{:.2f}'.format(s2)
  127. sigmaCode=re.sub('\.','p',sigmaCode)
  128. fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
  129. if mode!='general':
  130. fName='{}{}'.format(fName,mode)
  131. fName='{}_centers{}.png'.format(fName,i)
  132. return fName
  133. def printPixelFitParFinalRowNames(r,setup,nc,s2,mode):
  134. names=[]
  135. code=getCode(r,setup)
  136. names.append(getPixelFitParFinalName(code,nc,s2,mode))
  137. for i in range(nc):
  138. names.append(getPixelFitParFinalPngName(code,nc,i+1,s2,mode))
  139. return names