HMM Training¶
Parameters and training modes for the V3 gap-aware HMM. For a conceptual guide see HMM Training Modes.
Parameters¶
HMMParams
dataclass
¶
HMMParams(
mode: Literal[
"unsupervised", "semi_supervised", "supervised"
] = "unsupervised",
n_states: int = 3,
p_stay_per_base: float = 0.92,
init_prob: NDArray[float64] = (
lambda: array([0.98, 0.01, 0.01], dtype=float64)
)(),
emission_transform: (
EmissionCalibrator | EmissionKDE | None
) = None,
unmod_emission_beta: tuple[float, float] = (2.0, 8.0),
flank_emission_beta: tuple[float, float] = (3.0, 3.0),
mod_emission_beta: tuple[float, float] = (8.0, 2.0),
training_species: list[str] = list(),
n_training_positions: int = 0,
n_training_reads: int = 0,
)
Learned or default HMM parameters for V3.
All fields have defaults so the dataclass can be constructed
incrementally or via :func:create_unsupervised_params.
The n_states field controls HMM topology:
- n_states=2: Unmodified / Modified (original behaviour).
- n_states=3: Unmodified / Flank / Modified. The Flank state
absorbs the ±2-base signal halo around modification sites so that
only true modification positions contribute to p_mod_hmm.
unmod_emission_beta
class-attribute
instance-attribute
¶
Beta(2, 8) — mean ≈ 0.2, concentrates on low kNN scores.
flank_emission_beta
class-attribute
instance-attribute
¶
Beta(3, 3) — mean = 0.5, symmetric for moderate kNN scores.
mod_emission_beta
class-attribute
instance-attribute
¶
Beta(8, 2) — mean ≈ 0.8, concentrates on high kNN scores.
EmissionCalibrator
dataclass
¶
Platt-scaling calibrator for V2 → V3 emission mapping (Mode B).
Transforms raw p_mod via sigmoid: σ(a·x + b).
transform
¶
Map raw P(mod) to calibrated P(mod).
EmissionKDE
dataclass
¶
EmissionKDE(
grid: NDArray[float64],
density_unmod: NDArray[float64],
density_mod: NDArray[float64],
)
KDE-based emission likelihood model (Mode C).
Stores two pre-evaluated density curves on a fixed grid:
P(p_mod_raw | unmodified) and P(p_mod_raw | modified).
At inference time, :meth:emission_probs returns per-observation
likelihoods via linear interpolation on the grid.
emission_probs
¶
Return (P(obs|unmod), P(obs|mod)) via interpolation.
Values are clamped to [1e-10, ∞) to avoid log(0) issues.
Source code in baleen/eventalign/_hmm_training.py
Training modes¶
create_unsupervised_params
¶
create_unsupervised_params(n_states: int = 3) -> HMMParams
Build default (unsupervised) HMM parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_states
|
int
|
Number of HMM states. |
3
|
Source code in baleen/eventalign/_hmm_training.py
train_semi_supervised
¶
train_semi_supervised(
training_data: dict[str, ContigModificationResult],
labels: dict[tuple[str, int], bool],
*,
species_name: str = "",
species_names: list[str] | None = None,
learn_transitions: bool = True,
emission_source: str = "p_mod_raw",
n_states: int = 2
) -> HMMParams
Train Mode B (semi-supervised) HMM parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
training_data
|
dict[str, ContigModificationResult]
|
|
required |
labels
|
dict[tuple[str, int], bool]
|
|
required |
species_name
|
str
|
Optional single species tag stored in metadata. |
''
|
species_names
|
list[str] | None
|
Optional list of species names for multi-organism pooling.
Takes precedence over |
None
|
learn_transitions
|
bool
|
If True (default), learn |
True
|
n_states
|
int
|
Number of HMM states. |
2
|
Returns:
| Type | Description |
|---|---|
HMMParams
|
With Platt-calibrated emission transform, learned |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 20 labeled positions are provided, or fewer than 10 positive / 10 negative labels. |
Source code in baleen/eventalign/_hmm_training.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
train_supervised
¶
train_supervised(
training_data: dict[str, ContigModificationResult],
labels: dict[tuple[str, int], bool],
*,
species_name: str = "",
kde_n_bins: int = 200,
kde_bandwidth: float | None = None,
emission_source: str = "p_mod_raw",
n_states: int = 2
) -> HMMParams
Train Mode C (fully supervised) HMM parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
training_data
|
dict[str, ContigModificationResult]
|
|
required |
labels
|
dict[tuple[str, int], bool]
|
|
required |
species_name
|
str
|
Optional species tag. |
''
|
kde_n_bins
|
int
|
Number of evaluation points for KDE grid. |
200
|
kde_bandwidth
|
float | None
|
Explicit bandwidth for KDE; |
None
|
n_states
|
int
|
Number of HMM states. |
2
|
Returns:
| Type | Description |
|---|---|
HMMParams
|
With MLE transition, KDE emission model, and learned |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 50 labeled positions or fewer than 3 contigs. |
Source code in baleen/eventalign/_hmm_training.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 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 609 610 611 612 | |
Labels & cross-validation¶
labels_from_known_modifications
¶
labels_from_known_modifications(
known_mods: dict[tuple[str, int], tuple[str, str]],
contig_results: dict[str, ContigModificationResult],
*,
position_offset: int = 3,
auto_negatives: bool = True,
min_coverage: int = 5
) -> dict[tuple[str, int], bool]
Convert known biological modification sites to training labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
known_mods
|
dict[tuple[str, int], tuple[str, str]]
|
|
required |
contig_results
|
dict[str, ContigModificationResult]
|
|
required |
position_offset
|
int
|
|
3
|
auto_negatives
|
bool
|
If True, positions with |
True
|
min_coverage
|
int
|
Minimum total read coverage for auto-negative positions. |
5
|
Returns:
| Type | Description |
|---|---|
labels
|
|
Source code in baleen/eventalign/_hmm_training.py
cross_validate_hmm
¶
cross_validate_hmm(
contig_results: dict[str, ContigResult],
labels: dict[tuple[str, int], bool],
mode: Literal["semi_supervised", "supervised"],
*,
cv_strategy: Literal[
"leave_one_contig_out", "kfold"
] = "leave_one_contig_out",
k: int = 5,
emission_source: str = "p_mod_raw",
**hierarchical_kwargs
) -> CVResult
Cross-validate HMM training to detect overfitting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contig_results
|
dict[str, ContigResult]
|
Raw pipeline output per contig ( |
required |
labels
|
dict[tuple[str, int], bool]
|
|
required |
mode
|
Literal['semi_supervised', 'supervised']
|
Training mode to evaluate ( |
required |
cv_strategy
|
Literal['leave_one_contig_out', 'kfold']
|
|
'leave_one_contig_out'
|
k
|
int
|
Number of folds for k-fold CV. |
5
|
**hierarchical_kwargs
|
Forwarded to
:func: |
{}
|
Returns:
| Type | Description |
|---|---|
CVResult
|
|
Source code in baleen/eventalign/_hmm_training.py
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 | |
CVResult
dataclass
¶
CVResult(
per_fold_auroc: list[float],
per_fold_auprc: list[float],
mean_auroc: float,
mean_auprc: float,
std_auroc: float,
std_auprc: float,
fold_details: list[dict[str, Any]],
)
Cross-validation results.
Persistence¶
save_hmm_params
¶
save_hmm_params(
params: HMMParams, path: str | Path
) -> None
Serialize trained HMM parameters to JSON.
Source code in baleen/eventalign/_hmm_training.py
load_hmm_params
¶
load_hmm_params(path: str | Path) -> HMMParams
Load previously trained HMM parameters from JSON.
Backward-compatible: files without n_states default to 2-state.