Skip to content
Draft
Show file tree
Hide file tree
Changes from 7 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
17 changes: 13 additions & 4 deletions mne/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,16 +637,25 @@ def last_samp(self):
def _last_time(self):
return self.last_samp / float(self.info["sfreq"])

def time_as_index(self, times, use_rounding=False, origin=None):
def time_as_index(self, times, use_rounding=None, origin=None):
Comment thread
mathias-sm marked this conversation as resolved.
Outdated
"""Convert time to indices.

Parameters
----------
times : list-like | float | int
List of numbers or a number representing points in time.
use_rounding : bool
If True, use rounding (instead of truncation) when converting
times to indices. This can help avoid non-unique indices.
use_rounding : bool | None
If True, use rounding when converting times to indices. This can
help avoid non-unique indices.
If False, use truncation when converting times to indices.
If None (the default), rounding is used but a FutureWarning is
emitted. Pass ``True`` or ``False`` explicitly to silence the
warning.

.. versionchanged:: 2.0
Comment thread
mathias-sm marked this conversation as resolved.
Outdated
The default changed from ``False`` to ``None``, which will
round and emit a warning. In a future release the default
will change to ``True``.
origin : datetime | float | int | None
Time reference for times. If None, ``times`` are assumed to be
relative to :term:`first_samp`.
Expand Down
2 changes: 1 addition & 1 deletion mne/io/egi/tests/test_egi.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_egi_mff_pause(fname, skip_times, event_times):
for ii, annot in enumerate(raw.annotations):
assert annot["description"] == "BAD_ACQ_SKIP"
start, stop = raw.time_as_index(
[annot["onset"], annot["onset"] + annot["duration"]]
[annot["onset"], annot["onset"] + annot["duration"]], use_rounding=False
)
data, _ = raw[:, start:stop]
assert_array_equal(data[other_picks], 0.0)
Expand Down
12 changes: 9 additions & 3 deletions mne/io/tests/test_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,14 +620,20 @@ def test_time_as_index():
"""Test indexing of raw times."""
raw = read_raw_fif(raw_fname)

# Test original (non-rounding) indexing behavior
orig_inds = raw.time_as_index(raw.times)
# Test truncation behavior with explicit use_rounding=False
orig_inds = raw.time_as_index(raw.times, use_rounding=False)
assert len(set(orig_inds)) != len(orig_inds)

# Test new (rounding) indexing behavior
# Test rounding behavior with explicit use_rounding=True
new_inds = raw.time_as_index(raw.times, use_rounding=True)
assert_array_equal(new_inds, np.arange(len(raw.times)))

# Test deprecation warning when use_rounding is not specified
# Temporarily turned of to measure impact
#
# with pytest.warns(FutureWarning, match="use_rounding=False is being changed"):
# raw.time_as_index(raw.times)


@pytest.mark.parametrize("meas_date", [None, "orig"])
@pytest.mark.parametrize("first_samp", [0, 10000])
Expand Down
29 changes: 25 additions & 4 deletions mne/utils/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,16 +493,25 @@ def _check_decim(info, decim, offset, check_filter=True):
class TimeMixin:
"""Class for time operations on any MNE object that has a time axis."""

def time_as_index(self, times, use_rounding=False):
def time_as_index(self, times, use_rounding=None):
Comment thread
mathias-sm marked this conversation as resolved.
Outdated
"""Convert time to indices.

Parameters
----------
times : list-like | float | int
List of numbers or a number representing points in time.
use_rounding : bool
If True, use rounding (instead of truncation) when converting
times to indices. This can help avoid non-unique indices.
use_rounding : bool | None
If True, use rounding when converting times to indices. This can
help avoid non-unique indices.
If False, use truncation when converting times to indices.
If None (the default), rounding is used but a FutureWarning is
emitted. Pass ``True`` or ``False`` explicitly to silence the
warning.

.. versionchanged:: 2.0
The default changed from ``False`` to ``None``, which will
round and emit a warning. In a future release the default
will change to ``True``.

Returns
-------
Expand All @@ -511,6 +520,18 @@ def time_as_index(self, times, use_rounding=False):
"""
from ..source_estimate import _BaseSourceEstimate

if use_rounding is None:
# Turned off temporarily to see the impact of the change without
# crashing on a FutureWarning
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To really see the impact I would suggest following my RuntimeError suggestion from #13634 (comment)

By computing both ways and raising this error it would clearly show all tests and examples (with a [circle full]) there the behavior would actually change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah yes indeed somehow I had forgotten about this. Quick exploration of that: it breaks 5 tests. I will push this after making sure that I'm doing things right to get them from the CI.

FAILED mne/io/tests/test_raw.py::test_get_data_tmin_tmax - RuntimeError: This would have returned a different value: index=array([600.61499023]), different from np.round(index)=array([601.]).
FAILED mne/stats/tests/test_erp.py::test_compute_sme - RuntimeError: This would have returned a different value: index=array([180.06149902]), different from np.round(index)=array([180.]).
FAILED mne/tests/test_evoked.py::test_get_peak - RuntimeError: This would have returned a different value: index=array([30.03074951]), different from np.round(index)=array([30.]).
FAILED mne/viz/tests/test_ica.py::test_plot_ica_overlay - RuntimeError: This would have returned a different value: index=array([1801.8449707]), different from np.round(index)=array([1802.]).
FAILED mne/tests/test_proj.py::test_compute_proj_raw - RuntimeError: This would have returned a different value: index=array([1501.53747559]), different from np.round(index)=array([1502.]).

#
# warn(
# "The default of use_rounding=False is being changed to True "
# "in a future release. Pass use_rounding=True or "
# "use_rounding=False explicitly to silence this warning.",
# FutureWarning,
# )
use_rounding = True

if isinstance(self, _BaseSourceEstimate):
sfreq = 1.0 / self.tstep
else:
Expand Down
Loading