Spaces:
Runtime error
Runtime error
Download tests/test_tricky.py from jkos9/plotscribe: direct link, hf CLI and curl.
- Browser
- Download file 4.47 kB
-
https://huggingface.co/spaces/jkos9/plotscribe/resolve/main/tests/test_tricky.py
- Command line
-
hf download hf://spaces/jkos9/plotscribe/tests/test_tricky.py
-
curl -L -o test_tricky.py https://huggingface.co/spaces/jkos9/plotscribe/resolve/main/tests/test_tricky.py
4.47 kB
| """Regression checks on real figures that once failed (data/my_figs/tricky_examples). | |
| The images are not in the repository (data/ is ignored); the tests are skipped when | |
| they are missing. Expected values are read off the figures by eye. | |
| """ | |
| from __future__ import annotations | |
| from pathlib import Path | |
| import numpy as np | |
| import pytest | |
| from plotscribe import pipeline | |
| DIR = Path("data/my_figs/tricky_examples") | |
| def run(name: str): | |
| p = DIR / name | |
| if not p.exists(): | |
| pytest.skip(f"{p} not available") | |
| return pipeline.run(str(p)) | |
| def col(table, key: str) -> np.ndarray: | |
| match = [c for c in table.columns if key in c] | |
| assert match, f"no column containing {key!r}: {list(table.columns)}" | |
| return table[match[0]].to_numpy(float) | |
| def test_t1_dotted_lines_with_markers_log_axis(): | |
| # pgfplots: dotted lines with markers, coloured in-plot annotations, log y. | |
| r = run("t_1.png") | |
| t = r.table | |
| assert r.chart_type == "line" | |
| assert t["x"].round().tolist() == [20, 40, 50, 60, 70] | |
| assert t.shape[1] == 5 # x + 4 legend series, no annotation series | |
| np.testing.assert_allclose(col(t, "SBRNN")[1:], [3.8e-3, 9.3e-3, 5.6e-2, 0.11], rtol=0.15) | |
| np.testing.assert_allclose(col(t, "Volterra")[1:], [2.0e-3, 5.0e-3, 6.6e-3, 1.1e-2], rtol=0.15) | |
| np.testing.assert_allclose(col(t, "SFFNN"), [3.8e-5, 1.1e-3, 1.7e-3, 2.9e-3, 4.2e-3], rtol=0.15) | |
| np.testing.assert_allclose(col(t, "auto-enc")[2:], [2.3e-5, 3.7e-4, 3.1e-3], rtol=0.15) | |
| def test_t2_same_colour_marker_and_dashed_line(): | |
| # xmgrace: black diamonds and a black dashed line in one legend, black-outlined | |
| # markers, an unlabelled red fit line. | |
| r = run("t_2.png") | |
| t = r.table | |
| assert len(r.extra["legend_info"]["entries"]) == 7 | |
| greedy = col(t, "Greedy") | |
| assert np.sum(np.isfinite(greedy)) >= 5 | |
| np.testing.assert_allclose(greedy[np.isfinite(greedy)], -0.693, atol=0.002) | |
| parisi = col(t, "Parisi") | |
| np.testing.assert_allclose(parisi[np.isfinite(parisi)], -0.763, atol=0.004) | |
| eo = col(t, "EO-Heuristic") | |
| assert np.sum(np.isfinite(eo)) >= 5 | |
| np.testing.assert_allclose(col(t, "GNN")[np.isfinite(col(t, "GNN"))], -0.612, atol=0.002) | |
| def test_t3_grouped_bars_numeric_x_legend_with_bar_swatches(): | |
| r = run("t_3.png") | |
| t = r.table | |
| assert t["x"].tolist() == [10, 11, 12, 13, 14, 15] | |
| np.testing.assert_allclose(col(t, "determination"), [0.646, 0.70, 0.729, 0.756, 0.788, 0.797], atol=0.01) | |
| np.testing.assert_allclose(col(t, "pearson"), [0.966, 0.968, 0.971, 0.972, 0.975, 0.977], atol=0.01) | |
| def test_t4_tinted_bars_against_spine(): | |
| # Light pink / lavender bars (tints of red / blue); last bar against the frame. | |
| r = run("t_4.png") | |
| t = r.table | |
| assert t["x"].tolist() == pytest.approx([16, 32, 64]) | |
| assert t.shape[1] == 6 | |
| np.testing.assert_allclose(t.iloc[0, 1:].to_numpy(float), [12.7, 11.75, 12.1, 10.9, 10.8], atol=0.2) | |
| np.testing.assert_allclose(t.iloc[2, 1:].to_numpy(float), [10.0, 7.6, 8.15, 7.05, 6.5], atol=0.2) | |
| BAR_EXPECTED = { | |
| "t_3.png": ([10, 11, 12, 13, 14, 15], [[0.646, 0.70, 0.729, 0.756, 0.788, 0.797], | |
| [0.966, 0.968, 0.971, 0.972, 0.975, 0.977]], 0.012), | |
| "t_4.png": ([16, 32, 64], [[12.7, 12.7, 10.0], [11.75, 9.6, 7.6], [12.1, 10.7, 8.15], [10.9, 9.1, 7.05], | |
| [10.8, 8.75, 6.5]], 0.25), | |
| } | |
| def test_bar_figures_survive_rescaling_and_jpeg(tmp_path, name, scale, jpeg): | |
| # Uploads are often screenshots or JPEGs of the figure, not the original file. | |
| import cv2 | |
| src = DIR / name | |
| if not src.exists(): | |
| pytest.skip(f"{src} not available") | |
| img = cv2.imread(str(src)) | |
| img = cv2.resize(img, None, fx=scale, fy=scale, | |
| interpolation=cv2.INTER_AREA if scale < 1 else cv2.INTER_CUBIC) | |
| out = tmp_path / ("v.jpg" if jpeg else "v.png") | |
| cv2.imwrite(str(out), img, [cv2.IMWRITE_JPEG_QUALITY, jpeg] if jpeg else []) | |
| t = pipeline.run(str(out)).table | |
| xs, cols, tol = BAR_EXPECTED[name] | |
| assert t.shape == (len(xs), len(cols) + 1) | |
| np.testing.assert_allclose(t.iloc[:, 0].to_numpy(float), xs, atol=0.3) | |
| np.testing.assert_allclose(t.iloc[:, 1:].to_numpy(float).T, cols, atol=tol) | |