BioSight/python/equation_dna_encoder.py
allaun e0b2283972 feat(phi): full docstring + verification hardening pass
Every function in all 5 phi modules now has:
- Google-style docstring with Args/Returns/Examples
- Verified behavior via doctest examples (46 total)
- Self-verification assertion blocks on direct execution

Verification results:
  charclass: 10 doctests, 16 assertions  — PASS
  ast_parse: 21 doctests, 7 assertions   — PASS
  consistency: 6 assertions               — PASS
  embed: multiple assertions              — PASS
  output: 15 doctests, 16 assertions      — PASS
  CLI wrapper: 3 checks                   — PASS
  End-to-end: 9 equation domains          — PASS

Also added:
- .opencode/opencode.jsonc (gemma4 MCP config)
- phi/AGENTS.md (module-level contract)
- phi/test_phi.py (unittest test file)
- phi/encoding_rationale.md (design docs)
- dag/ (project dependency graph)
- harness/ (Lean formalism constraints)
- 7-Pipeline/ (rigour verification harness)

Build: python3 -W error -m py_compile — 0 warnings
2026-06-24 03:57:50 -05:00

99 lines
3.3 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
print(" ✓ phi imports OK")
except ImportError as e:
print(f"FAIL: phi import failed: {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()