Skip to content

Commit

Permalink
Merge pull request #2 from phobson/ruff-gha
Browse files Browse the repository at this point in the history
ruff config
  • Loading branch information
phobson authored Oct 26, 2024
2 parents e9e3fc4 + 4c0731a commit 84ca88c
Show file tree
Hide file tree
Showing 23 changed files with 162 additions and 121 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/python-ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: ruff 'n' stuff
on: [pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruff
# Update output format to enable automatic inline annotations.
- name: Run Ruff
run: ruff check --output-format=github .
15 changes: 11 additions & 4 deletions .github/workflows/python-run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Run basic unit tests

on:
push:
branches: [ main ]
branches: [ master ]
pull_request:
branches: [ main ]
branches: [ master ]

jobs:
build:
Expand All @@ -26,14 +26,21 @@ jobs:
create-args: >-
python=${{ matrix.python-version }}
gridgen
numpy
shapely
fiona
geopandas
scipy
matplotlib
pyproj
ipywidgets
pytest
pytest-mpl
pytest-cov
pytest-pep8
coverage
docopt
requests
pyyaml
pygridgen
init-shell: bash
cache-environment: true
post-cleanup: 'all'
Expand Down
2 changes: 1 addition & 1 deletion check_pygridtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
matplotlib.use('agg')
style.use('classic')

import pygridtools
import pygridtools # noqa: E402


if '--strict' in sys.argv:
Expand Down
12 changes: 6 additions & 6 deletions pygridtools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .misc import *
from .core import *
from . import iotools
from . import viz
from . import validate
from .misc import * # noqa: F403
from .core import * # noqa: F403
from . import iotools # noqa: F401
from . import viz # noqa: F401
from . import validate # noqa: F401

from .tests import test, teststrict
from .tests import test, teststrict # noqa: F401
21 changes: 10 additions & 11 deletions pygridtools/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division

import warnings
from copy import deepcopy
Expand Down Expand Up @@ -198,7 +197,7 @@ def extract(nodes, jstart=None, istart=None, jend=None, iend=None):
return deepcopy(nodes[jstart:jend, istart:iend])


class ModelGrid(object):
class ModelGrid:
"""
Container for a curvilinear-orthogonal grid. Provides convenient
access to masking, manipulation, and visualization methods.
Expand Down Expand Up @@ -745,12 +744,15 @@ def mask_centroids(self, inside=None, outside=None, use_existing=True):
cell_mask = numpy.bitwise_or(inside_cell_mask, outside_cell_mask)
return self.update_cell_mask(mask=cell_mask, merge_existing=use_existing)

@numpy.deprecate(message='use mask_nodes or mask_centroids')
def mask_cells_with_polygon(self, polyverts, use_centroids=True, **kwargs):
if use_centroids:
return self.mask_centroids(polyverts, **kwargs)
else:
return self.mask_nodes(polyverts, **kwargs)
with warnings.warn(
".mask_cells_with_polygon is deprecated. Use .mask_nodes or .mask_centroids",
DeprecationWarning
):
if use_centroids:
return self.mask_centroids(polyverts, **kwargs)
else:
return self.mask_nodes(polyverts, **kwargs)

def plot_cells(self, engine='mpl', ax=None, usemask=True, showisland=True,
cell_kws=None, domain_kws=None, extent_kws=None,
Expand Down Expand Up @@ -822,10 +824,7 @@ def to_point_geodataframe(self, which='nodes', usemask=True, elev=None):

def to_polygon_geodataframe(self, usemask=True, elev=None):
x, y = self._get_x_y(which='nodes', usemask=False)
if usemask:
mask = self.cell_mask.copy()
else:
mask = None
mask = self.cell_mask.copy() if usemask else None

return misc.gdf_of_cells(x, y, mask, crs=self.crs, elev=elev, triangles=False)

Expand Down
25 changes: 10 additions & 15 deletions pygridtools/gefdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from shapely.geometry import Point
import geopandas

from pygridtools import iotools
from pygridtools import misc
from pygridtools import validate


Expand Down Expand Up @@ -93,7 +91,7 @@ def write_cellinp(cell_array, outputfile='cell.inp', mode='w',
nrows, ncols = cell_array.shape

rowfmt = '{0:3d} {1:s}\n'
colfmt = f'{{:0{_n_digits(ncols)}d}}'
# colfmt = f'{{:0{_n_digits(ncols)}d}}'

if cell_array.shape[1] > maxcols:
first_array = cell_array[:, :maxcols]
Expand All @@ -108,7 +106,7 @@ def write_cellinp(cell_array, outputfile='cell.inp', mode='w',

else:
columns = numpy.arange(1, maxcols + 1, dtype=int)
colstr = [list('{:04d}'.format(c)) for c in columns]
colstr = [list(f'{c:04d}') for c in columns]
hundreds = ''.join([c[1] for c in colstr])
tens = ''.join([c[2] for c in colstr])
ones = ''.join([c[3] for c in colstr])
Expand All @@ -117,19 +115,16 @@ def write_cellinp(cell_array, outputfile='cell.inp', mode='w',
if writeheader:
title = 'C -- cell.inp for EFDC model by pygridtools\n'
outfile.write(title)
outfile.write('C {}\n'.format(hundreds[:ncols]))
outfile.write('C {}\n'.format(tens[:ncols]))
outfile.write('C {}\n'.format(ones[:ncols]))
outfile.write(f'C {hundreds[:ncols]}\n')
outfile.write(f'C {tens[:ncols]}\n')
outfile.write(f'C {ones[:ncols]}\n')

for n, row in enumerate(cell_array):
row_number = nrows - n
row_strings = row.astype(str)
cell_text = ''.join(row_strings.tolist())
if rowlabels:
rowheader = ''
row_text = rowfmt.format(int(row_number), cell_text)
else:
row_text = ' {0:s}\n'.format(cell_text)

row_text = rowfmt.format(int(row_number), cell_text) if rowlabels else f' {cell_text:s}\n'

outfile.write(row_text)

Expand All @@ -153,7 +148,7 @@ def write_gridout_file(xcoords, ycoords, outfile):
})

with Path(outfile).open('w') as f:
f.write('## {:d} x {:d}\n'.format(nx, ny))
f.write(f'## {nx:d} x {ny:d}\n')

# XXX: https://github.com/pandas-dev/pandas/issues/21882
with Path(outfile).open('a') as f:
Expand Down Expand Up @@ -295,8 +290,8 @@ def make_gefdc_cells(node_mask, cell_mask=None, triangles=False):
if total == bank_cell * shift**2:
cells[cj, ci] = land_cell

nrows = cells.shape[0]
ncols = cells.shape[1]
# nrows = cells.shape[0]
# ncols = cells.shape[1]

# nchunks = numpy.ceil(ncols / maxcols)
# if ncols > maxcols:
Expand Down
15 changes: 4 additions & 11 deletions pygridtools/iotools.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import os
from collections import OrderedDict
from textwrap import dedent
import warnings

import numpy
import pandas
from matplotlib import pyplot
from shapely.geometry import Point, Polygon
import geopandas

try:
Expand All @@ -17,8 +12,6 @@
from pygridgen.tests import requires
import pygridgen as pgg

from pygridtools import misc
from pygridtools import validate
from pygridtools import viz


Expand Down Expand Up @@ -218,7 +211,7 @@ def interactive_grid_shape(grid, max_n=200, plotfxn=None, **kwargs):
... ( 0, 0, 1.00), ( 0, 15, 0.50), ( 8, 15, -0.25),
... (11, 13, -0.25)])
>>> g = grid.Gridgen(d[:, 0], d[:, 1], d[:, 2], (75, 75), ul_idx=1, focus=None)
>>> new_grid, widget = iotools.interactive_grid_shape(g, plotfxn=plot_grid)
>>> new_grid, widget = iotools.interactive_grid_shape(g, plotfxn=plot_grid) # doctest: +SKIP
"""

if not plotfxn:
Expand All @@ -235,7 +228,7 @@ def interactive_grid_shape(grid, max_n=200, plotfxn=None, **kwargs):
)


class _FocusProperties():
class _FocusProperties:
"""A dummy class to hold the properties of the grid._FocusPoint() object.
This class is required so that multiple ipywidgets.interactive widgets
can interact on the same plot.
Expand Down Expand Up @@ -399,7 +392,7 @@ def interactive_grid_focus(g, n_points, plotfxn=None, **kwargs):
... (11, 13, -0.25)])
>>> g = grid.Gridgen(d[:, 0], d[:, 1], d[:, 2], (75, 75), ul_idx=1, focus=None)
>>> n = 4 # number of focus objects
>>> new_grid, widget = iotools.interactive_grid_focus(g, n)
>>> new_grid, widget = iotools.interactive_grid_focus(g, n) # doctest: +SKIP
"""

if not plotfxn:
Expand Down Expand Up @@ -437,6 +430,6 @@ def interactive_grid_focus(g, n_points, plotfxn=None, **kwargs):
tab_nest = ipywidgets.Tab()
tab_nest.children = widgets
for n in range(len(widgets)):
tab_nest.set_title(n, 'Focus {}'.format(n + 1))
tab_nest.set_title(n, f'Focus {n + 1}')

return focus_points, tab_nest
15 changes: 7 additions & 8 deletions pygridtools/misc.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from collections import OrderedDict
from shapely.geometry import Point, Polygon
import importlib

