CoMPASS Level 1 - HMM Models¶
Hidden Markov Model implementations for inferring fine-grained motor states.
Overview¶
This module implements Gamma-distributed HMM models to identify behavioral states based on:
- Step length distributions (Gamma)
- Turn angle distributions (von Mises)
The model identifies two primary states:
- Surveillance: Low step length + High turn angle
- Ambulation: High step length + Low turn angle
Model Architecture
The GammaHMM uses a custom implementation with forward-backward and Viterbi algorithms optimized for behavioral state inference.
Core Model Class¶
GammaHMM¶
compass_labyrinth.compass.level_1.momentuPY.GammaHMM
¶
GammaHMM(
n_states=2,
angle_model="vm",
max_iter=200,
tol=0.0001,
method="L-BFGS-B",
random_state=0,
stationary=True,
angle_bias: Optional[Tuple[float, float]] = None,
)
angle_model: - "vm" : use angle (radians) ~ von Mises - "absgamma" : use |angle| ~ Gamma
Model Fitting¶
fit_best_hmm¶
compass_labyrinth.compass.level_1.momentuPY.fit_best_hmm
¶
fit_best_hmm(
preproc_df: DataFrame,
n_states: int = 2,
n_repetitions: int = 20,
opt_methods: list[str] = [
"BFGS",
"L-BFGS-B",
"Nelder-Mead",
"Powell",
],
max_iter: int = 200,
use_abs_angle: tuple[bool, ...] = (True, False),
stationary_flag: str | bool = "auto",
use_data_driven_ranges: bool = True,
angle_mean_biased: tuple[float, float] = (
np.pi / 2,
0.0,
),
session_col: str = "Session",
seed: int = 123,
enforce_behavioral_constraints: bool = True,
show_progress: bool = True,
) -> BestResult
Source code in src/compass_labyrinth/compass/level_1/momentuPY.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 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 603 604 605 606 607 608 | |
save_compass_level_1_results¶
compass_labyrinth.compass.level_1.momentuPY.save_compass_level_1_results
¶
Save CoMPASS Level 1 HMM results to disk.
Exports: - model_summary.json: Model parameters and statistics - data_with_states.csv: Full dataset with HMM state assignments - model_selection_records.csv: All candidate models tried during fitting - fitted_model.joblib: Serialized GammaHMM object (if joblib available)
Parameters:
-
config(dict) –Project configuration containing 'project_path_full'
-
results(BestResult) –Result object from fit_best_hmm()
Source code in src/compass_labyrinth/compass/level_1/momentuPY.py
Parameter Estimation¶
compute_parameter_ranges¶
compass_labyrinth.compass.level_1.momentuPY.compute_parameter_ranges
¶
compute_parameter_ranges(
df: DataFrame, angle_var: str, use_vm: bool = False
) -> Dict[str, Tuple[float, float]]
Source code in src/compass_labyrinth/compass/level_1/momentuPY.py
Inference Algorithms¶
forward_backward¶
compass_labyrinth.compass.level_1.momentuPY.forward_backward
¶
Source code in src/compass_labyrinth/compass/level_1/momentuPY.py
viterbi¶
compass_labyrinth.compass.level_1.momentuPY.viterbi
¶
Source code in src/compass_labyrinth/compass/level_1/momentuPY.py
Probability Distributions¶
logpdf_gamma¶
compass_labyrinth.compass.level_1.momentuPY.logpdf_gamma
¶
Source code in src/compass_labyrinth/compass/level_1/momentuPY.py
logpdf_vonmises¶
compass_labyrinth.compass.level_1.momentuPY.logpdf_vonmises
¶
Source code in src/compass_labyrinth/compass/level_1/momentuPY.py
Utilities¶
print_hmm_summary¶
compass_labyrinth.compass.level_1.momentuPY.print_hmm_summary
¶
Source code in src/compass_labyrinth/compass/level_1/momentuPY.py
Related¶
- Data Preparation - Prepare data for HMM fitting
- Visualization - Visualize HMM results