Skip to content

Commit

Permalink
Fixes #497: tweaks phase for NEST ACSource (#502)
Browse files Browse the repository at this point in the history
* Fixes #497: tweaks phase for NEST ACSource

* Updates test for #497

* Updates test for #497: Excludes simulators as current recording not available in master

* Updates comment and minor change in test for #497

* Updates comment for #497

* Updates NEST phase fix and test for #497

* Updates NEST phase fix, restricts to ACSource
  • Loading branch information
appukuttan-shailesh authored and apdavison committed Jul 17, 2017
1 parent fe125c6 commit 39d01bb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pyNN/nest/standardmodels/electrodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class NestCurrentSource(StandardCurrentSource):
def __init__(self, **parameters):
self._device = nest.Create(self.nest_name)
self.cell_list = []
self.phase_given = 0.0 # required for PR #502
parameter_space = ParameterSpace(self.default_parameters,
self.get_schema(),
shape=(1,))
Expand Down Expand Up @@ -57,6 +58,17 @@ def _delay_correction(self, value):
corrected = max(corrected, 0.0)
return corrected

def _phase_correction(self, start, freq, phase):
"""
Fixes #497 (PR #502)
Tweaks the value of phase supplied to NEST ACSource
so as to remain consistent with other simulators
"""
phase_fix = ( (phase*numpy.pi/180) - (2*numpy.pi*freq*start/1000)) * 180/numpy.pi
phase_fix.shape = (1)
phase_fix = phase_fix.evaluate()[0]
nest.SetStatus(self._device, {'phase': phase_fix})

def set_native_parameters(self, parameters):
parameters.evaluate(simplify=True)
for key, value in parameters.items():
Expand All @@ -74,6 +86,14 @@ def set_native_parameters(self, parameters):
'amplitude_times': times})
elif key in ("start", "stop"):
nest.SetStatus(self._device, {key: self._delay_correction(value)})
if key == "start" and type(self).__name__ == "ACSource":
self._phase_correction(self.start, self.frequency, self.phase_given)
elif key == "frequency":
nest.SetStatus(self._device, {key: value})
self._phase_correction(self.start, self.frequency, self.phase_given)
elif key == "phase":
self.phase_given = value
self._phase_correction(self.start, self.frequency, self.phase_given)
elif not key == "amplitude_times":
nest.SetStatus(self._device, {key: value})

Expand Down
59 changes: 59 additions & 0 deletions test/system/scenarios/test_electrodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,64 @@ def issue487(sim):
assert_true (numpy.isclose(v_step_2_arr[0:int(step_2.times[0]/dt)], v_rest).all())


@register(exclude=["brian", "neuron", "nest"])
def issue497(sim):
"""
This is a test to check that the specified phase for the ACSource is valid
at the specified start time (and not, for example, at t=0 as NEST currently does)
NOTE: This test is currently excluded for all the simulators as the final
current recording implementation is currently unavailable on 'master'.
Approach:
> Two signals with different initial specified phases
> 'start' of one signal updated on the fly
> 'frequency' of other signal updated on the fly
> Test to ensure that initial specified phases applicable at t = start
"""
sim_dt = 0.1
sim.setup(min_delay=1.0, timestep = sim_dt)

start1 = 5.0
freq1 = 100.0
phase1 = 0.0
start2 = 5.0
freq2 = 100.0
phase2 = 90.0
amplitude = 1.0

cells = sim.Population(2, sim.IF_curr_exp(v_thresh=-55.0, tau_refrac=5.0))

acsource1 = sim.ACSource(start=start1, stop=20.0, amplitude=amplitude, offset=0.0,
frequency=freq1, phase=phase1)
cells[0].inject(acsource1)
acsource1.record()
acsource2 = sim.ACSource(start=start2, stop=20.0, amplitude=amplitude, offset=0.0,
frequency=freq2, phase=phase2)
cells[1].inject(acsource2)
acsource2.record()

# cannot directly assign/read from electrode variables, as each
# simulator currently returns parameters in different units (see #452)
start1 = 10.0
acsource1.start = start1
freq2 = 20.0
acsource2.frequency = freq2

cells.record('v')
sim.run(25.0)
vm = cells.get_data().segments[0].filter(name="v")[0]
sim.end()
i_t_ac1, i_amp_ac1 = acsource1.get_data()
i_t_ac2, i_amp_ac2 = acsource2.get_data()

# verify that acsource1 has value at t = start as 0 and as non-zero at next dt
assert_true (abs(i_amp_ac1[int(start1/sim_dt)]) < 1e-9)
assert_true (abs(i_amp_ac1[int(start1/sim_dt)+1]) > 1e-9)
# verify that acsources has value at t = start as 'amplitude'
assert_true (abs(i_amp_ac2[int(start2/sim_dt)]-amplitude) < 1e-9)


if __name__ == '__main__':
from pyNN.utility import get_simulator
sim, args = get_simulator()
Expand All @@ -381,3 +439,4 @@ def issue487(sim):
issue451(sim)
issue483(sim)
issue487(sim)
issue497(sim)

0 comments on commit 39d01bb

Please sign in to comment.