Skip to content

set_bipolar_reference is too slow #8718

Closed

Description

@jasmainak

If you're doing bipolar reference on sEEG data with 180 contacts, then it is very slow. Here is an alternative script that does the same in a fraction of second:

import numpy as np

import mne
from mne.datasets import sample

data_path = sample.data_path()

fname = data_path + '/MEG/sample/sample_audvis-ave.fif'

n_picks = 30  # change to bigger number to see difference in speed

# Reading evoked data
condition = 'Left Auditory'
evoked = mne.read_evokeds(fname, condition=condition, baseline=(None, 0),
                          proj=False)

del evoked.info['projs'][0]
evoked.pick_types(meg=False, eeg=True)
ch_names = evoked.info['ch_names']
evoked.pick_channels(ch_names[:n_picks])
ch_names = ch_names[:n_picks]

anode, cathode = ch_names[1:], ch_names[:-1]

n_channels, n_times = evoked.data.shape
n_virtual_channels = len(anode)
virtual_ch_names = [f'{a}-{c}' for (a, c) in zip(anode, cathode)]
multiplier = np.zeros((n_virtual_channels, n_channels))
for idx, (a, c) in enumerate(zip(anode, cathode)):
    multiplier[idx, ch_names.index(a)] = 1
    multiplier[idx, ch_names.index(c)] = -1

data_rereferenced = multiplier.dot(evoked.data)
evoked_rerefenced = mne.set_bipolar_reference(evoked, anode=anode, cathode=cathode)
picks = [evoked_rerefenced.ch_names.index(ch) for ch in virtual_ch_names]

data_rereferenced_master = evoked_rerefenced.data[picks]
assert np.allclose(data_rereferenced, data_rereferenced_master)

Activity

  1. jasmainak commented on Jan 8, 2021

    @jasmainak
    MemberAuthor

    Some other issues:

    • it seems like this function removes the channel locations. I'm not sure why because it means you can't do any processing downstream. I think the new channel locations should be the channel locations of the cathodes
    • the docstring has conflicting reports of what is done. First it says "anode minus the cathode", then it says "the 1st anode will be subtracted from the first cathode" which is the opposite
  2. marsipu commented on Mar 18, 2021

    @marsipu
    Member

    I want to give it a shot (I am still on it, but I haven't found the time yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions