python-hiddenmarkov 0.1.2__tar.gz → 0.1.4__tar.gz

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.
@@ -1,27 +1,31 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: python-hiddenmarkov
3
- Version: 0.1.2
3
+ Version: 0.1.4
4
4
  Summary: A simple Python package for Hidden Markov Models
5
- Home-page: https://github.com/neosatrapahereje/hiddenmarkov
6
- Author: Carlos Cancino-Chacón, Silvan Peter
7
- Author-email: carloscancinochacon@gmail.com
5
+ Author: Silvan Peter
6
+ Author-email: Carlos Cancino-Chacón <carloscancinochacon@gmail.com>
8
7
  License: MIT
9
- Keywords: hmm hidden_Markov_models
8
+ Project-URL: Homepage, https://github.com/neosatrapahereje/hiddenmarkov
9
+ Keywords: hmm,hidden_markov_models
10
10
  Classifier: License :: OSI Approved :: MIT License
11
11
  Classifier: Programming Language :: Python
12
12
  Classifier: Programming Language :: Python :: 3
13
13
  Classifier: Programming Language :: Python :: Implementation :: CPython
14
14
  Classifier: Programming Language :: Python :: Implementation :: PyPy
15
- Requires-Python: >=3.6
15
+ Requires-Python: >=3.8
16
16
  Description-Content-Type: text/markdown
17
17
  License-File: LICENSE
18
-
18
+ Requires-Dist: numpy
19
+ Requires-Dist: scipy
20
+ Dynamic: license-file
19
21
 
20
22
  # hiddenmarkov
21
23
 
