Skip to content

Postprocessing

The postprocess module turns raw parameter objects returned by SoftmaxGLMHMM.fit_per_subject into tidy pandas DataFrames ready for downstream analysis and plotting.

from glmhmmt import build_trial_df, build_emission_weights_df, build_posterior_df

Constructs a trial-level DataFrame that merges the original data with Viterbi-decoded state assignments and smoothed posterior probabilities.

build_trial_df(
fitted_params: list[Params],
df: pd.DataFrame,
model: SoftmaxGLMHMM,
inputs: list[Array],
choices: list[Array],
masks: list[Array],
subject_col: str = "subject",
) -> pd.DataFrame

Returns the original df augmented with:

New columnDescription
state_viterbiMost likely state sequence (Viterbi).
state_posterior_kPosterior probability of state k for each k.

Example

trial_df = build_trial_df(fitted_params, df, model, inputs, choices, masks)
# Group by subject and state to inspect dwell times
trial_df.groupby(["subject", "state_viterbi"]).size()

Returns a long-format DataFrame of GLM emission weights (one row per state × feature combination), useful for heatmap visualisations.

build_emission_weights_df(
fitted_params: list[Params],
feature_cols: list[str],
subject_col: str = "subject",
) -> pd.DataFrame
ColumnDescription
subjectSubject identifier.
stateLatent state index.
featureFeature name (from feature_cols).
weightGLM regression weight.

Example

w_df = build_emission_weights_df(fitted_params, feature_cols=["contrast", "prev_choice"])
import seaborn as sns
sns.heatmap(w_df.pivot(index="state", columns="feature", values="weight"))

Returns the full smoothed posteriors p(z_t | y_{1:T}) for every trial and subject as a long DataFrame.

build_posterior_df(
fitted_params: list[Params],
model: SoftmaxGLMHMM,
inputs: list[Array],
choices: list[Array],
masks: list[Array],
subject_col: str = "subject",
) -> pd.DataFrame
ColumnDescription
subjectSubject identifier.
trialTrial index within subject.
stateLatent state index.
posteriorSmoothed posterior probability.