Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format, lint, numpydoc – cg/ops/*.py #610

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions libpysal/cg/ops/_accessors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ruff: noqa: F822

import functools as _f

__all__ = [
Expand All @@ -17,13 +19,12 @@
def get_attr(df, geom_col="geometry", inplace=False, attr=None):
outval = df[geom_col].apply(lambda x: x.__getattribute__(attr))
if inplace:
outcol = "shape_{}".format(func.__name__)
outcol = f"shape_{func.__name__}" # noqa F821
df[outcol] = outval
return None
return outval



_doc_template = """
Tabular accessor to grab a geometric object's {n} attribute.

Expand All @@ -40,19 +41,16 @@ def get_attr(df, geom_col="geometry", inplace=False, attr=None):

Returns
-------

``None`` if inplace is set to ``True`` and operation is conducted
on ``df`` in memory. Otherwise, returns a pandas.Series.

See Also
--------

For further documentation about the attributes of the object in question, refer
to shape classes in ``pysal.cg.shapes``.

"""

_accessors = dict()
_accessors = {}
for k in __all__:
_accessors[k] = _f.partial(get_attr, attr=k)
_accessors[k].__doc__ = _doc_template.format(n=k)
Expand Down
25 changes: 10 additions & 15 deletions libpysal/cg/ops/_shapely.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ruff: noqa: F822

import functools as _f
from warnings import warn

from ...common import requires as _requires

__all__ = [
Expand Down Expand Up @@ -45,7 +48,7 @@ def _atomic_op(df, geom_col="geometry", inplace=False, _func=None, **kwargs):
"""Atomic operation internal function."""

outval = df[geom_col].apply(lambda x: _func(x, **kwargs))
outcol = "shape_{}".format(_func.__name__)
outcol = f"shape_{_func.__name__}"
if not inplace:
new = df.copy()
new[outcol] = outval
Expand Down Expand Up @@ -76,14 +79,11 @@ def _atomic_op(df, geom_col="geometry", inplace=False, _func=None, **kwargs):

Notes
-----

Some atomic operations require an 'other' argument.

See Also
--------

pysal.contrib.shapely_ext.{n}

"""

# ensure that the construction of atomics is done only if we can use shapely
Expand Down Expand Up @@ -123,10 +123,8 @@ def cascaded_union(df, geom_col="geometry", **groupby_kws):

See Also
--------

pysal.shapely_ext.cascaded_union
pandas.DataFrame.groupby

"""

by = groupby_kws.pop("by", None)
Expand Down Expand Up @@ -159,31 +157,29 @@ def unary_union(df, geom_col="geometry", **groupby_kws):

See Also
--------

pysal.shapely_ext.unary_union
pandas.DataFrame.groupby

"""

by = groupby_kws.pop("by", None)
level = groupby_kws.pop("level", None)
if by is not None or level is not None:
df = df.groupby(**groupby_kws)
out = df[geom_col].apply(_cascaded_union)
out = df[geom_col].apply(_cascaded_union) # noqa F821
else:
out = _cascaded_union(df[geom_col].tolist())
out = _cascaded_union(df[geom_col].tolist()) # noqa F821
return out


@_requires("shapely")
def _cascaded_intersection(shapes):
it = iter(shapes)
outshape = next(it)
for i, shape in enumerate(it):
for _i, shape in enumerate(it):
try:
outshape = _s.intersection(shape, outshape)
except NotImplementedError:
warn("An intersection is empty!")
warn("An intersection is empty!", stacklevel=2)
return None
return outshape

Expand All @@ -204,14 +200,13 @@ def cascaded_intersection(df, geom_col="geometry", **groupby_kws):
Returns
-------
out : {libpysal.cg.{Point, Chain, Polygon}, pandas.DataFrame}
A PySAL shape or a dataframe of shapes resulting from the intersection operation.
A PySAL shape or a dataframe of shapes resulting
from the intersection operation.

See Also
--------

pysal.shapely_ext.cascaded_intersection
pandas.DataFrame.groupby

"""

by = groupby_kws.pop("by", None)
Expand Down
2 changes: 1 addition & 1 deletion libpysal/cg/ops/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from . import _shapely as _s

# prefer access to shapely computation
_all = dict()
_all = {}
_all.update(_s.__dict__)
_all.update(_a.__dict__)

Expand Down
24 changes: 12 additions & 12 deletions libpysal/cg/ops/tabular.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from ...common import requires as _requires
from ...io.geotable.utils import to_gdf, to_df
from warnings import warn as _Warn
import functools as _f
import sys as _sys
from warnings import warn

from ...common import requires as _requires
from ...io.geotable.utils import to_df, to_gdf

try:
import pandas as _pd
Expand All @@ -12,7 +12,6 @@
def join(*args, **kwargs):
return _pd.merge(*args, **kwargs)


except ImportError:
pass

Expand Down Expand Up @@ -53,13 +52,12 @@ def spatial_join(
the suffix to apply to overlapping column names from ``df1``.;
and (4) ``'rsuffix'`` defaults to ``right'``),
the suffix to apply to overlapping column names from ``df2``.

Returns
-------
df : pandas.DataFrame
A pandas.DataFrame with a new set of polygons
and attributes resulting from the overlay.

"""

import geopandas as gpd
Expand Down Expand Up @@ -103,13 +101,12 @@ def spatial_overlay(
Default is ``'geometry'``.
**kwargs : dict
Optional keyword arguments passed in ``geopandas.tools.overlay``.

Returns
-------
df : pandas.DataFrame
A pandas.DataFrame with a new set of polygons
and attributes resulting from the overlay.

"""

import geopandas as gpd
Expand All @@ -130,13 +127,13 @@ def dissolve(df, by="", **groupby_kws):
return union(df, by=by, **groupby_kws)


def clip(return_exterior=False):
def clip(return_exterior=False): # noqa ARG001
# return modified entries of the df that are within an envelope
# provide an option to null out the geometries instead of not returning
raise NotImplementedError


def erase(return_interior=True):
def erase(return_interior=True): # noqa ARG001
# return modified entries of the df that are outside of an envelope
# provide an option to null out the geometries instead of not returning
raise NotImplementedError
Expand All @@ -145,7 +142,10 @@ def erase(return_interior=True):
@_requires("shapely")
def union(df, **kws):
if "by" in kws:
warn("When a 'by' argument is provided, you should be using 'dissolve'.")
warn(
"When a 'by' argument is provided, you should be using 'dissolve'.",
stacklevel=2,
)
return dissolve(df, **kws)
from ._shapely import cascaded_union as union

Expand Down