mne-icalabel icon indicating copy to clipboard operation
mne-icalabel copied to clipboard

[ENH] Add PSD feature via MNE

Open anandsaini024 opened this issue 2 years ago • 7 comments

PSD feature creation script.

anandsaini024 avatar Jul 19 '22 15:07 anandsaini024

Just to clarify, how are these and the topographic PR different from what is included in ICLabel?

I might've missed this discussion.

adam2392 avatar Jul 19 '22 15:07 adam2392

Codecov Report

Merging #73 (07888b3) into main (33856df) will increase coverage by 0.08%. The diff coverage is n/a.

:exclamation: Current head 07888b3 differs from pull request most recent head e1f1d6a. Consider uploading reports for the commit e1f1d6a to get more accurate results

@@            Coverage Diff             @@
##             main      #73      +/-   ##
==========================================
+ Coverage   86.76%   86.84%   +0.08%     
==========================================
  Files          15       16       +1     
  Lines         831      859      +28     
  Branches      101      105       +4     
==========================================
+ Hits          721      746      +25     
- Misses         82       83       +1     
- Partials       28       30       +2     
Impacted Files Coverage Δ
main/mne_icalabel/utils/_docs.py 93.10% <0.00%> (ø)
main/mne_icalabel/features/psd.py 89.28% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov[bot] avatar Jul 19 '22 15:07 codecov[bot]

@adam2392 The ICLabel ones are a copy/paste of the MATLAB features.. which are not super clean and nice. For instance, the ICLabel rpsd feature is supposed to have a random component right? Well.. in practice that's not the case because it does something along the lines of:

some_2d_matrix = [..., ...]
subsets = [indices of all columns]  # instead of an actual subset
select_and_shuffle = shuffle(some_2d_matrix [:, subsets])  # shuffle the columns
average = avg(select_and_shuffle, axis=1)  # avg across columns

And that's not the only example of 'weird' coding choices/behaviors. I added comment in the features.py file describing those items when I ran into them, e.g. https://github.com/mne-tools/mne-icalabel/blob/26a2f1fb2829d7f3ee7019c6591775be33ecde98/mne_icalabel/iclabel/features.py#L375-L378

which corresponds to the pct_data variable in this file https://github.com/sccn/ICLabel/blob/d0cfb0f8638cd4518ee2e66a3bff0bb32ff40969/eeg_autocorr_welch.m#L1-L25

variable which is never provided to the function https://github.com/sccn/ICLabel/blob/d0cfb0f8638cd4518ee2e66a3bff0bb32ff40969/ICL_feature_extractor.m#L77

Or this one: https://github.com/mne-tools/mne-icalabel/blob/26a2f1fb2829d7f3ee7019c6591775be33ecde98/mne_icalabel/iclabel/features.py#L426-L429

which corresponds to https://github.com/sccn/ICLabel/blob/d0cfb0f8638cd4518ee2e66a3bff0bb32ff40969/eeg_autocorr_welch.m#L36-L44


Overall, I'm not super confident in the ICLabel features, so I asked Anand to prepare functions to extract features from Instance/ICA decomposition using MNE, starting with the topographic map and the PSD based on the ica.plot_properties method. The idea is to use those functions to extract features for future models.

mscheltienne avatar Jul 19 '22 15:07 mscheltienne

FYI to fix the style easily and the sorting of imports:

black ./mne_icalabel/* and isort ./mne_icalabel commands, or type make black and make isort. The other checks like flake8 (style), pydocstyle, and mypy can be fixed manually.

adam2392 avatar Jul 21 '22 19:07 adam2392

@anandsaini024 Please look into additional tests as we discussed:

  • test inspired from tests of the PSD functions in MNE
  • test on signals with known frequency content, e.g. sinusoid, amplitude modulated signal, ...
  • test with different inputs for the arguments of get_psds

I'll try to double-check this PR this week.

mscheltienne avatar Jul 26 '22 18:07 mscheltienne

Hi, I popped in to see whats new in mne-icalabel (great project!) and saw your discussion of pct_data variable and how it is not used in the Matlab code. I think (but I may be wrong) that it is related to how EEGLAB usually computed PSD - to save time the spectrum was computed only for a portion of the data (especially important for very long raw recordings and the fact to you expect instant feedback when working via GUI). This option was controlled with a special parameter that in research usage would be set to 100. I think this may be the reason for the behavior of pct_data you see.

mmagnuski avatar Nov 01 '22 22:11 mmagnuski

Maybe, I didn't dig in too far, but it looked to me like the feature extraction code I translated was half-baked. And I think the unused pct_data variable appears both in the PSD and autocorrelation features.

Also, I'll finalize this PR at some point ;)

mscheltienne avatar Nov 02 '22 08:11 mscheltienne

This is now too out of date, especially with the added Spectrum objects. Closing.

mscheltienne avatar Jun 18 '24 08:06 mscheltienne