Skip to content

Commit

Permalink
clean up templates and debug print statements (#1083)
Browse files Browse the repository at this point in the history
Co-authored-by: C.A.P. Linssen <charl@turingbirds.com>
  • Loading branch information
clinssen and C.A.P. Linssen authored Jul 16, 2024
1 parent 4087d62 commit e8667f7
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 161 deletions.
3 changes: 2 additions & 1 deletion pynestml/codegeneration/nest_code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ def _get_synapse_model_namespace(self, synapse: ASTModel) -> Dict:

if "paired_neuron" in dir(synapse):
# synapse is being co-generated with neuron
namespace["paired_neuron"] = synapse.paired_neuron.get_name()
namespace["paired_neuron"] = synapse.paired_neuron
namespace["paired_neuron_name"] = synapse.paired_neuron.get_name()
namespace["post_ports"] = synapse.post_port_names
namespace["spiking_post_ports"] = synapse.spiking_post_port_names
namespace["vt_ports"] = synapse.vt_port_names
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ along with NEST. If not, see <http://www.gnu.org/licenses/>.
// Includes from sli:
#include "dictdatum.h"

// uncomment the next line to enable printing of detailed debug information
// #define DEBUG

namespace nest
{
namespace {{names_namespace}}
Expand Down Expand Up @@ -321,12 +324,9 @@ public:
// support for spike archiving

/**
* \fn void get_history(long t1, long t2,
* std::deque<Archiver::histentry__>::iterator* start,
* std::deque<Archiver::histentry__>::iterator* finish)
* return the spike times (in steps) of spikes which occurred in the range
* (t1,t2].
* XXX: two underscores to differentiate it from nest::Node::get_history()
* Return the spike times (in steps) of spikes which occurred in the range (t1,t2].
*
* Two underscores in the name to differentiate it from nest::Node::get_history().
*/
void get_history__( double t1,
double t2,
Expand All @@ -346,61 +346,62 @@ public:
// Getters/setters for state block
// -------------------------------------------------------------------------

{% filter indent(2, True) -%}
{%- for variable_symbol in neuron.get_state_symbols() %}
{%- if not is_delta_kernel(neuron.get_kernel_by_name(variable_symbol.name)) %}
{%- set variable = utils.get_state_variable_by_name(astnode, variable_symbol.get_symbol_name()) %}
{%- include "directives_cpp/MemberVariableGetterSetter.jinja2" %}
{% endif %}
{% endfor %}
{%- endfilter %}
{% filter indent(2, True) -%}
{%- for variable_symbol in neuron.get_state_symbols() %}
{%- if not is_delta_kernel(neuron.get_kernel_by_name(variable_symbol.name)) %}
{%- set variable = utils.get_state_variable_by_name(astnode, variable_symbol.get_symbol_name()) %}
{%- include "directives_cpp/MemberVariableGetterSetter.jinja2" %}
{% endif %}
{% endfor %}
{%- endfilter %}
{%- endif %}

{%- if neuron.get_parameter_symbols()|length > 0 %}
// -------------------------------------------------------------------------
// Getters/setters for parameters
// -------------------------------------------------------------------------

{% filter indent(2, True) -%}
{%- for variable_symbol in neuron.get_parameter_symbols() %}
{%- set variable = utils.get_parameter_variable_by_name(astnode, variable_symbol.get_symbol_name()) %}
{%- include "directives_cpp/MemberVariableGetterSetter.jinja2" %}
{% filter indent(2, True) -%}
{%- for variable_symbol in neuron.get_parameter_symbols() %}
{%- set variable = utils.get_parameter_variable_by_name(astnode, variable_symbol.get_symbol_name()) %}
{%- include "directives_cpp/MemberVariableGetterSetter.jinja2" %}

{% endfor %}
{%- endfilter %}
{% endfor %}
{%- endfilter %}
{%- endif %}

{%- if neuron.get_internal_symbols()|length > 0 %}
{%- if neuron.get_internal_symbols() | length > 0 %}
// -------------------------------------------------------------------------
// Getters/setters for internals
// -------------------------------------------------------------------------

{% filter indent(2, True) -%}
{%- for variable_symbol in neuron.get_internal_symbols() %}
{%- with variable = utils.get_internal_variable_by_name(astnode, variable_symbol.get_symbol_name()) %}
{%- include "directives_cpp/MemberVariableGetterSetter.jinja2" %}

{%- endwith %}
{% endfor %}
{%- endfilter %}
{% filter indent(2, True) -%}
{%- for variable_symbol in neuron.get_internal_symbols() %}
{%- with variable = utils.get_internal_variable_by_name(astnode, variable_symbol.get_symbol_name()) %}
{%- include "directives_cpp/MemberVariableGetterSetter.jinja2" %}
{%- endwith %}
{% endfor %}
{%- endfilter %}
{%- endif %}
{%- if paired_synapse is defined %}

/* getters/setters for variables transferred from synapse */
// -------------------------------------------------------------------------
// Getters/setters for variables transferred from synapse
// -------------------------------------------------------------------------

{%- for var in transferred_variables %}
{%- for var in transferred_variables %}
double get_{{var}}( double t, const bool before_increment = true );
{%- endfor %}
{%- endfor %}
{%- endif %}

// -------------------------------------------------------------------------
// Methods corresponding to event handlers
// -------------------------------------------------------------------------

{% filter indent(2, True) -%}
{%- for blk in neuron.get_on_receive_blocks() %}
{% filter indent(2, True) -%}
{%- for blk in neuron.get_on_receive_blocks() %}
void on_receive_block_{{ blk.get_port_name() }}();
{%- endfor %}
{%- endfor %}
{%- endfilter %}

// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ along with NEST. If not, see <http://www.gnu.org/licenses/>.
{{ synapse.print_comment() }}
**/

//#define DEBUG
// uncomment the next line to enable printing of detailed debug information
// #define DEBUG

namespace nest
{
Expand Down Expand Up @@ -218,8 +219,8 @@ public:
template < typename targetidentifierT >
class {{synapseName}} : public Connection< targetidentifierT >
{
{%- if paired_neuron | length > 0 %}
typedef {{ paired_neuron }} post_neuron_t;
{%- if paired_neuron_name | length > 0 %}
typedef {{ paired_neuron_name }} post_neuron_t;

{% endif %}
{%- if vt_ports is defined and vt_ports|length > 0 %}
Expand Down Expand Up @@ -640,12 +641,12 @@ public:
ConnTestDummyNode dummy_target;
ConnectionBase::check_connection_( dummy_target, s, t, receptor_type );

{%- if paired_neuron is defined %}
{%- if paired_neuron_name is defined %}
try {
dynamic_cast<{{paired_neuron}}&>(t);
dynamic_cast<{{ paired_neuron_name }}&>(t);
}
catch (std::bad_cast &exp) {
std::cout << "wrong type of neuron connected! Synapse '{{synapseName}}' will only work with neuron '{{paired_neuron}}'.\n";
std::cout << "wrong type of neuron connected! Synapse '{{synapseName}}' will only work with neuron '{{ paired_neuron_name }}'.\n";
exit(1);
}
{%- endif %}
Expand Down Expand Up @@ -682,7 +683,7 @@ public:

const double __t_spike = e.get_stamp().get_ms();
#ifdef DEBUG
std::cout << "{{synapseName}}::send(): handling pre spike at t = " << __t_spike << std::endl;
std::cout << "[synapse " << this << "] {{ synapseName }}::send(): handling pre spike at t = " << __t_spike << std::endl;
#endif

{%- if vt_ports is defined and vt_ports|length > 0 %}
Expand All @@ -691,8 +692,8 @@ public:

{%- endif %}
// use accessor functions (inherited from Connection< >) to obtain delay and target
{%- if paired_neuron is not none and paired_neuron|length > 0 %}
{{paired_neuron}}* __target = static_cast<{{paired_neuron}}*>(get_target(tid));
{%- if paired_neuron_name is not none and paired_neuron_name|length > 0 %}
{{ paired_neuron_name }}* __target = static_cast<{{ paired_neuron_name }}*>(get_target(tid));
assert(__target);
{%- else %}
Node* __target = get_target( tid );
Expand All @@ -707,12 +708,12 @@ public:
t_lastspike_ = 0.;
}

{%- if paired_neuron is not none and paired_neuron|length > 0 %}
{%- if paired_neuron_name is not none and paired_neuron_name|length > 0 %}
double timestep = 0;
{
// get spike history in relevant range (t1, t2] from post-synaptic neuron
std::deque< histentry__{{paired_neuron}} >::iterator start;
std::deque< histentry__{{paired_neuron}} >::iterator finish;
std::deque< histentry__{{ paired_neuron_name }} >::iterator start;
std::deque< histentry__{{ paired_neuron_name }} >::iterator finish;
{%- if vt_ports is defined and vt_ports|length > 0 %}
double t0 = t_last_update_;
{%- endif %}
Expand All @@ -737,8 +738,7 @@ public:
t0 = start->t_ + __dendritic_delay;
{%- endif %}
const double minus_dt = t_lastspike_ - ( start->t_ + __dendritic_delay );
// get_history() should make sure that
// start->t_ > t_lastspike_ - dendritic_delay, i.e. minus_dt < 0
// get_history() should make sure that ``start->t_ > t_lastspike_ - dendritic_delay``, i.e. ``minus_dt < 0``
assert( minus_dt < -kernel().connection_manager.get_stdp_eps() );

if (pre_before_post_update and start->t_ == __t_spike - __dendritic_delay)
Expand Down Expand Up @@ -806,14 +806,16 @@ public:

{%- filter indent(8, True) %}
{%- for pre_port in pre_ports %}
/**
* NESTML generated onReceive code block for presynaptic port "{{pre_port}}" begins here!
**/
/**
* NESTML generated onReceive code block for presynaptic port "{{pre_port}}" begins here!
**/

{%- if synapse.get_on_receive_block(pre_port) %}
{%- set dynamics = synapse.get_on_receive_block(pre_port) %}
{%- with ast = dynamics.get_block() %}
{%- include "directives_cpp/Block.jinja2" %}
{%- filter indent(8, True) %}
{%- include "directives_cpp/Block.jinja2" %}
{%- endfilter %}
{%- endwith %}
{%- endif %}
{%- endfor %}
Expand Down Expand Up @@ -905,7 +907,7 @@ void
const {{synapseName}}CommonSynapseProperties& cp )
{
#ifdef DEBUG
std::cout << "\tIn process_{{vt_port}}_spikes_(): t0 = " << t0 << ", t1 = " << t1 << "\n";
std::cout << "[synapse " << this << "] {{ synapseName }}::process_{{ vt_port }}_spikes_(): t0 = " << t0 << ", t1 = " << t1 << "\n";
#endif
// process dopa spikes in (t0, t1]
// propagate weight from t0 to t1
Expand All @@ -916,6 +918,7 @@ void
// propagate up to first dopa spike
update_internal_state_(t0, vt_spikes[ vt_spikes_idx_ + 1 ].spike_time_ - t0, cp );
++vt_spikes_idx_;

/**
* NESTML generated onReceive code block for volume transmitter synaptic port "{{vt_port}}" begins here!
**/
Expand Down Expand Up @@ -959,10 +962,6 @@ void
}
else
{
#ifdef DEBUG
std::cout << "\t\t4: updating internal state from t0 = " << t0 << " to t1 = " << t1 << "\n";
#endif

// no dopamine spikes in (t0, t1]
update_internal_state_( t0, t1 - t0, cp );
}
Expand Down Expand Up @@ -1205,7 +1204,7 @@ inline void
if (timestep < 1E-12)
{
#ifdef DEBUG
std::cout << "\tupdate_internal_state_() called with dt < 1E-12; skipping update\n" ;
std::cout << "[synapse " << this << "] {{ synapseName }}: update_internal_state_() called with dt < 1E-12; skipping update" << std::endl;
#endif
return;
}
Expand All @@ -1214,7 +1213,7 @@ inline void
auto get_t = [t_start](){ return t_start; }; // do not remove, this is in case the predefined time variable ``t`` is used in the NESTML model

#ifdef DEBUG
std::cout<< "\tUpdating internal state: t_start = " << t_start << ", dt = " << timestep << "\n";
std::cout << "[synapse " << this << "] {{ synapseName }}: Updating internal state: t_start = " << t_start << ", dt = " << timestep << std::endl;
#endif

V_.__h = timestep;
Expand Down Expand Up @@ -1268,15 +1267,15 @@ inline void
{
// propagate all state variables in the synapse to time t_trig
#ifdef DEBUG
std::cout << "\n{{synapseName}}::trigger_update_weight(): t = " << t_trig << std::endl;
std::cout << "[synapse " << this << "] {{ synapseName }}::trigger_update_weight(): t = " << t_trig << std::endl;
#endif
// purely dendritic delay
double dendritic_delay = get_delay();

// get spike history in relevant range (t_last_update, t_trig] from postsyn. neuron
std::deque< histentry__{{paired_neuron}} >::iterator start;
std::deque< histentry__{{paired_neuron}} >::iterator finish;
static_cast<{{paired_neuron}}*>(get_target(t))->get_history__( t_last_update_ - dendritic_delay, t_trig - dendritic_delay, &start, &finish );
std::deque< histentry__{{ paired_neuron_name }} >::iterator start;
std::deque< histentry__{{ paired_neuron_name }} >::iterator finish;
static_cast<{{ paired_neuron_name }}*>(get_target(t))->get_history__( t_last_update_ - dendritic_delay, t_trig - dendritic_delay, &start, &finish );

// facilitation due to postsyn. spikes since last update
double t0 = t_last_update_;
Expand All @@ -1291,21 +1290,18 @@ inline void
{%- endfor %}

#ifdef DEBUG
std::cout << "\tprocessing post spike from " << t_last_update_ << " to " << start->t_ + dendritic_delay << std::endl;
std::cout << "[synapse " << this << "] {{ synapseName }}: processing post spike from " << t_last_update_ << " to " << start->t_ + dendritic_delay << std::endl;
#endif

/**
* update synapse internal state from `t_last_update_` to `start->t_`
* update synapse internal state from `t_last_update_` to `start->t_ + dendritic_delay`
**/

update_internal_state_(t_last_update_,
(start->t_ + dendritic_delay) - t_last_update_,
cp);

const double _tr_t = start->t_;
#ifdef DEBUG
std::cout << "\tFacilitating from c = " << S_.c << " (using trace = " << S_.pre_tr << ")";
#endif
{%- filter indent(6, True) %}
{%- if post_ports is defined %}
{%- for post_port in spiking_post_ports %}
Expand All @@ -1321,9 +1317,6 @@ inline void
{%- endif %}
{%- endfilter %}

#ifdef DEBUG
std::cout << " to " << S_.c << std::endl;
#endif
/**
* internal state has now been fully updated to `start->t_ + dendritic_delay`
**/
Expand All @@ -1344,12 +1337,13 @@ inline void
{%- endfor %}

#ifdef DEBUG
//std::cout << "{{synapseName}}::trigger_update_weight(): \tupdating from " << t_lastspike_ << " to " << t_trig + dendritic_delay << std::endl;
std::cout << "[synapse " << this << "] {{ synapseName }}::trigger_update_weight(): \tupdating from " << t_lastspike_ << " to " << t_trig + dendritic_delay << std::endl;
#endif

/* update_internal_state_(t_lastspike_,
t_trig - t_lastspike_,
cp);*/
// N.B. call to update_internal state is not needed; this is already taken care of by ``process_{{vt_port}}_spikes_()``
// update_internal_state_(t_lastspike_,
// t_trig - t_lastspike_,
// cp);

vt_spikes_idx_ = 0;
t_lastspike_ = t_trig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#}
{%- if tracing %}/* generated by {{self._TemplateReference__context.name}} */ {% endif %}
{%- for statement in ast.get_stmts() %}
{%- with stmt = statement %}
{%- filter indent(2) %}
{%- include "directives_cpp/Statement.jinja2" %}
{%- endfilter %}
{%- endwith %}
{%- with stmt = statement %}
{%- include "directives_cpp/Statement.jinja2" %}
{%- endwith %}
{%- endfor %}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
**/
{% if ast.get_args() | length == 0 %}
{#- no parameters -- emit_spike() called from within neuron #}
#ifdef DEBUG
std::cout << "Emitting a spike at t = " << nest::Time(nest::Time::step(origin.get_steps() + lag + 1)).get_ms() << "\n";
#endif
set_spiketime(nest::Time::step(origin.get_steps() + lag + 1));
nest::SpikeEvent se;
nest::kernel().event_delivery_manager.send(*this, se, lag);
Expand Down
Loading

0 comments on commit e8667f7

Please sign in to comment.