about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.gitignore6
-rw-r--r--README.md50
-rwxr-xr-xbin/bnw444
-rw-r--r--info_files/gn4-integration-plan.md24
-rw-r--r--test/golden/six_traits_continuous_input.txt202
-rw-r--r--test/golden/six_traits_matrix.tsv201
-rw-r--r--test/golden/six_traits_structure_input.txt7
-rwxr-xr-xtest/parity_check.py119
-rwxr-xr-xvar_lib_genenet_bnw/build.sh32
-rw-r--r--var_lib_genenet_bnw/k-best/src/get_kbest_nets.cc47
10 files changed, 1095 insertions, 37 deletions
diff --git a/.gitignore b/.gitignore
index 8eebbc1e..968491f7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,9 @@ __pycache__/
 ai-memory.sqlite
 ai-memory.sqlite-wal
 ai-memory.sqlite-shm
+
+# build output of var_lib_genenet_bnw/build.sh
+var_lib_genenet_bnw/network_score
+var_lib_genenet_bnw/structure
+var_lib_genenet_bnw/k-best/src/get_kbest_parents
+var_lib_genenet_bnw/k-best/src/get_kbest_nets
diff --git a/README.md b/README.md
index af7e7e8d..dcbf7e22 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,56 @@ There are five branches.
         
 
 
+## Headless driver: `bin/bnw`
+
+The web application is not needed to run BNW's computation.  `bin/bnw` does the
+same three-stage pipeline -- `network_score`, the k-best search, `structure` --
+in a private working directory, and writes machine-readable results:
+
+```sh
+# build the C programs once (needs gcc, g++ and R's standalone math library)
+var_lib_genenet_bnw/build.sh
+
+# learn a network: variables x samples, header line first
+bin/bnw learn --input test/golden/six_traits_matrix.tsv
+```
+
+```
+# bnw 0.1.0 variables=6 samples=200 maxparents=4 k=1 tiers=3 threshold=0.5
+from	to	posterior	in_structure
+Trait2	Trait1	1	1
+Trait2	Trait4	1	1
+...
+```
+
+Every candidate edge is reported with its posterior, and `in_structure` says
+whether it survived `--threshold`.  Options: `--maxparents` (default 4),
+`--k` (1), `--tiers` (3), `--threshold` (0.5) -- BNW's own defaults from
+`sourcecodes/create_tiers_gom_part1.php`; `--ban`/`--white` for edge
+constraints; `--format json`; `--svg FILE` for a graphviz drawing;
+`--workdir DIR` and `--keep` to inspect a run.  Exit codes: 0 ok, 2 bad input,
+3 the computation failed, 77 the binaries are not installed.
+
+### Parity with the web application
+
+`test/parity_check.py` runs the driver on the six continuous traits of BNW's
+own 8-node example and compares against files the *web application* produced
+(the `test/golden/` fixtures, captured from the `bnw-container` on
+2026-11-12 by posting to `bn_genenet.php` and running `execute_bn_gom.php`):
+
+```
+ok   continuous_input.txt matches the web application's
+ok   structure_input.txt matches the web application's (5 edge(s))
+ok   stdout reports the same 5 edge(s)
+PARITY OK
+```
+
+That check is also how the input format was pinned down: `continuous_input.txt`
+is a header line of variable names, then a **type line** (1 = continuous, n =
+n-level discrete, one integer per variable), then the data.  The type line is
+not optional -- `network_score` reads it before the data -- and it is the
+reason the web application's copy has one more line than the matrix.
+
 ## Testing (HTTP functional tests)
 
 `test/bnw_http_tests.py` tests a *running* BNW instance over plain
