osl-dynamics#
osl-dynamics is a Python toolbox for studying brain dynamics using neuroimaging data (MEG, EEG and fMRI). It provides generative models that decompose data into dynamic functional brain networks, often called brain states or modes, including the Hidden Markov Model (HMM) and Dynamic Network Modes (DyNeMo). It also provides everything needed for a complete analysis: M/EEG preprocessing, source reconstruction and parcellation, data preparation, spectral estimation, power maps, functional connectivity, burst detection, simulation and statistical significance testing.
osl-dynamics was developed by the OHBA Methods Group at the University of Oxford. It works with MNE-Python: a typical M/EEG workflow preprocesses, source reconstructs and parcellates the data (all available in osl_dynamics.meeg), then models the dynamics of the parcel time courses. Data can be loaded from NumPy (.npy), MATLAB (.mat), text (.txt) or MNE (.fif) files.
If you use osl-dynamics, please cite: Gohil, C., Huang, R., Roberts, E., van Es, M. W., Quinn, A. J., Vidaurre, D., and Woolrich, M. W. (2024). osl-dynamics, a toolbox for modeling fast dynamic brain activity. eLife, 12, RP91949.
Installation#
conda (recommended, includes TensorFlow):
mamba create -n osld -c conda-forge osl-dynamicspip without TensorFlow:
pip install osl-dynamicspip with TensorFlow:
pip install "osl-dynamics[tf]", orpip install "osl-dynamics[tf-cuda]"for an NVIDIA GPU on Linux
TensorFlow is only needed to train models (osl_dynamics.models). Data preparation, M/EEG processing, spectral analysis, simulation and statistics work without it.
Core workflow#
A typical M/EEG analysis has five steps. For fMRI or sensor-level data, start at step 3 and prepare the data with {"standardize": {}} only (fMRI HMMs use learn_means=True).
1. M/EEG processing (each session)#
import mne
from osl_dynamics.meeg import preproc, rhino, source_recon, parcellation, Session
# Sensor-level preprocessing with MNE and osl-dynamics
raw = mne.io.read_raw_fif("sub-01_task-rest.fif", preload=True)
raw = raw.filter(l_freq=1, h_freq=45).resample(sfreq=250)
raw = preproc.detect_bad_segments(raw, picks="mag")
raw = preproc.detect_bad_channels(raw, picks="mag")
raw.save("derivatives/preprocessed/sub-01_preproc-raw.fif")
# Surface extraction (once per subject), coregistration and forward model
rhino.extract_surfaces(mri_file="sub-01_T1w.nii.gz", outdir="derivatives/anat_surfaces/sub-01")
session = Session(
outdir="derivatives/osl",
id="sub-01_task-rest",
preproc_file="derivatives/preprocessed/sub-01_preproc-raw.fif",
surfaces_dir="derivatives/anat_surfaces/sub-01",
)
rhino.extract_fiducials_and_headshape_from_fif(session)
rhino.coregister_head_and_mri(session)
rhino.forward_model(session, model="Single Layer", gridstep=8)
# LCMV beamformer, then parcellation with symmetric orthogonalization
source_recon.lcmv_beamformer(session, chantypes=["mag", "grad"], rank={"meg": 60})
parcel_data = parcellation.parcellate_lcmv(
session,
parcellation_file="atlas-Glasser_nparc-52_space-MNI_res-8x8x8.nii.gz",
orthogonalization="symmetric",
)
parcellation.save_as_fif(parcel_data, session.preproc_file, filename="derivatives/osl/sub-01_task-rest/lcmv-parc-raw.fif")
osl_dynamics.meeg.parallel.run runs a function like this on many sessions in parallel. Without a subject MRI, skip rhino.extract_surfaces, use osl_dynamics.files.mni152_surfaces.directory as surfaces_dir and pass allow_mri_scaling=True to rhino.coregister_head_and_mri.
2. Sign flipping (source-space M/EEG)#
from osl_dynamics.data import sign_flipping
# Align the sign of the parcel time courses across sessions
# (parc_fifs are the parcellated fif files from step 1)
template_cov, _ = sign_flipping.build_template_mne_raw(parc_fifs)
for parc_fif, sflip_fif in zip(parc_fifs, sflip_fifs):
sign_flipping.sign_flip_mne_raw(parc_fif, template_cov=template_cov, output_file=sflip_fif)
3. Model training#
from osl_dynamics.data import Data
from osl_dynamics.models.hmm import Config, Model
# Load parcel time courses: fif files, a directory, a list of files or a list of arrays
data = Data(sflip_fifs, picks="misc", reject_by_annotation="omit", sampling_frequency=250)
# Time-delay embedding + PCA, then standardize
# (n_pca_components is a round number roughly twice the number of channels, e.g. 120 for 52 parcels)
data.prepare({
"tde_pca": {"n_embeddings": 15, "n_pca_components": 120},
"standardize": {},
})
# Train a TDE-HMM
config = Config(
n_states=8,
n_channels=data.n_channels,
sequence_length=200,
learn_means=False,
learn_covariances=True,
batch_size=256,
learning_rate=0.01,
n_epochs=20,
)
model = Model(config)
model.random_state_time_course_initialization(data, n_init=3, n_epochs=1)
model.fit(data)
model.save("results/model") # reload with osl_dynamics.models.load("results/model")
# State probabilities: a list with one (n_samples, n_states) array per session
gamma = model.get_gamma(data)
4. Post-hoc analysis#
from osl_dynamics.analysis import spectral, power, connectivity, post_hoc
from osl_dynamics.inference import modes
parcellation_file = "atlas-Glasser_nparc-52_space-MNI_res-8x8x8.nii.gz"
# State spectra from the time series before preparation; a Data object is
# aligned to gamma automatically (or use data.trim_time_series with arrays)
spectra = spectral.multitaper_spectra(data, alpha=gamma, frequency_range=[1, 45])
group = spectra.group_average()
p = group.power_maps() # (n_states, n_parcels), change from static power
c = group.coherence_networks(percentile=97) # (n_states, n_parcels, n_parcels)
power.save(p, parcellation_file=parcellation_file, filename="power_.png")
connectivity.save(c, parcellation_file=parcellation_file, filename="coh_.png")
# Summary statistics
stc = modes.argmax_time_courses(gamma)
fo = post_hoc.fractional_occupancies(stc) # (n_sessions, n_states)
lt = post_hoc.mean_lifetimes(stc, sampling_frequency=250)
intv = post_hoc.mean_intervals(stc, sampling_frequency=250)
sr = post_hoc.switching_rates(stc, sampling_frequency=250)
5. Statistical significance testing#
from osl_dynamics import stats
# Group difference in each summary statistic, corrected across states
# (assignments is a group label for each session)
for name, x in {"fo": fo, "lt": lt, "intv": intv, "sr": sr}.items():
result = stats.group_diff(x, assignments, n_perm=1000)
print(name, result.pvalues, result.effect_size())
# Group difference in the state power maps, corrected across states and parcels
session_p = spectra.power_maps() # (n_sessions, n_states, n_parcels)
result = stats.group_diff(session_p, assignments, n_perm=1000)
The Config API runs the same pipeline from a YAML file: osl-dynamics config.yml results, or osl_dynamics.run_pipeline(config, output_dir="results"). Each top-level key is a function in osl_dynamics.config_api.wrappers.
API notes#
Datakeeps data in memory by default and writes nothing to disk. Withload_memmaps=Trueit writes memory maps to a subdirectory ofstore_dir(default./tmp), which is deleted automatically when the script ends. Don’t calldata.delete_dir()at the end of a script; it is only needed to free disk space early when usingload_memmaps=Truein a long-running session such as a notebook.Every model module (
osl_dynamics.models.hmm,.dynemo,.dyneste,.mdynemo,.hive,.dive,.hmm_poisson,.hmm_dirichlet,.scdynemo) defines aConfigdataclass and aModelclass withfit,train,saveand initialization methods. Models with a hidden Markov chain (HMM, HIVE, HMM-Poisson, HMM-Dirichlet) return the state probabilities withget_gamma; DyNeMo-family models (DyNeMo, DyNeStE, M-DyNeMo, DIVE, SC-DyNeMo) return the mixing coefficients withget_alpha(M-DyNeMo returns(alpha, beta)). The Keras model ismodel.keras_model. Models are loaded withosl_dynamics.models.load.HMM-family models infer mutually exclusive states (use
spectral.multitaper_spectra); DyNeMo-family models infer mixtures of modes (usespectral.regression_spectra).spectral.welch_spectra,spectral.multitaper_spectraandspectral.regression_spectrareturnStateSpectraorRegressionSpectraobjects (StaticSpectrawithoutalpha, as doanalysis.static.welch_spectraandanalysis.static.multitaper_spectra). They have the attributesfrequencies,psdandcoherence(andpsd_changeandcoherence_changefor state and regression spectra), and the methodsgroup_average,power_maps,coherence_networks,decompose,plot_psdandsave; load withspectral.Spectra.load. Arguments afteralphaare keyword-only.osl_dynamics.statshas the ready-made testsevoked_response,paired_diff,group_diff,condition_diffandregression, each returning aTestResultwithpvaluesandeffect_size(). Custom designs useDesignConfig,DesignandGLMwithMaxStatPermutationorClusterPermutation. Corrections:correction="max_stat"(default), cluster-based, TFCE, orstats.fdr/stats.holm/stats.bonferronion p-values.Simulations compose a state model with an observation model:
simulation.Simulation(state_model=simulation.HMM(...), obs_model=simulation.MVN(...), n_samples=...). State models:HMM,HSMM,HierarchicalHMM,MDynHMM,MixedSine,MixedHSMM. Observation models:MVN,OscillatoryMVN,MDyn_MVN,MSess_MVN,MAR,TDECovs,OscillatoryBursts,Poisson,Dirichlet. Classes such asHMM_MVNandHSMM_MVNcombine common pairs.M/EEG processing (
osl_dynamics.meeg):preproc,rhino(surface extraction, coregistration, forward model),source_recon(LCMV beamformer),parcellation(withorthogonalization="symmetric"), andSessionfor the files of a session.preproc.ica_labelonly removes artefacts from the channels it is fitted on: usepicks="meg"for Elekta data so the magnetometers and gradiometers are both cleaned.Parcellations, masks and surfaces are downloaded from the osl-files repository on first use and cached; refer to them by file name.
osl_dynamics.files.fetch_file,fetch_directoryandfetch_alldownload files explicitly (e.g. before working offline).Spelling is American throughout (e.g.
standardize,orthogonalization,regularization).
Removed APIs#
Code written for earlier versions may use names that no longer exist:
osl_dynamics.glmandosl_dynamics.analysis.statisticsare replaced byosl_dynamics.stats.Spectra functions no longer return tuples such as
f, psd, coh = spectral.multitaper_spectra(...); they return a spectra object with.frequencies,.psdand.coherence.The
use_tfrecordargument and themask_fileargument are removed.osl-ephys is no longer required for M/EEG preprocessing and source reconstruction; use
osl_dynamics.meeg.Models saved with osl-dynamics 3.0 to 3.9 load with
osl_dynamics.models.load(the config and weights are converted). For HIVE and DIVE, callset_regularizersafter loading to include the KL loss of the session deviations in the free energy.Model API (old → new):
HMM, HIVE, HMM-Poisson, HMM-Dirichlet:
get_alpha→get_gamma. HMM:dual_estimation(alpha=...)→dual_estimation(gamma=...).get_means_covariances()→get_means()andget_covariances(); M-DyNeMoget_means_stds_corrs()→get_means(),get_standard_deviations()andget_correlations().M-DyNeMo:
get_stds/get_corrs/set_stds/set_corrs→get_standard_deviations/get_correlations/set_standard_deviations/set_correlations;get_mode_time_courses→get_alpha(returns(alpha, beta)),get_mode_logits→get_theta,sample_time_courses→sample_alpha.HIVE, DIVE:
get_group_means/get_group_covariances→get_means/get_covariances;get_session_means_covariances()→get_session_means()andget_session_covariances();get_summed_embeddings→get_session_embeddings;get_embedding_weights→get_label_embeddings;set_dev_parameters_initializer→set_deviation_parameters;set_embeddings_initializer→set_embeddings.SC-DyNeMo:
get_amplitude/get_frequency/get_variance→get_amplitudes/get_frequencies/get_variances.set_trainable(layer_names, False)→with model.freeze("model_rnn"):(layer names are attributes ofmodel.keras_model, e.g.obs_model,markov_chain,inference_rnn,model_rnn).model.model(the Keras model) →model.keras_model; each model module’s Keras model class isKerasModel; Keras methods are no longer forwarded from the model (usemodel.get_weights(),model.summary()ormodel.keras_model).fit(kl_annealing_callback=...)→fit(kl_annealing=...); DyNeStEfit(gs_annealing_callback=...)→fit(gumbel_annealing=...);fit(save_filepath=...)→best_filepath(withsave_best_after) orcheckpoint_dir(withcheckpoint_freq). History keysll_loss→nll_loss,rho→ema_decayand DyNeStEtemperature→gumbel_temperature.model.compile()is removed (fitcompiles the model).fine_tuning(HMM, DyNeMo) and DyNeModual_estimationno longer takestore_dir.GradientMonitoringCallback(loss_indices=...)→loss_names;CheckpointCallback(save_freq=...)→checkpoint_freq.html_summary()/latex_summary()→summary_table("html")/summary_table("latex");multistart_initialization→random_subset_initialization(take=1);set_random_state_time_course_initializationis removed (userandom_state_time_course_initialization).osl_dynamics.models.sc_dynemo→osl_dynamics.models.scdynemo.osl_dynamics.inference.tf_ops→osl_dynamics.utils.devices(e.g.devices.gpu_growth());osl_dynamics.utils.model→osl_dynamics.utils.tables.osl_dynamics.models.hmm_poi→osl_dynamics.models.hmm_poisson;osl_dynamics.simulation.HMM_Poi→HMM_Poisson.train(data, n_init=..., stay_prob=...)→train(data, init_kwargs={...}): options for the initialization method go ininit_kwargs; other keyword arguments go tofit(all exceptepochsand the saving options are also passed to the initialization).osl_dynamics.models.mod_base,inf_mod_baseandobs_mod→ theosl_dynamics.models.basesubpackage (model,markov,variational,configs,observation,initialization; all classes importable fromosl_dynamics.models.base);osl_dynamics.inference.initializersandoptimizersare removed;KLAnnealingCallback,EMADecayCallbackandGumbelSoftmaxAnnealingCallbackare removed.
Config fields (old → new):
lr_decay→learning_rate_decay,loss_calc→loss_reduction,do_kl_annealing→kl_annealing,do_gs_annealing/gs_*/*_gs_temperature→gumbel_annealing/gumbel_*/*_gumbel_temperature,dev_*→deviation_*,n_corr_modes→n_correlation_modes,*stds*/*corrs*→*standard_deviations*/*correlations*,inference_rnn/model_rnn→inference_rnn_type/model_rnn_type.strategyandmultiple_dynamicsare removed.
Documentation#
Every page is available as Markdown by appending .md to its URL.
Full documentation: the user guide, FAQ and tutorials as one Markdown file (the API reference is not included).
Installation: conda and pip installation, with separate pages for Windows and the Oxford BMRC cluster.
Getting started: a typical workflow, a minimal HMM example and the Config API.
M/EEG processing: preprocessing, surface extraction, coregistration, source reconstruction, parcellation, orthogonalization and sign flipping.
Parcellations: the available parcellation files.
Models: HMM, DyNeMo, DyNeStE, M-DyNeMo and HIVE, with figures.
Spectral analysis: which spectra function to use for each model, the spectra objects, power maps and coherence networks.
Statistical significance testing: which test and which correction for multiple comparisons (maximum statistic, cluster-based, TFCE, FDR) to use for each kind of output, covariates and effect sizes.
Tutorials: step by step walkthroughs covering M/EEG processing, data loading and preparation, time-delay embedding, static analysis, HMM and DyNeMo training, spectra, power maps, connectivity, summary statistics, task analysis and statistical testing.
FAQ: guidance on preprocessing, time-delay embedding, choosing parameters, choosing between runs and interpreting results.
API reference: documentation for every module, class and function.
Examples#
Example scripts: full pipelines for MEG and EEG preprocessing, MEG and fMRI analysis, spectra, static analysis, statistics, simulation, decoding, TINDA and plotting.