24
+ [![PyPI](https://img.shields.io/pypi/v/python-hiddenmarkov)](https://pypi.org/project/python-hiddenmarkov/)
25
+
22
26
  A simple Python library for Hidden Markov Models.
23
27
 
24
- This library was created mostly for educational purposes and does not aim to have the most efficient implementation.
28
+ This library was created for educational purposes and does not aim to have the most efficient implementation.
25
29
 
26
30
  ## Setup
27
31
 
@@ -43,13 +47,16 @@ pip install -e .
43
47
  Example from [Wikipedia](https://en.wikipedia.org/wiki/Viterbi_algorithm#Example)
44
48
 
45
49
  ```python
50
+ import numpy as np
51
+ from hiddenmarkov import CategoricalObservationModel, ConstantTransitionModel, HMM
52
+
46
53
  obs = ("normal", "cold", "dizzy")
47
54
  observations = ("normal", "cold", "dizzy")
48
55
  states = ("Healthy", "Fever")
49
56
  observation_probabilities = np.array([[0.5, 0.1], [0.4, 0.3], [0.1, 0.6]])
50
57
  transition_probabilities = np.array([[0.7, 0.3], [0.4, 0.6]])
51
58
 
52
- observation_model = CategoricalStringObservationModel(
59
+ observation_model = CategoricalObservationModel(
53
60
  observation_probabilities, obs
54
61
  )
55
62
 
@@ -61,11 +68,12 @@ transition_model = ConstantTransitionModel(
61
68
 
62
69
  hmm = HMM(observation_model, transition_model, state_space=states)
63
70
 
64
- path, prob = viterbi_algorithm(hmm, observations, log_probabilities=False)
71
+ path, prob = hmm.find_best_sequence(observations, log_probabilities=False)
65
72
  print("Example Wikipedia")
66
73
  print("Best sequence", path)
67
74
  print("Expected Sequence", ["Healthy", "Healthy", "Fever"])
68
75
  print("Sequence probability", prob)
76
+ print("Expected probability", 0.01512)
69
77
  ```
70
78
 
71
79
  ## Licence
@@ -1,8 +1,10 @@
1
1
  # hiddenmarkov
2
2
 
3
+ [![PyPI](https://img.shields.io/pypi/v/python-hiddenmarkov)](https://pypi.org/project/python-hiddenmarkov/)
4
+
3
5
  A simple Python library for Hidden Markov Models.
4
6
 
5
- This library was created mostly for educational purposes and does not aim to have the most efficient implementation.
7
+ This library was created for educational purposes and does not aim to have the most efficient implementation.
6
8
 
7
9
  ## Setup
8
10
 
@@ -24,13 +26,16 @@ pip install -e .
24
26
  Example from [Wikipedia](https://en.wikipedia.org/wiki/Viterbi_algorithm#Example)
25
27
 
26
28
  ```python
29
+ import numpy as np
30
+ from hiddenmarkov import CategoricalObservationModel, ConstantTransitionModel, HMM
31
+
27
32
  obs = ("normal", "cold", "dizzy")
28
33
  observations = ("normal", "cold", "dizzy")
29
34
  states = ("Healthy", "Fever")
30
35
  observation_probabilities = np.array([[0.5, 0.1], [0.4, 0.3], [0.1, 0.6]])
31
36
  transition_probabilities = np.array([[0.7, 0.3], [0.4, 0.6]])
32
37
 
33
- observation_model = CategoricalStringObservationModel(
38
+ observation_model = CategoricalObservationModel(
34
39
  observation_probabilities, obs
35
40
  )
36
41
 
@@ -42,11 +47,12 @@ transition_model = ConstantTransitionModel(
42
47
 
43
48
  hmm = HMM(observation_model, transition_model, state_space=states)
44
49
 
45
- path, prob = viterbi_algorithm(hmm, observations, log_probabilities=False)
50
+ path, prob = hmm.find_best_sequence(observations, log_probabilities=False)
46
51
  print("Example Wikipedia")
47
52
  print("Best sequence", path)
48
53
  print("Expected Sequence", ["Healthy", "Healthy", "Fever"])
49
54
  print("Sequence probability", prob)
55
+ print("Expected probability", 0.01512)
50
56
  ```
51
57
 
52
58
  ## Licence
@@ -1,15 +1,16 @@
1
+ #!/usr/bin/env python
1
2
  # -*- coding: utf-8 -*-
2
3
  """
3
4
  Hidden Markov Models
4
5
 
5
6
  This module contains case classes to define Hidden Markov Models.
6
7
  """
7
- import pkg_resources
8
8
  import warnings
9
9
  import numpy as np
10
+ from importlib.metadata import version, PackageNotFoundError
10
11
 
11
12
  from collections import defaultdict
12
- from typing import Any, Iterable, Optional, Tuple, ClassVar
13
+ from typing import Any, Iterable, Optional, Tuple, ClassVar, List
13
14
 
14
15
  from scipy.special import logsumexp
15
16
 
@@ -17,7 +18,10 @@ from .utils import inverted_softmax, softmax
17
18
 
18
19
 
19
20
  # define a version variable
20
- __version__ = pkg_resources.get_distribution("python-hiddenmarkov").version
21
+ try:
22
+ __version__ = version("python-hiddenmarkov")
23
+ except PackageNotFoundError:
24
+ __version__ = "unknown"
21
25
 
22
26
 
23
27
  class TransitionModel(object):
@@ -31,7 +35,13 @@ class TransitionModel(object):
31
35
  def __init__(self, use_log_probabilities: bool = True) -> None:
32
36
  self.use_log_probabilities: bool = use_log_probabilities
33
37
 
34
- def __call__(self, i=None, j=None, *args, **kwargs):
38
+ def __call__(
39
+ self,
40
+ i: Optional[int] = None,
41
+ j: Optional[int] = None,
42
+ *args,
43
+ **kwargs,
44
+ ) -> np.ndarray:
35
45
  raise NotImplementedError
36
46
 
37
47
 
@@ -69,9 +79,6 @@ class HiddenMarkovModel(object):
69
79
  Number of states
70
80
  state_space: np.array
71
81
 
72
- TODO
73
- ----
74
- * Add forward algorithm
75
82
  """
76
83
 
77
84
  def __init__(
@@ -93,10 +100,31 @@ class HiddenMarkovModel(object):
93
100
 
94
101
  def find_best_sequence(
95
102
  self,
96
- observations: Any,
103
+ observations: Iterable[Any],
97
104
  log_probabilities: bool = True,
98
105
  viterbi: str = "optimized",
99
106
  ) -> Tuple[np.ndarray, float]:
107
+ """
108
+ Find the best sequence of hidden states given a sequence of
109
+ observations using the viterbi algorithm
110
+
111
+ Parameters
112
+ ----------
113
+ observations : Iterable
114
+ The sequence of observations.
115
+ log_probabilities : bool
116
+ If True, log probabilities will be used.
117
+ viterbi : {"optimized", "windowed", "naive"}
118
+ The implementation of the viterbi algorithm.
119
+
120
+ Returns
121
+ -------
122
+ best_sequence : np.ndarray
123
+ The best sequence of hidden states
124
+ sequence_likelihood : float
125
+ The probability (or log probability if `log_probabilities=True`)
126
+ of the sequence of observations.
127
+ """
100
128
  if viterbi == "optimized":
101
129
  viterbi_fun = viterbi_algorithm
102
130
  elif viterbi == "windowed":
@@ -104,15 +132,40 @@ class HiddenMarkovModel(object):
104
132
  elif viterbi == "naive":
105
133
  viterbi_fun = viterbi_algorithm_naive
106
134
  else:
107
- warnings.warn("viterbi needs to be 'optimized', 'windowed', or 'naive'")
108
- return
135
+ raise ValueError(
136
+ "`viterbi` needs to be 'optimized', 'windowed', or 'naive' "
137
+ f"but is {viterbi}"
138
+ )
139
+ # warnings.warn("viterbi needs to be 'optimized', 'windowed', or 'naive'")
140
+ # return
109
141
  best_sequence, sequence_likelihood = viterbi_fun(
110
- hmm=self, observations=observations, log_probabilities=log_probabilities
142
+ hmm=self,
143
+ observations=observations,
144
+ log_probabilities=log_probabilities,
111
145
  )
112
146
  return best_sequence, sequence_likelihood
113
147
 
114
- def forward_algorithm_step(self, observation, log_probabilities=False):
115
-
148
+ def forward_algorithm_step(
149
+ self,
150
+ observation: Any,
151
+ log_probabilities: bool = False,
152
+ ) -> int:
153
+ """
154
+ Find the hidden state that has the maximal probability
155
+ given the current observation.
156
+
157
+ Parameters
158
+ ----------
159
+ observation : Any
160
+ The current observation
161
+ log_probabilities: bool
162
+ If True, log probabilities will be used.
163
+
164
+ Returns
165
+ -------
166
+ current_state : int
167
+ Index of the current hidden state.
168
+ """
116
169
  self.forward_variable = forward_algorithm_step(
117
170
  observation_model=self.observation_model,
118
171
  transition_model=self.transition_model,
@@ -225,7 +278,11 @@ def viterbi_algorithm_naive(
225
278
  return best_sequence, path_likelihood
226
279
 
227
280
 
228
- def viterbi_algorithm(hmm, observations, log_probabilities=True):
281
+ def viterbi_algorithm(
282
+ hmm: HiddenMarkovModel,
283
+ observations: Iterable[Any],
284
+ log_probabilities: bool = True,
285
+ ) -> Tuple[np.ndarray, float]:
229
286
  """
230
287
  Find the most probable sequence of latent variables given
231
288
  a sequence of observations
@@ -309,7 +366,11 @@ def viterbi_algorithm(hmm, observations, log_probabilities=True):
309
366
  return best_sequence, path_likelihood
310
367
 
311
368
 
312
- def viterbi_algorithm_windowed(hmm, observations, log_probabilities=True):
369
+ def viterbi_algorithm_windowed(
370
+ hmm: HiddenMarkovModel,
371
+ observations: Iterable[Any],
372
+ log_probabilities: bool = True,
373
+ ) -> Tuple[np.ndarray, float]:
313
374
  """
314
375
  Find the most probable sequence of latent variables given
315
376
  a sequence of observations.
@@ -356,7 +417,8 @@ def viterbi_algorithm_windowed(hmm, observations, log_probabilities=True):
356
417
  omega_idx = np.zeros((len(observations), hmm.n_states), dtype=int)
357
418
 
358
419
  # Initialize vector for holding the current best idx
359
- # Current window id; start at state 0 and keep the state at the start of the current window
420
+ # Current window id; start at state 0 and keep the state at the
421
+ # start of the current window
360
422
  current_window_idx = np.zeros((len(observations) + 1), dtype=int)
361
423
 
362
424
  omega_idx[0, :] = 0
@@ -429,7 +491,28 @@ def forward_algorithm_step(
429
491
  log_probabilities: bool = False,
430
492
  ) -> np.ndarray:
431
493
  """
432
- Step of the forward algorithm
494
+ Step of the forward algorithm.
495
+
496
+ Parameters
497
+ ----------
498
+ observation_model : ObservationModel
499
+ An observation model.
500
+ transition_model: TransitionModel
501
+ A transition model.
502
+ observation: Any
503
+ An observation. The observation needs to be of the same
504
+ type as specified by the observation model.
505
+ forward_variable : np.ndarray
506
+ The forward variable of the HMM. If none is given, the
507
+ initial probabilities specified by the transition model
508
+ will be used.
509
+ log_probabilities : bool
510
+ If true, use log probabilities.
511
+
512
+ Returns
513
+ -------
514
+ forward_variable : np.ndarray
515
+ The updated forward variable.
433
516
  """
434
517
  # since computing the log of the matrix vector multiplication is not
435
518
  # trivial (TODO: see if there is a clever way to do this multiplication
@@ -469,22 +552,22 @@ class ConstantTransitionModel(TransitionModel):
469
552
  init_probabilities: np.ndarray or None (optional)
470
553
  A 1D vector of length n_states defining the initial
471
554
  probabilities of each state
472
- normalize_init_probabilities: Bool (optional)
555
+ normalize_init_probabilities: bool (optional)
473
556
  If True, the initial distribution will be normalized.
474
557
  Default is False.
475
- use_log_probabilities: Bool (optional)
558
+ use_log_probabilities: bool (optional)
476
559
  If True, use log proabilities instead of norm proabilities
477
560
  (better for numerical stability)
478
561
  """
479
562
 
480
563
  def __init__(
481
564
  self,
482
- transition_probabilities,
483
- init_probabilities=None,
484
- normalize_init_probabilities=False,
485
- normalize_transition_probabilities=False,
486
- use_log_probabilities=True,
487
- ):
565
+ transition_probabilities: np.ndarray,
566
+ init_probabilities: Optional[np.ndarray] = None,
567
+ normalize_init_probabilities: bool = False,
568
+ normalize_transition_probabilities: bool = False,
569
+ use_log_probabilities: bool = True,
570
+ ) -> None:
488
571
  super().__init__(use_log_probabilities=use_log_probabilities)
489
572
  self.transition_probabilities = transition_probabilities
490
573
  self.n_states = len(transition_probabilities)
@@ -508,30 +591,30 @@ class ConstantTransitionModel(TransitionModel):
508
591
  )
509
592
 
510
593
  @property
511
- def init_probabilities(self):
594
+ def init_probabilities(self) -> np.ndarray:
512
595
  if self.use_log_probabilities:
513
596
  return self._log_init_dist
514
597
  else:
515
598
  return self._init_dist
516
599
 
517
600
  @init_probabilities.setter
518
- def init_probabilities(self, init_probabilities):
601
+ def init_probabilities(self, init_probabilities: np.ndarray) -> None:
519
602
  self._init_dist = init_probabilities
520
603
  self._log_init_dist = np.log(self._init_dist)
521
604
 
522
605
  @property
523
- def transition_probabilities(self):
606
+ def transition_probabilities(self) -> np.ndarray:
524
607
  if self.use_log_probabilities:
525
608
  return self._log_transition_prob
526
609
  else:
527
610
  return self._transition_prob
528
611
 
529
612
  @transition_probabilities.setter
530
- def transition_probabilities(self, transition_probabilities):
613
+ def transition_probabilities(self, transition_probabilities) -> None:
531
614
  self._transition_prob = transition_probabilities
532
615
  self._log_transition_prob = np.log(self._transition_prob)
533
616
 
534
- def __call__(self, i=None, j=None):
617
+ def __call__(self, i=None, j=None) -> np.ndarray:
535
618
  if i is None and j is None:
536
619
  return self.transition_probabilities
537
620
  elif i is not None and j is None:
@@ -542,12 +625,25 @@ class ConstantTransitionModel(TransitionModel):
542
625
  return self.transition_probabilities[i, j]
543
626
 
544
627
 
545
- class CategoricalStringObservationModel(ObservationModel):
628
+ class CategoricalObservationModel(ObservationModel):
629
+ """
630
+ A Categorical observation model.
631
+
632
+ Parameters
633
+ ----------
634
+ observation_probabilities : np.ndarray
635
+ A table of probabilities for each observation in each state.
636
+ observations : Iterable
637
+ A list of the observations
638
+ use_log_probabilities : bool
639
+ If True, use log probabilities.
640
+ """
641
+
546
642
  def __init__(
547
643
  self,
548
- observation_probabilities,
549
- observations=None,
550
- use_log_probabilities=True,
644
+ observation_probabilities: np.ndarray,
645
+ observations: Iterable[Any] = None,
646
+ use_log_probabilities: bool = True,
551
647
  ):
552
648
  super().__init__(use_log_probabilities=use_log_probabilities)
553
649
 
@@ -558,6 +654,10 @@ class CategoricalStringObservationModel(ObservationModel):
558
654
  else:
559
655
  self.observations = [str(i) for i in range(len(observation_probabilities))]
560
656
 
657
+ self.observation_indices = dict(
658
+ [(obs, i) for obs, i in zip(self.observations, range(len(observations)))]
659
+ )
660
+
561
661
  @property
562
662
  def observation_probabilities(self):
563
663
  if self.use_log_probabilities:
@@ -571,10 +671,15 @@ class CategoricalStringObservationModel(ObservationModel):
571
671
  self._log_obs_prob = np.log(self._obs_prob)
572
672
 
573
673
  def __call__(self, observation, *args, **kwargs):
574
- idx = self.observations.index(observation)
674
+ idx = self.observation_indices[observation]
675
+ # idx = self.observations.index(observation)
575
676
  return self.observation_probabilities[idx]
576
677
 
577
678
 
679
+ # deprecated alias
680
+ CategoricalStringObservationModel = CategoricalObservationModel
681
+
682
+
578
683
  class WindowedObservationModel(ObservationModel):
579
684
  """
580
685
  Windowed Observation Model
@@ -597,7 +702,10 @@ class WindowedObservationModel(ObservationModel):
597
702
  """
598
703
 
599
704
  def __init__(
600
- self, prob_models_at_state, use_inverted_probs=True, use_log_probabilities=True
705
+ self,
706
+ prob_models_at_state: List[tuple],
707
+ use_inverted_probs: bool = True,
708
+ use_log_probabilities: bool = True,
601
709
  ):
602
710
  super().__init__(use_log_probabilities=use_log_probabilities)
603
711
  self.use_log_probabilities = use_log_probabilities
@@ -1,3 +1,4 @@
1
+ #!/usr/bin/env python
1
2
  # -*- coding: utf-8 -*-
2
3
  """
3
4
  Utility functions
@@ -0,0 +1,33 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "python-hiddenmarkov"
7
+ version = "0.1.4"
8
+ description = "A simple Python package for Hidden Markov Models"
9
+ readme = "README.md"
10
+ license = { text = "MIT" }
11
+ authors = [
12
+ { name = "Carlos Cancino-Chacón", email = "carloscancinochacon@gmail.com" },
13
+ { name = "Silvan Peter" },
14
+ ]
15
+ keywords = ["hmm", "hidden_markov_models"]
16
+ requires-python = ">=3.8"
17
+ dependencies = [
18
+ "numpy",
19
+ "scipy",
20
+ ]
21
+ classifiers = [
22
+ "License :: OSI Approved :: MIT License",
23
+ "Programming Language :: Python",
24
+ "Programming Language :: Python :: 3",
25
+ "Programming Language :: Python :: Implementation :: CPython",
26
+ "Programming Language :: Python :: Implementation :: PyPy",
27
+ ]
28
+
29
+ [project.urls]
30
+ Homepage = "https://github.com/neosatrapahereje/hiddenmarkov"
31
+
32
+ [tool.setuptools.packages.find]
33
+ exclude = ["tests*"]
@@ -1,27 +1,31 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: python-hiddenmarkov
3
- Version: 0.1.2
3
+ Version: 0.1.4
4
4
  Summary: A simple Python package for Hidden Markov Models
5
- Home-page: https://github.com/neosatrapahereje/hiddenmarkov
6
- Author: Carlos Cancino-Chacón, Silvan Peter
7
- Author-email: carloscancinochacon@gmail.com
5
+ Author: Silvan Peter
6
+ Author-email: Carlos Cancino-Chacón <carloscancinochacon@gmail.com>
8
7
  License: MIT
9
- Keywords: hmm hidden_Markov_models
8
+ Project-URL: Homepage, https://github.com/neosatrapahereje/hiddenmarkov
9
+ Keywords: hmm,hidden_markov_models
10
10
  Classifier: License :: OSI Approved :: MIT License
11
11
  Classifier: Programming Language :: Python
12
12
  Classifier: Programming Language :: Python :: 3
13
13
  Classifier: Programming Language :: Python :: Implementation :: CPython
14
14
  Classifier: Programming Language :: Python :: Implementation :: PyPy
15
- Requires-Python: >=3.6
15
+ Requires-Python: >=3.8
16
16
  Description-Content-Type: text/markdown
17
17
  License-File: LICENSE
18
-
18
+ Requires-Dist: numpy
19
+ Requires-Dist: scipy
20
+ Dynamic: license-file
19
21
 
20
22
  # hiddenmarkov
21
23
 
24
+ [![PyPI](https://img.shields.io/pypi/v/python-hiddenmarkov)](https://pypi.org/project/python-hiddenmarkov/)
25
+
22
26
  A simple Python library for Hidden Markov Models.
23
27
 
24
- This library was created mostly for educational purposes and does not aim to have the most efficient implementation.
28
+ This library was created for educational purposes and does not aim to have the most efficient implementation.
25
29
 
26
30
  ## Setup
27
31
 
@@ -43,13 +47,16 @@ pip install -e .
43
47
  Example from [Wikipedia](https://en.wikipedia.org/wiki/Viterbi_algorithm#Example)
44
48
 
45
49
  ```python
50
+ import numpy as np
51
+ from hiddenmarkov import CategoricalObservationModel, ConstantTransitionModel, HMM
52
+
46
53
  obs = ("normal", "cold", "dizzy")
47
54
  observations = ("normal", "cold", "dizzy")
48
55
  states = ("Healthy", "Fever")
49
56
  observation_probabilities = np.array([[0.5, 0.1], [0.4, 0.3], [0.1, 0.6]])
50
57
  transition_probabilities = np.array([[0.7, 0.3], [0.4, 0.6]])
51
58
 
52
- observation_model = CategoricalStringObservationModel(
59
+ observation_model = CategoricalObservationModel(
53
60
  observation_probabilities, obs
54
61
  )
55
62
 
@@ -61,11 +68,12 @@ transition_model = ConstantTransitionModel(
61
68
 
62
69
  hmm = HMM(observation_model, transition_model, state_space=states)
63
70
 
64
- path, prob = viterbi_algorithm(hmm, observations, log_probabilities=False)
71
+ path, prob = hmm.find_best_sequence(observations, log_probabilities=False)
65
72
  print("Example Wikipedia")
66
73
  print("Best sequence", path)
67
74
  print("Expected Sequence", ["Healthy", "Healthy", "Fever"])
68
75
  print("Sequence probability", prob)
76
+ print("Expected probability", 0.01512)
69
77
  ```
70
78
 
71
79
  ## Licence
@@ -1,10 +1,12 @@
1
1
  LICENSE
2
2
  README.md
3
- setup.py
3
+ pyproject.toml
4
4
  hiddenmarkov/__init__.py
5
5
  hiddenmarkov/utils.py
6
6
  python_hiddenmarkov.egg-info/PKG-INFO
7
7
  python_hiddenmarkov.egg-info/SOURCES.txt
8
8
  python_hiddenmarkov.egg-info/dependency_links.txt
9
9
  python_hiddenmarkov.egg-info/requires.txt
10
- python_hiddenmarkov.egg-info/top_level.txt
10
+ python_hiddenmarkov.egg-info/top_level.txt
11
+ tests/test_viterbi.py
12
+ tests/test_window.py
@@ -0,0 +1,161 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Tests for implementations of the viterbi algorithm
5
+ """
6
+
7
+ import unittest
8
+
9
+ import numpy as np
10
+
11
+ from hiddenmarkov import (
12
+ HMM,
13
+ ConstantTransitionModel,
14
+ CategoricalObservationModel,
15
+ viterbi_algorithm,
16
+ viterbi_algorithm_naive,
17
+ )
18
+
19
+
20
+ def wikipedia():
21
+ """
22
+ Example from
23
+ https://en.wikipedia.org/wiki/Viterbi_algorithm#Example
24
+ """
25
+
26
+ obs = ("normal", "cold", "dizzy")
27
+ observations = ("normal", "cold", "dizzy")
28
+ states = ("Healthy", "Fever")
29
+ observation_probabilities = np.array([[0.5, 0.1], [0.4, 0.3], [0.1, 0.6]])
30
+ transition_probabilities = np.array([[0.7, 0.3], [0.4, 0.6]])
31
+
32
+ observation_model = CategoricalObservationModel(
33
+ observation_probabilities, obs
34
+ )
35
+
36
+ init_probabilities = np.array([0.6, 0.4])
37
+
38
+ transition_model = ConstantTransitionModel(
39
+ transition_probabilities, init_probabilities
40
+ )
41
+
42
+ hmm = HMM(observation_model, transition_model, state_space=states)
43
+ expected_prob = 0.01512
44
+ expected_sequence = np.array(["Healthy", "Healthy", "Fever"])
45
+ return hmm, observations, expected_prob, expected_sequence
46
+
47
+
48
+ class TestViterbi(unittest.TestCase):
49
+ def test_viterbi_prob_wikipedia(self):
50
+ hmm, observations, expected_prob, expected_sequence = wikipedia()
51
+ path, prob = viterbi_algorithm(hmm, observations, log_probabilities=False)
52
+ self.assertTrue(np.all(path == expected_sequence))
53
+ self.assertTrue(np.isclose(prob, expected_prob, atol=1e-5))
54
+
55
+ def test_viterbi_log_wikipedia(self):
56
+ hmm, observations, expected_prob, expected_sequence = wikipedia()
57
+ path, prob = viterbi_algorithm(hmm, observations, log_probabilities=True)
58
+ self.assertTrue(np.all(path == expected_sequence))
59
+ self.assertTrue(np.isclose(prob, np.log(expected_prob), atol=1e-5))
60
+
61
+ def test_find_best_sequence_prob_wikipedia(self):
62
+ hmm, observations, expected_prob, expected_sequence = wikipedia()
63
+
64
+ # optimized log prob
65
+ path, prob = hmm.find_best_sequence(
66
+ observations=observations,
67
+ log_probabilities=True,
68
+ viterbi="optimized",
69
+ )
70
+ self.assertTrue(np.all(path == expected_sequence))
71
+ self.assertTrue(np.isclose(prob, np.log(expected_prob), atol=1e-5))
72
+
73
+ # optimized prob
74
+ path, prob = hmm.find_best_sequence(
75
+ observations=observations,
76
+ log_probabilities=False,
77
+ viterbi="optimized",
78
+ )
79
+ self.assertTrue(np.all(path == expected_sequence))
80
+ self.assertTrue(np.isclose(prob, expected_prob, atol=1e-5))
81
+
82
+ # naive log prob
83
+ path, prob = hmm.find_best_sequence(
84
+ observations=observations,
85
+ log_probabilities=True,
86
+ viterbi="naive",
87
+ )
88
+ self.assertTrue(np.all(path == expected_sequence))
89
+ self.assertTrue(np.isclose(prob, np.log(expected_prob), atol=1e-5))
90
+
91
+ # naive prob
92
+ path, prob = hmm.find_best_sequence(
93
+ observations=observations,
94
+ log_probabilities=False,
95
+ viterbi="naive",
96
+ )
97
+ self.assertTrue(np.all(path == expected_sequence))
98
+ self.assertTrue(np.isclose(prob, expected_prob, atol=1e-5))
99
+
100
+ # other (expect an error)
101
+
102
+ try:
103
+ path, prob = hmm.find_best_sequence(
104
+ observations=observations,
105
+ log_probabilities=False,
106
+ viterbi="other",
107
+ )
108
+ # fail the test if this the above code does not
109
+ # raise an error
110
+ self.assertTrue(False)
111
+ except ValueError:
112
+ self.assertTrue(True)
113
+
114
+ def test_wikipedia_naive(self):
115
+ """
116
+ https://en.wikipedia.org/wiki/Viterbi_algorithm#Example
117
+ """
118
+ hmm, observations, expected_prob, expected_sequence = wikipedia()
119
+ path, prob = viterbi_algorithm_naive(hmm, observations, log_probabilities=False)
120
+ print("Example Wikipedia")
121
+ print("Best sequence", path)
122
+ print("Expected Sequence", ["Healthy", "Healthy", "Fever"])
123
+ print("Sequence probability", prob)
124
+
125
+ self.assertTrue(all(path == ["Healthy", "Healthy", "Fever"]))
126
+ self.assertTrue(np.isclose(prob, 0.01512, atol=1e-5))
127
+
128
+ def test_upenn(self):
129
+ """
130
+ Example taken from
131
+ https://www.cis.upenn.edu/~cis262/notes/Example-Viterbi-DNA.pdf
132
+ """
133
+
134
+ obs = ("A", "C", "G", "T")
135
+ states = ("H", "L")
136
+ observation_probabilities = np.array(
137
+ [[0.2, 0.3], [0.3, 0.2], [0.3, 0.2], [0.2, 0.3]]
138
+ )
139
+ transition_probabilities = np.array([[0.5, 0.5], [0.4, 0.6]])
140
+
141
+ observation_model = CategoricalObservationModel(
142
+ observation_probabilities, obs
143
+ )
144
+
145
+ init_probabilities = np.array([0.5, 0.5])
146
+
147
+ transition_model = ConstantTransitionModel(
148
+ transition_probabilities, init_probabilities
149
+ )
150
+
151
+ hmm = HMM(observation_model, transition_model, state_space=states)
152
+
153
+ observations = ["G", "G", "C", "A", "C", "T", "G", "A", "A"]
154
+ path, prob = viterbi_algorithm(hmm, observations, log_probabilities=False)
155
+ print("Example Viterbi-DNA UPenn")
156
+ print("Best sequence", path)
157
+ print("Expected sequence", ["H", "H", "H", "L", "L", "L", "L", "L", "L"])
158
+ print("Sequence probability", prob)
159
+
160
+ self.assertTrue(all(path == ["H", "H", "H", "L", "L", "L", "L", "L", "L"]))
161
+ self.assertTrue(np.isclose(prob, 4.25e-8))
@@ -0,0 +1,246 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Tests for windowed observation model.
5
+ """
6
+ import unittest
7
+
8
+ import numpy as np
9
+
10
+ from hiddenmarkov import HMM, ConstantTransitionModel, WindowedObservationModel
11
+
12
+ from hiddenmarkov.utils import create_prob_models
13
+
14
+
15
+ class TestWindow(unittest.TestCase):
16
+ def test_window(self):
17
+ prob_models = create_prob_models(no_global_states=100, window_size=2)
18
+ observation_model = WindowedObservationModel(
19
+ prob_models, use_inverted_probs=False, use_log_probabilities=True
20
+ )
21
+
22
+ transition_probabilities = np.array([[0.7, 0.3], [0.001, 0.999]])
23
+
24
+ init_probabilities = np.array([0.999, 0.001])
25
+
26
+ transition_model = ConstantTransitionModel(
27
+ transition_probabilities,
28
+ init_probabilities=init_probabilities,
29
+ use_log_probabilities=True,
30
+ )
31
+
32
+ hmm = HMM(observation_model, transition_model)
33
+
34
+ obs = np.array(
35
+ [
36
+ 0,
37
+ 1,
38
+ 1,
39
+ 1,
40
+ 2,
41
+ 3,
42
+ 3,
43
+ 4,
44
+ 5,
45
+ 6,
46
+ 6,
47
+ 7,
48
+ 7,
49
+ 8,
50
+ 8,
51
+ 8,
52
+ 9,
53
+ 10,
54
+ 10,
55
+ 10,
56
+ 11,
57
+ 11,
58
+ 12,
59
+ 12,
60
+ 13,
61
+ 14,
62
+ 15,
63
+ 15,
64
+ 16,
65
+ 16,
66
+ 17,
67
+ 17,
68
+ 18,
69
+ 18,
70
+ 19,
71
+ 19,
72
+ 19,
73
+ 19,
74
+ 20,
75
+ 20,
76
+ 20,
77
+ 21,
78
+ 22,
79
+ 22,
80
+ 23,
81
+ 24,
82
+ 25,
83
+ 25,
84
+ 26,
85
+ 26,
86
+ 27,
87
+ 27,
88
+ 28,
89
+ 28,
90
+ 28,
91
+ 29,
92
+ 30,
93
+ 31,
94
+ 31,
95
+ 31,
96
+ 31,
97
+ 32,
98
+ 33,
99
+ 33,
100
+ 34,
101
+ 35,
102
+ 35,
103
+ 35,
104
+ 36,
105
+ 36,
106
+ 36,
107
+ 37,
108
+ 38,
109
+ 38,
110
+ 39,
111
+ 39,
112
+ 39,
113
+ 39,
114
+ 39,
115
+ 40,
116
+ 40,
117
+ 40,
118
+ 41,
119
+ 41,
120
+ 41,
121
+ 41,
122
+ 41,
123
+ 41,
124
+ 41,
125
+ 42,
126
+ 42,
127
+ 42,
128
+ 42,
129
+ 43,
130
+ 44,
131
+ 45,
132
+ 46,
133
+ 47,
134
+ 48,
135
+ 49,
136
+ 50,
137
+ 51,
138
+ 52,
139
+ 52,
140
+ 53,
141
+ 54,
142
+ 54,
143
+ 55,
144
+ 55,
145
+ 55,
146
+ 55,
147
+ 56,
148
+ 57,
149
+ 58,
150
+ 58,
151
+ 59,
152
+ 60,
153
+ 60,
154
+ 60,
155
+ 61,
156
+ 62,
157
+ 63,
158
+ 64,
159
+ 64,
160
+ 64,
161
+ 65,
162
+ 65,
163
+ 65,
164
+ 66,
165
+ 66,
166
+ 67,
167
+ 68,
168
+ 69,
169
+ 69,
170
+ 69,
171
+ 69,
172
+ 70,
173
+ 71,
174
+ 72,
175
+ 73,
176
+ 73,
177
+ 74,
178
+ 74,
179
+ 74,
180
+ 74,
181
+ 75,
182
+ 75,
183
+ 75,
184
+ 75,
185
+ 75,
186
+ 75,
187
+ 75,
188
+ 76,
189
+ 77,
190
+ 78,
191
+ 78,
192
+ 78,
193
+ 78,
194
+ 79,
195
+ 79,
196
+ 80,
197
+ 81,
198
+ 81,
199
+ 81,
200
+ 82,
201
+ 83,
202
+ 83,
203
+ 83,
204
+ 83,
205
+ 83,
206
+ 83,
207
+ 84,
208
+ 85,
209
+ 85,
210
+ 86,
211
+ 87,
212
+ 88,
213
+ 88,
214
+ 88,
215
+ 89,
216
+ 90,
217
+ 91,
218
+ 91,
219
+ 92,
220
+ 92,
221
+ 92,
222
+ 93,
223
+ 93,
224
+ 94,
225
+ 95,
226
+ 96,
227
+ 96,
228
+ 96,
229
+ 97,
230
+ 97,
231
+ 98,
232
+ 98,
233
+ 98,
234
+ 99,
235
+ 99,
236
+ ]
237
+ )
238
+
239
+ path, prob = hmm.find_best_sequence(
240
+ obs,
241
+ log_probabilities=True,
242
+ viterbi="windowed",
243
+ )
244
+
245
+ self.assertTrue(np.all(path == obs))
246
+ self.assertTrue(np.isclose(prob, -217.51414002612017, atol=1e-5))
@@ -1,74 +0,0 @@
1
- #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
-
4
- import io
5
- import os
6
-
7
- from setuptools import find_packages, setup
8
-
9
- # Package meta-data.
10
- NAME = 'python-hiddenmarkov'
11
- DESCRIPTION = 'A simple Python package for Hidden Markov Models'
12
- KEYWORDS = 'hmm hidden_Markov_models'
13
- URL = 'https://github.com/neosatrapahereje/hiddenmarkov'
14
- EMAIL = 'carloscancinochacon@gmail.com'
15
- AUTHOR = 'Carlos Cancino-Chacón, Silvan Peter'
16
- REQUIRES_PYTHON = '>=3.6'
17
- VERSION = '0.1.2'
18
-
19
- # What packages are required for this module to be executed?
20
- REQUIRED = [
21
- 'numpy',
22
- ]
23
-
24
- # What packages are optional?
25
- EXTRAS = {
26
- # 'fancy feature': ['django'],
27
- }
28
-
29
- here = os.path.abspath(os.path.dirname(__file__))
30
-
31
- # Import the README and use it as the long-description.
32
- # Note: this will only work if 'README.md' is present in your MANIFEST.in file!
33
- try:
34
- with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
35
- long_description = '\n' + f.read()
36
- except FileNotFoundError:
37
- long_description = DESCRIPTION
38
-
39
- # Load the package's __version__.py module as a dictionary.
40
- about = {}
41
- if not VERSION:
42
- with open(os.path.join(here, NAME, '__version__.py')) as f:
43
- exec(f.read(), about)
44
- else:
45
- about['__version__'] = VERSION
46
-
47
-
48
- # Where the magic happens:
49
- setup(
50
- name=NAME,
51
- version=about['__version__'],
52
- description=DESCRIPTION,
53
- long_description=long_description,
54
- long_description_content_type='text/markdown',
55
- keywords=KEYWORDS,
56
- author=AUTHOR,
57
- author_email=EMAIL,
58
- python_requires=REQUIRES_PYTHON,
59
- url=URL,
60
- packages=find_packages(exclude=('tests',)),
61
- install_requires=REQUIRED,
62
- extras_require=EXTRAS,
63
- include_package_data=True,
64
- license='MIT',
65
- classifiers=[
66
- # Trove classifiers
67
- # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
68
- 'License :: OSI Approved :: MIT License',
69
- 'Programming Language :: Python',
70
- 'Programming Language :: Python :: 3',
71
- 'Programming Language :: Python :: Implementation :: CPython',
72
- 'Programming Language :: Python :: Implementation :: PyPy'
73
- ],
74
- )