Skip to content

Commit acd8415

Browse files
committed
finalize all tests, except for broken one
1 parent 6c6ba62 commit acd8415

File tree

1 file changed

+64
-13
lines changed

1 file changed

+64
-13
lines changed

esda/tests/test_moran_local_mv.py

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import numpy
22
import geodatasets
33
import geopandas
4+
import pytest
45
from libpysal.weights import Queen
56
from sklearn.linear_model import TheilSenRegressor
6-
from esda.multivariate_moran import Partial_Moran_Local, Auxiliary_Moran_Local
7+
from esda.moran_local_mv import Partial_Moran_Local, Auxiliary_Moran_Local
8+
from esda.moran import Moran_Local_BV
79

810
df = geopandas.read_file(geodatasets.get_path("geoda.lansing1"))
911
df = df[df.FIPS.str.match("2606500[01234]...") | (df.FIPS == "26065006500")]
1012

11-
y = df.HH_INC.values
12-
X = df.HSG_VAL.values
13+
y = df.HH_INC.values.reshape(-1,1)
14+
X = df.HSG_VAL.values.reshape(-1,1)
1315
w = Queen.from_dataframe(df)
16+
w.transform = 'r'
1417

1518
def test_partial_runs():
1619
"""Check if the class computes successfully in a default configuration"""
@@ -22,36 +25,84 @@ def test_partial_accuracy():
2225
numpy.random.seed(111221)
2326
m = Partial_Moran_Local(y,X,w, permutations=10)
2427
# compute result by hand
28+
zy = (y - y.mean())/y.std()
29+
wz = (w.sparse * zy)
30+
zx = (X - X.mean(axis=0))/X.std(axis=0)
31+
rho = numpy.corrcoef(zy.squeeze(), zx.squeeze())[0,1]
32+
left = zy - rho * zx
33+
scale = (w.n-1) / (w.n * (1 - rho**2))
34+
# (y - rho x)*wy
35+
manual = (left*wz).squeeze() * scale
36+
# check values
37+
numpy.testing.assert_allclose(manual, m.associations_)
38+
39+
# check significances
40+
numpy.testing.assert_equal((m.significances_ < .01).sum(), 18)
41+
numpy.testing.assert_equal((m.significances_[:5] < .1), [True, True, True, False, False])
42+
43+
# check quad
44+
is_cluster = numpy.prod(m.partials_, axis=1) >= 0
45+
is_odd_label = m.labels_ % 2
46+
numpy.testing.assert_equal(is_cluster, is_odd_label)
2547

2648
def test_partial_unscaled():
2749
"""Check if the variance scaling behaves as expected"""
2850
m = Partial_Moran_Local(y,X,w, permutations=0, unit_scale=True)
2951
m2 = Partial_Moran_Local(y,X,w, permutations=0, unit_scale=False)
3052
# variance in the partials_ should be different
53+
s1y,s1x = m.partials_.std(axis=0)
54+
s2y,s2x = m2.partials_.std(axis=0)
55+
assert s1y > s2y, "variance is incorrectly scaled for y"
56+
assert s1x < s2x, "variance is incorrectly scaled for x"
3157

3258
def test_partial_uvquads():
3359
"""Check that the quadrant decisions vary correctly with the inputs"""
3460
m = Partial_Moran_Local(y,X,w, permutations=0, mvquads=False)
35-
...
61+
bv = Moran_Local_BV(y,X,w,permutations=0)
62+
# TODO: this currently fails, and it should pass. I am probably mis-calculating the bivariate quadrants for this option, and need to correct the code.
63+
numpy.testing.assert_array_equal(m.quads_, bv.q)
3664

3765
def test_aux_runs():
3866
"""Check that the class completes successfully in a default configuration"""
39-
m = Auxiliary_Moran_Local(y,X,w, permutations=1)
40-
...
67+
a = Auxiliary_Moran_Local(y,X,w, permutations=1)
68+
#done, just check if it runs
4169

4270
def test_aux_accuracy():
4371
"""Check that the class outputs expected values for a given seed"""
4472
numpy.random.seed(111221)
45-
m = Auxiliary_Moran_Local(y,X,w, permutations=10)
46-
...
73+
a = Auxiliary_Moran_Local(y,X,w, permutations=10)
74+
75+
# compute result by hand
76+
zy = (y - y.mean())/y.std()
77+
wz = (w.sparse * zy)
78+
zx = (X - X.mean(axis=0))/X.std(axis=0)
79+
wzx = w.sparse * zx
80+
rho = numpy.corrcoef(zy.squeeze(), zx.squeeze())[0,1]
81+
mean = zy * wz - rho * zx * wz - rho * zy * wzx + rho**2 * zx * wzx
82+
scale = (w.n-1) / (w.n * (1 - rho**2))
83+
84+
manual = (mean * scale).squeeze()
85+
# check values, may not be identical because of the
86+
# matrix inversion least squares estimator used in scikit
87+
numpy.testing.assert_allclose(manual, a.associations_)
88+
89+
# check significances
90+
numpy.testing.assert_equal((a.significances_ < .01).sum(), 18)
91+
numpy.testing.assert_equal((a.significances_[:5] < .1), [False, False, True, False, False])
92+
93+
is_cluster = numpy.prod(a.partials_, axis=1) >= 0
94+
is_odd_label = (a.labels_ % 2).astype(bool)
95+
numpy.testing.assert_equal(is_cluster, is_odd_label)
4796

4897
def test_aux_unscaled():
4998
"""Check that the variance scaling behaves as expected"""
50-
m = Auxiliary_Moran_Local(y,X,w, permutations=0, unit_scale=True)
51-
m2 = Auxiliary_Moran_Local(y,X,w, permutations=0, unit_scale=False)
52-
...
99+
a = Auxiliary_Moran_Local(y,X/10000,w, permutations=0, unit_scale=True)
100+
a2 = Auxiliary_Moran_Local(y,X,w, permutations=0, unit_scale=False)
101+
assert (a.partials_.std(axis=0) < a2.partials_.std(axis=0)).all(), (
102+
"variance is not scaled correctly in partial regression."
103+
)
53104

54105
def test_aux_transformer():
55106
"""Check that an alternative regressor can be used to calculate y|X"""
56-
m = Auxiliary_Moran_Local(y,X,w, permutations=0, transformer=TheilSenRegressor)
57-
...
107+
a = Auxiliary_Moran_Local(y,X,w, permutations=0, transformer=TheilSenRegressor)
108+
# done, should just complete

0 commit comments

Comments
 (0)