Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor date and time shifting #152

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 15 additions & 28 deletions src/dicognito/datetimeanonymizer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from itertools import zip_longest
from typing import Iterator, MutableSequence
import pydicom

Expand Down Expand Up @@ -66,45 +67,31 @@ def _anonymize_date_and_time(self, dataset: pydicom.dataset.Dataset, data_elemen

new_dates = []
new_times = []
for i in range(len(dates)):
date_value = dates[i]
date_format = "%Y%m%d"
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:])

for date, time in zip_longest(dates, times, fillvalue=""):
new_datetime = self._shift_datetime(date + time)

new_dates.append(new_datetime[:8])
new_times.append(new_datetime[8:])

data_element.value = new_dates
if times:
time_element.value = new_times # type: ignore[union-attr]

def _anonymize_datetime(self, dataset: pydicom.dataset.Dataset, data_element: pydicom.DataElement) -> None:
datetimes = self._get_value_as_sequence(data_element)
data_element.value = [self._shift_datetime(datetime_value) for datetime_value in datetimes]

new_datetimes = []
for datetime_value in datetimes:
datetime_format = "%Y%m%d%H"[: len(datetime_value) - 2]
def _shift_datetime(self, datetime_value: str) -> str:
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
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)
new_datetime_value = new_datetime.strftime(datetime_format)
new_datetime_value += datetime_value[len(new_datetime_value) :]

data_element.value = new_datetimes
return new_datetime_value

def _get_value_as_sequence(self, data_element: pydicom.DataElement) -> MutableSequence[str]:
if isinstance(data_element.value, pydicom.multival.MultiValue):
Expand Down
Loading