Skip to content

Instantly share code, notes, and snippets.

View larsoner's full-sized avatar

Eric Larson larsoner

View GitHub Profile
@larsoner
larsoner / run_demo.sh
Created August 28, 2026 15:27
plotly scraper demo
#!/usr/bin/env bash
# Before/after demo for plotly/plotly.py#5701 (sphinx-gallery scraper rewrite).
# Creates ./sgdemo and ./sgdemo-venv, builds the same one-example gallery with
# released plotly and with the PR branch, and prints what to compare.
set -euo pipefail
mkdir -p sgdemo/examples
# a rerun must not reuse sphinx-gallery's cache of executed examples
rm -rf sgdemo/auto_examples sgdemo/_build_before sgdemo/_build_after
@larsoner
larsoner / cluster_equiv_ft.m
Last active August 26, 2026 11:43
Cluster equivalence check MNE-Python<->FieldTrip
% FieldTrip reference runs for MNE-Python's mne/stats/tests/test_cluster_equiv.py
% (see mne-tools/mne-python#12663).
%
% The input file cluster_equiv_data.mat is produced by the scenario builders in
% test_cluster_equiv.py (exported with scipy.io.savemat); each scenario holds
% `data` (n_rows x n_chan x n_times), per-row factor labels, and (for
% within-subject designs) 1-based subject indices. This script runs the
% matching FieldTrip cluster-based permutation test for each scenario with
% ft_timelockstatistics (cfg.method = 'montecarlo', cfg.correctm = 'cluster',
% cfg.clusterstatistic = 'maxsum') and saves/prints the observed stat map and
"""Minimal reproducer: OpenBLAS 0.3.20 DSYMM is wrong on its Cooperlake kernel.
C = B @ A with A symmetric, column-major, via
cblas_dsymm(CblasColMajor, CblasRight, CblasUpper,
m, n, 1.0, A(n x n), n, B(m x n), m, 0.0, C, m)
compared against numpy. (This is the call OpenMEEG makes in
Matrix::operator*(const SymMatrix&).)
Needs only numpy and a libopenblas. To exercise a kernel your CPU does not
have, use Intel SDE -- `-cpx` sets CPUID to Cooper Lake -- together with
@larsoner
larsoner / create_fsaverage_1020.m
Last active June 1, 2026 16:09
fsaverage_1005 code
% Create fsaverage_1020
headshape = ft_read_headshape('~/mne_data/MNE-sample-data/subjects/fsaverage/bem/outer_skin.surf');
% [pos, tri, magic] = read_surf('~/mne_data/MNE-sample-data/subjects/fsaverage/surf/lh.seghead');
% headshape = [];
% headshape.pos = pos;
% headshape.tri = tri;
% ft_plot_mesh(headshape,'facecolor','skin');camlight;
@larsoner
larsoner / magic.ipynb
Last active February 11, 2026 17:01
MagicGUI
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
"""Visual stimulus design.
Tools for drawing shapes and text on the screen.
"""
# Authors: Dan McCloy <drmccloy@uw.edu>
# Eric Larson <larsoner@uw.edu>
# Ross Maddox <rkmaddox@uw.edu>
#
# License: BSD (3-clause)
@larsoner
larsoner / fwd_opt.py
Last active May 30, 2026 07:51
See how we can speed up dipole fitting
import numpy as np
import mne
import time
data_path = mne.datasets.sample.data_path()
subjects_dir = data_path / 'subjects'
n_dips = 10
radius = 0.05
rr = np.random.default_rng(0).normal(size=(n_dips, 3))
rr /= np.linalg.norm(rr, axis=1)[:, np.newaxis] / radius * 0.9
@larsoner
larsoner / mixed_norm_visual_left.py
Created April 15, 2025 15:48
mixed_norm_visual_left.py
import numpy as np
import mne
from mne.datasets import sample
from mne.inverse_sparse import make_stc_from_dipoles, mixed_norm
from mne.minimum_norm import apply_inverse, make_inverse_operator
from mne.viz import (
plot_dipole_amplitudes,
plot_dipole_locations,
plot_sparse_source_estimates,
@larsoner
larsoner / Makefile
Last active December 21, 2024 18:46
SWIG -fastdispatch changes arguments that can be used
.PHONY: all clean openmeeg_wrap.cxx _example.so test1 test2 test3 test4 test5
all : clean example_wrap.cxx _example.so test1 test2 test3 test4 test5
clean:
rm -f *.o
rm -f *.so
rm -f *wrap*.c*
rm -f *wrapper.py
rm -Rf build
rm -Rf __pycache__
import mne
data_path = mne.datasets.sample.data_path()
sample_data_raw_file = data_path / "MEG" / "sample" / "sample_audvis_raw.fif"
subjects_dir = data_path / "subjects"
raw = mne.io.read_raw_fif(sample_data_raw_file, verbose=False).crop(tmax=60)
events = mne.find_events(raw, stim_channel="STI 014")
epochs = mne.Epochs(raw, events, event_id = {"auditory/left": 1}, tmin=-0.3, tmax=0.7)[:10]
inverse_operator_file = data_path / "MEG" / "sample" / "sample_audvis-meg-oct-6-meg-inv.fif"
inv_operator = mne.minimum_norm.read_inverse_operator(inverse_operator_file)
stcs = mne.minimum_norm.apply_inverse_epochs(epochs, inv_operator, lambda2=1/3, pick_ori=None)