Skip to content

Ensembles and Population Dynamics

Representational dissimilarity matrices (RDM), second-order RDM similarity, Joint RSA (JRSA) across time, and nested cross-validated linear SVM population decoding.

Run the executable tutorial:

python examples/tutorials/07_ensembles.py

examples/ ships in neither the wheel nor the sdist, so this line needs a clone, not pip install jnwb.

The figure

Validation: synthetic | N = 30 units | seed 42 | input synthetic | jnwb 0.2.17

Synthetic RDM, second-order similarity of two RDMs, and nested cross-validated decoding accuracy Synthetic RDM, second-order similarity of two RDMs, and nested cross-validated decoding accuracy

Synthetic: the seeded patterns the tutorial draws, in the same order. Panel A is the RDM of the first pattern set (jnwb.rdm). Panel B compares the pair distances of the two sets; its legend gives the second-order Spearman rho (jnwb.rdm_similarity). Panel C is the outer-fold accuracy of the nested cross-validated linear SVM (jnwb.nested_cv_linear_svm) against its majority baseline; both values are in the legend.

Source

The page below is included from the tutorial script; edit the script, not this block.

"""Tutorial 07: Ensembles — Representational Dissimilarity, JRSA, and Decoding.

Run: python examples/tutorials/07_ensembles.py
"""

from __future__ import annotations

import sys
from pathlib import Path

import numpy as np

# This file is run from a checkout, so prefer that checkout over any installed jnwb:
# Python puts this directory on sys.path, not the repository root. A run that is
# deliberately qualifying an installed copy says so with JNWB_EXPECTED_PACKAGE_ROOT,
# and then this guard stands aside -- otherwise it would quietly redirect CI's
# installed-wheel tutorial step back to the checkout.
import os

_CHECKOUT = Path(__file__).resolve().parents[2]
if not os.environ.get("JNWB_EXPECTED_PACKAGE_ROOT") and (
    _CHECKOUT / "jnwb" / "__init__.py"
).exists():
    sys.path.insert(0, str(_CHECKOUT))

import jnwb


def main() -> None:
    rng = np.random.default_rng(42)
    n_conditions = 6
    n_units = 30

    # Synthetic population response vectors for 6 conditions across 30 neurons
    patterns_v1 = rng.normal(loc=1.0, scale=0.5, size=(n_conditions, n_units))
    patterns_v2 = patterns_v1 + 0.3 * rng.normal(size=(n_conditions, n_units))

    # 1. Representational Dissimilarity Matrix (RDM)
    # Condensed 1D vector form of size C * (C - 1) / 2
    rdm_v1_condensed = jnwb.rdm(patterns_v1, metric="correlation", condensed=True)
    expected_len = n_conditions * (n_conditions - 1) // 2
    assert len(rdm_v1_condensed) == expected_len
    print(f"Condensed RDM length: {len(rdm_v1_condensed)} pairs for {n_conditions} conditions")

    # Square symmetric form of shape (C, C)
    rdm_v1_square = jnwb.rdm(patterns_v1, metric="correlation", condensed=False)
    assert rdm_v1_square.shape == (n_conditions, n_conditions)
    np.testing.assert_allclose(np.diag(rdm_v1_square), 0.0)
    print(f"Square RDM shape: {rdm_v1_square.shape} with zero diagonal")

    # 2. Second-Order RDM Similarity across Areas
    rdm_v2_condensed = jnwb.rdm(patterns_v2, metric="correlation", condensed=True)
    rho, p_val = jnwb.rdm_similarity(rdm_v1_condensed, rdm_v2_condensed, metric="spearman")
    assert -1.0 <= rho <= 1.0
    print(f"Representational similarity (Spearman rho) between V1 and V2: {rho:.3f} (p={p_val:.4f})")

    # 3. Joint Representational Similarity Analysis (JRSA) over Time
    # Temporal response arrays: (n_conditions, n_features, n_timepoints)
    time_series_1 = rng.normal(size=(n_conditions, n_units, 10))
    time_series_2 = time_series_1 + 0.2 * rng.normal(size=(n_conditions, n_units, 10))
    jrsa_res = jnwb.jrsa(time_series_1, time_series_2, metric="rsa", null="iid")
    assert np.isfinite(jrsa_res.value)
    print(f"JRSA representational similarity: {float(jrsa_res.value):.3f}")

    # 4. Population Decoding: Nested Cross-Validated Linear SVM
    # Trials x Features matrix X, Trials binary label vector y
    n_trials = 40
    X = rng.normal(size=(n_trials, n_units))
    # Condition 1 has elevated firing in first 5 neurons
    X[:20, :5] += 0.8
    y = np.array([0] * 20 + [1] * 20)

    cv_results = jnwb.nested_cv_linear_svm(X, y, n_splits=3)
    assert "accuracy" in cv_results and "fold_accuracies" in cv_results
    print(f"Population decoding accuracy: {cv_results['accuracy'] * 100:.1f}% across {len(cv_results['fold_accuracies'])} CV folds")


if __name__ == "__main__":
    main()