diff --git a/bin/bnw b/bin/bnw
new file mode 100755
index 00000000..563e1462
--- /dev/null
+++ b/bin/bnw
@@ -0,0 +1,444 @@
+#!/usr/bin/env python3
+"""bnw --- headless Bayesian network learning for GeneNetwork
+
+The web application (PHP) builds a handful of files in /var/lib/genenet/bnw
+and then runs three C programs over them.  This program does the same
+computation without the web application: one matrix in, edges out, a private
+working directory per invocation, and nothing that needs a session key.
+
+    bnw learn --input matrix.tsv [options]        # learn a network
+    bnw version
+
+The matrix is variables × samples: a header line naming the variables, then
+one line per sample.  That is exactly what BNW's own example datasets carry
+(sourcecodes/example_datasets/), and what GeneNetwork4 can build from a set of
+traits or probes.
+
+Output (--format tsv, the default) goes to stdout:
+
+    # bnw <version> variables=8 samples=200 maxparents=4 k=1 tiers=3 threshold=0.5
+    from<TAB>to<TAB>posterior<TAB>in_structure
+    Geno1<TAB>Trait1<TAB>1<TAB>1
+    ...
+
+Every candidate edge is reported, with its posterior, and `in_structure` says
+whether it survived the threshold.  A posterior is not a p-value: it is the
+share of the k-best networks that contain the edge, so it answers "how often
+did the search put this arrow there", not "how surprising is it".
+
+Exit codes: 0 ok, 2 bad input (the message names the problem), 3 the
+computation failed (the message is the C program's own), 77 the BNW binaries
+are not installed -- the conventional "skip" for a test suite.
+"""
+
+import argparse
+import json
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+from pathlib import Path
+
+VERSION = "0.1.0"
+
+# BNW's own defaults, from sourcecodes/create_tiers_gom_part1.php: the web
+# application writes these into <key>parent.txt, <key>k.txt and <key>thr.txt
+# unless the user changes them in its settings page.
+DEFAULT_MAXPARENTS = 4
+DEFAULT_K = 1
+DEFAULT_TIERS = 3
+DEFAULT_THRESHOLD = 0.5
+
+# Where the C programs live, relative to this file: ../var_lib_genenet_bnw,
+# built by var_lib_genenet_bnw/build.sh.  --bin-dir overrides it.
+HERE = Path(__file__).resolve().parent
+DEFAULT_BIN_DIR = HERE.parent / "var_lib_genenet_bnw"
+
+
+class InputError(Exception):
+    """Bad input: exit 2, with a message the caller can act on."""
+
+
+class ComputeError(Exception):
+    """The C programs failed: exit 3, carrying their own message."""
+
+
+class MissingBinaries(ComputeError):
+    """The BNW binaries are not installed: exit 77, the conventional 'skip'."""
+
+
+# ---------------------------------------------------------------------------
+# Input
+
+
+def read_matrix(path, min_variables=2):
+    """Read the variables × samples matrix, and check it before any C runs.
+
+    A malformed matrix used to travel all the way into the C programs and come
+    back as a segmentation fault or an empty result; every check here is one
+    that failed silently before.
+    """
+    try:
+        text = Path(path).read_text()
+    except OSError as error:
+        raise InputError(f"cannot read {path}: {error.strerror}") from error
+    lines = [line.rstrip("\r") for line in text.splitlines()]
+    lines = [line for line in lines if line.strip()]
+    if not lines:
+        raise InputError(f"{path} is empty")
+    header = [cell.strip() for cell in lines[0].split("\t")]
+    if len(header) == 1:
+        # A comma-separated header with tab-separated rows is a common mixup.
+        header = [cell.strip() for cell in lines[0].split(",")]
+        if len(header) > 1:
+            raise InputError(
+                "the header is comma-separated but the rows are not: BNW's "
+                "matrix is tab-separated throughout (header and data)")
+    if len(header) < min_variables:
+        raise InputError(
+            f"the header names {len(header)} variable(s); a network needs at "
+            f"least {min_variables}")
+    seen = {}
+    for index, name in enumerate(header):
+        if not name:
+            raise InputError(f"empty variable name in column {index + 1}")
+        if name in seen:
+            raise InputError(
+                f"variable {name!r} appears twice (columns "
+                f"{seen[name] + 1} and {index + 1}); names must be unique — "
+                "rename one copy")
+        seen[name] = index
+
+    rows = []
+    for number, line in enumerate(lines[1:], start=2):
+        cells = line.split("\t")
+        if len(cells) != len(header):
+            raise InputError(
+                f"row {number} has {len(cells)} field(s) but the header has "
+                f"{len(header)}")
+        row = []
+        for name, cell in zip(header, cells):
+            cell = cell.strip()
+            try:
+                row.append(float(cell))
+            except ValueError:
+                raise InputError(
+                    f"row {number}, variable {name!r}: {cell!r} is not a "
+                    "number (BNW's continuous model wants numeric values)") from None
+            if row[-1] != row[-1] or row[-1] in (float("inf"), float("-inf")):
+                raise InputError(
+                    f"row {number}, variable {name!r}: {cell!r} is not finite")
+        rows.append(row)
+    if len(rows) < 2:
+        raise InputError(
+            f"only {len(rows)} sample row(s): a network cannot be learned from "
+            "fewer than 2 samples")
+    return header, rows
+
+
+def write_input(directory, header, rows):
+    """Write continuous_input.txt: names first, then one line per sample.
+
+    network_score reads the names with fscanf("%s") and then the numbers, so
+    the file has to be exactly this shape -- tabs in the header, tabs in the
+    data, no trailing blank line.
+    """
+    path = Path(directory) / "continuous_input.txt"
+    with path.open("w") as handle:
+        handle.write("\t".join(header) + "\n")
+        # The type line: one integer per variable, 1 = continuous, n = n-level
+        # discrete.  network_score reads it before the data (and counts rows as
+        # lines - 3, so without it every value shifts by one).  This driver is
+        # continuous-only for now, so it is all 1s; the web application writes
+        # the level counts because its upload path accepts discrete columns.
+        handle.write("\t".join("1" for _ in header) + "\n")
+        for row in rows:
+            handle.write("\t".join(repr(value) for value in row) + "\n")
+    return path
+
+
+def write_constraints(directory, banned, white):
+    """Empty ban/white files are what the web application writes by default."""
+    ban_path = Path(directory) / "ban.txt"
+    white_path = Path(directory) / "white.txt"
+    for path, edges, what in ((ban_path, banned, "ban"), (white_path, white, "white")):
+        if edges is None:
+            path.write_text("")
+            continue
+        lines = []
+        for edge in edges:
+            parts = edge.replace("->", ",").split(",")
+            if len(parts) != 2 or not all(part.strip() for part in parts):
+                raise InputError(f"--{what} {edge!r} is not 'from,to'")
+            lines.append(f"{parts[0].strip()},{parts[1].strip()}")
+        path.write_text("\n".join(lines) + ("\n" if lines else ""))
+    return ban_path, white_path
+
+
+# ---------------------------------------------------------------------------
+# Computation
+
+
+def binaries(bin_dir):
+    """Locate the four programs the pipeline needs, or say what is missing."""
+    bin_dir = Path(bin_dir)
+    wanted = {
+        "network_score": bin_dir / "network_score",
+        "structure": bin_dir / "structure",
+        "get_kbest_parents": bin_dir / "k-best" / "src" / "get_kbest_parents",
+        "get_kbest_nets": bin_dir / "k-best" / "src" / "get_kbest_nets",
+    }
+    # Build output and checkout layout differ: build.sh puts network_score and
+    # structure at the top and the k-best binaries under k-best/src.
+    missing = [name for name, path in wanted.items() if not os.access(path, os.X_OK)]
+    if missing:
+        raise MissingBinaries(
+            "missing BNW binaries: " + ", ".join(missing) +
+            f"\n  looked in {bin_dir}" +
+            "\n  build them with var_lib_genenet_bnw/build.sh (needs gcc, g++ and libRmath)")
+    return wanted
+
+
+def run(command, what, cwd):
+    """Run one stage, and fail with *its* message rather than a traceback."""
+    completed = subprocess.run(
+        command, cwd=str(cwd), capture_output=True, text=True, check=False)
+    if completed.returncode != 0:
+        message = (completed.stderr or completed.stdout or "").strip()
+        raise ComputeError(
+            f"{what} failed (exit {completed.returncode})" +
+            (f": {message}" if message else ""))
+    return completed
+
+
+def learn(args):
+    header, rows = read_matrix(args.input)
+    if args.maxparents < 0:
+        raise InputError("--maxparents must be >= 0")
+    if args.k < 1:
+        raise InputError("--k must be >= 1")
+    if not 0.0 <= args.threshold <= 1.0:
+        raise InputError("--threshold is an edge posterior in [0, 1]")
+
+    workdir = Path(args.workdir) if args.workdir else Path(tempfile.mkdtemp(prefix="bnw-"))
+    workdir.mkdir(parents=True, exist_ok=True)
+    bin_dir = Path(args.bin_dir) if args.bin_dir else DEFAULT_BIN_DIR
+    programs = binaries(bin_dir)
+    try:
+        input_path = write_input(workdir, header, rows)
+        ban_path, white_path = write_constraints(workdir, args.ban, args.white)
+
+        # Stage 1: local scores.  argv[5] is a directory prefix -- the program
+        # writes one binary file of doubles per node into it (0, 1, 2, ...).
+        run([str(programs["network_score"]), str(input_path), str(ban_path),
+             str(white_path), str(args.maxparents), str(workdir)],
+            "network_score", workdir)
+
+        # Stage 2: the k-best search, which writes postProbEachEdge.txt.
+        run([str(programs["get_kbest_parents"]), str(len(header)), str(workdir),
+             str(args.k)], "get_kbest_parents", workdir)
+        run([str(programs["get_kbest_nets"]), str(len(header)), str(workdir),
+             str(args.k)], "get_kbest_nets", workdir)
+
+        # Stage 3: threshold the posteriors into an adjacency.
+        structure_tmp = workdir / "structure_input_temp.txt"
+        structure_out = workdir / "structure_input.txt"
+        # `structure' exits with whatever its uninitialised return value
+        # happens to be -- the web application ignores that and reads the file,
+        # so do the same, and only fail if the file is missing or empty.
+        subprocess.run([str(programs["structure"]), str(input_path),
+                        str(workdir / "postProbEachEdge.txt"), str(structure_tmp),
+                        str(structure_out), str(args.threshold)],
+                       cwd=str(workdir), capture_output=True, text=True, check=False)
+        if not structure_out.exists() or structure_out.stat().st_size == 0:
+            raise ComputeError(
+                f"structure wrote no {structure_out.name}; the network could not "
+                "be thresholded (is the posterior table empty?)")
+
+        edges = read_edges(workdir / "postProbEachEdge.txt", header)
+        structure = read_structure(structure_out, header)
+        for edge in edges:
+            edge["in_structure"] = (edge["from"], edge["to"]) in structure
+        if args.svg:
+            write_svg(args.svg, header, edges, args.threshold)
+        emit(edges, header, len(rows), args)
+    finally:
+        if args.keep:
+            print(f"work directory kept: {workdir}", file=sys.stderr)
+        elif not args.workdir:
+            shutil.rmtree(workdir, ignore_errors=True)
+
+
+def read_edges(path, header):
+    """postProbEachEdge.txt: lines 'j -> i <TAB> posterior', node numbers."""
+    edges = []
+    try:
+        lines = Path(path).read_text().splitlines()
+    except OSError as error:
+        raise ComputeError(f"{path}: {error.strerror}") from error
+    for line in lines:
+        line = line.strip()
+        if not line:
+            continue
+        try:
+            arrow, posterior = line.split("\t")
+            source, target = arrow.split("->")
+            from_index, to_index = int(source), int(target)
+        except ValueError:
+            raise ComputeError(
+                f"unexpected line in {path.name}: {line!r}") from None
+        if not (0 <= from_index < len(header) and 0 <= to_index < len(header)):
+            raise ComputeError(
+                f"{path.name} names node {from_index}->{to_index}, outside "
+                f"the {len(header)} variables")
+        edges.append({
+            "from": header[from_index],
+            "to": header[to_index],
+            "posterior": float(posterior),
+        })
+    edges = [edge for edge in edges if edge["from"] != edge["to"]]
+    edges.sort(key=lambda edge: (-edge["posterior"], edge["from"], edge["to"]))
+    return edges
+
+
+def read_structure(path, header):
+    """structure_input.txt: a header of names, then a 0/1 adjacency matrix."""
+    try:
+        lines = [line.rstrip("\r") for line in Path(path).read_text().splitlines() if line.strip()]
+    except OSError as error:
+        raise ComputeError(f"{path}: {error.strerror}") from error
+    if len(lines) != len(header) + 1:
+        raise ComputeError(
+            f"{path.name} has {len(lines)} line(s); expected {len(header) + 1}")
+    present = set()
+    for row_index, line in enumerate(lines[1:]):
+        cells = [cell for cell in line.split("\t") if cell != ""]
+        if len(cells) != len(header):
+            raise ComputeError(
+                f"{path.name} row {row_index + 1} has {len(cells)} column(s); "
+                f"expected {len(header)}")
+        for col_index, cell in enumerate(cells):
+            if cell.strip() == "1":
+                present.add((header[row_index], header[col_index]))
+    return present
+
+
+# ---------------------------------------------------------------------------
+# Output
+
+
+def emit(edges, header, samples, args):
+    parameters = {
+        "maxparents": args.maxparents,
+        "k": args.k,
+        "tiers": args.tiers,
+        "threshold": args.threshold,
+    }
+    header_line = (
+        f"# bnw {VERSION} variables={len(header)} samples={samples} "
+        f"maxparents={args.maxparents} k={args.k} tiers={args.tiers} "
+        f"threshold={args.threshold}")
+    if args.format == "tsv":
+        out = [header_line, "from\tto\tposterior\tin_structure"]
+        for edge in edges:
+            out.append(f"{edge['from']}\t{edge['to']}\t"
+                       f"{edge['posterior']:.10g}\t{1 if edge['in_structure'] else 0}")
+        print("\n".join(out))
+    else:
+        json.dump({
+            "version": VERSION,
+            "parameters": parameters,
+            "variables": header,
+            "samples": samples,
+            "edges": [
+                {"from": edge["from"], "to": edge["to"],
+                 "posterior": edge["posterior"],
+                 "in_structure": edge["in_structure"]}
+                for edge in edges
+            ],
+            "structure": sorted(
+                [edge["from"], edge["to"]] for edge in edges if edge["in_structure"]),
+        }, sys.stdout, indent=1)
+        print()
+
+
+def write_svg(path, header, edges, threshold):
+    """Optional: a dot drawing of the thresholded network, if graphviz exists."""
+    dot = shutil.which("dot")
+    if not dot:
+        raise ComputeError("--svg needs graphviz's `dot` on PATH")
+    lines = ["digraph bnw {", '  rankdir=LR;', '  node [shape=ellipse, fontsize=10];']
+    for name in header:
+        lines.append(f'  "{name}";')
+    for edge in edges:
+        if edge["in_structure"]:
+            weight = max(0.2, min(1.0, edge["posterior"]))
+            lines.append(
+                f'  "{edge["from"]}" -> "{edge["to"]}" '
+                f'[label="{edge["posterior"]:.2f}", weight="{weight:.2f}"];')
+    lines.append("}")
+    source = "\n".join(lines) + "\n"
+    completed = subprocess.run([dot, "-Tsvg", "-o", str(path)],
+                               input=source, text=True, capture_output=True, check=False)
+    if completed.returncode != 0:
+        raise ComputeError(f"dot failed: {(completed.stderr or '').strip()}")
+
+
+# ---------------------------------------------------------------------------
+
+
+def main(argv):
+    parser = argparse.ArgumentParser(
+        prog="bnw", description="Headless Bayesian network learning for GeneNetwork.")
+    sub = parser.add_subparsers(dest="command", required=True)
+
+    learn_parser = sub.add_parser(
+        "learn", help="learn a network from a variables x samples matrix")
+    learn_parser.add_argument("--input", required=True,
+                              help="matrix: header of variable names, then one row per sample")
+    learn_parser.add_argument("--maxparents", type=int, default=DEFAULT_MAXPARENTS,
+                              help=f"maximum parents per node (default {DEFAULT_MAXPARENTS})")
+    learn_parser.add_argument("--k", type=int, default=DEFAULT_K,
+                              help=f"networks to average over (default {DEFAULT_K})")
+    learn_parser.add_argument("--tiers", type=int, default=DEFAULT_TIERS,
+                              help=f"variable ordering tiers (default {DEFAULT_TIERS})")
+    learn_parser.add_argument("--threshold", type=float, default=DEFAULT_THRESHOLD,
+                              help=f"edge-posterior threshold (default {DEFAULT_THRESHOLD})")
+    learn_parser.add_argument("--ban", action="append", metavar="FROM,TO",
+                              help="banned edge; repeatable")
+    learn_parser.add_argument("--white", action="append", metavar="FROM,TO",
+                              help="forced edge; repeatable")
+    learn_parser.add_argument("--format", choices=("tsv", "json"), default="tsv")
+    learn_parser.add_argument("--svg", metavar="PATH",
+                              help="also write an SVG of the thresholded network (needs graphviz)")
+    learn_parser.add_argument("--workdir", metavar="DIR",
+                              help="work here instead of a fresh temporary directory")
+    learn_parser.add_argument("--keep", action="store_true",
+                              help="keep the work directory (it is removed otherwise)")
+    learn_parser.add_argument("--bin-dir", metavar="DIR",
+                              help="where the BNW C binaries are "
+                                   "(default: ../var_lib_genenet_bnw beside this script)")
+
+    sub.add_parser("version", help="print the version and exit")
+
+    args = parser.parse_args(argv)
+    if args.command == "version":
+        print(f"bnw {VERSION}")
+        return 0
+    try:
+        learn(args)
+    except InputError as error:
+        print(f"bnw: {error}", file=sys.stderr)
+        return 2
+    except MissingBinaries as error:
+        print(f"bnw: {error}", file=sys.stderr)
+        return 77
+    except ComputeError as error:
+        print(f"bnw: {error}", file=sys.stderr)
+        return 3
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv[1:]))
diff --git a/info_files/gn4-integration-plan.md b/info_files/gn4-integration-plan.md
index b0276eb0..c155e782 100644
--- a/info_files/gn4-integration-plan.md
+++ b/info_files/gn4-integration-plan.md
@@ -1,6 +1,8 @@
 # Plan: BNW as a GeneNetwork4 endpoint
 
