def _run_action(self, field, action, value=None): """perform_action (above) typically is called using a loaded deid, and _run_addition is typically done via an addition in a config Both result in a call to this function. If an action fails or is not done, None is returned, and the calling function should handle this. """ # Blank the value if action == "BLANK": self.blank_field(field) # Unlikely to be called from perform_action elif action == "ADD": self.add_field(field, value) # Code the value with something in the response elif action == "REPLACE": self.replace_field(field, value) # Code the value with something in the response elif action == "JITTER": value = parse_value(item=self.lookup, dicom=self.dicom, value=value, field=field) if value is not None: # Jitter the field by the supplied value jitter_timestamp(dicom=self.dicom, field=field.element.keyword, value=value) else: bot.warning("JITTER %s unsuccessful" % field) # elif "KEEP" --> Do nothing. Keep the original # Remove the field entirely elif action == "REMOVE": # If a value is defined, parse it (could be filter) do_removal = True if value != None: do_removal = parse_value(item=self.lookup, dicom=self.dicom, value=value, field=field) if do_removal == True: self.delete_field(field)
def test_jitter_timestamp(self): from deid.dicom.actions import jitter_timestamp dicom = get_dicom(self.dataset) print("Testing test_jitter_timestamp") print("Case 1: Testing jitter_timestamp with DICOM Date (DA)") dicom.StudyDate = '20131210' dicom.data_element("StudyDate").VR = 'DA' jitter_timestamp(dicom, "StudyDate", 10) expected = '20131220' self.assertEqual(dicom.StudyDate, expected) print("Case 2: Testing with DICOM timestamp (DT)") dicom.AcquisitionDateTime = '20131210081530' dicom.data_element("AcquisitionDateTime").VR = 'DT' jitter_timestamp(dicom, "AcquisitionDateTime", 10) expected = '20131220081530.000000' self.assertEqual(dicom.AcquisitionDateTime, expected) print("Case 3: Testing with non-standard DICOM date (DA)") dicom.StudyDate = '2013/12/10' dicom.data_element("StudyDate").VR = 'DA' jitter_timestamp(dicom, "StudyDate", 10) expected = '20131220' self.assertEqual(dicom.StudyDate, expected) print("Case 4: Testing negative jitter value") dicom.StudyDate = '20131210' jitter_timestamp(dicom, "StudyDate", -5) expected = '20131205' self.assertEqual(dicom.StudyDate, expected) print("Case 5: Testing with empty field") dicom.StudyDate = expected = '' jitter_timestamp(dicom, "StudyDate", 10) self.assertEqual(dicom.StudyDate, expected) print('Case 6: Testing with nonexistent field') del dicom.StudyDate jitter_timestamp(dicom, "StudyDate", 10) self.assertTrue("StudyDate" not in dicom) print("Case 7: Testing JITTER recipe action") from deid.dicom.actions import perform_action dicom.StudyDate = '20131210' dicom.data_element("StudyDate").VR = 'DA' JITTER = {"action": "JITTER", "field": "StudyDate", "value": "-5"} expected = '20131205' dicom = perform_action(dicom=dicom, action=JITTER) self.assertTrue(dicom.StudyDate, expected)
def test_jitter_timestamp(self): from deid.dicom.actions import jitter_timestamp dicom = get_dicom(self.dataset) print("Testing test_jitter_timestamp") print("Case 1: Testing jitter_timestamp with DICOM Date (DA)") dicom.StudyDate = "20131210" dicom.data_element("StudyDate").VR = "DA" jitter_timestamp(dicom, "StudyDate", 10) expected = "20131220" self.assertEqual(dicom.StudyDate, expected) print("Case 2: Testing with DICOM timestamp (DT)") dicom.AcquisitionDateTime = "20131210081530" dicom.data_element("AcquisitionDateTime").VR = "DT" jitter_timestamp(dicom, "AcquisitionDateTime", 10) expected = "20131220081530.000000" self.assertEqual(dicom.AcquisitionDateTime, expected) print("Case 3: Testing with non-standard DICOM date (DA)") dicom.StudyDate = "2013/12/10" dicom.data_element("StudyDate").VR = "DA" jitter_timestamp(dicom, "StudyDate", 10) expected = "20131220" self.assertEqual(dicom.StudyDate, expected) print("Case 4: Testing negative jitter value") dicom.StudyDate = "20131210" jitter_timestamp(dicom, "StudyDate", -5) expected = "20131205" self.assertEqual(dicom.StudyDate, expected) print("Case 5: Testing with empty field") dicom.StudyDate = expected = "" jitter_timestamp(dicom, "StudyDate", 10) self.assertEqual(dicom.StudyDate, expected) print("Case 6: Testing with nonexistent field") del dicom.StudyDate jitter_timestamp(dicom, "StudyDate", 10) self.assertTrue("StudyDate" not in dicom)