Skip to content

Commit 00d52de

Browse files
authored
Fix Local Moran's I input column checking and exceptions docs (#453)
1 parent f42260e commit 00d52de

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

eis_toolkit/exploratory_analyses/local_morans_i.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from esda.moran import Moran_Local
77

88
from eis_toolkit import exceptions
9-
from eis_toolkit.exceptions import InvalidParameterValueException
109

1110

1211
@beartype
@@ -19,12 +18,12 @@ def _local_morans_i(
1918
elif weight_type == "knn":
2019
w = libpysal.weights.KNN.from_dataframe(gdf, k=k)
2120
else:
22-
raise InvalidParameterValueException("Invalid weight_type. Use 'queen' or 'knn'.")
21+
raise exceptions.InvalidParameterValueException("Invalid weight_type. Use 'queen' or 'knn'.")
2322

2423
w.transform = "R"
2524

2625
if len(gdf[column]) != len(w.weights):
27-
raise InvalidParameterValueException("Dimension mismatch between data and weights matrix.")
26+
raise exceptions.InvalidParameterValueException("Dimension mismatch between data and weights matrix.")
2827

2928
moran_loc = Moran_Local(gdf[column], w, permutations=permutations)
3029

@@ -59,12 +58,18 @@ def local_morans_i(
5958
6059
Raises:
6160
EmptyDataFrameException: The input geodataframe is empty.
61+
InvalidColumnException: The input column is not found in the input geodataframe.
62+
InvalidParameterValueException: Input parameter values for `k` or `permutations` are invalid.
63+
NonNumericDataException: The input column contains non-numeric data.
6264
"""
6365
if gdf.shape[0] == 0:
6466
raise exceptions.EmptyDataFrameException("Geodataframe is empty.")
6567

6668
if column not in gdf.columns:
67-
raise exceptions.InvalidParameterValueException(f"Column '{column}' not found in the GeoDataFrame.")
69+
raise exceptions.InvalidColumnException(f"Column '{column}' not found in the GeoDataFrame.")
70+
71+
if not np.issubdtype(gdf[column].dtype, np.number):
72+
raise exceptions.NonNumericDataException(f"Column '{column}' must contain numeric data.")
6873

6974
if k < 1:
7075
raise exceptions.InvalidParameterValueException("k must be > 0.")

tests/exploratory_analyses/local_morans_i_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_geodataframe_missing_column():
146146

147147
gdf = gpd.GeoDataFrame({"test_col": [1, 2, 3]})
148148

149-
with pytest.raises(exceptions.InvalidParameterValueException):
149+
with pytest.raises(exceptions.InvalidColumnException):
150150
local_morans_i(gdf, column="value", weight_type="queen", k=4, permutations=999)
151151

152152

0 commit comments

Comments
 (0)