Skip to content

Commit 1256cbf

Browse files
committed
new_linter() now uses a config name arg instead of a bool arg; Split example notebook into two
1 parent 878c0e2 commit 1256cbf

File tree

10 files changed

+1691
-711
lines changed

10 files changed

+1691
-711
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# xrlint
22
/xrlint_config.*
3+
/notebooks/xrlint_config.*
34

45
# Logs
56
*.log

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
- XRLint CLI now outputs single results immediately to console,
77
instead only after all results have been collected.
88
- Refactored and renamed `CliEngine` into `XRLint`. Documented the class.
9-
9+
- `new_linter()` now uses a config name arg instead of a bool arg.
10+
- Split example notebook into two
1011

1112
## Early development snapshots
1213

docs/start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ import xrlint.all as xrl
105105
106106
test_ds = xr.Dataset(attrs={"title": "Test Dataset"})
107107
108-
linter = xrl.new_linter(recommended=True)
108+
linter = xrl.new_linter("recommended")
109109
linter.verify_dataset(test_ds)
110110
```

notebooks/mkdataset.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import numpy as np
2+
import xarray as xr
3+
4+
nx = 2
5+
ny = 3
6+
nt = 4
7+
8+
9+
def make_dataset() -> xr.Dataset:
10+
"""Create a dataset that passes xrlint core rules."""
11+
12+
return xr.Dataset(
13+
attrs=dict(title="SST-Climatology Subset"),
14+
coords={
15+
"x": xr.DataArray(
16+
np.linspace(-180, 180, nx),
17+
dims="x",
18+
attrs={"units": "degrees"}
19+
),
20+
"y": xr.DataArray(
21+
np.linspace(-90, 90, ny),
22+
dims="y",
23+
attrs={"units": "degrees"}
24+
),
25+
"time": xr.DataArray(
26+
[2010 + y for y in range(nt)],
27+
dims="time",
28+
attrs={"units": "years"}
29+
),
30+
"spatial_ref": xr.DataArray(
31+
0,
32+
attrs={
33+
"grid_mapping_name": "latitude_longitude",
34+
"semi_major_axis": 6371000.0,
35+
"inverse_flattening": 0,
36+
},
37+
),
38+
},
39+
data_vars={
40+
"sst": xr.DataArray(
41+
np.random.random((nt, ny, nx)),
42+
dims=["time", "y", "x"],
43+
attrs={"units": "kelvin", "grid_mapping": "spatial_ref"}
44+
),
45+
"sst_anomaly": xr.DataArray(
46+
np.random.random((nt, ny, nx)),
47+
dims=["time", "y", "x"],
48+
attrs={"units": "kelvin", "grid_mapping": "spatial_ref"}
49+
)
50+
},
51+
)
52+
53+
54+
def make_dataset_with_issues() -> xr.Dataset:
55+
"""Create a dataset that produces issues with xrlint core rules."""
56+
invalid_ds = make_dataset()
57+
invalid_ds.attrs = {}
58+
invalid_ds.sst.attrs["units"] = 1
59+
invalid_ds["sst_avg"] = xr.DataArray(
60+
np.random.random((nx, ny)),
61+
dims=["x", "y"],
62+
attrs={"units": "kelvin"}
63+
)
64+
return invalid_ds

notebooks/xrlint-api-demo.ipynb

Lines changed: 0 additions & 694 deletions
This file was deleted.

notebooks/xrlint-cli.ipynb

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## XRLint CLI\n",
8+
"\n",
9+
"---"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 4,
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"Usage: xrlint [OPTIONS] [FILES]...\n",
22+
"\n",
23+
" Validate the given dataset FILES.\n",
24+
"\n",
25+
" Reads configuration from `./xrlint_config.*` if such file exists and unless\n",
26+
" `--no_config_lookup` is set or `--config` is provided. Then validates each\n",
27+
" dataset in FILES against the configuration. The default dataset patters are\n",
28+
" `**/*.zarr` and `**/.nc`. FILES may comprise also directories. If a\n",
29+
" directory is not matched by any file pattern, it will be traversed\n",
30+
" recursively. The validation result is dumped to standard output if not\n",
31+
" otherwise stated by `--output-file`. The output format is `simple` by\n",
32+
" default. Other inbuilt formats are `json` and `html` which you can specify\n",
33+
" using the `--format` option.\n",
34+
"\n",
35+
"Options:\n",
36+
" --no-config-lookup Disable use of default configuration from\n",
37+
" xrlint_config.*\n",
38+
" -c, --config FILE Use this configuration, overriding xrlint_config.*\n",
39+
" config options if present\n",
40+
" --print-config FILE Print the configuration for the given file\n",
41+
" --plugin MODULE Specify plugins. MODULE is the name of Python module\n",
42+
" that defines an 'export_plugin()' function.\n",
43+
" --rule SPEC Specify rules. SPEC must have format '<rule-name>:\n",
44+
" <rule-config>' (note the space character).\n",
45+
" -o, --output-file FILE Specify file to write report to\n",
46+
" -f, --format NAME Use a specific output format - default: simple\n",
47+
" --color / --no-color Force enabling/disabling of color\n",
48+
" --max-warnings COUNT Number of warnings to trigger nonzero exit code -\n",
49+
" default: 5\n",
50+
" --init Write initial configuration file and exit.\n",
51+
" --version Show the version and exit.\n",
52+
" --help Show this message and exit.\n"
53+
]
54+
}
55+
],
56+
"source": [
57+
"!xrlint --help"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 5,
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"data": {
67+
"text/plain": [
68+
"'C:\\\\Users\\\\norma\\\\Projects\\\\xrlint\\\\notebooks'"
69+
]
70+
},
71+
"execution_count": 5,
72+
"metadata": {},
73+
"output_type": "execute_result"
74+
}
75+
],
76+
"source": [
77+
"import os\n",
78+
"os.getcwd()"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 6,
84+
"metadata": {},
85+
"outputs": [
86+
{
87+
"name": "stderr",
88+
"output_type": "stream",
89+
"text": [
90+
"Error: file xrlint_config.yaml already exists.\n"
91+
]
92+
}
93+
],
94+
"source": [
95+
"!xrlint --init"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 7,
101+
"metadata": {},
102+
"outputs": [
103+
{
104+
"data": {
105+
"text/plain": [
106+
"<xarray.backends.zarr.ZarrStore at 0x2b09f4c3010>"
107+
]
108+
},
109+
"execution_count": 7,
110+
"metadata": {},
111+
"output_type": "execute_result"
112+
}
113+
],
114+
"source": [
115+
"from mkdataset import make_dataset, make_dataset_with_issues\n",
116+
"\n",
117+
"make_dataset().to_zarr(\"valid.zarr\", mode=\"w\")\n",
118+
"make_dataset_with_issues().to_zarr(\"invalid.zarr\", mode=\"w\")"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": 8,
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"name": "stdout",
128+
"output_type": "stream",
129+
"text": [
130+
"\n",
131+
"valid.zarr - ok\n",
132+
"\n",
133+
"invalid.zarr:\n",
134+
"dataset warn Missing 'title' attribute in dataset. dataset-title-attr\n",
135+
"dataset.attrs warn Missing metadata, attributes are empty. no-empty-attrs\n",
136+
"dataset.data_vars['sst'] warn Invalid 'units' attribute in variable 'sst'. var-units-attr\n",
137+
"\n",
138+
"3 warnings\n",
139+
"\n"
140+
]
141+
}
142+
],
143+
"source": [
144+
"!xrlint --no-color valid.zarr invalid.zarr"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 39,
150+
"metadata": {},
151+
"outputs": [
152+
{
153+
"name": "stdout",
154+
"output_type": "stream",
155+
"text": [
156+
"<div role=\"results\">\n",
157+
"<h3>Results</h3>\n",
158+
"<div role=\"result\">\n",
159+
"<p role=\"file\">valid.zarr - ok</p>\n",
160+
"</div>\n",
161+
"<hr/>\n",
162+
"<div role=\"result\">\n",
163+
"<p role=\"file\">invalid.zarr:</p>\n",
164+
"<table>\n",
165+
"<tbody>\n",
166+
"<tr><td>dataset </td><td>warn</td><td>Missing &#x27;title&#x27; attribute in dataset. </td><td>dataset-title-attr</td></tr>\n",
167+
"<tr><td>dataset.attrs </td><td>warn</td><td>Missing metadata, attributes are empty. </td><td>no-empty-attrs </td></tr>\n",
168+
"<tr><td>dataset.data_vars[&#x27;sst&#x27;]</td><td>warn</td><td>Invalid &#x27;units&#x27; attribute in variable &#x27;sst&#x27;.</td><td>var-units-attr </td></tr>\n",
169+
"</tbody>\n",
170+
"</table><p role=\"summary\">3 warnings</p>\n",
171+
"</div>\n",
172+
"</div>\n",
173+
"\n"
174+
]
175+
}
176+
],
177+
"source": [
178+
"!xrlint valid.zarr invalid.zarr -f html"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": 9,
184+
"metadata": {},
185+
"outputs": [
186+
{
187+
"name": "stdout",
188+
"output_type": "stream",
189+
"text": [
190+
"{\n",
191+
" \"name\": \"<computed>\",\n",
192+
" \"plugins\": {\n",
193+
" \"__core__\": \"xrlint.plugins.core\"\n",
194+
" },\n",
195+
" \"rules\": {\n",
196+
" \"coords-for-dims\": 2,\n",
197+
" \"dataset-title-attr\": 1,\n",
198+
" \"grid-mappings\": 2,\n",
199+
" \"no-empty-attrs\": 1,\n",
200+
" \"var-units-attr\": 1\n",
201+
" }\n",
202+
"}\n"
203+
]
204+
}
205+
],
206+
"source": [
207+
"!xrlint --print-config valid.zarr"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": null,
213+
"metadata": {},
214+
"outputs": [],
215+
"source": []
216+
}
217+
],
218+
"metadata": {
219+
"kernelspec": {
220+
"display_name": "Python 3 (ipykernel)",
221+
"language": "python",
222+
"name": "python3"
223+
},
224+
"language_info": {
225+
"codemirror_mode": {
226+
"name": "ipython",
227+
"version": 3
228+
},
229+
"file_extension": ".py",
230+
"mimetype": "text/x-python",
231+
"name": "python",
232+
"nbconvert_exporter": "python",
233+
"pygments_lexer": "ipython3",
234+
"version": "3.11.11"
235+
}
236+
},
237+
"nbformat": 4,
238+
"nbformat_minor": 4
239+
}

0 commit comments

Comments
 (0)