Skip to content

Views

The views module provides lightweight result container objects (SubjectFitView) that bundle a subject’s fitted parameters with their raw data and expose convenience plotting methods.

from glmhmmt import SubjectFitView, build_views

A container for a single subject’s fit result.

@dataclass
class SubjectFitView:
subject_id: Any
params: Params
inputs: Array # (T, F)
choices: Array # (T,)
masks: Array # (T,)
model: SoftmaxGLMHMM

Bar chart of the fraction of trials spent in each state (based on Viterbi decoding).

view.plot_state_occupancy()

.plot_emission_weights(feature_names=None, ax=None)

Section titled “.plot_emission_weights(feature_names=None, ax=None)”

Heatmap of GLM emission weights (num_states × num_features).

view.plot_emission_weights(feature_names=["contrast", "prev_choice", "prev_reward"])

Raster plot of the Viterbi state sequence across trials, colour-coded by state.

view.plot_state_sequence()

Ribbon plot of smoothed posterior probabilities p(z_t | y_{1:T}) across trials.

view.plot_posterior()

Returns the (K, K) transition matrix from the fitted parameters.


Batch-creates a list of SubjectFitView from the outputs of model.fit_per_subject.

build_views(
fitted_params: list[Params],
model: SoftmaxGLMHMM,
inputs: list[Array],
choices: list[Array],
masks: list[Array],
subject_ids: list[Any] | None = None,
) -> list[SubjectFitView]

Example

views = build_views(fitted_params, model, inputs, choices, masks)
# Plot all subjects
import matplotlib.pyplot as plt
fig, axes = plt.subplots(len(views), 2, figsize=(12, 4 * len(views)))
for i, v in enumerate(views):
v.plot_state_occupancy(ax=axes[i, 0])
v.plot_emission_weights(ax=axes[i, 1])
plt.tight_layout()
plt.savefig("all_subjects.pdf")

NameValueDescription
_LABEL_RANKdictMaps state index → human-readable label (e.g. "Engaged", "Lapse").
_STATE_HEXlist[str]Default hex colour palette for states in plots.