config.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 getCode(row,xconfig):
  33. return '{}_{}'.format(getPatientId(row,xconfig),getVisitId(row,xconfig))
  34. def getTargetSeqNum(row,xconfig):
  35. if getVisitId(row,xconfig)=='OBR':
  36. return 2
  37. return 1
  38. def getPathList(row,xconfig):
  39. return [xconfig['baseDir'],getPatientId(row,xconfig),getVisitId(row,xconfig)]
  40. def getOutputDir(row,xconfig):
  41. return '/'.join(getPathList(row,xconfig))
  42. def getTempDir(xconfig):
  43. tempDir=xconfig['tempDir']
  44. pathList=tempDir.split('/')
  45. pathList.insert(0,os.path.expanduser('~'))
  46. return os.path.join(*pathList)
  47. def getLocalDir(row,xconfig):
  48. return os.path.join(getTempDir(xconfig),getCode(row,xconfig))
  49. def getNodeName(row,xconfig,mode,i=0):
  50. if mode=='CT':
  51. return getCode(row,xconfig)+'_CT'
  52. if mode=='NM':
  53. return '{}_Volume{}'.format(getCode(row,xconfig),i)
  54. return getCode(row,xconfig)+'_Dummy'
  55. def decode(code,xconfig):
  56. #invert code and return object equivalent to row with relevant
  57. #fields set that can be used in
  58. #getPatientId, getVisitId,getIdFilter,getVisitFilter
  59. #as an equivalent replacement for r
  60. values=code.split('_')
  61. fid=values[0]
  62. vid=values[1]
  63. return {getPatientField(xconfig):fid,getVisitField(xconfig):vid}
  64. def printRowCenterNames(r,setup):
  65. names=[]
  66. nr=setup['nr']
  67. nclass=setup['nclass']
  68. code=getCode(r,setup)
  69. #tempDir=config.getTempDir(setup)
  70. for nc in nclass:
  71. for j in range(nr):
  72. for i in range(nc):
  73. fCode='{}_{}_{}_center{}'.format(code,nc,j+1,i+1)
  74. names.append('{}_center.txt'.format(fCode))
  75. names.append('{}_centerWeight.nrrd'.format(fCode))
  76. return names
  77. def getFitParFinalName(code,nc,j,aType):
  78. fCode='{}_{}_{}'.format(code,nc,j+1)
  79. if aType!='global':
  80. fCode='{}_{}'.format(fCode,aType)
  81. return '{}_fitParFinal.txt'.format(fCode)
  82. def printFitParFinalRowNames(r,setup,aType):
  83. names=[]
  84. nr=setup['nr']
  85. nclass=setup['nclass']
  86. code=getCode(r,setup)
  87. #tempDir=config.getTempDir(setup)
  88. for nc in nclass:
  89. for j in range(nr):
  90. names.append(getFitParFinalName(code,nc,j,aType))
  91. #names.append('{}_centerWeight.nrrd'.format(fCode))
  92. return names
  93. def getPixelFitParFinalName(code,nc,s2,mode):
  94. sigmaCode='{:.2f}'.format(s2)
  95. sigmaCode=re.sub('\.','p',sigmaCode)
  96. fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
  97. if mode!='general':
  98. fName='{}{}'.format(fName,mode)
  99. fName='{}_fitParFinal.txt'.format(fName)
  100. return fName
  101. def getPixelFitParFinalPngName(code,nc,i,s2,mode):
  102. sigmaCode='{:.2f}'.format(s2)
  103. sigmaCode=re.sub('\.','p',sigmaCode)
  104. fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
  105. if mode!='general':
  106. fName='{}{}'.format(fName,mode)
  107. fName='{}_centers{}.png'.format(fName,i)
  108. return fName
  109. def printPixelFitParFinalRowNames(r,setup,nc,s2,mode):
  110. names=[]
  111. code=getCode(r,setup)
  112. names.append(getPixelFitParFinalName(code,nc,s2,mode))
  113. for i in range(nc):
  114. names.append(getPixelFitParFinalPngName(code,nc,i+1,s2,mode))
  115. return names