Files
reTraining/scripts/verify_m1_diagrams.py
T

470 lines
19 KiB
Python

"""
Comprehensive Empirical Verification and Stress-Testing Suite for Milestone 1 Diagram Assets
Author: Challenger 1 (Milestone 1 - Diagram Asset Challenger)
Target Assets:
- docs/diagram-alur.fodg (LibreOffice Draw OASIS OpenDocument 1.3 XML)
- docs/diagram-alur.svg (Vector SVG diagram)
- docs/diagram-alur.png (4K UHD raster render)
- scripts/export_diagram_png.py (Exporter pipeline)
"""
import sys
import re
import math
import hashlib
import time
from pathlib import Path
import xml.etree.ElementTree as ET
from PIL import Image
import numpy as np
PROJECT_ROOT = Path(__file__).resolve().parent.parent
DOCS_DIR = PROJECT_ROOT / "docs"
FODG_PATH = DOCS_DIR / "diagram-alur.fodg"
SVG_PATH = DOCS_DIR / "diagram-alur.svg"
PNG_PATH = DOCS_DIR / "diagram-alur.png"
EXPORTER_PATH = PROJECT_ROOT / "scripts" / "export_diagram_png.py"
NAMESPACES = {
"office": "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
"draw": "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
"style": "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
"text": "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"svg": "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
"fo": "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
"table": "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
"xlink": "http://www.w3.org/1999/xlink",
}
def parse_dim_cm(val_str: str) -> float:
if not val_str:
return 0.0
val_str = val_str.strip()
if val_str.endswith("cm"):
return float(val_str[:-2])
elif val_str.endswith("mm"):
return float(val_str[:-2]) / 10.0
elif val_str.endswith("in"):
return float(val_str[:-2]) * 2.54
elif val_str.endswith("pt"):
return float(val_str[:-2]) * 2.54 / 72.0
elif val_str.endswith("px"):
return float(val_str[:-2]) * 2.54 / 96.0
try:
return float(val_str)
except ValueError:
return 0.0
def test_fodg_integrity():
print("\n" + "="*60)
print(">>> 1. FODG EMPIRICAL INTEGRITY & SCHEMA AUDIT")
print("="*60)
assert FODG_PATH.exists(), f"FODG file missing: {FODG_PATH}"
fodg_size = FODG_PATH.stat().st_size
print(f"FODG file size: {fodg_size} bytes")
assert fodg_size > 1000, f"FODG file unusually small: {fodg_size} bytes"
# XML Parse
raw_content = FODG_PATH.read_text(encoding="utf-8")
root = ET.fromstring(raw_content)
print(f"FODG Root tag: {root.tag}")
assert root.tag == f"{{{NAMESPACES['office']}}}document", f"Root tag mismatch: {root.tag}"
# Attribute checks
version = root.get(f"{{{NAMESPACES['office']}}}version")
mimetype = root.get(f"{{{NAMESPACES['office']}}}mimetype")
print(f"office:version: {version}")
print(f"office:mimetype: {mimetype}")
assert version == "1.3", f"Expected ODF version 1.3, got {version}"
assert mimetype == "application/vnd.oasis.opendocument.graphics", f"Invalid mimetype: {mimetype}"
# Automatic styles and page layout
page_layouts = root.findall(f".//{{{NAMESPACES['style']}}}page-layout")
print(f"Page layouts found: {len(page_layouts)}")
assert len(page_layouts) >= 1, "Missing style:page-layout in FODG"
page_prop = root.find(f".//{{{NAMESPACES['style']}}}page-layout-properties")
assert page_prop is not None, "Missing style:page-layout-properties"
page_w = page_prop.get(f"{{{NAMESPACES['fo']}}}page-width")
page_h = page_prop.get(f"{{{NAMESPACES['fo']}}}page-height")
orientation = page_prop.get(f"{{{NAMESPACES['style']}}}print-orientation")
print(f"Page dimensions: width={page_w}, height={page_h}, orientation={orientation}")
page_w_cm = parse_dim_cm(page_w)
page_h_cm = parse_dim_cm(page_h)
assert page_w_cm > 0 and page_h_cm > 0, "Invalid page dimensions in FODG"
assert page_w_cm > page_h_cm, f"Expected landscape orientation (w > h), got w={page_w_cm}, h={page_h_cm}"
# Shapes and Connectors
draw_page = root.find(f".//{{{NAMESPACES['draw']}}}page")
assert draw_page is not None, "Missing draw:page in FODG"
custom_shapes = root.findall(f".//{{{NAMESPACES['draw']}}}custom-shape")
connectors = root.findall(f".//{{{NAMESPACES['draw']}}}connector")
styles = root.findall(f".//{{{NAMESPACES['style']}}}style")
print(f"Total draw:custom-shape elements: {len(custom_shapes)}")
print(f"Total draw:connector elements: {len(connectors)}")
print(f"Total style:style elements: {len(styles)}")
assert len(custom_shapes) >= 8, f"Expected at least 8 custom shapes (for 8 stages), got {len(custom_shapes)}"
assert len(connectors) >= 7, f"Expected at least 7 connectors between stages, got {len(connectors)}"
# Shape coordinate and bounding box bounds check
shapes_info = []
for idx, shape in enumerate(custom_shapes):
s_id = shape.get(f"{{{NAMESPACES['draw']}}}id") or shape.get(f"{{{NAMESPACES['draw']}}}name") or f"shape_{idx}"
x_str = shape.get(f"{{{NAMESPACES['svg']}}}x")
y_str = shape.get(f"{{{NAMESPACES['svg']}}}y")
w_str = shape.get(f"{{{NAMESPACES['svg']}}}width")
h_str = shape.get(f"{{{NAMESPACES['svg']}}}height")
x = parse_dim_cm(x_str)
y = parse_dim_cm(y_str)
w = parse_dim_cm(w_str)
h = parse_dim_cm(h_str)
# Verify shape sits inside the page bounds (with 1.5cm safety margin)
assert x >= 0, f"Shape {s_id} x coordinate < 0: {x}"
assert y >= 0, f"Shape {s_id} y coordinate < 0: {y}"
assert x + w <= page_w_cm + 1.0, f"Shape {s_id} exceeds page width: {x}+{w} > {page_w_cm}"
assert y + h <= page_h_cm + 1.0, f"Shape {s_id} exceeds page height: {y}+{h} > {page_h_cm}"
shapes_info.append({"id": s_id, "x": x, "y": y, "w": w, "h": h})
print(f"All {len(shapes_info)} shapes pass page boundary checks!")
# Check stage text extraction
all_text = " ".join([elem.text for elem in root.iter() if elem.text])
print(f"Total text characters in FODG: {len(all_text)}")
required_keywords = [
"Video Archive",
"FFmpeg",
"SAM3",
"Review",
"Data Prep",
"Master Dataset",
"YOLO",
"Live Counter",
]
for kw in required_keywords:
assert kw.lower() in all_text.lower(), f"Missing required keyword '{kw}' in FODG text content"
print(f" [OK] FODG text includes keyword: '{kw}'")
print("[PASS] FODG Schema, Namespace, Geometry, and Text Integrity Verified 100%")
return True
def test_svg_integrity():
print("\n" + "="*60)
print(">>> 2. SVG EMPIRICAL INTEGRITY & VECTOR AUDIT")
print("="*60)
assert SVG_PATH.exists(), f"SVG file missing: {SVG_PATH}"
svg_size = SVG_PATH.stat().st_size
print(f"SVG file size: {svg_size} bytes")
assert svg_size > 5000, f"SVG file too small: {svg_size} bytes"
raw_svg = SVG_PATH.read_text(encoding="utf-8")
root = ET.fromstring(raw_svg)
# Strip namespace for clean tag matching
ns = "{http://www.w3.org/2000/svg}"
assert root.tag == f"{ns}svg" or root.tag == "svg", f"Root is not svg: {root.tag}"
viewBox = root.get("viewBox")
print(f"SVG viewBox: {viewBox}")
assert viewBox == "0 0 1920 1080", f"Expected viewBox '0 0 1920 1080', got '{viewBox}'"
# Extract all def IDs
defs = root.find(f".//{ns}defs")
assert defs is not None, "Missing <defs> section in SVG"
def_ids = set()
for child in defs.iter():
i = child.get("id")
if i:
def_ids.add(i)
print(f"Total <defs> element IDs declared: {len(def_ids)} ({sorted(list(def_ids))})")
assert len(def_ids) >= 10, f"Expected >= 10 def definitions (gradients, markers, filters), got {len(def_ids)}"
# Audit all url(#id) references in SVG
url_pattern = re.compile(r'url\(#([^)]+)\)')
all_refs = url_pattern.findall(raw_svg)
print(f"Total url(#...) references in SVG: {len(all_refs)}")
missing_refs = set(all_refs) - def_ids
print(f"Dangling/Missing url(#...) references: {missing_refs}")
assert len(missing_refs) == 0, f"SVG contains broken url references: {missing_refs}"
# Check Stage groups
for stage_num in range(1, 9):
stage_id = f"stage-{stage_num}"
stage_elem = root.find(f".//*[@id='{stage_id}']")
assert stage_elem is not None, f"Missing stage group with id='{stage_id}' in SVG"
print(f" [OK] SVG Group found: #{stage_id}")
# Check storage dock
storage_dock = root.find(".//*[@id='storage-dock']")
assert storage_dock is not None, "Missing #storage-dock in SVG"
print(" [OK] SVG Group found: #storage-dock")
# Check connectors
connectors_group = root.find(".//*[@id='flow-connectors']")
assert connectors_group is not None, "Missing #flow-connectors group in SVG"
print(" [OK] SVG Group found: #flow-connectors")
# Check connector path count
connector_paths = connectors_group.findall(f".//{ns}path") + connectors_group.findall("path")
print(f"Total connector paths in SVG: {len(connector_paths)}")
assert len(connector_paths) == 8, f"Expected 8 connector paths, got {len(connector_paths)}"
# Text extraction and verification
text_elements = root.findall(f".//{ns}text")
print(f"Total <text> elements in SVG: {len(text_elements)}")
assert len(text_elements) >= 30, f"Expected >= 30 text elements, got {len(text_elements)}"
all_svg_text = " ".join([t.text for t in root.iter() if t.text])
print(f"Total text characters in SVG: {len(all_svg_text)}")
pipeline_checks = [
("Video Archive & CCTV", "Stage 1 Ingest"),
("FFmpeg Frame Extraction", "Stage 2 Slicing"),
("SAM3 Grounding Engine", "Stage 3 SAM3"),
("Review & Edit Canvas", "Stage 4 Canvas"),
("Data Prep & Triage", "Stage 5 Data Prep"),
("Master Dataset Freezing", "Stage 6 Freeze"),
("YOLO Retraining Engine", "Stage 7 YOLO"),
("Live Counter & Inference", "Stage 8 Live Counter"),
("app.db", "Storage SQLite"),
("06:00", "Working Day Cycle"),
("WHEP", "Streaming Protocol"),
("ByteTrack", "Tracker"),
("mAP", "Evaluation Metric")
]
for term, label in pipeline_checks:
assert term.lower() in all_svg_text.lower(), f"Missing required term '{term}' ({label}) in SVG text"
print(f" [OK] Verified SVG semantic label: '{term}'")
print("[PASS] SVG Structure, Defs Integrity, Groups, and Text Coverage Verified 100%")
return True
def test_png_integrity():
print("\n" + "="*60)
print(">>> 3. PNG EMPIRICAL RASTER INTEGRITY & VISUAL AUDIT")
print("="*60)
assert PNG_PATH.exists(), f"PNG file missing: {PNG_PATH}"
png_size = PNG_PATH.stat().st_size
print(f"PNG file size: {png_size} bytes ({png_size/1024:.1f} KB)")
# Check requirement size > 1MB
assert png_size > 1_000_000, f"PNG file size must be > 1MB, got {png_size} bytes ({png_size/1024:.1f} KB)"
print(f" [OK] PNG size > 1MB: {png_size} bytes > 1,000,000 bytes")
with Image.open(PNG_PATH) as img:
w, h = img.size
mode = img.mode
fmt = img.format
print(f"Image Format: {fmt}")
print(f"Image Dimensions: {w}x{h} px")
print(f"Image Color Mode: {mode}")
assert fmt == "PNG", f"Expected PNG format, got {fmt}"
assert (w, h) == (3840, 2160), f"Expected dimensions (3840, 2160), got ({w}, {h})"
assert mode == "RGB", f"Expected color mode RGB, got {mode}"
# Statistical analysis of pixel data
arr = np.array(img)
assert arr.shape == (2160, 3840, 3), f"Unexpected array shape: {arr.shape}"
mean_r, mean_g, mean_b = arr.mean(axis=(0, 1))
std_r, std_g, std_b = arr.std(axis=(0, 1))
min_r, min_g, min_b = arr.min(axis=(0, 1))
max_r, max_g, max_b = arr.max(axis=(0, 1))
print(f"Channel Means (R,G,B): ({mean_r:.2f}, {mean_g:.2f}, {mean_b:.2f})")
print(f"Channel StdDev (R,G,B): ({std_r:.2f}, {std_g:.2f}, {std_b:.2f})")
print(f"Channel Min (R,G,B): ({min_r}, {min_g}, {min_b})")
print(f"Channel Max (R,G,B): ({max_r}, {max_g}, {max_b})")
# Check for vibrant contrast (dark background ~8-15, highlights > 200)
assert min_r <= 20 and min_g <= 20 and min_b <= 25, "Background not sufficiently dark"
assert max_r >= 220 and max_g >= 220 and max_b >= 220, "Foreground highlights missing or washed out"
assert std_r > 15 and std_g > 15 and std_b > 20, "Image has insufficient visual variance / flat render"
# Unique colors check across full pixel space
flat_pixels = arr.reshape(-1, 3)
unique_colors = len(np.unique(flat_pixels, axis=0))
print(f"Full unique colors: {unique_colors:,}")
assert unique_colors > 10000, f"Expected > 10,000 unique colors (anti-aliasing & gradients), got {unique_colors}"
print("[PASS] PNG Dimensions (3840x2160), Mode (RGB), Size (>1MB), and Pixel Quality Verified 100%")
return True
def test_anti_ai_and_language_qc():
print("\n" + "="*60)
print(">>> 4. ANTI-AI & TECHNICAL LANGUAGE QC AUDIT")
print("="*60)
# Extract all visible text nodes from FODG and SVG
fodg_root = ET.fromstring(FODG_PATH.read_text(encoding="utf-8"))
svg_root = ET.fromstring(SVG_PATH.read_text(encoding="utf-8"))
fodg_texts = " \n ".join([t.text for t in fodg_root.iter() if t.text and t.text.strip()])
svg_texts = " \n ".join([t.text for t in svg_root.iter() if t.text and t.text.strip()])
forbidden_patterns = [
r"--", # Double hyphens
r"delve", # AI cliche
r"crucial", # AI cliche
r"testament", # AI cliche
r"beacon", # AI cliche
r"furthermore", # AI filler
r"in conclusion", # AI filler
r"it is important", # AI filler
]
for p in forbidden_patterns:
match_fodg = re.findall(p, fodg_texts, re.IGNORECASE)
match_svg = re.findall(p, svg_texts, re.IGNORECASE)
assert len(match_fodg) == 0, f"Forbidden AI marker '{p}' found in FODG text: {match_fodg}"
assert len(match_svg) == 0, f"Forbidden AI marker '{p}' found in SVG text: {match_svg}"
print(f" [OK] Clean of AI marker pattern: '{p}'")
print("[PASS] Anti-AI Phrasing and Text Cleanliness Verified 100%")
return True
def test_exporter_reproducibility():
print("\n" + "="*60)
print(">>> 5. EXPORTER SCRIPT REPRODUCIBILITY & DETERMINISM")
print("="*60)
assert EXPORTER_PATH.exists(), f"Exporter script missing: {EXPORTER_PATH}"
# Read current PNG hash
initial_hash = hashlib.sha256(PNG_PATH.read_bytes()).hexdigest()
print(f"Initial PNG SHA-256: {initial_hash}")
# Import and test exporter
import importlib.util
spec = importlib.util.spec_from_file_location("exporter", EXPORTER_PATH)
exporter = importlib.util.module_from_spec(spec)
spec.loader.exec_module(exporter)
t0 = time.perf_counter()
import asyncio
asyncio.run(exporter.export_svg_to_png(SVG_PATH, PNG_PATH, width=1920, height=1080, scale=2.0))
elapsed = time.perf_counter() - t0
print(f"Exporter execution time: {elapsed:.2f}s")
new_hash = hashlib.sha256(PNG_PATH.read_bytes()).hexdigest()
print(f"Re-exported PNG SHA-256: {new_hash}")
assert initial_hash == new_hash, f"Hash mismatch after re-export! {initial_hash} vs {new_hash}"
print("[PASS] Exporter Script is 100% Deterministic and Reproducible")
return True
def test_multiscale_and_clipping():
print("\n" + "="*60)
print(">>> 6. MULTI-SCALE RENDERING & DOM CLIPPING AUDIT")
print("="*60)
import asyncio
from playwright.async_api import async_playwright
import io
async def _async_audit():
svg_content = SVG_PATH.read_text(encoding="utf-8")
scales = [1.0, 2.0, 3.0]
async with async_playwright() as p:
browser = await p.chromium.launch()
# 1. Multi-scale test
for scale in scales:
page = await browser.new_page(
viewport={"width": 1920, "height": 1080},
device_scale_factor=scale
)
html_wrapper = f"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
* {{ box-sizing: border-box; margin: 0; padding: 0; }}
html, body {{ width: 100vw; height: 100vh; overflow: hidden; background-color: #080c14; }}
svg {{ display: block; width: 100%; height: 100%; }}
</style>
</head>
<body>{svg_content}</body>
</html>"""
await page.set_content(html_wrapper)
await page.wait_for_timeout(300)
png_bytes = await page.screenshot(type="png")
with Image.open(io.BytesIO(png_bytes)) as img:
w, h = img.size
exp_w, exp_h = int(1920 * scale), int(1080 * scale)
assert (w, h) == (exp_w, exp_h), f"Scale {scale} failed: {w}x{h} != {exp_w}x{exp_h}"
print(f" [OK] Scale {scale:.1f}x -> {w}x{h} px ({len(png_bytes)/1024:.1f} KB)")
await page.close()
# 2. Text clipping / bounding box check
page = await browser.new_page(
viewport={"width": 1920, "height": 1080},
device_scale_factor=2.0
)
await page.set_content(html_wrapper)
await page.wait_for_timeout(300)
issues = await page.evaluate("""() => {
const issues = [];
const svg = document.querySelector('svg');
const svgRect = svg.getBoundingClientRect();
const textNodes = document.querySelectorAll('text');
textNodes.forEach(t => {
const rect = t.getBoundingClientRect();
if (rect.left < svgRect.left - 2 || rect.right > svgRect.right + 2 ||
rect.top < svgRect.top - 2 || rect.bottom > svgRect.bottom + 2) {
issues.push({
type: 'CANVAS_OVERFLOW',
text: t.textContent.trim()
});
}
});
return { totalTextElements: textNodes.length, issues: issues };
}""")
await page.close()
await browser.close()
print(f"Total Text Nodes inspected for clipping: {issues['totalTextElements']}")
assert len(issues['issues']) == 0, f"Detected text clipping issues: {issues['issues']}"
print(" [OK] Zero text elements exceed SVG canvas boundaries!")
asyncio.run(_async_audit())
print("[PASS] Multi-Scale & Layout Clipping Audit Passed 100%")
return True
def main():
print("="*70)
print("CHALLENGER 1 (MILESTONE 1) — EMPIRICAL VERIFICATION HARNESS")
print("="*70)
t_start = time.perf_counter()
test_fodg_integrity()
test_svg_integrity()
test_png_integrity()
test_anti_ai_and_language_qc()
test_exporter_reproducibility()
test_multiscale_and_clipping()
t_total = time.perf_counter() - t_start
print("\n" + "="*70)
print(f"ALL EMPIRICAL TESTS PASSED SUCCESSFULLY IN {t_total:.2f}s (100% PASS RATE)")
print("="*70)
if __name__ == "__main__":
main()