-Status: **plan, nothing implemented**. Written from the code in this
+Status: **Phase 0 and Phase 1 done** (the driver exists and matches the web
+application on BNW's own example; see `test/parity_check.py` and the README's
+*Headless driver* section). The GN4 endpoint is not started. Written from the code in this
 repository and from how GN4 already embeds the other engines
 (`/compute/bulklmm`, `/compute/qtl2`, `/compute/gemma/ravanan`), so that the
 first commit of real work has a target to hit and a parity test to pass.
@@ -166,6 +168,26 @@ Acceptance: byte-identical `structure_input.txt` and `postProbEachEdge.txt` to
 Phase 0's golden files, and one documented invocation that reproduces the web
 app's answer.
 
+**Done (2026-11-12).** `bin/bnw` exists; on the six continuous traits of BNW's
+own 8-node example its `structure_input.txt` is byte-identical to the
+container's, and `test/parity_check.py` checks that plus the input file on every
+run. Four things the reference run settled:
+
+* the input file is header, then a **type line** (1 = continuous, n = n-level
+  discrete), then data -- `network_score` reads the type before the data, which
+  is why the web application's `continuous_input.txt` has one more line than the
+  matrix;
+* `structure` exits with an uninitialised status while writing a correct file,
+  so it must be judged by its output -- the web application ignores the status
+  and so does the driver;
+* `get_kbest_nets.cc` used SGI's `<ext/hash_set>`; it now uses
+  `std::unordered_set` with an explicit hasher (specialising `std::hash` for a
+  pointer type is not allowed), and `build.sh` compiles the C with
+  `-std=gnu89` because the sources predate C99;
+* the driver produces `postProbEachEdge.txt` with per-edge posteriors; the web
+  application's flow leaves none in `/var/lib/genenet/bnw/`, so the posteriors
+  are new information the endpoint can report.
+
 Acceptance: byte-identical `structure_input.txt` and `postProbEachEdge.txt` to
 Phase 0's golden files.
 
