Skip to content

Commit 9430224

Browse files
authored
Merge pull request #728 from reddigari/master
FIX: Accommodate Python 3 in DICOM slice sorting
2 parents 4f7d3d5 + 5a30076 commit 9430224

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

nibabel/nicom/dicomreaders.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def slices_to_series(wrappers):
146146
out_vol_lists = []
147147
for vol_list in volume_lists:
148148
if len(vol_list) > 1:
149-
vol_list.sort(_slice_sorter)
149+
vol_list.sort(key=_slice_sorter)
150150
zs = [s.slice_indicator for s in vol_list]
151151
if len(set(zs)) < len(zs): # not unique zs
152152
# third pass
@@ -163,12 +163,12 @@ def slices_to_series(wrappers):
163163
return out_vol_lists
164164

165165

166-
def _slice_sorter(s1, s2):
167-
return cmp(s1.slice_indicator, s2.slice_indicator)
166+
def _slice_sorter(s):
167+
return s.slice_indicator
168168

169169

170-
def _instance_sorter(s1, s2):
171-
return cmp(s1.instance_number, s2.instance_number)
170+
def _instance_sorter(s):
171+
return s.instance_number
172172

173173

174174
def _third_pass(wrappers):
@@ -182,9 +182,9 @@ def _third_pass(wrappers):
182182
'missing InstanceNumber')
183183
if len(set(inos)) < len(inos):
184184
raise DicomReadError(msg_fmt % 'some or all slices with '
185-
'the sane InstanceNumber')
185+
'the same InstanceNumber')
186186
# sort by instance number
187-
wrappers.sort(_instance_sorter)
187+
wrappers.sort(key=_instance_sorter)
188188
# start loop, in which we start a new volume, each time we see a z
189189
# we've seen already in the current volume
190190
dw = wrappers[0]

nibabel/nicom/tests/data/0.dcm

221 KB
Binary file not shown.

nibabel/nicom/tests/data/1.dcm

221 KB
Binary file not shown.

nibabel/nicom/tests/test_dicomreaders.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
"""
44

5+
from os.path import join as pjoin, abspath
6+
57
import numpy as np
68

79
from .. import dicomreaders as didr
@@ -68,3 +70,12 @@ def test_passing_kwds():
6870
IO_DATA_PATH,
6971
csa_glob,
7072
dicom_kwargs=dict(force=True))
73+
74+
@dicom_test
75+
def test_slices_to_series():
76+
dicom_files = (pjoin(IO_DATA_PATH, "%d.dcm" % i) for i in range(2))
77+
wrappers = [didr.wrapper_from_file(f) for f in dicom_files]
78+
series = didr.slices_to_series(wrappers)
79+
assert_equal(len(series), 1)
80+
assert_equal(len(series[0]), 2)
81+

0 commit comments

Comments
 (0)