diff --git a/doc/Python_Tutorial.rst b/doc/Python_Tutorial.rst index 9d241f9..e7f5d06 100644 --- a/doc/Python_Tutorial.rst +++ b/doc/Python_Tutorial.rst @@ -62,7 +62,7 @@ the array of voxel data, get the affine transform, or create a Nifti1Image. .. code-block:: python >>> stack_data = my_stack.get_data() - >>> stack_affine = my_stack.affine + >>> stack_affine = my_stack.get_affine() >>> nii = my_stack.to_nifti() Embedding Meta Data @@ -150,25 +150,25 @@ method *split*. >>> from dcmstack.dcmmeta import NiftiWrapper >>> nw1 = NiftiWrapper.from_filename('img1.nii.gz') >>> nw2 = NiftiWrapper.from_filename('img2.nii.gz') - >>> print nw1.nii_img.shape + >>> print nw1.nii_img.get_shape() (384, 512, 60) - >>> print nw2.nii_img.shape + >>> print nw2.nii_img.get_shape() (384, 512, 60) >>> print nw1.get_meta('EchoTime') 11.0 >>> print nw2.get_meta('EchoTime') 87.0 >>> merged = NiftiWrapper.from_sequence([nw1, nw2]) - >>> print merged.nii_img.shape + >>> print merged.nii_img.get_shape() (384, 512, 60, 2) >>> print merged.get_meta('EchoTime', index=(0,0,0,0) 11.0 >>> print merged.get_meta('EchoTime', index=(0,0,0,1) 87.0 >>> splits = list(merge.split()) - >>> print splits[0].nii_img.shape + >>> print splits[0].nii_img.get_shape() (384, 512, 60) - >>> print splits[1].nii_img.shape + >>> print splits[1].nii_img.get_shape() (384, 512, 60) >>> print splits[0].get_meta('EchoTime') 11.0 diff --git a/src/dcmstack/dcmmeta.py b/src/dcmstack/dcmmeta.py index be527d4..a806688 100644 --- a/src/dcmstack/dcmmeta.py +++ b/src/dcmstack/dcmmeta.py @@ -1253,7 +1253,7 @@ class NiftiWrapper(object): def __init__(self, nii_img, make_empty=False): self.nii_img = nii_img - hdr = nii_img.header + hdr = nii_img.get_header() self.meta_ext = None for extension in hdr.extensions: if extension.get_code() == dcm_meta_ecode: @@ -1292,19 +1292,19 @@ def meta_valid(self, classification): if classification == ('global', 'const'): return True - img_shape = self.nii_img.shape + img_shape = self.nii_img.get_shape() meta_shape = self.meta_ext.shape if classification == ('vector', 'samples'): return meta_shape[4:] == img_shape[4:] if classification == ('time', 'samples'): return meta_shape[3:] == img_shape[3:] - hdr = self.nii_img.header + hdr = self.nii_img.get_header() if self.meta_ext.n_slices != hdr.get_n_slices(): return False slice_dim = hdr.get_dim_info()[2] - slice_dir = self.nii_img.affine[slice_dim, :3] + slice_dir = self.nii_img.get_affine()[slice_dim, :3] slices_aligned = np.allclose(slice_dir, self.meta_ext.slice_normal, atol=1e-6) @@ -1357,7 +1357,7 @@ def get_meta(self, key, index=None, default=None): #If an index is provided check the varying values if not index is None: #Test if the index is valid - shape = self.nii_img.shape + shape = self.nii_img.get_shape() if len(index) != len(shape): raise IndexError('Incorrect number of indices.') for dim, ind_val in enumerate(index): @@ -1371,7 +1371,7 @@ def get_meta(self, key, index=None, default=None): return values[index[4]] #Finally, if aligned, try per-slice values - slice_dim = self.nii_img.header.get_dim_info()[2] + slice_dim = self.nii_img.get_header().get_dim_info()[2] n_slices = shape[slice_dim] if classes == ('global', 'slices'): val_idx = index[slice_dim] @@ -1392,7 +1392,7 @@ def get_meta(self, key, index=None, default=None): def remove_extension(self): '''Remove the DcmMetaExtension from the header of nii_img. The attribute `meta_ext` will still point to the extension.''' - hdr = self.nii_img.header + hdr = self.nii_img.get_header() target_idx = None for idx, ext in enumerate(hdr.extensions): if id(ext) == id(self.meta_ext): @@ -1414,7 +1414,7 @@ def replace_extension(self, dcmmeta_ext): ''' self.remove_extension() - self.nii_img.header.extensions.append(dcmmeta_ext) + self.nii_img.get_header().extensions.append(dcmmeta_ext) self.meta_ext = dcmmeta_ext def split(self, dim=None): @@ -1434,9 +1434,9 @@ def split(self, dim=None): along `dim`. ''' - shape = self.nii_img.shape + shape = self.nii_img.get_shape() data = self.nii_img.get_data() - header = self.nii_img.header + header = self.nii_img.get_header() slice_dim = header.get_dim_info()[2] #If dim is None, choose the vector/time/slice dim in that order @@ -1541,7 +1541,7 @@ def from_dicom_wrapper(klass, dcm_wrp, meta_dict=None): #Create the nifti image and set header data nii_img = nb.nifti1.Nifti1Image(data, affine) - hdr = nii_img.header + hdr = nii_img.get_header() hdr.set_xyzt_units('mm', 'sec') dim_info = {'freq' : None, 'phase' : None, @@ -1606,9 +1606,9 @@ def from_sequence(klass, seq, dim=None): n_inputs = len(seq) first_input = seq[0] first_nii = first_input.nii_img - first_hdr = first_nii.header + first_hdr = first_nii.get_header() shape = first_nii.shape - affine = first_nii.affine.copy() + affine = first_nii.get_affine().copy() #If dim is None, choose a sane default if dim is None: @@ -1683,8 +1683,8 @@ def from_sequence(klass, seq, dim=None): input_wrp = seq[input_idx] input_nii = input_wrp.nii_img - input_aff = input_nii.affine - input_hdr = input_nii.header + input_aff = input_nii.get_affine() + input_hdr = input_nii.get_header() #Check that the affines match appropriately for axis_idx, axis_vec in enumerate(axes): @@ -1768,12 +1768,12 @@ def from_sequence(klass, seq, dim=None): #If we joined along a spatial dim, rescale the appropriate axis scaled_dim_dir = None if dim < 3: - scaled_dim_dir = seq[1].nii_img.affine[:3, 3] - trans + scaled_dim_dir = seq[1].nii_img.get_affine()[:3, 3] - trans affine[:3, dim] = scaled_dim_dir #Create the resulting Nifti and wrapper result_nii = nb.Nifti1Image(result_data, affine) - result_hdr = result_nii.header + result_hdr = result_nii.get_header() #Update the header with any info that is consistent across inputs if hdr_info['qform'] is not None and hdr_info['qform_code'] is not None: diff --git a/src/dcmstack/dcmstack.py b/src/dcmstack/dcmstack.py index 50247cf..522c229 100644 --- a/src/dcmstack/dcmstack.py +++ b/src/dcmstack/dcmstack.py @@ -60,7 +60,6 @@ def key_regex_filter(key, value): not (include_re and include_re.search(key))) return key_regex_filter - default_key_excl_res = ['Patient', 'Physician', 'Operator', @@ -94,19 +93,16 @@ def key_regex_filter(key, value): '''A list of regexes passed to `make_key_regex_filter` as `exclude_res` to create the `default_meta_filter`.''' - default_key_incl_res = ['ImageOrientationPatient', 'ImagePositionPatient', ] '''A list of regexes passed to `make_key_regex_filter` as `force_include_res` to create the `default_meta_filter`.''' - default_meta_filter = make_key_regex_filter(default_key_excl_res, default_key_incl_res) '''Default meta_filter for `DicomStack`.''' - def ornt_transform(start_ornt, end_ornt): '''Return the orientation that transforms from `start_ornt` to `end_ornt`. @@ -145,7 +141,6 @@ def ornt_transform(start_ornt, end_ornt): end_out_idx) return result - def axcodes2ornt(axcodes, labels=None): """ Convert axis codes `axcodes` to an orientation @@ -188,7 +183,6 @@ def axcodes2ornt(axcodes, labels=None): break return ornt - def reorder_voxels(vox_array, affine, voxel_order): '''Reorder the given voxel array and corresponding affine. @@ -253,7 +247,6 @@ def reorder_voxels(vox_array, affine, voxel_order): return (vox_array, affine, aff_trans, ornt_trans) - def dcm_time_to_sec(time_str): '''Convert a DICOM time value (value representation of 'TM') to the number of seconds past midnight. @@ -281,7 +274,6 @@ def dcm_time_to_sec(time_str): return float(result) - class IncongruentImageError(Exception): def __init__(self, msg): '''An exception denoting that a DICOM with incorrect size or orientation @@ -289,21 +281,13 @@ def __init__(self, msg): self.msg = msg def __str__(self): - return "The image is not congruent to the existing stack: %s" % self.msg - + return 'The image is not congruent to the existing stack: %s' % self.msg class ImageCollisionError(Exception): '''An exception denoting that a DICOM which collides with one already in the stack was passed to a `DicomStack.add_dcm`.''' def __str__(self): - return "The image collides with one already in the stack" - - -class NonImageDataSetError(Exception): - '''The DICOM doesn't contain image data''' - def __str__(self): - return "The DICOM doesn't contain an image" - + return 'The image collides with one already in the stack' class InvalidStackError(Exception): def __init__(self, msg): @@ -311,8 +295,7 @@ def __init__(self, msg): self.msg = msg def __str__(self): - return "The DICOM stack is not valid: %s" % self.msg - + return 'The DICOM stack is not valid: %s' % self.msg class DicomOrdering(object): '''Object defining an ordering for a set of dicom datasets. Create a @@ -366,7 +349,6 @@ def get_ordinate(self, ds): return val - def _make_dummy(reference, meta, iop): '''Make a "dummy" NiftiWrapper (no valid pixel data).''' #Create the dummy data array filled with largest representable value @@ -374,10 +356,10 @@ def _make_dummy(reference, meta, iop): data[...] = np.iinfo(np.int16).max #Create the nifti image and set header data - aff = reference.nii_img.affine.copy() + aff = reference.nii_img.get_affine().copy() aff[:3, 3] = [iop[1], iop[0], iop[2]] nii_img = nb.nifti1.Nifti1Image(data, aff) - hdr = nii_img.header + hdr = nii_img.get_header() hdr.set_xyzt_units('mm', 'sec') dim_info = {'freq' : None, 'phase' : None, @@ -399,7 +381,6 @@ def _make_dummy(reference, meta, iop): return result - default_group_keys = ('SeriesInstanceUID', 'SeriesNumber', 'ProtocolName', @@ -412,17 +393,6 @@ def _make_dummy(reference, meta, iop): '''Default keys needing np.allclose instead of equaulity testing when grouping ''' - -_pix_attrs = ('PixelData', 'FloatPixelData', 'DoubleFloatPixelData') - - -def is_image(dcm): - '''Test if the data set is an image''' - if any(hasattr(dcm, attr) for attr in _pix_attrs): - return True - return False - - class DicomStack(object): '''Defines a method for stacking together DICOM data sets into a multi dimensional volume. @@ -536,6 +506,7 @@ def _chk_congruent(self, meta): ) return is_dummy + def add_dcm(self, dcm, meta=None): '''Add a pydicom dataset to the stack. @@ -557,13 +528,8 @@ def add_dcm(self, dcm, meta=None): ImageCollisionError The provided `dcm` has the same slice location and time/vector values. - - NonImageDataSetError - The provided `dcm` doesn't contain image data - ''' - if not is_image(dcm): - raise NonImageDataSetError() + ''' if meta is None: from .extract import default_extractor meta = default_extractor(dcm) @@ -685,6 +651,7 @@ def _chk_order(self, slice_positions, files_per_vol, num_volumes, str(curr_vec_val)) raise InvalidStackError(''.join(error_msg)) + def get_shape(self): '''Get the shape of the stack. @@ -783,7 +750,7 @@ def get_shape(self): num_vec_comps) #Stack appears to be valid, build the shape tuple - file_shape = self._files_info[0][0].nii_img.shape + file_shape = self._files_info[0][0].nii_img.get_shape() vol_shape = list(file_shape) if files_per_vol > 1: vol_shape[2] = files_per_vol @@ -797,8 +764,6 @@ def get_shape(self): self._shape_dirty = False return self._shape - shape = property(fget=get_shape) - def get_data(self): '''Get an array of the voxel values. @@ -812,7 +777,7 @@ def get_data(self): The stack is incomplete or invalid. ''' #Create a numpy array for storing the voxel data - stack_shape = self.shape + stack_shape = self.get_shape() stack_shape = tuple(list(stack_shape) + ((5 - len(stack_shape)) * [1])) stack_dtype = self._files_info[0][0].nii_img.get_data_dtype() bits_stored = self._files_info[0][0].get_meta('BitsStored', default=16) @@ -830,7 +795,7 @@ def get_data(self): if len(stack_shape) > 4: n_vols *= stack_shape[4] files_per_vol = len(self._files_info) // n_vols - file_shape = self._files_info[0][0].nii_img.shape + file_shape = self._files_info[0][0].nii_img.get_shape() for vec_idx in range(stack_shape[4]): for time_idx in range(stack_shape[3]): if files_per_vol == 1 and file_shape[2] != 1: @@ -852,8 +817,6 @@ def get_data(self): return vox_array - data = property(fget=get_data) - def get_affine(self): '''Get the affine transform for mapping row/column/slice indices to Nifti (RAS) patient space. @@ -868,7 +831,7 @@ def get_affine(self): The stack is incomplete or invalid. ''' #Figure out the number of three (or two) dimensional volumes - shape = self.shape + shape = self.get_shape() n_vols = 1 if len(shape) > 3: n_vols *= shape[3] @@ -879,19 +842,17 @@ def get_affine(self): files_per_vol = len(self._files_info) // n_vols #Pull the DICOM Patient Space affine from the first input - aff = self._files_info[0][0].nii_img.affine + aff = self._files_info[0][0].nii_img.get_affine() #If there is more than one file per volume, we need to fix slice scaling if files_per_vol > 1: first_offset = aff[:3, 3] - second_offset = self._files_info[1][0].nii_img.affine[:3, 3] + second_offset = self._files_info[1][0].nii_img.get_affine()[:3, 3] scaled_slc_dir = second_offset - first_offset aff[:3, 2] = scaled_slc_dir return aff - affine = property(fget=get_affine) - def to_nifti(self, voxel_order='LAS', embed_meta=False): '''Returns a NiftiImage with the data and affine from the stack. @@ -912,7 +873,7 @@ def to_nifti(self, voxel_order='LAS', embed_meta=False): ''' #Get the voxel data and affine data = self.get_data() - affine = self.affine + affine = self.get_affine() #Figure out the number of three (or two) dimensional volumes n_vols = 1 @@ -953,7 +914,7 @@ def to_nifti(self, voxel_order='LAS', embed_meta=False): #Create the nifti image using the data array nifti_image = nb.Nifti1Image(data, affine) - nifti_header = nifti_image.header + nifti_header = nifti_image.get_header() #Set the units and dimension info nifti_header.set_xyzt_units('mm', 'msec') @@ -1027,7 +988,7 @@ def to_nifti(self, voxel_order='LAS', embed_meta=False): for vec_idx in range(data.shape[4]): start_idx = vec_idx * data.shape[3] end_idx = start_idx + data.shape[3] - meta = DcmMetaExtension.from_sequence( + meta = DcmMetaExtension.from_sequence(\ vol_meta[start_idx:end_idx], 3) vec_meta.append(meta) else: @@ -1060,7 +1021,6 @@ def to_nifti_wrapper(self, voxel_order=''): ''' return NiftiWrapper(self.to_nifti(voxel_order, True)) - def parse_and_group(src_paths, group_by=default_group_keys, extractor=None, force=False, warn_on_except=False, close_tests=default_close_keys): @@ -1117,11 +1077,6 @@ def parse_and_group(src_paths, group_by=default_group_keys, extractor=None, else: raise - # Warn and skip non-image data sets - if not is_image(dcm): - warnings.warn("Skipping non-image data set: %s" % dcm_path) - continue - #Extract the meta data and group meta = extractor(dcm) key_list = [] # Values from group_by elems with equality testing @@ -1172,7 +1127,6 @@ def parse_and_group(src_paths, group_by=default_group_keys, extractor=None, return OrderedDict(sorted(full_results.items())) - def stack_group(group, warn_on_except=False, **stack_args): result = DicomStack(**stack_args) for dcm, meta, fn in group: @@ -1186,7 +1140,6 @@ def stack_group(group, warn_on_except=False, **stack_args): raise return result - def parse_and_stack(src_paths, group_by=default_group_keys, extractor=None, force=False, warn_on_except=False, **stack_args): '''Parse the given dicom files into a dictionary containing one or more diff --git a/src/dcmstack/info.py b/src/dcmstack/info.py index 316b249..d9b0e09 100644 --- a/src/dcmstack/info.py +++ b/src/dcmstack/info.py @@ -4,9 +4,9 @@ import sys _version_major = 0 -_version_minor = 9 +_version_minor = 7 _version_micro = 0 -_version_extra = 'dev' +_version_extra = '' __version__ = "%s.%s.%s%s" % (_version_major, _version_minor, _version_micro, @@ -24,7 +24,7 @@ # Hard dependencies install_requires = ['pydicom >= 0.9.7', - 'nibabel >= 2.1.0', + 'nibabel >= 2.0.0', ] # Add version specific dependencies if sys.version_info < (2, 6): @@ -52,4 +52,4 @@ VERSION = __version__ INSTALL_REQUIRES = install_requires EXTRAS_REQUIRES = extras_requires -PROVIDES = ["dcmstack"] +PROVIDES = ["dcmstack"] \ No newline at end of file diff --git a/src/dcmstack/nitool_cli.py b/src/dcmstack/nitool_cli.py index 7ad4542..fd45353 100644 --- a/src/dcmstack/nitool_cli.py +++ b/src/dcmstack/nitool_cli.py @@ -179,7 +179,7 @@ def check_overwrite(): def embed(args): dest_nii = nb.load(args.dest_nii[0]) - hdr = dest_nii.header + hdr = dest_nii.get_header() try: src_wrp = NiftiWrapper(dest_nii, False) except MissingExtensionError: diff --git a/test/test_dcmmeta.py b/test/test_dcmmeta.py index 63d0e9c..af9726d 100644 --- a/test/test_dcmmeta.py +++ b/test/test_dcmmeta.py @@ -908,7 +908,7 @@ def test_nifti_wrapper_init(): assert_raises(dcmmeta.MissingExtensionError, dcmmeta.NiftiWrapper, nii) - hdr = nii.header + hdr = nii.get_header() ext = dcmmeta.DcmMetaExtension.make_empty((5, 5, 5), np.eye(4)) hdr.extensions.append(ext) nw = dcmmeta.NiftiWrapper(nii) @@ -922,7 +922,7 @@ def test_nifti_wrapper_init(): class TestMetaValid(object): def setUp(self): nii = nb.Nifti1Image(np.zeros((5, 5, 5, 7, 9)), np.eye(4)) - hdr = nii.header + hdr = nii.get_header() hdr.set_dim_info(None, None, 2) self.nw = dcmmeta.NiftiWrapper(nii, True) for classes in self.nw.meta_ext.get_valid_classes(): @@ -957,7 +957,7 @@ def test_slice_dim_changed(self): ok_(self.nw.meta_valid(classes)) def test_slice_dir_changed(self): - aff = self.nw.nii_img.affine + aff = self.nw.nii_img.get_affine() aff[:] = np.c_[aff[2, :], aff[1, :], aff[0, :], @@ -973,7 +973,7 @@ def test_slice_dir_changed(self): class TestGetMeta(object): def setUp(self): nii = nb.Nifti1Image(np.zeros((5, 5, 5, 7, 9)), np.eye(4)) - hdr = nii.header + hdr = nii.get_header() hdr.set_dim_info(None, None, 2) self.nw = dcmmeta.NiftiWrapper(nii, True) @@ -1075,7 +1075,7 @@ class TestSplit(object): def setUp(self): self.arr = np.arange(3 * 3 * 3 * 5 * 7).reshape(3, 3, 3, 5, 7) nii = nb.Nifti1Image(self.arr, np.diag([1.1, 1.1, 1.1, 1.0])) - hdr = nii.header + hdr = nii.get_header() hdr.set_dim_info(None, None, 2) self.nw = dcmmeta.NiftiWrapper(nii, True) @@ -1092,7 +1092,7 @@ def setUp(self): def test_split_slice(self): for split_idx, nw_split in enumerate(self.nw.split(2)): eq_(nw_split.nii_img.shape, (3, 3, 1, 5, 7)) - ok_(np.allclose(nw_split.nii_img.affine, + ok_(np.allclose(nw_split.nii_img.get_affine(), np.c_[[1.1, 0.0, 0.0, 0.0], [0.0, 1.1, 0.0, 0.0], [0.0, 0.0, 1.1, 0.0], @@ -1107,7 +1107,7 @@ def test_split_slice(self): def test_split_time(self): for split_idx, nw_split in enumerate(self.nw.split(3)): eq_(nw_split.nii_img.shape, (3, 3, 3, 1, 7)) - ok_(np.allclose(nw_split.nii_img.affine, + ok_(np.allclose(nw_split.nii_img.get_affine(), np.diag([1.1, 1.1, 1.1, 1.0]))) ok_(np.all(nw_split.nii_img.get_data() == self.arr[:, :, :, split_idx:split_idx+1, :]) @@ -1116,7 +1116,7 @@ def test_split_time(self): def test_split_vector(self): for split_idx, nw_split in enumerate(self.nw.split(4)): eq_(nw_split.nii_img.shape, (3, 3, 3, 5)) - ok_(np.allclose(nw_split.nii_img.affine, + ok_(np.allclose(nw_split.nii_img.get_affine(), np.diag([1.1, 1.1, 1.1, 1.0]))) ok_(np.all(nw_split.nii_img.get_data() == self.arr[:, :, :, :, split_idx]) @@ -1141,10 +1141,10 @@ def test_from_dicom(): src_dw = nb.nicom.dicomwrappers.wrapper_from_data(src_dcm) meta = {'EchoTime': 40} nw = dcmmeta.NiftiWrapper.from_dicom(src_dcm, meta) - hdr = nw.nii_img.header - eq_(nw.nii_img.shape, (192, 192, 1)) + hdr = nw.nii_img.get_header() + eq_(nw.nii_img.get_shape(), (192, 192, 1)) ok_(np.allclose(np.dot(np.diag([-1., -1., 1., 1.]), src_dw.get_affine()), - nw.nii_img.affine) + nw.nii_img.get_affine()) ) eq_(hdr.get_xyzt_units(), ('mm', 'sec')) eq_(hdr.get_dim_info(), (0, 1, 2)) @@ -1159,7 +1159,7 @@ def test_from_2d_slice_to_3d(): aff = np.diag((1.1, 1.1, 1.1, 1.0)) aff[:3, 3] += [0.0, 0.0, idx * 0.5] nii = nb.Nifti1Image(arr, aff) - hdr = nii.header + hdr = nii.get_header() hdr.set_dim_info(0, 1, 2) hdr.set_xyzt_units('mm', 'sec') nw = dcmmeta.NiftiWrapper(nii, True) @@ -1169,7 +1169,7 @@ def test_from_2d_slice_to_3d(): merged = dcmmeta.NiftiWrapper.from_sequence(slice_nws, 2) eq_(merged.nii_img.shape, (4, 4, 3)) - ok_(np.allclose(merged.nii_img.affine, + ok_(np.allclose(merged.nii_img.get_affine(), np.diag((1.1, 1.1, 0.5, 1.0))) ) eq_(merged.meta_ext.get_values_and_class('EchoTime'), @@ -1178,7 +1178,7 @@ def test_from_2d_slice_to_3d(): eq_(merged.meta_ext.get_values_and_class('SliceLocation'), (list(range(3)), ('global', 'slices')) ) - merged_hdr = merged.nii_img.header + merged_hdr = merged.nii_img.get_header() eq_(merged_hdr.get_dim_info(), (0, 1, 2)) eq_(merged_hdr.get_xyzt_units(), ('mm', 'sec')) merged_data = merged.nii_img.get_data() @@ -1194,7 +1194,7 @@ def test_from_3d_time_to_4d(): (idx + 1) * (4 * 4 * 4) ).reshape(4, 4, 4) nii = nb.Nifti1Image(arr, np.diag((1.1, 1.1, 1.1, 1.0))) - hdr = nii.header + hdr = nii.get_header() hdr.set_dim_info(0, 1, 2) hdr.set_xyzt_units('mm', 'sec') nw = dcmmeta.NiftiWrapper(nii, True) @@ -1208,7 +1208,7 @@ def test_from_3d_time_to_4d(): merged = dcmmeta.NiftiWrapper.from_sequence(time_nws, 3) eq_(merged.nii_img.shape, (4, 4, 4, 3)) - ok_(np.allclose(merged.nii_img.affine, + ok_(np.allclose(merged.nii_img.get_affine(), np.diag((1.1, 1.1, 1.1, 1.0))) ) eq_(merged.meta_ext.get_values_and_class('PatientID'), @@ -1223,7 +1223,7 @@ def test_from_3d_time_to_4d(): eq_(merged.meta_ext.get_values_and_class('AcquisitionTime'), (list(range(4 * 3)), ('global', 'slices')) ) - merged_hdr = merged.nii_img.header + merged_hdr = merged.nii_img.get_header() eq_(merged_hdr.get_dim_info(), (0, 1, 2)) eq_(merged_hdr.get_xyzt_units(), ('mm', 'sec')) merged_data = merged.nii_img.get_data() @@ -1240,7 +1240,7 @@ def test_from_3d_vector_to_4d(): (idx + 1) * (4 * 4 * 4) ).reshape(4, 4, 4) nii = nb.Nifti1Image(arr, np.diag((1.1, 1.1, 1.1, 1.0))) - hdr = nii.header + hdr = nii.get_header() hdr.set_dim_info(0, 1, 2) hdr.set_xyzt_units('mm', 'sec') nw = dcmmeta.NiftiWrapper(nii, True) @@ -1254,7 +1254,7 @@ def test_from_3d_vector_to_4d(): merged = dcmmeta.NiftiWrapper.from_sequence(vector_nws, 4) eq_(merged.nii_img.shape, (4, 4, 4, 1, 3)) - ok_(np.allclose(merged.nii_img.affine, + ok_(np.allclose(merged.nii_img.get_affine(), np.diag((1.1, 1.1, 1.1, 1.0))) ) eq_(merged.meta_ext.get_values_and_class('PatientID'), @@ -1269,7 +1269,7 @@ def test_from_3d_vector_to_4d(): eq_(merged.meta_ext.get_values_and_class('AcquisitionTime'), (list(range(4 * 3)), ('global', 'slices')) ) - merged_hdr = merged.nii_img.header + merged_hdr = merged.nii_img.get_header() eq_(merged_hdr.get_dim_info(), (0, 1, 2)) eq_(merged_hdr.get_xyzt_units(), ('mm', 'sec')) merged_data = merged.nii_img.get_data() @@ -1288,7 +1288,7 @@ def test_merge_inconsistent_hdr(): (idx + 1) * (4 * 4 * 4) ).reshape(4, 4, 4) nii = nb.Nifti1Image(arr, np.diag((1.1, 1.1, 1.1, 1.0))) - hdr = nii.header + hdr = nii.get_header() if idx == 1: hdr.set_dim_info(1, 0, 2) hdr.set_xyzt_units('mm', None) @@ -1299,7 +1299,7 @@ def test_merge_inconsistent_hdr(): time_nws.append(nw) merged = dcmmeta.NiftiWrapper.from_sequence(time_nws) - merged_hdr = merged.nii_img.header + merged_hdr = merged.nii_img.get_header() eq_(merged_hdr.get_dim_info(), (None, None, 2)) eq_(merged_hdr.get_xyzt_units(), ('mm', 'unknown')) @@ -1311,7 +1311,7 @@ def test_merge_with_slc_and_without(): (idx + 1) * (4 * 4 * 4) ).reshape(4, 4, 4) nii = nb.Nifti1Image(arr, np.diag((1.1, 1.1, 1.1, 1.0))) - hdr = nii.header + hdr = nii.get_header() if idx == 0: hdr.set_dim_info(0, 1, 2) hdr.set_xyzt_units('mm', 'sec') @@ -1326,7 +1326,7 @@ def test_merge_with_slc_and_without(): merged = dcmmeta.NiftiWrapper.from_sequence(input_nws) eq_(merged.nii_img.shape, (4, 4, 4, 3)) - ok_(np.allclose(merged.nii_img.affine, + ok_(np.allclose(merged.nii_img.get_affine(), np.diag((1.1, 1.1, 1.1, 1.0))) ) eq_(merged.meta_ext.get_values_and_class('PatientID'), @@ -1338,5 +1338,5 @@ def test_merge_with_slc_and_without(): eq_(merged.meta_ext.get_values_and_class('SliceLocation'), (list(range(4)) + ([None] * 8), ('global', 'slices')) ) - merged_hdr = merged.nii_img.header + merged_hdr = merged.nii_img.get_header() eq_(merged_hdr.get_xyzt_units(), ('mm', 'sec')) \ No newline at end of file diff --git a/test/test_dcmstack.py b/test/test_dcmstack.py index 47bc4f0..b09a2b7 100644 --- a/test/test_dcmstack.py +++ b/test/test_dcmstack.py @@ -43,7 +43,6 @@ 'BitsStored' : 16, 'PixelRepresentation' : 0, 'SamplesPerPixel' : 1, - 'PhotometricInterpretation': 'MONOCHROME2', } def make_dicom(attrs=None, pix_val=1): @@ -366,14 +365,14 @@ def setUp(self): def test_single_slice(self): stack = dcmstack.DicomStack() stack.add_dcm(self.inputs[0]) - shape = stack.shape + shape = stack.get_shape() eq_(shape, (192, 192, 1)) def test_three_dim(self): stack = dcmstack.DicomStack() stack.add_dcm(self.inputs[0]) stack.add_dcm(self.inputs[1]) - shape = stack.shape + shape = stack.get_shape() eq_(shape, (192, 192, 2)) def test_four_dim(self): @@ -382,7 +381,7 @@ def test_four_dim(self): stack.add_dcm(self.inputs[1]) stack.add_dcm(self.inputs[2]) stack.add_dcm(self.inputs[3]) - shape = stack.shape + shape = stack.get_shape() eq_(shape, (192, 192, 2, 2)) def test_five_dim(self): @@ -391,7 +390,7 @@ def test_five_dim(self): stack.add_dcm(self.inputs[1]) stack.add_dcm(self.inputs[2]) stack.add_dcm(self.inputs[3]) - shape = stack.shape + shape = stack.get_shape() eq_(shape, (192, 192, 2, 1, 2)) def test_allow_dummy(self): @@ -400,7 +399,7 @@ def test_allow_dummy(self): stack = dcmstack.DicomStack(allow_dummies=True) stack.add_dcm(self.inputs[0]) stack.add_dcm(self.inputs[1]) - shape = stack.shape + shape = stack.get_shape() eq_(shape, (192, 192, 2)) class TestGuessDim(object): @@ -436,7 +435,7 @@ def test_single_guess(self): for idx, in_dcm in enumerate(self.inputs): setattr(in_dcm, key, self._get_vr_ord(key, idx)) stack.add_dcm(in_dcm) - eq_(stack.shape, (192, 192, 2, 2)) + eq_(stack.get_shape(), (192, 192, 2, 2)) for in_dcm in self.inputs: delattr(in_dcm, key) @@ -451,7 +450,7 @@ def test_wrong_guess_first(self): dcmstack.DicomStack.sort_guesses[-1], self._get_vr_ord(key, idx) ) stack.add_dcm(in_dcm) - eq_(stack.shape, (192, 192, 2, 2)) + eq_(stack.get_shape(), (192, 192, 2, 2)) class TestGetData(object): def setUp(self): @@ -471,7 +470,7 @@ def test_single_slice(self): stack = dcmstack.DicomStack() stack.add_dcm(self.inputs[0]) data = stack.get_data() - eq_(data.shape, stack.shape) + eq_(data.shape, stack.get_shape()) eq_(sha256(data).hexdigest(), '15cfa107ca73810a1c97f1c1872a7a4a05808ba6147e039cef3f63fa08735f5d') @@ -480,7 +479,7 @@ def test_three_dim(self): stack.add_dcm(self.inputs[0]) stack.add_dcm(self.inputs[1]) data = stack.get_data() - eq_(data.shape, stack.shape) + eq_(data.shape, stack.get_shape()) eq_(sha256(data).hexdigest(), 'ab5225fdbedceeea3442b2c9387e1abcbf398c71f525e0017251849c3cfbf49c') @@ -491,7 +490,7 @@ def test_four_dim(self): stack.add_dcm(self.inputs[2]) stack.add_dcm(self.inputs[3]) data = stack.get_data() - eq_(data.shape, stack.shape) + eq_(data.shape, stack.get_shape()) eq_(sha256(data).hexdigest(), 'bb3639a6ece13dc9a11d65f1b09ab3ccaed63b22dcf0f96fb5d3dd8805cc7b8a') @@ -502,7 +501,7 @@ def test_five_dim(self): stack.add_dcm(self.inputs[2]) stack.add_dcm(self.inputs[3]) data = stack.get_data() - eq_(data.shape, stack.shape) + eq_(data.shape, stack.get_shape()) eq_(sha256(data).hexdigest(), 'bb3639a6ece13dc9a11d65f1b09ab3ccaed63b22dcf0f96fb5d3dd8805cc7b8a') @@ -513,7 +512,7 @@ def test_allow_dummy(self): stack.add_dcm(self.inputs[0]) stack.add_dcm(self.inputs[1]) data = stack.get_data() - eq_(data.shape, stack.shape) + eq_(data.shape, stack.get_shape()) ok_(np.all(data[:, :, -1] == np.iinfo(np.int16).max)) eq_(sha256(data).hexdigest(), '7d85fbcb60a5021a45df3975613dcb7ac731830e0a268590cc798dc39897c04b') @@ -533,7 +532,7 @@ def setUp(self): def test_single_slice(self): stack = dcmstack.DicomStack() stack.add_dcm(self.inputs[0]) - affine = stack.affine + affine = stack.get_affine() ref = np.load(path.join(self.data_dir, 'single_slice_aff.npy')) ok_(np.allclose(affine, ref)) @@ -541,7 +540,7 @@ def test_three_dim(self): stack = dcmstack.DicomStack() stack.add_dcm(self.inputs[0]) stack.add_dcm(self.inputs[1]) - affine = stack.affine + affine = stack.get_affine() ref = np.load(path.join(self.data_dir, 'single_vol_aff.npy')) ok_(np.allclose(affine, ref)) @@ -626,9 +625,9 @@ def _build_nii(self, name): assert False # Unknown name def _chk(self, nii, ref_base_fn): - hdr = nii.header + hdr = nii.get_header() ref_nii = nb.load(path.join(self.data_dir, ref_base_fn) + '.nii.gz') - ref_hdr = ref_nii.header + ref_hdr = ref_nii.get_header() for key in self.eq_keys: print("Testing key %s" % key)