Return copies, not views on C++ dynamic arrays, from x[:] and StateMonitor accessors (regression since 2.10) #1903
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 579 lines diff across 7 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.381
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1903"
general
weight: 0.35
mstimberg
Part 1 of #1897, as agreed there:
x[:]returns a copy for variables stored in a dynamic array, so that the returned array no longer refers to the memory of the C++ vector. Static arrays (part 2) are left unchanged.Changes
brian2/core/variables.py:get_with_index_arraycopies the values when the variable is aDynamicArrayVariableand the values are obtained with a slice ([:],[:, None], or another slice thatindexingpasses on as a slice). Index arrays already returned a copy. Static array variables (e.g.G.v[:]) still return a view.VariableView.__array__no longer copies these values a second time (np.array(S.w)would otherwise copy twice).brian2/monitors/statemonitor.py:mon.var_andmon[k].t/mon[k].t_returned the stored dynamic arrays directly, without going throughVariableView; they now return copies (mon.varwith units already did).brian2/memory/cythondynamicarray.pyx:DynamicArray1DClass.dataandDynamicArray2DClass.dataset the dynamic array as the base of the returned numpy array (cnp.set_array_base), so that code using.datadirectly keeps the owner alive. As you noted in #1791, this does not protect against a reallocation when the array grows.examples/frompapers/Maass_Natschlaeger_Markram_2002.py: replacedsynapses.U[:][synapses.U < 0] = U(and the same forDandF), which changed the synapses through the returned view and no longer would. The rewrite draws the same random numbers in the same order: for a fixed seed,U,DandFof the 680 synapses of an E→E projection are bit-identical to master, while the old lines with this branch would leave 12, 16 and 11 invalid values. (As you mentioned in #1897,synapses.U[synapses.U < 0] = Uwould be the natural replacement, but boolean indices are currently read as integer indices, see #1844.)docs_sphinx/user/models.rst: a note on which variables return a copy with[:]and how to change a subset of values.Tests
test_memory.py::test_dynamic_array_data_keeps_array_alive(codegen independent):.dataof 1D and 2D dynamic arrays has the dynamic array as base and stays valid after the dynamic array is deleted and memory is reallocated.test_monitor.py::test_monitor_values_are_copies: the values returned bySpikeMonitor,StateMonitor(includingmon.v_andmon[0].t) andPopulationRateMonitordo not share memory with the stored arrays, and keep their values after further runs and after the monitors are deleted.test_synapses.py::test_state_variable_get_all_copy:S.w[:]andS.i[:]are copies that survive a furtherconnect, writing into them does not change the synapses, and conditional assignment on the variable itself still works.On master, all five runs (codegen independent, and the two runtime tests with numpy and Cython) fail; with this branch they pass.
Validation
Linux (WSL Ubuntu 24.04), gcc 13.3, Python 3.13.14, numpy 2.5.4, Cython 3.3.0, on master 26d8f10 (2.11.0) with the extensions built from source for each tree. Windows and macOS were not tested locally.
brian2.test(["cython", "numpy"], test_codegen_independent=True), long tests excluded): with this branch, 56 doctests, 452 codegen-independent, 311 Cython and 314 numpy tests pass; on master, 56, 451, 309 and 312 (the difference is the new tests). The only failure,test_base.py::test_version, occurs on both, because the tests ran from a source copy withoutsetuptools_scm.S.w_[:]goes from about 6 µs to the cost of one copy (2.8 to 3.3 ms here), the same order asnp.array(S.w)on master (2.9 to 3.4 ms).np.array(S.w)stays at 2.9 to 4.1 ms, since it still copies once (without the change in__array__it would copy twice). Absolute times on this loaded machine are about three to four times those quoted in #1897.ruff formatandruff checkreport nothing on the changed files underbrian2/(the example file was already not formatted on master and is outside the hook).Not included
Part 2 of #1897: static arrays, read-only variables such as
G.i[:], and the shuffling recipe inrecording.rst.