Skip to content

Commit ae24644

Browse files
committed
feat: add yield_child_fields to yield child fields UIDs, bump version and changelog
1 parent d911d72 commit ae24644

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
1414
Referenced versions in headers are tagged on Github, in parentheses are for pypi.
1515

1616
## [vxx](https://github.com/pydicom/deid/tree/master) (master)
17+
- Fix field removal and blanking to clean up child UID references [#293](https://github.com/pydicom/deid/pull/293) (0.4.9)
1718
- Fix UID lookup for nested sequence fields in DICOM datasets [#292](https://github.com/pydicom/deid/pull/292) (0.4.8)
1819
- Allow saving with a compressed transfer syntax [#290](https://github.com/pydicom/deid/pull/290) (0.4.7)
1920
- Improve performance of header deid with caching and lookup tables [#289](https://github.com/pydicom/deid/pull/289)

deid/dicom/parser.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ def get_nested_field(self, field, return_parent=False):
176176
return parent, desired
177177
return desired
178178

179+
def yield_child_fields(self, field):
180+
"""
181+
Yield child field UIDs for a given field.
182+
183+
This method identifies all field UIDs in self.fields that are nested under the provided field,
184+
based on the UID prefix convention (parent UID + '__').
185+
It is used to find and remove all child fields when blanking or deleting a parent field (e.g., a sequence).
186+
"""
187+
field_uid_prefix = field.uid + "__"
188+
for uid in self.fields:
189+
if uid.startswith(field_uid_prefix):
190+
yield uid
191+
179192
def delete_field(self, field):
180193
"""
181194
Delete a field from the dicom.
@@ -191,11 +204,7 @@ def delete_field(self, field):
191204
# Remove the field itself from the lookup
192205
self.fields.remove(field.uid)
193206
# Also remove any child fields that were nested under this field
194-
field_uid_prefix = field.uid + "__"
195-
child_uids = [
196-
uid for uid in self.fields if uid.startswith(field_uid_prefix)
197-
]
198-
for child_uid in child_uids:
207+
for child_uid in self.yield_child_fields(field):
199208
self.fields.remove(child_uid)
200209

201210
def blank_field(self, field):
@@ -211,11 +220,7 @@ def blank_field(self, field):
211220
if parent and desired in parent:
212221
parent[desired].value = None
213222
# Also remove any child fields that were nested under this field
214-
field_uid_prefix = field.uid + "__"
215-
child_uids = [
216-
uid for uid in self.fields if uid.startswith(field_uid_prefix)
217-
]
218-
for child_uid in child_uids:
223+
for child_uid in self.yield_child_fields(field):
219224
self.fields.remove(child_uid)
220225

221226
def replace_field(self, field, value):

deid/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__copyright__ = "Copyright 2016-2025, Vanessa Sochat"
33
__license__ = "MIT"
44

5-
__version__ = "0.4.8"
5+
__version__ = "0.4.9"
66
AUTHOR = "Vanessa Sochat"
77
AUTHOR_EMAIL = "[email protected]"
88
NAME = "deid"

0 commit comments

Comments
 (0)