Skip to content

Conversation

@chaoming0625
Copy link
Member

@chaoming0625 chaoming0625 commented Dec 25, 2025

Summary by Sourcery

Make minor refactors and formatting cleanups across the codebase while adjusting JIT wrappers for newer JAX versions and adding a basic import test.

Enhancements:

  • Remove deprecated or incompatible JAX JIT arguments from object transform utilities.
  • Tidy imports, whitespace, and formatting across multiple modules for consistency.
  • Adjust linear layer plasticity imports to use the current brainevent API and keep mixin usages co-located with related logic.
  • Introduce a simple top-level test module that exercises importing the main brainpy package.

Tests:

  • Add or tweak tests around control flow utilities, JAX-compiled numpy-style indexers, and integrator error messages, and add a smoke test for main package imports.

@github-actions github-actions bot added the tests label Dec 25, 2025
@sourcery-ai
Copy link

sourcery-ai bot commented Dec 25, 2025

Reviewer's Guide

Refactors BrainPy to be compatible with newer JAX (>=0.8.2) by updating the JIT wrappers, cleaning up imports and style across many modules, and adding a small smoke test entry point, without changing core numerical logic.

Sequence diagram for updated jit wrapper behavior

sequenceDiagram
    participant User as UserCode
    participant BPJit as brainpy_math_object_transform_jit
    participant BSTransform as brainstate_transform
    participant compiled_func as compiled_func

    User->>BPJit: jit(func, static_argnums, static_argnames, donate_argnums, inline, keep_unused, **kwargs)
    activate BPJit

    BPJit->>BPJit: warp_to_no_state_input_output(func)
    BPJit->>BSTransform: jit(wrapped_func, static_argnums, static_argnames, donate_argnums, inline, keep_unused, **kwargs)
    activate BSTransform

    BSTransform-->>BPJit: compiled_func
    deactivate BSTransform

    BPJit-->>User: compiled_func
    deactivate BPJit

    User->>compiled_func: call_compiled(*args, **kwargs)
    activate compiled_func
    compiled_func-->>User: results
    deactivate compiled_func
Loading

Sequence diagram for cls_jit method wrapping

sequenceDiagram
    participant User as UserClassDefinition
    participant BPJit as brainpy_math_object_transform_jit
    participant BSTransform as brainstate_transform

    User->>BPJit: cls_jit(func, static_argnums, static_argnames, inline, keep_unused, **kwargs)
    activate BPJit

    BPJit->>BPJit: wrap func to bind self as first argument
    BPJit->>BPJit: call jit(wrapped_method, static_argnums, static_argnames, donate_argnums, inline, keep_unused, **kwargs)

    BPJit->>BSTransform: jit(wrapped_method, ...)
    activate BSTransform
    BSTransform-->>BPJit: compiled_method
    deactivate BSTransform

    BPJit-->>User: compiled_method (to be attached as bound method)
    deactivate BPJit

    User->>User: attach compiled_method to class instances
Loading

Class diagram for jit and ProgressBar related utilities

classDiagram
    class brainpy_math_object_transform_controls {
        +_convert_progress_bar_to_pbar(progress_bar) brainstate_transform_ProgressBar
    }

    class brainpy_math_object_transform_jit {
        +jit(func, static_argnums, static_argnames, donate_argnums, inline, keep_unused, kwargs) Callable
        +cls_jit(func, static_argnums, static_argnames, inline, keep_unused, kwargs) Callable
    }

    class brainpy_math_object_transform__utils {
        +warp_to_no_state_input_output(func) Callable
    }

    class brainstate_transform_ProgressBar {
        +freq
    }

    class brainstate_transform {
        +jit(func, static_argnums, static_argnames, donate_argnums, inline, keep_unused, kwargs) Callable
    }

    brainpy_math_object_transform_controls ..> brainstate_transform_ProgressBar : uses
    brainpy_math_object_transform_jit ..> brainpy_math_object_transform__utils : uses warp_to_no_state_input_output
    brainpy_math_object_transform_jit ..> brainstate_transform : wraps jit
    brainpy_math_object_transform_controls ..> brainstate_transform : creates ProgressBar instances
Loading

File-Level Changes

Change Details Files
Update JIT transform wrappers to match JAX>=0.8.2 API and remove deprecated parameters.
  • Remove the abstracted_axes parameter from brainpy.math.object_transform.jit.jit and cls_jit signatures and from the underlying brainstate.transform.jit call.
  • Trim unused Optional/Any typing imports that were only needed for abstracted_axes.
brainpy/math/object_transform/jit.py
Normalize imports and module wiring for compatibility and clarity.
  • Reorder imports to group standard library, third-party, and local modules consistently (e.g., brainstate, braintools, brainpy, jax).
  • Adjust several import paths to use new public locations (e.g., brainevent.csr_on_pre and dense_on_pre instead of private modules; move _JointGenericAlias import to brainstate.mixin).
  • Reorder brainpy.mixin and error imports after brainpy/math imports in various dyn/projection, delay, and base modules to avoid circular or ordering issues.