diff --git a/test/golden/six_traits_continuous_input.txt b/test/golden/six_traits_continuous_input.txt
new file mode 100644
index 00000000..0f57b501
--- /dev/null
+++ b/test/golden/six_traits_continuous_input.txt
@@ -0,0 +1,202 @@
+Trait1	Trait2	Trait3	Trait4	Trait5	Trait6
+1	1	1	1	1	1
+0.46259	1.2692	-1.3164	-0.44096	0.5929	-0.75422
+-0.10011	-0.88834	1.6407	0.99972	0.31288	0.65602
+-1.0533	-0.55296	1.2715	1.3686	-0.19153	0.37361
+0.042371	0.13573	-0.72659	-0.66133	0.43157	-1.6715
+-0.68686	-1.6152	-0.95019	-1.2239	0.12319	-0.79155
+-0.4558	0.090877	0.94224	1.2354	-0.36356	0.34937
+0.237	-0.49002	1.8939	1.4399	0.033059	1.3229
+0.070265	0.22547	-1.4903	-0.069341	-0.16148	-0.01449
+-0.69146	-0.43461	1.1501	0.21205	-0.18087	0.32746
+0.31535	-0.9858	0.57816	0.35924	0.12866	1.0553
+1.2718	0.39314	0.6669	0.90002	-0.30206	1.3376
+1.1871	-0.45611	0.87227	0.31014	-0.092846	0.81278
+-1.0168	-0.44286	1.6933	0.72968	0.038808	0.81906
+-0.25313	0.52468	0.02823	-0.20984	-0.075809	-1.3591
+-0.58108	-0.3967	-0.11925	-0.61288	0.46778	0.090941
+-0.88043	-0.28187	-1.1645	-1.2543	-0.28951	-1.039
+0.54121	0.24395	-1.0043	-1.4483	-0.19839	-2.5407
+0.47375	1.1319	0.68553	1.035	0.44043	1.3011
+0.57785	0.69093	1.5941	1.6735	0.19981	0.50708
+-0.43989	0.0024131	-1.5405	-1.2304	-0.15382	-1.1058
+0.38035	-0.063535	1.1606	1.3124	-0.38043	0.6277
+0.70114	-0.41044	-0.39767	-1.5424	0.27477	-0.76183
+-0.9406	-0.20526	1.3302	1.097	0.16006	1.2988
+0.52122	0.064793	-0.031492	0.21696	-0.41979	-0.79123
+-0.53387	-0.71043	-0.52344	-1.5584	-0.09538	-1.5781
+0.49563	-0.58126	0.7747	1.274	0.19196	0.10624
+1.1499	1.879	0.43595	0.73474	0.045381	-0.88645
+0.40705	-0.37647	-0.19815	-1.5366	0.37167	-0.10553
+0.62526	0.043371	-0.64449	-0.44076	-0.4318	-0.91807
+-0.61788	-0.62414	0.3169	0.85091	-0.10135	0.79065
+0.18491	-1.5226	1.1149	0.75046	0.25948	0.42431
+0.86063	0.92883	-1.1584	-0.099858	-0.55095	-0.064129
+-0.28946	0.0057533	-0.95703	-0.44018	0.084268	-0.23718
+-0.073096	0.16452	1.4444	1.1452	0.46025	0.30452
+0.15637	-0.73137	0.585	-0.046911	0.43881	1.2888
+0.081142	-0.63691	-0.14883	0.66593	-0.13479	-0.22207
+0.10876	-0.23383	1.8314	1.3282	0.4362	0.90139
+-1.0582	-0.97152	-0.57118	-1.2617	0.17115	-0.060679
+-0.47421	-0.56819	-0.38775	0.035215	0.51982	-1.6309
+-0.10242	0.8127	0.41773	-1.1308	-0.30719	-0.80758
+-0.083462	-0.4565	1.1556	0.3299	0.087362	0.31349
+-0.98158	0.01727	-1.3281	-1.4474	0.54555	-0.10096
+-0.80604	-0.19415	1.4986	1.3386	-0.16367	0.68271
+0.80082	-0.57853	1.0716	0.94015	-0.54114	0.032172
+2.0456	0.97954	-0.45536	-0.14332	-0.25819	-1.0833
+-0.30148	0.20623	0.64194	0.76414	-0.59067	0.64326
+-0.55625	-0.26957	1.8447	1.6568	-0.38077	0.58831
+0.11898	0.80706	-0.025844	-0.57984	-0.23395	-1.1554
+-0.095497	-1.0637	-0.92583	-0.57928	0.23695	-0.42992
+0.066154	0.73037	-0.73622	-0.57047	0.39169	0.40928
+0.81522	0.11381	0.68971	0.29219	0.037792	0.81502
+-0.65356	-0.20182	0.92089	0.92298	-0.5417	0.49215
+-0.90826	-0.10979	1.3847	0.75954	-0.20255	0.32975
+-0.25671	-1.1903	-1.4241	-1.6058	-0.062549	-1.3981
+-1.8038	-1.4801	-0.94121	-1.6244	0.4817	-0.0029469
+0.12651	0.028719	-1.5818	-1.0266	0.19779	-0.36357
+-1.0658	-0.58443	0.68956	0.35467	0.11177	1.5243
+0.14729	-0.15111	-0.027639	-0.2899	0.11533	-0.60305
+-0.76889	0.87161	-0.83556	-1.1777	-0.21878	-0.98527
+0.12252	0.49068	-0.2557	-1.5816	0.25332	-0.78715
+-0.40287	0.26456	0.62208	0.60909	0.36074	0.092703
+-0.78301	1.4641	1.3162	1.9448	-0.10841	0.9618
+-0.25018	-0.32797	-1.113	-1.6123	0.6272	-1.2718
+0.15804	-0.18147	2.4571	0.59504	-0.34056	0.8691
+-0.582	0.25738	1.4075	0.92251	-0.40889	0.66381
+-0.26827	0.58632	0.51883	1.5745	-0.13818	1.0526
+0.13453	0.18218	-0.51676	-0.54166	-0.36636	-1.206
+1.1502	0.36164	1.9983	1.0809	0.11272	0.67683
+0.27812	0.46559	-1.6109	-0.91039	0.3803	-0.71802
+0.15292	0.61921	0.71009	1.3063	-0.35819	0.8661
+0.19902	0.48986	-1.4026	-0.8495	0.47158	0.0622
+0.4838	1.1096	1.4392	1.1438	0.34739	0.084004
+-0.63609	-0.35105	0.63581	1.1939	-0.062938	0.91353
+0.64203	-0.5095	-1.7792	-0.88803	0.43482	-0.66314
+-0.18434	-0.48157	0.69942	0.56942	-0.019967	0.50506
+0.10168	-1.0928	1.2144	0.28313	0.22159	0.54058
+-0.18578	-0.45336	1.5468	0.99679	0.11821	1.0476
+-0.60607	-1.1275	-0.90963	-0.69014	-0.33163	-1.2734
+-0.35353	-0.42708	0.78989	0.14094	-0.056632	0.80598
+-0.020221	-1.1235	1.9782	-0.041281	0.19857	1.2488
+1.2346	-0.37121	1.3325	1.1237	-0.027458	1.0044
+-0.54489	1.6812	-0.59836	-0.012441	0.41243	-0.58835
+-0.029577	2.1707	1.3285	1.2109	0.29684	0.90646
+0.70774	1.0606	-1.3996	-0.020914	-0.60955	-1.2187
+0.07926	0.52862	1.3994	0.54549	0.55303	0.16155
+0.041856	-0.92023	0.87269	-0.66601	-0.15945	0.1004
+0.17665	0.31945	-1.7756	-0.60442	0.020525	-1.0625
+-0.090238	-0.65111	0.56993	-0.4117	-0.19486	-0.18849
+0.059166	-0.47194	-0.69068	-0.41022	0.25867	-0.057175
+0.10473	0.66887	0.082035	-0.55975	-0.28439	-1.468
+-0.20937	-0.36621	0.09609	0.28164	0.24673	-0.11107
+-1.2678	0.22369	-0.42201	-0.70103	0.075915	-1.3347
+-0.35467	0.11166	-0.42144	-0.095223	-0.29936	-0.68322
+0.84875	1.24	-0.81217	0.084034	-0.32224	-1.6241
+-0.45939	1.3208	0.97993	1.2991	-0.015791	1.487
+-0.42254	-0.57928	-0.58529	-2.2375	0.51999	-0.79909
+0.32092	-0.13796	0.67098	0.24639	-0.36066	-0.032948
+-1.2408	-0.94836	0.026276	-0.37981	-0.02237	-0.38777
+0.20959	2.1466	-1.7027	-1.3169	-0.16515	-1.046
+-0.10482	1.3773	-1.2998	-0.014223	0.34065	-0.35362
+-0.66188	-1.2477	-0.62084	0.27682	0.1674	-0.8918
+-0.97246	-0.73017	1.3843	1.5549	0.1067	0.85754
+0.74567	-0.032694	1.1153	0.97318	-0.0033905	0.85059
+0.29324	-0.090608	-0.4945	-1.0484	-0.14987	-1.0141
+-0.86818	0.44948	1.8531	0.51896	-0.021141	1.8486
+0.362	-0.29749	-0.66525	0.21709	-0.15594	-1.4087
+-0.17675	0.36465	-0.45423	-1.262	0.22968	-1.246
+-0.34633	-0.16067	-1.2216	-1.3019	-0.040891	-1.2935
+0.44608	-0.57732	0.25606	-1.1664	0.54705	-0.19859
+0.45398	-0.55214	-0.33433	-0.085418	-0.10094	-0.48509
+0.12407	0.022419	-1.0312	-0.47727	0.20924	-1.2961
+-0.76644	-1.1794	1.2345	0.84756	0.1532	0.77648
+-0.10329	-0.53768	-0.67677	-0.58708	-0.014634	-0.036347
+0.2378	0.15372	0.49652	0.29193	0.19047	1.152
+0.024605	-0.38937	-0.89907	-0.42106	0.16921	-0.80893
+0.28812	0.26265	-0.94234	-0.88527	-0.49803	0.48254
+0.85086	0.11783	1.5014	1.2382	0.24441	1.3649
+0.86172	0.14894	-0.4846	-1.3488	-0.41844	-1.1372
+0.67971	-0.27488	-0.50687	-0.79153	-0.093399	-1.391
+-0.91977	-0.66099	0.5801	0.46587	-0.2263	1.43
+0.88213	-0.48674	-0.42406	-1.2762	0.28981	-1.0813
+-0.36583	0.51518	1.4491	1.4995	-0.073755	1.1218
+-0.54006	-0.59443	0.81263	0.65809	-0.15621	1.2508
+1.2125	-0.012648	-0.92657	-0.81972	-0.1536	-1.0176
+0.42493	0.34558	-1.7672	-1.3503	-0.045928	0.37449
+0.17097	-0.86593	-0.13424	1.0257	0.058014	0.91044
+-1.393	0.5338	1.8514	2.291	0.15305	0.30028
+-0.58552	-1.5337	1.5133	0.9976	0.64638	1.4428
+0.046272	-0.56387	-0.86063	-1.0462	0.014457	-1.3038
+0.34933	-0.16796	-0.27854	-0.27327	-0.0040928	-0.17912
+-1.0672	0.3979	1.208	0.50111	-0.013978	0.015097
+0.31052	-0.46723	-1.1237	0.59267	0.30978	-1.0169
+-0.55337	-0.42776	-0.12612	-0.41838	-0.56558	-0.58189
+0.3925	0.15644	-0.72963	-0.66601	-0.10837	-0.27787
+-0.48513	-1.4119	-0.16889	-0.19646	-0.068098	-0.084247
+0.16692	1.5447	-1.3064	-1.3338	-0.25668	-0.48832
+-0.36092	0.31518	-0.44815	-1.4019	-0.47505	-0.37187
+-1.8192	-0.57745	1.1304	0.262	-0.3595	0.58577
+-0.065383	0.07156	1.4931	2.3215	0.85149	1.4585
+-1.119	0.70896	1.0498	0.74334	0.75838	1.6107
+-0.17682	-0.21029	-0.74465	-0.83806	0.10827	-1.2829
+0.69834	-0.082402	0.64008	-0.83119	-0.26766	0.36961
+-0.28266	1.1929	-0.15174	0.098554	-0.33989	-0.6376
+1.5369	1.8434	0.0030761	-0.59703	0.14497	-1.5165
+0.35023	-0.28475	-0.93594	-1.0937	-0.46614	-2.188
+0.62308	1.0873	-0.0037182	0.86847	-0.48699	-0.10105
+-1.0408	-0.99866	2.0799	1.1634	0.092516	0.30401
+-0.87125	-0.88987	1.0278	1.0157	-0.23725	1.2377
+-0.020591	-0.03398	-0.024834	-1.3588	-0.045961	-0.89693
+0.9213	-0.57037	1.044	1.2994	0.046517	0.66563
+-0.87288	-0.96382	-0.704	-1.2184	0.046903	-0.30008
+0.77586	-0.34168	-0.09867	-0.37744	-0.24309	-0.13441
+0.19521	0.67595	1.4533	1.5115	0.19553	0.79596
+-1.3273	1.0891	1.2544	0.98928	0.045725	0.54963
+0.59176	0.65915	-0.84578	-0.080229	-0.26961	0.24045
+-0.66958	-1.042	-1.2138	-0.7612	-0.22775	-0.40062
+1.1154	-0.57069	-0.6592	-2.1961	-0.013175	0.46007
+-0.5254	0.0031225	-0.77758	0.8169	0.13224	-0.65572
+0.24551	1.5078	0.10719	1.1454	0.70578	0.59051
+0.29875	-0.11455	-1.0497	-0.77881	0.080708	-0.48337
+-0.1047	0.011628	1.2504	0.41776	-0.20102	1.2252
+0.44808	-0.8165	-0.080134	-0.49618	0.051011	-0.71722
+2.0365	1.5592	1.3257	1.2472	-0.47899	0.10943
+-0.16507	-0.66011	0.98739	0.58519	0.245	-0.43277
+-0.028366	-0.16961	-1.3266	0.14248	-0.24634	-1.1154
+0.92842	1.8842	1.3169	1.0358	-0.20988	0.88021
+0.33158	0.32463	-0.50924	0.40233	0.54391	-0.24309
+1.2555	0.1928	-0.487	0.048779	-0.23483	-1.477
+0.75021	0.85943	-0.2323	0.10748	-0.68906	0.84276
+0.6297	1.7397	-1.6136	-0.43268	-0.21442	-0.77462
+0.53168	1.0807	1.0853	0.62304	0.44036	0.48087
+-0.31256	-0.69771	-0.88556	-1.333	0.038614	-1.5744
+-0.039695	-0.52447	0.91401	-0.93272	0.53434	-0.080746
+-0.16596	0.56821	0.76008	0.45442	0.10286	0.6539
+-0.4331	-0.89669	-1.3097	-1.2157	0.1065	-0.8512
+-0.34708	-0.51457	-1.012	-0.39437	-0.55374	-1.144
+0.024082	0.7128	-1.3702	-1.4492	-0.10228	-0.59071
+-0.0037109	-0.68845	-0.649	-0.91581	0.066805	-0.98583
+0.68706	0.067424	0.87139	0.7938	-0.099645	1.529
+-0.31183	0.57814	-0.80815	-0.66724	-0.31919	-0.61319
+-0.11153	-0.6246	-0.27979	-0.18363	0.83639	0.014919
+0.20406	1.5838	-0.96334	-1.1228	-0.17808	-1.2123
+-0.1134	0.63358	-0.53321	-0.84276	-0.27657	-0.36992
+0.25517	0.67006	-0.1349	0.18275	0.40982	-0.62487
+-1.2684	0.31042	0.91155	0.66143	0.2483	-0.12434
+-0.97542	-0.85419	0.7663	0.16362	-0.18737	0.67266
+-0.043882	-0.62746	-0.82816	-0.40626	-0.051048	-0.61297
+0.59605	-0.45852	1.2962	0.78156	-0.48154	0.17778
+-0.06266	1.1144	-0.80524	-0.3189	-0.16038	-0.96372
+-1.1821	1.0798	-1.3407	-0.52643	0.12458	-0.7871
+0.59446	0.89186	-0.97856	-0.64658	0.2712	-1.5538
+0.15067	0.41827	-1.0487	-1.0551	0.19143	-1.4537
+-1.073	0.022587	-1.7088	-1.3777	0.1964	-0.84025
+0.20928	1.8026	1.2073	2.78	0.15131	0.3448
+-1.1317	-0.6359	-0.70457	-1.676	-0.22886	-0.58769
+-0.039428	-0.58746	0.56	0.7896	-0.052581	0.99511
+0.55023	-0.20403	0.138	-0.70518	0.66927	-0.28861
+0.57423	1.1886	0.092876	0.025677	-0.41099	-1.2858
+-0.14179	1.5827	-0.94328	0.7703	-0.43967	-0.50448
+0.14666	1.0492	-0.0030259	-0.62418	-0.087515	-0.048386
diff --git a/test/golden/six_traits_matrix.tsv b/test/golden/six_traits_matrix.tsv
new file mode 100644
index 00000000..8ec75e79
--- /dev/null
+++ b/test/golden/six_traits_matrix.tsv
@@ -0,0 +1,201 @@
+Trait1	Trait2	Trait3	Trait4	Trait5	Trait6
+0.46259	1.2692	-1.3164	-0.44096	0.5929	-0.75422
+-0.10011	-0.88834	1.6407	0.99972	0.31288	0.65602
+-1.0533	-0.55296	1.2715	1.3686	-0.19153	0.37361
+0.042371	0.13573	-0.72659	-0.66133	0.43157	-1.6715
+-0.68686	-1.6152	-0.95019	-1.2239	0.12319	-0.79155
+-0.4558	0.090877	0.94224	1.2354	-0.36356	0.34937
+0.237	-0.49002	1.8939	1.4399	0.033059	1.3229
+0.070265	0.22547	-1.4903	-0.069341	-0.16148	-0.01449
+-0.69146	-0.43461	1.1501	0.21205	-0.18087	0.32746
+0.31535	-0.9858	0.57816	0.35924	0.12866	1.0553
+1.2718	0.39314	0.6669	0.90002	-0.30206	1.3376
+1.1871	-0.45611	0.87227	0.31014	-0.092846	0.81278
+-1.0168	-0.44286	1.6933	0.72968	0.038808	0.81906
+-0.25313	0.52468	0.02823	-0.20984	-0.075809	-1.3591
+-0.58108	-0.3967	-0.11925	-0.61288	0.46778	0.090941
+-0.88043	-0.28187	-1.1645	-1.2543	-0.28951	-1.039
+0.54121	0.24395	-1.0043	-1.4483	-0.19839	-2.5407
+0.47375	1.1319	0.68553	1.035	0.44043	1.3011
+0.57785	0.69093	1.5941	1.6735	0.19981	0.50708
+-0.43989	0.0024131	-1.5405	-1.2304	-0.15382	-1.1058
+0.38035	-0.063535	1.1606	1.3124	-0.38043	0.6277
+0.70114	-0.41044	-0.39767	-1.5424	0.27477	-0.76183
+-0.9406	-0.20526	1.3302	1.097	0.16006	1.2988
+0.52122	0.064793	-0.031492	0.21696	-0.41979	-0.79123
+-0.53387	-0.71043	-0.52344	-1.5584	-0.09538	-1.5781
+0.49563	-0.58126	0.7747	1.274	0.19196	0.10624
+1.1499	1.879	0.43595	0.73474	0.045381	-0.88645
+0.40705	-0.37647	-0.19815	-1.5366	0.37167	-0.10553
+0.62526	0.043371	-0.64449	-0.44076	-0.4318	-0.91807
+-0.61788	-0.62414	0.3169	0.85091	-0.10135	0.79065
+0.18491	-1.5226	1.1149	0.75046	0.25948	0.42431
+0.86063	0.92883	-1.1584	-0.099858	-0.55095	-0.064129
+-0.28946	0.0057533	-0.95703	-0.44018	0.084268	-0.23718
+-0.073096	0.16452	1.4444	1.1452	0.46025	0.30452
+0.15637	-0.73137	0.585	-0.046911	0.43881	1.2888
+0.081142	-0.63691	-0.14883	0.66593	-0.13479	-0.22207
+0.10876	-0.23383	1.8314	1.3282	0.4362	0.90139
+-1.0582	-0.97152	-0.57118	-1.2617	0.17115	-0.060679
+-0.47421	-0.56819	-0.38775	0.035215	0.51982	-1.6309
+-0.10242	0.8127	0.41773	-1.1308	-0.30719	-0.80758
+-0.083462	-0.4565	1.1556	0.3299	0.087362	0.31349
+-0.98158	0.01727	-1.3281	-1.4474	0.54555	-0.10096
+-0.80604	-0.19415	1.4986	1.3386	-0.16367	0.68271
+0.80082	-0.57853	1.0716	0.94015	-0.54114	0.032172
+2.0456	0.97954	-0.45536	-0.14332	-0.25819	-1.0833
+-0.30148	0.20623	0.64194	0.76414	-0.59067	0.64326
+-0.55625	-0.26957	1.8447	1.6568	-0.38077	0.58831
+0.11898	0.80706	-0.025844	-0.57984	-0.23395	-1.1554
+-0.095497	-1.0637	-0.92583	-0.57928	0.23695	-0.42992
+0.066154	0.73037	-0.73622	-0.57047	0.39169	0.40928
+0.81522	0.11381	0.68971	0.29219	0.037792	0.81502
+-0.65356	-0.20182	0.92089	0.92298	-0.5417	0.49215
+-0.90826	-0.10979	1.3847	0.75954	-0.20255	0.32975
+-0.25671	-1.1903	-1.4241	-1.6058	-0.062549	-1.3981
+-1.8038	-1.4801	-0.94121	-1.6244	0.4817	-0.0029469
+0.12651	0.028719	-1.5818	-1.0266	0.19779	-0.36357
+-1.0658	-0.58443	0.68956	0.35467	0.11177	1.5243
+0.14729	-0.15111	-0.027639	-0.2899	0.11533	-0.60305
+-0.76889	0.87161	-0.83556	-1.1777	-0.21878	-0.98527
+0.12252	0.49068	-0.2557	-1.5816	0.25332	-0.78715
+-0.40287	0.26456	0.62208	0.60909	0.36074	0.092703
+-0.78301	1.4641	1.3162	1.9448	-0.10841	0.9618
+-0.25018	-0.32797	-1.113	-1.6123	0.6272	-1.2718
+0.15804	-0.18147	2.4571	0.59504	-0.34056	0.8691
+-0.582	0.25738	1.4075	0.92251	-0.40889	0.66381
+-0.26827	0.58632	0.51883	1.5745	-0.13818	1.0526
+0.13453	0.18218	-0.51676	-0.54166	-0.36636	-1.206
+1.1502	0.36164	1.9983	1.0809	0.11272	0.67683
+0.27812	0.46559	-1.6109	-0.91039	0.3803	-0.71802
+0.15292	0.61921	0.71009	1.3063	-0.35819	0.8661
+0.19902	0.48986	-1.4026	-0.8495	0.47158	0.0622
+0.4838	1.1096	1.4392	1.1438	0.34739	0.084004
+-0.63609	-0.35105	0.63581	1.1939	-0.062938	0.91353
+0.64203	-0.5095	-1.7792	-0.88803	0.43482	-0.66314
+-0.18434	-0.48157	0.69942	0.56942	-0.019967	0.50506
+0.10168	-1.0928	1.2144	0.28313	0.22159	0.54058
+-0.18578	-0.45336	1.5468	0.99679	0.11821	1.0476
+-0.60607	-1.1275	-0.90963	-0.69014	-0.33163	-1.2734
+-0.35353	-0.42708	0.78989	0.14094	-0.056632	0.80598
+-0.020221	-1.1235	1.9782	-0.041281	0.19857	1.2488
+1.2346	-0.37121	1.3325	1.1237	-0.027458	1.0044
+-0.54489	1.6812	-0.59836	-0.012441	0.41243	-0.58835
+-0.029577	2.1707	1.3285	1.2109	0.29684	0.90646
+0.70774	1.0606	-1.3996	-0.020914	-0.60955	-1.2187
+0.07926	0.52862	1.3994	0.54549	0.55303	0.16155
+0.041856	-0.92023	0.87269	-0.66601	-0.15945	0.1004
+0.17665	0.31945	-1.7756	-0.60442	0.020525	-1.0625
+-0.090238	-0.65111	0.56993	-0.4117	-0.19486	-0.18849
+0.059166	-0.47194	-0.69068	-0.41022	0.25867	-0.057175
+0.10473	0.66887	0.082035	-0.55975	-0.28439	-1.468
+-0.20937	-0.36621	0.09609	0.28164	0.24673	-0.11107
+-1.2678	0.22369	-0.42201	-0.70103	0.075915	-1.3347
+-0.35467	0.11166	-0.42144	-0.095223	-0.29936	-0.68322
+0.84875	1.24	-0.81217	0.084034	-0.32224	-1.6241
+-0.45939	1.3208	0.97993	1.2991	-0.015791	1.487
+-0.42254	-0.57928	-0.58529	-2.2375	0.51999	-0.79909
+0.32092	-0.13796	0.67098	0.24639	-0.36066	-0.032948
+-1.2408	-0.94836	0.026276	-0.37981	-0.02237	-0.38777
+0.20959	2.1466	-1.7027	-1.3169	-0.16515	-1.046
+-0.10482	1.3773	-1.2998	-0.014223	0.34065	-0.35362
+-0.66188	-1.2477	-0.62084	0.27682	0.1674	-0.8918
+-0.97246	-0.73017	1.3843	1.5549	0.1067	0.85754
+0.74567	-0.032694	1.1153	0.97318	-0.0033905	0.85059
+0.29324	-0.090608	-0.4945	-1.0484	-0.14987	-1.0141
+-0.86818	0.44948	1.8531	0.51896	-0.021141	1.8486
+0.362	-0.29749	-0.66525	0.21709	-0.15594	-1.4087
+-0.17675	0.36465	-0.45423	-1.262	0.22968	-1.246
+-0.34633	-0.16067	-1.2216	-1.3019	-0.040891	-1.2935
+0.44608	-0.57732	0.25606	-1.1664	0.54705	-0.19859
+0.45398	-0.55214	-0.33433	-0.085418	-0.10094	-0.48509
+0.12407	0.022419	-1.0312	-0.47727	0.20924	-1.2961
+-0.76644	-1.1794	1.2345	0.84756	0.1532	0.77648
+-0.10329	-0.53768	-0.67677	-0.58708	-0.014634	-0.036347
+0.2378	0.15372	0.49652	0.29193	0.19047	1.152
+0.024605	-0.38937	-0.89907	-0.42106	0.16921	-0.80893
+0.28812	0.26265	-0.94234	-0.88527	-0.49803	0.48254
+0.85086	0.11783	1.5014	1.2382	0.24441	1.3649
+0.86172	0.14894	-0.4846	-1.3488	-0.41844	-1.1372
+0.67971	-0.27488	-0.50687	-0.79153	-0.093399	-1.391
+-0.91977	-0.66099	0.5801	0.46587	-0.2263	1.43
+0.88213	-0.48674	-0.42406	-1.2762	0.28981	-1.0813
+-0.36583	0.51518	1.4491	1.4995	-0.073755	1.1218
+-0.54006	-0.59443	0.81263	0.65809	-0.15621	1.2508
+1.2125	-0.012648	-0.92657	-0.81972	-0.1536	-1.0176
+0.42493	0.34558	-1.7672	-1.3503	-0.045928	0.37449
+0.17097	-0.86593	-0.13424	1.0257	0.058014	0.91044
+-1.393	0.5338	1.8514	2.291	0.15305	0.30028
+-0.58552	-1.5337	1.5133	0.9976	0.64638	1.4428
+0.046272	-0.56387	-0.86063	-1.0462	0.014457	-1.3038
+0.34933	-0.16796	-0.27854	-0.27327	-0.0040928	-0.17912
+-1.0672	0.3979	1.208	0.50111	-0.013978	0.015097
+0.31052	-0.46723	-1.1237	0.59267	0.30978	-1.0169
+-0.55337	-0.42776	-0.12612	-0.41838	-0.56558	-0.58189
+0.3925	0.15644	-0.72963	-0.66601	-0.10837	-0.27787
+-0.48513	-1.4119	-0.16889	-0.19646	-0.068098	-0.084247
+0.16692	1.5447	-1.3064	-1.3338	-0.25668	-0.48832
+-0.36092	0.31518	-0.44815	-1.4019	-0.47505	-0.37187
+-1.8192	-0.57745	1.1304	0.262	-0.3595	0.58577
+-0.065383	0.07156	1.4931	2.3215	0.85149	1.4585
+-1.119	0.70896	1.0498	0.74334	0.75838	1.6107
+-0.17682	-0.21029	-0.74465	-0.83806	0.10827	-1.2829
+0.69834	-0.082402	0.64008	-0.83119	-0.26766	0.36961
+-0.28266	1.1929	-0.15174	0.098554	-0.33989	-0.6376
+1.5369	1.8434	0.0030761	-0.59703	0.14497	-1.5165
+0.35023	-0.28475	-0.93594	-1.0937	-0.46614	-2.188
+0.62308	1.0873	-0.0037182	0.86847	-0.48699	-0.10105
+-1.0408	-0.99866	2.0799	1.1634	0.092516	0.30401
+-0.87125	-0.88987	1.0278	1.0157	-0.23725	1.2377
+-0.020591	-0.03398	-0.024834	-1.3588	-0.045961	-0.89693
+0.9213	-0.57037	1.044	1.2994	0.046517	0.66563
+-0.87288	-0.96382	-0.704	-1.2184	0.046903	-0.30008
+0.77586	-0.34168	-0.09867	-0.37744	-0.24309	-0.13441
+0.19521	0.67595	1.4533	1.5115	0.19553	0.79596
+-1.3273	1.0891	1.2544	0.98928	0.045725	0.54963
+0.59176	0.65915	-0.84578	-0.080229	-0.26961	0.24045
+-0.66958	-1.042	-1.2138	-0.7612	-0.22775	-0.40062
+1.1154	-0.57069	-0.6592	-2.1961	-0.013175	0.46007
+-0.5254	0.0031225	-0.77758	0.8169	0.13224	-0.65572
+0.24551	1.5078	0.10719	1.1454	0.70578	0.59051
+0.29875	-0.11455	-1.0497	-0.77881	0.080708	-0.48337
+-0.1047	0.011628	1.2504	0.41776	-0.20102	1.2252
+0.44808	-0.8165	-0.080134	-0.49618	0.051011	-0.71722
+2.0365	1.5592	1.3257	1.2472	-0.47899	0.10943
+-0.16507	-0.66011	0.98739	0.58519	0.245	-0.43277
+-0.028366	-0.16961	-1.3266	0.14248	-0.24634	-1.1154
+0.92842	1.8842	1.3169	1.0358	-0.20988	0.88021
+0.33158	0.32463	-0.50924	0.40233	0.54391	-0.24309
+1.2555	0.1928	-0.487	0.048779	-0.23483	-1.477
+0.75021	0.85943	-0.2323	0.10748	-0.68906	0.84276
+0.6297	1.7397	-1.6136	-0.43268	-0.21442	-0.77462
+0.53168	1.0807	1.0853	0.62304	0.44036	0.48087
+-0.31256	-0.69771	-0.88556	-1.333	0.038614	-1.5744
+-0.039695	-0.52447	0.91401	-0.93272	0.53434	-0.080746
+-0.16596	0.56821	0.76008	0.45442	0.10286	0.6539
+-0.4331	-0.89669	-1.3097	-1.2157	0.1065	-0.8512
+-0.34708	-0.51457	-1.012	-0.39437	-0.55374	-1.144
+0.024082	0.7128	-1.3702	-1.4492	-0.10228	-0.59071
+-0.0037109	-0.68845	-0.649	-0.91581	0.066805	-0.98583
+0.68706	0.067424	0.87139	0.7938	-0.099645	1.529
+-0.31183	0.57814	-0.80815	-0.66724	-0.31919	-0.61319
+-0.11153	-0.6246	-0.27979	-0.18363	0.83639	0.014919
+0.20406	1.5838	-0.96334	-1.1228	-0.17808	-1.2123
+-0.1134	0.63358	-0.53321	-0.84276	-0.27657	-0.36992
+0.25517	0.67006	-0.1349	0.18275	0.40982	-0.62487
+-1.2684	0.31042	0.91155	0.66143	0.2483	-0.12434
+-0.97542	-0.85419	0.7663	0.16362	-0.18737	0.67266
+-0.043882	-0.62746	-0.82816	-0.40626	-0.051048	-0.61297
+0.59605	-0.45852	1.2962	0.78156	-0.48154	0.17778
+-0.06266	1.1144	-0.80524	-0.3189	-0.16038	-0.96372
+-1.1821	1.0798	-1.3407	-0.52643	0.12458	-0.7871
+0.59446	0.89186	-0.97856	-0.64658	0.2712	-1.5538
+0.15067	0.41827	-1.0487	-1.0551	0.19143	-1.4537
+-1.073	0.022587	-1.7088	-1.3777	0.1964	-0.84025
+0.20928	1.8026	1.2073	2.78	0.15131	0.3448
+-1.1317	-0.6359	-0.70457	-1.676	-0.22886	-0.58769
+-0.039428	-0.58746	0.56	0.7896	-0.052581	0.99511
+0.55023	-0.20403	0.138	-0.70518	0.66927	-0.28861
+0.57423	1.1886	0.092876	0.025677	-0.41099	-1.2858
+-0.14179	1.5827	-0.94328	0.7703	-0.43967	-0.50448
+0.14666	1.0492	-0.0030259	-0.62418	-0.087515	-0.048386
diff --git a/test/golden/six_traits_structure_input.txt b/test/golden/six_traits_structure_input.txt
new file mode 100644
index 00000000..7f4768ca
--- /dev/null
+++ b/test/golden/six_traits_structure_input.txt
@@ -0,0 +1,7 @@
+Trait1	Trait2	Trait3	Trait4	Trait5	Trait6	
+0	0	0	0	0	0	
+1	0	0	1	0	0	
+0	0	0	1	0	0	
+0	0	0	0	0	0	
+0	0	0	0	0	0	
+0	0	1	1	0	0	
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:]))
diff --git a/var_lib_genenet_bnw/build.sh b/var_lib_genenet_bnw/build.sh
index c2975ff4..4dc345f4 100755
--- a/var_lib_genenet_bnw/build.sh
+++ b/var_lib_genenet_bnw/build.sh
@@ -1,6 +1,32 @@
 #!/bin/bash
