Skip to content

Commit

Permalink
Use unsigned code
Browse files Browse the repository at this point in the history
  • Loading branch information
rowleya committed Mar 21, 2024
1 parent 9c56873 commit e2454cb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
12 changes: 12 additions & 0 deletions neural_modelling/src/common/maths-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ static REAL macro_arg_1, macro_arg_2, macro_arg_3, macro_arg_4;
#define POW(x, p) pow((x), (p))

#define SQRT(x) sqrt(x)
#define SQRTU(x) sqrt(x)
#define EXP(x) exp(x)
#define EXPU(x) exp(x)
#define LN(x) log(x)
#define ABS(x) fabs(x)

Expand Down Expand Up @@ -127,11 +129,21 @@ typedef unsigned long fract UFRACT;
//! \return The ::REAL-valued square root of the argument
#define SQRT(x) sqrtk(x)

//! \brief This calculates the square-root of the argument
//! \param[in] x: The ::UREAL-valued argument
//! \return The ::UREAL-valued square root of the argument
#define SQRTU(x) sqrtuk(x)

//! \brief This calculates the exponential (to base _e_) of the argument
//! \param[in] x: The ::REAL-valued argument
//! \return The ::REAL-valued exponential of the argument
#define EXP(x) expk(x)

//! \brief This calculates the exponential (to base _e_) of the argument
//! \param[in] x: The ::UREAL-valued argument
//! \return The ::UREAL-valued exponential of the argument
#define EXPU(x) expuk(x)

