Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support channeled dicom slices #16

Merged
merged 6 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions dicom_numpy/combine_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,27 @@ def combine_slices(slice_datasets, rescale=None):


def _merge_slice_pixel_arrays(slice_datasets, rescale=None):
first_dataset = slice_datasets[0]
num_rows = first_dataset.Rows
num_columns = first_dataset.Columns
num_slices = len(slice_datasets)

sorted_slice_datasets = _sort_by_slice_position(slice_datasets)

if rescale is None:
rescale = any(_requires_rescaling(d) for d in sorted_slice_datasets)

if rescale:
voxels = np.empty((num_columns, num_rows, num_slices), dtype=np.float32, order='F')
for k, dataset in enumerate(sorted_slice_datasets):
first_dataset = sorted_slice_datasets[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't have a chance to read through this in detail until later, but two comments:

  1. We should add a test
  2. What happens if some slices are RGB and others aren't? Would the error message make the problem clear to a user?

Copy link
Contributor

@johndgiese johndgiese May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW thanks for making a PR for this---it will definitely make dicom-numpy more useful and generic!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My pleasure :)

  1. Yes, will add a bit later
  2. I do not think it is a legitimate situation (?). My guess is that along with Rows, Columns, BitsStored - the attribute SamplesPerPixel is necessary for calculating pixel_array (and its shape) from PixelData. If there are some slices with different shape, they cannot be combined.
    The current error will be something related to numpy's incompatible shapes.
    In order to avoid it, maybe we should add SamplesPerPixel to the invariant_properties list? (in _validate_slices_form_uniform_grid)

slice_dtype = first_dataset.pixel_array.dtype
slice_shape = first_dataset.pixel_array.T.shape
num_slices = len(sorted_slice_datasets)

voxels_shape = slice_shape + (num_slices,)
voxels_dtype = np.float32 if rescale else slice_dtype
voxels = np.empty(voxels_shape, dtype=voxels_dtype, order='F')

for k, dataset in enumerate(sorted_slice_datasets):
pixel_array = dataset.pixel_array.T
if rescale:
slope = float(getattr(dataset, 'RescaleSlope', 1))
intercept = float(getattr(dataset, 'RescaleIntercept', 0))
voxels[:, :, k] = dataset.pixel_array.T.astype(np.float32) * slope + intercept
else:
dtype = first_dataset.pixel_array.dtype
voxels = np.empty((num_columns, num_rows, num_slices), dtype=dtype, order='F')
for k, dataset in enumerate(sorted_slice_datasets):
voxels[:, :, k] = dataset.pixel_array.T
pixel_array = pixel_array.astype(np.float32) * slope + intercept
voxels[..., k] = pixel_array

return voxels

Expand Down Expand Up @@ -136,6 +136,7 @@ def _validate_slices_form_uniform_grid(slice_datasets):
'SeriesInstanceUID',
'Rows',
'Columns',
'SamplesPerPixel',
'PixelSpacing',
'PixelRepresentation',
'BitsAllocated',
Expand Down
24 changes: 21 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
negative_z_cos = (0, 0, -1)

arbitrary_shape = (10, 11)
arbitrary_rgb_shape = (10, 11, 3)


class MockSlice:
Expand All @@ -28,15 +29,21 @@ def __init__(self, pixel_array, slice_position, row_cosine=None, column_cosine=N
if column_cosine is None:
column_cosine = y_cos

na, nb = pixel_array.shape
shape = pixel_array.shape
if len(shape) == 2:
na, nb = shape
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
na, nb = shape
num_rows, num_columns = shape

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

num_columns, num_rows = shape
(because the pixel_array is not transposed:
pixel_array[j, i] == pixel_array.T[i, j])

SamplesPerPixel = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SamplesPerPixel = 1
samples_per_pixel = 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

else:
na, nb, SamplesPerPixel = shape
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
na, nb, SamplesPerPixel = shape
num_rows, num_columns, samples_per_pixel = shape

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 except of the order:
num_columns, num_rows, samples_per_pixel = shape


self.pixel_array = pixel_array

self.SeriesInstanceUID = 'arbitrary uid'
self.SOPClassUID = 'arbitrary sopclass uid'
self.PixelSpacing = [1.0, 1.0]
self.Rows = na
self.Columns = nb
self.Columns = na
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that your code appears to swap na and nb. My suggestions here swap them back---I am assuming your swap was unintentional.

Suggested change
self.Columns = na
self.Columns = num_columns

Copy link
Contributor Author

@jond01 jond01 May 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for naming, but the swap is intentional:
#16 (comment)

self.Rows = nb
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.Rows = nb
self.Rows = num_rows

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

self.SamplesPerPixel = SamplesPerPixel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.SamplesPerPixel = SamplesPerPixel
self.SamplesPerPixel = samples_per_pixel

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

self.Modality = 'MR'

# assume that the images are centered on the remaining unused axis
Expand All @@ -63,5 +70,16 @@ def axial_slices():
]


@pytest.fixture
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

def axial_rgb_slices():
# SamplesPerPixel = 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose and meaning of this comment wasn't entirely clear to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, unnecessary - removed

return [
MockSlice(randi(*arbitrary_rgb_shape), 0),
MockSlice(randi(*arbitrary_rgb_shape), 1),
MockSlice(randi(*arbitrary_rgb_shape), 2),
MockSlice(randi(*arbitrary_rgb_shape), 3),
]


def randi(*shape):
return np.random.randint(1000, size=shape, dtype='uint16')
6 changes: 6 additions & 0 deletions tests/test_combine_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def test_single_slice_spacing(self, axial_slices):
assert np.array_equal(array, dataset.pixel_array.T[:, :, None])
assert np.isclose(np.linalg.norm(affine[:, 2]), np.abs(slice_spacing))

def test_rgb_axial_set(self, axial_rgb_slices):
combined, _ = combine_slices(axial_rgb_slices)

manually_combined = np.stack([ds.pixel_array for ds in axial_rgb_slices], axis=0).T
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the transpose expected? Perhaps it was necessary because of the na and nb swap?

Copy link
Contributor Author

@jond01 jond01 May 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is expected because we return the transposed pixel array:

voxels[:, :, k] = dataset.pixel_array.T

With more convenient shape:
voxels.shape == (num_rows, num_columns, num_slices)
Or:
voxels.shape == (num_channels, num_rows, num_columns, num_slices)

assert np.array_equal(combined, manually_combined)


class TestMergeSlicePixelArrays:
def test_casting_if_only_rescale_slope(self):
Expand Down