-gcc ./localscore/network_score.c $(pkgconf --libs libRmath) -lm -o network_score
-gcc ./print_structure/structure.c -lm -o structure
+# Build the four C/C++ programs the structure-learning pipeline needs.
+#
+# The sources predate C99 and C++11: network_score and structure rely on
+# implicit int and fall off the end of main(), and get_kbest_nets used SGI's
+# <ext/hash_set>.  -std=gnu89 keeps the C compiling on a modern gcc, and
+# get_kbest_nets.cc was ported to std::unordered_set (see the comment at its
+# hash-set block) so no compatibility flag is needed there.
+#
+# network_score needs R's standalone math library:
+#   guix shell rmath-standalone gcc-toolchain -- ./build.sh
+set -e
+CFLAGS=${CFLAGS:--O2 -std=gnu89 -w}
+
+RMATH_PREFIX=${RMATH_PREFIX:-}
+if [ -z "$RMATH_PREFIX" ]; then
+    # ask pkgconf if it is around, else fall back to $R_HOME / the usual names
+    if command -v pkgconf >/dev/null 2>&1; then
+        RMATH_LIBS=$(pkgconf --libs libRmath)
+    fi
+    RMATH_PREFIX=$(dirname "$(dirname "$(command -v R 2>/dev/null || echo /usr/lib/R/bin/R)")")
+fi
+
+gcc $CFLAGS ${RMATH_PREFIX:+-I$RMATH_PREFIX/include} \
+    ./localscore/network_score.c ${RMATH_LIBS:-${RMATH_PREFIX:+-L$RMATH_PREFIX/lib -lRmath}} -lm \
+    -o network_score
+gcc $CFLAGS ./print_structure/structure.c -lm -o structure
 
 cd ./k-best/src/
