Skip to content

Dropout channel detection broken in PyPREP #80

Closed

Description

@a-hurst

Working on #79, I modified matlab_artifacts to simulate a "dropout" channel (I set the second 25% and last 25% of the channel to 0). This channel is successfully flagged as a bad-by-dropout by MatPREP, but in PyPREP the addition of this channel breaks NoisyChannels completely, seriously messing up the correlation windows and leaving too few usable channels to use for RANSAC.

I've been looking into it for a bit and it seems to be due to differences in how NaNs are generated during correlation between Numpy and MATLAB, but haven't figured out the exact source yet.

Activity

  1. a-hurst commented on May 5, 2021

    @a-hurst
    CollaboratorAuthor

    Okay, so at least some of the issue is due to difference in NaN handling between Numpy's quantile functions and MATLAB's. In MATLAB, any NaNs encountered will be ignored and removed before calculation, whereas np,quantile just returns all NaNs if any are encountered. np.nanquantile does what we want it to, but then we run into problems with another Numpy/MATLAB difference. Recall that we added a custom _mat_quantile function to adjust the quantiles based on the size of the input sample like MATLAB does:

    pyprep/pyprep/utils.py

    Lines 77 to 80 in ffa6346

    q = np.asarray(q, dtype=np.float64)
    n = len(arr)
    q_adj = ((q - 0.5) * n / (n - 1)) + 0.5
    return np.quantile(arr, np.clip(q_adj, 0, 1), axis=axis)

    Because of how MATLAB handles NaNs, however, we also need to adjust the calculation ofn here to decrease by 1 for each NaN encountered in each sample (i.e. for each completely-flat channel during the window). Maybe it's just been a long day, but I can't for the life of me figure out an elegant solution for handling this. Any ideas @sappelhoff?

  2. sappelhoff commented on May 6, 2021

    @sappelhoff
    Owner

    I guess a first thing would be to adjust _mat_quantile:

    +   arr = arr[~np.isnan(arr)]
        q = np.asarray(q, dtype=np.float64)
        n = len(arr)
        q_adj = ((q - 0.5) * n / (n - 1)) + 0.5
        return np.quantile(arr, np.clip(q_adj, 0, 1), axis=axis)

    right?

    I don't really see the other problem that you are talking about 🤔 Could you explain it once more?

  3. a-hurst commented on May 6, 2021

    @a-hurst
    CollaboratorAuthor

    The issue is that we're giving the function a 2-D matrix of correlation values and taking quantiles along the axis (i.e. one quantile per channel) so removing NaNs beforehand doesn't work (the change you suggested turns arr into a flat 1-D array). Anyway, I figured out a solution I'm reasonably happy with in the latest PR, now dropouts are getting handled smoothly!

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