getData.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import os
  2. import sys
  3. import config
  4. import analysis
  5. import json
  6. import SimpleITK
  7. def connectDB(server):
  8. sPath=os.path.join(os.path.expanduser('~'),'.labkey','setup.json')
  9. with open(sPath,'r') as f:
  10. setup=json.load(f)
  11. sys.path.append(setup['paths']['nixWrapper'])
  12. import nixWrapper
  13. nixWrapper.loadLibrary('labkeyInterface')
  14. import labkeyInterface
  15. net=labkeyInterface.labkeyInterface()
  16. fconfig=os.path.join(os.path.expanduser('~'),'.labkey',server)
  17. net.init(fconfig)
  18. net.getCSRF()
  19. import labkeyDatabaseBrowser
  20. import labkeyFileBrowser
  21. return labkeyDatabaseBrowser.labkeyDB(net),labkeyFileBrowser.labkeyFileBrowser(net)
  22. def getDataset(db,setup,datasetName,qfilter=[]):
  23. ds=db.selectRows(setup['project'],setup['schemaName'],\
  24. setup[datasetName],qfilter)
  25. try:
  26. rows=ds['rows']
  27. except KeyError:
  28. rows=[]
  29. return rows
  30. def updateDataset(db,setup,datasetName,mode,rows):
  31. db.modifyRows(mode,setup['project'],setup['schemaName'],\
  32. setup[datasetName],rows)
  33. def getPatients(db,setup,qfilter=[]):
  34. return getDataset(db,setup,'queryName',qfilter)
  35. def getSegmentation(db,setup,qfilter=[]):
  36. return getDataset(db,setup,'segmentationQueryName',qfilter)
  37. def getSummary(db,setup,qfilter=[]):
  38. return getDataset(db,setup,'summaryQueryName',qfilter)
  39. def updatePatients(db,setup,mode,rows):
  40. updateDataset(db,setup,'queryName',mode,rows)
  41. def updateSegmentation(db,setup,mode,rows):
  42. updateDataset(db,setup,'segmentationQueryName',mode,rows)
  43. def updateSummary(db,setup,mode,rows):
  44. updateDataset(db,setup,'summaryQueryName',mode,rows)
  45. def downloadNode(fb,fileName,rPath,lPath):
  46. rPath1=rPath+'/'+fileName
  47. available=fb.entryExists(rPath1)
  48. if not available:
  49. print('Missing {}'.format(fileName))
  50. return
  51. lPath1=os.path.join(lPath,fileName)
  52. if os.path.isfile(lPath1):
  53. return
  54. print('Loading {}'.format(fileName))
  55. fb.readFileToFile(rPath1,lPath1)
  56. def downloadFiles(fb,r,setup):
  57. #CT
  58. fileName=config.getNodeName(r,setup,'CT')+'.nrrd'
  59. copyFromServer(fb,r,setup,[fileName])
  60. for i in range(20):
  61. fileName=config.getNodeName(r,setup,'NM',i)+'.nrrd'
  62. copyFromServer(fb,r,setup,[fileName])
  63. fileName=config.getNodeName(r,setup,'Dummy')+'.mcsv'
  64. copyFromServer(fb,r,setup,[fileName])
  65. def downloadPatientFiles(db,fb,setup,qfilter=[]):
  66. rows=getPatients(db,setup,qfilter)
  67. for r in rows:
  68. #download
  69. downloadFiles(fb,r,setup)
  70. def uploadCenters(fb,r,setup):
  71. names=config.printRowCenterNames(r,setup)
  72. copyToServer(fb,r,setup,names)
  73. def downloadCenters(fb,r,setup):
  74. names=config.printRowCenterNames(r,setup)
  75. copyFromServer(fb,r,setup,names)
  76. def uploadFitParFinal(fb,r,setup,mode):
  77. names=config.printFitParFinalRowNames(r,setup,mode)
  78. #copy files to server
  79. copyToServer(fb,r,setup,names)
  80. def downloadFitParFinal(fb,r,setup,mode):
  81. names=config.printFitParFinalRowNames(r,setup,mode)
  82. #copy files to server
  83. copyFromServer(fb,r,setup,names)
  84. def uploadPixelFitParFinal(fb,r,xsetup,m='IVF'):
  85. sigma2=xsetup['sigma2']
  86. #get nc
  87. nc=segmentation.getNC(r,xsetup)
  88. for s2 in sigma2:
  89. names=config.printPixelFitParFinalRowNames(r,xsetup,nc,s2,m)
  90. copyToServer(fb,r,setup,names)
  91. def copyToServer(fb,r,setup,names):
  92. remoteDir=fb.buildPathURL(setup['project'],config.getPathList(r,setup))
  93. for n in names:
  94. localPath=getLocalPath(r,setup,n)
  95. remotePath='{}/{}'.format(remoteDir,n)
  96. fb.writeFileToFile(localPath,remotePath)
  97. def copyFromServer(fb,r,setup,names):
  98. try:
  99. forceReload=setup['forceReload']
  100. except KeyError:
  101. forceReload=False
  102. getLocalDir(r,setup,createIfMissing=True)
  103. remoteDir=fb.buildPathURL(setup['project'],config.getPathList(r,setup))
  104. for n in names:
  105. localPath=getLocalPath(r,setup,n)
  106. if os.path.isfile(localPath) and not forceReload:
  107. continue
  108. remotePath='/'.join([remoteDir,n])
  109. fb.readFileToFile(remotePath,localPath)
  110. def getLocalDir(r,setup,createIfMissing=False):
  111. localDir=os.path.join(config.getTempDir(setup),config.getCode(r,setup))
  112. if createIfMissing:
  113. if not os.path.isdir(localDir):
  114. os.makedirs(localDir)
  115. return localDir
  116. def getLocalPath(r,setup,name):
  117. tempDir=config.getTempDir(setup)
  118. code=config.getCode(r,setup)
  119. fileName=name[name.rfind('/')+1:]
  120. return os.path.join(tempDir,code,fileName)
  121. def getURL(fb,r,setup,name):
  122. remoteDir=fb.buildPathURL(setup['project'],config.getPathList(r,setup))
  123. return '/'.join([remoteDir,name])
  124. def getPatientNIM(fb,r,setup):
  125. fileName=config.getNodeName(r,setup,'NM',19)+'.nrrd'
  126. f=getLocalPath(r,setup,fileName)
  127. if not os.path.isfile(f):
  128. #download from server
  129. copyFromServer(fb,r,setup,[fileName])
  130. im=SimpleITK.ReadImage(f)
  131. nim=SimpleITK.GetArrayFromImage(im)
  132. return nim
  133. def updateStatus(db,r,setup,var,status=True):
  134. #update status of var to status
  135. fVars=['PatientId','visitCode']
  136. qFilter=[{'variable':x,'value':'{}'.format(r[x]),'oper':'eq'} for x in fVars]
  137. ds=db.selectRows(setup['project'],'study','processingStatus',qFilter)
  138. try:
  139. rows=ds['rows']
  140. except KeyError:
  141. #no rows returned, possibly the dataset does not exist
  142. return
  143. mode='update'
  144. try:
  145. entry=rows[0]
  146. except IndexError:
  147. fVars.extend([config.getPatientField(setup), "SequenceNum"])
  148. entry={x:r[x] for x in fVars}
  149. mode='insert'
  150. entry[var]=status
  151. db.modifyRows(mode,setup['project'],'study','processingStatus',[entry])