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

Switch to pytest from nose, support Python 3.10 #170

Merged
merged 6 commits into from
Feb 26, 2022
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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ python:
- "3.7"
- "3.8"
- "3.9"
- "3.10"
cache: pip
addons:
apt:
Expand All @@ -24,7 +25,7 @@ install:

# Run tests
script:
- nosetests --with-coverage
- pytest --cov

# Calculate coverage
after_success:
Expand Down
9 changes: 3 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
.. image:: https://api.codacy.com/project/badge/Grade/cb19f1f1093a44fa845ebfdaf76975f6
:alt: Codacy Badge
:target: https://app.codacy.com/app/nicodv/kmodes?utm_source=github.com&utm_medium=referral&utm_content=nicodv/kmodes&utm_campaign=Badge_Grade_Dashboard
.. image:: https://requires.io/github/nicodv/kmodes/requirements.svg
:target: https://requires.io/github/nicodv/kmodes/requirements/
:alt: Requirements Status
.. image:: https://img.shields.io/pypi/dm/kmodes.svg
:target: https://pypi.python.org/pypi/kmodes/
:alt: Monthly downloads
.. image:: https://img.shields.io/pypi/pyversions/kmodes.svg
:target: https://pypi.python.org/pypi/kmodes/
:alt: Supported Python versions
.. image:: https://img.shields.io/github/stars/nicodv/kmodes.svg
:target: https://github.com/nicodv/kmodes/
:alt: Github stars
.. image:: https://img.shields.io/pypi/l/kmodes.svg
:target: https://github.com/nicodv/kmodes/blob/master/LICENSE
:alt: License
Expand Down
6 changes: 3 additions & 3 deletions kmodes/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def test_all_estimator_no_base_class():

def test_all_estimators():
for name, Estimator in all_estimators():
yield (_named_check(check_parameters_default_constructible, name),
name, Estimator())
return (_named_check(check_parameters_default_constructible, name),
name, Estimator())


def test_non_meta_estimators():
Expand All @@ -79,4 +79,4 @@ def test_non_meta_estimators():
estimator = Estimator()
for check in _yield_all_checks(estimator):
if hasattr(check, '__name__') and check.__name__ in relevant_checks:
yield _named_check(check, name), name, estimator
return _named_check(check, name), name, estimator
10 changes: 8 additions & 2 deletions kmodes/tests/test_kmodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import unittest

import numpy as np
from nose.tools import assert_equal

from kmodes.kmodes import KModes
from kmodes.util.dissim import ng_dissim, jaccard_dissim_binary, jaccard_dissim_label
Expand Down Expand Up @@ -234,7 +233,14 @@ class TestKModes(unittest.TestCase):
def test_pickle(self):
obj = KModes()
s = pickle.dumps(obj)
assert_equal(type(pickle.loads(s)), obj.__class__)
assert type(pickle.loads(s)) == obj.__class__

def test_pickle_fitted(self):
kmodes_huang = KModes(n_clusters=4, n_init=2, init='Huang', verbose=2,
random_state=42)
model = kmodes_huang.fit(SOYBEAN)
s = pickle.dumps(model)
assert type(pickle.loads(s)) == model.__class__

