Skip to content

Commit

Permalink
Import Upstream version 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
yarikoptic committed Mar 5, 2020
1 parent 6a56e51 commit 714422f
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 134 deletions.
12 changes: 6 additions & 6 deletions doc/Python_Tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 17 additions & 17 deletions src/dcmstack/dcmmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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]
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
Loading

0 comments on commit 714422f

Please sign in to comment.