Skip to content

Commit

Permalink
Add infer_sq_for_un_vr config option (#1616)
Browse files Browse the repository at this point in the history
* add infer_sq_for_un_vr config option
* add test and release notes
  • Loading branch information
kalebdfischer committed Mar 25, 2022
1 parent b1d404a commit 85d0948
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
4 changes: 3 additions & 1 deletion doc/release_notes/v2.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Enhancements
:func:`~pydicom.pixel_data_handlers.utils.unpack_bits`(:pr:`1594`)
* Added :func:`~pydicom.pixel_data_handlers.util.expand_ybr422` for expanding
uncompressed ``YBR_FULL_422`` data to ``YBR_FULL`` (:pr:`1593`)

* Replacement of ``UN`` VR with ``SQ`` VR for undefined length data elements
(introduced in 2.2.2), can now be configured via
:attr:`~pydicom.config.settings.infer_sq_for_un_vr`

Fixes
-----
Expand Down
15 changes: 15 additions & 0 deletions pydicom/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def __init__(self) -> None:
self._writing_validation_mode: Optional[int] = (
RAISE if _use_future else None
)
self._infer_sq_for_un_vr: bool = True

@property
def reading_validation_mode(self) -> int:
Expand Down Expand Up @@ -227,6 +228,20 @@ def writing_validation_mode(self) -> int:
def writing_validation_mode(self, value: int) -> None:
self._writing_validation_mode = value

@property
def infer_sq_for_un_vr(self) -> bool:
"""If ``True``, and the VR of a known data element is encoded as
**UN** in an explicit encoding for an undefined length data element,
the VR is changed to SQ per PS 3.5, section 6.2.2. Can be set to
``False`` where the content of the tag shown as **UN** is not DICOM
conformant and would lead to a failure if accessing it.
"""
return self._infer_sq_for_un_vr

@infer_sq_for_un_vr.setter
def infer_sq_for_un_vr(self, value: bool) -> None:
self._infer_sq_for_un_vr = value


settings = Settings()
"""The global configuration object of type :class:`Settings` to access some
Expand Down
2 changes: 1 addition & 1 deletion pydicom/filereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def data_element_generator(
else:
# VR UN with undefined length shall be handled as SQ
# see PS 3.5, section 6.2.2
if vr == VR_.UN:
if vr == VR_.UN and config.settings.infer_sq_for_un_vr:
vr = VR_.SQ
# Try to look up type to see if is a SQ
# if private tag, won't be able to look it up in dictionary,
Expand Down
8 changes: 8 additions & 0 deletions pydicom/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def dont_replace_un_with_known_vr():
config.replace_un_with_known_vr = old_value


@pytest.fixture
def dont_replace_un_with_sq_vr():
old_value = config.settings.infer_sq_for_un_vr
config.settings.infer_sq_for_un_vr = False
yield
config.settings.infer_sq_for_un_vr = old_value


@pytest.fixture
def dont_raise_on_writing_invalid_value():
old_value = config.settings.writing_validation_mode
Expand Down
9 changes: 9 additions & 0 deletions pydicom/tests/test_filereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,15 @@ def test_un_sequence(self, dont_replace_un_with_known_vr):
assert len(seq_element.value) == 1
assert len(seq_element.value[0].ReferencedSeriesSequence) == 1

def test_un_sequence_dont_infer(
self,
dont_replace_un_with_sq_vr,
dont_replace_un_with_known_vr
):
ds = dcmread(get_testdata_file("UN_sequence.dcm"))
seq_element = ds[0x4453100c]
assert seq_element.VR == "UN"

def test_no_meta_group_length(self, no_datetime_conversion):
"""Read file with no group length in file meta."""
# Issue 108 -- iView example file with no group length (0002,0002)
Expand Down

0 comments on commit 85d0948

Please sign in to comment.