#if 0
//! \brief This calculates the logarithm (to base _e_) of the argument
//! \param[in] x: The ::REAL-valued argument
Expand Down
16 changes: 8 additions & 8 deletions neural_modelling/src/spike_source/poisson/spike_source_poisson.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ void set_spike_source_rate(uint32_t sub_id, UREAL rate) {

UREAL rate_per_tick = ukbits(
(__U64(bitsuk(rate)) * __U64(bitsulr(ssp_params.seconds_per_tick))) >> 32);
log_info("Setting rate of %u to %KHz (%K per tick)",
log_debug("Setting rate of %u to %KHz (%K per tick)",
sub_id, rate, rate_per_tick);
spike_source_t *spike_source = &source[sub_id];

Expand All @@ -373,22 +373,22 @@ void set_spike_source_rate(uint32_t sub_id, UREAL rate) {
spike_source->mean_isi_ticks = 0;
spike_source->time_to_spike_ticks = 0;
if (rate_per_tick >= ssp_params.fast_rate_per_tick_cutoff) {
spike_source->sqrt_lambda = (UREAL) SQRT(rate_per_tick);
spike_source->sqrt_lambda = SQRTU(rate_per_tick);
spike_source->exp_minus_lambda = UFRACT_CONST(0);
log_info(" fast, sqrt_lambda=%K", spike_source->sqrt_lambda);
log_debug(" fast, sqrt_lambda=%K", spike_source->sqrt_lambda);
} else {
spike_source->exp_minus_lambda = (UFRACT) EXP(-rate_per_tick);
spike_source->exp_minus_lambda = EXPU(rate_per_tick * -1.0k);
spike_source->sqrt_lambda = 0.0K;
log_info(" fast, exp_minus_lambda=%K", (UREAL) spike_source->exp_minus_lambda);
log_debug(" fast, exp_minus_lambda=%K", (UREAL) spike_source->exp_minus_lambda);
}
} else {
if (rate > 0.0K) {
spike_source->mean_isi_ticks =
(uint32_t) ((bitsulk(ts_per_second)) / bitsuk(rate));
log_info(" slow, isi ticks = %u", spike_source->mean_isi_ticks);
(uint32_t) ((bitsuk(ts_per_second)) / bitsuk(rate));
log_debug(" slow, isi ticks = %u", spike_source->mean_isi_ticks);
} else {
spike_source->mean_isi_ticks = 0;
log_info(" slow, isi ticks = 0");
log_debug(" slow, isi ticks = 0");
}

spike_source->exp_minus_lambda = UFRACT_CONST(0);
Expand Down
2 changes: 1 addition & 1 deletion spynnaker/pyNN/models/spike_source/spike_source_poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SpikeSourcePoisson(AbstractPyNNModel):

default_population_parameters = _population_parameters

def __init__(self, rate: Union[float, Sequence[float]] = 1.0,
def __init__(self, rate: Union[float, Sequence[float]] = 0.0,
start: Union[int, Sequence[int]] = 0,
duration: Union[int, Sequence[int], None] = None):
self.__start = start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@

class TestPoissonSpikeSource(BaseTestCase):

def check_spikes(self, n_neurons, input, expected):
neo = input.get_data("spikes")
def check_spikes(self, n_neurons, neo, expected):
spikes = neo.segments[0].spiketrains
count = sum(len(s) for s in spikes)
tolerance = math.sqrt(expected)
print(expected, float(count) / float(n_neurons))
self.assertAlmostEqual(expected, float(count) / float(n_neurons),
delta=tolerance,
msg="Error on {}".format(input.label))
delta=tolerance)

def recording_poisson_spikes(self, run_zero):
sim.setup(timestep=1.0, min_delay=1.0)
Expand All @@ -48,20 +45,21 @@ def recording_poisson_spikes(self, run_zero):

pop_1 = sim.Population(
n_neurons, sim.IF_curr_exp, cell_params_lif, label='pop_1')
input = sim.Population(
n_neurons, sim.SpikeSourcePoisson, {}, label='inputSpikes_1')
ssp = sim.Population(
n_neurons, sim.SpikeSourcePoisson(rate=1), label='inputSpikes_1')

sim.Projection(input, pop_1, sim.OneToOneConnector())
sim.Projection(ssp, pop_1, sim.OneToOneConnector())

input.record("spikes")
ssp.record("spikes")

if run_zero:
sim.run(0)
sim.run(5000)
self.check_spikes(n_neurons, input, 5)

neo = ssp.get_data("spikes")
sim.end()

self.check_spikes(n_neurons, neo, 5)

def recording_poisson_spikes_no_zero(self):
self.recording_poisson_spikes(False)

Expand Down Expand Up @@ -91,18 +89,19 @@ def recording_poisson_spikes_big(self):

pop_1 = sim.Population(
n_neurons, sim.IF_curr_exp, cell_params_lif, label='pop_1')
input = sim.Population(
n_neurons, sim.SpikeSourcePoisson, {}, label='inputSpikes_1')
ssp = sim.Population(
n_neurons, sim.SpikeSourcePoisson(rate=1), label='inputSpikes_1')

sim.Projection(input, pop_1, sim.OneToOneConnector())
sim.Projection(ssp, pop_1, sim.OneToOneConnector())

input.record("spikes")
ssp.record("spikes")

sim.run(5000)
self.check_spikes(n_neurons, input, 5)

neo = ssp.get_data("spikes")
sim.end()

self.check_spikes(n_neurons, neo, 5)

def test_recording_poisson_spikes_big(self):
self.runsafe(self.recording_poisson_spikes_big)

Expand All @@ -124,36 +123,41 @@ def recording_poisson_spikes_rate_0(self):

pop_1 = sim.Population(
n_neurons, sim.IF_curr_exp, cell_params_lif, label='pop_1')
input = sim.Population(
n_neurons, sim.SpikeSourcePoisson, {'rate': 0}, label='input')
ssp = sim.Population(
n_neurons, sim.SpikeSourcePoisson, {'rate': 0}, label='ssp')

sim.Projection(input, pop_1, sim.OneToOneConnector())
sim.Projection(ssp, pop_1, sim.OneToOneConnector())

input.record("spikes")
ssp.record("spikes")

sim.run(5000)
self.check_spikes(n_neurons, input, 0)

neo = ssp.get_data("spikes")
sim.end()

self.check_spikes(n_neurons, neo, 0)

def test_recording_poisson_spikes_rate_0(self):
self.runsafe(self.recording_poisson_spikes_rate_0)

def check_rates(self, rates, seconds, seed):
n_neurons = 100
sim.setup(timestep=1.0)
inputs = {}
ssps = {}
for rate in rates:
input = sim.Population(
ssp = sim.Population(
n_neurons, sim.SpikeSourcePoisson(rate),
label='inputSpikes_{}'.format(rate),
additional_parameters={"seed": seed})
input.record("spikes")
inputs[rate] = input
ssp.record("spikes")
ssps[rate] = ssp
sim.run(seconds * 1000)
spikes = {}
for rate in rates:
self.check_spikes(n_neurons, inputs[rate], rate*seconds)
neo = ssps[rate].get_data("spikes")
spikes[rate] = neo
sim.end()
for rate in rates:
self.check_spikes(n_neurons, spikes[rate], rate*seconds)

def recording_poisson_spikes_rate_fast(self):
self.check_rates(
Expand Down Expand Up @@ -259,3 +263,16 @@ def poisson_multi_run_change_rate(self):

def test_poisson_multi_run_change_rate(self):
self.runsafe(self.poisson_multi_run_change_rate)

def poisson_higher_rate(self):
sim.setup(timestep=1.0)
pop_src = sim.Population(
100, sim.SpikeSourcePoisson(rate=1000), label="src")
pop_src.record("spikes")
sim.run(10000)
neo = pop_src.get_data("spikes")
sim.end()
self.check_spikes(100, neo, 10000)

def test_poisson_higher_rate(self):
self.runsafe(self.poisson_higher_rate)

0 comments on commit e2454cb

Please sign in to comment.