Skip to content

Commit 4c0731a

Browse files
committed
rufff
1 parent 7021dab commit 4c0731a

19 files changed

+71
-99
lines changed

check_pygridtools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
matplotlib.use('agg')
66
style.use('classic')
77

8-
import pygridtools
8+
import pygridtools # noqa: E402
99

1010

1111
if '--strict' in sys.argv:

pygridtools/__init__.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from .misc import *
2-
from .core import *
3-
from . import iotools
4-
from . import viz
5-
from . import validate
1+
from .misc import * # noqa: F403
2+
from .core import * # noqa: F403
3+
from . import iotools # noqa: F401
4+
from . import viz # noqa: F401
5+
from . import validate # noqa: F401
66

7-
from .tests import test, teststrict
7+
from .tests import test, teststrict # noqa: F401

pygridtools/core.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import division
21

32
import warnings
43
from copy import deepcopy
@@ -198,7 +197,7 @@ def extract(nodes, jstart=None, istart=None, jend=None, iend=None):
198197
return deepcopy(nodes[jstart:jend, istart:iend])
199198

200199

201-
class ModelGrid(object):
200+
class ModelGrid:
202201
"""
203202
Container for a curvilinear-orthogonal grid. Provides convenient
204203
access to masking, manipulation, and visualization methods.
@@ -825,10 +824,7 @@ def to_point_geodataframe(self, which='nodes', usemask=True, elev=None):
825824

826825
def to_polygon_geodataframe(self, usemask=True, elev=None):
827826
x, y = self._get_x_y(which='nodes', usemask=False)
828-
if usemask:
829-
mask = self.cell_mask.copy()
830-
else:
831-
mask = None
827+
mask = self.cell_mask.copy() if usemask else None
832828

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

pygridtools/gefdc.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from shapely.geometry import Point
88
import geopandas
99

10-
from pygridtools import iotools
11-
from pygridtools import misc
1210
from pygridtools import validate
1311

1412

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

9593
rowfmt = '{0:3d} {1:s}\n'
96-
colfmt = f'{{:0{_n_digits(ncols)}d}}'
94+
# colfmt = f'{{:0{_n_digits(ncols)}d}}'
9795

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

109107
else:
110108
columns = numpy.arange(1, maxcols + 1, dtype=int)
111-
colstr = [list('{:04d}'.format(c)) for c in columns]
109+
colstr = [list(f'{c:04d}') for c in columns]
112110
hundreds = ''.join([c[1] for c in colstr])
113111
tens = ''.join([c[2] for c in colstr])
114112
ones = ''.join([c[3] for c in colstr])
@@ -117,19 +115,16 @@ def write_cellinp(cell_array, outputfile='cell.inp', mode='w',
117115
if writeheader:
118116
title = 'C -- cell.inp for EFDC model by pygridtools\n'
119117
outfile.write(title)
120-
outfile.write('C {}\n'.format(hundreds[:ncols]))
121-
outfile.write('C {}\n'.format(tens[:ncols]))
122-
outfile.write('C {}\n'.format(ones[:ncols]))
118+
outfile.write(f'C {hundreds[:ncols]}\n')
119+
outfile.write(f'C {tens[:ncols]}\n')
120+
outfile.write(f'C {ones[:ncols]}\n')
123121

124122
for n, row in enumerate(cell_array):
125123
row_number = nrows - n
126124
row_strings = row.astype(str)
127125
cell_text = ''.join(row_strings.tolist())
128-
if rowlabels:
129-
rowheader = ''
130-
row_text = rowfmt.format(int(row_number), cell_text)
131-
else:
132-
row_text = ' {0:s}\n'.format(cell_text)
126+
127+
row_text = rowfmt.format(int(row_number), cell_text) if rowlabels else f' {cell_text:s}\n'
133128

134129
outfile.write(row_text)
135130

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

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

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

298-
nrows = cells.shape[0]
299-
ncols = cells.shape[1]
293+
# nrows = cells.shape[0]
294+
# ncols = cells.shape[1]
300295

301296
# nchunks = numpy.ceil(ncols / maxcols)
302297
# if ncols > maxcols:

pygridtools/iotools.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import os
2-
from collections import OrderedDict
3-
from textwrap import dedent
41
import warnings
52

63
import numpy
7-
import pandas
84
from matplotlib import pyplot
9-
from shapely.geometry import Point, Polygon
105
import geopandas
116

127
try:
@@ -17,8 +12,6 @@
1712
from pygridgen.tests import requires
1813
import pygridgen as pgg
1914

20-
from pygridtools import misc
21-
from pygridtools import validate
2215
from pygridtools import viz
2316

2417

@@ -235,7 +228,7 @@ def interactive_grid_shape(grid, max_n=200, plotfxn=None, **kwargs):
235228
)
236229

237230

238-
class _FocusProperties():
231+
class _FocusProperties:
239232
"""A dummy class to hold the properties of the grid._FocusPoint() object.
240233
This class is required so that multiple ipywidgets.interactive widgets
241234
can interact on the same plot.
@@ -437,6 +430,6 @@ def interactive_grid_focus(g, n_points, plotfxn=None, **kwargs):
437430
tab_nest = ipywidgets.Tab()
438431
tab_nest.children = widgets
439432
for n in range(len(widgets)):
440-
tab_nest.set_title(n, 'Focus {}'.format(n + 1))
433+
tab_nest.set_title(n, f'Focus {n + 1}')
441434

