python-hiddenmarkov 0.3.0__cp311-cp311-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
hiddenmarkov/_hmm.pyi ADDED
@@ -0,0 +1,10 @@
1
+ import numpy as np
2
+
3
+ def viterbi_loop(
4
+ omega: np.ndarray,
5
+ omega_idx: np.ndarray,
6
+ obs_probs: np.ndarray,
7
+ trans_mat: np.ndarray,
8
+ init_probs: np.ndarray,
9
+ log_probabilities: bool,
10
+ ) -> None: ...
hiddenmarkov/_hmm.pyx ADDED
@@ -0,0 +1,90 @@
1
+ # cython: language_level=3
2
+ # cython: boundscheck=False
3
+ # cython: wraparound=False
4
+ # cython: cdivision=True
5
+ """Cython-accelerated inner loop for the Viterbi algorithm.
6
+
7
+ Why Cython helps here but not in the forward step
8
+ --------------------------------------------------
9
+ The Viterbi step needs a row-wise argmax over the N×N matrix
10
+ omega[t-1, i] + log_T[i, j] for all (i, j).
11
+ NumPy handles this by broadcasting a length-N vector over the whole N×N
12
+ transition matrix, creating a fresh N×N temporary allocation at *every*
13
+ time step. The Cython version replaces that with a doubly-nested C loop:
14
+ no temporaries, working set stays in L1/L2 cache for moderate N.
15
+
16
+ The forward step is different: its inner operation is a plain matrix-vector
17
+ multiply (np.dot), which NumPy hands to BLAS. BLAS is already SIMD-
18
+ vectorised and for N ≳ 64 will outperform any pure-C scalar loop. We
19
+ therefore keep the forward step in NumPy.
20
+ """
21
+
22
+ import numpy as np
23
+ cimport numpy as np
24
+ from libc.math cimport INFINITY
25
+
26
+ np.import_array()
27
+
28
+
29
+ def viterbi_loop(
30
+ double[:, ::1] omega,
31
+ np.int64_t[:, ::1] omega_idx,
32
+ const double[:, ::1] obs_probs,
33
+ const double[:, ::1] trans_mat,
34
+ const double[:] init_probs,
35
+ bint log_probabilities,
36
+ ):
37
+ """Fill the Viterbi trellis on pre-computed probability arrays.
38
+
39
+ The caller is responsible for pre-computing *obs_probs* and *trans_mat*
40
+ from the HMM's Python model objects, and for the backtracking step
41
+ after this function returns.
42
+
43
+ Parameters
44
+ ----------
45
+ omega : (T, N) float64, written in-place
46
+ Viterbi trellis.
47
+ omega_idx : (T, N) int64, written in-place
48
+ Backtracking table; omega_idx[t, j] = best predecessor state for
49
+ state j at time t.
50
+ obs_probs : (T, N) float64
51
+ Observation (log-)probabilities for every time step and state.
52
+ trans_mat : (N, N) float64
53
+ Transition (log-)probabilities; trans_mat[i, j] = P(j | i).
54
+ init_probs : (N,) float64
55
+ Initial state (log-)probabilities.
56
+ log_probabilities : bool
57
+ When True all arrays contain log-probabilities and additions replace
58
+ multiplications; when False they contain raw probabilities.
59
+ """
60
+ cdef Py_ssize_t T = obs_probs.shape[0]
61
+ cdef Py_ssize_t N = obs_probs.shape[1]
62
+ cdef Py_ssize_t t, i, j
63
+ cdef double best_val, val
64
+
65
+ if log_probabilities:
66
+ for j in range(N):
67
+ omega[0, j] = obs_probs[0, j] + init_probs[j]
68
+ for t in range(1, T):
69
+ for j in range(N):
70
+ best_val = -INFINITY
71
+ omega_idx[t, j] = 0
72
+ for i in range(N):
73
+ val = omega[t - 1, i] + trans_mat[i, j]
74
+ if val > best_val:
75
+ best_val = val
76
+ omega_idx[t, j] = i
77
+ omega[t, j] = obs_probs[t, j] + best_val
78
+ else:
79
+ for j in range(N):
80
+ omega[0, j] = obs_probs[0, j] * init_probs[j]
81
+ for t in range(1, T):
82
+ for j in range(N):
83
+ best_val = 0.0
84
+ omega_idx[t, j] = 0
85
+ for i in range(N):
86
+ val = omega[t - 1, i] * trans_mat[i, j]
87
+ if val > best_val:
88
+ best_val = val
89
+ omega_idx[t, j] = i
90
+ omega[t, j] = obs_probs[t, j] * best_val
hiddenmarkov/py.typed ADDED
File without changes
hiddenmarkov/utils.py ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Utility functions
5
+ """
6
+ from __future__ import annotations
7
+
8
+ from typing import Any, Callable, List, Tuple
9
+
10
+ import numpy as np
11
+
12
+
13
+ def inverted_softmax(array: np.ndarray) -> np.ndarray:
14
+ """Convert distances to probabilities: lower distance → higher probability."""
15
+ e = np.exp(-array)
16
+ return e / e.sum()
17
+
18
+
19
+ def softmax(array: np.ndarray) -> np.ndarray:
20
+ """Convert scores to probabilities: higher score → higher probability."""
21
+ e = np.exp(array)
22
+ return e / e.sum()
23
+
24
+
25
+ def create_prob_models(
26
+ no_global_states: int = 100,
27
+ window_size: int = 5,
28
+ ) -> List[List[Tuple[Callable[..., Any], int]]]:
29
+ """
30
+ Build example windowed probability models for use with
31
+ :class:`~hiddenmarkov.WindowedObservationModel` and
32
+ :func:`~hiddenmarkov.viterbi_algorithm_windowed`.
33
+
34
+ Parameters
35
+ ----------
36
+ no_global_states : int
37
+ Total number of global states in the HMM.
38
+ window_size : int
39
+ Number of states visible at any one time.
40
+
41
+ Returns
42
+ -------
43
+ list of lists of (callable, int) pairs
44
+ ``result[i]`` is the window of ``(prob_model, global_index)`` pairs
45
+ centred at global state ``i``.
46
+ """
47
+ models_per_state: List[List[Tuple[Callable[..., Any], int]]] = []
48
+ for i in range(no_global_states):
49
+ models: List[Tuple[Callable[..., Any], int]] = []
50
+ for j in range(window_size):
51
+ def probf(obs: Any, ref: int = i + j) -> int:
52
+ return int(obs == ref)
53
+ models.append((probf, max(0, min(no_global_states - 1, i + j))))
54
+ models_per_state.append(models)
55
+ return models_per_state
@@ -0,0 +1,84 @@
1
+ Metadata-Version: 2.4
2
+ Name: python-hiddenmarkov
3
+ Version: 0.3.0
4
+ Summary: A simple Python package for Hidden Markov Models
5
+ Author: Silvan Peter
6
+ Author-email: Carlos Cancino-Chacón <carloscancinochacon@gmail.com>
7
+ License: MIT
8
+ Project-URL: Homepage, https://github.com/CarlosCancino-Chacon/hiddenmarkov
9
+ Keywords: hmm,hidden_markov_models
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: Implementation :: CPython
14
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
15
+ Requires-Python: >=3.10
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Requires-Dist: numpy>=1.24
19
+ Requires-Dist: scipy>=1.7
20
+ Dynamic: license-file
21
+
22
+ # hiddenmarkov
23
+
24
+ [![PyPI](https://img.shields.io/pypi/v/python-hiddenmarkov)](https://pypi.org/project/python-hiddenmarkov/)
25
+ [![CI](https://github.com/CarlosCancino-Chacon/hiddenmarkov/actions/workflows/ci.yml/badge.svg)](https://github.com/CarlosCancino-Chacon/hiddenmarkov/actions/workflows/ci.yml)
26
+ [![codecov](https://codecov.io/gh/CarlosCancino-Chacon/hiddenmarkov/branch/main/graph/badge.svg)](https://codecov.io/gh/CarlosCancino-Chacon/hiddenmarkov)
27
+
28
+ A simple Python library for Hidden Markov Models.
29
+
30
+ This library was created for educational purposes and does not aim to have the most efficient implementation.
31
+
32
+ ## Setup
33
+
34
+ Simple installation
35
+
36
+ ```
37
+ pip install python-hiddenmarkov
38
+ ```
39
+
40
+ Develop version
41
+ ```bash
42
+ git clone https://github.com/CarlosCancino-Chacon/hiddenmarkov.git
43
+ cd hiddenmarkov
44
+ pip install -e .
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ Example from [Wikipedia](https://en.wikipedia.org/wiki/Viterbi_algorithm#Example)
50
+
51
+ ```python
52
+ import numpy as np
53
+ from hiddenmarkov import CategoricalObservationModel, ConstantTransitionModel, HMM
54
+
55
+ obs = ("normal", "cold", "dizzy")
56
+ observations = ("normal", "cold", "dizzy")
57
+ states = ("Healthy", "Fever")
58
+ observation_probabilities = np.array([[0.5, 0.1], [0.4, 0.3], [0.1, 0.6]])
59
+ transition_probabilities = np.array([[0.7, 0.3], [0.4, 0.6]])
60
+
61
+ observation_model = CategoricalObservationModel(
62
+ observation_probabilities, obs
63
+ )
64
+
65
+ init_distribution = np.array([0.6, 0.4])
66
+
67
+ transition_model = ConstantTransitionModel(
68
+ transition_probabilities, init_distribution
69
+ )
70
+
71
+ hmm = HMM(observation_model, transition_model, state_space=states)
72
+
73
+ path, prob = hmm.find_best_sequence(observations, log_probabilities=False)
74
+ print("Example Wikipedia")
75
+ print("Best sequence", path)
76
+ print("Expected Sequence", ["Healthy", "Healthy", "Fever"])
77
+ print("Sequence probability", prob)
78
+ print("Expected probability", 0.01512)
79
+ ```
80
+
81
+ ## Licence
82
+
83
+ The code in this package is licensed under the MIT Licence. For details,
84
+ please see the [LICENSE](https://github.com/CarlosCancino-Chacon/hiddenmarkov/blob/main/LICENSE) file.
@@ -0,0 +1,12 @@
1
+ hiddenmarkov/__init__.py,sha256=bqUzorpLu3mqL4v_Hv85VPLtme2qyNdOFvkzYYUD0Jo,31952
2
+ hiddenmarkov/_hmm.c,sha256=quauSfFddD-4v0ppGK2quqfsmvTZBDIRKkbM6a-xbGE,1239791
3
+ hiddenmarkov/_hmm.cp311-win_amd64.pyd,sha256=88mhnw7VepcGXlkTBbQzGkQxW4-VgBj91j9dcSBAzXE,142848
4
+ hiddenmarkov/_hmm.pyi,sha256=9AZfiDMoLRemrjIJ0jShItanFYb0jCJFz3LHYI0CNoI,224
5
+ hiddenmarkov/_hmm.pyx,sha256=rFwia_YlCzOGU2Ju6dlPTQo5165b9lrf9J-BxBSDA2U,3440
6
+ hiddenmarkov/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ hiddenmarkov/utils.py,sha256=t0dcXzlkxmEYGyxsrCDJzIq8WnfoAOumJo0n-LF9hQc,1717
8
+ python_hiddenmarkov-0.3.0.dist-info/licenses/LICENSE,sha256=aKW387ClCasMelXOka-SlIQnfJkO0sHLZ3LZABEfM8s,1122
9
+ python_hiddenmarkov-0.3.0.dist-info/METADATA,sha256=2dCAGU2aCOO9WowZWAtViimH9Qy85RDm_YS6nqUIO2U,2879
10
+ python_hiddenmarkov-0.3.0.dist-info/WHEEL,sha256=Gp86XeEC8ZvwAF0N5UXenlzFa5hMKrX8iP-Ck-9cwsI,101
11
+ python_hiddenmarkov-0.3.0.dist-info/top_level.txt,sha256=8arGHdwpV-edtmp5LcS3xiOeslDQY0yFYL84WjAAxI4,30
12
+ python_hiddenmarkov-0.3.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: false
4
+ Tag: cp311-cp311-win_amd64
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Carlos Eduardo Cancino-Chacón, Silvan Peter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,3 @@
1
+ build
2
+ hiddenmarkov
3
+ wheelhouse