flysim-malecns / flysim /tests /test_brain.py
DevNexon's picture
flysim-malecns: connectome-v1 release
aecbd27 verified
Raw History Blame Contribute Delete
3.77 kB
"""The brain-view layout: positions packed for a browser client."""
import os
from pathlib import Path
import numpy as np
import pyarrow as pa
import pyarrow.parquet as pq
import pytest
from flysim.live import brain
CONNECTOME = Path(os.environ.get(
"FLYSIM_CONNECTOME", Path(__file__).parents[2] / "data" / "connectome-v1"))
def write_neurons(directory: Path) -> None:
"""A tiny ``neurons.parquet``: the three stimuli (right side), the readouts, others."""
types = ["LC4", "LC16", "LC9", "LC9", "DNp01", "DNp01", "MDN", "MDN", "DNp09", "DNp09",
"other", "other"]
sides = ["R", "R", "R", "L", "L", "R", "L", "R", "L", "R", None, None]
supers = ["visual_projection"] * 4 + ["descending_neuron"] * 6 + ["cb_intrinsic", None]
n = len(types)
xyz = np.array([[10 + i, 100 - i, 1000 + 10 * i] for i in range(n)], dtype=np.float32)
xyz[11] = np.nan
pq.write_table(pa.table({
"idx": np.arange(n), "bodyId": np.arange(500, 500 + n, dtype=np.int64),
"type": types, "somaSide": sides, "superclass": supers,
"sign": np.ones(n, dtype=np.int8),
"x": xyz[:, 0], "y": xyz[:, 1], "z": xyz[:, 2],
}), directory / "neurons.parquet")
def decode(blob: bytes, n: int) -> tuple[np.ndarray, np.ndarray]:
pos = np.frombuffer(blob[: n * 6], "<u2").reshape(n, 3)
return pos, np.frombuffer(blob[n * 6:], np.uint8)
def test_layout_of_a_small_table(tmp_path: Path) -> None:
write_neurons(tmp_path)
lay = brain.build(tmp_path)
m = lay.meta
assert m["n"] == 12 and m["missing"] == 1 and len(lay.blob) == 12 * 7
assert m["bounds"] == {"min": [10.0, 90.0, 1000.0], "max": [20.0, 100.0, 1100.0]}
pos, regions = decode(lay.blob, 12)
assert pos[0].tolist() == [0, brain.SCALE, 0]
assert pos[10].tolist() == [brain.SCALE, 0, brain.SCALE]
assert pos[11].tolist() == [brain.MISSING] * 3
assert regions.tolist() == [0] * 4 + [3] * 6 + [1, 4]
assert m["stimuli"] == {
"LC4": {"targets": [0], "readout": "DNp01", "chain_status": "not_validated"},
"LC16": {"targets": [1], "readout": "MDN", "chain_status": "not_validated"},
"LC9": {"targets": [2], "readout": "DNp09", "chain_status": "validated"},
}
assert m["readouts"] == {"DNp01": {"L": [4], "R": [5]}, "MDN": {"L": [6], "R": [7]},
"DNp09": {"L": [8], "R": [9]}}
# The ETag follows the data: a fetched directory's checksums, else the blob.
(tmp_path / "SHA256SUMS").write_text("aa neurons.parquet\n")
assert brain.build(tmp_path).etag != lay.etag
def test_quantization_keeps_positions_within_half_a_step() -> None:
rng = np.random.default_rng(0)
xyz = rng.uniform(0, 1e5, size=(500, 3))
blob, lo, hi = brain.pack(xyz, np.zeros(500, np.uint8))
pos, _ = decode(blob, 500)
back = np.array(lo) + pos / brain.SCALE * (np.array(hi) - np.array(lo))
step = (np.array(hi) - np.array(lo)) / brain.SCALE
assert (np.abs(back - xyz) <= step / 2 + 1e-9).all()
def test_regions() -> None:
assert [brain.region_of(s) for s in ("ol_intrinsic", "visual_centrifugal", "cb_motor",
"vnc_sensory", "ascending_neuron", "ENS", None)] \
== [0, 0, 1, 2, 3, 4, 4]
@pytest.mark.skipif(not (CONNECTOME / "neurons.parquet").exists(), reason="no connectome")
def test_real_connectome_layout() -> None:
lay = brain.build(CONNECTOME)
assert lay.meta["n"] == 165_122 and lay.meta["missing"] == 496
assert len(lay.blob) == 165_122 * 7
stimuli = lay.meta["stimuli"]
assert isinstance(stimuli, dict)
assert {k: len(v["targets"]) for k, v in stimuli.items()} == {"LC4": 55, "LC16": 94,
"LC9": 115}