Skip to content

Commit

Permalink
rf: Construct DicomExtensions more simply
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Sep 7, 2024
1 parent 9bc3712 commit 46923c1
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions nibabel/nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ class Nifti1DicomExtension(Nifti1Extension[DicomDataset]):
"""

code = 2
_is_implict_VR: bool = False
_is_little_endian: bool = True

def __init__(
self,
Expand Down Expand Up @@ -586,27 +588,25 @@ def __init__(
code should always be 2 for DICOM.
"""

self._is_little_endian = parent_hdr is None or parent_hdr.endianness == '<'
if code != 2:
raise ValueError(f'code must be 2 for DICOM. Got {code}.')

Check warning on line 592 in nibabel/nifti1.py

View check run for this annotation

Codecov / codecov/patch

nibabel/nifti1.py#L592

Added line #L592 was not covered by tests

if content is None:
content = pdcm.Dataset()

if parent_hdr is not None:
self._is_little_endian = parent_hdr.endianness == '<'

bytes_content: bytes
if isinstance(content, pdcm.dataset.Dataset):
self._is_implicit_VR = False
self._object = content
bytes_content = self._mangle(content)
super().__init__(code, object=content)
elif isinstance(content, bytes): # Got a byte string - unmangle it
self._is_implicit_VR = self._guess_implicit_VR(content)
self._object = self._unmangle(content)
bytes_content = content
elif content is None: # initialize a new dicom dataset
self._is_implicit_VR = False
self._object = pdcm.dataset.Dataset()
bytes_content = self._mangle(self._object)
super().__init__(code, content=content)
else:
raise TypeError(
f'content must be either a bytestring or a pydicom Dataset. '
f'Got {content.__class__}'
)
super().__init__(code, bytes_content)

@staticmethod
def _guess_implicit_VR(content) -> bool:
Expand Down

0 comments on commit 46923c1

Please sign in to comment.