Support boolean masks as indices for synaptic variables #1902
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 296 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.341
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1902"
general
weight: 0.35
mstimberg
Fixes #1844.
Indexing a synaptic variable with a one-dimensional boolean array treated the mask as integer indices.
SynapticIndexing.__call__passed it throughnp.asarray(...).astype(np.int32), soS.j[S.j >= N_E]returned the values of synapses 0 and 1 over and over, andS.w[mask] = xonly changed those two synapses. This is the behaviour mentioned in #1897 as well (S.U[S.U < 0]).Indexingfor neuron groups already converted masks withnp.nonzero, but it did not check the length, so a mask that was too short silently selected a subset (G.i[np.array([True, False, True])]gives[0, 2]for a group of 50 neurons).Changes:
boolean_index_to_indices(mask, size)inbrian2/groups/group.py. It raises anIndexErrorunless the mask has exactly one entry per indexed element (same rule as numpy) and returns the indices of theTrueentries.SynapticIndexinguses it with the number of synapses, soS.w[mask],S.w[mask] = ...,S.j[mask]andS.indices[mask]select the expected synapses.Indexinguses it with the same size as its slice branch (the group size, or the size of the index variable for subgroups).The length check is a small behaviour change for neuron groups: a mask of the wrong length now raises instead of being accepted. I think that matches what people expect from numpy, but I can drop that part if you prefer to keep this PR to synapses only.
Tests:
test_synapses.py::test_state_variable_boolean_indexing(get, set, list of booleans, all-False mask, wrong shapes)test_subgroup.py::test_state_variables_boolean_indextest_neurongroup.py::test_state_variable_accessOn unchanged master the synapse test fails (
S.j[mask]gives[0, 9, 0, 9, 0, 9, 0]instead of[9, 8, 9]) and the neuron group length check does not raise. The subgroup test already passed on master and is there to cover the subgroup size.Local runs on macOS arm64, Python 3.13, numpy 2.5.4, through
brian2.test(["numpy"], test_codegen_independent=True): doctests 56 passed, codegen-independent 453 passed and 4 skipped, numpy target 304 passed and 15 skipped. pre-commit passes on the changed files. I did not run the Cython or standalone targets locally, and I did not build the docs.