Skip to content

Commit

Permalink
[ENH] RESS implem sklearn interface (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Barascud <[email protected]>
  • Loading branch information
ludovicdmt and nbara authored May 11, 2023
1 parent 501ef5c commit d9326e7
Show file tree
Hide file tree
Showing 19 changed files with 2,731 additions and 2,150 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install meegkit and dependencies
Expand Down
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PYTHON ?= python
PYTESTS ?= py.test
CTAGS ?= ctags
CODESPELL_SKIPS ?= "*.html,*.fif,*.eve,*.gz,*.tgz,*.zip,*.mat,*.stc,*.label,*.w,*.bz2,*.annot,*.sulc,*.log,*.local-copy,*.orig_avg,*.inflated_avg,*.gii,*.pyc,*.doctree,*.pickle,*.inv,*.png,*.edf,*.touch,*.thickness,*.nofix,*.volume,*.defect_borders,*.mgh,lh.*,rh.*,COR-*,FreeSurferColorLUT.txt,*.examples,.xdebug_mris_calc,bad.segments,BadChannels,*.hist,empty_file,*.orig,*.js,*.map,*.ipynb,searchindex.dat"
CODESPELL_DIRS ?= meegkit/ doc/
CODESPELL_DIRS ?= meegkit/ examples/

help:
@echo "Please use \`make <target>' where <target> is one of:"
Expand Down Expand Up @@ -43,9 +43,8 @@ build-doc:
cd doc; make html

build-examples:
cd examples;
find . -name "example_*.py" | xargs sphx_glr_python_to_jupyter.py
find . -name "example_*.ipynb" | xargs jupyter nbconvert --execute --to notebook --inplace
cd examples; find . -name "example_*.py" | xargs sphx_glr_python_to_jupyter.py
cd examples; find . -name "example_*.ipynb" | xargs jupyter nbconvert --execute --to notebook --inplace

# Style
# =============================================================================
Expand Down
1 change: 1 addition & 0 deletions doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ help:
clean:
-rm -rf _build/*
-rm -rf _source/*
-rm -rf auto_examples/*
-rm -rf generated
-rm -rf *.stc
-rm -rf *.fif
Expand Down
272 changes: 172 additions & 100 deletions examples/example_asr.ipynb

Large diffs are not rendered by default.

203 changes: 129 additions & 74 deletions examples/example_dering.ipynb

Large diffs are not rendered by default.

813 changes: 405 additions & 408 deletions examples/example_detrend.ipynb

Large diffs are not rendered by default.

80 changes: 52 additions & 28 deletions examples/example_dss.ipynb

Large diffs are not rendered by default.

593 changes: 304 additions & 289 deletions examples/example_dss_line.ipynb

Large diffs are not rendered by default.

714 changes: 420 additions & 294 deletions examples/example_mcca.ipynb

Large diffs are not rendered by default.

507 changes: 264 additions & 243 deletions examples/example_ress.ipynb

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions examples/example_ress.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import scipy.signal as ss

from meegkit import ress
from meegkit.utils import fold, matmul3d, rms, snr_spectrum, unfold
from meegkit.utils import fold, matmul3d, snr_spectrum, unfold

# import config

Expand Down Expand Up @@ -48,8 +48,8 @@
noise = fold(noise, n_times)

# mix signal and noise
signal = SNR * signal / rms(signal.flatten())
noise = noise / rms(noise.flatten())
signal = SNR * signal / np.sqrt(np.mean(signal ** 2))
noise = noise / np.sqrt(np.mean(noise ** 2))
data = signal + noise

# Plot
Expand All @@ -64,14 +64,14 @@
###############################################################################
# Enhance oscillatory activity using RESS
# -----------------------------------------------------------------------------

# Apply RESS
out, maps, _ = ress.RESS(data, sfreq=sfreq, peak_freq=target, return_maps=True)
r = ress.RESS(sfreq=sfreq, peak_freq=target, compute_unmixing=True)
out = r.fit_transform(data)

# Compute PSD
nfft = 250
df = sfreq / nfft # frequency resolution
bins, psd = ss.welch(out.squeeze(1), sfreq, window="hamming", nperseg=nfft,
bins, psd = ss.welch(np.squeeze(out), sfreq, window="hamming", nperseg=nfft,
noverlap=125, axis=0)
psd = psd.mean(axis=1, keepdims=True) # average over trials
snr = snr_spectrum(psd, bins, skipbins=2, n_avg=2)
Expand All @@ -88,12 +88,12 @@
###############################################################################
# Project components back into sensor space to see the effects of RESS on the
# average SSVEP.

proj = matmul3d(out, maps)
fromress = r.from_ress
proj = matmul3d(out, fromress)
f, ax = plt.subplots(n_chans, 2, sharey="col")
for c in range(n_chans):
ax[c, 0].plot(data[:, c].mean(-1), lw=.5)
ax[c, 1].plot(proj[:, c].mean(-1), lw=.5)
ax[c, 0].plot(data[:, c].mean(-1), lw=.5, label="data")
ax[c, 1].plot(proj[:, c].mean(-1), lw=.5, label="projection")
ax[c, 0].set_ylabel(f"ch{c}")
if c < n_chans:
ax[c, 0].set_xticks([])
Expand All @@ -102,3 +102,4 @@
ax[0, 0].set_title("Trial average (before)")
ax[0, 1].set_title("Trial average (after)")
plt.show()

328 changes: 203 additions & 125 deletions examples/example_star.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit d9326e7

Please sign in to comment.