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

FIX: Accommodate Python 3 in DICOM slice sorting #728

Merged
merged 6 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions nibabel/nicom/dicomreaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def slices_to_series(wrappers):
out_vol_lists = []
for vol_list in volume_lists:
if len(vol_list) > 1:
vol_list.sort(_slice_sorter)
vol_list.sort(key=_slice_sorter)
zs = [s.slice_indicator for s in vol_list]
if len(set(zs)) < len(zs): # not unique zs
# third pass
Expand All @@ -163,12 +163,12 @@ def slices_to_series(wrappers):
return out_vol_lists


def _slice_sorter(s1, s2):
return cmp(s1.slice_indicator, s2.slice_indicator)
def _slice_sorter(s):
return s.slice_indicator


def _instance_sorter(s1, s2):
return cmp(s1.instance_number, s2.instance_number)
def _instance_sorter(s):
return s.instance_number


def _third_pass(wrappers):
Expand All @@ -182,9 +182,9 @@ def _third_pass(wrappers):
'missing InstanceNumber')
if len(set(inos)) < len(inos):
raise DicomReadError(msg_fmt % 'some or all slices with '
'the sane InstanceNumber')
'the same InstanceNumber')
# sort by instance number
wrappers.sort(_instance_sorter)
wrappers.sort(key=_instance_sorter)
# start loop, in which we start a new volume, each time we see a z
# we've seen already in the current volume
dw = wrappers[0]
Expand Down
Binary file added nibabel/nicom/tests/data/0.dcm
Binary file not shown.
Binary file added nibabel/nicom/tests/data/1.dcm
Binary file not shown.
11 changes: 11 additions & 0 deletions nibabel/nicom/tests/test_dicomreaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

"""

from os.path import join as pjoin, abspath

import numpy as np

from .. import dicomreaders as didr
Expand Down Expand Up @@ -68,3 +70,12 @@ def test_passing_kwds():
IO_DATA_PATH,
csa_glob,
dicom_kwargs=dict(force=True))

@dicom_test
def test_slices_to_series():
dicom_files = (pjoin(IO_DATA_PATH, "%d.dcm" % i) for i in range(2))
wrappers = [didr.wrapper_from_file(f) for f in dicom_files]
series = didr.slices_to_series(wrappers)
assert_equal(len(series), 1)
assert_equal(len(series[0]), 2)