orthancDatabaseBrowser.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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):
  46. url=self.net.getCoreURL()
  47. url+='/instances/'+orthancInstanceId+'/content/'+dicomField
  48. response=self.net.get(url)
  49. try:
  50. encoding=chardet.detect(response.data)["encoding"]
  51. return response.data.decode(encoding)
  52. except TypeError:
  53. return ''
  54. def selectRows(self, qfilter):
  55. obj={}
  56. obj["Level"]="Instance"
  57. obj["Query"]=qfilter
  58. jsonData=json.dumps(obj)
  59. url=self.net.getCoreURL()
  60. url+='/tools/find'
  61. response=self.net.post(url,'json',jsonData)
  62. encoding=chardet.detect(response.data)["encoding"]
  63. return json.loads(response.data.decode(encoding))
  64. def upload(self, f) :
  65. url=self.net.getCoreURL()
  66. url+='/instances'
  67. fobj=open (f, 'rb')
  68. response=self.net.post(url,'octet-stream',fobj.read())
  69. try:
  70. encoding=chardet.detect(response.data)["encoding"]
  71. except AttributeError:
  72. print(response)
  73. return json.loads('{"status":"BAD"}')
  74. return json.loads(response.data.decode(encoding))
  75. def remove(self,level,orthancId):
  76. url=self.net.getCoreURL()
  77. url+='/'+level+'/'+orthancId
  78. response=self.net.delete(url)
  79. print(response.data)
  80. def changes(self,limit=10,since=0):
  81. url=self.net.getCoreURL()
  82. url+=f'/changes?limit={limit}&since={since}'
  83. response=self.net.get(url)
  84. return extractJSON(response.data)