CoMPASS Level 2 - Models¶
Bayesian Gaussian Mixture Models and GMM-HMM for cognitive state inference.
Overview¶
This module implements the Level 2 models that identify goal-directed cognitive states:
- BGMM (Bayesian Gaussian Mixture Model): Clusters behavioral features
- GMM-HMM: Models temporal dependencies across states
- Cross-validation for model selection
- State sequence decoding
The models identify:
- Oriented states: Goal-directed behavior toward target
- Non-Oriented states: Exploratory or non-goal-directed behavior
Model Selection
Use run_compass with cross-validation to automatically select the optimal number of states.
Main Modeling Function¶
run_compass¶
compass_labyrinth.compass.level_2.model.run_compass
¶
run_compass(
config: dict,
df: DataFrame,
features: list,
phase_options: list = [5],
ncomp_options: list[int] = [2, 3],
k_options: list[int] = [2, 3],
reg_options: list = [0.0001, 1e-05, 1e-06],
terminal_values: list = [47],
bout_col: str = "Bout_ID",
patience: None | str = None,
patience_candidates: list = [2, 3, 5, 10],
verbose: bool = False,
) -> tuple[pd.DataFrame, list]
Run CoMPASS.
Parameters:
-
config(dict) –Configuration dictionary.
-
df(DataFrame) –Input dataframe.
-
features(list) –List of feature column names to use.
-
phase_options(list, default:[5]) –List of phase options to test (default is [5]).
-
ncomp_options(list, default:[2, 3]) –Range of number of components to test (default is [2, 3]).
-
k_options(list, default:[2, 3]) –Range of k values to test (default is [2, 3]).
-
reg_options(list, default:[0.0001, 1e-05, 1e-06]) –List of regularization values to test (default is [1e-4, 1e-5, 1e-6]).
-
terminal_values(list, default:[47]) –List of terminal grid values (default is [47]).
-
bout_col(str, default:'Bout_ID') –Name of the bout column (default is "Bout_ID").
-
patience(None or str, default:None) –Patience setting for early stopping (default is None). Set to 'tune' if wanted to apply patience window.
-
patience_candidates(list, default:[2, 3, 5, 10]) –List of patience candidates to test if patience is 'tune' (default is [2, 3, 5, 10]). Only used if patience is set to 'tune'.
-
verbose(bool, default:False) –Whether to print detailed logs during model training (default is False).
Returns:
-
tuple–A tuple containing: - pd.DataFrame: DataFrame with assigned Level 2 states. - list: List of all CV results for visualization.
Source code in src/compass_labyrinth/compass/level_2/model.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
Model Initialization¶
initialize_bgmm¶
compass_labyrinth.compass.level_2.model.initialize_bgmm
¶
initialize_gmmhmm¶
compass_labyrinth.compass.level_2.model.initialize_gmmhmm
¶
Source code in src/compass_labyrinth/compass/level_2/model.py
Model Evaluation¶
compute_aic¶
compass_labyrinth.compass.level_2.model.compute_aic
¶
Source code in src/compass_labyrinth/compass/level_2/model.py
visualize_cv_results¶
compass_labyrinth.compass.level_2.model.visualize_cv_results
¶
visualize_cv_results(
config: dict,
all_results: list,
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
) -> None | list[plt.Figure]
Visualize cross-validation results.
Parameters:
-
config(dict) –Configuration dictionary.
-
all_results(list) –List of tuples containing CV results.
-
save_fig(bool, default:True) –Whether to save the figures (default is True).
-
show_fig(bool, default:True) –Whether to show the figures (default is True).
-
return_fig(bool, default:False) –Whether to return the figures (default is False).
Returns:
-
None or list of plt.Figure–List of figures if return_fig is True, otherwise None.
Source code in src/compass_labyrinth/compass/level_2/model.py
regularize_covariances¶
compass_labyrinth.compass.level_2.model.regularize_covariances
¶
State Analysis¶
get_unique_states¶
compass_labyrinth.compass.level_2.model.get_unique_states
¶
generate_state_color_map¶
compass_labyrinth.compass.level_2.model.generate_state_color_map
¶
Assign each state a color from the selected Seaborn palette.
Source code in src/compass_labyrinth/compass/level_2/model.py
assign_reward_orientation¶
compass_labyrinth.compass.level_2.model.assign_reward_orientation
¶
assign_reward_orientation(
df: DataFrame,
angle_col: str = "Targeted_Angle_smooth_abs",
level_2_state_col: str = "Level_2_States",
session_col: str = "Session",
) -> pd.DataFrame
Assigns reward orientation labels ('Reward Oriented' or 'Non-Reward Oriented') to Level 2 states per session, based on the median Targeted_Angle_smooth within each state and relative to session median.
Parameters:
-
df(DataFrame) –Input dataframe with columns for session, level 2 state, and angle.
-
angle_col(str, default:'Targeted_Angle_smooth_abs') –Column name representing the smoothed targeted angle.
-
level_2_state_col(str, default:'Level_2_States') –Column name for level 2 HMM states.
-
session_col(str, default:'Session') –Column name for session identifier.
Returns:
-
DataFrame–Updated dataframe with 'Reward_Oriented' column.
Source code in src/compass_labyrinth/compass/level_2/model.py
assign_hhmm_state¶
compass_labyrinth.compass.level_2.model.assign_hhmm_state
¶
Assigns a final HHMM (Hierarchical Hidden Markov Model) state to the dataframe. The final HHMM state is based on the combination of level 1 and level 2 states.
Parameters:
-
df(DataFrame) –Dataframe containing the level_1_state_col and level_2_state_col columns.
-
level_1_state_col(str) –The name of the column representing the first-level HMM state.
-
level_2_state_col(str) –The name of the column representing the second-level state (reward-oriented or not).
Returns:
-
df(DataFrame) –DataFrame with an additional 'HHMM State' column indicating the final HHMM state.
Source code in src/compass_labyrinth/compass/level_2/model.py
State Sequence Visualization¶
plot_state_sequence_for_session¶
compass_labyrinth.compass.level_2.model.plot_state_sequence_for_session
¶
plot_state_sequence_for_session(
df_session: DataFrame,
state_col: str = "Level_2_States",
color_map: None | dict = None,
title_prefix: str = "State Sequence",
) -> plt.Figure
Plot the state sequence using color bars for one session.
Source code in src/compass_labyrinth/compass/level_2/model.py
plot_state_sequences¶
compass_labyrinth.compass.level_2.model.plot_state_sequences
¶
plot_state_sequences(
config: dict,
df: DataFrame,
genotype: str = "WT-WT",
state_col: str = "Level_2_States",
sessions_to_plot: str | list | int = "all",
title_prefix: str = "State Sequence",
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
) -> None | list[plt.Figure]
Plot state sequences for specified sessions and genotype.
Source code in src/compass_labyrinth/compass/level_2/model.py
plot_hhmm_state_sequence¶
compass_labyrinth.compass.level_2.model.plot_hhmm_state_sequence
¶
plot_hhmm_state_sequence(
config: dict,
df: DataFrame,
session_col: str = "Session",
state_col: str = "HHMM State",
session_id: None | int = None,
title_prefix: str = "State Sequence",
colors: None | dict = None,
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
) -> None | plt.Figure
Plots a rectangular sequence of HHMM states for a given session.
Parameters:
-
config(dict) –Configuration dictionary for the project.
-
df(DataFrame) –DataFrame containing session and HHMM state columns.
-
session_col(str, default:'Session') –Name of the column indicating session.
-
state_col(str, default:'HHMM State') –Name of the column containing HHMM state labels.
-
session_id(None | int, default:None) –Specific session to plot. If None, plots all sessions.
-
title_prefix(str, default:'State Sequence') –Custom title prefix for plots.
-
colors(None | dict, default:None) –Dictionary mapping HHMM states to colors. If None, default colors are used.
-
save_fig(bool, default:True) –Whether to save the figure.
-
show_fig(bool, default:True) –Whether to display the figure.
-
return_fig(bool, default:False) –Whether to return the figure object.
Returns:
-
None or Figure–The figure object if return_fig is True, otherwise None.
Source code in src/compass_labyrinth/compass/level_2/model.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 | |
Related¶
- Data Streams - Compute features for Level 2
- Plotting - Additional visualization functions
- Utilities - Helper functions