Skip to content
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

Clean up Python code generator #1146

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pynestml/codegeneration/nest_code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,11 @@ def _get_neuron_model_namespace(self, neuron: ASTModel) -> Dict:
namespace["state_vars_that_need_continuous_buffering_transformed_iv"][var_name] = self._nest_printer.print(neuron.get_initial_value(var_name_transformed))
else:
namespace["state_vars_that_need_continuous_buffering"] = []
namespace["extra_on_emit_spike_stmts_from_synapse"] = neuron.extra_on_emit_spike_stmts_from_synapse
if "extra_on_emit_spike_stmts_from_synapse" in dir(neuron):
namespace["extra_on_emit_spike_stmts_from_synapse"] = neuron.extra_on_emit_spike_stmts_from_synapse
namespace["paired_synapse"] = neuron.paired_synapse
namespace["paired_synapse_original_model"] = neuron.paired_synapse_original_model
if "paired_synapse_original_model" in dir(neuron):
namespace["paired_synapse_original_model"] = neuron.paired_synapse_original_model
namespace["paired_synapse_name"] = neuron.paired_synapse.get_name()
namespace["post_spike_updates"] = neuron.post_spike_updates
namespace["transferred_variables"] = neuron._transferred_variables
Expand Down
4 changes: 3 additions & 1 deletion pynestml/codegeneration/nest_desktop_code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.
import os

from typing import Sequence, Optional, Mapping, Any, Dict

from pynestml.codegeneration.code_generator import CodeGenerator
Expand Down Expand Up @@ -62,8 +62,10 @@ def _get_neuron_model_namespace(self, neuron: ASTModel) -> Dict:
:return: a map from name to functionality.
"""
from pynestml.codegeneration.nest_tools import NESTTools

namespace = dict()
namespace["neuronName"] = neuron.get_name()
namespace["neuron"] = neuron
namespace["parameters"] = NESTTools.get_neuron_parameters(neuron.get_name())

return namespace
14 changes: 13 additions & 1 deletion pynestml/codegeneration/printers/python_variable_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,20 @@ def print_variable(self, variable: ASTVariable) -> str:
"""
assert isinstance(variable, ASTVariable)

# print external variables (such as a variable in the synapse that needs to call the getter method on the postsynaptic partner)
if isinstance(variable, ASTExternalVariable):
raise Exception("Python-standalone target does not support synapses")
_name = str(variable)
if variable.get_alternate_name():
if not variable._altscope:
# get the value from the postsynaptic partner continuous-time buffer (for post_connected_continuous_input_ports); this has been buffered in a local temp variable starting with "__"
return variable.get_alternate_name()

# get the value from the postsynaptic partner (without time specified)
# the disadvantage of this approach is that the time the value is to be obtained is not explicitly specified, so we will actually get the value at the end of the min_delay timestep
return "__target.get_" + variable.get_alternate_name() + "()"

# grab the value from the postsynaptic spiking history buffer
return "start.get_" + _name + "()"

if variable.get_name() == PredefinedVariables.E_CONSTANT:
return "math.e"
Expand Down
10 changes: 7 additions & 3 deletions pynestml/codegeneration/python_standalone_code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ class PythonStandaloneCodeGenerator(NESTCodeGenerator):
"templates": {
"path": "resources_python_standalone/point_neuron",
"model_templates": {
"neuron": ["@[email protected]"]
"neuron": ["@[email protected]"],
"synapse": ["@[email protected]"]
},
"module_templates": ["simulator.py.jinja2", "test_python_standalone_module.py.jinja2", "neuron.py.jinja2", "spike_generator.py.jinja2", "utils.py.jinja2"]
"module_templates": ["simulator.py.jinja2", "test_python_standalone_module.py.jinja2", "neuron.py.jinja2", "synapse.py.jinja2", "spike_generator.py.jinja2", "utils.py.jinja2"]
},
"solver": "analytic",
"numeric_solver": "rk45"
"numeric_solver": "rk45",
"neuron_synapse_pairs": [],
"delay_variable": {},
"weight_variable": {}
}

def __init__(self, options: Optional[Mapping[str, Any]] = None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ import scipy.integrate
from .neuron import Neuron
from .utils import steps

DEBUG = 1


{%- set stateSize = neuron.get_non_inline_state_symbols()|length %}

Expand Down Expand Up @@ -153,29 +151,6 @@ class Neuron_{{neuronName}}(Neuron):
self._timestep = timestep
self.recompute_internal_variables(self._timestep)

{%- if paired_synapse is defined %}
# -----------------------------
# code for paired synapse
# -----------------------------

# state variables for archiving state for paired synapse
self.n_incoming_ = 0.
self.max_delay_ = 0.
self.last_spike_ = -1.

# cache initial values
{%- for var in transferred_variables %}
{%- set variable_symbol = transferred_variables_syms[var] %}
{%- set variable = utils.get_variable_by_name(astnode, variable_name) %}
{%- if not var == variable_symbol.get_symbol_name() %}
{{ raise('Error in resolving variable to symbol') }}
{%- endif %}
{{var}}__iv = get_{{ printer.print(variable) }}()
{%- endfor %}

self.clear_history()
{%- endif %}

def get_model(self) -> str:
return "{{neuronName}}"

Expand Down Expand Up @@ -297,13 +272,13 @@ class Neuron_{{neuronName}}(Neuron):
# NESTML generated code for the update block
# -------------------------------------------------------------------------

{% if neuron.get_update_blocks()|length > 0 %}
{%- filter indent(4) %}
{%- for dynamics in neuron.get_update_blocks() %}
{%- set ast = dynamics.get_stmts_body() %}
{%- include "directives_py/StmtsBody.jinja2" %}
{%- endfor %}
{%- endfilter %}
{% if neuron.get_update_blocks() | length > 0 %}
{%- filter indent(4) %}
{%- for dynamics in neuron.get_update_blocks() %}
{%- set ast = dynamics.get_stmts_body() %}
{%- include "directives_py/StmtsBody.jinja2" %}
{%- endfor %}
{%- endfilter %}
{%- endif %}

# -------------------------------------------------------------------------
Expand Down Expand Up @@ -356,27 +331,27 @@ class Neuron_{{neuronName}}(Neuron):
{%- endfor %}


{% if has_spike_input %}
{% if has_spike_input %}
# -------------------------------------------------------------------------
# Spiking input handlers
# -------------------------------------------------------------------------

def handle(self, t_spike: float, w: float, port_name: str) -> None:
{%- for port in neuron.get_spike_input_ports() %}
{%- for port in neuron.get_spike_input_ports() %}
if port_name == "{{port.name}}":
self.B_.{{port.get_symbol_name()}} += abs(w)
self.B_.spike_received_{{port.get_symbol_name()}} = True
return
{%- endfor %}
{%- endif %}
{%- endfor %}
raise Exception("Received a spike on unknown input port \"" + port_name + "\" at t = " + "{0:E}".format(t_spike))

def get_spiking_input_ports(self) -> List[str]:
return [
{%- for port in neuron.get_spike_input_ports() %}
{%- for port in neuron.get_spike_input_ports() %}
"{{port.name}}",
{%- endfor %}
{%- endfor %}
]
{%- endif %}

# -------------------------------------------------------------------------
# Methods corresponding to event handlers
Expand Down
Loading
Loading