import numpy
import matplotlib.path as mpath
import pandas
from shapely import geometry
import geopandas
from pygridgen import csa

Expand Down Expand Up @@ -81,7 +81,7 @@ def make_record(ID, coords, geomtype, props):
"""

if geomtype not in ['Point', 'LineString', 'Polygon']:
raise ValueError('Geometry {} not suppered'.format(geomtype))
raise ValueError(f'Geometry {geomtype} not suppered')

if isinstance(coords, numpy.ma.MaskedArray):
coords = coords.data
Expand Down Expand Up @@ -123,10 +123,9 @@ def interpolate_bathymetry(bathy, x_points, y_points, xcol='x', ycol='y', zcol='
"""

try:
import pygridgen
except ImportError: # pragma: no cover
raise ImportError("`pygridgen` not installed. Cannot interpolate bathymetry.")
HASPGG = bool(importlib.util.find_spec("pygridgen"))
if not HASPGG:
raise RuntimeError("`pygridgen` not installed. Cannot interpolate bathymetry.")

if bathy is None:
elev = numpy.zeros(x_points.shape)
Expand Down Expand Up @@ -372,7 +371,7 @@ def gdf_of_cells(X, Y, mask, crs, elev=None, triangles=False):
# build the attributes
record = OrderedDict(
id=row, ii=ii + 2, jj=jj + 2, elev=Z,
ii_jj='{:02d}_{:02d}'.format(ii + 2, jj + 2),
ii_jj=f'{ii + 2:02d}_{jj + 2:02d}',
geometry=Polygon(shell=coords)
)

Expand Down Expand Up @@ -428,7 +427,7 @@ def gdf_of_points(X, Y, crs, elev=None):
record = OrderedDict(
id=int(row), ii=int(ii + 2), jj=int(jj + 2),
elev=float(elev[jj, ii]),
ii_jj='{:02d}_{:02d}'.format(ii + 2, jj + 2),
ii_jj=f'{ii + 2:02d}_{jj + 2:02d}',
geometry=Point(coords)
)

Expand Down
5 changes: 2 additions & 3 deletions pygridtools/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from importlib import resources

import pygridtools
from pygridgen.tests import requires

try:
Expand All @@ -11,15 +10,15 @@

@requires(pytest, 'pytest')
def test(*args):
options = [resources('pygridtools', '')]
options = [str(resources.files("pygridtools"))]
options.extend(list(args))
return pytest.main(options)


@requires(pytest, 'pytest')
def teststrict(*args):
options = list(set([
resources('pygridtools', ''),
str(resources.files("pygridtools")),
'--mpl',
'--doctest-modules'
] + list(args)))
Expand Down
2 changes: 1 addition & 1 deletion pygridtools/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pygridtools.iotools import _FocusProperties


class FakeGrid(object):
class FakeGrid:
def __init__(self, boundary):
self.x, self.y = simple_nodes()
self.xn, self.yn = simple_nodes()
Expand Down
10 changes: 5 additions & 5 deletions pygridtools/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import os
import warnings
from pkg_resources import resource_filename
import tempfile

import numpy
from numpy import nan
Expand Down Expand Up @@ -282,7 +279,7 @@ def polyverts():
def test_ModelGrid_bad_shapes(simple_cells):
xc, yc = simple_cells
with raises(ValueError):
mg = core.ModelGrid(xc, yc[2:, 2:])
core.ModelGrid(xc, yc[2:, 2:])


def test_ModelGrid_nodes_and_cells(g1, simple_cells):
Expand Down Expand Up @@ -665,7 +662,10 @@ def test_masks_no_polys(mg):

def test_ModelGrid_to_point_geodataframe(g1):
expectedfile = resource_filename('pygridtools.tests.baseline_files', 'mgshp_nomask_nodes_points.shp')
expected = geopandas.read_file(expectedfile)
expected = geopandas.read_file(expectedfile).assign(
ii=lambda df: df["ii"].astype("int64"),
jj=lambda df: df["jj"].astype("int64"),
)
result = g1.to_point_geodataframe(which='nodes', usemask=False)
utils.assert_gdfs_equal(expected.drop(columns=['river', 'reach']), result)

Expand Down
Loading

0 comments on commit 84ca88c

Please sign in to comment.