Skip to content

Commit 612de10

Browse files
committed
action ruff checks
1 parent 89dfba3 commit 612de10

File tree

7 files changed

+208
-182
lines changed

7 files changed

+208
-182
lines changed

index.ipynb

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
"\n",
4747
"nx, ny = 6, 3\n",
4848
"\n",
49-
"np.random.seed(0)\n",
50-
"orography = np.random.normal(1000, 600, size=(ny, nx)) - 400\n",
51-
"sea_level_temp = np.random.normal(290, 5, size=(ny, nx))"
49+
"rng = np.random.default_rng(0)\n",
50+
"orography = rng.normal(1000, 600, size=(ny, nx)) - 400\n",
51+
"sea_level_temp = rng.normal(290, 5, size=(ny, nx))"
5252
]
5353
},
5454
{
@@ -85,20 +85,20 @@
8585
"\n",
8686
"import matplotlib.pyplot as plt\n",
8787
"\n",
88-
"plt.set_cmap('viridis')\n",
88+
"plt.set_cmap(\"viridis\")\n",
8989
"fig = plt.figure(figsize=(8, 6))\n",
9090
"\n",
9191
"plt.subplot(1, 2, 1)\n",
9292
"plt.pcolormesh(orography)\n",
93-
"cbar = plt.colorbar(orientation='horizontal',\n",
94-
" label='Orography (m)')\n",
93+
"cbar = plt.colorbar(orientation=\"horizontal\",\n",
94+
" label=\"Orography (m)\")\n",
9595
"# Reduce the maximum number of ticks to 5.\n",
9696
"cbar.ax.xaxis.get_major_locator().nbins = 5\n",
9797
"\n",
9898
"plt.subplot(1, 2, 2)\n",
9999
"plt.pcolormesh(sea_level_temp)\n",
100-
"cbar = plt.colorbar(orientation='horizontal',\n",
101-
" label='Sea level temperature (K)')\n",
100+
"cbar = plt.colorbar(orientation=\"horizontal\",\n",
101+
" label=\"Sea level temperature (K)\")\n",
102102
"# Reduce the maximum number of ticks to 5.\n",
103103
"cbar.ax.xaxis.get_major_locator().nbins = 5\n",
104104
"\n",
@@ -179,17 +179,17 @@
179179
"source": [
180180
"plt.figure(figsize=(8, 6))\n",
181181
"plt.fill_between(np.arange(6), np.zeros(6), orography[1, :],\n",
182-
" color='green', linewidth=2, label='Orography')\n",
182+
" color=\"green\", linewidth=2, label=\"Orography\")\n",
183183
"\n",
184184
"plt.plot(np.zeros(nx),\n",
185-
" color='blue', linewidth=1.2,\n",
186-
" label='Sea level')\n",
185+
" color=\"blue\", linewidth=1.2,\n",
186+
" label=\"Sea level\")\n",
187187
"\n",
188188
"for i in range(9):\n",
189-
" plt.plot(altitude[i, 1, :], color='gray', linestyle='--',\n",
190-
" label='Model levels' if i == 0 else None)\n",
189+
" plt.plot(altitude[i, 1, :], color=\"gray\", linestyle=\"--\",\n",
190+
" label=\"Model levels\" if i == 0 else None)\n",
191191
"\n",
192-
"plt.ylabel('altitude / m')\n",
192+
"plt.ylabel(\"altitude / m\")\n",
193193
"plt.margins(0.1)\n",
194194
"plt.legend()\n",
195195
"plt.show()"
@@ -250,14 +250,12 @@
250250
}
251251
],
252252
"source": [
253-
"from matplotlib.colors import LogNorm\n",
254-
"\n",
255253
"fig = plt.figure(figsize=(8, 6))\n",
256254
"norm = plt.Normalize(vmin=temperature.min(), vmax=temperature.max())\n",
257255
"\n",
258256
"for i in range(nz):\n",
259257
" plt.subplot(3, 3, i + 1)\n",
260-
" qm = plt.pcolormesh(temperature[i], cmap='viridis', norm=norm)\n",
258+
" qm = plt.pcolormesh(temperature[i], cmap=\"viridis\", norm=norm)\n",
261259
"\n",
262260
"plt.subplots_adjust(right=0.84, wspace=0.3, hspace=0.3)\n",
263261
"cax = plt.axes([0.85, 0.1, 0.03, 0.8])\n",
@@ -332,21 +330,21 @@
332330
"source": [
333331
"plt.figure(figsize=(8, 6))\n",
334332
"plt.fill_between(np.arange(6), np.zeros(6), orography[1, :],\n",
335-
" color='green', linewidth=2, label='Orography')\n",
333+
" color=\"green\", linewidth=2, label=\"Orography\")\n",
336334
"\n",
337335
"for i in range(9):\n",
338336
" plt.plot(altitude[i, 1, :],\n",
339-
" color='gray', lw=1.2,\n",
340-
" label=None if i > 0 else 'Source levels \\n(model levels)')\n",
337+
" color=\"gray\", lw=1.2,\n",
338+
" label=None if i > 0 else \"Source levels \\n(model levels)\")\n",
341339
"for i, target in enumerate(target_altitudes):\n",
342340
" plt.plot(np.repeat(target, 6),\n",
343-
" color='gray', linestyle='--', lw=1.4, alpha=0.6,\n",
344-
" label=None if i > 0 else 'Target levels \\n(altitude)')\n",
341+
" color=\"gray\", linestyle=\"--\", lw=1.4, alpha=0.6,\n",
342+
" label=None if i > 0 else \"Target levels \\n(altitude)\")\n",
345343
"\n",
346-
"plt.ylabel('height / m')\n",
344+
"plt.ylabel(\"height / m\")\n",
347345
"plt.margins(top=0.1)\n",
348346
"plt.legend()\n",
349-
"plt.savefig('summary.png')\n",
347+
"plt.savefig(\"summary.png\")\n",
350348
"plt.show()"
351349
]
352350
},
@@ -413,7 +411,7 @@
413411
"plt.figure(figsize=(8, 6))\n",
414412
"ax1 = plt.subplot(1, 2, 1)\n",
415413
"plt.fill_between(np.arange(6), np.zeros(6), orography[1, :],\n",
416-
" color='green', linewidth=2, label='Orography')\n",
414+
" color=\"green\", linewidth=2, label=\"Orography\")\n",
417415
"cs = plt.contourf(np.tile(np.arange(6), nz).reshape(nz, 6),\n",
418416
" altitude[:, 1],\n",
419417
" temperature[:, 1])\n",
@@ -423,7 +421,7 @@
423421
"\n",
424422
"plt.subplot(1, 2, 2, sharey=ax1)\n",
425423
"plt.fill_between(np.arange(6), np.zeros(6), orography[1, :],\n",
426-
" color='green', linewidth=2, label='Orography')\n",
424+
" color=\"green\", linewidth=2, label=\"Orography\")\n",
427425
"plt.contourf(np.arange(6), target_altitudes,\n",
428426
" np.ma.masked_invalid(new_temperature[:, 1]),\n",
429427
" cmap=cs.cmap, norm=cs.norm)\n",
@@ -432,9 +430,9 @@
432430
" c=new_temperature[:, 1])\n",
433431
"plt.scatter(np.tile(np.arange(nx), target_nz).reshape(target_nz, nx),\n",
434432
" np.repeat(target_altitudes, nx).reshape(target_nz, nx),\n",
435-
" s=np.isnan(new_temperature[:, 1]) * 15, marker='x')\n",
433+
" s=np.isnan(new_temperature[:, 1]) * 15, marker=\"x\")\n",
436434
"\n",
437-
"plt.suptitle('Temperature cross-section before and after restratification')\n",
435+
"plt.suptitle(\"Temperature cross-section before and after restratification\")\n",
438436
"plt.show()"
439437
]
440438
}

