Image Pipeline Playground
test(backend/nodes): assert first-party params affect execute output
Mohammad Rajaeibf8c0bccf1bd
diff --git a/backend/tests/helpers/__init__.py b/backend/tests/helpers/__init__.pynew file mode 100644index 0000000..7ba898d--- /dev/null+++ b/backend/tests/helpers/__init__.py@@ -0,0 +1 @@+"""Shared test helpers for backend node tests."""diff --git a/backend/tests/helpers/node_fixtures.py b/backend/tests/helpers/node_fixtures.pynew file mode 100644index 0000000..5424c38--- /dev/null+++ b/backend/tests/helpers/node_fixtures.py@@ -0,0 +1,355 @@+"""Fixtures and sampling helpers for first-party node param coverage."""++from __future__ import annotations++from typing import Any++import cv2+import numpy as np++from app.engine.registry import BaseNode, registry+from app.models.graph import NodeMetadata, ParamField, PortDirection+from app.nodes import register_builtin_nodes++SKIP_NODES: frozenset[str] = frozenset(+ {+ "load_image", # needs asset / sample path setup+ "save_image", # params affect disk write, not image port pixels+ }+)++# (node_type, param_name) — skip sweeping this param (known no-op or fragile).+SKIP_PARAMS: frozenset[tuple[str, str]] = frozenset(+ {+ ("kmeans_colors", "attempts"),+ ("blur_detect", "threshold"), # label flip needs score near threshold+ ("gaussian_blur", "sigma"), # 0 means auto-from-ksize; tiny changes noop+ ("normalize", "beta"), # unused for non-minmax; context locks minmax but beta=255 default+ ("sobel", "dx"), # covered via dy/ksize/scale; dx=0 with dy=0 collapses to default+ # Retrieval mode often draws the same outer contour set on simple blobs.+ ("find_contours", "mode"),+ }+)++# Extra params merged into baseline when sweeping any field on the node.+PARAM_CONTEXT: dict[str, dict[str, Any]] = {+ "threshold": {"method": "binary"},+ "distance_transform": {"distance": "l2"},+ "normalize": {"norm_type": "minmax"},+ "in_range": {+ "space": "bgr",+ "c0_min": 0,+ "c0_max": 120,+ "c1_min": 0,+ "c1_max": 120,+ "c2_min": 0,+ "c2_max": 120,+ },+ "find_contours": {"overlay": "blank"},+ "convex_hull": {"overlay": "blank"},+ "moments": {"overlay": "blank"},+ "connected_components": {"overlay": "blank", "mode": "boxes", "min_area": 1},+ "blob_detect": {"overlay": "blank"},+ "bounding_rect": {"overlay": "blank"},+ "approx_poly": {"overlay": "blank"},+ "sobel": {"dx": "1", "dy": "0"},+ "blur_detect": {"output": "score_card"},+ "apply_mask": {"fill": "black"},+ "erode": {"ksize": 5, "iterations": 1},+ "dilate": {"ksize": 5, "iterations": 1},+ "morphology_ex": {"op": "gradient", "ksize": 5},+}++# Prefer odd values when sampling these integer params (OpenCV odd-kernel collapse).+ODD_KSIZE_PARAMS: frozenset[tuple[str, str]] = frozenset(+ {+ ("gaussian_blur", "ksize"),+ ("median_blur", "ksize"),+ ("sobel", "ksize"),+ ("laplacian", "ksize"),+ ("erode", "ksize"),+ ("dilate", "ksize"),+ ("morphology_ex", "ksize"),+ ("adaptive_threshold", "block_size"),+ ("blur_detect", "ksize"),+ ("box_blur", "ksize"),+ }+)++# Explicit numeric/select samples when min/default/max is insufficient.+PARAM_SAMPLES: dict[tuple[str, str], list[Any]] = {+ ("rotate", "angle"): [0.0, 90.0, -45.0],+ ("crop", "x"): [0, 8, 16],+ ("crop", "y"): [0, 8, 16],+ ("crop", "width"): [16, 32, 48],+ ("crop", "height"): [16, 32, 48],+ ("blank_image", "width"): [32, 64, 128],+ ("blank_image", "height"): [32, 64, 128],+ ("gaussian_noise", "stddev"): [0.0, 10.0, 50.0],+ ("gaussian_noise", "mean"): [0.0, 20.0, -20.0],+ ("random_brightness_contrast", "brightness"): [0.0, 30.0, 80.0],+ ("random_brightness_contrast", "contrast"): [0.0, 0.2, 0.8],+ ("brightness_contrast", "alpha"): [0.5, 1.0, 2.0],+ ("brightness_contrast", "beta"): [-50.0, 0.0, 50.0],+ ("normalize", "alpha"): [0.0, 50.0],+ ("normalize", "beta"): [200.0, 255.0],+ ("threshold", "thresh"): [50.0, 127.0, 200.0],+ ("threshold", "maxval"): [128.0, 255.0],+ ("canny", "threshold1"): [10.0, 100.0, 250.0],+ ("canny", "threshold2"): [50.0, 200.0, 400.0],+ # Keep min_area <= default max_area (5000) and max_area >= default min_area (20).+ ("blob_detect", "min_area"): [1.0, 20.0, 2000.0],+ ("blob_detect", "max_area"): [100.0, 5000.0, 50000.0],+ ("annotations", "bboxes_json"): [+ '[[10, 10, 100, 80, "object"]]',+ '[[5, 5, 40, 40, "a"], [50, 50, 90, 90, "b"]]',+ ],+ ("annotations", "keypoints_json"): [+ "[[40, 40], [80, 60]]",+ "[[10, 10]]",+ "[]",+ ],+}++# Nodes that need a soft-edged / multi-tone image (not pure 0/255 blobs).+TONAL_NODES: frozenset[str] = frozenset(+ {+ "threshold",+ "adaptive_threshold",+ "canny",+ "sobel",+ "laplacian",+ "blur_detect",+ "clahe",+ "normalize",+ "draw_histogram",+ "compare_hist",+ "in_range",+ "bilateral_filter",+ "gaussian_blur",+ "median_blur",+ "box_blur",+ "sharpen",+ "brightness_contrast",+ "random_brightness_contrast",+ "gaussian_noise",+ "kmeans_colors",+ "dominant_colors_hist",+ }+)++STRUCTURE_NODES: frozenset[str] = frozenset(+ {+ "find_contours",+ "convex_hull",+ "moments",+ "connected_components",+ "blob_detect",+ "bounding_rect",+ "approx_poly",+ "distance_transform",+ "erode",+ "dilate",+ "morphology_ex",+ }+)+++def ensure_registry() -> None:+ register_builtin_nodes()+++def first_party_param_metadata() -> list[NodeMetadata]:+ ensure_registry()+ return [+ m+ for m in registry.list_metadata()+ if not m.type.startswith("albu_") and m.params+ ]+++def textured_bgr(size: int = 64) -> np.ndarray:+ """Gradient + bright rectangle — not flat zeros."""+ yy, xx = np.mgrid[0:size, 0:size]+ image = np.zeros((size, size, 3), dtype=np.uint8)+ image[:, :, 0] = (xx * 3).astype(np.uint8)+ image[:, :, 1] = (yy * 3).astype(np.uint8)+ image[:, :, 2] = ((xx + yy) * 2).astype(np.uint8)+ lo, hi = size // 4, 3 * size // 4+ image[lo:hi, lo:hi] = (40, 180, 220)+ # Extra smaller blob for multi-component structure nodes.+ image[4:14, 4:14] = (20, 220, 40)+ return image+++def textured_gray(size: int = 64) -> np.ndarray:+ return cv2.cvtColor(textured_bgr(size), cv2.COLOR_BGR2GRAY)+++def binary_blob(size: int = 64) -> np.ndarray:+ """Axis-aligned blobs that only touch at a corner (4- vs 8-connectivity differs)."""+ image = np.zeros((size, size), dtype=np.uint8)+ image[10:20, 10:20] = 255+ image[20:30, 20:30] = 255+ image[6:16, 40:50] = 255+ return image+++def rotated_blob(size: int = 64) -> np.ndarray:+ """Filled rotated ellipse so axis vs rotated bounding rects differ."""+ image = np.zeros((size, size), dtype=np.uint8)+ center = (size // 2, size // 2)+ axes = (size // 3, size // 8)+ cv2.ellipse(image, center, axes, 35, 0, 360, 255, thickness=-1)+ return image+++def soft_edges_gray(size: int = 64) -> np.ndarray:+ """Blurred blob — soft thresholds respond to thresh params."""+ image = np.zeros((size, size), dtype=np.uint8)+ lo, hi = size // 4, 3 * size // 4+ image[lo:hi, lo:hi] = 255+ image = cv2.GaussianBlur(image.astype(np.float32), (9, 9), 2.0)+ return np.clip(image, 0, 255).astype(np.uint8)+++def morph_blob(size: int = 64) -> np.ndarray:+ """Circular blob so morph kernel shapes (rect/ellipse/cross) diverge."""+ image = np.zeros((size, size), dtype=np.uint8)+ cv2.circle(image, (size // 2, size // 2), size // 3, 255, thickness=-1)+ return image+++def partial_mask(size: int = 64) -> np.ndarray:+ mask = np.zeros((size, size), dtype=np.uint8)+ mask[:, : size // 2] = 255+ return mask+++def build_inputs(meta: NodeMetadata) -> dict[str, Any]:+ """Build execute() inputs for a node's required (and useful optional) ports."""+ required = [+ p+ for p in meta.ports+ if p.direction == PortDirection.INPUT and not p.optional+ ]+ optional = [+ p for p in meta.ports if p.direction == PortDirection.INPUT and p.optional+ ]+ ids = {p.id for p in required}++ if meta.type == "blank_image":+ return {}+ if meta.type == "annotations":+ return {}+ if meta.type == "apply_mask":+ return {"image": textured_bgr(), "mask": partial_mask()}+ if ids == {"a", "b"} or ids == {"a", "b", "mask"}:+ a = textured_bgr()+ b = textured_bgr()+ b = cv2.rotate(b, cv2.ROTATE_180)+ inputs: dict[str, Any] = {"a": a, "b": b}+ if "mask" in ids or any(p.id == "mask" for p in optional):+ inputs["mask"] = None+ return inputs+ if ids == {"image_a", "image_b"}:+ a = textured_bgr()+ b = np.full_like(a, 80)+ b[16:48, 16:48] = (200, 40, 40)+ return {"image_a": a, "image_b": b}+ if "image" in ids:+ if meta.type == "bounding_rect":+ return {"image": rotated_blob()}+ if meta.type == "canny":+ return {"image": textured_gray()}+ if meta.type in {"threshold", "adaptive_threshold", "distance_transform"}:+ return {"image": soft_edges_gray()}+ if meta.type in {"erode", "dilate", "morphology_ex"}:+ return {"image": morph_blob()}+ if meta.type in STRUCTURE_NODES:+ return {"image": binary_blob()}+ if meta.type in TONAL_NODES:+ return {"image": textured_bgr()}+ return {"image": textured_bgr()}+ return {}+++def _coerce_odd(value: int) -> int:+ value = int(value)+ if value % 2 == 0:+ value += 1+ return max(1, value)+++def sample_values(node_type: str, field: ParamField) -> list[Any]:+ key = (node_type, field.name)+ if key in PARAM_SAMPLES:+ return list(PARAM_SAMPLES[key])++ if field.type == "select" and field.options:+ return list(field.options)++ if field.type in {"integer", "number"}:+ values: list[Any] = []+ for candidate in (field.minimum, field.default, field.maximum):+ if candidate is None:+ continue+ values.append(int(candidate) if field.type == "integer" else float(candidate))+ if key in ODD_KSIZE_PARAMS:+ values = [_coerce_odd(int(v)) for v in values]+ # Deduplicate while preserving order.+ seen: set[Any] = set()+ unique: list[Any] = []+ for v in values:+ if v not in seen:+ seen.add(v)+ unique.append(v)+ return unique++ # string / file — only via PARAM_SAMPLES+ return []+++def baseline_params(node: BaseNode, meta: NodeMetadata) -> dict[str, Any]:+ context = dict(PARAM_CONTEXT.get(meta.type, {}))+ return node.validate_params(context)+++def primary_output(result: dict[str, Any], meta: NodeMetadata) -> Any:+ if meta.type == "annotations":+ return (result.get("bboxes"), result.get("keypoints"))+ if "image" in result:+ return result["image"]+ for port in meta.ports:+ if port.direction == PortDirection.OUTPUT and port.id in result:+ return result[port.id]+ raise KeyError(f"No primary output for node {meta.type}: {list(result)}")+++def outputs_equal(a: Any, b: Any) -> bool:+ if isinstance(a, np.ndarray) and isinstance(b, np.ndarray):+ return a.shape == b.shape and np.array_equal(a, b)+ if isinstance(a, tuple) and isinstance(b, tuple):+ return all(outputs_equal(x, y) for x, y in zip(a, b, strict=True))+ return a == b+++def param_cases() -> list[tuple[str, str]]:+ """(node_type, param_name) pairs the harness should cover."""+ cases: list[tuple[str, str]] = []+ for meta in first_party_param_metadata():+ if meta.type in SKIP_NODES:+ continue+ node = registry.get(meta.type)+ for field in meta.params:+ if (meta.type, field.name) in SKIP_PARAMS:+ continue+ samples = sample_values(meta.type, field)+ if not samples:+ continue+ # Need at least one sample that can differ from baseline value.+ baseline = baseline_params(node, meta)+ if all(s == baseline.get(field.name) for s in samples):+ continue+ cases.append((meta.type, field.name))+ return casesdiff --git a/backend/tests/test_params_affect_output.py b/backend/tests/test_params_affect_output.pynew file mode 100644index 0000000..7015d73--- /dev/null+++ b/backend/tests/test_params_affect_output.py@@ -0,0 +1,140 @@+"""Registry-driven tests: first-party node params must affect execute() output."""++from __future__ import annotations++import cv2+import numpy as np+import pytest++from app.engine.registry import registry+from app.nodes import register_builtin_nodes+from tests.helpers.node_fixtures import (+ SKIP_NODES,+ baseline_params,+ build_inputs,+ ensure_registry,+ first_party_param_metadata,+ outputs_equal,+ param_cases,+ primary_output,+ sample_values,+ textured_gray,+)++[email protected](scope="module", autouse=True)+def _register_nodes() -> None:+ ensure_registry()++[email protected](("node_type", "param_name"), param_cases())+def test_param_affects_output(node_type: str, param_name: str) -> None:+ meta = next(m for m in registry.list_metadata() if m.type == node_type)+ node = registry.get(node_type)+ field = next(f for f in meta.params if f.name == param_name)+ inputs = build_inputs(meta)+ base_params = baseline_params(node, meta)+ baseline = primary_output(node.execute(inputs, base_params, seed=0), meta)++ differed = False+ last_error: Exception | None = None+ for value in sample_values(node_type, field):+ if value == base_params.get(param_name):+ continue+ mutated = dict(base_params)+ mutated[param_name] = value+ try:+ params = node.validate_params(mutated)+ result = primary_output(node.execute(inputs, params, seed=0), meta)+ except Exception as exc: # noqa: BLE001 — collect and continue other samples+ last_error = exc+ continue+ if not outputs_equal(baseline, result):+ differed = True+ break++ if not differed and last_error is not None:+ raise AssertionError(+ f"{node_type}.{param_name}: no differing sample; last error: {last_error}"+ ) from last_error+ assert differed, (+ f"{node_type}.{param_name}: changing among {sample_values(node_type, field)!r} "+ f"did not change output vs baseline {base_params.get(param_name)!r}"+ )+++def test_every_first_party_param_node_is_accounted_for() -> None:+ covered_nodes = {node_type for node_type, _ in param_cases()}+ missing: list[str] = []+ for meta in first_party_param_metadata():+ if meta.type in SKIP_NODES:+ continue+ if meta.type in covered_nodes:+ continue+ # Node must either generate cases or be explicitly skipped.+ missing.append(meta.type)+ assert not missing, (+ "First-party param nodes with no harness coverage and not in SKIP_NODES: "+ f"{missing}"+ )+++def test_threshold_binary_inv_matches_opencv() -> None:+ register_builtin_nodes()+ image = textured_gray(32)+ thresh, maxval = 127.0, 255.0+ node = registry.get("threshold")+ binary = node.execute(+ {"image": image},+ node.validate_params({"method": "binary", "thresh": thresh, "maxval": maxval}),+ seed=0,+ )["image"]+ binary_inv = node.execute(+ {"image": image},+ node.validate_params(+ {"method": "binary_inv", "thresh": thresh, "maxval": maxval}+ ),+ seed=0,+ )["image"]+ _, expected_inv = cv2.threshold(image, thresh, maxval, cv2.THRESH_BINARY_INV)+ assert not np.array_equal(binary, binary_inv)+ assert np.array_equal(binary_inv, expected_inv)+++ "method",+ [+ "binary",+ "binary_inv",+ "trunc",+ "tozero",+ "tozero_inv",+ "otsu",+ "otsu_inv",+ "triangle",+ "triangle_inv",+ ],+)+def test_threshold_method_matches_opencv_flag(method: str) -> None:+ register_builtin_nodes()+ image = textured_gray(48)+ params = registry.get("threshold").validate_params(+ {"method": method, "thresh": 100.0, "maxval": 255.0}+ )+ result = registry.get("threshold").execute({"image": image}, params, seed=0)["image"]++ flags: dict[str, int] = {+ "binary": cv2.THRESH_BINARY,+ "binary_inv": cv2.THRESH_BINARY_INV,+ "trunc": cv2.THRESH_TRUNC,+ "tozero": cv2.THRESH_TOZERO,+ "tozero_inv": cv2.THRESH_TOZERO_INV,+ "otsu": cv2.THRESH_BINARY | cv2.THRESH_OTSU,+ "otsu_inv": cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU,+ "triangle": cv2.THRESH_BINARY | cv2.THRESH_TRIANGLE,+ "triangle_inv": cv2.THRESH_BINARY_INV | cv2.THRESH_TRIANGLE,+ }+ _, expected = cv2.threshold(+ image, float(params["thresh"]), float(params["maxval"]), flags[method]+ )+ assert np.array_equal(result, expected)