Skip to content

Plotting

mtdr.plot

Plotting helpers (optional plot extra, matplotlib).

from mtdr import plot always works; matplotlib is imported when a function is first called, and its absence then raises ImportError with the install hint pip install 'mtdr[plot]'. Every function returns the Axes (or the array of Axes) it drew on and never calls plt.show().

Axes handling: a one-panel function takes ax (an Axes, or None for a new figure); a several-panel function takes axes: None (a new figure, one row of panels), an array of Axes with at least as many entries as panels (used in order, extras untouched), or a single Axes when exactly one panel is drawn. time is an optional (n_bins,) array of bin centres for the x-axis.

The quantities drawn are the fitted objects of the model (docs/model.md): the temporal bases \(S_p\) and weights \(W_p\) of (M1), the coefficient norms \(\lVert B_p[:,t]\rVert\) of (M37), the projections (M49)-(M49a) of condition averages, and the AIC trace of the greedy search (M39).

bases(model, regressors=None, time=None, axes=None)

One panel per regressor, one line per component of S_[p].

The temporal bases \(S_p\) of the model (M1), \(B_p=W_pS_p^\top\). Answers "when in the trial does this regressor's code live". In the canonical frame column \(j\) of \(S_p\) is \(\sigma_jv_j\) (its norm the singular value of \(B_p\)) and is labelled by \(\sigma_j\) and its share of \(\lVert B_p\rVert_F^2\); in the raw frame by \(\lVert W_p[:,j]\rVert\,\lVert S_p[:,j]\rVert\).

Parameters:

Name Type Description Default
model MTDR

A fitted model.

required
regressors sequence of str

Which; default every regressor of positive rank.

None
time array_like

(n_bins,) bin centres.

None
axes Axes, array of Axes or None

Where to draw.

None

Returns:

Type Description
ndarray

The Axes, one per regressor.

Examples:

>>> from mtdr import MTDR, plot, simulate
>>> sim = simulate(n_neurons=20, n_bins=6, n_trials=100, ranks=[2, 1], seed=0)
>>> model = MTDR(ranks=[2, 1], estimator="svd").fit(sim.Y, sim.X)
>>> plot.bases(model).shape
(2,)

weights(model, regressors=None, kind='hist', component=0, axes=None)

Per regressor, the distribution of W_[p][:, component] over neurons.

