copyAnonymizedDataset.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import os
  2. import json
  3. import re
  4. import subprocess
  5. import shutil
  6. import sys
  7. import numpy
  8. import pydicom
  9. import copy
  10. shome=os.path.expanduser('~nixUser')
  11. sys.path.insert(1,shome+'/software/src/labkeyInterface')
  12. import labkeyInterface
  13. import labkeyDatabaseBrowser
  14. fhome=os.path.expanduser('~')
  15. fconfig=os.path.join(fhome,'.labkey','network.json')
  16. net=labkeyInterface.labkeyInterface()
  17. net.init(fconfig)
  18. db=labkeyDatabaseBrowser.labkeyDB(net)
  19. fb=labkeyFileBrowser.labkeyFileBrowser(net)
  20. tempDir=os.path.join(os.path.expanduser('~'),'temp')
  21. #also need merlin credentials
  22. fconfigMerlin=os.path.join(fhome,'.labkey','merlin.json')
  23. netMerlin=labkeyInterface.labkeyInterface()
  24. netMerlin.init(fconfigMerlin)
  25. dbMerlin=labkeyDatabaseBrowser.labkeyDB(netMerlin)
  26. fbMerlin=labkeyFileBrowser.labkeyFileBrowser(netMerlin)
  27. project='iPNUMMretro/Study'
  28. merlinProject=project
  29. #project='Orthanc/Database'
  30. anonymousDataset='AnonymousClinicalData'
  31. merlinDataset='ClinicalData'
  32. #getNixID
  33. studyData=db.selectRows(project,'study','Study',[])
  34. nixID=studyData['rows'][0]['nixID']
  35. ds=db.selectRows(project,'study',anonymousDataset,[])
  36. for row in ds['rows']:
  37. outRow=copy.deepcopy(row)
  38. outRow['PatientId']='{}-{}'.format(nixID,row['PatientId'])
  39. dbMerlin.modifyRows('insert',merlinProject,'study',merlinDataset,[outRow])
  40. #
  41. print('Done')
  42. quit()
  43. imagingDataset="AnonymousImaging1?"
  44. merlinImagingDataset="Imaging1"
  45. imageDir="preprocessedImages"
  46. ds=db.selectRows(project,'study',imagingDataset,[],)
  47. for row in ds['rows']:
  48. ofiles=[row['preprocessedCT'],row['preprocessedPET']]
  49. pCode=row['PatientCode']
  50. vCode=row['visitCode']
  51. for ofile in ofiles:
  52. xPath=[imageDir,pCode,vCode]
  53. xPath.append(ofile)
  54. path='/'.join(xPath)
  55. localPath=os.path.join(tempDir,ofile)
  56. remotePath=fb.formatPathURL(project,path)
  57. fb.readFileToFile(remotePath,localPath)
  58. merlinPath=fbMerlin.buildPathURL(merlinProject,xPath)
  59. fbMerlin.writeFileToFile(localPath,merlinPath)
  60. outRow=copy.deepcopy(row)
  61. outRow['PatientId']='{}-{}'.format(nixID,row['PatientId'])
  62. dbMerlin.modifyRows('insert',merlinProject,'study',merlinImagingDataset,[outRow])