def test_Response_DicomJson(self): response = odil.webservices.QIDORSResponse() response.set_representation(odil.webservices.Utils.Representation.DICOM_JSON) response.set_data_sets(self.data_sets) self.assertEqual(response.get_media_type(), "application/dicom+json") self.assertEqual(response.get_representation(), odil.webservices.Utils.Representation.DICOM_JSON) http = response.get_http_response() content_type = http.get_header("Content-Type") self.assertEqual(content_type, "application/dicom+json") json_http = json.loads(http.get_body().decode()) json_ds = [ json.loads(odil.as_json(x)) for x in self.data_sets ] self.assertEqual(json_ds, json_http)
def test_respondDICOM_JSON(self): response = odil.webservices.STOWRSResponse() response.set_representation(odil.webservices.Utils.Representation.DICOM_JSON) response.set_store_instance_responses(self.data_set) self.assertEqual(response.get_media_type(), "application/dicom+json") self.assertEqual(response.get_representation(), odil.webservices.Utils.Representation.DICOM_JSON) http = response.get_http_response() msg = self._http_message_to_email_message(http) self.assertFalse(msg.is_multipart()) self.assertTrue(msg.get_content_type(), "application/dicom+json") odil_json = [json.loads(odil.as_json(self.data_set))] http_json = json.loads(http.get_body().decode()) self.assertSequenceEqual(http_json, odil_json)
def test_respondDICOMJSON(self): wado = odil.webservices.WADORSResponse() wado.set_data_sets(self.data_sets) wado.respond_dicom(odil.webservices.Utils.Representation.DICOM_JSON) self.assertEqual(wado.get_type(), odil.webservices.Utils.Type.DICOM) self.assertEqual(wado.get_representation(), odil.webservices.Utils.Representation.DICOM_JSON) http = wado.get_http_response() msg = self._http_message_to_email_message(http) self.assertFalse(msg.is_multipart()) self.assertTrue(msg.get_content_type(), "application/dicom+json") odil_json = [json.loads(odil.as_json(x)) for x in self.data_sets] http_json = json.loads(http.get_body().decode()) self.assertSequenceEqual(http_json, odil_json)
def test_respondDICOM_JSON(self): response = odil.webservices.STOWRSResponse() response.set_representation( odil.webservices.Utils.Representation.DICOM_JSON) response.set_store_instance_responses(self.data_set) self.assertEqual(response.get_media_type(), "application/dicom+json") self.assertEqual(response.get_representation(), odil.webservices.Utils.Representation.DICOM_JSON) http = response.get_http_response() msg = self._http_message_to_email_message(http) self.assertFalse(msg.is_multipart()) self.assertTrue(msg.get_content_type(), "application/dicom+json") odil_json = [json.loads(odil.as_json(self.data_set))] http_json = json.loads(http.get_body().decode()) self.assertSequenceEqual(http_json, odil_json)
def convert_element(element, specific_character_set): """ Convert a DICOM element to its NIfTI+JSON representation: the "Value" (or "InlineBinary") attribute of its standard DICOM JSON representation. """ result = None # element can be None because a get is called above if element is None: result = None elif element.empty(): result = None elif element.is_int(): result = list(element.as_int()) elif element.is_real(): result = list(element.as_real()) elif element.vr == odil.VR.PN: data_set = odil.DataSet() if specific_character_set: data_set.add("SpecificCharacterSet", specific_character_set) data_set.add(odil.registry.PersonName, element.as_string(), element.vr) json_data_set = json.loads(odil.as_json(data_set)) result = json_data_set[str(odil.registry.PersonName)]["Value"] elif element.is_string(): result = list( odil.as_unicode(x, specific_character_set) for x in element.as_string()) elif element.is_data_set(): result = [ convert_data_set(x, specific_character_set) for x in element.as_data_set() ] elif element.is_binary(): result = [ base64.b64encode(x.get_memory_view().tobytes()).decode() for x in element.as_binary() ] else: raise Exception("Unknown element type") return result
def convert_element(element, specific_character_set): """ Convert a DICOM element to its NIfTI+JSON representation: the "Value" (or "InlineBinary") attribute of its standard DICOM JSON representation. """ result = None # element can be None because a get is called above if element is None: result = None elif element.empty(): result = None elif element.is_int(): result = list(element.as_int()) elif element.is_real(): result = list(element.as_real()) elif element.is_string(): if element.vr.name in ["LO", "LT", "PN", "SH", "ST", "UT"]: data_set = odil.DataSet() if specific_character_set: data_set.add(odil.registry.SpecificCharacterSet, specific_character_set) data_set.add(odil.Tag(0xffff, 0xffff), element.as_string(), element.vr) result = json.loads(odil.as_json(data_set))["ffffffff"]["Value"] else: result = list(element.as_string()) elif element.is_data_set(): result = [ convert_data_set(x, specific_character_set) for x in element.as_data_set() ] elif element.is_binary(): result = [ base64.b64encode(x.get_memory_view().tobytes()) for x in element.as_binary() ] else: raise Exception("Unknown element type") return result
def test_respondDICOM_JSON(self): response = odil.webservices.STOWRSResponse() response.set_representation(odil.webservices.Utils.Representation.DICOM_JSON) response.set_store_instance_responses(self.data_set) self.assertEqual(response.get_media_type(), "application/dicom+json") self.assertEqual(response.get_representation(), odil.webservices.Utils.Representation.DICOM_JSON) http = response.get_http_response() # Convert http response into string http_str = "" h = [] for header in http.get_headers(): h.append(header.key()) for header in h: http_str += header + ": " + http.get_header(header) http_str += "\r\n" + http.get_body() msg = email.message_from_string(http_str) self.assertFalse(msg.is_multipart()) self.assertTrue(msg.get_content_type(), "application/dicom+json") odil_json = [json.loads(odil.as_json(self.data_set))] http_json = json.loads(http.get_body()) self.assertSequenceEqual(http_json, odil_json)
def test_respondDICOMJSON(self): wado = odil.webservices.WADORSResponse() wado.set_data_sets(self.data_sets) wado.respond_dicom(odil.webservices.Utils.Representation.DICOM_JSON) self.assertEqual(wado.get_type(), odil.webservices.Utils.Type.DICOM) self.assertEqual(wado.get_representation(), odil.webservices.Utils.Representation.DICOM_JSON) http = wado.get_http_response() # Convert http response into string http_str = "" h = [] for header in http.get_headers(): h.append(header.key()) for header in h: http_str += header + ": " + http.get_header(header) http_str += "\r\n" + http.get_body() msg = email.message_from_string(http_str) self.assertFalse(msg.is_multipart()) self.assertTrue(msg.get_content_type(), "application/dicom+json") odil_json = [json.loads(odil.as_json(x)) for x in self.data_sets] http_json = json.loads(http.get_body()) self.assertSequenceEqual(http_json, odil_json)
def as_json(input, output, pretty_print): with odil.open(input) as stream: _, data_set = odil.Reader.read_file(stream) with open(output, "w") as fd: json = odil.as_json(data_set, pretty_print) fd.write(json)
def test_pretty_print(self): serialized = odil.as_json(self.data_set, True) self.assertEqual(re.sub(r"\s+", "", serialized), self.json)
def as_json(input, output, pretty_print): _, data_set = odil.read(input) with open(output, "w") as fd: json = odil.as_json(data_set, pretty_print) fd.write(json)