Skip to content

Commit

Permalink
Updating permutations and structure from Levi LOSH LJC code review
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcsauer committed Aug 29, 2020
1 parent dfab1cb commit 449e767
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
32 changes: 19 additions & 13 deletions esda/local_geary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
)


PERMUTATIONS = 999
SIG = 0.05


class Local_Geary(BaseEstimator):

"""Local Geary - Univariate"""

def __init__(self, connectivity=None, labels=False, sig=SIG,
permutations=PERMUTATIONS, n_jobs=1, keep_simulations=True,
def __init__(self, connectivity=None, labels=False, sig=0.05,
permutations=999, n_jobs=1, keep_simulations=True,
seed=None):
"""
Initialize a Local_Geary estimator
Arguments
---------
connectivity : scipy.sparse matrix object
the connectivity structure describing
the relationships between observed units.
Expand All @@ -40,9 +40,11 @@ def __init__(self, connectivity=None, labels=False, sig=SIG,
Default significance threshold used for
creation of labels groups.
permutations : int
(default=999)
number of random permutations for calculation
of pseudo p_values
n_jobs : int
(default=1)
Number of cores to be used in the conditional
randomisation. If -1, all available cores are used.
keep_simulations : Boolean
Expand Down Expand Up @@ -77,7 +79,7 @@ def __init__(self, connectivity=None, labels=False, sig=SIG,
self.keep_simulations = keep_simulations
self.seed = seed

def fit(self, x, n_jobs=1, permutations=999):
def fit(self, x):
"""
Arguments
---------
Expand Down Expand Up @@ -109,10 +111,15 @@ def fit(self, x, n_jobs=1, permutations=999):

w = self.connectivity
w.transform = 'r'

permutations = self.permutations
sig = self.sig
n_jobs = self.n_jobs
seed = self.seed

self.localG = self._statistic(x, w)

if self.permutations:
if permutations:
self.p_sim, self.rlocalG = _crand_plus(
z=(x - np.mean(x))/np.std(x),
w=w,
Expand All @@ -131,16 +138,16 @@ def fit(self, x, n_jobs=1, permutations=999):
# Outliers
self.labs[(self.localG < Eij_mean) &
(y > y_mean) &
(self.p_sim <= self.sig)] = 1
(self.p_sim <= sig)] = 1
# Clusters
self.labs[(self.localG < Eij_mean) &
(y < y_mean) &
(self.p_sim <= self.sig)] = 2
(self.p_sim <= sig)] = 2
# Other
self.labs[(self.localG > Eij_mean) &
(self.p_sim <= self.sig)] = 3
(self.p_sim <= sig)] = 3
# Non-significant
self.labs[self.p_sim > self.sig] = 4
self.labs[self.p_sim > sig] = 4

del (self.keep_simulations, self.n_jobs,
self.permutations, self.seed, self.rlocalG,
Expand Down Expand Up @@ -174,7 +181,6 @@ def _statistic(x, w):

# Note: does not using the scaling parameter


@_njit(fastmath=True)
def _local_geary(i, z, permuted_ids, weights_i, scaling):
zi, zrand = _prepare_univariate(i, z, permuted_ids, weights_i)
Expand Down
18 changes: 13 additions & 5 deletions esda/local_geary_mv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@
import libpysal as lp


PERMUTATIONS = 999


class Local_Geary_MV(BaseEstimator):

"""Local Geary - Multivariate"""

def __init__(self, connectivity=None, permutations=PERMUTATIONS):
def __init__(self, connectivity=None, permutations=999):
"""
Initialize a Local_Geary_MV estimator
Arguments
---------
connectivity : scipy.sparse matrix object
the connectivity structure describing
the relationships between observed units.
Need not be row-standardized.
permutations : int
(default=999)
number of random permutations for calculation
of pseudo p_values
Attributes
Expand All @@ -36,7 +37,7 @@ def __init__(self, connectivity=None, permutations=PERMUTATIONS):
self.connectivity = connectivity
self.permutations = permutations

def fit(self, variables, permutations=999):
def fit(self, variables):
"""
Arguments
---------
Expand All @@ -59,6 +60,11 @@ def fit(self, variables, permutations=999):
>>> guerry = lp.examples.load_example('Guerry')
>>> guerry_ds = gpd.read_file(guerry.get_path('Guerry.shp'))
>>> w = libpysal.weights.Queen.from_dataframe(guerry_ds)
>>> import libpysal
>>> import geopandas as gpd
>>> guerry = lp.examples.load_example('Guerry')
>>> guerry_ds = gpd.read_file(guerry.get_path('Guerry.shp'))
>>> w = libpysal.weights.Queen.from_dataframe(guerry_ds)
>>> x1 = guerry_ds['Donatns']
>>> x2 = guerry_ds['Suicids']
>>> lG_mv = Local_Geary(connectivity=w).fit([x1,x2])
Expand All @@ -72,6 +78,8 @@ def fit(self, variables, permutations=999):

self.n = len(variables[0])
self.w = w

permutations = self.permutations

# Caclulate z-scores for input variables
# to be used in _statistic and _crand
Expand Down

0 comments on commit 449e767

Please sign in to comment.