From 19d4933e080fb5980e2d2ac861c1fa7bd090902d Mon Sep 17 00:00:00 2001 From: Leif Denby Date: Thu, 28 Nov 2024 17:23:15 +0100 Subject: [PATCH 1/8] add test to run python-code example in readme Add missing test that ensures that python-code example in README executes without error. --- tests/test_readme.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/test_readme.py diff --git a/tests/test_readme.py b/tests/test_readme.py new file mode 100644 index 0000000..02ddaaf --- /dev/null +++ b/tests/test_readme.py @@ -0,0 +1,18 @@ +import re +from pathlib import Path + +import pytest + + +def _parse_readme_examples(): + lines = open(Path(__file__).parent.parent / "README.md").read().splitlines() + + # use regex to find all python code blocks + code_blocks = re.findall(r"```python(.*?)```", "\n".join(lines), re.DOTALL) + + return code_blocks + + +@pytest.mark.parametrize("example", _parse_readme_examples()) +def test_readme_example(example): + exec(example) From 61ba1ec40d06605b91f34d0e4c7e6511a5d87387 Mon Sep 17 00:00:00 2001 From: Leif Denby Date: Fri, 29 Nov 2024 11:30:26 +0100 Subject: [PATCH 2/8] clearer docstring and types --- tests/test_readme.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_readme.py b/tests/test_readme.py index 02ddaaf..c580a11 100644 --- a/tests/test_readme.py +++ b/tests/test_readme.py @@ -13,6 +13,9 @@ def _parse_readme_examples(): return code_blocks -@pytest.mark.parametrize("example", _parse_readme_examples()) -def test_readme_example(example): - exec(example) +@pytest.mark.parametrize("codeblock_examples", _parse_readme_examples()) +def test_readme_example(codeblock_example: str): + """ + Check that execution of the python code block in the README does not raise an exception. + """ + exec(codeblock_example) From a770cfdda3b59fc10ae233589325bbb47f1c1b0b Mon Sep 17 00:00:00 2001 From: Leif Denby Date: Fri, 29 Nov 2024 11:34:07 +0100 Subject: [PATCH 3/8] fix typo --- tests/test_readme.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_readme.py b/tests/test_readme.py index c580a11..7d06885 100644 --- a/tests/test_readme.py +++ b/tests/test_readme.py @@ -13,7 +13,7 @@ def _parse_readme_examples(): return code_blocks -@pytest.mark.parametrize("codeblock_examples", _parse_readme_examples()) +@pytest.mark.parametrize("codeblock_example", _parse_readme_examples()) def test_readme_example(codeblock_example: str): """ Check that execution of the python code block in the README does not raise an exception. From f5bcf2746a93644c4e1f5e1da46ebf13f8f74528 Mon Sep 17 00:00:00 2001 From: Leif Denby Date: Fri, 29 Nov 2024 11:46:24 +0100 Subject: [PATCH 4/8] changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7ca907..9fc515f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased](https://github.com/mllam/weather-model-graphs/compare/v0.2.0...HEAD) +### Added + +- added test to check python codeblocks in README keep working as code changes + [\#38](https://github.com/mllam/weather-model-graphs/pull/38) @leifdenby + ## [v0.2.0](https://github.com/mllam/weather-model-graphs/releases/tag/v0.2.0) From f1cffe294da4fad0d30ad9dc63aacc9e83eaea85 Mon Sep 17 00:00:00 2001 From: Leif Denby Date: Mon, 15 Sep 2025 15:25:56 +0200 Subject: [PATCH 5/8] update pre-commit action version --- .github/workflows/pre-commit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 92a3c07..c3566b9 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -13,4 +13,4 @@ jobs: steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - - uses: pre-commit/action@v2.0.3 + - uses: pre-commit/action@v3.0.1 From c1620cf3340e8260c13de00ddfd1a27bd03c4ca2 Mon Sep 17 00:00:00 2001 From: Leif Denby Date: Mon, 15 Sep 2025 15:42:06 +0200 Subject: [PATCH 6/8] fix mistake in readme caught by test :) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 693ca5c..7b29218 100644 --- a/README.md +++ b/README.md @@ -79,10 +79,10 @@ import weather_model_graphs as wmg # define your (x,y) grid coodinates xy_grid = np.meshgrid(np.linspace(0, 1, 32), np.linspace(0, 1, 32)) -xy_grid = np.stack(xy_grid, axis=0) +coords = np.stack(xy_grid, axis=0) # create the full graph -graph = wmg.create.archetype.create_keisler_graph(xy_grid=xy_grid) +graph = wmg.create.archetype.create_keisler_graph(coords=coords) # split the graph by component graph_components = wmg.split_graph_by_edge_attribute(graph=graph, attr='component') From bac14151ef2256d1306a4bce9678000973a60a0a Mon Sep 17 00:00:00 2001 From: Leif Denby Date: Tue, 16 Sep 2025 09:07:15 +0200 Subject: [PATCH 7/8] fix readme example and helping exception text --- README.md | 9 ++++++--- src/weather_model_graphs/create/mesh/kinds/flat.py | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7b29218..e22fd8d 100644 --- a/README.md +++ b/README.md @@ -78,11 +78,14 @@ import numpy as np import weather_model_graphs as wmg # define your (x,y) grid coodinates -xy_grid = np.meshgrid(np.linspace(0, 1, 32), np.linspace(0, 1, 32)) -coords = np.stack(xy_grid, axis=0) +xs, ys = np.meshgrid(np.linspace(0, 1, 32), np.linspace(0, 1, 32)) +coords = np.stack([xs.flatten(), ys.flatten()], axis=-1) # create the full graph -graph = wmg.create.archetype.create_keisler_graph(coords=coords) +graph = wmg.create.archetype.create_keisler_graph( + coords=coords, + mesh_node_distance=1.0/16 # use half the grid spacing for mesh nodes +) # split the graph by component graph_components = wmg.split_graph_by_edge_attribute(graph=graph, attr='component') diff --git a/src/weather_model_graphs/create/mesh/kinds/flat.py b/src/weather_model_graphs/create/mesh/kinds/flat.py index cf14fc5..92f47b0 100644 --- a/src/weather_model_graphs/create/mesh/kinds/flat.py +++ b/src/weather_model_graphs/create/mesh/kinds/flat.py @@ -112,4 +112,12 @@ def create_flat_singlescale_mesh_graph(xy, mesh_node_distance: float): nx = int(range_x / mesh_node_distance) ny = int(range_y / mesh_node_distance) + if nx == 0 or ny == 0: + raise ValueError( + "The given `mesh_node_distance` is too large for the provided coordinates. " + f"Got mesh_node_distance={mesh_node_distance}, but the x-range is {range_x} " + f"and y-range is {range_y}. Maybe you want to decrease the `mesh_node_distance`" + " so that the mesh nodes are spaced closer together?" + ) + return mesh_graph.create_single_level_2d_mesh_graph(xy=xy, nx=nx, ny=ny) From cef4862f531831296f407c915cc96b06f075b41e Mon Sep 17 00:00:00 2001 From: Leif Denby Date: Tue, 16 Sep 2025 14:56:22 +0200 Subject: [PATCH 8/8] changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1acc36..40faf55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix crash when trying to create flat multiscale graphs with >= 3 levels [\#41](https://github.com/mllam/weather-model-graphs/pull/41), @joeloskarsson +- Fix example in README + [\#38](https://github.com/mllam/weather-model-graphs/pull/38) @leifdenby + ### Maintenance - Update github CI actions, including pre-commit action to fix caching issue