Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add MEGnet to make MNE-ICALabel work on MEG data #207

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ec28e4f
add megnet
colehank Oct 18, 2024
6f272b1
add megnet
colehank Oct 18, 2024
b3433c8
double check
colehank Oct 18, 2024
34c2f31
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2024
5b7dc9c
bug fix
colehank Oct 18, 2024
96ed02d
Merge branch 'megnet' of https://github.com/colehank/mne-icalabel int…
colehank Oct 18, 2024
989cb40
topomaps plot modify & bug fix
colehank Oct 23, 2024
bc64aa2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2024
8af24b1
bug fix
colehank Oct 24, 2024
8f5e0e6
bug fix
colehank Oct 24, 2024
5aabe3f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2024
bd7f8cc
bug fix
colehank Oct 24, 2024
59aedfb
bug fix
colehank Oct 24, 2024
067849c
Merge branch 'main' into megnet
colehank Oct 25, 2024
a0da5ee
bug fix
colehank Oct 25, 2024
143df13
:q!Merge branch 'megnet' of https://github.com/colehank/mne-icalabel …
colehank Oct 25, 2024
58a719a
more validation of raw obejct
colehank Oct 29, 2024
b89c864
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 29, 2024
8465017
bug fix
colehank Oct 29, 2024
a0e526d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 29, 2024
40b1074
bug fix
colehank Oct 29, 2024
49f39d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 29, 2024
4f2d43a
fix model path discovery and include assets in package
mscheltienne Nov 7, 2024
bbce3cc
improve docstrings
mscheltienne Nov 7, 2024
19e0260
simplify and test validation of line noise
mscheltienne Nov 7, 2024
c47d582
clean-up utils
mscheltienne Nov 7, 2024
686fda1
test scripts update
colehank Nov 14, 2024
c5897e1
Merge branch 'main' into megnet
colehank Nov 14, 2024
88d0619
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 14, 2024
7d0a7e2
Merge branch 'main' into megnet
colehank Nov 14, 2024
25824fb
bug fix
colehank Nov 14, 2024
036599b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 14, 2024
30cc94a
bug fix
colehank Nov 14, 2024
7922f2f
test file fixing
colehank Dec 27, 2024
ce6e82c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 27, 2024
4c672b2
add test fuc's doctring
colehank Dec 27, 2024
401740d
Merge branch 'main' into megnet
colehank Dec 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test file fixing
  • Loading branch information
colehank committed Dec 27, 2024
commit 7922f2fdbd6ef746af7deee994cb082bdd2fa922
Empty file added mne_icalabel/megnet/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion mne_icalabel/megnet/features.py
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ def get_megnet_features(raw: BaseRaw, ica: ICA):

Parameters
----------
raw : Raw.
raw : Raw
Raw MEG recording used to fit the ICA decomposition.
The raw instance should be bandpass filtered between
1 and 100 Hz and notch filtered at 50 or 60 Hz to
35 changes: 35 additions & 0 deletions mne_icalabel/megnet/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#%%
import numpy as np
import mne
from mne_icalabel.megnet.label_components import megnet_label_components


def raw_ica():
"""Create a Raw instance and ICA instance for testing."""
sample_dir = mne.datasets.sample.data_path()
sample_fname = sample_dir / "MEG" / "sample" / "sample_audvis_raw.fif"

raw = mne.io.read_raw_fif(sample_fname).pick("mag")
raw.load_data()
raw.resample(250)
raw.notch_filter(60)
raw.filter(1, 100)

ica = mne.preprocessing.ICA(n_components=20, method="infomax", random_state=88)
ica.fit(raw)

return raw, ica


def test_megnet_label_components(raw_ica):
"""Test whether the function returns the correct artifact index."""
real_atrifact_idx = [0, 3, 5] # heart beat, eye movement, heart beat
prob = megnet_label_components(*raw_ica)
this_atrifact_idx = [int(idx) for idx in np.nonzero(prob.argmax(axis=1))[0]]
assert set(real_atrifact_idx) == set(this_atrifact_idx)
print (f"this_atrifact_idx: {this_atrifact_idx}")

#%%
raw_ica = raw_ica()
test_megnet_label_components(raw_ica)
#%%
29 changes: 26 additions & 3 deletions mne_icalabel/megnet/tests/test_label_components.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import MagicMock

import mne
import numpy as np
from numpy.testing import assert_allclose
import onnxruntime as ort
import pytest

@@ -34,8 +34,8 @@ def test_megnet_label_components(raw_ica):
"""Test whether the function returns the correct artifact index."""
real_atrifact_idx = [0, 3, 5] # heart beat, eye movement, heart beat
prob = megnet_label_components(*raw_ica)
this_atrifact_idx = list(np.nonzero(prob.argmax(axis=1))[0])
assert this_atrifact_idx == real_atrifact_idx
this_atrifact_idx = [int(idx) for idx in np.nonzero(prob.argmax(axis=1))[0]]
assert set(real_atrifact_idx) == set(this_atrifact_idx)


def test_get_chunk_start():
@@ -65,3 +65,26 @@ def test_chunk_predicting():

assert predictions.shape == (5, 4)
assert isinstance(predictions, np.ndarray)


def test_ica(raw_ica):
raw1, ica1 = raw_ica
raw2 = raw1.copy()
ica = mne.preprocessing.ICA(n_components=20, method="infomax", random_state=88)
ica2 = ica.fit(raw2)
assert_allclose(raw1.get_data(), raw2.get_data(), atol=1e-6, err_msg="Raw data should be the same!")

assert_allclose(ica1.mixing_matrix_, ica2.mixing_matrix_, atol=1e-6, err_msg="ICA mixing matrices should be the same!")
assert_allclose(ica1.unmixing_matrix_, ica2.unmixing_matrix_, atol=1e-6, err_msg="ICA unmixing matrices should be the same!")

ica1_data = ica1.get_sources(raw1).get_data()
ica2_data = ica2.get_sources(raw2).get_data()
assert_allclose(ica1_data, ica2_data, atol=1e-6, err_msg="ICA transformed data should be the same!")


def test_megnet(raw_ica):
raw, ica = raw_ica
prob1 = megnet_label_components(raw, ica)
prob2 = megnet_label_components(raw, ica)
assert_allclose(prob1, prob2, atol=1e-6, err_msg="MEGnet predictions should be the same!")