Repository navigation
ENH, BUG: Retain Epochs.baseline after cropping; replace baseline=(None, None) with (tmin, tmax) - #8444
Conversation
I don't think this is the right approach. I think we should make it so that there is no longer a requirement that the baseline period be within |
I'm not sure I understand? What I want to change in a followup PR is if we crop BL-corrected Epochs, we would retain the baseline attribute and the repr would essentially tell you, "these Epochs were BL-corrected for the following BL-period, which doesn't exist anymore" Is that where you would like to go too? |
|
Just trying to make super small incremental steps to reduce the risk of introducing bugs |
|
No, I want to make it so that we no longer require the two values in
|
This behavior is currently implemented in this PR. I like it because it's explicit, and when you look at the repr of the epochs = mne.Epochs(raw, events, baseline=(None, 0), preload=True)
assert epochs.baseline == (epochs.tmin, 0)
epochs.crop(-0.05, None)
assert epochs.baseline is None |
I can live with this behavior. I can see advantages of each. But I don't like the first part of the PR:
I don't think we need it because in my plan we make it so that the baseline period is not removed and can always be reported properly. |
Yes yes yes, if I'm getting you correctly, this is exactly what I had initially planned for a follow-up PR. We're talking about this intended behavior, right? baseline = (None, 0)
epochs = mne.Epochs(raw, events, baseline=baseline, preload=True)
epochs.crop(0, None)
assert epochs.baseline == baseline |
As long as you really mean for the last line: Then yes this is the behavior I have in mind. But why add this other, seemingly unrelated new behavior ("Partial cropping of an Epochs baseline will now correctly inform the user that baseline correction has been switched off.") in this PR if it's immediately going to be removed by the next PR, which will avoid the removal / "switch off" part? I don't think we should do this, i.e., it should be removed from this PR. We should not backport any of this stuff BTW because we want months to test it out and make sure we're not breaking things. |
|
Had to update my above comment, it shouldn't be |
Yes this is what I had in mind. Sorry, got mixed up going back-and-forth between our discussion and my code. I agree we should probably do this all at once in this PR. I won't have time to work on it for a few days I'm afraid, but happy to pick it up again very soon. |
62255a1 to
53a1d1b
Compare
|
I've tried to implement what we discussed, but I'm in need of a break and will have to look at this again later tonight with hopefully fresh(er) eyes. This just to say, still WIP, and needs a little more thinking |
|
Let's see if tests pass now. I personally feel it's not so easy to intuitively grasp what I've done here just by looking at the altered code & tests. I will try to update one or more examples to demonstrate the new behavior. |
|
I have no clue how to fix the Circle issue. Doc building chokes on in maybe because it's a property..? |
|
Yes properties and attributes are documented differently by NumpyDoc and what works for one will not necessarily work for the other |
@hoechenberger nowadays we don't even need the |
|
@drammock
and it's not woring either. I've also tried adjusting without linking :\ |
|
Ok it's actually not that complicated after all :) The altered cropping behavior is demonstrated in https://23393-1301584-gh.circle-artifacts.com/0/dev/auto_tutorials/epochs/plot_10_epochs_overview.html#selection-in-the-time-domain Further changes# %%
import os
import mne
sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(sample_data_raw_file, verbose=False).crop(tmax=60)
events = mne.find_events(raw, stim_channel='STI 014')
|
Please do not do this. It does not reflect what was done to the data and is not part of the plan I keep pushing, see for example point (2) above. |
Ok I will rework this part. |
|
... in particular, doing |
That's actually already implemented here 😅 seems I got priorities wrong. Will rework things. |
|
If it makes sense to do it all as part of this PR it's fine by me. I don't know what's going to be easiest actually, as different parts of the code interact a lot. |
| if self.baseline != _check_baseline(self.baseline, tmin=self.tmin, | ||
| tmax=self.tmax, | ||
| sfreq=self.info['sfreq'], | ||
| on_baseline_outside_data='adjust'): | ||
| rescale_data = False |
There was a problem hiding this comment.
Relying on exact float equality is dangerous. One time when tmin and sfreq are double floating point you could get one result, and once a save-load round trip happens and they are single floating point you might get another. Is there some way instead to actually keep track of when we actually need to apply a baseline?
When I originally went to work on this at one point, I considered adding a private self._do_baseline = False that was True only in the one case when it actually needed to be applied later rather than right away, which I think is only for non-preloaded Epochs. This way you can either 1) check that not self._do_baseline if a crop operation is requested that would remove the baseline period, or (better but harder) 2) we would make the raw data getter get data from the interval min(bmin, tmin), max(bmax, tmax), then crop.
There was a problem hiding this comment.
... if it seems very difficult to refactor using some _do_baseline, another alternative would be to have a _baseline_isclose(a, b, sfreq) that checked that the terms were close to within 1e-2 / sfreq or so for example. This should be good enough to deal with floating point inaccuracies (time points should step by 1. / sfreq, so doing 1e-2 / sfreq allows for a lot of slop, which could be necessary for very long / hundreds-of-thousands-of-time-points epochs).
There was a problem hiding this comment.
When I originally went to work on this at one point, I considered adding a private
self._do_baseline = Falsethat wasTrueonly in the one case when it actually needed to be applied later rather than right away, which I think is only for non-preloaded Epochs.
I have implemented this now in eca094a. And I have to admit I'm starting to get (have gotten?) lost a bit regarding all the side-effects this open-heart surgery might have. Fortunately we've got excellent test coverage, but I'm quite concerned I might be introducing bugs here. I think we should also manually test before finally merging this…
This way you can either 1) check that
not self._do_baselineif acropoperation is requested that would remove the baseline period,
This is not necessary, as cropping only works on preloaded data, and loading applies baseline correction.
or (better but harder) 2) we would make the raw data getter get data from the interval
min(bmin, tmin), max(bmax, tmax), then crop.
I haven't looked into this, I don't think I want to have this kind of complexity in my life right now ;)
| def _check_baseline(baseline, tmin, tmax, sfreq, | ||
| on_baseline_outside_data='raise'): |
There was a problem hiding this comment.
Rather than do all the tmin/tmax/sfreq gymnastics, can we just pass self.times?
There was a problem hiding this comment.
We now expect times instead of tmin, tmax (ad21ff9); getting rid of sfreq is not as trivial: I tried simply replacing it with times[-1] - times[0], but this breaks a few tests. So I'm not touching that one for now…
Edit: I tried sfreq = times[1] - times[0] of course, not times[-1] - times[0]
There was a problem hiding this comment.
Haha or maybe I didn't. Just tried again and all tests do pass. Maybe I actually tried with times[-1] - times[0] initially, instead of times[1] - times[0]. At any rate, dropped the sfreq param now in a2ea0b5
There was a problem hiding this comment.
I've reverted the change (dropping sfreq) again, as it won't work with data that only has one time point (as used in test_pick_seeg_ecog())
| if self.baseline != _check_baseline( | ||
| self.baseline, times=self.times, sfreq=self.info['sfreq'], | ||
| on_baseline_outside_data='adjust'): | ||
| s += ' (baseline period was cropped after baseline correction)' |
There was a problem hiding this comment.
I need to rethink this one, as we still have a float comparison here
There was a problem hiding this comment.
No never mind, I think this is okay in this specific case… emphasis on I think
There was a problem hiding this comment.
_check_baseline allows for up to 1./sfreq seconds of slack, so this should be okay here.
larsoner
left a comment
There was a problem hiding this comment.
Changes look surprisingly readable!
I would say what is missing are some tests that the data are not changed. For this you could both add to the existing tests, but also add some new basic/separate test, for example have 1 channel of "EEG" data be a 2 sec linear ramp from -1 to 1 with a single event at 1 sec and epoch with Epochs(..., tmin=-0.2, tmax=0.2, baseline=(-0.1, 0.1)), check the values during baseline and epoch are allclose with what you'd expect, then crop to t>=0, assert the values are still the same and the .baseline is the same, do I/O round trip, etc.
Yes, this actually made me a bit suspicious ;) Will add more tests as you suggested! Thanks for the review! |
|
@hoechenberger it would be great to have this for 0.22 -- any chance you can add the tests quickly so we can have at least a week or two of this code in |
|
@larsoner I will work on this tomorrow if it's okay with @agramfort? Hopefully the tests will all pass and not reveal a major screwup ;) |
|
@larsoner I've added some tests as suggested |
larsoner
left a comment
There was a problem hiding this comment.
@agramfort feel free to review and merge if you're happy!
|
@cbrnr @drammock @sappelhoff @jasmainak |
agramfort
left a comment
There was a problem hiding this comment.
just a nitpick.
I did not review as the diff is hard but I tested locally with a bunch of scenario and it works great
|
I don't really have a pipeline for ERP processing because I mostly work with ERD/ERS. However, in one of our current studies we will actually analyze ERPs, but this will probably take too long to serve as a test for this PR. Therefore, I'll have to pass on this one. |
|
Thanks anyway, @cbrnr! |
|
@larsoner your call? merge?
|
|
Thanks @hoechenberger ! |
Epochsbaseline will now correctly inform the user that baseline correction has been switched off. Previously, this warning would only be issued if the entire baseline period was removedbaselinetuple containing one or twoNonevalues – e.g.(None, 0),(0, None), or(None, None)– is passed toEpochsorEpochs.apply_baseline, thoseNonevalue(s) will be replaced with the actual time (i.e.,Epochs.tminandEpochs.tmax, respectively) when populatingEpochs.baselineThis makes it possible (or at least easier) to later figure out whether we're cropping away parts of an existing baseline.This is work that's laying the ground for resolving #8216
Probably should also be backported?