Skip to content
Open
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
28 changes: 28 additions & 0 deletions mne/epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,34 @@ def _handle_empty(self, on_empty, meth):
)
_on_missing(on_empty, msg, error_klass=RuntimeError)

def _handle_tmin_tmax(self, tmin, tmax):
Copy link
Member

Choose a reason for hiding this comment

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

adding a new private method to Epochs that has the same name as an existing utility function is not the right way to go about this. It duplicates code and introduces the possibility of epochs behaving differently than Raw/Evoked for example.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review @drammock.

I understand the concern about code duplication. I initially tried modifying the shared _handle_tmin_tmax in mixin.py, but adding use_rounding=True there caused regressions in Raw tests (shifting data by one sample on Windows environments due to float precision). That's why I attempted the override in Epochs.

The core issue linked (#13634) is that epochs.crop(tmin=t) includes a sample that epochs.get_data(tmin=t) excludes. Since crop uses rounding internally, users expect get_data to match that behavior for consistency.

If modifying the global mixin.py is risky for Raw backward compatibility, and overriding in Epochs is discouraged, do you have a suggestion on how to reconcile get_data with crop for Epochs specifically? Maybe passing a round_tmin argument to get_data?

Copy link
Member

Choose a reason for hiding this comment

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

The core issue linked (#13634) is that epochs.crop(tmin=t) includes a sample that epochs.get_data(tmin=t) excludes. Since crop uses rounding internally, users expect get_data to match that behavior for consistency.

yes, that was clear from the issue description.

If modifying the global mixin.py is risky for Raw backward compatibility, and overriding in Epochs is discouraged, do you have a suggestion on how to reconcile get_data with crop for Epochs specifically? Maybe passing a round_tmin argument to get_data?

I suspect that the problem is not limited to Epochs, but also affects Raw and Evoked (and TFR... anything that inherits the mixin). I don't have a suggestion off the top of my head; as I said before, this will need some discussion. Changing how get_data() works (to accord with crop()) --- or vice-versa --- has potentially wide-reaching consequences. I know it's a single sample, but as you've seen it's enough to break our tests, and it's also enough to change the results of user's existing analysis code, or even cause that code to crash if re-run. We don't take that lightly.

"""Convert seconds to index into data."""
_validate_type(
tmin,
types=("numeric", None),
item_name="tmin",
type_name="int, float, None",
)
_validate_type(
tmax,
types=("numeric", None),
item_name="tmax",
type_name="int, float, None",
)

# handle tmin/tmax as start and stop indices into data array
n_times = self.times.size
start = 0 if tmin is None else self.time_as_index(tmin, use_rounding=True)[0]
stop = (
n_times if tmax is None else self.time_as_index(tmax, use_rounding=True)[0]
)

# truncate start/stop to the open interval [0, n_times]
start = min(max(0, start), n_times)
stop = min(max(0, stop), n_times)

return start, stop

@verbose
def _get_data(
self,
Expand Down
34 changes: 34 additions & 0 deletions mne/tests/test_epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4855,6 +4855,40 @@ def fun(data):
assert_array_equal(out.get_data(non_picks), epochs.get_data(non_picks))


def test_get_data_rounding():
"""Test that get_data respects rounding for tmin/tmax (gh-13634)."""
# Data setup mirroring the issue report
data = np.linspace(-3.5, 1, 451).reshape((1, 1, 451))
info = create_info(["test"], 100.0, "eeg")
epochs = EpochsArray(data, info, tmin=-3.5, verbose=False)

t = 0.77

# compare crop() vs get_data()
# crop() uses proper rounding internally, get_data() should match it.
val_crop = epochs.copy().crop(tmin=t).get_data()[0, 0, 0]
val_get_data = epochs.get_data(tmin=t)[0, 0, 0]

assert_allclose(
val_get_data,
val_crop,
atol=1e-12,
err_msg="get_data(tmin) does not match crop(tmin)",
)

# verification on time consistency
# ensure we are getting the sample corresponding exactly to time 't'
idx = np.where(epochs.times == t)[0][0]
val_direct = epochs.get_data()[0, 0, idx]

assert_allclose(
val_get_data,
val_direct,
atol=1e-12,
err_msg="get_data(tmin) does not match direct indexing",
)


def test_apply_function_epo_ch_access():
"""Test ch-access within apply function to epoch objects."""

Expand Down