/
nest_reference_converter.py
313 lines (247 loc) · 12.9 KB
/
nest_reference_converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# -*- coding: utf-8 -*-
#
# nest_reference_converter.py
#
# This file is part of NEST.
#
# Copyright (C) 2004 The NEST Initiative
#
# NEST is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# NEST is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.
import re
from pynestml.codegeneration.printers.cpp_reference_converter import CppReferenceConverter
from pynestml.codegeneration.printers.unit_converter import UnitConverter
from pynestml.meta_model.ast_variable import ASTVariable
from pynestml.meta_model.ast_external_variable import ASTExternalVariable
from pynestml.meta_model.ast_function_call import ASTFunctionCall
from pynestml.symbol_table.scope import Scope
from pynestml.symbols.predefined_functions import PredefinedFunctions
from pynestml.symbols.predefined_units import PredefinedUnits
from pynestml.symbols.predefined_variables import PredefinedVariables
from pynestml.symbols.symbol import SymbolKind
from pynestml.symbols.unit_type_symbol import UnitTypeSymbol
from pynestml.symbols.variable_symbol import BlockType
from pynestml.symbols.variable_symbol import VariableSymbol
from pynestml.utils.ast_utils import ASTUtils
from pynestml.utils.logger import Logger, LoggingLevel
from pynestml.utils.messages import Messages
class NESTReferenceConverter(CppReferenceConverter):
r"""
Reference converter for C++ syntax and using the NEST API.
"""
def convert_function_call(self, function_call: ASTFunctionCall, prefix: str = '') -> str:
"""
Converts a single handed over function call to C++ NEST API syntax.
Parameters
----------
function_call
The function call node to convert.
prefix
Optional string that will be prefixed to the function call. For example, to refer to a function call in the class "node", use a prefix equal to "node." or "node->".
Predefined functions will not be prefixed.
Returns
-------
s
The function call string in C++ syntax.
"""
function_name = function_call.get_name()
if function_name == 'and':
return '&&'
if function_name == 'or':
return '||'
if function_name == PredefinedFunctions.TIME_RESOLUTION:
# context dependent; we assume the template contains the necessary definitions
return '__resolution'
if function_name == PredefinedFunctions.TIME_STEPS:
return 'nest::Time(nest::Time::ms((double) ({!s}))).get_steps()'
if function_name == PredefinedFunctions.CLIP:
# warning: the arguments of this function must swapped and
# are therefore [v_max, v_min, v], hence its structure
return 'std::min({2!s}, std::max({1!s}, {0!s}))'
if function_name == PredefinedFunctions.MAX:
return 'std::max({!s}, {!s})'
if function_name == PredefinedFunctions.MIN:
return 'std::min({!s}, {!s})'
if function_name == PredefinedFunctions.EXP:
return 'std::exp({!s})'
if function_name == PredefinedFunctions.LN:
return 'std::log({!s})'
if function_name == PredefinedFunctions.LOG10:
return 'std::log10({!s})'
if function_name == PredefinedFunctions.COSH:
return 'std::cosh({!s})'
if function_name == PredefinedFunctions.SINH:
return 'std::sinh({!s})'
if function_name == PredefinedFunctions.TANH:
return 'std::tanh({!s})'
if function_name == PredefinedFunctions.EXPM1:
return 'numerics::expm1({!s})'
if function_name == PredefinedFunctions.RANDOM_NORMAL:
return '(({!s}) + ({!s}) * ' + prefix + 'normal_dev_( nest::get_vp_specific_rng( ' + prefix + 'get_thread() ) ))'
if function_name == PredefinedFunctions.RANDOM_UNIFORM:
return '(({!s}) + ({!s}) * nest::get_vp_specific_rng( ' + prefix + 'get_thread() )->drand())'
if function_name == PredefinedFunctions.EMIT_SPIKE:
return 'set_spiketime(nest::Time::step(origin.get_steps()+lag+1));\n' \
'nest::SpikeEvent se;\n' \
'nest::kernel().event_delivery_manager.send(*this, se, lag)'
if function_name == PredefinedFunctions.PRINT:
return 'std::cout << {!s}'
if function_name == PredefinedFunctions.PRINTLN:
return 'std::cout << {!s} << std::endl'
if function_name == PredefinedFunctions.DELIVER_SPIKE:
return '''
set_delay( {1!s} );
const long __delay_steps = nest::Time::delay_ms_to_steps( get_delay() );
set_delay_steps(__delay_steps);
e.set_receiver( *__target );
e.set_weight( {0!s} );
// use accessor functions (inherited from Connection< >) to obtain delay in steps and rport
e.set_delay_steps( get_delay_steps() );
e.set_rport( get_rport() );
e();
'''
# suppress prefix for misc. predefined functions
# check if function is "predefined" purely based on the name, as we don't have access to the function symbol here
function_is_predefined = PredefinedFunctions.get_function(function_name)
if function_is_predefined:
prefix = ''
if ASTUtils.needs_arguments(function_call):
n_args = len(function_call.get_args())
return prefix + function_name + '(' + ', '.join(['{!s}' for _ in range(n_args)]) + ')'
return prefix + function_name + '()'
def convert_name_reference(self, variable: ASTVariable, prefix: str = '') -> str:
"""
Converts a single variable to nest processable format.
:param variable: a single variable.
:return: a nest processable format.
"""
if isinstance(variable, ASTExternalVariable):
_name = str(variable)
if variable.get_alternate_name():
# 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 "((POST_NEURON_TYPE*)(__target))->get_" + variable.get_alternate_name() + "()"
return "((POST_NEURON_TYPE*)(__target))->get_" + _name + "(_tr_t)"
if variable.get_name() == PredefinedVariables.E_CONSTANT:
return "numerics::e"
symbol = variable.get_scope().resolve_to_symbol(variable.get_complete_name(), SymbolKind.VARIABLE)
if symbol is None:
# test if variable name can be resolved to a type
if PredefinedUnits.is_unit(variable.get_complete_name()):
return str(UnitConverter.get_factor(PredefinedUnits.get_unit(variable.get_complete_name()).get_unit()))
code, message = Messages.get_could_not_resolve(variable.get_name())
Logger.log_message(log_level=LoggingLevel.ERROR, code=code, message=message,
error_position=variable.get_source_position())
return ""
vector_param = ""
if symbol.has_vector_parameter():
vector_param = "[" + variable.get_vector_parameter() + "]"
# if symbol.is_local():
# return variable.get_name() + vector_param
if symbol.is_buffer():
if isinstance(symbol.get_type_symbol(), UnitTypeSymbol):
units_conversion_factor = UnitConverter.get_factor(symbol.get_type_symbol().unit.unit)
else:
units_conversion_factor = 1
s = ""
if not units_conversion_factor == 1:
s += "(" + str(units_conversion_factor) + " * "
s += self.print_origin(symbol, prefix=prefix) + self.buffer_value(symbol)
s += vector_param
if not units_conversion_factor == 1:
s += ")"
return s
if symbol.is_inline_expression:
return self.getter(symbol) + "()" + vector_param
assert not symbol.is_kernel(), "NEST reference converter cannot print kernel; kernel should have been " \
"converted during code generation code generation "
if symbol.is_state() or symbol.is_inline_expression:
return self.getter(symbol) + "()" + vector_param
variable_name = self.convert_to_cpp_name(variable.get_complete_name())
if symbol.is_local():
return variable_name + vector_param
return self.print_origin(symbol, prefix=prefix) + \
self.name(symbol) + vector_param
def convert_delay_variable(self, variable: ASTVariable, prefix=''):
"""
Converts a delay variable to NEST processable format
:param variable:
:return:
"""
symbol = variable.get_scope().resolve_to_symbol(variable.get_complete_name(), SymbolKind.VARIABLE)
if symbol:
if symbol.is_state() and symbol.has_delay_parameter():
return "get_delayed_" + variable.get_name() + "()"
return ""
def __get_unit_name(self, variable: ASTVariable):
assert variable.get_scope() is not None, "Undeclared variable: " + variable.get_complete_name()
variable_name = self.convert_to_cpp_name(variable.get_complete_name())
symbol = variable.get_scope().resolve_to_symbol(variable_name, SymbolKind.VARIABLE)
if isinstance(symbol.get_type_symbol(), UnitTypeSymbol):
return symbol.get_type_symbol().unit.unit.to_string()
return ''
def convert_print_statement(self, function_call: ASTFunctionCall) -> str:
r"""
A wrapper function to convert arguments of a print or println functions
:param function_call: print function call
:return: the converted print string with corresponding variables, if any
"""
stmt = function_call.get_args()[0].get_string()
stmt = stmt[stmt.index('"') + 1: stmt.rindex('"')] # Remove the double quotes from the string
scope = function_call.get_scope()
return self.__convert_print_statement_str(stmt, scope)
def __convert_print_statement_str(self, stmt: str, scope: Scope) -> str:
r"""
Converts the string argument of the print or println function to NEST processable format
Variables are resolved to NEST processable format and printed with physical units as mentioned in model, separated by a space
.. code-block:: nestml
print("Hello World")
.. code-block:: C++
std::cout << "Hello World";
.. code-block:: nestml
print("Membrane potential = {V_m}")
.. code-block:: C++
std::cout << "Membrane potential = " << V_m << " mV";
:param stmt: argument to the print or println function
:param scope: scope of the variables in the argument, if any
:return: the converted string to NEST
"""
pattern = re.compile(r'\{[a-zA-Z_][a-zA-Z0-9_]*\}') # Match the variables enclosed within '{ }'
match = pattern.search(stmt)
if match:
var_name = match.group(0)[match.group(0).find('{') + 1:match.group(0).find('}')]
left, right = stmt.split(match.group(0), 1) # Split on the first occurrence of a variable
fun_left = (lambda l: self.__convert_print_statement_str(l, scope) + ' << ' if l else '')
fun_right = (lambda r: ' << ' + self.__convert_print_statement_str(r, scope) if r else '')
ast_var = ASTVariable(var_name, scope=scope)
right = ' ' + self.__get_unit_name(ast_var) + right # concatenate unit separated by a space with the right part of the string
return fun_left(left) + self.convert_name_reference(ast_var) + fun_right(right)
return '"' + stmt + '"' # format bare string in C++ (add double quotes)
def print_origin(self, variable_symbol: VariableSymbol, prefix: str = '') -> str:
"""
Returns a prefix corresponding to the origin of the variable symbol.
:param variable_symbol: a single variable symbol.
:return: the corresponding prefix
"""
if variable_symbol.block_type == BlockType.STATE:
return prefix + 'S_.'
if variable_symbol.block_type == BlockType.EQUATION:
return prefix + 'S_.'
if variable_symbol.block_type == BlockType.PARAMETERS:
return prefix + 'P_.'
if variable_symbol.block_type == BlockType.COMMON_PARAMETERS:
return prefix + 'cp.'
if variable_symbol.block_type == BlockType.INTERNALS:
return prefix + 'V_.'
if variable_symbol.block_type == BlockType.INPUT:
return prefix + 'B_.'
return ''