Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions brainpy/dyn/others/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,11 @@ def __init__(
self.reset_state(self.mode)

def update(self):
spikes = bm.random.rand_like(self.spike.value) <= (self.freqs * share['dt'] / 1000.)
# ``dtype=float`` is required: ``self.spike`` is boolean, and ``rand_like``
# defaults to the input dtype. Without it the draw is boolean (~all True),
# making the ``<= prob`` comparison always False so the group never fires.
spikes = bm.random.rand_like(self.spike.value, dtype=float) <= (self.freqs * share['dt'] / 1000.)
spikes = bm.asarray(spikes, dtype=self.spk_type)
# import jax
# jax.debug.print('PoissonGroup: freqs = {f}, spikes = {s}', f=self.freqs, s=spikes)
self.spike.value = spikes
return spikes

Expand Down
35 changes: 35 additions & 0 deletions brainpy/dyn/others/input_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import numpy as np
from absl.testing import parameterized

import brainpy as bp
import brainpy.math as bm
from brainpy.dyn.others import input


Expand All @@ -35,3 +37,36 @@ def test_PoissonGroup(self):
progress_bar=False)
runner.run(30.)
self.assertTupleEqual(runner.mon['spike'].shape, (300, 2))

def test_PoissonGroup_fires_at_expected_rate(self):
# Regression: PoissonGroup must actually emit spikes at ~freqs Hz.
# A dtype bug (boolean rand_like) previously made it fire at 0 Hz while
# still returning the correct output shape, so a shape-only check missed it.
bm.random.seed(1234)
freqs = 200. # Hz
num = 200
duration = 1000. # ms
model = input.PoissonGroup(size=num, freqs=freqs)
runner = bp.DSRunner(model, monitors=['spike'], progress_bar=False)
runner.run(duration)
spikes = np.asarray(runner.mon['spike'])
empirical_rate = spikes.sum() / num / (duration / 1000.)
# Poisson counting noise here is < 1%; 15% tolerance is safe yet still
# rejects the 0 Hz regression by a wide margin.
self.assertAlmostEqual(empirical_rate, freqs, delta=0.15 * freqs)

def test_PoissonGroup_fires_in_batching_mode(self):
# The float draw must preserve the batched shape of ``spike`` and still
# produce the expected rate across the batch axis.
bm.random.seed(1234)
freqs = 200. # Hz
num, batch = 30, 4
duration = 1000. # ms
model = input.PoissonGroup(size=num, freqs=freqs, mode=bm.BatchingMode(batch))
runner = bp.DSRunner(model, monitors=['spike'], progress_bar=False)
runner.run(duration)
spikes = np.asarray(runner.mon['spike']) # (batch, time, num)
self.assertEqual(spikes.shape[0], batch)
self.assertEqual(spikes.shape[-1], num)
empirical_rate = spikes.sum() / (batch * num) / (duration / 1000.)
self.assertAlmostEqual(empirical_rate, freqs, delta=0.15 * freqs)
Loading