3
3
import geopandas
4
4
import pytest
5
5
from libpysal .weights import Queen
6
+ from libpysal .graph import Graph
6
7
from sklearn .linear_model import TheilSenRegressor
7
8
from esda .moran_local_mv import Partial_Moran_Local , Auxiliary_Moran_Local
8
9
from esda .moran import Moran_Local_BV
12
13
13
14
y = df .HH_INC .values .reshape (- 1 ,1 )
14
15
X = df .HSG_VAL .values .reshape (- 1 ,1 )
15
- w = Queen .from_dataframe (df )
16
- w .transform = 'r'
16
+ w_classic = Queen .from_dataframe (df )
17
+ w_classic .transform = 'r'
18
+ g = Graph .build_contiguity (df ).transform ("r" )
17
19
18
- def test_partial_runs ():
20
+ pytestmark = pytest .mark .parametrize ("w" , [g , w_classic ])
21
+
22
+ def test_partial_runs (w ):
19
23
"""Check if the class computes successfully in a default configuration"""
20
24
m = Partial_Moran_Local (y ,X ,w , permutations = 1 )
21
25
# done, just check if it runs
22
26
23
- def test_partial_accuracy ():
27
+ def test_partial_accuracy (w ):
24
28
"""Check if the class outputs expected results at a given seed"""
25
29
numpy .random .seed (111221 )
26
30
m = Partial_Moran_Local (y ,X ,w , permutations = 10 )
27
31
# compute result by hand
28
32
zy = (y - y .mean ())/ y .std ()
29
- wz = (w .sparse * zy )
33
+ wz = (w .sparse @ zy )
30
34
zx = (X - X .mean (axis = 0 ))/ X .std (axis = 0 )
31
35
rho = numpy .corrcoef (zy .squeeze (), zx .squeeze ())[0 ,1 ]
32
36
left = zy - rho * zx
@@ -45,7 +49,7 @@ def test_partial_accuracy():
45
49
is_odd_label = m .labels_ % 2
46
50
numpy .testing .assert_equal (is_cluster , is_odd_label )
47
51
48
- def test_partial_unscaled ():
52
+ def test_partial_unscaled (w ):
49
53
"""Check if the variance scaling behaves as expected"""
50
54
m = Partial_Moran_Local (y ,X ,w , permutations = 0 , unit_scale = True )
51
55
m2 = Partial_Moran_Local (y ,X ,w , permutations = 0 , unit_scale = False )
@@ -55,33 +59,35 @@ def test_partial_unscaled():
55
59
assert s1y > s2y , "variance is incorrectly scaled for y"
56
60
assert s1x < s2x , "variance is incorrectly scaled for x"
57
61
58
- def test_partial_uvquads ():
62
+ def test_partial_uvquads (w ):
59
63
"""Check that the quadrant decisions vary correctly with the inputs"""
60
64
m = Partial_Moran_Local (y ,X ,w , permutations = 0 , mvquads = False )
61
65
bv = Moran_Local_BV (y ,X ,w ,permutations = 0 )
62
66
# 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
67
numpy .testing .assert_array_equal (m .quads_ , bv .q )
64
68
65
- def test_aux_runs ():
69
+ def test_aux_runs (w ):
70
+ print (type (w ), w .transform )
66
71
"""Check that the class completes successfully in a default configuration"""
67
- a = Auxiliary_Moran_Local (y ,X ,w , permutations = 1 )
72
+ a = Auxiliary_Moran_Local (y ,X , w , permutations = 1 )
68
73
#done, just check if it runs
69
74
70
- def test_aux_accuracy ():
75
+ def test_aux_accuracy (w ):
76
+ print (type (w ), w .transform )
71
77
"""Check that the class outputs expected values for a given seed"""
72
78
numpy .random .seed (111221 )
73
79
a = Auxiliary_Moran_Local (y ,X ,w , permutations = 10 )
74
80
75
81
# compute result by hand
76
82
zy = (y - y .mean ())/ y .std ()
77
- wz = (w .sparse * zy )
83
+ wz = (w .sparse @ zy )
78
84
zx = (X - X .mean (axis = 0 ))/ X .std (axis = 0 )
79
- wzx = w .sparse * zx
85
+ wzx = w .sparse @ zx
80
86
rho = numpy .corrcoef (zy .squeeze (), zx .squeeze ())[0 ,1 ]
81
87
mean = zy * wz - rho * zx * wz - rho * zy * wzx + rho ** 2 * zx * wzx
82
88
scale = (w .n - 1 ) / (w .n * (1 - rho ** 2 ))
83
89
84
- manual = (mean * scale ).squeeze ()
90
+ manual = numpy . asarray (mean * scale ).squeeze ()
85
91
# check values, may not be identical because of the
86
92
# matrix inversion least squares estimator used in scikit
87
93
numpy .testing .assert_allclose (manual , a .associations_ )
@@ -94,15 +100,17 @@ def test_aux_accuracy():
94
100
is_odd_label = (a .labels_ % 2 ).astype (bool )
95
101
numpy .testing .assert_equal (is_cluster , is_odd_label )
96
102
97
- def test_aux_unscaled ():
103
+ def test_aux_unscaled (w ):
104
+ print (type (w ), w .transform )
98
105
"""Check that the variance scaling behaves as expected"""
99
106
a = Auxiliary_Moran_Local (y ,X / 10000 ,w , permutations = 0 , unit_scale = True )
100
107
a2 = Auxiliary_Moran_Local (y ,X ,w , permutations = 0 , unit_scale = False )
101
108
assert (a .partials_ .std (axis = 0 ) < a2 .partials_ .std (axis = 0 )).all (), (
102
109
"variance is not scaled correctly in partial regression."
103
110
)
104
111
105
- def test_aux_transformer ():
112
+ def test_aux_transformer (w ):
113
+ print (type (w ), w .transform )
106
114
"""Check that an alternative regressor can be used to calculate y|X"""
107
115
a = Auxiliary_Moran_Local (y ,X ,w , permutations = 0 , transformer = TheilSenRegressor )
108
116
# done, should just complete
0 commit comments