-
-
Notifications
You must be signed in to change notification settings - Fork 47
Fix incorrect assignment of deeply-nested tags to top-level tags #277
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f9cb7e
Add test case for nested UID overwriting
ReeceStevens 8b646e5
Fix assignment of deided attributes to correct nested tags
ReeceStevens 6c2c0c0
Move compiled regex statements to top-level
ReeceStevens bf0f2ef
Fix formatting issue
ReeceStevens 9793a48
Simplify returns and add comments on field handling
ReeceStevens c1e3e3f
Fix spelling in comment
ReeceStevens a273411
Bump version and add changelog entry
ReeceStevens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import contextlib | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job to add a full test! 👏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! :) |
||
import os | ||
import tempfile | ||
import unittest | ||
|
||
from pydicom import dcmread | ||
from pydicom.dataset import Dataset | ||
from pydicom.sequence import Sequence | ||
from pydicom.uid import generate_uid | ||
|
||
from deid.config import DeidRecipe | ||
from deid.dicom.header import get_identifiers, replace_identifiers | ||
|
||
|
||
@contextlib.contextmanager | ||
def temporary_recipe(recipe_text: str): | ||
""" | ||
Create a temporary recipe file for testing. | ||
""" | ||
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as recipe_file: | ||
recipe_file.write(recipe_text.encode()) | ||
recipe_file.flush() | ||
recipe = DeidRecipe(deid=recipe_file.name, base=False) | ||
yield recipe | ||
|
||
|
||
def hashuid(item, value, field, dicom, element_name=None): | ||
""" | ||
Generate a new UID based on the previous UID | ||
""" | ||
if hasattr(field, "element"): | ||
hash_src = str(field.element.value) | ||
else: | ||
hash_src = field | ||
new_uid = generate_uid(entropy_srcs=[hash_src]) | ||
return new_uid | ||
|
||
|
||
class TestNestedDicomFields(unittest.TestCase): | ||
def setUp(self): | ||
print("\n######################START######################") | ||
|
||
def tearDown(self): | ||
print("\n######################END########################") | ||
|
||
def test_nested_dicom_fields(self): | ||
""" | ||
Tests that header deidentification does not overwrite existing top-level tags | ||
when iterating over deeply nested tags. | ||
""" | ||
# Create a mock DICOM dataset with a top-level and nested SeriesInstanceUID | ||
original_dicom = Dataset() | ||
original_dicom.SeriesInstanceUID = generate_uid() | ||
|
||
referenced_series = Dataset() | ||
referenced_series.SeriesInstanceUID = generate_uid() | ||
|
||
original_dicom.ReferencedSeriesSequence = Sequence([referenced_series]) | ||
|
||
# Enforce precondition that the two SeriesInstanceUID attributes are different | ||
self.assertNotEqual( | ||
original_dicom.ReferencedSeriesSequence[0].SeriesInstanceUID, | ||
original_dicom.SeriesInstanceUID, | ||
) | ||
|
||
recipe_text = """ | ||
FORMAT dicom | ||
%header | ||
REPLACE SeriesInstanceUID func:hashuid | ||
""" | ||
|
||
with ( | ||
tempfile.TemporaryDirectory() as temp_dir, | ||
temporary_recipe(recipe_text) as recipe, | ||
): | ||
temp_file_name = os.path.join(temp_dir, "input.dcm") | ||
original_dicom.save_as(temp_file_name, implicit_vr=True, little_endian=True) | ||
dicom_paths = [temp_file_name] | ||
|
||
# Add hash function to deid context | ||
ids = get_identifiers(dicom_paths) | ||
for dicom_id in ids: | ||
ids[dicom_id]["hashuid"] = hashuid | ||
|
||
os.makedirs(os.path.join(temp_dir, "out")) | ||
output_paths = replace_identifiers( | ||
dicom_paths, | ||
ids=ids, | ||
deid=recipe, | ||
save=True, | ||
overwrite=True, | ||
output_folder=os.path.join(temp_dir, "out"), | ||
) | ||
|
||
output_dataset = dcmread(output_paths[0], force=True) | ||
# Assert that the SeriesInstanceUID has been replaced | ||
self.assertNotEqual( | ||
output_dataset.SeriesInstanceUID, original_dicom.SeriesInstanceUID | ||
) | ||
# Assert that the two unique UIDs were deidentified to different values | ||
self.assertNotEqual( | ||
output_dataset.ReferencedSeriesSequence[0].SeriesInstanceUID, | ||
output_dataset.SeriesInstanceUID, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
__copyright__ = "Copyright 2016-2025, Vanessa Sochat" | ||
__license__ = "MIT" | ||
|
||
__version__ = "0.4.1" | ||
__version__ = "0.4.2" | ||
AUTHOR = "Vanessa Sochat" | ||
AUTHOR_EMAIL = "[email protected]" | ||
NAME = "deid" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment here about how this data is unwrapped, and why the last_tag is special/different?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment added! Let me know if you'd like me to expand on it more.