A histogram (kind="hist") or a bar per neuron ("bar", the reference demo's view; useful below about 200 neurons). Answers "is the code carried by a few neurons or by many" (\(W_p\) of (M1)).

Parameters:

Name Type Description Default
model MTDR

A fitted model.

required
regressors sequence of str

Which; default every regressor of positive rank.

None
kind ('hist', 'bar')

The view.

"hist"
component int

Which column of W_[p]; must exist for every regressor drawn.

0
axes Axes, array of Axes or None

Where to draw.

None

Returns:

Type Description
ndarray

The Axes.

Examples:

>>> from mtdr import MTDR, plot, simulate
>>> sim = simulate(n_neurons=20, n_bins=6, n_trials=100, ranks=[2, 1], seed=0)
>>> model = MTDR(ranks=[2, 1], estimator="svd").fit(sim.Y, sim.X)
>>> plot.weights(model, kind="bar", component=0).shape
(2,)

Plot the AIC trace of a greedy rank search, every candidate included (M39).

AIC against search step for the accepted path (the accepted regressor annotated at each step), and every candidate tried from a step as a marker at the next step, coloured by the regressor it raised (legend), including the terminal rejected round. The AIC axis has no offset and the steps are integers. A search seeded by an SVD stage (rank_search_init="svd_weighted" or "svd") gets a second panel, the SVD stage first, each on its own y-axis: the two stages' scores are on different scales. Answers "how decisive was the rank choice"; a flat tail means near ties (history.near_ties()).

Parameters:

Name Type Description Default
history RankSearchHistory or MTDR

A history, or a model fitted with ranks="aic".

required
axes Axes, array of Axes or None

One or two panels.

None

Returns:

Type Description
ndarray

The Axes, one per stage.

Raises:

Type Description
ParameterError

For a model fitted with fixed ranks, or another object.

Examples:

>>> from mtdr import MTDR, plot, simulate
>>> sim = simulate(n_neurons=30, n_bins=6, n_trials=150, ranks=[2, 1], seed=0)
>>> model = MTDR(ranks="aic", estimator="svd").fit(sim.Y, sim.X)
>>> plot.rank_search(model).shape
(1,)

coefficient_norms(model, regressors=None, time=None, ax=None)

\(\lVert B_p[:,t]\rVert\) over time bins, one line per regressor.

The population-level strength of each regressor's coefficient matrix (M37) over the trial; frame-independent.

Parameters:

Name Type Description Default
model MTDR

A fitted model.

required
regressors sequence of str

Which; default every regressor.

None
time array_like

(n_bins,) bin centres.

None
ax Axes or None

Where to draw.

None

Returns:

Type Description
Axes

The axes.

Examples:

>>> from mtdr import MTDR, plot, simulate
>>> sim = simulate(n_neurons=20, n_bins=6, n_trials=100, ranks=[2, 1], seed=0)
>>> model = MTDR(ranks=[2, 1], estimator="svd").fit(sim.Y, sim.X)
>>> len(plot.coefficient_norms(model).lines)
2

trajectories(model, Y, X, regressor, mask=None, by=None, components=None, method='gls', weighted=True, basis=None, min_trials=1, time=None, cmap='viridis', ax=None)

Condition-averaged trajectories in a regressor's subspace.

Calls condition_average(Y, X, mask, by=by, regressor_names=model.regressor_names_, min_trials=min_trials), then MTDR.project on the averages with their mask and the given method, weighted and basis ((M49)-(M49a)). One trajectory per condition in the plane of two components (or one component against time, when components is an int or the width is 1), coloured by the condition's value of regressor, with a marker at the first bin. The colours are keyed by a legend with one entry per level of regressor (not per condition, when by spans several regressors) at up to eight levels, and by a colourbar ticked at the levels beyond. Conditions whose projection is all NaN are skipped and counted in the title.

Parameters:

Name Type Description Default
model MTDR

A fitted model.

required
Y array_like

(n_trials, n_neurons, n_bins) responses to average, NaN allowed (as fit).

required
X array_like

(n_trials, n_regressors) design that defines the conditions.

required
mask array_like

(n_trials, n_neurons); None infers it from NaN, as fit.

None
regressor str

The subspace to project onto, and the colouring variable.

required
by sequence of int or str

Condition columns; default every column.

None
components ((int, int), int or None)

None is (0, 1) at width >= 2, else 0 against time.

None
method ('gls', 'paper')

The projection, as MTDR.project.

"gls"
weighted bool

Weight neurons by their noise precision, as project.

True
basis mapping of str to array_like

(n_neurons, r) bases replacing W_[name], as project (e.g. from orthogonalize).

None
min_trials int

As condition_average.

1
time array_like

(n_bins,) bin centres, for the against-time view.

None
cmap str

Matplotlib colormap name.

'viridis'
ax Axes or None

Where to draw.

None

Returns:

Type Description
Axes

The axes.

Raises:

Type Description
ParameterError

For components beyond the projected width, or as project.

Examples:

>>> from mtdr import MTDR, plot, simulate
>>> sim = simulate(n_neurons=30, n_bins=6, n_trials=200, ranks=[2, 1], seed=0)
>>> model = MTDR(ranks=[2, 1], estimator="svd").fit(sim.Y, sim.X)
>>> ax = plot.trajectories(model, sim.Y, sim.X, "x0", by=["x0"])
>>> len(ax.lines)   # five levels of x0, a line and a start marker each
10

recovery(model, sim, axes=None)

Estimated against true B_p, intercept and noise precision.

One scatter per regressor's coefficient matrix \(B_p=W_pS_p^\top\) of (M1) (frame-invariant, so no alignment is needed), one for the intercept when both the model and the simulation have one, and one for the noise precisions on log-log axes, each with the identity line and the correlation over all entries in its title ("r undefined" when either side is constant, e.g. a scalar noise_precision): the reference demo's final figures.

Parameters:

Name Type Description Default
model MTDR

A model fitted to sim (same neurons, regressors in the same order).

required
sim SimulatedData

The ground truth.

required
axes array of Axes or None

n_regressors + 2 panels (+ 1 without an intercept).

None

Returns:

Type Description
ndarray

The Axes.

Raises:

Type Description
ParameterError

If the model and the simulation do not match.

Examples:

>>> from mtdr import MTDR, plot, simulate
>>> sim = simulate(n_neurons=20, n_bins=6, n_trials=100, ranks=[2, 1], seed=0)
>>> model = MTDR(ranks=[2, 1], estimator="svd").fit(sim.Y, sim.X)
>>> plot.recovery(model, sim).shape
(4,)