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

formats, lints, pytest pt. 2 -- test_adbscan.py #343

Merged
merged 1 commit into from
Jul 21, 2024
Merged
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
63 changes: 29 additions & 34 deletions esda/tests/test_adbscan.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import unittest

import numpy as np
import pandas
import sklearn
import pytest
import sklearn

from .. import adbscan


class ADBSCAN_Tester(unittest.TestCase):
def setUp(self):
class TestADBSCAN:
def setup_method(self):
np.random.seed(10)
self.db = pandas.DataFrame(
{"x": np.random.random(25), "y": np.random.random(25)}
Expand Down Expand Up @@ -82,14 +81,14 @@ def test_adbscan(self):
ads = adbscan.ADBSCAN(0.03, 3, reps=10, keep_solus=True)
_ = ads.fit(self.db, xy=["x", "y"])
# Params
self.assertAlmostEqual(ads.eps, 0.03)
self.assertEqual(ads.min_samples, 3)
self.assertEqual(ads.algorithm, "auto")
self.assertEqual(ads.n_jobs, 1)
self.assertEqual(ads.pct_exact, 0.1)
self.assertEqual(ads.reps, 10)
self.assertEqual(ads.keep_solus, True)
self.assertEqual(ads.pct_thr, 0.9)
assert pytest.approx(ads.eps) == 0.03
assert ads.min_samples == 3
assert ads.algorithm == "auto"
assert ads.n_jobs == 1
assert ads.pct_exact == 0.1
assert ads.reps == 10
assert ads.keep_solus is True
assert ads.pct_thr == 0.9
# Labels
np.testing.assert_equal(ads.labels_, self.lbls)
# Votes
Expand All @@ -109,9 +108,16 @@ def test_adbscan(self):
# ------------------------#
np.random.seed(10)
ads = adbscan.ADBSCAN(0.03, 3, reps=10, keep_solus=True, n_jobs=-1)
_ = ads.fit(self.db, xy=["x", "y"])
with pytest.warns(
UserWarning,
match=(
"Multi-core implementation only works on relabelling solutions. "
"Execution of draws is still sequential."
),
):
_ = ads.fit(self.db, xy=["x", "y"])
# Params
self.assertEqual(ads.n_jobs, -1)
assert ads.n_jobs == -1
# Labels
np.testing.assert_equal(ads.labels_, self.lbls)
# Votes
Expand All @@ -128,8 +134,8 @@ def test_adbscan(self):
np.testing.assert_equal(ads.solus.astype(int).sum(axis=1).values, i_sum)


class Remap_lbls_Tester(unittest.TestCase):
def setUp(self):
class TestRemapLBLS:
def setup_method(self):
self.db = pandas.DataFrame({"X": [0, 0.1, 4, 6, 5], "Y": [0, 0.2, 5, 7, 5]})
self.solus = pandas.DataFrame(
{
Expand Down Expand Up @@ -159,8 +165,8 @@ def test_remap_lbls(self):
np.testing.assert_equal(lbls.values, vals)


class Ensemble_Tester(unittest.TestCase):
def setUp(self):
class TestEnsemble:
def setup_method(self):
self.db = pandas.DataFrame(
{"X": [0, 0.1, 4, 6, 5], "Y": [0, 0.2, 5, 7, 5]}
).rename(lambda i: "i_" + str(i))
Expand Down Expand Up @@ -192,8 +198,8 @@ def test_ensemble(self):
np.testing.assert_almost_equal(ensemble_solu.values, vals)


class Get_Cluster_Boundary_Tester(unittest.TestCase):
def setUp(self):
class TestGetClusterBoundary:
def setup_method(self):
np.random.seed(10)
self.db = pandas.DataFrame(
{"x": np.random.random(25), "y": np.random.random(25)}
Expand Down Expand Up @@ -275,7 +281,7 @@ def test_get_cluster_boundary(self):
"0.5475861559192435, 0.5425443680112613 0.7546476915298572, "
"0.7217553174317995 0.8192869956700687))"
)
self.assertEqual(polys[0].wkt, wkt)
assert polys.iloc[0].wkt == wkt

# ------------------------#
# # Multi Core #
Expand All @@ -290,15 +296,4 @@ def test_get_cluster_boundary(self):
"0.5475861559192435, 0.5425443680112613 0.7546476915298572, "
"0.7217553174317995 0.8192869956700687))"
)
self.assertEqual(polys[0].wkt, wkt)


suite = unittest.TestSuite()
test_classes = [ADBSCAN_Tester, Remap_lbls_Tester, Ensemble_Tester]
for i in test_classes:
a = unittest.TestLoader().loadTestsFromTestCase(i)
suite.addTest(a)

if __name__ == "__main__":
runner = unittest.TextTestRunner()
runner.run(suite)
assert polys.iloc[0].wkt == wkt