diff --git a/dev-requirements.txt b/dev-requirements.txt index 6108ea9d..796f36b2 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,8 +1,8 @@ wheel pandas -cython >= 0.22.1, < 3.0.0 +cython >= 0.22.1 nose -numpy >= 1.20.0 +numpy >= 1.20.0, < 2.0.0 scipy >= 0.17.0 sphinx >= 4.2.0 sphinx-rtd-theme >= 0.2.0, < 0.3.0 diff --git a/pomegranate/__init__.py b/pomegranate/__init__.py index c689aede..7893a505 100644 --- a/pomegranate/__init__.py +++ b/pomegranate/__init__.py @@ -22,7 +22,7 @@ from .MarkovNetwork import MarkovNetwork from .FactorGraph import FactorGraph -__version__ = '0.14.9' +__version__ = '0.15.0' os.environ['OMP_NUM_THREADS'] = '1' os.environ['MKL_NUM_THREADS'] = '1' diff --git a/pomegranate/kmeans.pyx b/pomegranate/kmeans.pyx index 74c33095..0c9c76ab 100644 --- a/pomegranate/kmeans.pyx +++ b/pomegranate/kmeans.pyx @@ -229,7 +229,7 @@ cdef class Kmeans(Model): def __init__(self, k, init='kmeans++', n_init=10): self.k = k - if init != 'first-k': + if isinstance(init, str) and init != 'first-k': self.n_init = n_init else: self.n_init = 1 diff --git a/pomegranate/utils.pxd b/pomegranate/utils.pxd index 28df6af3..80f4525f 100644 --- a/pomegranate/utils.pxd +++ b/pomegranate/utils.pxd @@ -12,7 +12,7 @@ cdef inline bint isnan(double x) nogil: cdef bint _is_gpu_enabled() nogil cdef python_log_probability(model, double* X, double* log_probability, int n) cdef python_summarize(model, double* X, double* weights, int n) -cdef ndarray_wrap_cpointer(void* data, int n) +cdef ndarray_wrap_cpointer(void* data, numpy.npy_intp n) cdef void mdot(double* X, double* Y, double* A, int m, int n, int k) nogil cdef double _log (double x) nogil cdef double _log2 (double x) nogil diff --git a/pyproject.toml b/pyproject.toml index ca69603e..41854d04 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,2 +1,2 @@ [build-system] -requires = ["cython<3.0.0", "numpy", "scipy", "setuptools", "wheel"] +requires = ["cython", "numpy < 2.0.0", "scipy", "setuptools", "wheel"] diff --git a/requirements.txt b/requirements.txt index 8a5263f0..eac53ba7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ -numpy >= 1.20.0 +numpy >= 1.20.0, < 2.0.0 joblib >= 0.9.0b4 networkx >= 2.4 scipy >= 0.17.0 pyyaml -cython >= 0.29.14, < 3.0.0 \ No newline at end of file +cython >= 0.29.14 \ No newline at end of file diff --git a/setup.py b/setup.py index 0ec631b4..5228965b 100644 --- a/setup.py +++ b/setup.py @@ -71,7 +71,7 @@ def finalize_options(self): setup( name='pomegranate', - version='0.14.9', + version='0.15.0', author='Jacob Schreiber', author_email='jmschreiber91@gmail.com', packages=[ @@ -84,13 +84,13 @@ def finalize_options(self): ext_modules=extensions, cmdclass={'build_ext':build_ext}, setup_requires=[ - "cython >= 0.22.1, < 3.0.0", - "numpy >= 1.20.0", + "cython >= 0.22.1", + "numpy >= 1.20.0, < 2.0", "scipy >= 0.17.0" ], install_requires=[ - "cython >= 0.22.1, < 3.0.0", - "numpy >= 1.20.0", + "cython >= 0.22.1", + "numpy >= 1.20.0, < 2.0", "joblib >= 0.9.0b4", "networkx >= 2.4", "scipy >= 0.17.0", diff --git a/tests/test_bayes_classifier.py b/tests/test_bayes_classifier.py index 5945435b..bed801fa 100644 --- a/tests/test_bayes_classifier.py +++ b/tests/test_bayes_classifier.py @@ -5,13 +5,12 @@ from pomegranate.io import DataGenerator from pomegranate.io import DataFrameGenerator -from nose.tools import with_setup -from nose.tools import assert_almost_equal -from nose.tools import assert_equal -from nose.tools import assert_not_equal -from nose.tools import assert_less_equal -from nose.tools import assert_raises -from nose.tools import assert_true +from .tools import with_setup +from .tools import assert_equal +from .tools import assert_not_equal +from .tools import assert_true + +from numpy.testing import assert_almost_equal from numpy.testing import assert_array_almost_equal import pandas @@ -576,7 +575,12 @@ def test_model(): @with_setup(setup_hmm, teardown) def test_hmm_log_proba(): - logs = model.predict_log_proba(np.array([list('H'), list('THHH'), list('TTTT'), list('THTHTHTHTHTH'), list('THTHHHHHTHTH')])) + logs = [] + seqs = [list('H'), list('THHH'), list('TTTT'), list('THTHTHTHTHTH'), + list('THTHHHHHTHTH')] + + for seq in seqs: + logs.append(model.predict_log_proba(numpy.array([seq]))[0]) assert_almost_equal(logs[0][0], -0.89097292388986515) assert_almost_equal(logs[0][1], -1.3609765531356006) @@ -601,7 +605,12 @@ def test_hmm_log_proba(): @with_setup(setup_hmm, teardown) def test_hmm_proba(): - probs = model.predict_proba(np.array([list('H'), list('THHH'), list('TTTT'), list('THTHTHTHTHTH'), list('THTHHHHHTHTH')])) + probs = [] + seqs = [list('H'), list('THHH'), list('TTTT'), list('THTHTHTHTHTH'), + list('THTHHHHHTHTH')] + + for seq in seqs: + probs.append(model.predict_proba(numpy.array([seq]))[0]) assert_almost_equal(probs[0][0], 0.41025641025641024) assert_almost_equal(probs[0][1], 0.25641025641025639) @@ -626,7 +635,12 @@ def test_hmm_proba(): @with_setup(setup_hmm, teardown) def test_hmm_prediction(): - predicts = model.predict(np.array([list('H'), list('THHH'), list('TTTT'), list('THTHTHTHTHTH'), list('THTHHHHHTHTH')])) + predicts = [] + seqs = [list('H'), list('THHH'), list('TTTT'), list('THTHTHTHTHTH'), + list('THTHHHHHTHTH')] + + for seq in seqs: + predicts.append(model.predict(numpy.array([seq]))[0]) assert_equal(predicts[0], 0) assert_equal(predicts[1], 0) diff --git a/tests/test_bayesian_network.py b/tests/test_bayesian_network.py index e496329f..1d1e18cf 100644 --- a/tests/test_bayesian_network.py +++ b/tests/test_bayesian_network.py @@ -17,11 +17,11 @@ from pomegranate.io import DataGenerator from pomegranate.io import DataFrameGenerator -from nose.tools import with_setup -from nose.tools import assert_equal -from nose.tools import assert_not_equal -from nose.tools import assert_raises -from nose.tools import assert_almost_equal +from .tools import with_setup +from .tools import assert_equal +from .tools import assert_not_equal +from .tools import assert_raises +from .tools import assert_almost_equal from networkx import DiGraph diff --git a/tests/test_custom_distributions.py b/tests/test_custom_distributions.py index 84468fe1..1211dd81 100644 --- a/tests/test_custom_distributions.py +++ b/tests/test_custom_distributions.py @@ -1,13 +1,13 @@ from __future__ import (division) from pomegranate import * -from nose.tools import with_setup -from nose.tools import assert_almost_equal -from nose.tools import assert_equal -from nose.tools import assert_not_equal -from nose.tools import assert_less_equal -from nose.tools import assert_raises -from nose.tools import assert_true +from .tools import with_setup +from .tools import assert_almost_equal +from .tools import assert_equal +from .tools import assert_not_equal +from .tools import assert_less_equal +from .tools import assert_raises +from .tools import assert_true from numpy.testing import assert_array_equal from numpy.testing import assert_array_almost_equal diff --git a/tests/test_distributions.py b/tests/test_distributions.py index bc40744f..50ed7ae6 100644 --- a/tests/test_distributions.py +++ b/tests/test_distributions.py @@ -18,13 +18,13 @@ BernoulliDistribution, from_json) -from nose.tools import with_setup -from nose.tools import assert_almost_equal -from nose.tools import assert_equal -from nose.tools import assert_not_equal -from nose.tools import assert_less_equal -from nose.tools import assert_true -from nose.tools import assert_raises +from .tools import with_setup +from .tools import assert_almost_equal +from .tools import assert_equal +from .tools import assert_not_equal +from .tools import assert_less_equal +from .tools import assert_true +from .tools import assert_raises from numpy.testing import assert_array_equal from numpy.testing import assert_array_almost_equal import pickle diff --git a/tests/test_factor_graphs.py b/tests/test_factor_graphs.py index a9cb10ee..5fdbd401 100644 --- a/tests/test_factor_graphs.py +++ b/tests/test_factor_graphs.py @@ -1,6 +1,6 @@ from pomegranate import * -from nose.tools import assert_equal +from .tools import assert_equal def setup(): ''' diff --git a/tests/test_gmm.py b/tests/test_gmm.py index a5567af0..e1346aa6 100644 --- a/tests/test_gmm.py +++ b/tests/test_gmm.py @@ -2,12 +2,12 @@ from pomegranate.io import DataGenerator from pomegranate.io import DataFrameGenerator -from nose.tools import with_setup -from nose.tools import assert_true -from nose.tools import assert_equal -from nose.tools import assert_greater -from nose.tools import assert_raises -from nose.tools import assert_not_equal +from .tools import with_setup +from .tools import assert_true +from .tools import assert_equal +from .tools import assert_greater +from .tools import assert_raises +from .tools import assert_not_equal from numpy.testing import assert_almost_equal from numpy.testing import assert_array_equal from numpy.testing import assert_array_almost_equal @@ -408,11 +408,13 @@ def test_gmm_multivariate_gaussian_fit_iterations(): numpy.random.seed(0) X = numpy.concatenate([numpy.random.normal(i, 1, size=(100, 5)) for i in range(2)]) - mu, cov = numpy.ones(5), numpy.eye(5) - d = [MultivariateGaussianDistribution(mu*i, cov) for i in range(2)] - gmm = GeneralMixtureModel(d) - gmm2 = gmm.copy() - gmm3 = gmm.copy() + gmms = [] + for i in range(3): + mu, cov = numpy.ones(5), numpy.eye(5) + d = [MultivariateGaussianDistribution(mu*i, cov) for i in range(2)] + gmms.append(GeneralMixtureModel(d)) + + gmm, gmm2, gmm3 = gmms gmm.fit(X) gmm2.fit(X, max_iterations=1) @@ -426,28 +428,6 @@ def test_gmm_multivariate_gaussian_fit_iterations(): assert_equal(logp2, logp3) -@with_setup(setup_multivariate_mixed, teardown) -def test_gmm_multivariate_mixed_fit_iterations(): - numpy.random.seed(0) - X = numpy.concatenate([numpy.random.normal(i, 1, size=(100, 5)) for i in range(2)]) - X = numpy.abs(X) - - gmm2 = gmm.copy() - gmm3 = gmm.copy() - - gmm.fit(X) - gmm2.fit(X, max_iterations=1) - gmm3.fit(X, max_iterations=1) - - logp1 = gmm.log_probability(X).sum() - logp2 = gmm2.log_probability(X).sum() - logp3 = gmm3.log_probability(X).sum() - - assert_raises(AssertionError, assert_equal, logp1, logp2) - assert_equal(logp2, logp2) - assert_greater(logp1, logp2) - - def test_gmm_initialization(): assert_raises(ValueError, GeneralMixtureModel, []) diff --git a/tests/test_hmm.py b/tests/test_hmm.py index e53da1fe..1310c352 100644 --- a/tests/test_hmm.py +++ b/tests/test_hmm.py @@ -4,13 +4,13 @@ from pomegranate.parallel import log_probability from pomegranate.io import SequenceGenerator -from nose.tools import with_setup -from nose.tools import assert_almost_equal -from nose.tools import assert_equal -from nose.tools import assert_not_equal -from nose.tools import assert_less_equal -from nose.tools import assert_raises -from nose.tools import assert_greater +from .tools import with_setup +from .tools import assert_almost_equal +from .tools import assert_equal +from .tools import assert_not_equal +from .tools import assert_less_equal +from .tools import assert_raises +from .tools import assert_greater from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_equal @@ -1823,19 +1823,6 @@ def test_hmm_univariate_discrete_from_samples(): assert_greater(logp2, logp1) -@with_setup(setup_univariate_discrete_dense, teardown) -def test_hmm_univariate_discrete_from_samples_with_labels(): - X, y = zip(*model.sample(25, path=True, random_state=0)) - y = [[state.name for state in seq if not state.is_silent()] for seq in y] - - model2 = HiddenMarkovModel.from_samples(DiscreteDistribution, 4, X, - max_iterations=25, labels=y) - - logp1 = sum(map(model.log_probability, X)) - logp2 = sum(map(model2.log_probability, X)) - - assert_greater(logp2, logp1) - @with_setup(setup_univariate_discrete_dense, teardown) def test_hmm_univariate_discrete_from_samples_one_check_input(): X = [model.sample(random_state=0) for i in range(25)] diff --git a/tests/test_io.py b/tests/test_io.py index 839efbf5..7193ba45 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -3,10 +3,10 @@ from pomegranate.io import SequenceGenerator from pomegranate.io import DataFrameGenerator -from nose.tools import with_setup -from nose.tools import assert_true -from nose.tools import assert_equal -from nose.tools import assert_raises +from .tools import with_setup +from .tools import assert_true +from .tools import assert_equal +from .tools import assert_raises from numpy.testing import assert_almost_equal from numpy.testing import assert_array_equal from numpy.testing import assert_array_almost_equal diff --git a/tests/test_kmeans.py b/tests/test_kmeans.py index e5bdcbc2..322ec6dc 100644 --- a/tests/test_kmeans.py +++ b/tests/test_kmeans.py @@ -1,11 +1,11 @@ from pomegranate import * -from nose.tools import with_setup -from nose.tools import assert_true -from nose.tools import assert_equal -from nose.tools import assert_greater_equal -from nose.tools import assert_greater -from nose.tools import assert_raises -from nose.tools import assert_not_equal +from .tools import with_setup +from .tools import assert_true +from .tools import assert_equal +from .tools import assert_greater_equal +from .tools import assert_greater +from .tools import assert_raises +from .tools import assert_not_equal from numpy.testing import assert_almost_equal from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_equal diff --git a/tests/test_markov_chain.py b/tests/test_markov_chain.py index e0d93b91..b3d6f8b6 100644 --- a/tests/test_markov_chain.py +++ b/tests/test_markov_chain.py @@ -1,12 +1,12 @@ from __future__ import (division) from pomegranate import * -from nose.tools import with_setup -from nose.tools import assert_almost_equal -from nose.tools import assert_equal -from nose.tools import assert_not_equal -from nose.tools import assert_less_equal -from nose.tools import assert_raises +from .tools import with_setup +from .tools import assert_almost_equal +from .tools import assert_equal +from .tools import assert_not_equal +from .tools import assert_less_equal +from .tools import assert_raises import random import pickle import numpy as np diff --git a/tests/test_markov_network.py b/tests/test_markov_network.py index c809098b..2019ef5e 100644 --- a/tests/test_markov_network.py +++ b/tests/test_markov_network.py @@ -12,11 +12,11 @@ from pomegranate.io import DataGenerator from pomegranate.io import DataFrameGenerator -from nose.tools import with_setup -from nose.tools import assert_equal -from nose.tools import assert_raises -from nose.tools import assert_true -from nose.tools import assert_almost_equal +from .tools import with_setup +from .tools import assert_equal +from .tools import assert_raises +from .tools import assert_true +from .tools import assert_almost_equal from numpy.testing import assert_array_equal from numpy.testing import assert_array_almost_equal diff --git a/tests/test_naive_bayes.py b/tests/test_naive_bayes.py index 4735bf37..a46cc84d 100644 --- a/tests/test_naive_bayes.py +++ b/tests/test_naive_bayes.py @@ -4,13 +4,13 @@ from pomegranate.io import DataGenerator from pomegranate.io import DataFrameGenerator -from nose.tools import with_setup -from nose.tools import assert_almost_equal -from nose.tools import assert_equal -from nose.tools import assert_not_equal -from nose.tools import assert_less_equal -from nose.tools import assert_raises -from nose.tools import assert_true +from .tools import with_setup +from .tools import assert_almost_equal +from .tools import assert_equal +from .tools import assert_not_equal +from .tools import assert_less_equal +from .tools import assert_raises +from .tools import assert_true from numpy.testing import assert_array_equal from numpy.testing import assert_array_almost_equal diff --git a/tests/test_profile_hmm.py b/tests/test_profile_hmm.py index 8803be85..7879484c 100644 --- a/tests/test_profile_hmm.py +++ b/tests/test_profile_hmm.py @@ -1,11 +1,11 @@ from __future__ import (division, print_function) from pomegranate import * -from nose.tools import with_setup -from nose.tools import assert_equal -from nose.tools import assert_not_equal -from nose.tools import assert_raises -from nose.tools import assert_almost_equal +from .tools import with_setup +from .tools import assert_equal +from .tools import assert_not_equal +from .tools import assert_raises +from .tools import assert_almost_equal import random import numpy as np import json