brainpy/dnn/linear.py
brainpy/dyn/ions/base.py
brainpy/inputs/currents.py
brainpy/dyn/projections/align_post.py
brainpy/dynold/synapses/base.py
brainpy/dynsys.py
brainpy/tools/dicts.py
brainpy/connect/base.py
brainpy/connect/custom_conn.py
brainpy/delay.py
brainpy/dyn/base.py
brainpy/dyn/channels/base.py
brainpy/dyn/others/input.py
brainpy/dyn/outs/base.py
brainpy/dyn/projections/align_pre.py
brainpy/dyn/projections/base.py
brainpy/dyn/projections/delta.py
brainpy/dyn/projections/inputs.py
brainpy/dyn/projections/vanilla.py
brainpy/dyn/synapses/abstract_models.py
brainpy/dynold/synapses/learning_rules.py
brainpy/math/object_transform/_utils.py
brainpy/math/object_transform/collectors.py
brainpy/math/surrogate/_utils.py
brainpy/connect/base.py
brainpy/connect/custom_conn.py
brainpy/delay.py
Style and minor behavior-neutral cleanups across math, tests, and deprecated facades.
  • Reindent functions and docstrings to 4-space style in controls and other modules.
  • Normalize spacing in slicing syntax, lambda formatting, and long argument lists in tests and helpers.
  • Remove stray blank lines or trailing comments and add missing newline at EOF in a few init modules and wrappers for deprecated APIs (neurons, channels, layers, rates, synouts, etc.).
  • Keep numpy/jax behavior identical (e.g., only cosmetic change to mgrid/ogrid assertions).
brainpy/math/object_transform/controls.py
brainpy/neurons.py
brainpy/math/object_transform/tests/test_controls.py
brainpy/math/jitconn/event_matvec.py
brainpy/math/tests/test_numpy_ops.py
brainpy/math/__init__.py
brainpy/math/activations.py
brainpy/math/_utils.py
brainpy/math/sparse/utils.py
brainpy/check.py
brainpy/dyn/projections/utils.py
brainpy/math/ndarray.py
brainpy/math/object_transform/tests/test_collector.py
brainpy/tools/others.py
brainpy/visualization.py
brainpy/inputs/tests/test_currents.py
brainpy/integrators/tests/test_joint_eq.py
brainpy/train/back_propagation.py
brainpy/transform.py
brainpy/__init__.py
brainpy/channels.py
brainpy/layers.py
brainpy/synouts.py
brainpy/dynold/__init__.py
brainpy/dynold/experimental/__init__.py
brainpy/integrators/pde/__init__.py
brainpy/math/object_transform/__init__.py
Add a simple top-level smoke test entry point for BrainPy.
  • Introduce brainpy/test_main.py with a test() function that imports brainpy and prints brainpy.state, likely to catch import/initialization regressions under new JAX versions.
brainpy/test_main.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@chaoming0625
Copy link
Member Author

@sourcery-ai title

@sourcery-ai sourcery-ai bot changed the title refactor: comaptible with jax>=0.8.2 Refine JIT wrappers for new JAX and add brainpy import smoke test Dec 25, 2025
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • In brainpy/dyn/projections/utils.py, _get_return() still references bm but the from brainpy import math as bm import was removed, which will cause a NameError at runtime; restore or replace this import.
  • The new brainpy/test_main.py test function only imports and prints brainpy.state without any assertions or checks; consider turning this into an actual smoke test by asserting basic properties of the import instead of printing.
  • Several added if __name__ == '__main__': ... blocks (e.g., in math/object_transform/__init__.py, brainpy/__init__.py, channels.py) simply reference symbols without doing anything and can be removed to avoid dead, confusing code paths.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `brainpy/dyn/projections/utils.py`, `_get_return()` still references `bm` but the `from brainpy import math as bm` import was removed, which will cause a NameError at runtime; restore or replace this import.
- The new `brainpy/test_main.py` test function only imports and prints `brainpy.state` without any assertions or checks; consider turning this into an actual smoke test by asserting basic properties of the import instead of printing.
- Several added `if __name__ == '__main__': ...` blocks (e.g., in `math/object_transform/__init__.py`, `brainpy/__init__.py`, `channels.py`) simply reference symbols without doing anything and can be removed to avoid dead, confusing code paths.

## Individual Comments

### Comment 1
<location> `brainpy/math/object_transform/__init__.py:43-44` </location>
<code_context>

-
-
 if __name__ == '__main__':
     connect
     initialize,  # weight initialization
</code_context>

<issue_to_address>
**suggestion:** The `__main__` block referencing `ProgressBar` appears to be dead code.

This `if __name__ == '__main__':` block has no side effects (the expressions are evaluated and discarded) and won’t run in normal library usage. If it isn’t needed, consider removing it; if it is, replace it with explicit calls or move the relevant logic to import time to make its purpose clear.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +43 to +44
if __name__ == '__main__':
ProgressBar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The __main__ block referencing ProgressBar appears to be dead code.

This if __name__ == '__main__': block has no side effects (the expressions are evaluated and discarded) and won’t run in normal library usage. If it isn’t needed, consider removing it; if it is, replace it with explicit calls or move the relevant logic to import time to make its purpose clear.

@chaoming0625 chaoming0625 changed the title Refine JIT wrappers for new JAX and add brainpy import smoke test Refine JIT wrappers for new JAX for comaptiblity with jax>=0.8.2 Dec 25, 2025
@chaoming0625 chaoming0625 merged commit fc487d1 into master Dec 25, 2025
3 of 14 checks passed
@github-actions github-actions bot added documentation Improvements or additions to documentation dependencies Pull requests that update a dependency file build labels Dec 25, 2025
@chaoming0625 chaoming0625 deleted the compat branch December 25, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants