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

Ruff/numpydoc fixes #439

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# See https://pre-commit.com/hooks.html for more hooks
# See https://pre-commit.ci/#configuration
# See https://github.com/scientific-python/cookie#sp-repo-review
# See https://github.com/SciTools/.github/wiki/Linting for common linter rules

ci:
autofix_prs: false
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/asv_delegated_conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
identifier string.

tagged_env_vars : dict
Environment variables, tagged for build vs. non-build
Environment variables, tagged for build vs. non-build.

"""
ignored = ["`python`"]
Expand Down Expand Up @@ -150,11 +150,11 @@ def copy_asv_files(src_parent: Path, dst_parent: Path) -> None:
local_envs = dict(environ)
local_envs.update(env)
if cwd is None:
cwd = str(build_dir)
_cwd = str(build_dir)
_ = asv_util.check_output(
command,
timeout=self._install_timeout,
cwd=cwd,
cwd=_cwd,
env=local_envs,
valid_return_codes=return_codes,
)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benchmarks/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def run_function_elsewhere(func_to_run, *args, **kwargs):
func_call_string = (
f"{func_to_run.__name__}(" + ",".join(func_call_term_strings) + ")"
)
python_string = "\n".join([func_string, func_call_string])
python_string = f"{func_string}\n{func_call_string}"
old_esmf_mk_file = environ.get(ESMFMKFILE, None)
environ[ESMFMKFILE] = str(Path(sys.executable).parents[1] / "lib" / "esmf.mk")
result = run(
Expand Down
8 changes: 5 additions & 3 deletions esmf_regrid/_esmf_sdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __init__(
Array describing the areas associated with
each face. If ``None``, then :mod:`esmpy` will use its own
calculated areas.
mask: :obj:`~numpy.typing.ArrayLike`, optional
mask : :obj:`~numpy.typing.ArrayLike`, optional
Array describing which elements :mod:`esmpy` will ignore.
center : bool, default=False
Describes if the center points of the grid cells are used in regridding
Expand Down Expand Up @@ -370,9 +370,11 @@ def __init__(

# Ensure bounds are strictly increasing.
if not np.all(lonbounds[:-1] < lonbounds[1:]):
raise ValueError("The longitude bounds must be strictly increasing.")
e_msg = "The longitude bounds must be strictly increasing."
raise ValueError(e_msg)
if not np.all(latbounds[:-1] < latbounds[1:]):
raise ValueError("The latitude bounds must be strictly increasing.")
e_msg = "The latitude bounds must be strictly increasing."
raise ValueError(e_msg)

self.resolution = resolution
self.n_lons_orig = len(lonbounds) - 1
Expand Down
12 changes: 6 additions & 6 deletions esmf_regrid/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class Constants:
"""Encompassing class for best practice import."""

class Method(Enum):
"""holds enums for Method values."""
"""Holds enums for Method values."""

CONSERVATIVE = esmpy.RegridMethod.CONSERVE
BILINEAR = esmpy.RegridMethod.BILINEAR
NEAREST = esmpy.RegridMethod.NEAREST_STOD

class NormType(Enum):
"""holds enums for norm types."""
"""Holds enums for norm types."""

FRACAREA = esmpy.api.constants.NormType.FRACAREA
DSTAREA = esmpy.api.constants.NormType.DSTAREA
Expand All @@ -41,9 +41,10 @@ def check_method(method):
elif method in method_dict.values():
result = method
else:
raise ValueError(
e_msg = (
f"Method must be a member of `Constants.Method` enum, instead got {method}"
)
raise ValueError(e_msg)
return result


Expand All @@ -54,7 +55,6 @@ def check_norm(norm):
elif norm in norm_dict.values():
result = norm
else:
raise ValueError(
f"NormType must be a member of `Constants.NormType` enum, instead got {norm}"
)
e_msg = f"NormType must be a member of `Constants.NormType` enum, instead got {norm}"
raise ValueError(e_msg)
return result
10 changes: 5 additions & 5 deletions esmf_regrid/esmf_regridder.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ def __init__(
)
else:
if not scipy.sparse.isspmatrix(precomputed_weights):
raise ValueError(
"Precomputed weights must be given as a sparse matrix."
)
e_msg = "Precomputed weights must be given as a sparse matrix."
raise ValueError(e_msg)
if precomputed_weights.shape != (self.tgt.size, self.src.size):
msg = "Expected precomputed weights to have shape {}, got shape {} instead."
raise ValueError(
Expand All @@ -146,7 +145,7 @@ def regrid(self, src_array, norm_type=Constants.NormType.FRACAREA, mdtol=1):
Parameters
----------
src_array : :obj:`~numpy.typing.ArrayLike`
Array whose shape is compatible with ``self.src``
Array whose shape is compatible with ``self.src``.
norm_type : :class:`Constants.NormType`
Either ``Constants.NormType.FRACAREA`` or ``Constants.NormType.DSTAREA``.
Determines the type of normalisation applied to the weights.
Expand All @@ -172,10 +171,11 @@ def regrid(self, src_array, norm_type=Constants.NormType.FRACAREA, mdtol=1):
array_shape = src_array.shape
main_shape = array_shape[-self.src.dims :]
if main_shape != self.src.shape:
raise ValueError(
e_msg = (
f"Expected an array whose shape ends in {self.src.shape}, "
f"got an array with shape ending in {main_shape}."
)
raise ValueError(e_msg)
extra_shape = array_shape[: -self.src.dims]
extra_size = max(1, np.prod(extra_shape))
src_inverted_mask = self.src._array_to_matrix(~ma.getmaskarray(src_array))
Expand Down
16 changes: 8 additions & 8 deletions esmf_regrid/experimental/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,19 @@ def _managed_var_name(src_cube, tgt_cube):
if src_cube.mesh is not None:
src_mesh = src_cube.mesh
src_mesh_coords = src_mesh.coords()
for coord in src_mesh_coords:
src_coord_names.append(coord.var_name)
src_coord_names = [coord.var_name for coord in src_mesh_coords]
tgt_coord_names = []
tgt_mesh_coords = []
if tgt_cube.mesh is not None:
tgt_mesh = tgt_cube.mesh
tgt_mesh_coords = tgt_mesh.coords()
for coord in tgt_mesh_coords:
tgt_coord_names.append(coord.var_name)
tgt_coord_names = [coord.var_name for coord in tgt_mesh_coords]

try:
for coord in src_mesh_coords:
coord.var_name = "_".join([SOURCE_NAME, "mesh", coord.name()])
coord.var_name = f"{SOURCE_NAME}_mesh_{coord.name()}"
for coord in tgt_mesh_coords:
coord.var_name = "_".join([TARGET_NAME, "mesh", coord.name()])
coord.var_name = f"{TARGET_NAME}_mesh_{coord.name()}"
yield None
finally:
for coord, var_name in zip(src_mesh_coords, src_coord_names, strict=False):
Expand Down Expand Up @@ -154,7 +152,8 @@ def _standard_mesh_cube(mesh, location, name):
src_mesh, src_location = src_grid
src_cube = _standard_mesh_cube(src_mesh, src_location, SOURCE_NAME)
else:
raise ValueError("Improper type for `rg._src`.")
e_msg = "Improper type for `rg._src`."
raise ValueError(e_msg)
_add_mask_to_cube(rg.src_mask, src_cube, SOURCE_MASK_NAME)

tgt_grid = rg._tgt
Expand All @@ -166,7 +165,8 @@ def _standard_mesh_cube(mesh, location, name):
tgt_mesh, tgt_location = tgt_grid
tgt_cube = _standard_mesh_cube(tgt_mesh, tgt_location, TARGET_NAME)
else:
raise ValueError("Improper type for `rg._tgt`.")
e_msg = "Improper type for `rg._tgt`."
raise ValueError(e_msg)
_add_mask_to_cube(rg.tgt_mask, tgt_cube, TARGET_MASK_NAME)
elif regridder_type == "GridToMeshESMFRegridder":
src_grid = (rg.grid_y, rg.grid_x)
Expand Down
21 changes: 11 additions & 10 deletions esmf_regrid/experimental/unstructured_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import numpy as np

from .. import esmpy
from .._esmf_sdo import SDO
from esmf_regrid import esmpy
from esmf_regrid._esmf_sdo import SDO


class MeshInfo(SDO):
Expand Down Expand Up @@ -33,29 +33,29 @@ def __init__(

Parameters
----------
node_coords: :obj:`~numpy.typing.ArrayLike`
node_coords : :obj:`~numpy.typing.ArrayLike`
An ``Nx2`` array describing the location of the nodes of the mesh.
``node_coords[:,0]`` describes the longitudes in degrees and
``node_coords[:,1]`` describes the latitudes in degrees.
face_node_connectivity: :obj:`~numpy.typing.ArrayLike`
face_node_connectivity : :obj:`~numpy.typing.ArrayLike`
A masked array describing the face node connectivity of the
mesh. The unmasked points of ``face_node_connectivity[i]`` describe
which nodes are connected to the ``i``'th face.
node_start_index: int
node_start_index : int
A value which, appearing in the ``face_node_connectivity``
array, indicates the first node in the ``node_coords`` array.
UGRID supports both ``0`` based and ``1`` based indexing, so both must be
accounted for here:
https://ugrid-conventions.github.io/ugrid-conventions/#zero-or-one
elem_start_index: int, default=0
https://ugrid-conventions.github.io/ugrid-conventions/#zero-or-one .
elem_start_index : int, default=0
Describes what index should be considered by :mod:`esmpy` to be
the start index for describing its elements. This makes no
difference to the regridding calculation and will only affect the
intermediate :mod:`esmpy` objects, should the user need access to them.
areas: :obj:`~numpy.typing.ArrayLike`, optional
areas : :obj:`~numpy.typing.ArrayLike`, optional
Array describing the areas associated with
each face. If ``None``, then :mod:`esmpy` will use its own calculated areas.
mask: :obj:`~numpy.typing.ArrayLike`, optional
mask : :obj:`~numpy.typing.ArrayLike`, optional
Array describing which elements :mod:`esmpy` will ignore.
elem_coords : :obj:`~numpy.typing.ArrayLike`, optional
An ``Nx2`` array describing the location of the face centers of the mesh.
Expand All @@ -77,10 +77,11 @@ def __init__(
field_kwargs = {"meshloc": esmpy.MeshLoc.NODE}
shape = (len(node_coords),)
else:
raise ValueError(
e_msg = (
f"The mesh location '{location}' is not supported, only "
f"'face' and 'node' are supported."
)
raise ValueError(e_msg)
super().__init__(
shape=shape,
index_offset=1,
Expand Down
15 changes: 10 additions & 5 deletions esmf_regrid/experimental/unstructured_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def regrid_unstructured_to_rectilinear(

"""
if src_cube.mesh is None:
raise ValueError("src_cube has no mesh.")
e_msg = "src_cube has no mesh."
raise ValueError(e_msg)
src_mask = _get_mask(src_cube, use_src_mask)
tgt_mask = _get_mask(grid_cube, use_tgt_mask)
method = check_method(method)
Expand Down Expand Up @@ -165,7 +166,8 @@ def __init__(

"""
if src.mesh is None:
raise ValueError("src has no mesh.")
e_msg = "src has no mesh."
raise ValueError(e_msg)
super().__init__(
src,
tgt,
Expand Down Expand Up @@ -252,7 +254,8 @@ def regrid_rectilinear_to_unstructured(

"""
if mesh_cube.mesh is None:
raise ValueError("mesh_cube has no mesh.")
e_msg = "mesh_cube has no mesh."
raise ValueError(e_msg)
src_mask = _get_mask(src_cube, use_src_mask)
tgt_mask = _get_mask(mesh_cube, use_tgt_mask)
method = check_method(method)
Expand Down Expand Up @@ -333,7 +336,8 @@ def __init__(

"""
if not isinstance(tgt, MeshXY) and tgt.mesh is None:
raise ValueError("tgt has no mesh.")
e_msg = "tgt has no mesh."
raise ValueError(e_msg)
super().__init__(
src,
tgt,
Expand Down Expand Up @@ -404,7 +408,8 @@ def regrid_unstructured_to_unstructured(
"""
method = check_method(method)
if tgt_mesh_cube.mesh is None:
raise ValueError("mesh_cube has no mesh.")
e_msg = "mesh_cube has no mesh."
raise ValueError(e_msg)
src_mask = _get_mask(src_mesh_cube, use_src_mask)
tgt_mask = _get_mask(tgt_mesh_cube, use_tgt_mask)

Expand Down
Loading
Loading