Skip to content

BF: deterministic order of slice_time deduction, warning if multiple match #647

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

Merged
merged 6 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 13 additions & 3 deletions nibabel/nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,14 +1573,24 @@ def set_slice_times(self, slice_times):
so_recoder = self._field_recoders['slice_code']
labels = so_recoder.value_set('label')
labels.remove('unknown')
for label in labels:

matching_labels = []
# reversing the order so that "alternative" go last
for label in sorted(labels, reverse=True):
if np.all(st_order == self._slice_time_order(
label,
n_timed)):
break
else:
matching_labels.append(label)

if not matching_labels:
raise HeaderDataError('slice ordering of %s fits '
'with no known scheme' % st_order)
if len(matching_labels) > 1:
warnings.warn(
'Multiple slice orders satisfy: %s. Choosing the first one'
% ', '.join(matching_labels)
)
label = matching_labels[0]
# Set values into header
hdr['slice_start'] = slice_start
hdr['slice_end'] = slice_end
Expand Down
13 changes: 13 additions & 0 deletions nibabel/tests/test_nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,19 @@ def test_slice_times(self):
assert_equal(hdr['slice_end'], 5)
assert_array_almost_equal(hdr['slice_duration'], 0.1)

# Ambiguous case
hdr2 = self.header_class()
hdr2.set_dim_info(slice=2)
hdr2.set_slice_duration(0.1)
hdr2.set_data_shape((1, 1, 2))
hdr2.set_slice_times([0.1, 0]) # will generate warning that multiple match
Copy link
Member

Choose a reason for hiding this comment

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

Want to check that the warning is generated? e.g.

with clear_and_catch_warnings() as w:
    hdr2.set_slice_times([0.1, 0])
    assert len(w) == 1

# but always must be choosing sequential one first
assert_equal(hdr2.get_value_label('slice_code'), 'sequential decreasing')
# and the other direction
hdr2.set_slice_times([0, 0.1])
assert_equal(hdr2.get_value_label('slice_code'), 'sequential increasing')


def test_intents(self):
ehdr = self.header_class()
ehdr.set_intent('t test', (10,), name='some score')
Expand Down
2 changes: 1 addition & 1 deletion nibabel/volumeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(self, codes, fields=('code',), map_maker=dict):

Parameters
----------
codes : seqence of sequences
codes : sequence of sequences
Each sequence defines values (codes) that are equivalent
fields : {('code',) string sequence}, optional
names by which elements in sequences can be accessed
Expand Down