Skip to content

Commit fc8fa54

Browse files
committed
Improve code style and correct copyright header
1 parent f8f0511 commit fc8fa54

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

models/ou_noise_generator.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ Description
5050
+++++++++++
5151
5252
The `ou_noise_generator` can be used to inject a temporally correlated noise current into a node.
53-
The current I(t) follows an Ornstein-Uhlenbeck (OU) process, which is described by the following stochastic differential equation:
53+
The current I(t) follows an Ornstein-Uhlenbeck (OU) process, which is described by the following stochastic differential
54+
equation:
5455
5556
.. math::
5657
@@ -62,13 +63,18 @@ The current I(t) follows an Ornstein-Uhlenbeck (OU) process, which is described
6263
- :math:`\sigma_{stat}` is the stationary standard deviation of the process (`std` parameter).
6364
- :math:`dW` is a Wiener process (Gaussian white noise).
6465
65-
The generator integrates this process at a user-defined interval `dt` and delivers the resulting current to its targets. A larger time constant :math:`\tau` results in a more slowly varying noise signal.
66+
The generator integrates this process at a user-defined interval `dt` and delivers the resulting current to its targets.
67+
A larger time constant :math:`\tau` results in a more slowly varying noise signal.
6668
67-
All targets of a noise generator receive different, independent noise currents, but the currents for all targets are updated at the same points in time. The interval `dt` between updates must be a multiple of the simulation time step.
69+
All targets of a noise generator receive different, independent noise currents, but the currents for all targets are
70+
updated at the same points in time. The interval `dt` between updates must be a multiple of the simulation time step.
6871
6972
.. admonition:: Recording the generated current
7073
71-
You can use a :doc:`multimeter <multimeter>` to record the average current sent to all targets for each time step if simulating on a single thread; multiple MPI processes with one thread each also work. In this case, the recording interval of the multimeter should be equal to the `dt` of the generator to avoid aliasing effects. In multi-threaded mode, recording of noise currents is prohibited for technical reasons.
74+
You can use a :doc:`multimeter <multimeter>` to record the average current sent to all targets for each time step if
75+
simulating on a single thread; multiple MPI processes with one thread each also work. In this case, the recording
76+
interval of the multimeter should be equal to the `dt` of the generator to avoid aliasing effects. In multi-threaded
77+
mode, recording of noise currents is prohibited for technical reasons.
7278
7379
7480
.. include:: ../models/stimulation_device.rst

testsuite/pytests/sli2py_stimulating/test_ou_noise_generator.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# test_noise_generator.py
3+
# test_ou_noise_generator.py
44
#
55
# This file is part of NEST.
66
#
@@ -34,10 +34,12 @@ def prepare_kernel():
3434
nest.ResetKernel()
3535
nest.resolution = 0.1
3636

37+
3738
def burn_in_start(dt, tau, k=10):
3839
"""Return number of steps to discard for a k*tau burn-in."""
3940
return int(round((k * tau) / dt))
4041

42+
4143
def test_ou_noise_generator_set_parameters(prepare_kernel):
4244
params = {"mean": 210.0, "std": 60.0, "dt": 0.1}
4345

@@ -54,24 +56,25 @@ def test_ou_noise_generator_incorrect_noise_dt(prepare_kernel):
5456
with pytest.raises(nest.kernel.NESTError, match="StepMultipleRequired"):
5557
nest.Create("ou_noise_generator", {"dt": 0.25})
5658

59+
5760
def test_ou_noise_mean_and_variance(prepare_kernel):
5861
# run for resolution dt=0.1 project to iaf_psc_alpha.
5962
# create 100 repetitions of 1000ms simulations
6063

61-
oung = nest.Create('ou_noise_generator', {'mean':0.0, 'std': 60.0, 'tau':1., 'dt':0.1})
64+
oung = nest.Create("ou_noise_generator", {"mean": 0.0, "std": 60.0, "tau": 1.0, "dt": 0.1})
6265
neuron = nest.Create("iaf_psc_alpha")
63-
66+
6467
# we need to connect to a neuron otherwise the generator does not generate
6568
nest.Connect(oung, neuron)
66-
mm = nest.Create('multimeter', 1, {'record_from':['I'], "interval": 0.1})
67-
nest.Connect(mm, oung, syn_spec={'weight': 1})
69+
mm = nest.Create("multimeter", 1, {"record_from": ["I"], "interval": 0.1})
70+
nest.Connect(mm, oung, syn_spec={"weight": 1})
6871

