Skip to content

fix(examples): repair 6 broken example scripts (API drift, data path, channels)#858

Merged
chaoming0625 merged 1 commit into
masterfrom
worktree-examples-fix
Jun 19, 2026
Merged

fix(examples): repair 6 broken example scripts (API drift, data path, channels)#858
chaoming0625 merged 1 commit into
masterfrom
worktree-examples-fix

Conversation

@chaoming0625

@chaoming0625 chaoming0625 commented Jun 19, 2026

Copy link
Copy Markdown
Member

Summary

Ran a crash-safe, strictly-sequential triage of all 24 examples/ scripts
(isolated subprocess each, ulimit -v capped, thread-capped, headless Agg,
OS timeout, PYTHONPATH=repo so imports resolve to this source). Result:
14 already pass, 6 fixed here, 4 unchanged (need optional deps or
exceed the time budget).

Fixes (all verified green)

Example Bug Fix
integrate_flax_into_brainpy bp.layers.GRUCell removed bp.dyn.GRUCell
whole_brain_simulation_with_fhn dead ../../tests/simulation/data/hcp.npz path load bundled brainpy/dyn/rates/data/hcp.npz via package dir
whole_brain_simulation_with_sl_oscillator same dead hcp.npz path same
Song_2016_EI_RNN RandomState(seed=...) kwarg renamed → positional RandomState(seed)
highdim_RNN_Analysis same RandomState(seed=...) same
mnist_ResNet first conv hardcoded in_channels=3 vs 1-channel MNIST → ValueError parameterize in_channels (default 3), pass 1 for MNIST

Verification: heavy scripts smoke-tested (network construction + one forward
pass / 20 ms sim); Song_2016_EI_RNN (95% acc) and highdim_RNN_Analysis
(fixed-point search) run to completion.

Unchanged (documented, not bugs)

  • reservoir-mnist — functional but ~11 min of training (exceeds the 10 min sandbox timeout).
  • integrate_brainpy_into_flax-lif, integrate_brainpy_into_flax-convlstm — require tensorflow_datasets.
  • spikebased_bp_for_cifar10 — requires torch.

Summary by Sourcery

Update several example scripts to align with current BrainPy APIs, bundled data locations, and dataset channel configurations.

Bug Fixes:

  • Load HCP connectome data in whole brain simulation examples from the bundled BrainPy package path instead of a removed test-data path.
  • Adjust the ResNet example to parameterize input channels and use a single-channel configuration for MNIST to avoid shape errors.
  • Update RNN-related examples to construct RandomState with a positional seed argument compatible with the current BrainPy random API.
  • Switch the Flax integration example to use the current GRUCell location in BrainPy's dynamical systems module.

… channels)

Triaged all 24 examples/ scripts. 14 already pass; the following 6 are fixed
and verified (construction + forward pass / short sim, or full run):

- integrate_flax_into_brainpy: bp.layers.GRUCell -> bp.dyn.GRUCell (relocated)
- whole_brain_simulation_with_fhn / _sl_oscillator: load the bundled
  hcp.npz from the brainpy package (the old ../../tests/simulation/data
  path no longer exists; the file lives at brainpy/dyn/rates/data/hcp.npz)
- Song_2016_EI_RNN / highdim_RNN_Analysis: RandomState(seed=...) ->
  positional (the kwarg was renamed to seed_or_key)
- mnist_ResNet: parameterize ResNet in_channels and pass 1 for 1-channel
  MNIST (was hardcoded to 3, raising a channel-mismatch ValueError)

The remaining 4 are unchanged: they need optional deps or exceed the sandbox
time budget -- reservoir-mnist (functional but ~11 min of training),
integrate_brainpy_into_flax-{lif,convlstm} (tensorflow_datasets),
spikebased_bp_for_cifar10 (torch).
@sourcery-ai

sourcery-ai Bot commented Jun 19, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Repairs six example scripts so they run against the current BrainPy API and bundled data, including fixing a removed GRUCell symbol, updating HCP connectome data loading to use the installed package path, adjusting RandomState construction to the new API, and parameterizing the ResNet input channels so the MNIST example works with 1-channel images.

Flow diagram for loading bundled HCP connectome data

flowchart LR
    A[Network.__init__] --> B[import brainpy as bp]
    B --> C[get dirname of bp.__file__]
    C --> D[join path dyn/rates/data/hcp.npz]
    D --> E["np.load(hcp_path)"]
    E --> F["bm.asarray(hcp['Cmat'])"]
    E --> G["bm.round(hcp['Dmat'] / signal_speed / bm.dt)"]
Loading

File-Level Changes

Change Details Files
Load bundled HCP connectome data from the installed BrainPy package instead of a hardcoded relative test path.
  • Replace the ../../tests/simulation/data/hcp.npz path with a path constructed from os.path.dirname(bp.file) into dyn/rates/data/hcp.npz.
  • Update comments to explain that hcp.npz is bundled with BrainPy under brainpy/dyn/rates/data.
examples/dynamics_simulation/whole_brain_simulation_with_fhn.py
examples/dynamics_simulation/whole_brain_simulation_with_sl_oscillator.py
Make the ResNet example configurable for input channel count and fix MNIST usage to be 1-channel.
  • Add an in_channels argument (default 3) to the ResNet constructor.
  • Use in_channels when constructing the first Conv2D layer instead of a hardcoded value of 3.
  • Instantiate ResNet18 in main() with in_channels=1 for the MNIST dataset.
examples/training_ann_models/mnist_ResNet.py
Update RandomState construction in RNN-related examples to use the new positional seed API.
  • Change bm.random.RandomState(seed=seed) to bm.random.RandomState(seed) in the high-dimensional RNN analysis example.
  • Change bm.random.RandomState(seed=seed) to bm.random.RandomState(seed) in the Song_2016_EI_RNN example.
examples/dynamics_analysis/highdim_RNN_Analysis.py
examples/dynamics_training/Song_2016_EI_RNN.py
Update the GRU cell reference in the Flax integration example to the current BrainPy location.
  • Replace bp.layers.GRUCell with bp.dyn.GRUCell in the integrate_flax_into_brainpy Network definition.
examples/dynamics_training/integrate_flax_into_brainpy.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 chaoming0625 merged commit bc9e23c into master Jun 19, 2026
14 checks passed
@chaoming0625 chaoming0625 deleted the worktree-examples-fix branch June 19, 2026 08:54

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The HCP data path resolution in the two whole-brain examples hardcodes the package layout via os.path.dirname(bp.__file__); consider using a resource-loading helper (e.g., importlib.resources or a BrainPy-provided API) so the examples remain robust to internal package reorganization.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The HCP data path resolution in the two whole-brain examples hardcodes the package layout via `os.path.dirname(bp.__file__)`; consider using a resource-loading helper (e.g., `importlib.resources` or a BrainPy-provided API) so the examples remain robust to internal package reorganization.

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.

@codecov

codecov Bot commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant