Skip to content

RANSAC channel picks differ between MATLAB PREP and PyPREP using the same seed #61

Closed

Description

@a-hurst

When RANSAC does its random channel selection, the random channel choices diverge between MATLAB PREP and PyPREP, even when using the same seed. This isn't super-surprising since they're different languages with different randomization functions, but it would be handy if we could PyPREP to make the same picks as MatPREP given the same seed.

In my preliminary efforts looking into this, I've learned a few things:

  • Most importantly, Numpy offers an identical implementation of the "twister" randomization algorithm to MATLAB such that they should be able to produce identical random results given the same seed
  • MATLAB PREP implements its own custom randsample function for selecting subsets of channels without replacement. If I re-implement the same logic in Python I identical results.
  • MATLAB PREP also has a getRandomSubsets function that calls the above randsample function and produces different numbers yet again. Haven't tried re-implementing it in Python yet, but hopefully it's trivial!

With some luck I'll have a PR for this shortly. It's going to break seed compatibility with earlier PyPREP versions, but I think we've already done that since last release in #43.

Activity

  1. sappelhoff commented on Apr 9, 2021

    @sappelhoff
    Owner

    With some luck I'll have a PR for this shortly. It's going to break seed compatibility with earlier PyPREP versions, but I think we've already done that since last release in #43.

    yes, maintaining backwards compatibility (also with seeds) is no issue we need to burden ourselves with at this point.

  2. a-hurst commented on Apr 9, 2021

    @a-hurst
    CollaboratorAuthor

    @sappelhoff I've got this implemented and working now (just waiting on the correlation PR to be merged before I make a new one), but in the process I've noticed something interesting that might explain some RANSAC variability:

    In MATLAB PREP, the number of channels to use for RANSAC predictions is 25% of the total number of EEG channels in the data (e.g. 16 for a 64-channel dataset). In PyPREP, it's 25% of the number of good channels (e.g. 15 for a 64-channel dataset where 4 channels were bad by correlation). Both implementations ignore bad channels when making selections, but MATLAB PREP is going to generally be making its predictions based on more channels (and thus is likely producing better estimates). The effect of this would be compounded with each iteration of NoisyChannels during robust re-referencing, as PyPREP makes its RANSAC predictions with fewer and fewer channels each round (assuming new bad channels are detected each time).

    My question is: is there a reason to prefer PyPREP's approach over MatPREP's here, or should I change it to match MatPREP's behaviour when I make my PR for this?

  3. yjmantilla commented on Apr 10, 2021

    @yjmantilla
    Collaborator

    Dude you are on fire!

    PD:
    Lets wait for @sappelhoff , maybe he had a reason for keeping it that way. I would argue it would be better to have the MATLAB version. If there happens to be too many bad channels then just throw an exception (or whatever matlab does in this case).

  4. sappelhoff commented on Apr 10, 2021

    @sappelhoff
    Owner

    very interesting 🤔 thanks for digging this up. I am pretty sure that when I coded ransac in 2018, I tried to stay as close as possible to the publication: https://www.frontiersin.org/articles/10.3389/fninf.2015.00016/full

    it's entirely possible that either:

    • I misread something in the paper ... or
    • the matlab PREP does not follow what it says in the paper

    If paper and matlab prep both agree, then let's change pyprep.

    if paper and pyprep agree, then let's first raise an issue on matlab prep to discuss.

  5. sappelhoff commented on Apr 10, 2021

    @sappelhoff
    Owner

    PS: it may also be worth looking into good old https://github.com/autoreject/autoreject again, how Mainak did it back then.

  6. a-hurst commented on Apr 10, 2021

    @a-hurst
    CollaboratorAuthor

    @sappelhoff Here's what the PREP publication says about it, it's sort of ambiguous:

    Before applying RANSAC, we remove channels designated as noisy by other methods. We then select a random subset of predictor channels for each channel (25% by default). If not enough channels remain to form the required subsets, the algorithm terminates.

    However, the AutoReject code seems to match MATLAB PREP here, where self.picks defaults to all EEG and MEG channels:

    picked_info = mne.io.pick.pick_info(info, self.picks)
    n_channels = len(picked_info['ch_names'])
    
    # number of channels to interpolate from
    n_samples = int(np.round(self.min_channels * n_channels))

    What do you think? Should I add this to the next PR?

  7. sappelhoff commented on Apr 10, 2021

    @sappelhoff
    Owner

    What do you think? Should I add this to the next PR?

    okay, let's do it that way then (like autoreject and matprep) :-)

    Strictly speaking, it should not make too much of a difference.

  8. added this to the 0.3.2 milestone on Apr 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions