Skip to content

Commit

Permalink
Add Ruff lint to github actions (#41)
Browse files Browse the repository at this point in the history
* Lint

* Linting

* add lint workflow

* Update pixi lock
  • Loading branch information
mpiannucci authored Jul 2, 2024
1 parent 9c9808b commit 8d9f041
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 130 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ jobs:
cache: true
# auth-host: prefix.dev
# auth-token: ${{ secrets.PREFIX_DEV_TOKEN }}
- run: pixi run --environment ${{ matrix.environment }} lint
- run: pixi run --environment ${{ matrix.environment }} test


1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,4 @@ xarray_subset_grid/_version.py
.pixi
*.egg-info

.ruff_cache/
1 change: 0 additions & 1 deletion examples/subset_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import numpy as np
import xarray as xr

import xarray_subset_grid.accessor
from xarray_subset_grid.grids import ugrid
from xarray_subset_grid.utils import format_bytes
from xarray_subset_grid.visualization.mpl_plotting import plot_ugrid
Expand Down
4 changes: 2 additions & 2 deletions pixi.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions profiling/STOFS_benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import time

import cf_xarray
import fsspec
import s3fs
import shapely
import thalassa
import xarray as xr
import xugrid as xu

import xarray_subset_grid as xrsg

bucket_name = 'noaa-gestofs-pds'
key = '_para2/stofs_2d_glo.20230819/stofs_2d_glo.t00z.fields.cwl.nc'
url = f"s3://{bucket_name}/{key}"
Expand Down
55 changes: 27 additions & 28 deletions profiling/p_in_p_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,32 @@ def ray_tracing_numpy(x, y, poly):

import numpy as np

# Using a 20 vertex polygon -- arbitrary decision as common max for
# Using a 20 vertex polygon -- arbitrary decision as common max for
# bounding poly used for subsetting.
test_poly = np.array([
[-69.0842494, 41.8576263],
[-69.3834133, 41.6994390],
[-69.4844079, 41.5818408],
[-69.7009389, 41.5498641],
[-70.0628678, 41.5884718],
[-70.3054548, 41.6810850],
[-70.6109682, 41.7607248],
[-70.8657576, 41.9553727],
[-71.1089099, 42.1369069],
[-71.1294295, 42.4274792],
[-70.8877302, 42.6500898],
[-70.7118900, 42.7635708],
[-70.4645152, 42.8363260],
[-70.1066827, 42.8113145],
[-69.9021696, 42.7796958],
[-69.7686684, 42.7210923],
[-69.4055325, 42.5535379],
[-69.1527168, 42.3072355],
[-68.9597074, 42.0243090],
[-68.9939291, 41.9264228],
])
test_poly = np.array(
[
[-69.0842494, 41.8576263],
[-69.3834133, 41.6994390],
[-69.4844079, 41.5818408],
[-69.7009389, 41.5498641],
[-70.0628678, 41.5884718],
[-70.3054548, 41.6810850],
[-70.6109682, 41.7607248],
[-70.8657576, 41.9553727],
[-71.1089099, 42.1369069],
[-71.1294295, 42.4274792],
[-70.8877302, 42.6500898],
[-70.7118900, 42.7635708],
[-70.4645152, 42.8363260],
[-70.1066827, 42.8113145],
[-69.9021696, 42.7796958],
[-69.7686684, 42.7210923],
[-69.4055325, 42.5535379],
[-69.1527168, 42.3072355],
[-68.9597074, 42.0243090],
[-68.9939291, 41.9264228],
]
)

min_lon, min_lat = test_poly.min(axis=0)
max_lon, max_lat = test_poly.max(axis=0)
Expand All @@ -88,8 +90,8 @@ def ray_tracing_numpy(x, y, poly):
# 9.03 ms ± 303 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)



from geometry_utils import polygon_inside

geom = polygon_inside(test_poly, test_points)
# 1000 pts
# In [11]: %timeit polygon_inside(test_poly, test_points)
Expand All @@ -101,7 +103,7 @@ def ray_tracing_numpy(x, y, poly):
# Do they give the same answer?
assert np.array_equal(rtn_inside, geom)

from shapely import contains_xy, Polygon
from shapely import Polygon, contains_xy

sh_poly = Polygon(test_poly)

Expand All @@ -114,7 +116,4 @@ def ray_tracing_numpy(x, y, poly):
# 54.7 ms ± 1.4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)



assert np.array_equal(rtn_inside, shapely)


60 changes: 28 additions & 32 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,38 @@ build-backend = "setuptools.build_meta"

[project]
name = "xarray_subset_grid"
authors = [{ name = "Matthew Iannucci", email = "[email protected]" },
{ name = "Christopher H. Barker", email = "[email protected]" }]
authors = [
{ name = "Matthew Iannucci", email = "[email protected]" },
{ name = "Christopher H. Barker", email = "[email protected]" },
]
description = "Subset Xarray datasets in space"
readme = "README.md"
requires-python = ">=3.10"
keywords = ["xarray"]
license = { file = "LICENSE" }

classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Science/Research",
"Operating System :: OS Independent",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Science/Research",
"Operating System :: OS Independent",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering",
]

