You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Extremely slow access to epochs after channel remapping. #7947
I have two epoch lists. In one of the lists all channels have to be mirrored to the other side of the hemisphere (flipped). After that I want to concatenate both lists to one epoch file. So I'm using mne.rename_channels() to remap the channels of the one list. After that I'm using mne.equalize_channels() to get a uniform channel mapping of the two lists. After that I'm using mne.concatenate_epochs() to concat the lists. It works perfectly fine but if I try to access a condition after the concatenation in the resulting epoch instance, things become really slow. Before the access is really quick like instant. After it takes up to 20s. How could I resolve that I tried a lot already. Thank you very much!
Unfortunately, I can't replicate the issue with sample data. The reason is probably that in my data I have around 400 Event IDs whereas here one has only 4. I'm flipping 200 out of these 400 IDs. Here I'm only flipping 2 out of 4. After concatenation of the epoch list with the 400 IDs of which 200 have flipped channels, the issue arises when I try to create evokeds out of them.
The reason I'm flipping the channels is that it is a tactile paradigm and I want to pool right stimuli with left stimuli by flipping them.
In [6]: len(Epochs_remapped)
Out[6]: 6271
In [7]: len(epochs)
Out[7]: 6271
In [8]: %timeit epochs['side_left'].average()
754 ms ± 67.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [9]: %timeit Epochs_remapped['side_left'].average()
3.05 s ± 174 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [12]: len(epochs['side_left'])
Out[12]: 3193
In [13]: len(Epochs_remapped['side_left'])
Out[13]: 3193
Done. It seems the issue has something to do with appending the epochs and then using mne.concatenate_epochs(). Is there any way to mirror/flip the channels in place (meaning in the original epoch instance)? Then I wouldn't need concatenate_epochs(). I tried rename_channels() on the original epoch instance and reorder_channels(). After that I tried equalize_channels() in both cases. Both times it didn't work. The channels stay the same:
from copy import deepcopy
import datetime
import mne
data_path = mne.datasets.sample.data_path()
fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
raw = mne.io.read_raw_fif(fname).pick_types(meg=False, eeg=True).load_data()
events = mne.make_fixed_length_events(raw, duration=0.5)
epochs = mne.Epochs(raw, events, preload=True)
epochs_list = [epochs[ii] for ii in range(len(epochs))]
epochs_concat = mne.concatenate_epochs(epochs_list)
assert len(epochs_concat) == len(epochs)
#untouched epochs
a = datetime.datetime.now()
evoked_l = epochs['1']
b = datetime.datetime.now()
print(f'Time untouched epochs: {b-a}')
#concatenated epochs
a = datetime.datetime.now()
evoked_remap_l = epochs_concat['1']
b = datetime.datetime.now()
print(f'Time concatenated epochs: {b-a}')
a = datetime.datetime.now()
deepcopy(epochs_concat.drop_log)
b = datetime.datetime.now()
print(f'Time deepcopy drop_log: {b-a}')
The output is:
Time untouched epochs: 0:00:00.046417
Time concatenated epochs: 0:00:00.637247
Time deepcopy drop_log: 0:00:00.632559
i.e., the problem is deepcopy on a list of list of str that has lots of elements.
One fix would be to make it a tuple of tuple of str, which is immutable. I actually like this change because epochs.drop_log is not meant to be modified directly by users anyway.
(and it will speed up all epochs.copy operations, not just the one associated with epochs[...] -> epochs.__getitem__ -> epochs._getitem(..., copy=True) which is the cause of the slowdown here)
@agramfort@larsoner Can I fix this by myself anyhow (with tuples?) or do I have to wait for an update? Unfortunately, due to this issue my analysis came to a stop. Thank you!
You can try doing epochs.drop_log = [[]] * len(epochs) before subselection to see if it helps. If it doesn't, then you can do epochs.drop_log = []. Neither are actually correct, but they might allow you to continue your analysis until we fix the slowness.
Activity
agramfort commentedon Jun 30, 2020
KSuljic commentedon Jun 30, 2020
Got it, I will post it tomorrow. Thanks for the reply.
KSuljic commentedon Jul 1, 2020
Unfortunately, I can't replicate the issue with sample data. The reason is probably that in my data I have around 400 Event IDs whereas here one has only 4. I'm flipping 200 out of these 400 IDs. Here I'm only flipping 2 out of 4. After concatenation of the epoch list with the 400 IDs of which 200 have flipped channels, the issue arises when I try to create evokeds out of them.
The reason I'm flipping the channels is that it is a tactile paradigm and I want to pool right stimuli with left stimuli by flipping them.
The procedure here is the same as in my code:
MNE version: 0.20.7
agramfort commentedon Jul 1, 2020
@KSuljic you can make fake events using sample data to replicate the issue
KSuljic commentedon Jul 1, 2020
Thank you for the hint. I could replicate the issue to some extend but in my code the difference is stronger:
Time to evoke unflipped epochs: 0:00:00.056981
Time to evoke flipped epochs: 0:00:12.620404
It seems not much but in my analysis it makes out of 0.8s -> 3.5h.
Thank you for your aid.
agramfort commentedon Jul 2, 2020
I confirm the weirdness:
@larsoner any hint?
KSuljic commentedon Jul 2, 2020
The issue is also reproducible without mne.rename_channels(), only by appending the epochs to a list and concatinating with mne.concatenate_epochs().
drammock commentedon Jul 2, 2020
In that case, can you edit your post above to simplify the code? The more minimal your example is, the easier it is for us to track down the problem.
KSuljic commentedon Jul 2, 2020
Done. It seems the issue has something to do with appending the epochs and then using mne.concatenate_epochs(). Is there any way to mirror/flip the channels in place (meaning in the original epoch instance)? Then I wouldn't need concatenate_epochs(). I tried rename_channels() on the original epoch instance and reorder_channels(). After that I tried equalize_channels() in both cases. Both times it didn't work. The channels stay the same:
or
EDIT: I just realized that both functions don't operate in place.
larsoner commentedon Jul 2, 2020
Here is an example that can be copy-pasted:
Details
The output is:
i.e., the problem is
deepcopyon a list of list of str that has lots of elements.One fix would be to make it a tuple of tuple of str, which is immutable. I actually like this change because
epochs.drop_logis not meant to be modified directly by users anyway.larsoner commentedon Jul 2, 2020
(and it will speed up all
epochs.copyoperations, not just the one associated withepochs[...] -> epochs.__getitem__ -> epochs._getitem(..., copy=True)which is the cause of the slowdown here)agramfort commentedon Jul 3, 2020
KSuljic commentedon Jul 3, 2020
Thank you very much for looking into it.
Is there any direct fix I could apply now?
KSuljic commentedon Jul 6, 2020
@agramfort @larsoner Can I fix this by myself anyhow (with tuples?) or do I have to wait for an update? Unfortunately, due to this issue my analysis came to a stop. Thank you!
larsoner commentedon Jul 6, 2020
You can try doing
epochs.drop_log = [[]] * len(epochs)before subselection to see if it helps. If it doesn't, then you can doepochs.drop_log = []. Neither are actually correct, but they might allow you to continue your analysis until we fix the slowness.KSuljic commentedon Jul 15, 2020
Unfortunately, both didn't work. If I try to subselect it states:
IndexError: list assignment index out of range
KSuljic commentedon Jul 15, 2020
I'll try to install the dev version in another conda env. It seems you fixed it there already.
KSuljic commentedon Jul 15, 2020
In the dev version it works perfectly (roughly only double the time as you mentioned). Thank you very much!