python-hiddenmarkov 0.1.3__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,25 +1,27 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: python-hiddenmarkov
3
- Version: 0.1.3
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
 
22
- [![PyPI version](https://badge.fury.io/py/python-hiddenmarkov.svg)](https://badge.fury.io/py/python-hiddenmarkov)
24
+ [![PyPI](https://img.shields.io/pypi/v/python-hiddenmarkov)](https://pypi.org/project/python-hiddenmarkov/)
23
25
 
24
26
  A simple Python library for Hidden Markov Models.
25
27
 
@@ -1,6 +1,6 @@
1
1
  # hiddenmarkov
2
2
 
3
- [![PyPI version](https://badge.fury.io/py/python-hiddenmarkov.svg)](https://badge.fury.io/py/python-hiddenmarkov)
3
+ [![PyPI](https://img.shields.io/pypi/v/python-hiddenmarkov)](https://pypi.org/project/python-hiddenmarkov/)
4
4
 
5
5
  A simple Python library for Hidden Markov Models.
6
6
 
@@ -5,9 +5,9 @@ Hidden Markov Models
5
5
 
6
6
  This module contains case classes to define Hidden Markov Models.
7
7
  """
8
- import pkg_resources
9
8
  import warnings
10
9
  import numpy as np
10
+ from importlib.metadata import version, PackageNotFoundError
11
11
 
12
12
  from collections import defaultdict
13
13
  from typing import Any, Iterable, Optional, Tuple, ClassVar, List
@@ -18,7 +18,10 @@ from .utils import inverted_softmax, softmax
18
18
 
19
19
 
20
20
  # define a version variable
21
- __version__ = pkg_resources.get_distribution("python-hiddenmarkov").version
21
+ try:
22
+ __version__ = version("python-hiddenmarkov")
23
+ except PackageNotFoundError:
24
+ __version__ = "unknown"
22
25
 
23
26
 
24
27
  class TransitionModel(object):
@@ -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,25 +1,27 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: python-hiddenmarkov
3
- Version: 0.1.3
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
 
22
- [![PyPI version](https://badge.fury.io/py/python-hiddenmarkov.svg)](https://badge.fury.io/py/python-hiddenmarkov)
24
+ [![PyPI](https://img.shields.io/pypi/v/python-hiddenmarkov)](https://pypi.org/project/python-hiddenmarkov/)
23
25
 
24
26
  A simple Python library for Hidden Markov Models.
25
27
 
@@ -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.3'
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
- )