Repository: blairconrad/dicognito

Issue:
Date/time offset is not always the same for a given seed
When anonymizing using a set seed, the date (time) offsets will sometimes vary.
To see this, anonymize an object a few times with the same seed.
This is the reason that `test_multivalued_date_and_time_pair_gets_anonymized` fails from time to time.

Comments:

Patch:
diff --git a/src/dicognito/anonymizer.py b/src/dicognito/anonymizer.py
index 80f8b54..b0f6a8b 100644
--- a/src/dicognito/anonymizer.py
+++ b/src/dicognito/anonymizer.py
@@ -12,7 +12,6 @@ from dicognito.unwantedelements import UnwantedElementsStripper
 from dicognito.randomizer import Randomizer
 
 import pydicom
-import random
 
 
 class Anonymizer:
@@ -58,8 +57,14 @@ class Anonymizer:
         """
         minimum_offset_hours = 62 * 24
         maximum_offset_hours = 730 * 24
+
         randomizer = Randomizer(seed)
+
+        date_offset_hours = -(
+            randomizer.to_int("date_offset") % (maximum_offset_hours - minimum_offset_hours) + minimum_offset_hours
+        )
         address_anonymizer = AddressAnonymizer(randomizer)
+
         self._element_handlers = [
             UnwantedElementsStripper(
                 "BranchOfService",
@@ -97,7 +102,7 @@ class Anonymizer:
             EquipmentAnonymizer(address_anonymizer),
             FixedValueAnonymizer("RequestingService", ""),
             FixedValueAnonymizer("CurrentPatientLocation", ""),
-            DateTimeAnonymizer(-random.randint(minimum_offset_hours, maximum_offset_hours)),
+            DateTimeAnonymizer(date_offset_hours),
         ]
 
     def anonymize(self, dataset):
diff --git a/src/dicognito/release_notes.md b/src/dicognito/release_notes.md
index e9933be..4a72ac6 100644
--- a/src/dicognito/release_notes.md
+++ b/src/dicognito/release_notes.md
@@ -8,6 +8,9 @@
 - Deflated files are corrupt when anonymized from the command line
   ([#80](https://github.com/blairconrad/dicognito/issues/80))
 
+- Date/time offset is not always the same for a given seed
+  ([#86](https://github.com/blairconrad/dicognito/issues/86))
+
 
 ## 0.10.0
 


Test patch:
diff --git a/tests/test_anonymize_single_instance.py b/tests/test_anonymize_single_instance.py
index 23a05bc..5cffe58 100644
--- a/tests/test_anonymize_single_instance.py
+++ b/tests/test_anonymize_single_instance.py
@@ -452,6 +452,25 @@ def test_multivalued_date_and_time_pair_gets_anonymized():
     assert len(new_time) == len(original_time)
 
 
+def test_multivalued_date_and_time_pair_gets_anonymized_same_with_same_seed():
+    with load_test_instance() as dataset1, load_test_instance() as dataset2:
+        dataset1.DateOfLastCalibration = original_date = ["20010401", "20010402"]
+        dataset1.TimeOfLastCalibration = original_time = ["120000", "135959"]
+        dataset2.DateOfLastCalibration = original_date
+        dataset2.TimeOfLastCalibration = original_time
+
+        Anonymizer(seed="").anonymize(dataset1)
+        Anonymizer(seed="").anonymize(dataset2)
+
+        new_date1 = dataset1.DateOfLastCalibration
+        new_time1 = dataset1.TimeOfLastCalibration
+        new_date2 = dataset2.DateOfLastCalibration
+        new_time2 = dataset2.TimeOfLastCalibration
+
+    assert new_date1 == new_date2
+    assert new_time1 == new_time2
+
+
 def test_issue_date_of_imaging_service_request_gets_anonymized():
     original_datetime = datetime.datetime(1974, 11, 3, 12, 15, 58)
     original_date_string = original_datetime.strftime("%Y%m%d")