Skip to content

Commit 3f873a7

Browse files
committed
Merge pull request #393 from moloney/missing-csa-fix
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.
2 parents 5a42cb3 + 1faade2 commit 3f873a7

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

nibabel/nicom/csareader.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ 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+
# The element could be missing due to anonymization
70+
return None
6871
return read(tag.value)
6972

7073

nibabel/nicom/tests/test_csareader.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
""" Testing Siemens CSA header reader
22
"""
3+
import sys
34
from os.path import join as pjoin
5+
from copy import deepcopy
46
import gzip
57

68
import numpy as np
@@ -10,9 +12,12 @@
1012

1113
from nose.tools import (assert_true, assert_false, assert_equal, assert_raises)
1214

15+
from numpy.testing.decorators import skipif
1316

1417
from .test_dicomwrappers import (have_dicom, dicom_test,
1518
IO_DATA_PATH, DATA, DATA_FILE)
19+
if have_dicom:
20+
from .test_dicomwrappers import pydicom
1621

1722
CSA2_B0 = open(pjoin(IO_DATA_PATH, 'csa2_b0.bin'), 'rb').read()
1823
CSA2_B1000 = open(pjoin(IO_DATA_PATH, 'csa2_b1000.bin'), 'rb').read()
@@ -30,11 +35,7 @@ def test_csa_header_read():
3035
assert_true(csa.is_mosaic(hdr))
3136
# Get a shallow copy of the data, lacking the CSA marker
3237
# Need to do it this way because del appears broken in pydicom 0.9.7
33-
try:
34-
from dicom.dataset import Dataset
35-
except ImportError:
36-
from pydicom.dataset import Dataset
37-
data2 = Dataset()
38+
data2 = pydicom.dataset.Dataset()
3839
for element in DATA:
3940
if (element.tag.group, element.tag.elem) != (0x29, 0x10):
4041
data2.add(element)
@@ -128,3 +129,17 @@ def test_ice_dims():
128129
assert_equal(csa.get_ice_dims(csa_info),
129130
ex_dims)
130131
assert_equal(csa.get_ice_dims({}), None)
132+
133+
134+
@dicom_test
135+
@skipif(sys.version_info < (2,7) and pydicom.__version__ < '1.0',
136+
'Known issue for python 2.6 and pydicom < 1.0')
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.dataset.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)