442435
return focus_points, tab_nest

pygridtools/misc.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from collections import OrderedDict
22
from shapely.geometry import Point, Polygon
3+
import importlib
34

45
import numpy
56
import matplotlib.path as mpath
67
import pandas
7-
from shapely import geometry
88
import geopandas
99
from pygridgen import csa
1010

@@ -81,7 +81,7 @@ def make_record(ID, coords, geomtype, props):
8181
"""
8282

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

8686
if isinstance(coords, numpy.ma.MaskedArray):
8787
coords = coords.data
@@ -123,10 +123,9 @@ def interpolate_bathymetry(bathy, x_points, y_points, xcol='x', ycol='y', zcol='
123123
124124
"""
125125

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

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

@@ -428,7 +427,7 @@ def gdf_of_points(X, Y, crs, elev=None):
428427
record = OrderedDict(
429428
id=int(row), ii=int(ii + 2), jj=int(jj + 2),
430429
elev=float(elev[jj, ii]),
431-
ii_jj='{:02d}_{:02d}'.format(ii + 2, jj + 2),
430+
ii_jj=f'{ii + 2:02d}_{jj + 2:02d}',
432431
geometry=Point(coords)
433432
)
434433

pygridtools/tests/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from importlib import resources
22

3-
import pygridtools
43
from pygridgen.tests import requires
54

65
try:

pygridtools/tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pygridtools.iotools import _FocusProperties
1111

1212

13-
class FakeGrid(object):
13+
class FakeGrid:
1414
def __init__(self, boundary):
1515
self.x, self.y = simple_nodes()
1616
self.xn, self.yn = simple_nodes()

pygridtools/tests/test_core.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import os
2-
import warnings
31
from pkg_resources import resource_filename
4-
import tempfile
52

63
import numpy
74
from numpy import nan
@@ -282,7 +279,7 @@ def polyverts():
282279
def test_ModelGrid_bad_shapes(simple_cells):
283280
xc, yc = simple_cells
284281
with raises(ValueError):
285-
mg = core.ModelGrid(xc, yc[2:, 2:])
282+
core.ModelGrid(xc, yc[2:, 2:])
286283

287284

288285
def test_ModelGrid_nodes_and_cells(g1, simple_cells):

pygridtools/tests/test_gefdc.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
import numpy
66
import pandas
7-
from shapely import geometry
8-
import geopandas
97

108
import pytest
119
import numpy.testing as nptest
12-
import pandas.testing as pdtest
1310

1411
from pygridtools import gefdc
1512
from . import utils
@@ -52,7 +49,6 @@ def test_convert_gridext_to_gis(example_crs):
5249
gridextfile = resource_filename('pygridtools.tests.test_data', 'gridext.inp')
5350
baselinefile = resource_filename('pygridtools.tests.baseline_files', 'gridext.shp')
5451
river = 'test'
55-
reach = 1
5652

5753
with tempfile.TemporaryDirectory() as outputdir:
5854
outputfile = os.path.join(outputdir, 'gridext.shp')
@@ -90,7 +86,7 @@ def test_write_gridout_file(simple_nodes):
9086
with tempfile.TemporaryDirectory() as outdir:
9187
result_filename = os.path.join(outdir, 'testgrid.out')
9288

93-
df = gefdc.write_gridout_file(x, y, result_filename)
89+
_ = gefdc.write_gridout_file(x, y, result_filename)
9490
utils.assert_textfiles_equal(known_filename, result_filename)
9591

9692

pygridtools/tests/test_iotools.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import os
2-
import sys
31
from pkg_resources import resource_filename
4-
import tempfile
52