pyproject.toml

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ preview = false
145145

146146
[tool.ruff.lint]
147147
ignore = [
148-
149148
# flake8-annotations (ANN)
150149
# https://docs.astral.sh/ruff/rules/#flake8-annotations-ann
151150
"ANN001", # Missing type annotation for function argument {name}
@@ -156,21 +155,6 @@ ignore = [
156155
"ANN204", # Missing return type annotation for special method {name}
157156

158157
"ARG002", # Unused method argument: {name}
159-
"ARG003", # Unused class method argument: {name}
160-
161-
# flake8-bugbear (B)
162-
# https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
163-
"B028", # No explicit stacklevel keyword argument found
164-
165-
# flake8-comprehensions (C4)
166-
# https://docs.astral.sh/ruff/rules/#flake8-comprehensions-c4
167-
"C405", # Unnecessary {obj_type} literal (rewrite as a set literal)
168-
"C419", # Unnecessary list comprehension
169-
170-
# flake8-commas (COM)
171-
# https://docs.astral.sh/ruff/rules/#flake8-commas-com
172-
"COM812", # Trailing comma missing.
173-
"COM819", # Trailing comma prohibited.
174158

175159
# pydocstyle (D)
176160
# https://docs.astral.sh/ruff/rules/#pydocstyle-d
@@ -185,46 +169,12 @@ ignore = [
185169
# https://docs.astral.sh/ruff/rules/#eradicate-era
186170
"ERA001", # Found commented-out code
187171

188-
# flake8-boolean-trap (FBT)
189-
# https://docs.astral.sh/ruff/rules/#flake8-boolean-trap-fbt
190-
"FBT002", # Boolean default positional argument in function definition
191-
192-
# flake8-implicit-str-concat (ISC)
193-
# https://docs.astral.sh/ruff/rules/single-line-implicit-string-concatenation/
194-
# NOTE: This rule may cause conflicts when used with "ruff format".
195-
"ISC001", # Implicitly concatenate string literals on one line.
196-
197-
# pep8-naming (N)
198-
# https://docs.astral.sh/ruff/rules/#pep8-naming-n
199-
"N801", # Class name {name} should use CapWords convention
172+
"PT011",
200173

201-
# Refactor (R)
202-
# https://docs.astral.sh/ruff/rules/#refactor-r
203-
"PLR2004", # Magic value used in comparison, consider replacing {value} with a constant variable
204-
205-
# flake8-pytest-style (PT)
206-
# https://docs.astral.sh/ruff/rules/#flake8-pytest-style-pt
207-
"PT009", # Use a regular assert instead of unittest-style {assertion}
208-
"PT027", # Use pytest.raises instead of unittest-style {assertion}
209-
210-
# flake8-return (RET)
211-
# https://docs.astral.sh/ruff/rules/#flake8-return-ret
212-
"RET504", # Unnecessary assignment to {name} before return statement
213-
214-
# Ruff-specific rules (RUF)
215-
# https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf
216-
"RUF005", # Consider {expression} instead of concatenation
217-
"RUF012", # Mutable class attributes should be annotated with typing.ClassVar
218-
219-
# flake8-self (SLF)
220-
# https://docs.astral.sh/ruff/rules/#flake8-self-slf
221-
"SLF001", # Private member accessed: {access}
174+
"PLR2004",
222175

223-
# flake8-print (T20)
224-
# https://docs.astral.sh/ruff/rules/#flake8-print-t20
225-
"T201", # print found
226-
227-
]
176+
"S101", # Use of assert detected.
177+
]
228178
preview = false
229179
select = [
230180
"ALL",
@@ -239,13 +189,20 @@ known-first-party = ["iris"]
239189

240190
[tool.ruff.lint.per-file-ignores]
241191
# All test scripts
242-
192+
"src/stratify/tests/performance.py" = [
193+
"T201", # print found
194+
]
195+
"setup.py" = [
196+
"T201", # print found
197+
"RUF012", # Mutable class attributes should be annotated with typing.ClassVar
198+
]
243199
# Change to match specific package path:
244-
"lib/iris/tests/*.py" = [
200+
"src/stratify/tests/*.py" = [
245201
# https://docs.astral.sh/ruff/rules/undocumented-public-module/
246202
"D100", # Missing docstring in public module
247203
"D205", # 1 blank line required between summary line and description
248204
"D401", # 1 First line of docstring should be in imperative mood
205+
"SLF001", # Private member accessed: {access}
249206
]
250207

251208
[tool.ruff.lint.pydocstyle]

setup.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from Cython.Build import cythonize # isort:skip
1212
except ImportError:
1313
wmsg = "Cython unavailable, unable to build stratify extensions!"
14-
warnings.warn(wmsg)
14+
warnings.warn(wmsg, stacklevel=2)
1515
cythonize = None
1616

1717

@@ -53,7 +53,7 @@ def run(self):
5353
[
5454
("CYTHON_TRACE", "1"),
5555
("CYTHON_TRACE_NOGIL", "1"),
56-
]
56+
],
5757
)
5858
cython_directives.update({"linetrace": True})
5959
if FLAG_COVERAGE in sys.argv:
@@ -70,9 +70,11 @@ def run(self):
7070
)
7171
extensions.append(extension)
7272

