← Back to Pull requests

Support boolean masks as indices for synaptic variables #1902

Closed
unknown closed this pull request without merging on Oct 27, 2026 21:02 UTC
View on GitHub
unknown unknown commented on Oct 26, 2026 07:38 UTC
Author

Fixes #1844.

Indexing a synaptic variable with a one-dimensional boolean array treated the mask as integer indices. SynapticIndexing.__call__ passed it through np.asarray(...).astype(np.int32), so S.j[S.j >= N_E] returned the values of synapses 0 and 1 over and over, and S.w[mask] = x only changed those two synapses. This is the behaviour mentioned in #1897 as well (S.U[S.U < 0]).

Indexing for neuron groups already converted masks with np.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:

  • New helper boolean_index_to_indices(mask, size) in brian2/groups/group.py. It raises an IndexError unless the mask has exactly one entry per indexed element (same rule as numpy) and returns the indices of the True entries.

  • SynapticIndexing uses it with the number of synapses, so S.w[mask], S.w[mask] = ..., S.j[mask] and S.indices[mask] select the expected synapses.

  • Indexing uses it with the same size as its slice branch (the group size, or the size of the index variable for subgroups).

  • One sentence in the synapses user guide about boolean indices, since the reporter found this unclear.
  • 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_index

  • extra checks in test_neurongroup.py::test_state_variable_access
  • On 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.

    Diff: 296 lines (+222 / -74) across 3 files Reviewer: mstimberg Review duration: 2243m
    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
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 40.0% (MEDIUM) · Higher = larger blast radius
    40.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.8% · Safe to merge
    15.8%