Przeglądaj źródła

Rearranging scripts to do IO and parse config

NIX Worker 1 rok temu
rodzic
commit
8d56a9f074
2 zmienionych plików z 125 dodań i 3 usunięć
  1. 60 1
      pythonScripts/config.py
  2. 65 2
      pythonScripts/getData.py

+ 60 - 1
pythonScripts/config.py

@@ -1,5 +1,21 @@
 import os
+import json
 
+
+def cmdMatlab():
+   pwd=os.path.dirname(os.path.abspath(__file__))
+   pwdUp=os.path.dirname(pwd)
+   mDir=os.path.join(pwdUp,'matlab')
+   with open(os.path.join(os.path.expanduser('~'),'.labkey','setup.json'),'r') as f:
+      siteSetup=json.load(f)
+   mExec=siteSetup['paths']['matlab']
+
+   cmds=[mExec]
+   cmds.append('-sd')
+   cmds.append(mDir)
+   #cmds.append('-batch')
+   return cmds
+ 
 def getPatientId(row,xconfig):
    return row[getPatientField(xconfig)]
 
@@ -36,8 +52,16 @@ def getPathList(row,xconfig):
 def getOutputDir(row,xconfig):
    return '/'.join(getPathList(row,xconfig))
 
+
+def getTempDir(xconfig):
+   tempDir=xconfig['tempDir']
+   pathList=tempDir.split('/')
+   pathList.insert(0,os.path.expanduser('~'))
+   return os.path.join(*pathList)
+ 
+
 def getLocalDir(row,xconfig):
-   return os.path.join(xconfig['tempDir'],getCode(row,xconfig))
+   return os.path.join(getTempDir(xconfig),getCode(row,xconfig))
 
 def getNodeName(row,xconfig,mode,i=0):
    if mode=='CT':
@@ -56,4 +80,39 @@ def decode(code,xconfig):
    vid=values[1]
    return {getPatientField(xconfig):fid,getVisitField(xconfig):vid}
 
+def printRowCenterNames(r,setup):
+   names=[]
+   nr=setup['nr']
+   nclass=setup['nclass']
+   code=getCode(r,setup)
+   #tempDir=config.getTempDir(setup)
+   for nc in nclass:
+      for j in range(nr):
+         for i in range(nc): 
+            fCode='{}_{}_{}_center{}'.format(code,nc,j+1,i+1)
+            names.append('{}_center.txt'.format(fCode))
+            names.append('{}_centerWeight.nrrd'.format(fCode))
+   return names
+ 
+def getFitParFinalName(code,nc,j,aType):
+   fCode='{}_{}_{}'.format(code,nc,j+1)
+   if aType!='global':
+      fCode='{}_{}'.format(fCode,aType)
+   
+   return '{}_fitParFinal.txt'.format(fCode)
+   
+
+def printFitParFinalRowNames(r,setup,aType):
+   names=[]
+   nr=setup['nr']
+   nclass=setup['nclass']
+   code=getCode(r,setup)
+   #tempDir=config.getTempDir(setup)
+   for nc in nclass:
+      for j in range(nr):
+         names.append(getFitParFinalName(code,nc,j,aType))
+         #names.append('{}_centerWeight.nrrd'.format(fCode))
+   return names
+ 
+
 

+ 65 - 2
pythonScripts/getData.py

@@ -1,6 +1,27 @@
 import os
 import sys
 import config
+import analysis
+import json
+
+def connectDB(server):
+   sPath=os.path.join(os.path.expanduser('~'),'.labkey','setup.json')
+   with open(sPath,'r') as f:
+      setup=json.load(f)
+
+   sys.path.append(setup['paths']['nixWrapper'])
+   import nixWrapper
+   nixWrapper.loadLibrary('labkeyInterface')
+   import labkeyInterface
+
+
+   net=labkeyInterface.labkeyInterface()
+   fconfig=os.path.join(os.path.expanduser('~'),'.labkey',server)
+   net.init(fconfig)
+   net.getCSRF()
+   import labkeyDatabaseBrowser
+   import labkeyFileBrowser
+   return labkeyDatabaseBrowser.labkeyDB(net),labkeyFileBrowser.labkeyFileBrowser(net)
 
 def getDataset(db,setup,datasetName,qfilter=[]):
     ds=db.selectRows(setup['project'],setup['schemaName'],\
@@ -62,9 +83,51 @@ def downloadFiles(fb,r,setup):
     fileName=config.getNodeName(r,setup,'Dummy')+'.mcsv'
     downloadNode(fb,fileName,rPath,lPath)
     
-def downloadPatientFiles(db,fb,setup):
-    rows=getPatients(db,setup)
+def downloadPatientFiles(db,fb,setup,qfilter=[]):
+    rows=getPatients(db,setup,qfilter)
     for r in rows:
         #download
         downloadFiles(fb,r,setup)
+
+def uploadCenters(fb,r,setup):
+   names=config.printRowCenterNames(r,setup)
+   copyToServer(fb,r,setup,names)
+
+def downloadCenters(fb,r,setup):
+   names=config.printRowCenterNames(r,setup)
+   copyFromServer(fb,r,setup,names)
+
+def uploadFitParFinal(fb,r,setup,mode):
+   names=config.printFitParFinalRowNames(r,setup,mode)
+   #copy files to server
+   copyToServer(fb,r,setup,names)
+
+def copyToServer(fb,r,setup,names):
+   tempDir=config.getTempDir(setup)
+   code=config.getCode(r,setup)
+   project=setup['project']
+   remoteDir=fb.buildPathURL(project,config.getPathList(r,setup))
+   for n in names:
+      localPath=os.path.join(tempDir,code,n)
+      remotePath='{}/{}'.format(remoteDir,n)
+      fb.writeFileToFile(localPath,remotePath)
+
+def copyFromServer(fb,r,setup,names):
+   try:
+      forceReload=setup['forceReload']
+   except KeyError:
+      forceReload=False
+
+   tempDir=config.getTempDir(setup)
+   code=config.getCode(r,setup)
+   project=setup['project']
+   remoteDir=fb.buildPathURL(project,config.getPathList(r,setup))
+   for n in names:
+      localPath=os.path.join(tempDir,code,n)
+      if os.path.isfile(localPath) and not forceReload:
+         continue
+      remotePath='{}/{}'.format(remoteDir,n)
+      fb.readFileToFile(localPath,remotePath)
+
+