-
Notifications
You must be signed in to change notification settings - Fork 242
Added before_run codeblock in numpy and C++ templates #1603
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,25 +1,48 @@ | ||||||
{# USES_VARIABLES { _spikespace, neuron_index, _timebins, _period_bins, _lastindex, t_in_timesteps } #} | ||||||
{% extends 'common_group.py_' %} | ||||||
{% extends 'common_group.py.jinja2' %} | ||||||
|
||||||
{% block before_code %} | ||||||
# Copy of the SpikeGeneratorGroup.before_run code | ||||||
dt = {{dt.item()}} # Always copy dt | ||||||
period = {{period_}} # Always copy period | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These variables are not available to the template, since it does not know that they are needed. You can use |
||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
# Always recalculate timesteps | ||||||
from brian2 import defaultclock # Use Brian 2's clock instead of 't' | ||||||
current_t = defaultclock.t | ||||||
timesteps = ({{_spike_time}} / dt).astype(np.int32) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should use |
||||||
current_step = int(current_t / dt) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The names used here have to be the names as added with |
||||||
|
||||||
# Always update _lastindex | ||||||
in_the_past = np.nonzero(timesteps < current_step)[0] | ||||||
if len(in_the_past): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the Python templates,
Suggested change
|
||||||
{{_lastindex}}[0] = in_the_past[-1] + 1 | ||||||
else: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For scalar variables, Brian already applies the
Suggested change
|
||||||
{{_lastindex}}[0] = 0 | ||||||
|
||||||
# Always recalculate _timebins | ||||||
shift = 1e-3 * dt | ||||||
timebins = np.asarray(({{_spike_time}} + shift) / dt, dtype=np.int32) | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{{_timebins}}[:] = timebins | ||||||
|
||||||
# Always recalculate period_bins (ignore limit checks) | ||||||
period_bins = int(round(period / dt)) | ||||||
{{_period_bins}}[0] = period_bins | ||||||
{% endblock %} | ||||||
|
||||||
{% block maincode %} | ||||||
_the_period = {{_period_bins}} | ||||||
_timebin = {{t_in_timesteps}} | ||||||
_timebin = int(defaultclock.t / {{dt.item()}}) # Use Brian 2's clock instead of 't_in_timesteps' | ||||||
_n_spikes = 0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment about |
||||||
|
||||||
_lastindex_before = {{_lastindex}} | ||||||
|
||||||
if _the_period > 0: | ||||||
_timebin %= _the_period | ||||||
# If there is a periodicity in the SpikeGenerator, we need to reset the | ||||||
# lastindex when the period has passed | ||||||
if _lastindex_before > 0 and {{_timebins}}[_lastindex_before - 1] >= _timebin: | ||||||
_lastindex_before = 0 | ||||||
# Always reset _lastindex if period is applied | ||||||
_timebin %= _the_period | ||||||
_lastindex_before = 0 | ||||||
|
||||||
_n_spikes = _numpy.searchsorted({{_timebins}}[_lastindex_before:], | ||||||
_timebin, side='right') | ||||||
_n_spikes = _numpy.searchsorted({{_timebins}}, _timebin, side='right') | ||||||
|
||||||
{{_lastindex}} = _lastindex_before + _n_spikes | ||||||
|
||||||
_indices = {{neuron_index}}[_lastindex_before:_lastindex_before+_n_spikes] | ||||||
|
||||||
{{_spikespace}}[:_n_spikes] = _indices | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might have been better to call templates like this, but it is currently called
common_group.py_
.