diff --git a/nibabel-data/nitest-dicom b/nibabel-data/nitest-dicom index ff6844f3a5..2246c92726 160000 --- a/nibabel-data/nitest-dicom +++ b/nibabel-data/nitest-dicom @@ -1 +1 @@ -Subproject commit ff6844f3a5ef79974c5809a79314c98fd81693cf +Subproject commit 2246c9272658693c02810836bdf820c1c6607624 diff --git a/nibabel/nicom/csareader.py b/nibabel/nicom/csareader.py index de2b5dbb1a..1764e2878c 100644 --- a/nibabel/nicom/csareader.py +++ b/nibabel/nicom/csareader.py @@ -100,7 +100,8 @@ def read(csa_str): csa_dict['n_tags'], csa_dict['check'] = up_str.unpack('2I') if not 0 < csa_dict['n_tags'] <= MAX_CSA_ITEMS: raise CSAReadError('Number of tags `t` should be ' - '0 < t <= %d' % MAX_CSA_ITEMS) + '0 < t <= %d. Instead found %d tags.' + % (MAX_CSA_ITEMS, csa_dict['n_tags'])) for tag_no in range(csa_dict['n_tags']): name, vm, vr, syngodt, n_items, last3 = \ up_str.unpack('64si4s3i') diff --git a/nibabel/nicom/dicomwrappers.py b/nibabel/nicom/dicomwrappers.py index 51e8475acc..f37d0323a8 100755 --- a/nibabel/nicom/dicomwrappers.py +++ b/nibabel/nicom/dicomwrappers.py @@ -80,7 +80,13 @@ def wrapper_from_data(dcm_data): return MultiframeWrapper(dcm_data) # Check for Siemens DICOM format types # Only Siemens will have data for the CSA header - csa = csar.get_csa_header(dcm_data) + try: + csa = csar.get_csa_header(dcm_data) + except csar.CSAReadError as e: + warnings.warn('Error while attempting to read CSA header: ' + + str(e.args) + + '\n Ignoring Siemens private (CSA) header info.') + csa = None if csa is None: return Wrapper(dcm_data) if csar.is_mosaic(csa): diff --git a/nibabel/nicom/tests/test_dicomwrappers.py b/nibabel/nicom/tests/test_dicomwrappers.py index 1ebf464d0e..c78249c381 100755 --- a/nibabel/nicom/tests/test_dicomwrappers.py +++ b/nibabel/nicom/tests/test_dicomwrappers.py @@ -20,7 +20,7 @@ from nose.tools import (assert_true, assert_false, assert_equal, assert_not_equal, assert_raises) -from numpy.testing import assert_array_equal, assert_array_almost_equal +from numpy.testing import assert_array_equal, assert_array_almost_equal, assert_warns from ...tests.nibabel_data import get_nibabel_data, needs_nibabel_data IO_DATA_PATH = pjoin(dirname(__file__), 'data') @@ -39,6 +39,8 @@ DATA_FILE_EMPTY_ST = pjoin(IO_DATA_PATH, 'slicethickness_empty_string.dcm') DATA_FILE_4D_DERIVED = pjoin(get_nibabel_data(), 'nitest-dicom', '4d_multiframe_with_derived.dcm') +DATA_FILE_CT = pjoin(get_nibabel_data(), 'nitest-dicom', + 'siemens_ct_header_csa.dcm') # This affine from our converted image was shown to match our image spatially # with an image from SPM DICOM conversion. We checked the matching with SPM @@ -633,6 +635,13 @@ def test_data_derived_shape(self): dw = didw.wrapper_from_file(DATA_FILE_4D_DERIVED) assert_equal(dw.image_shape, (96, 96, 60, 33)) + @dicom_test + @needs_nibabel_data('nitest-dicom') + def test_data_unreadable_private_headers(self): + # Test CT image with unreadable CSA tags + dw = assert_warns(UserWarning, didw.wrapper_from_file, DATA_FILE_CT) + assert_equal(dw.image_shape, (512, 571)) + @dicom_test def test_data_fake(self): # Test algorithm for get_data