Skip to content

Commit b2a88b8

Browse files
authored
Merge pull request #818 from hbraunDSP/ignore-bad-csa
Ignore unreadable siemens private headers
2 parents 785e9e2 + 702515e commit b2a88b8

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

nibabel/nicom/csareader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def read(csa_str):
100100
csa_dict['n_tags'], csa_dict['check'] = up_str.unpack('2I')
101101
if not 0 < csa_dict['n_tags'] <= MAX_CSA_ITEMS:
102102
raise CSAReadError('Number of tags `t` should be '
103-
'0 < t <= %d' % MAX_CSA_ITEMS)
103+
'0 < t <= %d. Instead found %d tags.'
104+
% (MAX_CSA_ITEMS, csa_dict['n_tags']))
104105
for tag_no in range(csa_dict['n_tags']):
105106
name, vm, vr, syngodt, n_items, last3 = \
106107
up_str.unpack('64si4s3i')

nibabel/nicom/dicomwrappers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ def wrapper_from_data(dcm_data):
8080
return MultiframeWrapper(dcm_data)
8181
# Check for Siemens DICOM format types
8282
# Only Siemens will have data for the CSA header
83-
csa = csar.get_csa_header(dcm_data)
83+
try:
84+
csa = csar.get_csa_header(dcm_data)
85+
except csar.CSAReadError as e:
86+
warnings.warn('Error while attempting to read CSA header: ' +
87+
str(e.args) +
88+
'\n Ignoring Siemens private (CSA) header info.')
89+
csa = None
8490
if csa is None:
8591
return Wrapper(dcm_data)
8692
if csar.is_mosaic(csa):

nibabel/nicom/tests/test_dicomwrappers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from nose.tools import (assert_true, assert_false, assert_equal,
2121
assert_not_equal, assert_raises)
2222

23-
from numpy.testing import assert_array_equal, assert_array_almost_equal
23+
from numpy.testing import assert_array_equal, assert_array_almost_equal, assert_warns
2424
from ...tests.nibabel_data import get_nibabel_data, needs_nibabel_data
2525

2626
IO_DATA_PATH = pjoin(dirname(__file__), 'data')
@@ -39,6 +39,8 @@
3939
DATA_FILE_EMPTY_ST = pjoin(IO_DATA_PATH, 'slicethickness_empty_string.dcm')
4040
DATA_FILE_4D_DERIVED = pjoin(get_nibabel_data(), 'nitest-dicom',
4141
'4d_multiframe_with_derived.dcm')
42+
DATA_FILE_CT = pjoin(get_nibabel_data(), 'nitest-dicom',
43+
'siemens_ct_header_csa.dcm')
4244

4345
# This affine from our converted image was shown to match our image spatially
4446
# with an image from SPM DICOM conversion. We checked the matching with SPM
@@ -633,6 +635,13 @@ def test_data_derived_shape(self):
633635
dw = didw.wrapper_from_file(DATA_FILE_4D_DERIVED)
634636
assert_equal(dw.image_shape, (96, 96, 60, 33))
635637

638+
@dicom_test
639+
@needs_nibabel_data('nitest-dicom')
640+
def test_data_unreadable_private_headers(self):
641+
# Test CT image with unreadable CSA tags
642+
dw = assert_warns(UserWarning, didw.wrapper_from_file, DATA_FILE_CT)
643+
assert_equal(dw.image_shape, (512, 571))
644+
636645
@dicom_test
637646
def test_data_fake(self):
638647
# Test algorithm for get_data

0 commit comments

Comments
 (0)