dynamic = ["version"]

dependencies = [
"numpy",
"xarray>=2023.10.0",
"cf_xarray",
"numpy",
"xarray>=2023.10.0",
"cf_xarray",
"dask[complete]",
"netcdf4",
]
]

[project.optional-dependencies]
dev = [
Expand All @@ -45,13 +47,7 @@ dev = [
"sphinx",
"sphinx-rtd-theme",
]
examples = [
"fsspec",
"s3fs",
"zarr",
"matplotlib",
"h5netcdf"
]
examples = ["fsspec", "s3fs", "zarr", "matplotlib", "h5netcdf"]

[project.urls]
"Homepage" = "https://github.com/asascience-open/xarray-subset-grid"
Expand All @@ -64,25 +60,23 @@ write_to = "xarray_subset_grid/_version.py"

[tool.ruff]
builtins = ["ellipsis"]
extend-exclude = [
"xarray_subset_grid/_version.py"
]
extend-exclude = ["xarray_subset_grid/_version.py"]
target-version = "py310"
# Use a longer line length.
line-length = 95
line-length = 100

[tool.ruff.lint]
ignore = [
"E402", # module level import not at top of file
"E731", # do not assign a lambda expression, use a def
]
select = [
"F", # Pyflakes
"E", # Pycodestyle
"F", # Pyflakes
"E", # Pycodestyle
"W",
"TID", # flake8-tidy-imports (absolute imports)
"I", # isort
"UP", # Pyupgrade
"I", # isort
"UP", # Pyupgrade
]
extend-safe-fixes = [
"TID252", # absolute imports
Expand All @@ -104,7 +98,7 @@ default = { solve-group = "default" }
dev = { features = ["dev"], solve-group = "default" }
examples = { features = ["examples"], solve-group = "default" }
all = { features = ["dev", "examples"], solve-group = "default" }
test310 = ["dev", "py310"] # implicit: test310 = ["dev", "py310", "default"]
test310 = ["dev", "py310"] # implicit: test310 = ["dev", "py310", "default"]
test311 = ["dev", "py311"]
test312 = ["dev", "py312"]

Expand Down Expand Up @@ -134,7 +128,9 @@ pytest-cov = "*"
sphinx = "*"
sphinx-rtd-theme = "*"


[tool.pixi.feature.dev.tasks]
lint = "ruff check"
test = "pytest tests/"

[tool.pixi.feature.examples.dependencies]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_grids/test_ugrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
tests for ugrid code
"""
from pathlib import Path
import xarray as xr
from xarray_subset_grid.grids import ugrid

import pytest
import xarray as xr

from xarray_subset_grid.grids import ugrid

EXAMPLE_DATA = Path(__file__).parent.parent.parent / "examples" / "example_data"

Expand Down Expand Up @@ -220,7 +221,6 @@
# topology for TEST_FILE1
grid_topology = {'node_coordinates': ('lon', 'lat'),
'face_node_connectivity': 'nv',
'node_coordinates': ('lon', 'lat'),
'face_coordinates': ('lonc', 'latc'),
'face_face_connectivity': 'nbe'
}
Expand Down
13 changes: 6 additions & 7 deletions xarray_subset_grid/accessor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Optional
import warnings
#from typing import Optional, Union

#from typing import Optional, Union
import numpy as np
import xarray as xr

Expand All @@ -20,7 +19,7 @@ def register_grid_impl(grid_impl: Grid, priority: int = 0):
_grid_impls.insert(priority, grid_impl)


def grid_factory(ds: xr.Dataset) -> Optional[Grid]:
def grid_factory(ds: xr.Dataset) -> Grid | None:
"""
Get the grid implementation for the given dataset.
:param ds: The dataset to get the grid implementation for
Expand All @@ -37,7 +36,7 @@ class GridDatasetAccessor:
"""Accessor for grid operations on datasets"""

_ds: xr.Dataset
_grid: Optional[Grid]
_grid: Grid | None

def __init__(self, ds: xr.Dataset):
"""
Expand All @@ -48,7 +47,7 @@ def __init__(self, ds: xr.Dataset):
self._grid = grid_factory(ds)

@property
def grid(self) -> Optional[Grid]:
def grid(self) -> Grid | None:
"""The recognized grid implementation for the given dataset
:return: The grid implementation or None if no implementation is found
"""
Expand Down Expand Up @@ -100,7 +99,7 @@ def subset_vars(self, vars: list[str]) -> xr.Dataset:
return self._ds

def subset_polygon(self, polygon: list[tuple[float, float]] | np.ndarray
) -> Optional[xr.Dataset]:
) -> xr.Dataset | None:
"""
Subset the dataset to the grid.
Expand All @@ -116,7 +115,7 @@ def subset_polygon(self, polygon: list[tuple[float, float]] | np.ndarray

def subset_bbox(
self, bbox: tuple[float, float, float, float]
) -> Optional[xr.Dataset]:
) -> xr.Dataset | None:
"""Subset the dataset to the bounding box
This call is forwarded to the grid implementation with the loaded dataset.
Expand Down
3 changes: 1 addition & 2 deletions xarray_subset_grid/grid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from abc import ABC, abstractmethod
from collections.abc import Iterable
from typing import Union

import numpy as np
import xarray as xr
Expand Down Expand Up @@ -55,7 +54,7 @@ def subset_vars(self, ds: xr.Dataset, vars: Iterable[str]) -> xr.Dataset:

@abstractmethod
def subset_polygon(
self, ds: xr.Dataset, polygon: Union[list[tuple[float, float]], np.ndarray]
self, ds: xr.Dataset, polygon: list[tuple[float, float]] | np.ndarray
) -> xr.Dataset:
"""Subset the dataset to the grid
:param ds: The dataset to subset
Expand Down
15 changes: 5 additions & 10 deletions xarray_subset_grid/grids/sgrid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Union

import numpy as np
import xarray as xr

Expand All @@ -18,7 +16,8 @@ def recognize(ds: xr.Dataset) -> bool:
except KeyError:
return False

# For now, if the dataset has a grid topology and not a mesh topology, we assume it's a SGRID
# For now, if the dataset has a grid topology and not a mesh topology,
# we assume it's a SGRID
return len(_grid_topology_keys) > 0 and _grid_topology_keys[0] in ds

@property
Expand Down Expand Up @@ -56,7 +55,7 @@ def data_vars(self, ds: xr.Dataset) -> set[str]:
return {var for var in ds.data_vars if not set(ds[var].dims).isdisjoint(dims)}

def subset_polygon(
self, ds: xr.Dataset, polygon: Union[list[tuple[float, float]], np.ndarray]
self, ds: xr.Dataset, polygon: list[tuple[float, float]] | np.ndarray
) -> xr.Dataset:
"""Subset the dataset to the grid
:param ds: The dataset to subset
Expand Down Expand Up @@ -111,14 +110,10 @@ def subset_polygon(
# so that we can use it to mask and drop via xr.where, which requires that
# the mask and data have the same shape and both are DataArrays with matching
# dimensions
ds_subset = ds.assign(
subset_mask=xr.DataArray(polygon_mask, dims=mask_dims)
)
ds_subset = ds.assign(subset_mask=xr.DataArray(polygon_mask, dims=mask_dims))

# Now we can use the mask to subset the data
ds_subset = (
ds_subset[vars].where(ds_subset.subset_mask, drop=True).drop_encoding()
)
ds_subset = ds_subset[vars].where(ds_subset.subset_mask, drop=True).drop_encoding()

# Add the subsetted dataset to the list for merging
ds_out.append(ds_subset)
Expand Down
Loading

0 comments on commit 8d9f041

Please sign in to comment.