dataTransfer.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import os
  2. import sys
  3. import json
  4. def loadData(parFile):
  5. #parameterFile is a json with settings
  6. with open(parFile,'r') as f:
  7. pars=json.load(f)
  8. ds=openData(pars['data']['filename'],pars['data']['format'])
  9. #var names are here
  10. #print(ds[0])
  11. #open data
  12. transferToDestination(ds,pars)
  13. def openData(qFile,qFormat="DTA"):
  14. if qFormat=="DTA":
  15. import pandas as pd
  16. ds=pd.read_stata(qFile)
  17. #print(ds.dtypes)
  18. rows=[]
  19. n=len(ds.columns)
  20. rng=range(len(ds.columns))
  21. for row in ds.itertuples(index=False):
  22. varRow={ds.columns[i]:row[i] for i in rng if not pd.isna(row[i])}
  23. rows.append(varRow)
  24. return rows
  25. def transferToDestination(rows,pars):
  26. dest=pars['destination']
  27. if dest['format']=='LabKey':
  28. return transferToLabKey(rows,pars)
  29. def transferToLabKey(rows,pars):
  30. src=pars['data']
  31. dest=pars['destination']
  32. fsetup=os.path.join(os.path.expanduser('~'),'.labkey','setup.json')
  33. with open(fsetup,'r') as f:
  34. setup=json.load(f)
  35. sys.path.append(setup['paths']['nixWrapper'])
  36. import nixWrapper
  37. nixWrapper.loadLibrary('labkeyInterface')
  38. import labkeyInterface
  39. import labkeyDatabaseBrowser
  40. net=labkeyInterface.labkeyInterface()
  41. net.init(dest['server'])
  42. db=labkeyDatabaseBrowser.labkeyDB(net)
  43. for r in rows:
  44. idField=dest['participantField']
  45. idValue=r[src['idField']]
  46. idFilter={'variable':idField,'value':'{}'.format(idValue),'oper':'eq'}
  47. try:
  48. sField=dest['visitField']
  49. except KeyError:
  50. sField='SequenceNum'
  51. try:
  52. seqNum=src['visitField']
  53. except KeyError:
  54. seqNum="0.0"
  55. visitFilter={'variable':sField,'value':seqNum,'oper':'eq'}
  56. ds=db.selectRows(dest['project'],dest['schema'],dest['query'],
  57. [idFilter,visitFilter])
  58. if len(ds['rows'])>0:
  59. mode='update'
  60. outRow=ds['rows'][0]
  61. else:
  62. mode='insert'
  63. outRow={idField:idValue,sField:seqNum}
  64. for var in r.keys():
  65. outRow[var]=r[var]
  66. resp=db.modifyRows(mode,dest['project'],dest['schema'],dest['query'],
  67. [outRow])
  68. print(resp)
  69. print('Done')
  70. if __name__=="__main__":
  71. loadData(sys.argv[1])