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. 1 -- test_{utils,topo}.py #341

Merged
merged 3 commits into from
Jul 21, 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
19 changes: 7 additions & 12 deletions esda/tests/test_topo.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from unittest import TestCase

import numpy
import pandas
import pytest

from ..topo import isolation, prominence, to_elevation, weights


class TopoTester(TestCase):
def setUp(self):
class TestTopo:
def setup_method(self):
self.points = numpy.array(
[[0, 0], [0, 1], [1, 1], [2, 0.5], [0.5, 0.5], [0.75, 0]]
)
self.marks = numpy.array([-1, 0.5, 1, 2, 3, 1.25])
self.cxn = weights.Voronoi(self.points)
self.cxn = weights.Voronoi(self.points, use_index=False)

def test_prominence_valid(self):
w = self.cxn
Expand Down Expand Up @@ -49,7 +47,6 @@ def test_prominence_options(self):
assert not numpy.allclose(default, middle)

def test_isolation_options(self):

pytest.importorskip("rtree")

marks = self.marks
Expand Down Expand Up @@ -79,9 +76,8 @@ def test_isolation_valid(self):
assert numpy.isnan(iso.loc[4, "parent_rank"])
assert (iso.dropna().parent_index == 4).all()
assert (
iso.sort_values("marks", ascending=False).index == (
iso.sort_values("rank").index
)
iso.sort_values("marks", ascending=False).index
== (iso.sort_values("rank").index)
).all()
assert iso.loc[3, "isolation"] == 1.5
assert iso.loc[2, "gap"] == (
Expand All @@ -97,9 +93,8 @@ def test_isolation_valid(self):
assert numpy.isnan(iso.loc[3, "parent_index"])
assert (iso.dropna().parent_index == [4, 2, 5, 5, 3]).all()
assert (
iso.sort_values("marks", ascending=False).index == (
iso.sort_values("rank").index
)
iso.sort_values("marks", ascending=False).index
== (iso.sort_values("rank").index)
).all()
assert iso.loc[1, "isolation"] == 1
assert iso.loc[2, "gap"] == (
Expand Down
22 changes: 5 additions & 17 deletions esda/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import unittest

import libpysal
import numpy as np
import pytest

from .. import moran, util


class Fdr_Tester(unittest.TestCase):
def setUp(self):
class TestFdr:
def setup_method(self):
self.w = libpysal.io.open(libpysal.examples.get_path("stl.gal")).read()
f = libpysal.io.open(libpysal.examples.get_path("stl_hom.txt"))
self.y = np.array(f.by_col["HR8893"])
Expand All @@ -16,16 +15,5 @@ def test_fdr(self):
lm = moran.Moran_Local(
self.y, self.w, transformation="r", permutations=999, seed=10
)
self.assertAlmostEqual(util.fdr(lm.p_sim, 0.1), 0.002564102564102564)
self.assertAlmostEqual(util.fdr(lm.p_sim, 0.05), 0.001282051282051282)


suite = unittest.TestSuite()
test_classes = [Fdr_Tester]
for i in test_classes:
a = unittest.TestLoader().loadTestsFromTestCase(i)
suite.addTest(a)

if __name__ == "__main__":
runner = unittest.TextTestRunner()
runner.run(suite)
assert pytest.approx(util.fdr(lm.p_sim, 0.1)) == 0.002564102564102564
assert pytest.approx(util.fdr(lm.p_sim, 0.05)) == 0.001282051282051282