makeAnonymizeLinks.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import os
  2. import json
  3. import re
  4. import subprocess
  5. import nibabel
  6. import shutil
  7. import sys
  8. shome=os.path.expanduser('~nixUser')
  9. sys.path.insert(1,shome+'/software/src/labkeyInterface')
  10. import labkeyInterface
  11. import labkeyDatabaseBrowser
  12. sys.path.insert(1,shome+'/software/src/orthancInterface')
  13. import orthancInterface
  14. import orthancFileBrowser
  15. sys.path.insert(1,shome+'/software/src/IPNUMM/dicomUtils')
  16. import loadDicom
  17. fhome=os.path.expanduser('~')
  18. fconfig=os.path.join(fhome,'.labkey','network.json')
  19. net=labkeyInterface.labkeyInterface()
  20. net.init(fconfig)
  21. db=labkeyDatabaseBrowser.labkeyDB(net)
  22. onet=orthancInterface.orthancInterface()
  23. onet.init(fconfig)
  24. ofb=orthancFileBrowser.orthancFileBrowser(onet)
  25. hi=0
  26. project='iPNUMMretro/Study'
  27. #project='Orthanc/Database'
  28. tempBase=os.path.join(fhome,'temp')
  29. #all images from database
  30. ds=db.selectRows(project,'study','Imaging',[])
  31. imageSelector=["CT","PETWB"];
  32. niftiBase='/data/nifti'
  33. labkeyBase='/data1/labkey'
  34. projectNIfTIBase=os.path.join(labkeyBase,'/files/',project,'/@files/nifti')
  35. i=0
  36. for row in ds["rows"]:
  37. for im in imageSelector:
  38. seriesId=row[im];
  39. print("{}: {}".format(im,seriesId))
  40. fname=os.path.join(tempBase,seriesId+".zip");
  41. ofb.getZip('series',seriesId,fname)
  42. unzipDir=os.path.join(tempBase,seriesId)
  43. try:
  44. os.mkdir(unzipDir)
  45. except FileExistsError:
  46. shutil.rmtree(unzipDir)
  47. try:
  48. outTxt=subprocess.check_output(["unzip","-d",unzipDir,"-xj",fname])
  49. except subprocess.CalledProcessError:
  50. print("unzip failed for {}".format(fname))
  51. continue
  52. slices=loadDicom.load(unzipDir)
  53. img=loadDicom.convertToNIfTI(slices)
  54. outNIfTI=os.path.join(niftiBase,seriesId+'.nii.gz')
  55. nibabel.save(img,outNIfTI)
  56. shutil.rmtree(unzipDir)
  57. os.remove(fname)
  58. labkeyNIfTI=os.path.join(projectNIfTIBase,seriesId+'.nii.gz')
  59. try:
  60. os.symlink(labkeyNIfTI,outNIfTI)
  61. except FileExistsError:
  62. pass
  63. if i==0:
  64. break
  65. i=i+1
  66. print("Done")