From 0a3ae225ab998462ce70f8a2c61439df548242d0 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 4 Sep 2024 10:04:30 +0200 Subject: [PATCH 1/7] CI: Split off example testing in new script --- .github/workflows/build_lint.yml | 23 ------------------ .github/workflows/examples.yml | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/examples.yml diff --git a/.github/workflows/build_lint.yml b/.github/workflows/build_lint.yml index d407b178dce..f489f54cc03 100644 --- a/.github/workflows/build_lint.yml +++ b/.github/workflows/build_lint.yml @@ -64,26 +64,3 @@ jobs: - if: matrix.os == 'ubuntu' name: Codecov uses: codecov/codecov-action@v4 - - examples: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: "3.12" - cache: 'pip' - - name: Install uv - run: pip install uv - - name: Install Mesa - run: uv pip install --system .[examples] - - name: Checkout mesa-examples - uses: actions/checkout@v4 - with: - repository: projectmesa/mesa-examples - path: mesa-examples - - name: Test examples - run: | - cd mesa-examples - pytest -rA -Werror -Wdefault::FutureWarning test_examples.py diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml new file mode 100644 index 00000000000..9873daf2024 --- /dev/null +++ b/.github/workflows/examples.yml @@ -0,0 +1,40 @@ +name: Examples + +on: + push: + branches: + - main + - release** + - "**maintenance" + paths-ignore: + - '**.md' + pull_request: + paths-ignore: + - '**.md' + workflow_dispatch: + schedule: + - cron: '0 6 * * 1' + +jobs: + examples: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + cache: 'pip' + - name: Install uv + run: pip install uv + - name: Install dependencies + run: uv pip install --system .[examples] + - name: Checkout mesa-examples + uses: actions/checkout@v4 + with: + repository: projectmesa/mesa-examples + path: mesa-examples + - name: Test examples + run: | + cd mesa-examples + pytest -rA -Werror -Wdefault::FutureWarning test_examples.py From 25cdf5f5304bd986df0e6fece29ac4ae0db00cd4 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 4 Sep 2024 10:08:29 +0200 Subject: [PATCH 2/7] CI: Add script and job to test tutorials --- .github/workflows/examples.yml | 17 +++++++++++++++++ tests/test_tutorials.py | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/test_tutorials.py diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index 9873daf2024..b61bde698b3 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -38,3 +38,20 @@ jobs: run: | cd mesa-examples pytest -rA -Werror -Wdefault::FutureWarning test_examples.py + + tutorials: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + cache: 'pip' + - name: Install uv + run: pip install uv + - name: Install dependencies + run: uv pip install --system . pytest nbval jupyter nbconvert + # Run Pytest on Jupyter Notebooks + - name: Test Jupyter Notebooks with Pytest + run: pytest -rA -Werror -Wdefault::FutureWarning tests/test_tutorials.py diff --git a/tests/test_tutorials.py b/tests/test_tutorials.py new file mode 100644 index 00000000000..2cfe3b70a7d --- /dev/null +++ b/tests/test_tutorials.py @@ -0,0 +1,21 @@ +import glob + +import pytest + +# Locate all notebooks in docs and subdirectories +notebooks = glob.glob("docs/**/*.ipynb", recursive=True) + + +# Parameterize the test with all found notebooks +@pytest.mark.parametrize("notebook", notebooks) +def test_notebook_execution(notebook): + # This runs each notebook using nbval and ensures there are no errors + pytest.main( + [ + "--nbval", + "--current-env", # Use the current environment + "--sanitize-with", + ".sanitize_config.yaml", # Optional: If you want to handle variable output, use this + notebook, + ] + ) From ab9fc1354bb2dc151bb7ac07c6314215a641a307 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 4 Sep 2024 10:15:49 +0200 Subject: [PATCH 3/7] Resolve warnings --- tests/test_tutorials.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_tutorials.py b/tests/test_tutorials.py index 2cfe3b70a7d..edd05db1a7d 100644 --- a/tests/test_tutorials.py +++ b/tests/test_tutorials.py @@ -13,8 +13,8 @@ def test_notebook_execution(notebook): pytest.main( [ "--nbval", - "--current-env", # Use the current environment - "--sanitize-with", + "--nbval-current-env", # Use the current environment + "--nbval-sanitize-with", ".sanitize_config.yaml", # Optional: If you want to handle variable output, use this notebook, ] From d99dafe654d3d78dd4fe67a89b0ce9b27adb246c Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 4 Sep 2024 10:19:45 +0200 Subject: [PATCH 4/7] Remove sanitize --- tests/test_tutorials.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_tutorials.py b/tests/test_tutorials.py index edd05db1a7d..18aea7ae7f5 100644 --- a/tests/test_tutorials.py +++ b/tests/test_tutorials.py @@ -14,8 +14,6 @@ def test_notebook_execution(notebook): [ "--nbval", "--nbval-current-env", # Use the current environment - "--nbval-sanitize-with", - ".sanitize_config.yaml", # Optional: If you want to handle variable output, use this notebook, ] ) From 69dc8a937073740f52f946fb90945546cee25ad7 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 4 Sep 2024 10:26:47 +0200 Subject: [PATCH 5/7] Use new setup --- .github/workflows/examples.yml | 6 ++++-- tests/test_tutorials.py | 28 ++++++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index b61bde698b3..58ff2b0ddd8 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -50,8 +50,10 @@ jobs: cache: 'pip' - name: Install uv run: pip install uv - - name: Install dependencies - run: uv pip install --system . pytest nbval jupyter nbconvert + - name: Install Mesa dependencies + run: uv pip install --system . + - name: Install tutorial dependencies + run: uv pip install --system pytest jupyter nbconvert seaborn pandas # Run Pytest on Jupyter Notebooks - name: Test Jupyter Notebooks with Pytest run: pytest -rA -Werror -Wdefault::FutureWarning tests/test_tutorials.py diff --git a/tests/test_tutorials.py b/tests/test_tutorials.py index 18aea7ae7f5..3280ad5ad86 100644 --- a/tests/test_tutorials.py +++ b/tests/test_tutorials.py @@ -1,19 +1,23 @@ import glob +import nbformat import pytest +from nbconvert.preprocessors import ExecutePreprocessor -# Locate all notebooks in docs and subdirectories notebooks = glob.glob("docs/**/*.ipynb", recursive=True) -# Parameterize the test with all found notebooks -@pytest.mark.parametrize("notebook", notebooks) -def test_notebook_execution(notebook): - # This runs each notebook using nbval and ensures there are no errors - pytest.main( - [ - "--nbval", - "--nbval-current-env", # Use the current environment - notebook, - ] - ) +@pytest.mark.parametrize("notebook_path", notebooks) +def test_notebook_execution(notebook_path): + # Load the notebook + with open(notebook_path) as f: + nb = nbformat.read(f, as_version=4) + + # Set up the notebook execution + ep = ExecutePreprocessor(timeout=600, kernel_name="python3") + + try: + # Execute the notebook and catch any errors + ep.preprocess(nb, {"metadata": {"path": "./"}}) + except Exception as e: + pytest.fail(f"Notebook {notebook_path} failed: {e!s}") From 989822044c80db5e2ab8c492bfc2fec2c6fdf494 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 4 Sep 2024 10:32:37 +0200 Subject: [PATCH 6/7] jupyter platform dirs --- .github/workflows/examples.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index 58ff2b0ddd8..e0a1f622bc9 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -56,4 +56,6 @@ jobs: run: uv pip install --system pytest jupyter nbconvert seaborn pandas # Run Pytest on Jupyter Notebooks - name: Test Jupyter Notebooks with Pytest + env: + JUPYTER_PLATFORM_DIRS: 1 run: pytest -rA -Werror -Wdefault::FutureWarning tests/test_tutorials.py From 7389cce1a7f7c7a91080523b9ff22d8873c39658 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Wed, 4 Sep 2024 10:39:24 +0200 Subject: [PATCH 7/7] Introduce error to test RTD --- docs/tutorials/visualization_tutorial.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/visualization_tutorial.ipynb b/docs/tutorials/visualization_tutorial.ipynb index c40b0d1b165..ba8d7a922d2 100644 --- a/docs/tutorials/visualization_tutorial.ipynb +++ b/docs/tutorials/visualization_tutorial.ipynb @@ -208,7 +208,7 @@ " # plt.figure(), for thread safety purpose\n", " fig = Figure()\n", " ax = fig.subplots()\n", - " wealth_vals = [agent.wealth for agent in model.agents]\n", + " wealth_vals = [agent.wealth for agent in model.schedule.agents]\n", " # Note: you have to use Matplotlib's OOP API instead of plt.hist\n", " # because plt.hist is not thread-safe.\n", " ax.hist(wealth_vals, bins=10)\n",