Skip to content

Commit

Permalink
drop black - reup linting w/0 Python 3.8 pin (#332)
Browse files Browse the repository at this point in the history
* adopt ruff-format -- add pre-commit

* remove py38 target linting pin

* ruff formating esda/

* lint topo.py

* lint tabular.py

* lint smoother.py

* lint smaup

* lint silhouettes.py

* lint shape.py

* lint moran.py

* lint mixture_smoothing.py

* lint map_comparison.py

* lint losh.py

* lint lee.py

* lint join_counts.py

* lint join_counts_local.py

* lint join_counts_local_mv.py

* lint join_counts_local_bv.py

* lint getisord.py

* lint geary.py

* lint geary_local.py

* lint geary_local_mv.py

* lint gamma.py

* lint crand.py

* lint adbscan.py
  • Loading branch information
jGaboardi authored Jul 20, 2024
1 parent 6c34d27 commit c6ca25f
Show file tree
Hide file tree
Showing 24 changed files with 255 additions and 264 deletions.
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
files: "esda\/"
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.5.3"
hooks:
- id: ruff
- id: ruff-format

ci:
autofix_prs: false
autoupdate_schedule: quarterly
24 changes: 12 additions & 12 deletions esda/adbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def __init__(
self.pct_thr = pct_thr
self.keep_solus = keep_solus

def fit(self, X, y=None, sample_weight=None, xy=["X", "Y"]):
def fit(self, X, y=None, sample_weight=None, xy=["X", "Y"]): # noqa: ARG002
"""
Perform ADBSCAN clustering from fetaures
...
Expand All @@ -181,14 +181,16 @@ def fit(self, X, y=None, sample_weight=None, xy=["X", "Y"]):
solus = pandas.DataFrame(
np.zeros((X.shape[0], self.reps), dtype=str),
index=X.index,
columns=["rep-%s" % str(i).zfill(zfiller) for i in range(self.reps)],
columns=[f"rep-{str(i).zfill(zfiller)}" for i in range(self.reps)],
)
# Multi-core implementation of parallel draws
if (self.n_jobs == -1) or (self.n_jobs > 1):
# Set different parallel seeds!!!
warnings.warn(
"Multi-core implementation only works on relabelling solutions. "
"Execution of draws is still sequential."
"Execution of draws is still sequential.",
UserWarning,
stacklevel=2,
)
for i in range(self.reps):
pars = (
Expand Down Expand Up @@ -346,7 +348,7 @@ def remap_lbls(solus, xys, xy=["X", "Y"], n_jobs=1):
remapped_solus.loc[:, ref] = solus.loc[:, ref]
return remapped_solus.fillna(lbl_type(-1)).astype(lbl_type)
else:
warnings.warn("No clusters identified.")
warnings.warn("No clusters identified.", UserWarning, stacklevel=2)
return solus


Expand Down Expand Up @@ -418,7 +420,9 @@ def ensemble(solus_relabelled):
"""

counts = np.array(
list(map(lambda a: Counter(a).most_common(1)[0], solus_relabelled.values))
list( # noqa: C417
map(lambda a: Counter(a).most_common(1)[0], solus_relabelled.values)
)
)
winner = counts[:, 0]
votes = counts[:, 1].astype(int) / solus_relabelled.shape[1]
Expand All @@ -441,11 +445,7 @@ def _setup_pool(n_jobs):
"""
import multiprocessing as mp

if n_jobs == -1:
pool = mp.Pool(mp.cpu_count())
else:
pool = mp.Pool(n_jobs)
return pool
return mp.Pool(mp.cpu_count()) if n_jobs == -1 else mp.Pool(n_jobs)


def get_cluster_boundary(labels, xys, xy=["X", "Y"], n_jobs=1, crs=None, step=1):
Expand Down Expand Up @@ -503,13 +503,13 @@ def get_cluster_boundary(labels, xys, xy=["X", "Y"], n_jobs=1, crs=None, step=1)
>>> polys = get_cluster_boundary(labels, db)
>>> polys[0].wkt
'POLYGON ((0.7217553174317995 0.8192869956700687, 0.7605307121989587 0.9086488808086682, 0.9177741225129434 0.8568503024577332, 0.8126209616521135 0.6262871483113925, 0.6125260668293881 0.5475861559192435, 0.5425443680112613 0.7546476915298572, 0.7217553174317995 0.8192869956700687))'
""" # noqa E501
""" # noqa: E501

try:
from geopandas import GeoSeries
except ModuleNotFoundError:

def GeoSeries(data, index=None, crs=None):
def GeoSeries(data, index=None, crs=None): # noqa: ARG001, N802
return list(data)

lbl_type = type(labels.iloc[0])
Expand Down
36 changes: 14 additions & 22 deletions esda/crand.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from numba import boolean, jit, njit, prange
except (ImportError, ModuleNotFoundError):

def jit(*dec_args, **dec_kwargs):
def jit(*dec_args, **dec_kwargs): # noqa: ARG001
"""
decorator mimicking numba.jit
"""

def intercepted_function(f, *f_args, **f_kwargs):
def intercepted_function(f, *f_args, **f_kwargs): # noqa: ARG001
return f

return intercepted_function
Expand Down Expand Up @@ -174,7 +174,7 @@ def crand(

if n_jobs != 1:
try:
import joblib # noqa F401
import joblib # noqa: F401
except (ModuleNotFoundError, ImportError):
warnings.warn(
f"Parallel processing is requested (n_jobs={n_jobs}),"
Expand Down Expand Up @@ -304,10 +304,7 @@ def compute_chunk(
chunk_n = z_chunk.shape[0]
n = z.shape[0]
larger = np.zeros((chunk_n,), dtype=np.int64)
if keep:
rlocals = np.empty((chunk_n, permuted_ids.shape[0]))
else:
rlocals = np.empty((1, 1))
rlocals = np.empty((chunk_n, permuted_ids.shape[0])) if keep else np.empty((1, 1))

mask = np.ones((n,), dtype=np.int8) == 1
wloc = 0
Expand All @@ -322,7 +319,7 @@ def compute_chunk(
weights_i = np.zeros(cardinality + 1, dtype=other_weights.dtype)
weights_i[0] = self_weights[i]
# this chomps the next `cardinality` weights off of `weights`
weights_i[1:] = other_weights[wloc : (wloc + cardinality)] # noqa E203
weights_i[1:] = other_weights[wloc : (wloc + cardinality)]
wloc += cardinality
mask[chunk_start + i] = False
rstats = stat_func(chunk_start + i, z, permuted_ids, weights_i, scaling)
Expand Down Expand Up @@ -362,7 +359,7 @@ def build_weights_offsets(cardinalities: np.ndarray, n_chunks: int):
chunk_size = np.int64(n / n_chunks) + 1
start = 0
for i in range(n_chunks):
advance = cardinalities[start : start + chunk_size].sum() # noqa E203
advance = cardinalities[start : start + chunk_size].sum()
boundary_points[i + 1] = boundary_points[i] + advance
start += chunk_size
return boundary_points
Expand Down Expand Up @@ -424,13 +421,11 @@ def chunk_generator(
chunk_size = starts[1] - starts[0]
for i in range(n_jobs):
start = starts[i]
z_chunk = z[start : (start + chunk_size)] # noqa E203
self_weights_chunk = self_weights[start : (start + chunk_size)] # noqa E203
observed_chunk = observed[start : (start + chunk_size)] # noqa E203
cardinalities_chunk = cardinalities[start : (start + chunk_size)] # noqa E203
w_chunk = other_weights[
w_boundary_points[i] : w_boundary_points[i + 1] # noqa E203
]
z_chunk = z[start : (start + chunk_size)]
self_weights_chunk = self_weights[start : (start + chunk_size)]
observed_chunk = observed[start : (start + chunk_size)]
cardinalities_chunk = cardinalities[start : (start + chunk_size)]
w_chunk = other_weights[w_boundary_points[i] : w_boundary_points[i + 1]]
yield (
start,
z_chunk,
Expand Down Expand Up @@ -522,10 +517,7 @@ def parallel_crand(
# ------------------------------------------------------------------
# Set up output holders
larger = np.zeros((n,), dtype=np.int64)
if keep:
rlocals = np.empty((n, permuted_ids.shape[0]))
else:
rlocals = np.empty((1, 1))
rlocals = np.empty((n, permuted_ids.shape[0])) if keep else np.empty((1, 1))
# ------------------------------------------------------------------
# Joblib parallel loop by chunks

Expand All @@ -548,7 +540,7 @@ def parallel_crand(
)
for pars in chunks
)
larger, rlocals = zip(*worker_out)
larger, rlocals = zip(*worker_out, strict=True)
larger = np.hstack(larger).squeeze()
rlocals = np.row_stack(rlocals).squeeze()
return larger, rlocals
Expand Down Expand Up @@ -591,6 +583,6 @@ def _prepare_bivariate(i, z, permuted_ids, weights_i):


@njit(fastmath=True)
def local(i, z, permuted_ids, weights_i, scaling):
def local(i, z, permuted_ids, weights_i, scaling): # noqa: ARG001
raise NotImplementedError
# returns (k_permutations,) array of random statistics for observation i
8 changes: 4 additions & 4 deletions esda/gamma.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def __calc_w(self, z, op):
for i, i0 in enumerate(self.w.id_order):
neighbors = self.w.neighbor_offsets[i0]
wijs = self.w.weights[i0]
zw = list(zip(neighbors, wijs))
zw = list(zip(neighbors, wijs, strict=True))
zs[i] = sum(
[wij * (z2[i] - 2.0 * z[i] * z[j] + z2[j]) for j, wij in zw]
)
Expand All @@ -224,15 +224,15 @@ def __calc_w(self, z, op):
for i, i0 in enumerate(self.w.id_order):
neighbors = self.w.neighbor_offsets[i0]
wijs = self.w.weights[i0]
zw = list(zip(neighbors, wijs))
zw = list(zip(neighbors, wijs, strict=True))
zs[i] = sum([wij * abs(z[i] - z[j]) for j, wij in zw])
g = zs.sum()
else: # any previously defined function op
zs = np.zeros(z.shape)
for i, i0 in enumerate(self.w.id_order):
neighbors = self.w.neighbor_offsets[i0]
wijs = self.w.weights[i0]
zw = list(zip(neighbors, wijs))
zw = list(zip(neighbors, wijs, strict=True))
zs[i] = sum([wij * op(z, i, j) for j, wij in zw])
g = zs.sum()

Expand Down Expand Up @@ -282,7 +282,7 @@ def by_col(
"The `.by_col()` methods are deprecated and will be "
"removed in a future version of `esda`."
)
warnings.warn(msg, FutureWarning)
warnings.warn(msg, FutureWarning, stacklevel=2)

return _univariate_handler(
df,
Expand Down
4 changes: 2 additions & 2 deletions esda/geary.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Geary:
"""

def __init__(self, y, w, transformation="r", permutations=999):
if not isinstance(w, (weights.W, graph.Graph)):
if not isinstance(w, weights.W | graph.Graph):
raise TypeError(
"w must be a libpysal.weights.W or libpysal.graph.Graph object, "
f"got {type(w)} instead."
Expand Down Expand Up @@ -237,7 +237,7 @@ def by_col(
"The `.by_col()` methods are deprecated and will be "
"removed in a future version of `esda`."
)
warnings.warn(msg, FutureWarning)
warnings.warn(msg, FutureWarning, stacklevel=2)

return _univariate_handler(
df,
Expand Down
4 changes: 2 additions & 2 deletions esda/geary_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from esda.crand import njit as _njit


class Geary_Local(BaseEstimator):
class Geary_Local(BaseEstimator): # noqa: N801
"""Local Geary - Univariate"""

def __init__(
Expand Down Expand Up @@ -206,7 +206,7 @@ def _statistic(x, w, drop_islands):


@_njit(fastmath=True)
def _local_geary(i, z, permuted_ids, weights_i, scaling):
def _local_geary(i, z, permuted_ids, weights_i, scaling): # noqa: ARG001
other_weights = weights_i[1:]
zi, zrand = _prepare_univariate(i, z, permuted_ids, other_weights)
return (zi - zrand) ** 2 @ other_weights
6 changes: 3 additions & 3 deletions esda/geary_local_mv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sklearn.utils import check_array


class Geary_Local_MV(BaseEstimator):
class Geary_Local_MV(BaseEstimator): # noqa: N801
"""Local Geary - Multivariate"""

def __init__(self, connectivity=None, permutations=999, drop_islands=True):
Expand Down Expand Up @@ -199,15 +199,15 @@ def _crand(self, zvariables):
np.random.shuffle(idsi)
vars_rand = []
for j in range(nvars):
vars_rand.append(zvariables[j][idsi[rids[:, 0 : wc[i]]]]) # noqa E203
vars_rand.append(zvariables[j][idsi[rids[:, 0 : wc[i]]]])
# vars rand as tmp
# Calculate diff
diff = []
for z in range(nvars):
_diff = np.array((zvariables[z][i] - vars_rand[z]) ** 2 * w[i])
diff.append(_diff.sum(1) / nvars)
# add up differences
temp = np.array([sum(x) for x in zip(*diff)])
temp = np.array([sum(x) for x in zip(*diff, strict=True)])
# Assign to object to be returned
Gs[i] = temp
self.Gs = Gs
18 changes: 9 additions & 9 deletions esda/getisord.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def by_col(
"The `.by_cols()` methods are deprecated and will be "
"removed in a future version of `esda`."
)
warnings.warn(msg, FutureWarning)
warnings.warn(msg, FutureWarning, stacklevel=2)

return _univariate_handler(
df,
Expand All @@ -245,7 +245,7 @@ def by_col(
)


class G_Local:
class G_Local: # noqa: N801
"""
Generalized Local G Autocorrelation
Expand Down Expand Up @@ -465,7 +465,7 @@ def __crand(self, keep_simulations):
rids = np.array([np.random.permutation(rid)[0:k] for i in prange])
ids = np.arange(self.w.n)
wc = self.__getCardinalities()
if self.w_transform == "r":
if self.w_transform == "r": # noqa: SIM108 -- keeping readability
den = np.array(wc) + self.star
else:
den = np.ones(self.w.n)
Expand All @@ -485,7 +485,7 @@ def __crand(self, keep_simulations):
larger[below] = self.permutations - larger[below]
self.p_sim = (larger + 1) / (self.permutations + 1)

def __getCardinalities(self):
def __getCardinalities(self): # noqa: N802
if isinstance(self.w, W):
ido = self.w.id_order
self.wc = np.array([self.w.cardinalities[ido[i]] for i in range(self.n)])
Expand Down Expand Up @@ -579,7 +579,7 @@ def by_col(
"The `.by_col()` methods are deprecated and will be "
"removed in a future version of `esda`."
)
warnings.warn(msg, FutureWarning)
warnings.warn(msg, FutureWarning, stacklevel=2)

return _univariate_handler(
df,
Expand Down Expand Up @@ -613,7 +613,7 @@ def _infer_star_and_structure_w(weights, star, transform):
else:
weights = weights.assign_self_weight(0).eliminate_zeros()
# Want nonzero diagonal and have it
elif (not zero_diagonal) & (star is True):
elif (not zero_diagonal) & (star is True): # noqa: SIM114 -- keeping readability
weights = weights
# Want zero diagonal and have it
elif zero_diagonal & (star is False):
Expand All @@ -639,15 +639,15 @@ def _infer_star_and_structure_w(weights, star, transform):
# this works successfully for effectively binary but "O"-transformed input
elif transform.lower() == "r":
# This warning is presented in the documentation as well
warnings.warn(star_warn)
warnings.warn(star_warn, UserWarning, stacklevel=2)
weights = fill_diagonal(
weights, np.asarray(adj_matrix.max(axis=1).todense()).flatten()
)
else:
if transform.lower() == "b" or weights.transformation.lower() == "b":
weights = weights.assign_self_weight(1)
elif transform.lower() == "r":
warnings.warn(star_warn)
warnings.warn(star_warn, UserWarning, stacklevel=2)
weights = weights.assign_self_weight(
np.asarray(adj_matrix.max(axis=1).todense()).flatten()
)
Expand All @@ -661,7 +661,7 @@ def _infer_star_and_structure_w(weights, star, transform):
raise TypeError(
f"Type of star ({type(star)}) not understood."
f" Must be an integer, boolean, float, or numpy.ndarray."
)
) from None
star = (weights.sparse.diagonal() > 0).any()
if isinstance(weights, W):
weights.transform = transform
Expand Down
4 changes: 2 additions & 2 deletions esda/join_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
PERMUTATIONS = 999


class Join_Counts:
class Join_Counts: # noqa: N801
"""Binary Join Counts
Expand Down Expand Up @@ -308,7 +308,7 @@ def by_col(
"The `.by_col()` methods are deprecated and will be "
"removed in a future version of `esda`."
)
warnings.warn(msg, FutureWarning)
warnings.warn(msg, FutureWarning, stacklevel=2)

if outvals is None:
outvals = []
Expand Down
Loading

0 comments on commit c6ca25f

Please sign in to comment.