Skip to content

Bad channels by correlation differ between PyPREP and MATLAB PREP #56

Closed

Description

@a-hurst

In my recent comparison efforts, I noticed that the numbers of initial bad channels by correlation sometimes differed slightly between PyPREP and MATLAB PREP. Since this code doesn't involve any random number generation, it should always be consistent between runs and also between implementations.

Anyway, I did some digging with breakpoints and think I've figured it out. It has to do with these lines here:

        for k in range(0, w_correlation):
            eeg_portion = np.transpose(np.squeeze(EEG_new_win[:, :, k]))
            data_portion = np.transpose(np.squeeze(data_win[:, :, k]))
            window_correlation = np.corrcoef(np.transpose(eeg_portion))
            abs_corr = np.abs(
                np.subtract(window_correlation, np.diag(np.diag(window_correlation)))
            )
            channel_correlation[k, :] = np.quantile(abs_corr, 0.98, axis=0)
            noiselevels[k, :] = np.divide(
                robust.mad(np.subtract(data_portion, eeg_portion), c=1),
                robust.mad(eeg_portion, c=1),
            )
            channel_deviations[k, :] = 0.7413 * iqr(data_portion, axis=0)

Using the same input data, we see that eeg_portion, data_portion, window_correlation, abs_corr, and noiselevels are all identical between PyPREP and MatPREP, but channel_correlation and channel_deviations both differ.

The reason for this is that apparently MATLAB's quantile functions work slightly differently than Numpy's and SciPy's: instead of having quantiles be restricted to the range [0, 1], they get restricted to the range [0.5 / n, (n - 0.5) / n], with n being the number of samples passed to the quantile function. This is apparently equivalent to treating the provided values as a sample of an unknown population rather than a full population themselves. After hacking together some new quantile and iqr functions that account for this, the channel_correlation and channel_deviations data both match the MATLAB values!

Anyway, I'll put together a PR to address this, but I figure there should be a discussion of which quantile logic actually makes the most sense for this use-case. If Numpy's default logic is actually more reasonable/justifiable here, we can make the new quantile functions optional with a match_matlab flag or parameter of some kind indicating strict adherence to the original PREP logic. If MATLAB's logic makes more sense, I'm assuming we should just fully replace np.quantile and iqr with their modified equivalents?

Activity

  1. yjmantilla commented on Apr 3, 2021

    @yjmantilla
    Collaborator

    Wow, you are really digging inside. Looking forward to this.

  2. a-hurst commented on Apr 3, 2021

    @a-hurst
    CollaboratorAuthor

    Heh, well I figure it's probably a sunk-cost thing at this point: I've dedicated enough hours of my life to trying to get to the bottom of this that I feel like I need to see this through!

    Now that I have a rough understanding of how MATLAB works and how to run PREP with it (thanks again for your example script btw) I can finally do all the low level step-by-step comparisons I've always wanted to, so hopefully reaching result-parity with MATLAB PREP will happen a lot quicker now.

  3. sappelhoff commented on Apr 4, 2021

    @sappelhoff
    Owner

    awesome! 🎉 PR very welcome!

    Anyway, I'll put together a PR to address this, but I figure there should be a discussion of which quantile logic actually makes the most sense for this use-case

    yes, such a discussion would be valuable. For now I suggest in your PR you adjust pyprep to be in line with matprep, and we document this interesting "quantile" detail in the changelog and/or an inline code comment to return to it sometime in the future.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions