Skip to main content

OHBA Software Library: Dynamics Toolbox

PyPI version Documentation License Paper

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 brain networks (often called brain states or modes), including the Hidden Markov Model (HMM) and Dynamic Network Modes (DyNeMo), along with everything needed for a complete analysis: data loading and preparation, spectral estimation, network visualisation and statistical significance testing.

You can use osl-dynamics to:

  • Infer dynamic functional networks from resting-state or task M/EEG and fMRI data using the HMM, DyNeMo and related models (M-DyNeMo, HIVE, DIVE, DyNeSTE and more).
  • Characterise brain states/modes with summary statistics (fractional occupancy, lifetimes, intervals, switching rates), state-specific power maps, and functional connectivity.
  • Estimate spectra using multitaper and regression-based methods, or wavelet transforms.
  • Detect oscillatory bursts.
  • Test for statistical significance using GLM permutation testing.
  • Preprocess and source reconstruct M/EEG data: preprocessing, coregistration, beamforming and parcellation.
  • Simulate time series data from HMMs, sinusoidal oscillators and autoregressive models.

osl-dynamics works with MNE-Python: a typical M/EEG workflow preprocesses, source reconstructs and parcellates data first, then models the dynamics of the parcel time courses with osl-dynamics. Data can be loaded from NumPy (.npy), MATLAB (.mat), text (.txt) or MNE (.fif) files.

For a full description of the toolbox, see the documentation.

Quick example

Train a Time-Delay Embedded Hidden Markov Model (TDE-HMM) on source-space MEG data to infer dynamic functional brain networks:

from osl_dynamics.data import Data
from osl_dynamics.models.hmm import Config, Model

# Load data, e.g. parcel time courses
data = Data("training_data")

# Prepare the data: time-delay embedding + PCA captures spectral structure
data.prepare({
    "tde_pca": {"n_embeddings": 15, "n_pca_components": 80},
    "standardize": {},
})

# Train an 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)

# Get inferred state probabilities, then compute summary statistics,
# spectra, power maps and connectivity networks
alpha = model.get_alpha(data)

See the tutorials for complete walkthroughs and the examples directory for full analysis pipelines.

Installation

We recommend installing osl-dynamics using the conda environment files in /envs, which can be installed using Miniforge.

conda / mamba installation

Miniforge (conda/mamba) can be installed with:

curl -LO "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh
rm Miniforge3-$(uname)-$(uname -m).sh

osl-dynamics installation

Different computers have their own environment files. For more information see the envs README.

Linux

curl -LO https://raw.githubusercontent.com/OHBA-analysis/osl-dynamics/refs/heads/main/envs/osld-tf.yml
mamba env create -f osld-tf.yml
rm osld-tf.yml

If you have a GPU, then use the osld-tf-cuda.yml environment instead:

curl -LO https://raw.githubusercontent.com/OHBA-analysis/osl-dynamics/refs/heads/main/envs/osld-tf-cuda.yml
mamba env create -f osld-tf-cuda.yml
rm osld-tf-cuda.yml

Mac

If you have an M-series (M1, M2, M3) chip use:

curl -LO https://raw.githubusercontent.com/OHBA-analysis/osl-dynamics/refs/heads/main/envs/osld-tf.yml
mamba env create -f osld-tf.yml
rm osld-tf.yml

Otherwise, if you have an Intel chip use:

curl -LO https://raw.githubusercontent.com/OHBA-analysis/osl-dynamics/refs/heads/main/envs/osld-tf-macos.yml
mamba env create -f osld-tf-macos.yml
rm osld-tf-macos.yml

Windows

If you are using a Windows computer, we recommend first installing Linux (Ubuntu) as a Windows Subsystem by following the instructions here. Then follow the instructions for Linux above in the Ubuntu terminal.

hbaws (Oxford)

On the OHBA workstation (hbaws), install Miniforge and Mamba using the instructions above and install osl-dynamics using:

curl -LO https://raw.githubusercontent.com/OHBA-analysis/osl-dynamics/refs/heads/main/envs/hbaws.yml
mamba env create -f hbaws.yml
rm hbaws.yml

BMRC (Oxford)

On the Biomedical Research Computing (BMRC) cluster, conda is available as a software module:

module load Miniforge3

and osl-dynamics can be installed with:

curl -LO https://raw.githubusercontent.com/OHBA-analysis/osl-dynamics/refs/heads/main/envs/bmrc.yml
conda env create -f bmrc.yml
rm bmrc.yml

The above can be run on the login nodes (clusterX.bmrc.ox.ac.uk). On compg017 you will need to set the following to use conda:

unset https_proxy http_proxy no_proxy HTTPS_PROXY HTTP_PROXY NO_PROXY

Install the latest code from the GitHub repository (optional)

You should only need to do this if you need a feature or fix that has not been released on pip yet.

After you have created an osld environment you can install the latest code (development version) from the GitHub repository with:

conda activate osld
pip install git+https://github.com/OHBA-analysis/osl-dynamics.git

Install the source code (optional)

After you have created an osld environment you can install an editable local copy of the source code on your computer with:

git clone https://github.com/OHBA-analysis/osl-dynamics.git
conda activate osld
cd osl-dynamics
pip install -e .

You will run your local copy of the code when you import osl_dynamics.

If you are a developer, you may wish to clone the repository using SSH rather than HTTPS to make pushing branches/commits easier:

git clone git@github.com:OHBA-analysis/osl-dynamics.git

Test GPUs are working

You can use the following to check if TensorFlow is using any GPUs you have available:

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

This should return a list of GPUs.

Removing osl-dynamics

Simply delete the conda environment:

conda env remove -n osld
conda clean --all

And remove the GitHub repository if you have cloned it:

rm -rf osl-dynamics

Documentation

The read the docs page should be automatically updated whenever there's a new commit on the main branch.

The documentation is included as docstrings in the source code. The API reference documentation will only be automatically generated if the docstrings are written correctly. The documentation directory /doc also contains .rst files that provide additional info regarding installation, development, the models, etc.

To compile the documentation locally you need to install the required packages (sphinx, etc) in your conda environment:

cd osl-dynamics
conda activate osld
pip install -r doc/requirements.txt

To compile the documentation locally use:

sphinx-build -b html doc build

The local build of the documentation webpage can be found in build/sphinx/html/index.html.

To skip building the tutorials, comment out "sphinx_gallery.gen_gallery" here.

Releases

To release a new version:

  1. Check the latest commit on main has compiled successfully on readthedocs.

  2. Create a new release using the 'Create a new release' link on the right of the GitHub repo webpage. Set the tag to the new version number with a v prefix (e.g. v3.3.0), write the release notes, the output of the following is a useful starting point:

git log --oneline <previous tag>..main

Select 'Latest' for the release label and click 'Publish release'.

  1. Publishing the release triggers a GitHub Actions workflow (.github/workflows/release.yml) that builds the package and uploads it to PyPI. Check the workflow succeeded under the Actions tab of the GitHub repo.

Installations from a clone of the repo (pip install -e .) automatically get a development version number based on the latest tag, e.g. 3.3.1.dev12 if 12 commits have been made since v3.3.0.

Citation

If you find this toolbox useful, please cite the paper:

Gohil, C., Huang, R., Roberts, E., van Es, M. W., Quinn, A. J., Vidaurre, D., & Woolrich, M. W. (2024). osl-dynamics, a toolbox for modeling fast dynamic brain activity. Elife, 12, RP91949.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

osl_dynamics-3.3.0.tar.gz (23.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

osl_dynamics-3.3.0-py3-none-any.whl (23.3 MB view details)

Uploaded Python 3

File details

Details for the file osl_dynamics-3.3.0.tar.gz.

File metadata

  • Download URL: osl_dynamics-3.3.0.tar.gz
  • Upload date:
  • Size: 23.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for osl_dynamics-3.3.0.tar.gz
Algorithm Hash digest
SHA256 e4a3a0084a09e1ca1c67b4c74f38fa7a165e7debf9cd06148c98e464ced9e513
MD5 5d9a960ab3cbcf69ec486014d0aabd3e
BLAKE2b-256 5894f4dcfe5d59a0b25aebf2a38486b37426fcfcde337c0a722e8abd0a395ea3

See more details on using hashes here.

Provenance

The following attestation bundles were made for osl_dynamics-3.3.0.tar.gz:

Publisher: release.yml on OHBA-analysis/osl-dynamics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file osl_dynamics-3.3.0-py3-none-any.whl.

File metadata

  • Download URL: osl_dynamics-3.3.0-py3-none-any.whl
  • Upload date:
  • Size: 23.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for osl_dynamics-3.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5bb29158de69f02dbe2fac73eeb6c4d0e5e8b230c68b21a9f6f9f366fbd243b6
MD5 ac5434b5e5d10c0803042f5f846c81cb
BLAKE2b-256 4f451d2cf79ca7fa1a55836b3b2c417c8200cfc71d8556374fd4be7778456dce

See more details on using hashes here.

Provenance

The following attestation bundles were made for osl_dynamics-3.3.0-py3-none-any.whl:

Publisher: release.yml on OHBA-analysis/osl-dynamics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

3.3.0 This release

2 files

3.2.3

2 files

3.2.2

2 files

3.2.1

2 files

3.2.0

2 files

3.1.0

2 files

3.0.0

2 files

2.3.3

2 files

2.3.2

2 files

2.3.1

2 files

2.3.0

2 files

2.2.1

2 files

2.2.0

2 files

2.1.8

2 files

2.1.7

2 files

2.1.6

2 files

2.1.5

2 files

2.1.4.post1

2 files

2.1.4

2 files

2.1.3

2 files

2.1.2

2 files

2.1.1

2 files

2.1.0

2 files

2.0.2

2 files

2.0.1

2 files

2.0.0

2 files

1.4.5

2 files

1.4.4

2 files

1.4.3

2 files

1.4.2

2 files

1.4.1

2 files

1.4.0

2 files

1.3.2

2 files

1.3.1

2 files

1.3.0

2 files

1.2.11

2 files

1.2.10

2 files

1.2.9

2 files

1.2.8

2 files

1.2.7

2 files

1.2.6

2 files

1.2.5

2 files

1.2.4

2 files

1.2.3

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.7

2 files

1.1.6

2 files

1.1.5

2 files

1.1.4

2 files

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.0

3 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page