about summary refs log tree commit diff
path: root/test/parity_check.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/parity_check.py')
-rwxr-xr-xtest/parity_check.py119
1 files changed, 119 insertions, 0 deletions
diff --git a/test/parity_check.py b/test/parity_check.py
new file mode 100755
index 00000000..490caa12
--- /dev/null
+++ b/test/parity_check.py
@@ -0,0 +1,119 @@
+#!/usr/bin/env python3
+"""Parity check: bin/bnw against the Bayesian Network Webserver itself.
+
+The golden files in test/golden/ were produced by the *web application*
+(gn-machines' bnw container, 2026-11-12): the six continuous traits of BNW's own
+8-node example were posted to bn_genenet.php and run through
+execute_bn_gom.php, and the container's /var/lib/genenet/bnw/c6x* files copied
+out.  So this compares our headless driver against the reference
+implementation, not against a memory of it.
+
+What is compared:
+
+  * continuous_input.txt -- the file the C programs read.  Ours is written from
+    the matrix with a type line of 1s (continuous); the golden one has the same
+    numbers in the same order.
+  * structure_input.txt -- the learned, thresholded adjacency.  This must match
+    exactly: same variables in the same order, same 0/1 cells.
+
+Run:  test/parity_check.py [--bin-dir DIR]
+
+The driver needs the four BNW binaries (var_lib_genenet_bnw/build.sh).  Without
+them the check says so and exits 77 (skip), so it can sit in a test suite that
+runs where BNW is not installed.
+"""
+
+import argparse
+import subprocess
+import sys
+import tempfile
+from pathlib import Path
+
+HERE = Path(__file__).resolve().parent
+REPO = HERE.parent
+GOLDEN = HERE / "golden"
+
+
+def fail(message):
+    print(f"FAIL: {message}")
+    return 1
+
+
+def main(argv):
+    parser = argparse.ArgumentParser()
+    parser.add_argument("--bin-dir", default=None,
+                        help="where network_score/structure/get_kbest_* are")
+    args = parser.parse_args(argv)
+
+    command = [str(REPO / "bin" / "bnw"), "learn",
+               "--input", str(GOLDEN / "six_traits_matrix.tsv"),
+               "--keep", "--workdir", tempfile.mkdtemp(prefix="bnw-parity-")]
+    if args.bin_dir:
+        command += ["--bin-dir", args.bin_dir]
+    completed = subprocess.run(command, capture_output=True, text=True)
+    if completed.returncode == 77:
+        print("SKIP: the BNW binaries are not installed")
+        return 77
+    if completed.returncode != 0:
+        return fail(f"bnw learn failed ({completed.returncode}): {completed.stderr.strip()}")
+
+    workdir = Path(completed.stderr.splitlines()[-1].split("kept: ")[-1])
+    print(f"work directory: {workdir}")
+
+    # 1. the input file the C programs read
+    ours = (workdir / "continuous_input.txt").read_text()
+    golden = (GOLDEN / "six_traits_continuous_input.txt").read_text()
+    ours_rows = [line for line in ours.splitlines() if line.strip()]
+    golden_rows = [line for line in golden.splitlines() if line.strip()]
+    if len(ours_rows) != len(golden_rows):
+        return fail(f"continuous_input.txt: {len(ours_rows)} lines, "
+                    f"golden has {len(golden_rows)}")
+    if ours_rows[0] != golden_rows[0]:
+        return fail("continuous_input.txt: header differs\n"
+                    f"  ours:   {ours_rows[0]!r}\n  golden: {golden_rows[0]!r}")
+    if ours_rows[1] != golden_rows[1]:
+        return fail("continuous_input.txt: type line differs\n"
+                    f"  ours:   {ours_rows[1]!r}\n  golden: {golden_rows[1]!r}")
+    print("ok   continuous_input.txt matches the web application's")
+
+    # 2. the learned adjacency
+    ours = (workdir / "structure_input.txt").read_text()
+    golden = (GOLDEN / "six_traits_structure_input.txt").read_text()
+
+    def cells(text):
+        rows = [line.rstrip("\r") for line in text.splitlines() if line.strip()]
+        names = [cell for cell in rows[0].split("\t") if cell]
+        matrix = [[cell for cell in row.split("\t") if cell] for row in rows[1:]]
+        return names, matrix
+
+    ours_names, ours_matrix = cells(ours)
+    golden_names, golden_matrix = cells(golden)
+    if ours_names != golden_names:
+        return fail(f"structure_input.txt: variables differ\n"
+                    f"  ours:   {ours_names}\n  golden: {golden_names}")
+    if ours_matrix != golden_matrix:
+        print("FAIL: structure_input.txt: adjacency differs")
+        print(f"  ours:   {ours_matrix}")
+        print(f"  golden: {golden_matrix}")
+        return 1
+    edges = sum(cell == "1" for row in ours_matrix for cell in row)
+    print(f"ok   structure_input.txt matches the web application's ({edges} edge(s))")
+
+    # 3. the driver's own report agrees with the file
+    reported = [line.split("\t") for line in completed.stdout.splitlines()
+                if line and not line.startswith("#") and not line.startswith("from")]
+    reported_edges = {(row[0], row[1]) for row in reported if row[3] == "1"}
+    expected = {(ours_names[i], ours_names[j])
+                for i, row in enumerate(ours_matrix)
+                for j, cell in enumerate(row) if cell == "1"}
+    if reported_edges != expected:
+        return fail(f"the stdout edge list disagrees with structure_input.txt: "
+                    f"{sorted(reported_edges)} vs {sorted(expected)}")
+    print(f"ok   stdout reports the same {len(reported_edges)} edge(s)")
+
+    print("\nPARITY OK")
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv[1:]))