orthancDatabaseBrowser.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import orthancInterface
  2. import json
  3. import chardet
  4. import io
  5. import sys
  6. def extractJSON(data):
  7. encoding=chardet.detect(data)["encoding"]
  8. #try with a set of encodings to maximize probability of success
  9. encodings=[encoding,'utf_8']
  10. for x in encodings:
  11. try:
  12. return json.loads(data.decode(x))
  13. except UnicodeDecodeError:
  14. pass
  15. print('Failed to decode with [{}]: {}'.format(x,data))
  16. return None
  17. class orthancDB:
  18. def __init__(self,net):
  19. self.net=net
  20. def getList(self,category):
  21. url=self.net.getCoreURL()
  22. url+='/'+category
  23. response=self.net.get(url)
  24. return extractJSON(response.data)
  25. def getPatients(self):
  26. return self.getList('patients')
  27. def getStudies(self):
  28. return self.getList('studies')
  29. def getPatientData(self, orthancId):
  30. return self.getData('patients',orthancId)
  31. def getStudyData(self, studyOrthancId):
  32. return self.getData('studies',studyOrthancId)
  33. def getSeriesData(self, seriesOrthancId):
  34. return self.getData('series',seriesOrthancId)
  35. def getNumpy(self,level,orthancId):
  36. url=self.net.getCoreURL()
  37. url+='/'+level+'/'+orthancId+'/numpy'
  38. response=self.net.get(url,binary=True)
  39. return io.BytesIO(response.data)
  40. def getData(self, level, orthancId):
  41. url=self.net.getCoreURL()
  42. url+='/'+level+'/'+orthancId
  43. response=self.net.get(url)
  44. return extractJSON(response.data)
  45. def getDicomField(self,orthancInstanceId,dicomField,raw=False):
  46. url=self.net.getCoreURL()
  47. url+='/instances/'+orthancInstanceId+'/content/'+dicomField
  48. response=self.net.get(url)
  49. if raw:
  50. return response.data
  51. try:
  52. encoding=chardet.detect(response.data)["encoding"]
  53. return response.data.decode(encoding)
  54. except TypeError:
  55. return ''
  56. def selectRows(self, qfilter):
  57. obj={}
  58. obj["Level"]="Instance"
  59. obj["Query"]=qfilter
  60. jsonData=json.dumps(obj)
  61. url=self.net.getCoreURL()
  62. url+='/tools/find'
  63. response=self.net.post(url,'json',jsonData)
  64. encoding=chardet.detect(response.data)["encoding"]
  65. return json.loads(response.data.decode(encoding))
  66. def upload(self, f) :
  67. url=self.net.getCoreURL()
  68. url+='/instances'
  69. fobj=open (f, 'rb')
  70. response=self.net.post(url,'octet-stream',fobj.read())
  71. try:
  72. encoding=chardet.detect(response.data)["encoding"]
  73. except AttributeError:
  74. print(response)
  75. return json.loads('{"status":"BAD"}')
  76. return json.loads(response.data.decode(encoding))
  77. def remove(self,level,orthancId):
  78. url=self.net.getCoreURL()
  79. url+='/'+level+'/'+orthancId
  80. response=self.net.delete(url)
  81. print(response.data)
  82. def changes(self,limit=10,since=0):
  83. url=self.net.getCoreURL()
  84. #url+=f'/changes?limit={limit}&since={since}'
  85. url+='/changes?limit={}&since={}'.format(limit,since)
  86. response=self.net.get(url)
  87. return extractJSON(response.data)