def testBuildJsonSR(self, *_): study_json_copy = _MOCK_STUDY_JSON.copy() builder = dicom_builder.DicomBuilder() dicom_json_sr = builder.BuildJsonSR(_REPORT_CONTENT, _MOCK_STUDY_JSON) # Ensure the study json is not affected by the builder. self.assertEqual(_MOCK_STUDY_JSON, study_json_copy) # Test the UIDs, order doesn't matter. self.assertCountEqual( (dicom_json_sr.series_uid, dicom_json_sr.instance_uid), (_UID1, _UID2)) dicom_dict = dicom_json_sr.dicom_dict # Test Study Level Tags for item in _MOCK_STUDY_JSON: self.assertIn(item, dicom_dict) self._AssertJsonTag(dicom_dict, tags.STUDY_INSTANCE_UID, _STUDY_UID) # Test instance level tags. self._AssertJsonTag(dicom_dict, tags.SPECIFIC_CHARACTER_SET, dicom_builder._ISO_CHARACTER_SET) self._AssertJsonTag(dicom_dict, tags.MEDIA_STORAGE_SOP_INSTANCE_UID, dicom_json_sr.instance_uid) self._AssertJsonTag(dicom_dict, tags.MEDIA_STORAGE_SOP_CLASS_UID, tag_values.BASIC_TEXT_SR_CUID) self._AssertJsonTag(dicom_dict, tags.SOP_CLASS_UID, tag_values.BASIC_TEXT_SR_CUID) self._AssertJsonTag(dicom_dict, tags.MODALITY, tag_values.SR_MODALITY) # Test content sequence tags including inference text. content_sequence = dicom_json.GetValue(dicom_dict, tags.CONTENT_SEQUENCE) self._AssertJsonTag(content_sequence, tags.TEXT_VALUE, _REPORT_CONTENT) self._AssertJsonTag(content_sequence, tags.VALUE_TYPE, 'TEXT') self._AssertJsonTag(content_sequence, tags.RELATIONSHIP_TYPE, 'CONTAINS')
def BuildJsonSC(self, image_array: np.ndarray, metadata_json: Dict[Text, Any], series_uid: Text) -> dicom_json.ObjectWithBulkData: """Builds and returns a DICOM Secondary Capture. Args: image_array: Image array (RGB) to embed in DICOM instance. metadata_json: Dict of tags (including study-level information) to add. series_uid: UID of the series to create the SC in. Returns: DICOM JSON Object containing JSON and bulk data of the Secondary Capture. """ # Copy over any study and instance level tags. instance_uid = self.GenerateUID() metadata_json = metadata_json.copy() dicom_json.Insert(metadata_json, tags.SOP_CLASS_UID, tag_values.SECONDARY_CAPTURE_CUID) dicom_json.Insert(metadata_json, tags.MODALITY, tag_values.OT_MODALITY) dicom_json.Insert(metadata_json, tags.SERIES_INSTANCE_UID, series_uid) dicom_json.Insert(metadata_json, tags.SPECIFIC_CHARACTER_SET, _ISO_CHARACTER_SET) dicom_json.Insert(metadata_json, tags.SOP_INSTANCE_UID, instance_uid) dicom_json.Insert(metadata_json, tags.TRANSFER_SYNTAX_UID, _IMPLICIT_VR_LITTLE_ENDIAN) dicom_json.Insert(metadata_json, tags.MEDIA_STORAGE_SOP_CLASS_UID, tag_values.SECONDARY_CAPTURE_CUID) dicom_json.Insert(metadata_json, tags.MEDIA_STORAGE_SOP_INSTANCE_UID, instance_uid) # Assures URI is unique. study_uid = dicom_json.GetValue(metadata_json, tags.STUDY_INSTANCE_UID) uri = '{}/{}/{}'.format(study_uid, series_uid, instance_uid) metadata_json[tags.PIXEL_DATA.number] = { 'vr': tags.PIXEL_DATA.vr, 'BulkDataURI': uri } dicom_json.Insert(metadata_json, tags.PHOTOMETRIC_INTERPRETATION, 'RGB') dicom_json.Insert(metadata_json, tags.SAMPLES_PER_PIXEL, 3) # Indicates we store pixel data as R1,G1,B1,R2,G2,B2... dicom_json.Insert(metadata_json, tags.PLANAR_CONFIGURATION, 0) dicom_json.Insert(metadata_json, tags.ROWS, image_array.shape[0]) dicom_json.Insert(metadata_json, tags.COLUMNS, image_array.shape[1]) dicom_json.Insert(metadata_json, tags.BITS_ALLOCATED, 8) dicom_json.Insert(metadata_json, tags.BITS_STORED, 8) dicom_json.Insert(metadata_json, tags.HIGH_BIT, 7) dicom_json.Insert(metadata_json, tags.PIXEL_REPRESENTATION, 0) bulkdata = dicom_web.DicomBulkData( uri=uri, data=image_array.tobytes(), content_type='application/octet-stream') return dicom_json.ObjectWithBulkData(metadata_json, [bulkdata])
def _AssertJsonTag(self, dicomjson: Dict[Text, Any], tag: tags.DicomTag, expected: Text) -> None: """Given a DICOM JSON asserts whether the tag's value is the expected value. Args: dicomjson: Dict representing the DICOM JSON structure. tag: A tuple representing a DICOM Tag and its associated VR. expected: String value to compare the value of the tag in the dicomjson to. """ self.assertEqual(dicom_json.GetValue(dicomjson, tag), expected)
def _IsMammoInstance(instance_path): # type: dicom_path.Path -> bool """Returns whether the DICOM instance is of type MG modality. Args: instance_path: dicom_path.Path of DICOM instance. Returns: ParsedMessage or None if the message is invalid. """ series_url = posixpath.join(_HEALTHCARE_API_URL_PREFIX, str(instance_path.GetSeriesPath())) qido_url = ('%s/instances?SOPInstanceUID=%s&includefield=%s' % (series_url, instance_path.instance_uid, tags.MODALITY.number)) parsed_content = _QidoRs(qido_url)[0] modality = dicom_json.GetValue(parsed_content, tags.MODALITY) if modality is None or modality != tag_values.MG_MODALITY: return False return True
def testGetValue(self, tag, expected_value): """Tests expected return values for `GetValue()`.""" actual_value = dicom_json.GetValue(_DICOM_JSON, tag) self.assertEqual(actual_value, expected_value)