nbara / python-meegkit

🔧🧠 MEEGkit: MEG & EEG processing toolkit in Python
https://nbara.github.io/python-meegkit/
BSD 3-Clause "New" or "Revised" License
182 stars 49 forks source link

ASR transform fails (unexpected zi shape) #10

Closed Hororohoruru closed 3 years ago

Hororohoruru commented 4 years ago

Hello! I am trying to apply ASR to some EEG data recorded at our lab with EEGLAB. We are using MNE for the analyses and we wanted to incorporate ASR as a preprocessing step. I was currently running the asr example code:

# Get data array for ASR
raw_array = raw.get_data()

# Train ASR
asr = ASR(method='euclid')
train_idx = np.arange(0 * sfreq, 30 * sfreq, dtype=int) # 30s period
_, sample_mask = asr.fit(raw_array[:, train_idx])

# Apply a sliding window to the data
X = sliding_window(raw_array, window=int(sfreq), step=int(sfreq))
Y = np.zeros_like(X)
for i in range(X.shape[1]):
    Y[:, i, :] = asr.transform(X[:, i, :])

And I got the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-63-b37fc0c9f43f> in <module>
     11 Y = np.zeros_like(X)
     12 for i in range(X.shape[1]):
---> 13     Y[:, i, :] = asr.transform(X[:, i, :])

~/gitrepos/python-meegkit/meegkit/asr.py in transform(self, X, y, **kwargs)
    221         # Yulewalk-filtered data (optional).
    222         X_filt, self.zi_ = yulewalk_filter(
--> 223             X, sfreq=self.sfreq, ab=self.ab_, zi=self.zi_)
    224 
    225         if not self._fitted:

~/gitrepos/python-meegkit/meegkit/utils/asr.py in yulewalk_filter(X, sfreq, zi, ab, axis)
    317     if zi is None:
    318         zi = signal.lfilter_zi(B, A)
--> 319         out, zf = signal.lfilter(B, A, X, zi=zi[:, None] * X[:, 0], axis=axis)
    320     else:
    321         out, zf = signal.lfilter(B, A, X, zi=zi, axis=axis)

~/anaconda3/envs/mne/lib/python3.7/site-packages/scipy/signal/signaltools.py in lfilter(b, a, x, axis, zi)
   1907             return sigtools._linear_filter(b, a, x, axis)
   1908         else:
-> 1909             return sigtools._linear_filter(b, a, x, axis, zi)
   1910 
   1911 

ValueError: Unexpected shape for zi:  expected (32,8), found (8,32).

My understanding of the analysis is not very deep, but after looking around in scipy documentation and the repo's code I found that this 'zi' parameter is calculated automatically before being passed to the lfilter function. Being so, I am a bit confused by the fact that it has unexpected shape. Any ideas? Thanks!

nbara commented 3 years ago

Hi @Hororohoruru thanks for reporting this. Somehow I completely missed this issue. I will look into it this week.

nbara commented 3 years ago

Would you be willing to share a .fif file with your raw data (or about a minute of continuous recording or so) ? You don't have to tell me what it is or anything, it would just make debugging much easier.

nbara commented 3 years ago

Nevermind, I think I have a fix in #15. Can you please try to checkout nb/fix-issue-10 and report back if it solves your issue?

Hororohoruru commented 3 years ago

Hello! Sorry for the delay. I can confirm that the fix indeed solves the issue. We ended up doing the same locally, but we weren't sure whether that was the intended action or whether it could change the results. Thank you very much!

nbara commented 3 years ago

Glad to hear this ! Sorry for taking more than a month to reply (I usually get notifications for issues but this one slipped by)