Skip to content

notebook screenshot tests #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,6 @@ jobs:
if: ${{ failure() }}
with:
name: screenshot-diffs
path: examples/desktop/diffs
path: |
examples/desktop/diffs
examples/notebooks/diffs
5 changes: 4 additions & 1 deletion .github/workflows/screenshots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ jobs:
run: |
# regenerate screenshots
REGENERATE_SCREENSHOTS=1 pytest -v examples
REGENERATE_SCREENSHOTS=1 pytest --nbmake examples/notebooks/
- uses: actions/upload-artifact@v3
if: always()
with:
name: screenshots
path: examples/desktop/screenshots/
path: |
examples/desktop/screenshots/
examples/notebooks/screenshots/
114 changes: 114 additions & 0 deletions examples/notebooks/lines_cmap.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
"import fastplotlib as fpl"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d2ef4aa-0e4c-4694-ae2e-05da1153a413",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# this is only for testing, you do not need this to use fastplotlib\n",
"from nb_test_utils import plot_test, notebook_finished"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -49,6 +62,18 @@
"plot.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "727282c3-aadf-420f-a88e-9dd4d4e91263",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"plot_test(\"lines-cmap-white\", plot)"
]
},
{
"cell_type": "markdown",
"id": "889b1858-ed64-4d6b-96ad-3883fbe4d38e",
Expand All @@ -69,6 +94,19 @@
"plot.graphics[0].cmap = \"jet\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c9b0bc8-b176-425c-8036-63dc55ab7466",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# for testing, ignore\n",
"plot_test(\"lines-cmap-jet\", plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -81,6 +119,19 @@
"plot.graphics[0].cmap.values = sine[:, 1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b19d2d4-90e7-40ed-afb9-13abe5474ace",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# for testing, ignore\n",
"plot_test(\"lines-cmap-jet-values\", plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -93,6 +144,19 @@
"plot.graphics[0].cmap.values = cosine[:, 1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a6c4739-fa61-4532-865e-21107eab76f9",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# for testing, ignore\n",
"plot_test(\"lines-cmap-jet-values-cosine\", plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -105,6 +169,19 @@
"plot.graphics[0].cmap = \"viridis\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45acfd2f-09f5-418c-bca5-3e574348b7d5",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# for testing, ignore\n",
"plot_test(\"lines-cmap-viridis\", plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -129,6 +206,19 @@
"plot.graphics[0].cmap.values = cmap_values"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7548407f-05ed-4c47-93cc-131c61f8e242",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# for testing, ignore\n",
"plot_test(\"lines-cmap-viridis-values\", plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -147,6 +237,30 @@
"id": "c290c642-ba5f-4a46-9a17-c434cb39de26",
"metadata": {},
"outputs": [],
"source": [
"# for testing, ignore\n",
"plot_test(\"lines-cmap-tab-10\", plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4b9e735-72e9-4f0e-aa3e-43db57e65c99",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# for testing, ignore\n",
"notebook_finished()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6735cc0-910c-4854-ac50-8ee553a6475e",
"metadata": {},
"outputs": [],
"source": []
}
],
Expand Down
87 changes: 87 additions & 0 deletions examples/notebooks/nb_test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from typing import *
import os
from pathlib import Path

import imageio.v3 as iio
import numpy as np

from fastplotlib import Plot, GridPlot

# make dirs for screenshots and diffs
current_dir = Path(__file__).parent

SCREENSHOTS_DIR = current_dir.joinpath("screenshots")
DIFFS_DIR = current_dir.joinpath("diffs")

os.makedirs(SCREENSHOTS_DIR, exist_ok=True)
os.makedirs(DIFFS_DIR, exist_ok=True)


# store all the failures to allow the nb to proceed to test other examples
FAILURES = list()


def plot_test(name, plot: Union[Plot, GridPlot]):
snapshot = plot.canvas.snapshot()

if "REGENERATE_SCREENSHOTS" in os.environ.keys():
if os.environ["REGENERATE_SCREENSHOTS"] == "1":
regenerate_screenshot(name, snapshot.data)

try:
assert_screenshot_equal(name, snapshot.data)
except AssertionError:
FAILURES.append(name)


def regenerate_screenshot(name, data):
iio.imwrite(SCREENSHOTS_DIR.joinpath(f"nb-{name}.png"), data)


def assert_screenshot_equal(name, data):
ground_truth = iio.imread(SCREENSHOTS_DIR.joinpath(f"nb-{name}.png"))

is_similar = np.allclose(data, ground_truth)

update_diffs(name, is_similar, data, ground_truth)

assert is_similar, (
f"notebook snapshot for {name} has changed"
)


def update_diffs(name, is_similar, img, ground_truth):
diffs_rgba = None

def get_diffs_rgba(slicer):
# lazily get and cache the diff computation
nonlocal diffs_rgba
if diffs_rgba is None:
# cast to float32 to avoid overflow
# compute absolute per-pixel difference
diffs_rgba = np.abs(ground_truth.astype("f4") - img)
# magnify small values, making it easier to spot small errors
diffs_rgba = ((diffs_rgba / 255) ** 0.25) * 255
# cast back to uint8
diffs_rgba = diffs_rgba.astype("u1")
return diffs_rgba[..., slicer]

# split into an rgb and an alpha diff
diffs = {
DIFFS_DIR.joinpath(f"nb-diff-{name}-rgb.png"): slice(0, 3),
DIFFS_DIR.joinpath(f"nb-diff-{name}-alpha.png"): 3,
}

for path, slicer in diffs.items():
if not is_similar:
diff = get_diffs_rgba(slicer)
iio.imwrite(path, diff)
elif path.exists():
path.unlink()


def notebook_finished():
if len(FAILURES) > 0:
raise AssertionError(
f"Failures for plots:\n{FAILURES}"
)
116 changes: 44 additions & 72 deletions examples/notebooks/scatter.ipynb

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-astronaut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-astronaut_RGB.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-camera.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-lines-3d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-lines-cmap-jet-values.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-lines-cmap-jet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-lines-cmap-tab-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-lines-cmap-viridis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-lines-cmap-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-lines-colors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-lines-data.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-lines-underlay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/notebooks/screenshots/nb-lines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy