# Felisyn recipe-check-009: measurement of the published Figure 4G log-log panels.
# Written during the 21-22 October 2026 investigation (original file measure_fig4g_v2.py,
# SHA-256 7edd38c32401ae624ab9a2d0ff86eff748e78b3c91dcd991855adb34a79d3eed); reviewed and re-run by Felisyn on 22 October 2026 without code changes.
# Usage: python3 -I measure_fig4g.py sources/Figure-4-large.png 1255
# 1255 is the x pixel of the right-hand (log-log) panels' vertical axis, found as the
# longest dark vertical line in the right half of the image.
"""Judge measurement v2 of Fig 4G right-column (log-log) panels.
Usage: python3 -I measure_fig4g_v2.py IMAGE SPINE_X
Calibrates y from the three major y ticks (1e0, 1e-2, 1e-4) of each panel and
x from the major x ticks (1e-1..1e2) of the bottom panel; reports darkness-weighted
marker centroids."""
import sys
import numpy as np
from PIL import Image
from scipy import ndimage

g = np.asarray(Image.open(sys.argv[1]).convert("L")).astype(float)
spine_x = int(sys.argv[2])
H, W = g.shape
dk = 255.0 - g
dark = g < 80

col = dark[:, spine_x]
lab, n = ndimage.label(col)
panels = []
for i in range(1, n + 1):
    ys = np.where(lab == i)[0]
    if len(ys) > 0.1 * H:
        panels.append((int(ys.min()), int(ys.max())))
print("panels:", panels)

def groups_weighted(profile, offset, thr):
    idx = np.where(profile > thr)[0]
    gr = []
    for i in idx:
        if gr and i - gr[-1][-1] <= 1:
            gr[-1].append(i)
        else:
            gr.append([i])
    out = []
    for gg in gr:
        w = profile[gg]
        out.append(offset + float(np.sum(np.array(gg) * w) / np.sum(w)))
    return out

scale = H / 1000.0
yb0, yb1 = panels[-1]
rowsum = dk[yb0:yb1 + int(8 * scale), spine_x + 5:spine_x + int(200 * scale)].sum(axis=1)
spine_y = yb0 + int(np.argmax(rowsum))
r0, r1 = spine_y + int(round(3.5 * scale)), spine_y + int(round(5.5 * scale))
prof_x = dk[r0:r1, spine_x - 2:W].sum(axis=0)
xt = groups_weighted(prof_x, spine_x - 2, 120 * (r1 - r0) / 2.0)
print("x-axis spine row", spine_y, "major x ticks:", [round(v, 2) for v in xt])
xt = xt[:4]
ax, bx = np.polyfit(np.array(xt), [-1, 0, 1, 2], 1)
print("x px/decade %.3f residuals %s" % (1 / ax, np.round(np.polyval([ax, bx], xt) - np.array([-1, 0, 1, 2]), 4)))

names = ["L4e", "L4i", "L2/3e", "L2/3i"]
out = {}
for pi, (y0, y1) in enumerate(panels):
    c0, c1 = spine_x - int(round(5 * scale)), spine_x - 1
    prof = dk[y0:y1 - 4, c0:c1].sum(axis=1)
    yt = groups_weighted(prof, y0, 150 * (c1 - c0) / 4.0)
    if len(yt) != 3:
        print(names[pi], "y ticks found:", yt)
        continue
    ay, by = np.polyfit(np.array(yt), [0, -2, -4], 1)
    res = np.polyval([ay, by], yt) - np.array([0, -2, -4])
    print("\n%s: y ticks %s px/decade %.3f residuals %s" % (names[pi], [round(v, 2) for v in yt], -1 / ay, np.round(res, 4)))
    sub = g[y0:y1 - 3, spine_x + 3:W] < 60
    lab2, m = ndimage.label(sub)
    rows = []
    for j in range(1, m + 1):
        ys, xs = np.where(lab2 == j)
        h_, w_ = int(np.ptp(ys)) + 1, int(np.ptp(xs)) + 1
        area = len(ys)
        if area < 20 * scale * scale:
            continue
        yy0, yy1 = ys.min() + y0 - 2, ys.max() + y0 + 3
        xx0, xx1 = xs.min() + spine_x + 3 - 2, xs.max() + spine_x + 3 + 3
        win = dk[yy0:yy1, xx0:xx1]
        win = np.where(win > 150, win, 0)
        Y, X = np.mgrid[yy0:yy1, xx0:xx1]
        cy = (Y * win).sum() / win.sum()
        cx = (X * win).sum() / win.sum()
        isdot = abs(h_ - w_) <= 3 * scale and 8 * scale <= h_ <= 16 * scale and area / (h_ * w_) > 0.6
        lx, ly = ax * cx + bx, ay * cy + by
        rows.append((lx, ly, isdot, h_, w_, area))
    for lx, ly, isdot, h_, w_, area in sorted(rows):
        print("  %-5s bbox %3dx%3d area %4d  log10x=%.3f  log10h=%.3f  h=%.4g" % ("dot" if isdot else "OTHER", h_, w_, area, lx, ly, 10 ** ly))
