"""Direct PlotQA observation comparisons, without digest validation. These are the production drawing primitives. They are not an independent renderer; independence applies to the two semantic executors only. """ from __future__ import annotations import copy import io from pathlib import Path _FONT_READY = False def pixels(blob): import numpy as np from PIL import Image return np.asarray(Image.open(io.BytesIO(blob)).convert("RGBA")) def render_direct(world, manifest): global _FONT_READY import matplotlib matplotlib.use("Agg") from matplotlib import font_manager from explicit_learning.renderers import plot if not _FONT_READY: root = Path(matplotlib.get_data_path()) / "fonts" / "ttf" for name in ("DejaVuSans.ttf", "DejaVuSerif.ttf"): font_manager.fontManager.addfont(root / name) _FONT_READY = True family = plot.PLOT_FAMILIES[manifest["renderer_id"]] series = plot._parse_series(world) domain = plot._build_domain(world, series) canvas = plot._Canvas(manifest["width"], manifest["height"], family) visible = plot._draw_plot(canvas, world, series, domain, family) image, owners, counts = canvas.finish() glyphs = [ {"node_id": g.node_id, "text": g.text, "bbox": [g.x0, g.y0, g.x1, g.y1]} for g in sorted(canvas.glyphs, key=lambda x: (x.node_id, x.text, x.x0, x.y0)) ] return { "image": image, "owners": owners, "glyphs": glyphs, "node_table": ["__background__", *canvas.owner_ids], "coverage": {n: counts.get(n, 0) for n in sorted(visible)}, "domain": domain.manifest_record(), } def visible_projection(world): world = copy.deepcopy(world) hidden = {n for n, flag in world.get("_node_visibility", {}).items() if flag == "hidden"} for series in world.get("series", []): for point in series.get("points", []): if point["id"] in hidden: point["y"] = None return world