Skip to content

Commit 61b198d

Browse files
committed
BF+TST: Skip over missing CSA header elem instead of raising.
Allows us to load data where the PrivateCreator is present but the element containing the header info is not. Ran into this with anonymized data.
1 parent 5a42cb3 commit 61b198d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

nibabel/nicom/csareader.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ def get_csa_header(dcm_data, csa_type='image'):
6363
if section_start is None:
6464
return None
6565
element_no = section_start + element_offset
66-
# Assume tag exists
67-
tag = dcm_data[(0x29, element_no)]
66+
try:
67+
tag = dcm_data[(0x29, element_no)]
68+
except KeyError:
69+
return None
6870
return read(tag.value)
6971

7072

nibabel/nicom/tests/test_csareader.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" Testing Siemens CSA header reader
22
"""
33
from os.path import join as pjoin
4+
from copy import deepcopy
45
import gzip
56

67
import numpy as np
@@ -13,6 +14,8 @@
1314

1415
from .test_dicomwrappers import (have_dicom, dicom_test,
1516
IO_DATA_PATH, DATA, DATA_FILE)
17+
if have_dicom:
18+
from .test_dicomwrappers import pydicom
1619

1720
CSA2_B0 = open(pjoin(IO_DATA_PATH, 'csa2_b0.bin'), 'rb').read()
1821
CSA2_B1000 = open(pjoin(IO_DATA_PATH, 'csa2_b1000.bin'), 'rb').read()
@@ -128,3 +131,15 @@ def test_ice_dims():
128131
assert_equal(csa.get_ice_dims(csa_info),
129132
ex_dims)
130133
assert_equal(csa.get_ice_dims({}), None)
134+
135+
136+
@dicom_test
137+
def test_missing_csa_elem():
138+
# Test that we get None instead of raising an Exception when the file has
139+
# the PrivateCreator element for the CSA dict but not the element with the
140+
# actual CSA header (perhaps due to anonymization)
141+
dcm = deepcopy(DATA)
142+
csa_tag = pydicom.tag.Tag(0x29, 0x1010)
143+
del dcm[csa_tag]
144+
hdr = csa.get_csa_header(dcm, 'image')
145+
assert_equal(hdr, None)

0 commit comments

Comments
 (0)