22import re
33
44from enum import Enum
5- from typing import List , Union
5+ from typing import Callable , List , Union
66from dataclasses import dataclass
77
8- from dicomanonymizer .dicomfields import (
9- D_TAGS ,
10- Z_TAGS ,
11- X_TAGS ,
12- U_TAGS ,
13- Z_D_TAGS ,
14- X_Z_TAGS ,
15- X_D_TAGS ,
16- X_Z_D_TAGS ,
17- X_Z_U_STAR_TAGS ,
18- )
8+ from dicomanonymizer .dicomfields_selector import dicom_anonymization_database_selector
199from dicomanonymizer .format_tag import tag_to_hex_strings
2010
21-
11+ # keeps the mapping from old UID to new UID
2212dictionary = {}
2313
2414
@@ -344,33 +334,47 @@ class ActionsMapNameFunctions(Enum):
344334 regexp = Action (regexp , 2 )
345335
346336
347- def initialize_actions () -> dict :
337+ def initialize_actions (dicom_version : str = "dicomfields_2023" ) -> dict :
348338 """
349339 Initialize anonymization actions with DICOM standard values
350340
341+ :param dicom_version: DICOM version to use
351342 :return Dict object which map actions to tags
352343 """
353- anonymization_actions = {tag : replace for tag in D_TAGS }
354- anonymization_actions .update ({tag : empty for tag in Z_TAGS })
355- anonymization_actions .update ({tag : delete for tag in X_TAGS })
356- anonymization_actions .update ({tag : replace_UID for tag in U_TAGS })
357- anonymization_actions .update ({tag : empty_or_replace for tag in Z_D_TAGS })
358- anonymization_actions .update ({tag : delete_or_empty for tag in X_Z_TAGS })
359- anonymization_actions .update ({tag : delete_or_replace for tag in X_D_TAGS })
344+ tags = dicom_anonymization_database_selector (dicom_version )
345+
346+ anonymization_actions = {tag : replace for tag in tags ["D_TAGS" ]}
347+ anonymization_actions .update ({tag : empty for tag in tags ["Z_TAGS" ]})
348+ anonymization_actions .update ({tag : delete for tag in tags ["X_TAGS" ]})
349+ anonymization_actions .update ({tag : replace_UID for tag in tags ["U_TAGS" ]})
350+ anonymization_actions .update ({tag : empty_or_replace for tag in tags ["Z_D_TAGS" ]})
351+ anonymization_actions .update ({tag : delete_or_empty for tag in tags ["X_Z_TAGS" ]})
352+ anonymization_actions .update ({tag : delete_or_replace for tag in tags ["X_D_TAGS" ]})
360353 anonymization_actions .update (
361- {tag : delete_or_empty_or_replace for tag in X_Z_D_TAGS }
354+ {tag : delete_or_empty_or_replace for tag in tags [ " X_Z_D_TAGS" ] }
362355 )
363356 anonymization_actions .update (
364- {tag : delete_or_empty_or_replace_UID for tag in X_Z_U_STAR_TAGS }
357+ {tag : delete_or_empty_or_replace_UID for tag in tags [ " X_Z_U_STAR_TAGS" ] }
365358 )
366359 return anonymization_actions
367360
368361
362+ def initialize_actions_2024b () -> dict :
363+ """
364+ Initialize anonymization actions with DICOM standard values of 2024b.
365+ If you want to use 2024b version of anonymization, call anonymize_dataset with base_rules_gen=initialize_actions_2024b.
366+
367+ :return Dict object which map actions to tags
368+ """
369+ return initialize_actions ("dicomfields_2024b" )
370+
371+
369372def anonymize_dicom_file (
370373 in_file : str ,
371374 out_file : str ,
372375 extra_anonymization_rules : dict = None ,
373376 delete_private_tags : bool = True ,
377+ base_rules_gen : Callable = initialize_actions ,
374378) -> None :
375379 """
376380 Anonymize a DICOM file by modifying personal tags
@@ -384,7 +388,9 @@ def anonymize_dicom_file(
384388 """
385389 dataset = pydicom .dcmread (in_file )
386390
387- anonymize_dataset (dataset , extra_anonymization_rules , delete_private_tags )
391+ anonymize_dataset (
392+ dataset , extra_anonymization_rules , delete_private_tags , base_rules_gen
393+ )
388394
389395 # Store modified image
390396 dataset .save_as (out_file )
@@ -450,15 +456,17 @@ def anonymize_dataset(
450456 dataset : pydicom .Dataset ,
451457 extra_anonymization_rules : dict = None ,
452458 delete_private_tags : bool = True ,
459+ base_rules_gen : Callable = initialize_actions ,
453460) -> None :
454461 """
455462 Anonymize a pydicom Dataset by using anonymization rules which links an action to a tag
456463
457464 :param dataset: Dataset to be anonymize
465+ :param base_rules_gen: Function to generate the base rules
458466 :param extra_anonymization_rules: Rules to be applied on the dataset
459467 :param delete_private_tags: Define if private tags should be delete or not
460468 """
461- current_anonymization_actions = initialize_actions ()
469+ current_anonymization_actions = base_rules_gen ()
462470
463471 if extra_anonymization_rules is not None :
464472 current_anonymization_actions .update (extra_anonymization_rules )
0 commit comments