"""Culling-equivalence harness (preregistration, section 5) on synthetic data (no download).""" import importlib.util from pathlib import Path import numpy as np from flysim.validation import chains as ch from flysim.validation import culling as cu # the synthetic summaries of the chain tests (pytest runs with --import-mode=importlib, # so the sibling module is loaded by path) _spec = importlib.util.spec_from_file_location("_chain_tests", Path(__file__).with_name("test_chains.py")) assert _spec is not None and _spec.loader is not None _chain_tests = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(_chain_tests) GOOD, _data = _chain_tests.GOOD, _chain_tests._data ALL = ["LC4", "LC16", "LC9", "LC16+LPLC1+LPLC2"] WITH_ALT = {**GOOD, "LC16+LPLC1+LPLC2": {"MDN": 9.0, "DNp09": 1.0, "DNp01": 30.0}} def test_tasks_cover_every_full_stimulus_set_a_only(): full = _data(ALL, WITH_ALT) t = cu.tasks(full) # 4 stimuli x 5 rates x 30, plus 20 control sets x 10 trials for each of 4 sizes assert len(t) == 4 * 5 * 30 + 4 * 20 * 10 and len(set(t)) == len(t) assert ("LC4/100Hz", 29) in t and ("LC4/100Hz", 30) not in t # without the LC16 alternative on full, it is not run on the variant either assert len(cu.tasks(_data(["LC4", "LC16", "LC9"], GOOD))) == 3 * 150 + 3 * 200 def test_identical_variant_passes_everything(): full = _data(ALL, WITH_ALT) checks = cu.compare(full, _data(ALL, WITH_ALT)) assert checks and all(c.passed for c in checks) # 4 stimuli x 5 rates x 3 readouts rate checks; one chain-result check per primary # (all pass on full here, so no section 4.3 attempt was judged) assert sum(c.what.endswith("rate") for c in checks) == 60 assert sum(c.rate is None for c in checks) == 3 md = cu.render(full, {"min2": checks}) assert "| min2 | 60/60 |" in md and "**PASS**" in md def test_lost_drive_fails_rate_and_gate_result(): full = _data(ALL, WITH_ALT) weak = {**WITH_ALT, "LC9": {"DNp09": 1.0, "DNp01": 1.0, "MDN": 1.0}} checks = cu.compare(full, _data(ALL, weak)) bad = [c for c in checks if not c.passed] assert any(c.stimulus == "LC9" and c.what == "DNp09 rate" for c in bad) gate = [c for c in bad if c.rate is None] assert [c.what for c in gate] == ["chain result LC9 primary"] assert gate[0].full == "PASS" and gate[0].variant.startswith("FAIL") assert "**FAIL**" in cu.render(full, {"min10": checks}) def _bump(t, key, extra): """Add ``extra`` spikes to the first DNp01 body in trial 0 of ``key``.""" t.c[key]["counts"]["DNp01"][0][0] += extra def test_rate_tolerance_is_max_of_floor_and_one_hz(): full = _data(ALL, WITH_ALT) k = ch.stim_key("LC16", 25.0) # LC16 drives no DNp01 here: floor 0, 1 Hz rule for extra, ok in ((17, True), (19, False)): # 17 spikes / 30 trials / 0.3 s / 2 bodies var = _data(ALL, WITH_ALT) _bump(var, k, extra) (c,) = [c for c in cu.compare(full, var) if c.stimulus == "LC16" and c.rate == 25.0 and c.what == "DNp01 rate"] assert c.passed is ok, c def test_latency_defined_on_one_matrix_only_fails(): full = _data(ALL, WITH_ALT) var = _data(ALL, WITH_ALT) k = ch.stim_key("LC4", 25.0) var.c[k]["first_ms"]["DNp01"] = [None] * 30 lat = [c for c in cu.compare(full, var) if c.stimulus == "LC4" and c.rate == 25.0 and c.what == "DNp01 latency"] assert len(lat) == 1 and not lat[0].passed and "one matrix only" in lat[0].tol def test_noise_floors_are_deterministic_and_order_free(): full = _data(ALL, WITH_ALT) f1 = cu.noise_floors(full, "LC4") k = ch.stim_key("LC4", 100.0) c = full.c[k] rev = np.arange(len(c["trials"]))[::-1] for field in ("trials", "late_readout", "net_stim", "net_late", "net_stim_nonstim", "net_late_nonstim"): c[field] = [c[field][i] for i in rev] for field in ("counts", "first_ms"): c[field] = {r: [v[i] for i in rev] for r, v in c[field].items()} assert repr(cu.noise_floors(full, "LC4")) == repr(f1) assert f1["DNp01"].rate_hz > 0