BioSight/python/equation_dna_encoder.py
allaun e0d0f2f7fd feat(test): implement verified test units matching Lean oracle witnesses
Added fixtures.json and test_verified_units.py. Verifies character classification
classes, consistency rule maps under syntax errors, and Q16_16 parabola conjugate
slopes exact outputs (large: 158217, small: -27147) and tolerance bounds matching
Lean witnesses.

Build: 0 failures (python3 equation_dna_encoder.py --verify)
2026-06-27 23:20:23 -05:00

122 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""
equation_dna_encoder.py — SilverSight Φ Encoder CLI
Thin CLI wrapper around the phi package modules. Reads one or more
equations from argv or stdin, applies the Φ encoding pipeline
(F → DNA → consistency), and prints the result as human-readable
key-value lines plus the FASTQ entry.
Usage:
echo 'd_F(p,q) = 2*arccos(sum(sqrt(p_i*q_i)))' | python equation_dna_encoder.py
python equation_dna_encoder.py 'E(x) = x^T Q x'
python equation_dna_encoder.py --verify # run verification suite
"""
from __future__ import annotations
import sys
from phi import encode_phi, to_fastq
def main():
"""Encode equation(s) from argv or stdin and print Φ-encoded result.
When called without arguments, reads equations line-by-line from
stdin. When called with arguments, the first ``len(sys.argv) - 1``
arguments are joined as a single equation.
Each equation is passed to ``phi.encode_phi``; the resulting dict
is printed as labelled key-value lines followed by the FASTQ entry.
"""
if len(sys.argv) > 1:
equations = [" ".join(sys.argv[1:])]
else:
equations = [line.strip() for line in sys.stdin if line.strip()]
for eq in equations:
result = encode_phi(eq)
if result is None:
print(f"FAIL: could not parse: {eq!r}", file=sys.stderr)
continue
print(f"# Equation: {result['equation']}")
print(f"# DNA: {result['dna_sequence']} ({result['length']} bases)")
print(f"# PASS: {result['consistency_pass']}")
print(f"# Hash: {result['sha256_prefix']}")
print(f"# F: {result['F']}")
print(f"# τ: {result['tau']}")
print(f"# δ: {result['delta']}")
print(f"# FASTQ: {to_fastq(result).rstrip()}")
print()
if __name__ == "__main__":
if "--verify" in sys.argv:
# ── Verification (python3 equation_dna_encoder.py --verify) ────
import subprocess
import os
fail = 0
# 1. Import from phi
try:
from phi import encode_phi, to_fastq, verify_pipeline_receipt
print(" ✓ phi imports OK")
except ImportError as e:
print(f"FAIL: phi import failed: {e}")
fail += 1
# 1b. SilverSight Formalization Verification
print(" Running SilverSight Formal Verification Checks...")
try:
ss_results = verify_pipeline_receipt()
for check, passed in ss_results.items():
if passed:
print(f"{check} OK")
else:
print(f" FAIL: {check} failed")
fail += 1
except Exception as e:
print(f"FAIL: SilverSight verification failed: {e}")
fail += 1
# 1c. Verified Test Units
try:
from phi.test_verified_units import run_verified_tests
if not run_verified_tests():
fail += 1
except Exception as e:
print(f"FAIL: Verified Test Units failed to run: {e}")
fail += 1
# 2. encode_phi
result = encode_phi("E(x) = x^2")
if result is None:
fail += 1; print("FAIL: encode_phi returned None")
else:
print(f" ✓ encode_phi → {result['length']} bases, "
f"PASS={result['consistency_pass']}")
assert "dna_sequence" in result, "result missing dna_sequence"
assert "F" in result, "result missing F"
assert "consistency" in result, "result missing consistency"
# 3. CLI mode with subprocess
cp = subprocess.run(
[sys.executable, __file__, "a = b"],
capture_output=True, text=True, timeout=10,
)
if cp.returncode != 0:
fail += 1
print(f"FAIL: CLI subprocess exited {cp.returncode}: {cp.stderr}")
elif "FASTQ:" not in cp.stdout:
fail += 1
print(f"FAIL: CLI output missing FASTQ:\n{cp.stdout}")
else:
print(" ✓ CLI subprocess output includes FASTQ")
print(f"\nVerdict: {fail} failure(s)")
sys.exit(fail)
else:
main()