getData.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import os
  2. import sys
  3. import config
  4. import analysis
  5. import json
  6. def connectDB(server):
  7. sPath=os.path.join(os.path.expanduser('~'),'.labkey','setup.json')
  8. with open(sPath,'r') as f:
  9. setup=json.load(f)
  10. sys.path.append(setup['paths']['nixWrapper'])
  11. import nixWrapper
  12. nixWrapper.loadLibrary('labkeyInterface')
  13. import labkeyInterface
  14. net=labkeyInterface.labkeyInterface()
  15. fconfig=os.path.join(os.path.expanduser('~'),'.labkey',server)
  16. net.init(fconfig)
  17. net.getCSRF()
  18. import labkeyDatabaseBrowser
  19. import labkeyFileBrowser
  20. return labkeyDatabaseBrowser.labkeyDB(net),labkeyFileBrowser.labkeyFileBrowser(net)
  21. def getDataset(db,setup,datasetName,qfilter=[]):
  22. ds=db.selectRows(setup['project'],setup['schemaName'],\
  23. setup[datasetName],qfilter)
  24. try:
  25. rows=ds['rows']
  26. except KeyError:
  27. rows=[]
  28. return rows
  29. def updateDataset(db,setup,datasetName,mode,rows):
  30. db.modifyRows(mode,setup['project'],setup['schemaName'],\
  31. setup[datasetName],rows)
  32. def getPatients(db,setup,qfilter=[]):
  33. return getDataset(db,setup,'queryName',qfilter)
  34. def getSegmentation(db,setup,qfilter=[]):
  35. return getDataset(db,setup,'segmentationQueryName',qfilter)
  36. def getSummary(db,setup,qfilter=[]):
  37. return getDataset(db,setup,'summaryQueryName',qfilter)
  38. def updatePatients(db,setup,mode,rows):
  39. updateDataset(db,setup,'queryName',mode,rows)
  40. def updateSegmentation(db,setup,mode,rows):
  41. updateDataset(db,setup,'segmentationQueryName',mode,rows)
  42. def updateSummary(db,setup,mode,rows):
  43. updateDataset(db,setup,'summaryQueryName',mode,rows)
  44. def downloadNode(fb,fileName,rPath,lPath):
  45. rPath1=rPath+'/'+fileName
  46. available=fb.entryExists(rPath1)
  47. if not available:
  48. print('Missing {}'.format(fileName))
  49. return
  50. lPath1=os.path.join(lPath,fileName)
  51. if os.path.isfile(lPath1):
  52. return
  53. print('Loading {}'.format(fileName))
  54. fb.readFileToFile(rPath1,lPath1)
  55. def downloadFiles(fb,r,setup):
  56. rPath=fb.formatPathURL(setup['project'],config.getOutputDir(r,setup))
  57. lPath=config.getLocalDir(r,setup)
  58. if not os.path.isdir(lPath):
  59. os.makedirs(lPath)
  60. #CT
  61. fileName=config.getNodeName(r,setup,'CT')+'.nrrd'
  62. downloadNode(fb,fileName,rPath,lPath)
  63. for i in range(20):
  64. fileName=config.getNodeName(r,setup,'NM',i)+'.nrrd'
  65. downloadNode(fb,fileName,rPath,lPath)
  66. fileName=config.getNodeName(r,setup,'Dummy')+'.mcsv'
  67. downloadNode(fb,fileName,rPath,lPath)
  68. def downloadPatientFiles(db,fb,setup,qfilter=[]):
  69. rows=getPatients(db,setup,qfilter)
  70. for r in rows:
  71. #download
  72. downloadFiles(fb,r,setup)
  73. def uploadCenters(fb,r,setup):
  74. names=config.printRowCenterNames(r,setup)
  75. copyToServer(fb,r,setup,names)
  76. def downloadCenters(fb,r,setup):
  77. names=config.printRowCenterNames(r,setup)
  78. copyFromServer(fb,r,setup,names)
  79. def uploadFitParFinal(fb,r,setup,mode):
  80. names=config.printFitParFinalRowNames(r,setup,mode)
  81. #copy files to server
  82. copyToServer(fb,r,setup,names)
  83. def copyToServer(fb,r,setup,names):
  84. tempDir=config.getTempDir(setup)
  85. code=config.getCode(r,setup)
  86. project=setup['project']
  87. remoteDir=fb.buildPathURL(project,config.getPathList(r,setup))
  88. for n in names:
  89. localPath=os.path.join(tempDir,code,n)
  90. remotePath='{}/{}'.format(remoteDir,n)
  91. fb.writeFileToFile(localPath,remotePath)
  92. def copyFromServer(fb,r,setup,names):
  93. try:
  94. forceReload=setup['forceReload']
  95. except KeyError:
  96. forceReload=False
  97. tempDir=config.getTempDir(setup)
  98. code=config.getCode(r,setup)
  99. project=setup['project']
  100. remoteDir=fb.buildPathURL(project,config.getPathList(r,setup))
  101. for n in names:
  102. localPath=os.path.join(tempDir,code,n)
  103. if os.path.isfile(localPath) and not forceReload:
  104. continue
  105. remotePath='{}/{}'.format(remoteDir,n)
  106. fb.readFileToFile(localPath,remotePath)