73-
if cythonize and not any([arg in CMDS_NOCYTHONIZE for arg in sys.argv]):
73+
if cythonize and not any(arg in CMDS_NOCYTHONIZE for arg in sys.argv):
7474
extensions = cythonize(
75-
extensions, compiler_directives=cython_directives, language_level=3
75+
extensions,
76+
compiler_directives=cython_directives,
77+
language_level=3,
7678
)
7779

7880
cmdclass = {"clean_cython": CleanCython}

src/stratify/_bounded_vinterp.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def interpolate_conservative(z_target, z_src, fz_src, axis=-1):
9191
)
9292
raise ValueError(msg.format(tuple(dat_shape), tuple(src_shape)))
9393

94-
if z_src.shape[-1] != 2:
94+
shape_2 = 2
95+
if z_src.shape[-1] != shape_2:
9596
msg = "Unexpected source and target bounds shape. shape[-1] != 2"
9697
raise ValueError(msg)
9798

@@ -100,8 +101,8 @@ def interpolate_conservative(z_target, z_src, fz_src, axis=-1):
100101

101102
# src_data
102103
bdims = list(range(fz_src.ndim - (z_src.ndim - 1)))
103-
data_vdims = [ind for ind in range(fz_src.ndim) if ind not in (bdims + [axis])]
104-
data_transpose = bdims + [axis] + data_vdims
104+
data_vdims = [ind for ind in range(fz_src.ndim) if ind not in ([*bdims, axis])]
105+
data_transpose = [*bdims, axis, *data_vdims]
105106
fz_src_reshaped = np.transpose(fz_src, data_transpose)
106107
fz_src_orig = list(fz_src_reshaped.shape)
107108
shape = (
@@ -113,21 +114,24 @@ def interpolate_conservative(z_target, z_src, fz_src, axis=-1):
113114

114115
# Define our src and target bounds in a consistent way.
115116
# [axis_interpolation, z_varying, 2]
116-
vdims = list(set(range(z_src.ndim)) - set([axis_relative]))
117-
z_src_reshaped = np.transpose(z_src, [axis_relative] + vdims)
118-
z_target_reshaped = np.transpose(z_target, [axis_relative] + vdims)
117+
# vdims = list(set(range(z_src.ndim)) - set([axis_relative]))
118+
vdims = list(set(range(z_src.ndim)) - {axis_relative})
119+
z_src_reshaped = np.transpose(z_src, [axis_relative, *vdims])
120+
z_target_reshaped = np.transpose(z_target, [axis_relative, *vdims])
119121

120122
shape = int(np.prod(z_src_reshaped.shape[1:-1]))
121123
z_src_reshaped = z_src_reshaped.reshape(
122-
[z_src_reshaped.shape[0], shape, z_src_reshaped.shape[-1]]
124+
[z_src_reshaped.shape[0], shape, z_src_reshaped.shape[-1]],
123125
)
124126
shape = int(np.prod(z_target_reshaped.shape[1:-1]))
125127
z_target_reshaped = z_target_reshaped.reshape(
126-
[z_target_reshaped.shape[0], shape, z_target_reshaped.shape[-1]]
128+
[z_target_reshaped.shape[0], shape, z_target_reshaped.shape[-1]],
127129
)
128130

129131
result = conservative_interpolation(
130-
z_src_reshaped, z_target_reshaped, fz_src_reshaped
132+
z_src_reshaped,
133+
z_target_reshaped,
134+
fz_src_reshaped,
131135
)
132136

133137
# Turn the result into a shape consistent with the source.
@@ -136,5 +140,5 @@ def interpolate_conservative(z_target, z_src, fz_src, axis=-1):
136140
shape[len(bdims)] = z_target.shape[axis_relative]
137141
result = result.reshape(shape)
138142
invert_transpose = [data_transpose.index(ind) for ind in list(range(result.ndim))]
139-
result = result.transpose(invert_transpose)
140-
return result
143+
# result = result.transpose(invert_transpose)
144+
return result.transpose(invert_transpose)

src/stratify/tests/performance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import stratify
77

88

9-
def src_data(shape=(400, 500, 100), lazy=False):
9+
def src_data(shape=(400, 500, 100)):
1010
z = np.tile(np.linspace(0, 100, shape[-1]), np.prod(shape[:2])).reshape(shape)
1111
if lazy:
1212
fz = da.arange(np.prod(shape), dtype=np.float64).reshape(shape)

0 commit comments

Comments
 (0)