Skip to content

Commit 78542a0

Browse files
authored
Fix README example and add test to run python-code example in CI (#38)
Fix incorrect example in README, and add missing test that ensures that python-code example in README executes without error.
1 parent 4bb6937 commit 78542a0

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Add test to check python codeblocks in README keep working as code changes
13+
[\#38](https://github.com/mllam/weather-model-graphs/pull/38) @leifdenby
14+
1215
- Add coords_crs and graph_crs arguments to allow for using lat-lons coordinates
1316
or other CRSs as input. These are then converted to the specific CRS used when
1417
constructing the graph.
@@ -25,6 +28,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2528
- Fix crash when trying to create flat multiscale graphs with >= 3 levels
2629
[\#41](https://github.com/mllam/weather-model-graphs/pull/41), @joeloskarsson
2730

31+
- Fix example in README
32+
[\#38](https://github.com/mllam/weather-model-graphs/pull/38) @leifdenby
33+
2834
### Maintenance
2935

3036
- Update github CI actions, including pre-commit action to fix caching issue

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@ import numpy as np
7878
import weather_model_graphs as wmg
7979

8080
# define your (x,y) grid coodinates
81-
xy_grid = np.meshgrid(np.linspace(0, 1, 32), np.linspace(0, 1, 32))
82-
xy_grid = np.stack(xy_grid, axis=0)
81+
xs, ys = np.meshgrid(np.linspace(0, 1, 32), np.linspace(0, 1, 32))
82+
coords = np.stack([xs.flatten(), ys.flatten()], axis=-1)
8383

8484
# create the full graph
85-
graph = wmg.create.archetype.create_keisler_graph(xy_grid=xy_grid)
85+
graph = wmg.create.archetype.create_keisler_graph(
86+
coords=coords,
87+
mesh_node_distance=1.0/16 # use half the grid spacing for mesh nodes
88+
)
8689

8790
# split the graph by component
8891
graph_components = wmg.split_graph_by_edge_attribute(graph=graph, attr='component')

src/weather_model_graphs/create/mesh/kinds/flat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,12 @@ def create_flat_singlescale_mesh_graph(xy, mesh_node_distance: float):
112112
nx = int(range_x / mesh_node_distance)
113113
ny = int(range_y / mesh_node_distance)
114114

115+
if nx == 0 or ny == 0:
116+
raise ValueError(
117+
"The given `mesh_node_distance` is too large for the provided coordinates. "
118+
f"Got mesh_node_distance={mesh_node_distance}, but the x-range is {range_x} "
119+
f"and y-range is {range_y}. Maybe you want to decrease the `mesh_node_distance`"
120+
" so that the mesh nodes are spaced closer together?"
121+
)
122+
115123
return mesh_graph.create_single_level_2d_mesh_graph(xy=xy, nx=nx, ny=ny)

tests/test_readme.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import re
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
7+
def _parse_readme_examples():
8+
lines = open(Path(__file__).parent.parent / "README.md").read().splitlines()
9+
10+
# use regex to find all python code blocks
11+
code_blocks = re.findall(r"```python(.*?)```", "\n".join(lines), re.DOTALL)
12+
13+
return code_blocks
14+
15+
16+
@pytest.mark.parametrize("codeblock_example", _parse_readme_examples())
17+
def test_readme_example(codeblock_example: str):
18+
"""
19+
Check that execution of the python code block in the README does not raise an exception.
20+
"""
21+
exec(codeblock_example)

0 commit comments

Comments
 (0)