Skip to content

03. Representational Similarity Analysis (JRSA)

jnwb.jrsa runs representational similarity analysis (RSA) on any neural response tensor, such as population firing rates or multichannel LFP. The ensembles tutorial runs it beside a decoder.


1. Overview & Core Architecture

RSA compares neural population geometry across experimental conditions without fitting a classifier.

The diagram is the call order: two tensors enter, one JRSAResult leaves, and summary and plot read that result.

graph LR
    P[Population Response Tensor: X1, X2] --> Dist[Pairwise Metric Estimator]
    Dist --> JRSA[jrsa function: Alignment, Reduction, Metric]
    JRSA --> Result[JRSAResult: RDM, Permutation Stats, CIs]
    Result --> Summary[result.summary]
    Result --> Plot[result.plot]

Key Capabilities

  • Metrics (14): "rsa", "pearson", "spearman", "cosine", "kendall", "distance_correlation", "mutual_information", "transfer_entropy_histogram_nats", "phase_slope", "granger_ssr_ftest", "hsic", "cka", "rv", "procrustes". "granger_ssr_ftest" and "transfer_entropy_histogram_nats" are not the same estimands as connectivity granger or transfer_entropy. jRSA exposes the statsmodels SSR F-test and a plug-in histogram TE in nats. The SSR F-test, "phase_slope" and the TE each take one series per input. "mutual_information" is a plug-in histogram estimate on 32 bins, biased upward at small N: for independent inputs it averages about 2.0 nats at N = 60 and 0.32 nats at N = 1000. Only its permutation p compares across N, not its magnitude.
  • Direction of the directed metrics:
Metric jrsa(x1, x2, ...) measures Compare
"granger_ssr_ftest" x2 → x1: how much x2's past predicts x1 the reverse of jnwb.granger(X, Y).x_to_y
"transfer_entropy_histogram_nats" x2 → x1 the reverse of jnwb.transfer_entropy(X, Y).x_to_y
"phase_slope" positive when x1 leads x2 the same sign as jnwb.phase_slope_index(X, Y, fs=fs).x_to_y
- Tensor alignment: 2D, 3D, and 4D tensors, with automatic trial/time alignment (align="auto", lag=0).
- Resampling: permutation distributions (permutations=1000), bootstrap confidence intervals (bootstrap=500), and FDR correction (correction="fdr_bh").
- CPU arithmetic: jrsa has no GPU path; every metric computes in NumPy on the CPU. Inputs may be NumPy arrays, scipy.sparse matrices (densified), JAX arrays, and torch tensors or CuPy arrays on any device (copied to the host); a masked array with a masked element raises. backend is recorded and moves no arithmetic, so backend="cupy" gives the same numbers on the CPU; device="cuda" warns and runs on the CPU, and execution["device"] records cpu. n_jobs parallelizes over CPU workers, and res.execution records what ran.

2. Core API: jnwb.jrsa

Basic Execution

import numpy as np
import jnwb

# x1: population activity, 12 conditions x 100 units
x1 = np.random.default_rng(0).normal(size=(12, 100))
result = jnwb.jrsa(
    x1,
    x2=None,            # If x2 is None, computes symmetric self-similarity
    metric="rsa",       # "rsa", "pearson", "cosine", "spearman", etc.
    stats=True,         # Enable permutation hypothesis testing
    permutations=1000,
    null="iid",         # conditions are exchangeable (see "The Permutation Null")
    bootstrap=500,
    correction="fdr_bh",
    alpha=0.05
)

# Inspect statistical summary
result.summary()

# Render visualization
fig = result.plot()

The JRSAResult Container Class

jnwb.JRSAResult encapsulates: - result.value: Scalar or array of estimated similarities. - result.p: Permutation p-value when stats=True and permutations > 0; otherwise the metric's parametric p-value, or None for a metric without one. alternative sets its tail in both cases. - result.ci: Bootstrap confidence intervals (lower, upper) when requested. - result.statistic: Test statistic accompanying p when applicable. - result.null_distribution: Array of surrogate permutation values when computed.

result.save(path, fmt) writes every field in each format: "npz" stores the arrays and the other fields (metric, parameters, execution with its seed) as one JSON string under fields_json; "json" stores every field; "csv" stores one field,value row per scalar field and each array as rows of numbers (a 2-D value as one row per matrix row, after a value.shape row). result.plot() centres its diverging colormap on 0 when the matrix has both signs; vmin, vmax and cmap override it.

The Permutation Null (null=)

Each permutation resamples x2 along one axis, and lag shifts the same one, whatever adim names: the last axis for the paired metrics ("pearson", "spearman", "kendall", "cosine", "mutual_information", "granger_ssr_ftest", "transfer_entropy_histogram_nats", "phase_slope"), so put time last, and axis 0, the observations, for "rsa", "cka", "rv", "hsic", "distance_correlation" and "procrustes". A non-default adim that does not name that axis raises ValueError with a permutation null, bootstrap or a nonzero lag. Without adim, window cuts that axis too; adim=-1 makes the axis-0 metrics window features. A lag of l pairs x1[t] with x2[t - l], dropping |l| samples; execution['n_overlap'] records the count.