63
import numpy
74
import pandas
@@ -20,7 +17,6 @@
2017
from pygridtools import iotools
2118
import pygridgen
2219
from pygridgen.tests import raises, requires
23-
from . import utils
2420

2521

2622
@pytest.mark.parametrize(('filterfxn', 'points_in_boundary'), [
@@ -132,7 +128,7 @@ def test__change_focus(simple_grid):
132128
others = (iotools._FocusProperties(
133129
pos=old_pos, axis=old_axis, factor=old_factor, extent=old_extent),) * 3
134130

135-
xn = iotools._change_focus(fp, others, new_axis, new_pos,
131+
_ = iotools._change_focus(fp, others, new_axis, new_pos,
136132
new_factor, new_extent, simple_grid, lambda x, y: x)
137133

138134
# test single focus modification
@@ -166,7 +162,7 @@ def test_interactive_grid_focus_tabs(simple_grid, tab):
166162
(1, ipywidgets.widgets.widget_float.FloatSlider),
167163
(2, ipywidgets.widgets.widget_float.FloatLogSlider),
168164
(3, ipywidgets.widgets.widget_float.FloatSlider)])
169-
def test_interactive_grid_focus_tabs(simple_grid, parent, child, widget_type):
165+
def test_interactive_grid_focus_tabs2(simple_grid, parent, child, widget_type):
170166
focus_points, widget = iotools.interactive_grid_focus(simple_grid, n_points=2)
171167
assert isinstance(widget.children[parent], ipywidgets.widgets.interaction.interactive)
172168
assert isinstance(widget.children[parent].children[child], widget_type)

pygridtools/tests/test_misc.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import importlib.util
12
from pathlib import Path
23
from pkg_resources import resource_filename
4+
import importlib
35

46
import numpy
57
from numpy import nan
@@ -14,11 +16,7 @@
1416

1517
numpy.set_printoptions(linewidth=150, nanstr='-')
1618

17-
try:
18-
import pygridgen
19-
HASPGG = True
20-
except ImportError:
21-
HASPGG = False
19+
HASPGG = bool(importlib.util.find_spec("pygridgen"))
2220

2321

2422
@pytest.mark.parametrize(('masked', 'z', 'triangles'), [
@@ -234,8 +232,8 @@ def stackgrids():
234232
[nan, nan, nan, nan, 4.4, 4.5, 4.6, 4.7, 4.8, nan, nan, nan, nan, nan, nan, nan, nan],
235233
[nan, nan, nan, nan, 5.4, 5.5, 5.6, 5.7, 5.8, nan, nan, nan, nan, nan, nan, nan, nan],
236234
[nan, nan, nan, nan, nan, nan, 6.6, 6.7, 6.8, nan, nan, nan, nan, nan, nan, nan, nan],
237-
[nan, nan, nan, nan, nan, nan, 7.6, 7.7, 7.8, 7.9, 7.10, 7.11, 7.12, 7.13, 7.14, 7.15, 7.16],
238-
[nan, nan, nan, nan, nan, nan, 8.6, 8.7, 8.8, 8.9, 8.10, 8.11, 8.12, 8.13, 8.14, 8.15, 8.16],
235+
[nan, nan, nan, nan, nan, nan, 7.6, 7.7, 7.8, 7.9, 7.10, 7.11, 7.12, 7.13, 7.14, 7.15, 7.16], # noqa: E501
236+
[nan, nan, nan, nan, nan, nan, 8.6, 8.7, 8.8, 8.9, 8.10, 8.11, 8.12, 8.13, 8.14, 8.15, 8.16], # noqa: E501
239237
[nan, nan, nan, nan, nan, nan, 9.6, 9.7, 9.8, 9.9, 9.10, 9.11, 9.12, 9.13, nan, nan, nan],
240238
[nan, nan, nan, nan, nan, nan, 10.6, 10.7, 10.8, nan, nan, nan, nan, nan, nan, nan, nan],
241239
[nan, nan, nan, nan, nan, nan, 11.6, 11.7, 11.8, nan, nan, nan, nan, nan, nan, nan, nan],
@@ -388,7 +386,6 @@ def test_gdf_of_cells(usemasks, fname, simple_grid, example_crs):
388386
mask = None
389387

390388
baselinedir = Path(resource_filename('pygridtools.tests', 'baseline_files'))
391-
river = 'test'
392389
expected = geopandas.read_file(str(baselinedir / fname))
393390
result = misc.gdf_of_cells(simple_grid.x, simple_grid.y, mask, example_crs)
394391
utils.assert_gdfs_equal(expected.drop(columns=['river', 'reach']), result)

0 commit comments

Comments
 (0)