Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test of rates #386

Merged
merged 3 commits into from
Jun 20, 2023
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
14 changes: 10 additions & 4 deletions brainpy/_src/rates/populations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from typing import Union, Callable
import jax

from brainpy import math as bm
from brainpy._src.context import share
Expand All @@ -18,7 +19,6 @@
from brainpy.types import Shape, ArrayType

__all__ = [
'RateModel',
'FHN',
'FeedbackFHN',
'QIF',
Expand Down Expand Up @@ -350,7 +350,7 @@ def __init__(
# integral
self.integral = odeint(method=method,
f=JointEq([self.dx, self.dy]),
state_delays={'V': self.x_delay})
state_delays={'x': self.x_delay})

def reset_state(self, batch_size=None):
self.x.value = variable(self._x_initializer, batch_size, self.varshape)
Expand Down Expand Up @@ -1053,13 +1053,19 @@ def update(self, x1=None, x2=None):
input_i = x2 if (x2 is not None) else 0.

de = -self.e + self.beta_e * bm.maximum(input_e, 0.)
if bm.any(self.noise_e != 0.):
with jax.ensure_compile_time_eval():
has_noise = bm.any(self.noise_e != 0.)

if has_noise:
de += bm.random.randn(self.varshape) * self.noise_e
de = de / self.tau_e
self.e.value = bm.maximum(self.e + de * dt, 0.)

di = -self.i + self.beta_i * bm.maximum(input_i, 0.)
if bm.any(self.noise_i != 0.):
with jax.ensure_compile_time_eval():
has_noise = bm.any(self.noise_i != 0.)

if has_noise:
di += bm.random.randn(self.varshape) * self.noise_i
di = di / self.tau_i
self.i.value = bm.maximum(self.i + di * dt, 0.)
Expand Down
85 changes: 85 additions & 0 deletions brainpy/_src/rates/tests/test_rates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-


import brainpy as bp
from absl.testing import parameterized
from brainpy._src.rates import populations
from unittest import TestCase


class TestRate(TestCase):
def test_fhn(self):
fhn = bp.rates.FHN(10)
self.assertTrue(fhn.tau is not None)

def test_ffhn(self):
ffhn = bp.rates.FeedbackFHN(size=1)
self.assertTrue(ffhn.tau is not None)

def test_qif(self):
qif = bp.rates.QIF(size=1)
self.assertTrue(qif.tau is not None)

def test_slo(self):
slo = bp.rates.StuartLandauOscillator(size=1)
self.assertTrue(slo.x_ou_tau is not None)

def test_wcm(self):
wcm = bp.rates.WilsonCowanModel(size=1)
self.assertTrue(wcm.x_ou_tau is not None)

def test_tlm(self):
tlm = bp.rates.ThresholdLinearModel(size=1)
self.assertTrue(tlm.tau_e is not None)


class TestPopulation(parameterized.TestCase):
@parameterized.named_parameters(
{'testcase_name': f'noise_of_{name}', 'neuron': name}
for name in populations.__all__
)
def test_runner(self, neuron):
model = getattr(populations, neuron)(size=10)
runner = bp.DSRunner(model, progress_bar=False)
runner.run(10.)

class TestShape(parameterized.TestCase):
def test_FHN_shape(self):
model = getattr(populations, 'FHN')(size=10)
runner = bp.DSRunner(model,
monitors=['x'],
progress_bar=False)
runner.run(10.)
self.assertTupleEqual(runner.mon.x.shape, (100, 10))

def test_FFHN_shape(self):
model = getattr(populations, 'FeedbackFHN')(size=10)
runner = bp.DSRunner(model,
monitors=['x'],
progress_bar=False)
runner.run(10.)
self.assertTupleEqual(runner.mon.x.shape, (100, 10))

def test_QIF_shape(self):
model = getattr(populations, 'QIF')(size=10)
runner = bp.DSRunner(model,
monitors=['x'],
progress_bar=False)
runner.run(10.)
self.assertTupleEqual(runner.mon.x.shape, (100, 10))

def test_SLO_shape(self):
model = getattr(populations, 'StuartLandauOscillator')(size=10)
runner = bp.DSRunner(model,
monitors=['x'],
progress_bar=False)
runner.run(10.)
self.assertTupleEqual(runner.mon.x.shape, (100, 10))

def test_TLM_shape(self):
model = getattr(populations, 'ThresholdLinearModel')(size=10)
runner = bp.DSRunner(model,
monitors=['e'],
progress_bar=False)
runner.run(10.)
self.assertTupleEqual(runner.mon.e.shape, (100, 10))
Loading