Skip to content

Commit

Permalink
Allow redaction plans to share UID map
Browse files Browse the repository at this point in the history
  • Loading branch information
naglepuff committed Apr 23, 2024
1 parent 2fecd5b commit 4cb4629
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
7 changes: 5 additions & 2 deletions imagedephi/redact/build_redaction_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@


def build_redaction_plan(
image_path: Path, base_rules: Ruleset, override_rules: Ruleset | None = None
image_path: Path,
base_rules: Ruleset,
override_rules: Ruleset | None = None,
dcm_uid_map: dict[str, str] | None = None,
) -> RedactionPlan:
file_format = get_file_format_from_path(image_path)
if file_format == FileFormat.TIFF:
Expand Down Expand Up @@ -46,6 +49,6 @@ def build_redaction_plan(
if override_rules:
dicom_rules.metadata.update(override_rules.dicom.metadata)
dicom_rules.delete_custom_metadata = override_rules.dicom.delete_custom_metadata
return DicomRedactionPlan(image_path, dicom_rules)
return DicomRedactionPlan(image_path, dicom_rules, dcm_uid_map)
else:
raise UnsupportedFileTypeError(f"File format for {image_path} not supported.")
8 changes: 6 additions & 2 deletions imagedephi/redact/dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,17 @@ def _iter_dicom_elements(
else:
yield element, dicom_dataset

def __init__(self, image_path: Path, rules: DicomRules) -> None:
def __init__(self, image_path: Path, rules: DicomRules, uid_map: dict[str, str] | None) -> None:
self.image_path = image_path
self.dicom_data = pydicom.dcmread(image_path)

self.metadata_redaction_steps = {}
self.no_match_tags = []
self.uid_map = {}

# When redacting many files at a time, keep track of all UIDs across all files,
# since the DICOM format uses separate files for different resolutions and
# associated images.
self.uid_map = uid_map if uid_map else {}

for element, _ in DicomRedactionPlan._iter_dicom_elements(self.dicom_data):
custom_metadata_key = "CustomMetadataItem"
Expand Down
6 changes: 5 additions & 1 deletion imagedephi/redact/redact.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def redact_images(
redact_dir = create_redact_dir(output_dir)
show_redaction_plan(input_path)

dcm_uid_map = {}

# When we build a redaction plan for dicom, use all
# related files using the function from util/dicom.
# Use this set to skip over files previously included
Expand All @@ -104,7 +106,9 @@ def redact_images(
for image_file in bar:
push_progress(output_file_counter, output_file_max)
try:
redaction_plan = build_redaction_plan(image_file, base_rules, override_rules)
redaction_plan = build_redaction_plan(
image_file, base_rules, override_rules, dcm_uid_map=dcm_uid_map
)
# Handle and report other errors without stopping the process
except Exception as e:
logger.error(f"{image_file.name} could not be processed. {e.args[0]}")
Expand Down

0 comments on commit 4cb4629

Please sign in to comment.