Skip to content

fix(dyn): PoissonGroup never spikes due to boolean rand_like dtype#862

Merged
chaoming0625 merged 1 commit into
masterfrom
worktree-fix-poisson-rand-dtype
Jul 8, 2026
Merged

fix(dyn): PoissonGroup never spikes due to boolean rand_like dtype#862
chaoming0625 merged 1 commit into
masterfrom
worktree-fix-poisson-rand-dtype

Conversation

@chaoming0625

@chaoming0625 chaoming0625 commented Jul 8, 2026

Copy link
Copy Markdown
Member

Problem

PoissonGroup never emitted spikes. In PoissonGroup.update, the random draw used
rand_like(self.spike.value). Since spike is boolean-typed and rand_like inherits the
input dtype, the draw was boolean (≈ all True), so the comparison True <= prob was always
False and the group never fired.

Because both examples/dynamics_simulation/decision_making_network.py and
examples/dynamics_simulation/stdp.py are driven entirely by PoissonGroup, the decision
network was silent (0 Hz in every population) and STDP weights stayed frozen.

Fix

Force a real uniform draw with dtype=float:

spikes = bm.random.rand_like(self.spike.value, dtype=float) <= (self.freqs * share['dt'] / 1000.)

Tests

The existing test_PoissonGroup only asserted output shape, which is why the regression
slipped through. Added:

  • test_PoissonGroup_fires_at_expected_rate — asserts empirical rate ≈ freqs (0 Hz on old code).
  • test_PoissonGroup_fires_in_batching_mode — covers batched shape preservation + rate.

Verification

  • input_test.py + STDP_test.py: 139 passed.
  • Decision network: Group A wins at 41.8 Hz (persistent delay activity), Group B suppressed to 0.4 Hz.
  • STDP: max|ΔW| over 10 s = 0.0032 (weights now evolve).

Summary by Sourcery

Ensure PoissonGroup emits spikes by fixing its random draw dtype and strengthen tests to verify firing rates, including in batching mode.

Bug Fixes:

  • Fix PoissonGroup spike generation by forcing uniform random draws to use a floating-point dtype instead of inheriting the boolean spike dtype.

Tests:

  • Add tests asserting PoissonGroup fires at the expected rate over time.
  • Add a batching-mode PoissonGroup test verifying both batched spike shape and firing rate.

PoissonGroup.update drew random values with rand_like(self.spike.value).
Since spike is boolean-typed and rand_like inherits the input dtype, the
draw was boolean (approximately all True), so the spike-probability
comparison was always False and the group never fired. This silenced every
network driven by PoissonGroup, including the decision_making_network and
stdp simulation examples.

Force a real uniform draw with dtype=float. Add firing-rate regression
tests (default and batching mode); the previous shape-only test missed this.
@sourcery-ai

sourcery-ai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Reviewer's Guide

Fixes PoissonGroup so it actually emits spikes by forcing the random draw to use a float dtype, and adds regression tests that verify both firing rate and batched behavior.

File-Level Changes

Change Details Files
Ensure PoissonGroup uses a float uniform random draw so spike probability comparison behaves correctly.
  • Change PoissonGroup.update() to call bm.random.rand_like with dtype=float instead of inheriting the boolean spike dtype.
  • Document in a comment why dtype=float is required and how the previous boolean draw suppressed spikes.
  • Simplify update() by removing commented-out jax.debug.print lines.
brainpy/dyn/others/input.py
Add regression tests that verify PoissonGroup actually fires at the configured rate, including in batching mode.
  • Import numpy and brainpy.math in the PoissonGroup test module to compute empirical firing rates and control RNG seeding.
  • Add test_PoissonGroup_fires_at_expected_rate to assert empirical firing rate is close to freqs over a 1 s simulation.
  • Add test_PoissonGroup_fires_in_batching_mode to verify batched PoissonGroup preserves shape and achieves the expected rate across batch and neuron dimensions.
brainpy/dyn/others/input_test.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

@github-actions github-actions Bot added the tests label Jul 8, 2026
@chaoming0625 chaoming0625 merged commit 09ebc94 into master Jul 8, 2026
11 of 13 checks passed
@chaoming0625 chaoming0625 deleted the worktree-fix-poisson-rand-dtype branch July 8, 2026 09:42

@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 reviewed your changes and they look great!


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 Jul 8, 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