Skip to content

Commit

Permalink
expose cluster labels
Browse files Browse the repository at this point in the history
  • Loading branch information
knaaptime committed Oct 27, 2023
1 parent 6e7026f commit 9855707
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions esda/moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
from warnings import simplefilter

import numpy as np
import pandas as pd
import scipy.stats as stats
from libpysal.weights.spatial_lag import lag_spatial as slag
from matplotlib import colors
from scipy import sparse

from .crand import _prepare_univariate
from .crand import crand as _crand_plus
from .crand import njit as _njit
from .smoothing import assuncao_rate
from .tabular import _bivariate_handler, _univariate_handler
from matplotlib import colors


__all__ = [
"Moran",
Expand Down Expand Up @@ -1185,6 +1185,22 @@ def by_col(
**stat_kws,
)

def get_cluster_labels(self, crit_value=0.05):
"""Return LISA cluster labels for each observation.
Parameters
----------
crit_value : float, optional
crititical significance value for statistical inference, by default 0.05
Returns
-------
numpy.array
an array of cluster labels aligned with the input data used to conduct the
local Moran analysis
"""
return _get_cluster_labels(self, crit_value)

Check warning on line 1202 in esda/moran.py

View check run for this annotation

Codecov / codecov/patch

esda/moran.py#L1202

Added line #L1202 was not covered by tests

def explore(self, gdf, crit_value=0.05, explore_kwargs=None, m=None):
"""Create interactive map of LISA indicators
Expand All @@ -1204,8 +1220,10 @@ def explore(self, gdf, crit_value=0.05, explore_kwargs=None, m=None):
Folium.Map
interactive map with LISA clusters
"""
gdf = gdf.copy()
gdf["Moran Cluster"] = self.get_cluster_labels(crit_value)
return _explore_local_moran(

Check warning on line 1225 in esda/moran.py

View check run for this annotation

Codecov / codecov/patch

esda/moran.py#L1223-L1225

Added lines #L1223 - L1225 were not covered by tests
self, gdf, crit_value=crit_value, explore_kwargs=explore_kwargs, m=m
self, gdf, crit_value, explore_kwargs=explore_kwargs, m=m
)


Expand Down Expand Up @@ -1731,9 +1749,7 @@ def by_col(
df[col] = rate_df[col]


def _explore_local_moran(
moran_local, gdf, crit_value=0.05, explore_kwargs=None, m=None
):
def _explore_local_moran(moran_local, gdf, crit_value, explore_kwargs=None, m=None):
"""Plot local Moran values as an interactive map
Parameters
Expand All @@ -1757,17 +1773,8 @@ def _explore_local_moran(
if explore_kwargs is None:
explore_kwargs = dict()
gdf = gdf.copy()

gdf["q"] = moran_local.q
gdf["p_sim"] = moran_local.p_sim

gdf.loc[
(gdf["p_sim"] < crit_value) & (gdf["q"] == 1), "Moran Cluster"
] = "High-High"
gdf.loc[(gdf["p_sim"] < crit_value) & (gdf["q"] == 2), "Moran Cluster"] = "Low-High"
gdf.loc[(gdf["p_sim"] < crit_value) & (gdf["q"] == 3), "Moran Cluster"] = "Low-Low"
gdf.loc[(gdf["p_sim"] < crit_value) & (gdf["q"] == 4), "Moran Cluster"] = "High-Low"
gdf.loc[gdf["p_sim"] >= crit_value, "Moran Cluster"] = "Insignificant"
gdf["Moran Cluster"] = moran_local.get_cluster_labels(crit_value)
gdf["p-value"] = moran_local.p_sim

Check warning on line 1777 in esda/moran.py

View check run for this annotation

Codecov / codecov/patch

esda/moran.py#L1774-L1777

Added lines #L1774 - L1777 were not covered by tests

x = gdf["Moran Cluster"].values
y = np.unique(x)
Expand All @@ -1783,10 +1790,30 @@ def _explore_local_moran(
if "cmap" in explore_kwargs.keys():
del explore_kwargs["cmap"]

Check warning on line 1791 in esda/moran.py

View check run for this annotation

Codecov / codecov/patch

esda/moran.py#L1791

Added line #L1791 was not covered by tests

m = gdf.explore("Moran Cluster", cmap=hmap, **explore_kwargs)
m = gdf[["Moran Cluster", "p-value", "geometry"]].explore(

Check warning on line 1793 in esda/moran.py

View check run for this annotation

Codecov / codecov/patch

esda/moran.py#L1793

Added line #L1793 was not covered by tests
"Moran Cluster", cmap=hmap, **explore_kwargs
)
return m

Check warning on line 1796 in esda/moran.py

View check run for this annotation

Codecov / codecov/patch

esda/moran.py#L1796

Added line #L1796 was not covered by tests


def _get_cluster_labels(moran_local, crit_value):

gdf = pd.DataFrame()
gdf["q"] = moran_local.q
gdf["p_sim"] = moran_local.p_sim
gdf["Moran Cluster"] = "Insignificant"

Check warning on line 1804 in esda/moran.py

View check run for this annotation

Codecov / codecov/patch

esda/moran.py#L1801-L1804

Added lines #L1801 - L1804 were not covered by tests

gdf.loc[

Check warning on line 1806 in esda/moran.py

View check run for this annotation

Codecov / codecov/patch

esda/moran.py#L1806

Added line #L1806 was not covered by tests
(gdf["p_sim"] < crit_value) & (gdf["q"] == 1), "Moran Cluster"
] = "High-High"
gdf.loc[(gdf["p_sim"] < crit_value) & (gdf["q"] == 2), "Moran Cluster"] = "Low-High"
gdf.loc[(gdf["p_sim"] < crit_value) & (gdf["q"] == 3), "Moran Cluster"] = "Low-Low"
gdf.loc[(gdf["p_sim"] < crit_value) & (gdf["q"] == 4), "Moran Cluster"] = "High-Low"

Check warning on line 1811 in esda/moran.py

View check run for this annotation

Codecov / codecov/patch

esda/moran.py#L1809-L1811

Added lines #L1809 - L1811 were not covered by tests
# gdf.loc[gdf["p_sim"] >= crit_value, "Moran Cluster"] = "Insignificant"

return gdf["Moran Cluster"].values

Check warning on line 1814 in esda/moran.py

View check run for this annotation

Codecov / codecov/patch

esda/moran.py#L1814

Added line #L1814 was not covered by tests


# --------------------------------------------------------------
# Conditional Randomization Moment Estimators
# --------------------------------------------------------------
Expand Down

0 comments on commit 9855707

Please sign in to comment.