← Back to Pull requests

Insert numpy scalars as plain numbers when substituting values #1909

Closed
unknown closed this pull request without merging on Oct 30, 2026 10:28 UTC
View on GitHub
unknown unknown commented on Oct 29, 2026 01:10 UTC
Author

Substituting a name with a numpy scalar fails with numpy 2:

import numpy as np
from brian2 import *

k = np.exp(-1.0)  # np.float64
Equations("dv/dt = -k*v/ms : 1", k=k)
# SyntaxError: Brian equations/expressions do not support the 'Call' syntax.
Expression("g + k*w", k=k)        # same error
Statements("g += k*w", k=k)       # Statements('g += (np.float64(0.36787944117144233))*w')

All three substitution mechanisms insert "(" + repr(value) + ")" into the code, and since numpy 2 the repr of a numpy scalar is np.float64(0.37) instead of 0.37. Brian requires numpy >= 2.3, so this happens for any value that comes out of numpy: np.exp(...), params[0], arr.mean(), rng.uniform(), and so on. For Statements the bad code is only caught later, when the group is created or run. Quantities were not affected because Quantity.__repr__ already prints a plain number (np.float64(10)*ms works).

The change adds a small value_to_code helper in brian2/equations/codestrings.py that turns numpy scalars into Python scalars with .item() before taking the repr, and uses it in Equations._substitute, Statements._substitute and Expression._substitute. That is the same conversion Brian already does in VariableView.set_item for string indices and in CPPStandaloneDevice.fill_with_array. Arrays are left alone, so Equations(..., tau=np.arange(5)) still raises a SyntaxError as tested in test_wrong_replacements.

Tests

  • test_codestrings.py::test_expr_substitutions: np.float64 and np.int64 values

  • test_statements.py::test_statements_numpy_scalar_substitution: np.float64, np.float32, np.int64 and np.bool_ (exact string comparison, since the existing in checks would also match (np.float64(0.3)))

  • test_equations.py::test_correct_replacements: a numpy scalar, and a quantity built from a numpy scalar
  • Without the change in codestrings.py and equations.py, the new cases fail with the errors above; with it they pass.

    Local runs on macOS arm64, Python 3.13, numpy 2.5.4, through brian2.test(["numpy"], test_codegen_independent=True): doctests 57 passed, codegen-independent 457 passed and 4 skipped, numpy target 305 passed and 15 skipped. ruff check and ruff format --check (v0.14.2, as in the pre-commit config) pass on the changed files. The change only touches code strings before code generation, so I did not run the Cython or standalone targets locally.

    Diff: 306 lines (+229 / -77) across 3 files Reviewer: mstimberg Review duration: 1998m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.40) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 306 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%
    56%
    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.343
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1909"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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.2% (MEDIUM) · Higher = larger blast radius
    40.2%
    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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 17.4% · Safe to merge
    17.4%