def test_kmodes_huang_soybean(self):
kmodes_huang = KModes(n_clusters=4, n_init=2, init='Huang', verbose=2,
Expand Down
9 changes: 7 additions & 2 deletions kmodes/tests/test_kprototypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import unittest

import numpy as np
from nose.tools import assert_equal

from kmodes import kprototypes
from kmodes.tests.test_kmodes import assert_cluster_splits_equal
Expand Down Expand Up @@ -39,7 +38,13 @@ class TestKProtoTypes(unittest.TestCase):
def test_pickle(self):
obj = kprototypes.KPrototypes()
s = pickle.dumps(obj)
assert_equal(type(pickle.loads(s)), obj.__class__)
assert type(pickle.loads(s)) == obj.__class__

def test_pickle_fitted(self):
kproto = kprototypes.KPrototypes(n_clusters=4, init='Cao', verbose=2)
model = kproto.fit(STOCKS[:, :2], categorical=1)
s = pickle.dumps(model)
assert type(pickle.loads(s)) == model.__class__

def test_kprotoypes_categoricals_stocks(self):
# Number/index of categoricals does not make sense
Expand Down
2 changes: 1 addition & 1 deletion kmodes/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def encode_features(X, enc_map=None):
else:
fit = False

Xenc = np.zeros(X.shape, dtype='uint32')
Xenc = np.zeros(X.shape, dtype='int32')
for ii in range(X.shape[1]):
if fit:
col_enc = {val: jj for jj, val in enumerate(np.unique(X[:, ii]))
Expand Down
19 changes: 9 additions & 10 deletions kmodes/util/tests/test_dissim.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import unittest

import numpy as np
from nose.tools import assert_equal
from sklearn.utils._testing import assert_array_equal

from kmodes.util.dissim import matching_dissim, euclidean_dissim, ng_dissim
Expand All @@ -17,11 +16,11 @@ class TestDissimilarityMeasures(unittest.TestCase):
def test_matching_dissim(self):
a = np.array([[0, 1, 2, 0, 1, 2]])
b = np.array([[0, 1, 2, 0, 1, 0]])
assert_equal(1, matching_dissim(a, b))
assert 1 == matching_dissim(a, b)

a = np.array([[np.NaN, 1, 2, 0, 1, 2]])
b = np.array([[0, 1, 2, 0, 1, 0]])
assert_equal(2, matching_dissim(a, b))
assert 2 == matching_dissim(a, b)

a = np.array([['a', 'b', 'c', 'd']])
b = np.array([['a', 'b', 'c', 'd'], ['d', 'c', 'b', 'a']])
Expand All @@ -30,7 +29,7 @@ def test_matching_dissim(self):
def test_jaccard_dissim_binary(self):
a = np.array([[0, 1, 1, 0, 1, 1]])
b = np.array([[0, 1, 1, 0, 1, 0]])
assert_equal(0.25, jaccard_dissim_binary(a, b))
assert 0.25 == jaccard_dissim_binary(a, b)

a = np.array([[0, 1, 1, 0, 1, 1]])
b = np.array([[0, np.NaN, 1, 0, 1, 0]])
Expand All @@ -46,17 +45,17 @@ def test_jaccard_dissim_binary(self):
# test for dissimilarity = 0: sets are the same
a = np.array([[1, 1, 0, 1, 1, 0]])
b = np.array([[1, 1, 0, 1, 1, 0]])
assert_equal(0, jaccard_dissim_binary(a, b))
assert 0 == jaccard_dissim_binary(a, b)

# test for dissimilarity = 1: sets are completely different
a = np.array([[0, 0, 1, 0, 0, 1]])
b = np.array([[1, 1, 0, 1, 1, 0]])
assert_equal(1, jaccard_dissim_binary(a, b))
assert 1 == jaccard_dissim_binary(a, b)

def test_jaccard_dissim_label(self):
a = np.array([[0, 1, 2, 0, 1, 2]])
b = np.array([[0, 1, 2, 0, 3, 0]])
assert_equal(0.25, jaccard_dissim_label(a, b))
assert 0.25 == jaccard_dissim_label(a, b)

a = np.array([[np.NaN, 1, 2, 0, 1, 2]])
b = np.array([[0, 1, 2, 0, 1, 0]])
Expand All @@ -66,17 +65,17 @@ def test_jaccard_dissim_label(self):
# test for dissimilarity = 0: sets are the same
a = np.array([[1, 2, 0, 3, 1, 0]])
b = np.array([[1, 2, 0, 3, 1, 0]])
assert_equal(0, jaccard_dissim_label(a, b))
assert 0 == jaccard_dissim_label(a, b)

# test for dissimilarity = 1: sets are completely different
a = np.array([[1, 2, 0, 3, 1, 0]])
b = np.array([[5, 4, 6, 7, 8, 9]])
assert_equal(1, jaccard_dissim_label(a, b))
assert 1 == jaccard_dissim_label(a, b)

def test_euclidian_dissim(self):
a = np.array([[0., 1., 2., 0., 1., 2.]])
b = np.array([[3., 1., 3., 0., 1., 0.]])
assert_equal(14., euclidean_dissim(a, b))
assert 14. == euclidean_dissim(a, b)

a = np.array([[np.NaN, 1., 2., 0., 1., 2.]])
b = np.array([[3., 1., 3., 0., 1., 0.]])
Expand Down
5 changes: 2 additions & 3 deletions kmodes/util/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import unittest

import numpy as np
from nose.tools import assert_equal
from sklearn.utils._testing import assert_array_equal

from kmodes.util import get_max_value_key, encode_features, get_unique_rows, \
Expand Down Expand Up @@ -47,11 +46,11 @@ class TestUtils(unittest.TestCase):

def test_get_max_value_key(self):
max_key = get_max_value_key({'a': 3, 'b': 10, 'c': -1, 'd': 9.9})
assert_equal('b', max_key)
assert 'b' == max_key

# Make sure minimum key is consistently selected for equal values.
max_key = get_max_value_key({'d': 10, 'c': 10, 'b': 10, 'a': 10})
assert_equal('a', max_key)
assert 'a' == max_key

def test_encode_features(self):
X_enc, enc_map = encode_features(STOCKS_CAT)
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
'joblib>=0.11'
],
extras_requires=[
'nose',
'pytest',
'pytest-cov',
],
classifiers=['Development Status :: 3 - Alpha',
'Intended Audience :: Science/Research',
Expand All @@ -42,5 +43,6 @@
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Topic :: Scientific/Engineering'],
)