Skip to content

Fix: Rounding error in get_data with tmin/tmax#13635

Open
git-gigi wants to merge 6 commits intomne-tools:mainfrom
git-gigi:fix-get-data-rounding
Open

Fix: Rounding error in get_data with tmin/tmax#13635
git-gigi wants to merge 6 commits intomne-tools:mainfrom
git-gigi:fix-get-data-rounding

Conversation

@git-gigi
Copy link

@git-gigi git-gigi commented Feb 4, 2026

Description

This PR fixes an off-by-one inconsistency between epochs.crop() and epochs.get_data() caused by floating point truncation when converting time to indices.

Analysis

Previously, _handle_tmin_tmax used the default behavior of time_as_index (which performs a floor operation/truncation). When passing a float time t (e.g., 0.77) that is represented internally as slightly less (e.g., 0.76999...), get_data(tmin=t) would return the sample at 0.76 instead of 0.77.
crop() correctly uses use_rounding=True, leading to inconsistent results for the same input time.

Fix

Closes #13634

@welcome
Copy link

welcome bot commented Feb 4, 2026

Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴

)
_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.

Comment on lines +1616 to +1619
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]
)
Copy link
Member

Choose a reason for hiding this comment

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

This will need some discussion. When use_rounding was introduced, it was determined that we shouldn't change the default:

#2311 (comment)

mne/epochs.py Outdated

# handle tmin/tmax as start and stop indices into data array
n_times = self.times.size
# QUI c'è la fix specifica per le Epochs
Copy link
Member

Choose a reason for hiding this comment

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

code comments in English please. But also: code comments should be more specific/useful than "here is the fix for Epochs". This one isn't really needed at all IMO

Suggested change
# QUI c'è la fix specifica per le Epochs

Copy link
Author

Choose a reason for hiding this comment

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

Sorry about that! I left a debug comment by mistake. I will remove it in the next commit

@git-gigi
Copy link
Author

git-gigi commented Feb 5, 2026

Thanks @drammock. I get why this is tricky regarding backward compatibility. I'll stop working on the code for now and wait for the discussion. Just let me know if you want me to check Raw or Evoked in the meantime - happy to help if needed :)

@drammock drammock mentioned this pull request Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In crop(tmin) versus get_data(tmin), tmin has a different meaning

2 participants