|
2 | 2 | tests/test_gridspec.py |
3 | 3 | ====================== |
4 | 4 |
|
5 | | -Tests for GridSpec / SubplotSpec indexing AND the figure sizing pipeline |
6 | | -(_compute_cell_sizes) that converts grid specs + figsize into per-panel |
7 | | -canvas pixel dimensions. |
| 5 | +Tests for GridSpec / SubplotSpec indexing, the figure sizing pipeline |
| 6 | +(_compute_cell_sizes), and per-panel plot-area alignment. |
8 | 7 |
|
9 | | -The sizing contract (all measured at the *canvas* level, before PAD margins): |
| 8 | +Sizing contract (all measured at the *canvas* level, before PAD margins): |
10 | 9 | - All panels in the same grid column have the same canvas width (pw). |
11 | 10 | - All panels in the same grid row have the same canvas height (ph). |
12 | 11 | - Grid tracks are pure ratio math — no aspect-locking. |
|
19 | 18 | sum(row tracks) <= fh. |
20 | 19 | - Images are rendered "contain" (letterboxed) in JS — the Python layout |
21 | 20 | engine never modifies tracks because of image content. |
| 21 | +
|
| 22 | +Alignment contract (inner plot-area coordinates, shared PAD constants): |
| 23 | + - PAD_L=58 PAD_R=12 PAD_T=12 PAD_B=42 |
| 24 | + - The inner plot/image area for any panel kind is: |
| 25 | + x=PAD_L, y=PAD_T, w=pw-PAD_L-PAD_R, h=ph-PAD_T-PAD_B |
| 26 | + - All panels in the same column share pw → same left/right edges. |
| 27 | + - All panels in the same row share ph → same top/bottom edges. |
22 | 28 | """ |
23 | 29 |
|
24 | 30 | from __future__ import annotations |
|
31 | 37 | from anyplotlib.figure import Figure |
32 | 38 | from anyplotlib.figure_plots import GridSpec, SubplotSpec, Axes # noqa: F401 |
33 | 39 |
|
| 40 | +# PAD constants must match figure_esm.js (used in panel-alignment tests) |
| 41 | +PAD_L, PAD_R, PAD_T, PAD_B = 58, 12, 12, 42 |
| 42 | + |
34 | 43 |
|
35 | 44 | # ───────────────────────────────────────────────────────────────────────────── |
36 | 45 | # Helpers |
@@ -705,6 +714,129 @@ def test_figsize_in_layout_json(self): |
705 | 714 | assert layout["fig_height"] == 555 |
706 | 715 |
|
707 | 716 |
|
| 717 | +# ───────────────────────────────────────────────────────────────────────────── |
| 718 | +# Part 8 – Panel alignment |
| 719 | +# ───────────────────────────────────────────────────────────────────────────── |
| 720 | + |
| 721 | +def _plot_area(pw: int, ph: int) -> tuple[int, int, int, int]: |
| 722 | + """Return (x, y, w, h) of the inner plot/image area for any panel kind. |
| 723 | +
|
| 724 | + Both 1-D and 2-D panels use the same PAD constants in figure_esm.js, |
| 725 | + so as long as Python assigns the same (pw, ph) to sibling panels they |
| 726 | + are guaranteed to be pixel-aligned inside the shared canvas grid cell. |
| 727 | + """ |
| 728 | + return PAD_L, PAD_T, pw - PAD_L - PAD_R, ph - PAD_T - PAD_B |
| 729 | + |
| 730 | + |
| 731 | +class TestPanelAlignment: |
| 732 | + """Same-row / same-column panels must share canvas dimensions and |
| 733 | + therefore produce identical inner plot-area coordinates.""" |
| 734 | + |
| 735 | + # ── two-row, one-column ─────────────────────────────────────────────── |
| 736 | + |
| 737 | + def test_2row_1col_same_width(self): |
| 738 | + fig, axs = vw.subplots(2, 1, figsize=(600, 600)) |
| 739 | + v2d = axs[0].imshow(np.random.rand(128, 128)) |
| 740 | + v1d = axs[1].plot(np.sin(np.linspace(0, 6, 256))) |
| 741 | + s = _sizes(fig) |
| 742 | + pw2d = s[v2d._id][0] |
| 743 | + pw1d = s[v1d._id][0] |
| 744 | + assert pw2d == pw1d, ( |
| 745 | + f"Panels in same column must have equal width: 2D={pw2d}, 1D={pw1d}" |
| 746 | + ) |
| 747 | + |
| 748 | + def test_2row_1col_left_edge_aligned(self): |
| 749 | + """Left edge of the 2D image area and 1D plot area must both be PAD_L.""" |
| 750 | + fig, axs = vw.subplots(2, 1, figsize=(600, 600)) |
| 751 | + v2d = axs[0].imshow(np.random.rand(128, 128)) |
| 752 | + v1d = axs[1].plot(np.sin(np.linspace(0, 6, 256))) |
| 753 | + s = _sizes(fig) |
| 754 | + x2d = _plot_area(*s[v2d._id])[0] |
| 755 | + x1d = _plot_area(*s[v1d._id])[0] |
| 756 | + assert x2d == x1d == PAD_L, ( |
| 757 | + f"Left edge must be PAD_L={PAD_L}: 2D={x2d}, 1D={x1d}" |
| 758 | + ) |
| 759 | + |
| 760 | + def test_2row_1col_plot_area_widths_equal(self): |
| 761 | + """Plot-area widths must match when panels share a column.""" |
| 762 | + fig, axs = vw.subplots(2, 1, figsize=(600, 600)) |
| 763 | + v2d = axs[0].imshow(np.random.rand(128, 128)) |
| 764 | + v1d = axs[1].plot(np.sin(np.linspace(0, 6, 256))) |
| 765 | + s = _sizes(fig) |
| 766 | + w2d = _plot_area(*s[v2d._id])[2] |
| 767 | + w1d = _plot_area(*s[v1d._id])[2] |
| 768 | + assert w2d == w1d, f"Plot area widths: 2D={w2d}, 1D={w1d}" |
| 769 | + |
| 770 | + # ── one-row, two-column ─────────────────────────────────────────────── |
| 771 | + |
| 772 | + def test_1row_2col_same_height(self): |
| 773 | + fig, axs = vw.subplots(1, 2, figsize=(800, 400)) |
| 774 | + v2d = axs[0].imshow(np.random.rand(64, 64)) |
| 775 | + v1d = axs[1].plot(np.cos(np.linspace(0, 6, 256))) |
| 776 | + s = _sizes(fig) |
| 777 | + ph2d = s[v2d._id][1] |
| 778 | + ph1d = s[v1d._id][1] |
| 779 | + assert ph2d == ph1d, ( |
| 780 | + f"Panels in same row must have equal height: 2D={ph2d}, 1D={ph1d}" |
| 781 | + ) |
| 782 | + |
| 783 | + def test_1row_2col_top_bottom_aligned(self): |
| 784 | + """Top and bottom y-coordinates of plot areas must match across the row.""" |
| 785 | + fig, axs = vw.subplots(1, 2, figsize=(800, 400)) |
| 786 | + v2d = axs[0].imshow(np.random.rand(64, 64)) |
| 787 | + v1d = axs[1].plot(np.cos(np.linspace(0, 6, 256))) |
| 788 | + s = _sizes(fig) |
| 789 | + y2d, h2d = _plot_area(*s[v2d._id])[1], _plot_area(*s[v2d._id])[3] |
| 790 | + y1d, h1d = _plot_area(*s[v1d._id])[1], _plot_area(*s[v1d._id])[3] |
| 791 | + assert y2d == y1d == PAD_T, f"Top y: 2D={y2d}, 1D={y1d}" |
| 792 | + assert h2d == h1d, f"Plot area heights: 2D={h2d}, 1D={h1d}" |
| 793 | + |
| 794 | + # ── 2D panel canvas equals its grid cell ───────────────────────────── |
| 795 | + |
| 796 | + def test_square_image_gets_square_canvas(self): |
| 797 | + """A 128×128 image in a 500×500 figsize → canvas is 500×500 (pw == ph). |
| 798 | + Images are letterboxed in JS; the Python layout never changes the cell.""" |
| 799 | + fig, axs = vw.subplots(1, 1, figsize=(500, 500)) |
| 800 | + v2d = axs.imshow(np.random.rand(128, 128)) |
| 801 | + pw, ph = _sizes(fig)[v2d._id] |
| 802 | + assert pw == ph, f"Square figsize must give pw==ph: pw={pw}, ph={ph}" |
| 803 | + |
| 804 | + def test_wide_image_canvas_equals_cell(self): |
| 805 | + """A 2:1 image in a square cell gets a square canvas — no aspect-lock.""" |
| 806 | + fig, axs = vw.subplots(1, 1, figsize=(512, 512)) |
| 807 | + v2d = axs.imshow(np.random.rand(128, 256)) # w=256, h=128 |
| 808 | + pw, ph = _sizes(fig)[v2d._id] |
| 809 | + assert pw == 512 and ph == 512, ( |
| 810 | + f"Canvas should equal full figsize 512×512, got {pw}×{ph}" |
| 811 | + ) |
| 812 | + |
| 813 | + # ── non-square 2D panel plus 1D panel — column width consistent ─────── |
| 814 | + |
| 815 | + def test_nonsquare_2d_and_1d_same_column(self): |
| 816 | + """A tall non-square image in a 2-row, 1-col layout must not affect the |
| 817 | + 1D panel's canvas width — both must equal the column track width.""" |
| 818 | + fig, axs = vw.subplots(2, 1, figsize=(600, 800)) |
| 819 | + v2d = axs[0].imshow(np.random.rand(256, 128)) # tall image |
| 820 | + v1d = axs[1].plot(np.random.rand(256)) |
| 821 | + s = _sizes(fig) |
| 822 | + pw2d = s[v2d._id][0] |
| 823 | + pw1d = s[v1d._id][0] |
| 824 | + assert pw2d == pw1d, ( |
| 825 | + f"Same-column panels must have equal width: 2D={pw2d}, 1D={pw1d}" |
| 826 | + ) |
| 827 | + |
| 828 | + # ── plot-area dimensions are positive ───────────────────────────────── |
| 829 | + |
| 830 | + def test_plot_areas_positive(self): |
| 831 | + fig, axs = vw.subplots(2, 1, figsize=(400, 400)) |
| 832 | + v2d = axs[0].imshow(np.random.rand(64, 64)) |
| 833 | + v1d = axs[1].plot(np.random.rand(128)) |
| 834 | + for pid, (pw, ph) in _sizes(fig).items(): |
| 835 | + x, y, w, h = _plot_area(pw, ph) |
| 836 | + assert w > 0, f"Panel {pid}: plot area width must be positive, got {w}" |
| 837 | + assert h > 0, f"Panel {pid}: plot area height must be positive, got {h}" |
| 838 | + |
| 839 | + |
708 | 840 |
|
709 | 841 |
|
710 | 842 |
|
|
0 commit comments