anonymizeImages.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. imageDataset='Imaging1'
  31. ds=db.selectRows(project,'study',imageDataset,[])
  32. imageSelector=["CT","PETWB"];
  33. outputBase='/data/dicom/anonymous'
  34. labkeyBase='/data/labkey'
  35. #for links
  36. projectAnonymousBase=os.path.join(labkeyBase,'files',project,'@files/anonymous')
  37. #selected dicom fields
  38. dsFields=db.selectRows(project,'lists','dicomFields',[])
  39. try:
  40. fields=[r['dicomTagName'] for r in dsFields['rows']]
  41. except KeyError:
  42. fields=[r['dicomtagname'] for r in dsFields['rows']]
  43. i=0
  44. uid=loadDicom.uuid()
  45. for row in ds["rows"]:
  46. #link dicoms with a common studiyID
  47. studyUID=uid.generateStudyUUID('volume')
  48. for im in imageSelector:
  49. linkField=im+"2"
  50. print("Checking row[{}]={}".format(linkField,row[linkField]))
  51. if row[linkField]=="[DICOM]":
  52. print("Skipping {}".format(im))
  53. continue
  54. seriesId=row[im];
  55. if seriesId=="0":
  56. continue
  57. if seriesId==None:
  58. continue
  59. print("{}: {}".format(im,seriesId))
  60. fname=os.path.join(tempBase,seriesId+".zip");
  61. ofb.getZip('series',seriesId,fname)
  62. unzipDir=os.path.join(tempBase,seriesId)
  63. try:
  64. os.mkdir(unzipDir)
  65. except FileExistsError:
  66. shutil.rmtree(unzipDir)
  67. try:
  68. outTxt=subprocess.check_output(["unzip","-d",unzipDir,"-xj",fname])
  69. except subprocess.CalledProcessError:
  70. print("unzip failed for {}".format(fname))
  71. continue
  72. slices=loadDicom.load(unzipDir)
  73. anonymousDir=os.path.join(tempBase,seriesId+'-a')
  74. try:
  75. os.mkdir(anonymousDir)
  76. except FileExistsError:
  77. shutil.rmtree(anonymousDir)
  78. os.mkdir(anonymousDir)
  79. patientID="XXXXXX"
  80. loadDicom.writeAnonymousSeries(slices,anonymousDir,patientID, studyUID,fields)
  81. anonymousZip=os.path.join(outputBase,seriesId+".zip")
  82. try:
  83. outTxt=subprocess.check_output(["zip","-rj",anonymousZip,anonymousDir])
  84. except subprocess.CalledProcessError:
  85. print("zip failed for {}, dir {}".format(anonymousZip,anonymousDir))
  86. shutil.rmtree(unzipDir)
  87. shutil.rmtree(anonymousDir)
  88. os.remove(fname)
  89. labkeyAnonymousZip=os.path.join(projectAnonymousBase,seriesId+'.zip')
  90. try:
  91. os.symlink(anonymousZip,labkeyAnonymousZip)
  92. except FileExistsError:
  93. pass
  94. row[linkField]="[DICOM]"
  95. db.modifyRows("update",project,"study",imageDataset,[row])
  96. if i==-1:
  97. break
  98. i=i+1
  99. print("Done")