null= Resampling Valid when
None (default) "circular_shift" for the paired metrics; the axis-0 metrics have no default and raise ValueError see those rows
"circular_shift" rotate x2 by a random shift of 0 to n - 1 samples each series is stationary; autocorrelation is kept. Two-sided p cannot fall below about 2/n
"block" permute consecutive blocks of block_len samples, which must be given block_len spans several autocorrelation times. On independent AR(1) series with coefficient 0.9 (200 samples, 80 pairs), block_len=20 rejected at p ≤ 0.05 for 0.30 of pairs with "cka" and 0.125 with "pearson"; block_len=50 for 0.062 and 0.037
"iid" permute single samples samples are independent. On two independent AR(1) series with coefficient 0.9 it rejects at p ≤ 0.05 about half the time

result.execution["null"] records the scheme that ran and result.execution["null_block_len"] the block length. Before 0.2.6.1 every metric used "iid", so p-values of the paired metrics on autocorrelated data have changed.

The axis-0 metrics take "iid" for exchangeable conditions and "circular_shift" when axis 0 is time, where the row permutation made "cka" and "rv" reject all 40 independent AR(1) pairs at p ≤ 0.05. "block" is not calibrated for them.

bootstrap > 0 with a paired metric raises unless null="iid" is named. The bootstrap resamples single samples, which undercovers on autocorrelated data: on independent AR(1) pairs with coefficient 0.9 the 95% interval of "pearson" covered 0 for 0.475 of pairs. No block bootstrap is implemented. The axis-0 metrics' bootstrap is unchanged.

# x1, x2: (12 conditions, 100 units, 50 timepoints); five blocks of 10 on the time axis
res = jnwb.jrsa(x1, x2, metric="pearson", null="block", block_len=10, rng=0)
res.execution["null"]   # "block"

3. Windows and Lags

Sliding Windows

jrsa analyzes one window per call, or every window in one call with sliding=True. There window is the width and step the stride, in samples along the aligned axis, and the result holds one value per window: p is each window's raw p, and q its family-wise p from the maximum statistic over the windows. A loop of single-window calls gives the same values and p:

# x1, x2: (12 conditions, 100 units, 50 timepoints); the aligned axis is time
width, step = 20, 5
n_times = x1.shape[-1]
starts = range(0, n_times - width + 1, step)
per_window = [
    jnwb.jrsa(x1, x2, metric="pearson", window=(s, s + width), lag=5, rng=0)
    for s in starts
]
values = np.array([float(r.value) for r in per_window])   # one value per window

The lag drops 5 of each window's 20 samples, leaving 15 pairs. The circular-shift null rotates all 20, so p cannot fall below about 2/20, which is above 0.05.

Each call forms its own permutation null, so correct the per-window p-values together (for example with jnwb.StatisticalAnalysis.fdr_correct) before reading any one of them.

4. Missing Condition Handling & Preprocessing Invariants

  • Missing data (nan_policy): nan_policy="omit" drops each observation that is NaN anywhere: a last-axis sample for the paired metrics, a row of axis 0 for the axis-0 metrics; if none remain, every metric raises ValueError.
  • Preprocessing: standardizing each condition's pattern before correlation-distance RSA changes nothing, because correlation centers and scales each pattern itself. Z-scoring each feature across conditions does change the RDM.

5. Standalone RDM Operations (jnwb.rdm, jnwb.rdm_similarity)

To build or compare RDMs without jrsa:

# Compute pairwise distance matrix (N conditions x D features)
# Returns 1D condensed vector of length N*(N-1)//2 (default)
rdm_vec = jnwb.rdm(X, metric="correlation", condensed=True)

# Or full N x N symmetric square matrix with zero diagonal
rdm_sq = jnwb.rdm(X, metric="correlation", condensed=False)

# Compare two RDMs directly (Spearman, Pearson, Kendall, or Cosine)
rho, p_val = jnwb.rdm_similarity(rdm_vec1, rdm_vec2, metric="spearman")

Only the off-diagonal upper triangle is compared. RDM cells are not independent observations, so p_val is not a valid test of RDM relatedness; for inference, permute the condition labels and recompute the similarity (Kriegeskorte et al. 2008, "Step 5"). rdm_similarity leaves that to the caller, and jnwb.jrsa with stats=True runs a permutation null. With metric="cosine" the p value is nan.

Validation: synthetic | N = 12 conditions | seed 19 | input synthetic | jnwb 0.2.17

Synthetic RDMs and windowed jRSA trace Synthetic RDMs and windowed jRSA trace

Panels A and B are the RDMs of two synthetic populations that share one condition pattern, over samples 50 to 70, where the trace peaks (jnwb.rdm). Panel C is jnwb.jrsa on 20-sample windows: the peak rho of 0.74 equals jnwb.rdm_similarity on those two RDMs, and 3 of 11 windows exceed the upper edge of their 95% null band (1000 permutations; per window, uncorrected).

References

The methods on this page are cited in References.