ENH: Support concatenate_epochs() for EpochsTFR#13745
Conversation
| shift = np.int64((10 + ref.times[-1]) * ref.info["sfreq"]) | ||
| events_offset = int(np.max(epochs_list[0].events[:, 0])) + shift |
There was a problem hiding this comment.
| shift = np.int64((10 + ref.times[-1]) * ref.info["sfreq"]) | |
| events_offset = int(np.max(epochs_list[0].events[:, 0])) + shift | |
| shift = len(ref.times) | |
| events_offset = ref.events[-1, 0] + shift |
nordme
left a comment
There was a problem hiding this comment.
Hi @aman-coder03 ! Thanks for the PR.
I took a look at the concatenation function and tests, and it's looking pretty good so nice work. I spotted a few more EpochsTFR attributes that need handling.
Attributes to check:
ep.baseline
Even if the times arrays are equivalent across epochs instances, some instances could have baselines where others have none, and that's a problem. For concatenation, baselines should be the same across instances.ep.method
We should check that the method of construction is the same for all our epochs instances (e.g. not mixing multitaper and morlet wavelet TFR results for example).ep.weights
This attribute stores weights for tapers. If applicable, these need to be equivalent across epochs instances.
Attributes that need concatenation:
ep.comment
This attribute typically stores trial descriptions of some sort and might not be the same across epochs instances (e.g. 'run 1', 'run2', 'run 3'). These need to be put together for the returned epochs instance.
Cheers.
|
thanks for the reviews... shift = len(ref.times)
events_offset = ref.events[-1, 0] + shiftaddressed all three attribute checks @nordme
regarding |
|
@aman-coder03 I think you're right we can ignore the comment attribute since the |
|
|
|
merging main should clear up the CI failures (fixed in #13832) |
|
@drammock yes the errors are fixed now! |
drammock
left a comment
There was a problem hiding this comment.
for some reason GitHub thinks I don't have access to this repo 🙄 so this is a "comment" review, but FWIW I am REQUESTING CHANGES.
I think there's too much duplicated code between _concatenate_epochs and _concatenate_epochs_tfr. Prefer refactoring _concatenate_epochs to triage function behavior based on type of the first element in the concatenation list.
| @@ -0,0 +1 @@ | |||
| Add support for :func:`mne.concatenate_epochs` with :class:`~mne.time_frequency.EpochsTFR` instances, by ``aman-coder03``. (:gh:`13745`) No newline at end of file | |||
There was a problem hiding this comment.
| Add support for :func:`mne.concatenate_epochs` with :class:`~mne.time_frequency.EpochsTFR` instances, by ``aman-coder03``. (:gh:`13745`) | |
| Add support for :func:`mne.concatenate_epochs` with :class:`~mne.time_frequency.EpochsTFR` instances, by ``aman-coder03``. |
| f"epochs_list[{ii}] method {ep.method!r} does not match " | ||
| f"epochs_list[0] method {ref.method!r}" | ||
| ) | ||
| if not np.array_equal(ep.weights, ref.weights): |
There was a problem hiding this comment.
| if not np.array_equal(ep.weights, ref.weights): | |
| if not np.array_equal(ep.weights, ref.weights): # NB: array_equal(None, None) == True |
| evs = ep.events.copy() | ||
| if add_offset: | ||
| evs[:, 0] += events_offset | ||
| events_offset += int(np.max(ep.events[:, 0])) + shift |
There was a problem hiding this comment.
| events_offset += int(np.max(ep.events[:, 0])) + shift | |
| events_offset += ep.events[-1, 0] + shift |
| event_id = deepcopy(ref.event_id) | ||
| for ep in epochs_list[1:]: | ||
| event_id.update(ep.event_id) |
There was a problem hiding this comment.
not safe. See corresponding code in existing _concatenate_epochs func:
Lines 4620 to 4629 in 692ecce
Reference issue (if any)
towards #13191
What does this implement/fix?
concatenate_epochs()now works withEpochsTFRobjects. Before this, passing a list ofEpochsTFRinstances would just crash with aTypeErrorbecauseEpochsTFRisn't a subclass ofBaseEpochsfix adds a helper function
_concatenate_epochs_tfr()inmne/epochs.pythat takes care of stacking the data, merging event IDs, offsetting event timestamps, and handling metadata, basically the same things the existing concatenation does for regular epochsconcatenate_epochs()now checks if it's dealing withEpochsTFRobjects and routes to this helper automaticallytests are added in
mne/time_frequency/tests/test_tfr.pyAdditional information
task 1 of 3 from #13191 (as discussed with @drammock)
tasks 2 and 3(
EpochsSpectrumsupport and allowingEpochsTFRto acceptRawTFRas input) will come in follow up PRs