Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch hmmlearn, fix dependencies #620

Merged
merged 11 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions MACS3/Signal/HMMR_HMM.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cython: language_level=3
# cython: profile=True
# Time-stamp: <2022-10-04 15:14:15 Tao Liu>
# Time-stamp: <2024-02-18 16:21:00 Tao Liu>

"""Module description:

Expand All @@ -20,7 +20,8 @@ from math import sqrt
import numpy as np
cimport numpy as np
from cpython cimport bool
from hmmlearn import hmm
from hmmlearn import hmm, _utils
from sklearn import cluster
import json
# from hmmlearn cimport hmm

Expand Down Expand Up @@ -49,6 +50,35 @@ cdef inline float get_weighted_density( int x, float m, float v, w ):
# ------------------------------------
# Classes
# ------------------------------------

class GaussianHMM_modified( hmm.GaussianHMM ):
def _init(self, X, lengths=None):
super()._init(X, lengths)
# we will overwrite initial means_ and covars_
kmeans = cluster.KMeans(n_clusters=self.n_components,
random_state=self.random_state,
n_init=10) # https://github.com/hmmlearn/hmmlearn/pull/545
# the idea is to do the random seeds
# for 10 times orginally, hmmlearn 0.3
# will do this only once. However,
# due to the change in scikit-learn
# 1.3, the random seeding in KMeans
# will generate different results with
# previous scikit-learn. It will make
# the results irreproducible between
# sklearn <1.3 and sklearn
# >=1.3. Hopefully, if we choose to do
# the process 10 times, the results
# will be more similar.
kmeans.fit(X)
self.means_ = kmeans.cluster_centers_

cv = np.cov(X.T) + self.min_covar * np.eye(X.shape[1])
if not cv.shape:
cv.shape = (1, 1)
self.covars_ = \
_utils.distribute_covar_matrix_to_match_covariance_type( cv, self.covariance_type, self.n_components ).copy()

# ------------------------------------
# public functions
# ------------------------------------
Expand All @@ -60,7 +90,7 @@ cpdef hmm_training( list training_data, list training_data_lengths, int n_states
# according to base documentation, if init_prob not stated, it is set to be equally likely for any state (1/ # of components)
# if we have other known parameters, we should set these (ie: means_weights, covariance_type etc.)
rs = np.random.RandomState(np.random.MT19937(np.random.SeedSequence(random_seed)))
hmm_model = hmm.GaussianHMM( n_components= n_states, covariance_type = covar, random_state = rs, verbose = False )
hmm_model = GaussianHMM_modified( n_components= n_states, covariance_type = covar, random_state = rs, verbose = False )
hmm_model = hmm_model.fit( training_data, training_data_lengths )
assert hmm_model.n_features == 4
return hmm_model
Expand Down Expand Up @@ -91,7 +121,7 @@ cpdef void hmm_model_save( str model_file, object hmm_model, int hmm_binsize, in
cpdef list hmm_model_init( str model_file ):
with open( model_file ) as f:
m = json.load( f )
hmm_model = hmm.GaussianHMM( n_components=3, covariance_type=m["covariance_type"] )
hmm_model = GaussianHMM_modified( n_components=3, covariance_type=m["covariance_type"] )
hmm_model.startprob_ = np.array(m["startprob"])
hmm_model.transmat_ = np.array(m["transmat"])
hmm_model.means_ = np.array(m["means"])
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[build-system]
requires=['setuptools>=60.0', 'numpy>=1.24', 'scipy>=1.10', 'cykhash>=2.0,<3.0', 'Cython~=3.0', 'scikit-learn>=1.2,<1.4', 'hmmlearn>=0.3']
requires=['setuptools>=60.0', 'numpy>=1.24.2', 'scipy>=1.11.4', 'cykhash>=2.0,<3.0', 'Cython~=3.0', 'scikit-learn>=1.2.1', 'hmmlearn>=0.3']

6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Cython~=3.0
numpy>=1.24
scipy>=1.10
scikit-learn>=1.2,<1.4
numpy>=1.24.2
scipy>=1.11.4
scikit-learn>=1.2.1
hmmlearn>=0.3
cykhash>=2.0,<3.0
pytest>=7.0
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
'Programming Language :: Python :: 3.12',
'Programming Language :: Cython', ]

install_requires = [ "numpy>=1.24",
"scipy>=1.10",
install_requires = [ "numpy>=1.24.2",
"scipy>=1.11.4",
"hmmlearn>=0.3",
"scikit-learn>=1.2,<1.4",
"scikit-learn>=1.2.1",
"cykhash>=2.0,<3.0"]


Expand Down
Loading
Loading