"""Structural check of the three input -> readout chains in MaleCNS v1.0. Counts direct synapses and a crude two-hop estimate (sum over intermediate neurons of in-count x out-count, signed by the intermediate's transmitter) from each stimulated LC population to each descending-neuron readout. This is a pre-simulation sanity check, not a substitute for the chain tests. Usage: python -I connectome_paths.py """ import sys import numpy as np import pandas as pd import pyarrow as pa import pyarrow.compute as pc import pyarrow.feather as pf SOURCES = ["LC4", "LC16", "LC9"] READOUTS = ["DNp01", "MDN", "DNp09"] SIGN = {"acetylcholine": 1, "gaba": -1, "glutamate": -1, "histamine": -1} def main(d: str) -> None: ann = pf.read_table( f"{d}/body-annotations-male-cns-v1.0-minconf-0.5.feather", columns=["bodyId", "type", "status"], ).to_pandas() ann = ann[ann.status == "Traced"] nt = ( pf.read_table( f"{d}/body-neurotransmitters-male-cns-v1.0.feather", columns=["body", "consensus_nt", "predicted_nt"], ) .to_pandas() .set_index("body") ) nt_of = nt.consensus_nt.fillna(nt.predicted_nt) traced = set(ann.bodyId) type_of = ann.set_index("bodyId").type ids = {t: set(ann.bodyId[ann.type == t]) for t in SOURCES + READOUTS} w = pf.read_table( f"{d}/connectome-weights-male-cns-v1.0-minconf-0.5.feather", memory_map=True ) all_src = pa.array(list(set().union(*(ids[t] for t in SOURCES)))) all_dst = pa.array(list(set().union(*(ids[t] for t in READOUTS)))) into = w.filter(pc.is_in(w.column("body_post"), all_dst)).to_pandas() out = w.filter(pc.is_in(w.column("body_pre"), all_src)).to_pandas() into = into[into.body_pre.isin(traced)] out = out[out.body_post.isin(traced)] for s in SOURCES: for t in READOUTS: direct = into[into.body_pre.isin(ids[s]) & into.body_post.isin(ids[t])].weight.sum() o = out[out.body_pre.isin(ids[s])].groupby("body_post").weight.sum() i = into[into.body_post.isin(ids[t])].groupby("body_pre").weight.sum() mids = o.index.intersection(i.index) sg = np.array([SIGN.get(str(nt_of.get(m, "")).lower(), 0) for m in mids]) val = o[mids].values * i[mids].values top = ( pd.Series(val * sg, index=[type_of.get(m, "?") for m in mids]) .groupby(level=0) .sum() .sort_values() ) print( f"{s:5}->{t:6} direct={direct:6} 2hop_mids={len(mids):4} " f"exc={val[sg > 0].sum():.3g} inh={val[sg < 0].sum():.3g} " f"top+={list(top.tail(3).index)} top-={list(top.head(2).index)}" ) if __name__ == "__main__": main(sys.argv[1])