6972
# Simulate for 100 times
7073
n_sims = 100
7174
ou_current = np.empty(n_sims)
7275
for i in range(n_sims):
7376
nest.Simulate(1000.0)
74-
ou_current[i] = mm.get('events')['I'][-1]
77+
ou_current[i] = mm.get("events")["I"][-1]
7578

7679
curr_mean = np.mean(ou_current)
7780
curr_var = np.var(ou_current)
@@ -88,51 +91,47 @@ def test_ou_noise_generator_autocorrelation(prepare_kernel):
8891
dt = 0.1
8992
tau = 1.0
9093

91-
oung = nest.Create('ou_noise_generator', {'mean':0.0, 'std': 60.0, 'tau':tau,'dt': nest.resolution})
94+
oung = nest.Create("ou_noise_generator", {"mean": 0.0, "std": 60.0, "tau": tau, "dt": nest.resolution})
9295
neuron = nest.Create("iaf_psc_alpha")
93-
96+
9497
# we need to connect to a neuron otherwise the generator does not generate
9598
nest.Connect(oung, neuron)
96-
mm = nest.Create('multimeter', 1, {'record_from':['I'], "interval": dt})
97-
nest.Connect(mm, oung, syn_spec={'weight': 1 })
99+
mm = nest.Create("multimeter", 1, {"record_from": ["I"], "interval": dt})
100+
nest.Connect(mm, oung, syn_spec={"weight": 1})
98101
nest.Simulate(10000.0)
99-
ou_current = mm.get('events')['I']
102+
ou_current = mm.get("events")["I"]
100103

101-
## drop first k*tau
104+
# drop first k*tau
102105
cutoff = burn_in_start(dt=dt, tau=tau)
103106
x = ou_current[cutoff:]
104107
x -= x.mean()
105-
108+
106109
# empirical lag-1 autocorrelation
107-
emp_ac1 = np.dot(x[:-1], x[1:]) / ((len(x)-1)*x.var())
110+
emp_ac1 = np.dot(x[:-1], x[1:]) / ((len(x) - 1) * x.var())
108111
# theoretical lag-1: exp(-dt/tau)
109112
theor_ac1 = np.exp(-dt / tau)
110113

111-
assert abs(emp_ac1 - theor_ac1) < 0.05, \
112-
f"autocorr {emp_ac1:.3f} vs theoretical {theor_ac1:.3f}"
113-
114+
assert abs(emp_ac1 - theor_ac1) < 0.05, f"autocorr {emp_ac1:.3f} vs theoretical {theor_ac1:.3f}"
115+
114116

115117
def test_cross_correlation(prepare_kernel):
116118
# two neurons driven by the same OU noise should remain uncorrelated
117119
dt, tau = 0.1, 1.0
118120

119-
ou = nest.Create("ou_noise_generator",
120-
{"mean": 0.0, "std": 50.0, "tau": tau, "dt": dt})
121-
neurons = nest.Create("iaf_psc_alpha", 2,
122-
{"E_L": 0.0, "V_th": 1e9, "C_m": 1.0, "tau_m": tau})
121+
ou = nest.Create("ou_noise_generator", {"mean": 0.0, "std": 50.0, "tau": tau, "dt": dt})
122+
neurons = nest.Create("iaf_psc_alpha", 2, {"E_L": 0.0, "V_th": 1e9, "C_m": 1.0, "tau_m": tau})
123123
nest.Connect(ou, neurons)
124124

125-
mm = nest.Create("multimeter",
126-
params={"record_from": ["V_m"], "interval": dt})
125+
mm = nest.Create("multimeter", params={"record_from": ["V_m"], "interval": dt})
127126
nest.Connect(mm, neurons)
128127

129128
simtime = 50000.0
130129
nest.Simulate(simtime)
131130

132131
ev = mm.get("events")
133132
senders = np.asarray(ev["senders"], int)
134-
times = np.asarray(ev["times"])
135-
vms = np.asarray(ev["V_m"])
133+
times = np.asarray(ev["times"])
134+
vms = np.asarray(ev["V_m"])
136135

137136
# build time array
138137
n_steps = int(round(simtime / dt)) + 1
@@ -150,7 +149,7 @@ def test_cross_correlation(prepare_kernel):
150149
# cross-corr via FFT
151150
corr = fftconvolve(X1, X2[::-1], mode="full")
152151
corr /= np.sqrt(np.dot(X1, X1) * np.dot(X2, X2))
153-
mid = corr.size // 2
152+
mid = corr.size // 2
154153

155154
# maximum absolute cross-corr should be near zero
156-
assert np.max(np.abs(corr[mid:])) < 0.05
155+
assert np.max(np.abs(corr[mid:])) < 0.05

0 commit comments

Comments
 (0)