-sh buildk_poster.sh
+D=${CXX:-g++}
+$D -w -O2 -o get_kbest_parents get_kbest_parents.cc
+$D -w -O2 -o get_kbest_nets get_kbest_nets.cc
diff --git a/var_lib_genenet_bnw/k-best/src/get_kbest_nets.cc b/var_lib_genenet_bnw/k-best/src/get_kbest_nets.cc
index 2ee35265..d0911cf0 100644
--- a/var_lib_genenet_bnw/k-best/src/get_kbest_nets.cc
+++ b/var_lib_genenet_bnw/k-best/src/get_kbest_nets.cc
@@ -16,24 +16,13 @@
 
 
 //HR: Add for hash_set
-#if __GNUC__ < 3 && __GNUC__ >= 2 && __GNUC_MINOR__ >= 95
-# include <hash_set>
-# include <functional>
-# define gnu_namespace std
-#elif __GNUC__ >= 3
-# include <ext/hash_set>
-# if __GNUC_MINOR__ == 0
-# include <functional>
-# define gnu_namespace std
-# else
-# include <ext/functional>
-# define gnu_namespace __gnu_cxx
-# endif
-#else
-# include <hash_set.h>
-# include <functional.h>
-# define gnu_namespace std
-#endif
+// Upstream this was a maze of #ifdefs for <hash_set>/<ext/hash_set>, which
+// C++11 and later dropped.  The set is a std::unordered_set with an explicit
+// hasher; naming the hasher (rather than specialising std::hash for a pointer
+// type, which is not allowed) is the whole portability fix.
+#include <unordered_set>
+#include <functional>
+#define gnu_namespace std
 using namespace gnu_namespace;
 #include <set>
 //
@@ -609,21 +598,13 @@ void insert_vec(list<node>* v,node s, int k){
 
 //===================================================================//
 //HR: Add hash_set to store net_set (i.e. (net, score) pair
-namespace gnu_namespace {
-	
-	template<>
+struct net_set_hash {
+	std::hash<unsigned> hasher_ns;
 
-	struct hash<const net_set*> {
-	
-		hash<unsigned> hasher_ns;
-		
-		size_t operator()(const net_set* ns) const {
-			return hasher_ns((unsigned) roundl(- ns->score*10)); // wrong hash function??
-		}
-
-	}; //end of struct
-
-} //end of namespace
+	size_t operator()(const net_set* ns) const {
+		return hasher_ns((unsigned) roundl(- ns->score*10)); // wrong hash function??
+	}
+}; //end of struct
 
 
 
@@ -635,7 +616,7 @@ struct eqNetScore{
 
 
 
-typedef hash_set<const net_set *, hash<const net_set *>, eqNetScore> NetScoreHashSet;
+typedef std::unordered_set<const net_set *, net_set_hash, eqNetScore> NetScoreHashSet;
 
 NetScoreHashSet knets;