diff --git a/nibabel/nicom/csareader.py b/nibabel/nicom/csareader.py index c42e37fb2a..cba9e39e34 100644 --- a/nibabel/nicom/csareader.py +++ b/nibabel/nicom/csareader.py @@ -63,8 +63,11 @@ def get_csa_header(dcm_data, csa_type='image'): if section_start is None: return None element_no = section_start + element_offset - # Assume tag exists - tag = dcm_data[(0x29, element_no)] + try: + tag = dcm_data[(0x29, element_no)] + except KeyError: + # The element could be missing due to anonymization + return None return read(tag.value) diff --git a/nibabel/nicom/tests/test_csareader.py b/nibabel/nicom/tests/test_csareader.py index ffef46ae44..d9066bff6c 100644 --- a/nibabel/nicom/tests/test_csareader.py +++ b/nibabel/nicom/tests/test_csareader.py @@ -1,6 +1,8 @@ """ Testing Siemens CSA header reader """ +import sys from os.path import join as pjoin +from copy import deepcopy import gzip import numpy as np @@ -10,9 +12,12 @@ from nose.tools import (assert_true, assert_false, assert_equal, assert_raises) +from numpy.testing.decorators import skipif from .test_dicomwrappers import (have_dicom, dicom_test, IO_DATA_PATH, DATA, DATA_FILE) +if have_dicom: + from .test_dicomwrappers import pydicom CSA2_B0 = open(pjoin(IO_DATA_PATH, 'csa2_b0.bin'), 'rb').read() CSA2_B1000 = open(pjoin(IO_DATA_PATH, 'csa2_b1000.bin'), 'rb').read() @@ -30,11 +35,7 @@ def test_csa_header_read(): assert_true(csa.is_mosaic(hdr)) # Get a shallow copy of the data, lacking the CSA marker # Need to do it this way because del appears broken in pydicom 0.9.7 - try: - from dicom.dataset import Dataset - except ImportError: - from pydicom.dataset import Dataset - data2 = Dataset() + data2 = pydicom.dataset.Dataset() for element in DATA: if (element.tag.group, element.tag.elem) != (0x29, 0x10): data2.add(element) @@ -128,3 +129,17 @@ def test_ice_dims(): assert_equal(csa.get_ice_dims(csa_info), ex_dims) assert_equal(csa.get_ice_dims({}), None) + + +@dicom_test +@skipif(sys.version_info < (2,7) and pydicom.__version__ < '1.0', + 'Known issue for python 2.6 and pydicom < 1.0') +def test_missing_csa_elem(): + # Test that we get None instead of raising an Exception when the file has + # the PrivateCreator element for the CSA dict but not the element with the + # actual CSA header (perhaps due to anonymization) + dcm = deepcopy(DATA) + csa_tag = pydicom.dataset.Tag(0x29, 0x1010) + del dcm[csa_tag] + hdr = csa.get_csa_header(dcm, 'image') + assert_equal(hdr, None)