def __call__(self, dataset: pydicom.dataset.Dataset, data_element: pydicom.DataElement) -> bool: """\ Potentially anonymize a single DataElement, replacing its value with something that obscures the patient's identity. Parameters ---------- dataset : pydicom.dataset.Dataset The dataset to operate on. data_element : pydicom.dataset.DataElement The current element. Will be anonymized if its VR is UI. If multi-valued, each item will be anonymized independently. Returns ------- True if the element was anonymized, or False if not. """ if ( data_element.VR != "UI" or not data_element.value or pydicom.datadict.keyword_for_tag(data_element.tag).endswith("ClassUID") or data_element.tag == pydicom.datadict.tag_for_keyword("TransferSyntaxUID") ): return False if isinstance(data_element.value, pydicom.multival.MultiValue): data_element.value = list([self._ui_map[v] for v in data_element.value]) else: data_element.value = self._ui_map[data_element.value] return True
def update_uids_cb(ds: Dataset, elem: DataElement) -> None: """Callback for updating UID values except `SOPClassUID`""" if elem.VR == "UI" and elem.keyword != "SOPClassUID": if elem.VM > 1: elem.value = [ generate_uid(uid_prefix, [x] + add_uid_entropy) # type: ignore for x in elem.value ] else: elem.value = generate_uid( uid_prefix, [elem.value] + add_uid_entropy # type: ignore )
def __call__(self, dataset: pydicom.dataset.Dataset, data_element: pydicom.DataElement) -> bool: """\ Potentially anonymize a single DataElement, replacing its value with something that obscures the patient's identity. Parameters ---------- dataset : pydicom.dataset.Dataset The dataset to operate on. data_element : pydicom.dataset.DataElement The current element. Will be anonymized if it has a value and if its keyword is one of PatientAddress, RegionOfResidence, or CountryOfResidences. Returns ------- True if the element was anonymized, or False if not. """ value_factory = self._value_factories.get(data_element.tag, None) if not value_factory: return False if not data_element.value: return True data_element.value = value_factory(data_element.value) return True
def __call__(self, dataset: pydicom.dataset.Dataset, data_element: pydicom.DataElement) -> bool: """\ Potentially anonymize a single DataElement, replacing its value with something that obscures the patient's identity. Parameters ---------- dataset : pydicom.dataset.Dataset The dataset to operate on. data_element : pydicom.dataset.DataElement The current element. Will be anonymized if it has a value and if its keyword matches one of the keywords supplied when creating this anonymizer or matches IssuerOfPatientID. The element may be multi-valued, in which case each item is anonymized independently. Returns ------- True if the element was anonymized, or False if not. """ if data_element.tag in self.id_tags: self._replace_id(data_element) return True if self._anonymize_mitra_global_patient_id(dataset, data_element): return True if data_element.tag == self.issuer_tag and data_element.value: data_element.value = "DICOGNITO" return True return False
def _anonymize_mitra_global_patient_id( self, dataset: pydicom.dataset.Dataset, data_element: pydicom.DataElement ) -> bool: if data_element.tag.group == 0x0031 and data_element.tag.element % 0x0020 == 0: private_tag_group = data_element.tag.element >> 8 if dataset[(0x0031 << 16) + private_tag_group].value == "MITRA LINKED ATTRIBUTES 1.0": self._replace_id(data_element) data_element.value = data_element.value.encode() return True return False
def anonymize_institution_name(self, dataset: pydicom.dataset.Dataset, data_element: pydicom.DataElement) -> None: region = self.address_anonymizer.get_region(data_element.value) street_address = self.address_anonymizer.get_street_address( data_element.value) street = street_address.split(" ", 1)[1] dataset.InstitutionAddress = ", ".join([ street_address, region, self.address_anonymizer.get_country(data_element.value) ]) data_element.value = region + "'S " + street + " CLINIC"
def _anonymize_date_and_time(self, dataset: pydicom.dataset.Dataset, data_element: pydicom.DataElement) -> None: date_value = data_element.value if isinstance(data_element.value, pydicom.multival.MultiValue): dates = list([v for v in data_element.value]) else: dates = [data_element.value] times = [] time_name = data_element.keyword.replace("Date", "Time") if time_name in dataset: time_element = dataset.data_element(time_name) time_value = time_element.value # type: ignore[union-attr] if time_value: if isinstance(time_value, pydicom.multival.MultiValue): times = list([v for v in time_value]) else: times = [time_value] new_dates = [] new_times = [] for i in range(len(dates)): date_value = dates[i] date_format = "%Y%m%d"[: len(date_value) - 2] old_date = datetime.datetime.strptime(date_value, date_format).date() time_value = "" old_hours = datetime.time() if i < len(times): time_value = times[i] if time_value: old_hours = datetime.datetime.strptime(time_value[:2], "%H").time() else: old_hours = datetime.time() old_datetime = datetime.datetime.combine(old_date, old_hours) new_datetime = old_datetime + self.offset new_dates.append(new_datetime.strftime(date_format)) new_times.append(new_datetime.strftime("%H") + time_value[2:]) new_dates_string = "\\".join(new_dates) new_times_string = "\\".join(new_times) data_element.value = new_dates_string if times: time_element.value = new_times_string # type: ignore[union-attr]
def _anonymize_datetime(self, dataset: pydicom.dataset.Dataset, data_element: pydicom.DataElement) -> None: if isinstance(data_element.value, pydicom.multival.MultiValue): datetimes = list([v for v in data_element.value]) else: datetimes = [data_element.value] new_datetimes = [] for datetime_value in datetimes: datetime_format = "%Y%m%d%H"[: len(datetime_value) - 2] old_datetime = datetime.datetime.strptime(datetime_value[:10], datetime_format) new_datetime = old_datetime + self.offset new_datetime_value = new_datetime.strftime(datetime_format) new_datetime_value += datetime_value[len(new_datetime_value) :] new_datetimes.append(new_datetime_value) data_element.value = "\\".join(new_datetimes)
def __call__(self, dataset: pydicom.dataset.Dataset, data_element: pydicom.DataElement) -> bool: """\ Potentially anonymize a single DataElement, replacing its value with self.value. Parameters ---------- dataset : pydicom.dataset.Dataset The dataset to operate on. data_element : pydicom.dataset.DataElement The current element. Will be anonymized if its keyword is the same as that for the keyword supplied when creating this anonymizer. Returns ------- True if the element was anonymized, or False if not. """ if data_element.tag == self.tag: data_element.value = self.value return True return False
def anonymize_department_name(self, dataset: pydicom.dataset.Dataset, data_element: pydicom.DataElement) -> None: data_element.value = "RADIOLOGY"
def _replace_id(self, data_element: pydicom.DataElement) -> None: if isinstance(data_element.value, pydicom.multival.MultiValue): data_element.value = [self._new_id(id) for id in data_element.value] else: data_element.value = self._new_id(data_element.value)