pynestml.transformers package

Submodules

pynestml.transformers.add_timestep_to_internals_transformer module

class pynestml.transformers.add_timestep_to_internals_transformer.AddTimestepToInternalsTransformer(options: Mapping[str, Any] | None = None)

Bases: Transformer

Add timestep variable to the internals block

classmethod add_timestep_symbol(model: ASTModel) None

Add timestep variable to the internals block

transform(models: Iterable[ASTModel], metadata: Dict[str, Dict[str, Any]]) Iterable[ASTModel]

pynestml.transformers.convolutions_to_buffers_transformer module

class pynestml.transformers.convolutions_to_buffers_transformer.ConvolutionsToBuffersTransformer(options: Mapping[str, Any] | None = None)

Bases: Transformer

Replace all occurrences of convolve(kernel[‘]^n, spike_input_port) with the corresponding buffer variable, e.g. g_E__X__spikes_exc[__d]^n for a kernel named g_E and a spike input port named spikes_exc. Replaces all occurrences of spike_input_port outside of convolutions with 0 because it corresponds to a delta function-like increment. Similarly, convolutions with delta kernels are replaced with a value of 0.

Store metadata pertaining to which buffer variables are needed (with key kernel_buffers) and metadata pertaining to increments due to delta kernels (with key delta_factors).

generate_kernel_buffers(model: ASTModel) Set[Tuple[ASTKernel, ASTInputPort]]

For every occurrence of a convolution of the form convolve(var, spike_buf): add the element (kernel, spike_buf) to the set, with kernel being the kernel that contains variable var.

get_delta_factors_from_convolutions(model: ASTModel) dict

For every occurrence of a convolution of the form x^(n) = a * convolve(kernel, inport) + … where kernel is a delta function, add the element (x^(n), inport) –> a to the set.

get_delta_factors_from_input_port_references(model: ASTModel) dict

For every occurrence of a convolution of the form x^(n) = a * inport + ..., add the element (x^(n), inport) –> a to the set.

get_factor_str_from_expr_and_inport(expr, sub_expr)

Use sympy.coeff() to extract the factor a with which sub_expr appears in expr = ... + a * sub_expr + .... Using sympy means that this should also work with more complex, nested expressions.

transform(models: Iterable[ASTModel], metadata: Dict[str, Dict[str, Any]]) Iterable[ASTModel]

pynestml.transformers.equations_with_delay_vars_transformer module

class pynestml.transformers.equations_with_delay_vars_transformer.EquationsWithDelayVarsTransformer(options: Mapping[str, Any] | None = None)

Bases: Transformer

Convert the delay variables parsed as function calls to ASTVariable and collects all the equations that have these delay variables in the metadata with key equations_with_delay_vars.

Warning: this transformer sets attributes on the symbols in the model symbol table. If the ASTSymbolTableVisitor is run on the model, these attributes are lost.

transform(models: Iterable[ASTModel], metadata: Dict[str, Dict[str, Any]]) Iterable[ASTModel]

pynestml.transformers.equations_with_vector_vars_transformer module

class pynestml.transformers.equations_with_vector_vars_transformer.EquationsWithVectorVarsTransformer(options: Mapping[str, Any] | None = None)

Bases: Transformer

Collect all the equations that contain vector variables and store them in the model metadata under the key equations_with_vector_vars.

transform(models: Iterable[ASTModel], metadata: Dict[str, Dict[str, Any]]) Iterable[ASTModel]

pynestml.transformers.illegal_variable_name_transformer module

class pynestml.transformers.illegal_variable_name_transformer.IllegalVariableNameTransformer(options: Mapping[str, Any] | None = None)

Bases: Transformer

Perform a model transformation step, for instance, rewriting disallowed variable names like “lambda” because it conflicts with a keyword.

class VariableNameRewriterVisitor(forbidden_names: List[str], fix_name_func: Callable[[str], str])

Bases: ASTVisitor

fix_name_func_: Callable[[str], str]
forbidden_names_: List[str]
visit_assignment(node)

Used to visit a single assignment. :param node: an assignment object. :type node: ASTAssignment

visit_declaration(node)

Used to visit a single declaration. :param node: a declaration object. :type node: ASTDeclaration

visit_expression(node)

Used to visit a single rhs. :param node: an rhs. :type node: ASTExpression

visit_ode_equation(node)

Used to visit a single ode-equation. :param node: a single ode-equation. :type node: ASTOdeEquation

visit_simple_expression(node)

Used to visit a single simple rhs. :param node: a simple rhs. :type node: ASTSimpleExpression

fix_name_append_underscores_(name: str) str
fix_name_func_: Callable[[str], str]
rewritten_names_: List[Tuple[str, str]]
transform(models: Iterable[ASTModel], metadata: Dict[str, Dict[str, Any]]) Iterable[ASTModel]

pynestml.transformers.inline_expression_expansion_transformer module

class pynestml.transformers.inline_expression_expansion_transformer.InlineExpressionExpansionTransformer(options: Mapping[str, Any] | None = None)

Bases: Transformer

Make inline expressions self contained, i.e. without any references to other inline expressions.

Additionally, replace variable symbols referencing inline expressions in defining expressions of ODEs with the corresponding defining expressions from the inline expressions.

make_inline_expressions_self_contained(inline_expressions: List[ASTInlineExpression]) List[ASTInlineExpression]

Make inline expressions self contained, i.e. without any references to other inline expressions.

Parameters:

inline_expressions – A sorted list with entries ASTInlineExpression.

Returns:

A list with ASTInlineExpressions. Defining expressions don’t depend on each other.

classmethod replace_inline_expressions_through_defining_expressions(definitions: Sequence[ASTOdeEquation], inline_expressions: Sequence[ASTInlineExpression]) Sequence[ASTOdeEquation]

Replace variable symbols referencing inline expressions in defining expressions of ODEs with the corresponding defining expressions from the inline expressions.

Parameters:
  • definitions – A list of ODE definitions (updated in-place).

  • inline_expressions – A list of inline expression definitions.

Returns:

A list of updated ODE definitions (same as the definitions parameter).

transform(models: Iterable[ASTModel], metadata: Dict[str, Dict[str, Any]]) Iterable[ASTModel]

pynestml.transformers.ode_toolbox_transformer module

class pynestml.transformers.ode_toolbox_transformer.ODEToolboxTransformer(options: Mapping[str, Any] | None = None)

Bases: Transformer

Run ODE-toolbox analysis on a model.

Store the result in the model metadata with keys analytic_solver and numeric_solver.

Options

  • preserve_expressions: Set to True, or a list of strings corresponding to individual variable names, to disable internal rewriting of expressions, and return same output as input expression where possible. Only applies to variables specified as first-order differential equations. (This parameter is passed to ODE-toolbox.)

  • simplify_expression: For all expressions expr that are rewritten by ODE-toolbox: the contents of this parameter string are eval()``ed in Python to obtain the final output expression. Override for custom expression simplification steps. Example: ``sympy.simplify(expr). Default: "sympy.logcombine(sympy.powsimp(sympy.expand(expr)))". (This parameter is passed to ODE-toolbox.)

  • solver: A string identifying the preferred ODE solver. "analytic" for propagator solver preferred; fallback to numeric solver in case ODEs are not analytically solvable. Use "numeric" to disable analytic solver.

  • ode_toolbox_json_options: An optional extra dictionary; key-value pairs are passed to ODE-toolbox indict “options” key.

create_ode_toolbox_indict(neuron: ASTModel, kernel_buffers: Mapping[ASTKernel, ASTInputPort])
ode_toolbox_analysis(model: ASTModel, kernel_buffers: Mapping[ASTKernel, ASTInputPort], metadata: Dict[str, Dict[str, Any]])

Prepare data for ODE-toolbox input format, invoke ODE-toolbox analysis via its API, and return the output.

transform(models: Iterable[ASTModel], metadata: Dict[str, Dict[str, Any]]) Iterable[ASTModel]

pynestml.transformers.synapse_post_neuron_transformer module

class pynestml.transformers.synapse_post_neuron_transformer.SynapsePostNeuronTransformer(options: Mapping[str, Any] | None = None)

Bases: Transformer

In a (pre neuron, synapse, post neuron) tuple, process (synapse, post_neuron) to move all variables that are only triggered by postsynaptic events to the postsynaptic neuron.

Options:

  • strictly_synaptic_vars: a mapping from synapse name (as a string) to a list of state variables. These variables will not be moved from synapse to neuron during code generation.

get_neuron_var_name_from_syn_port_name(port_name: str, neuron_name: str, synapse_name: str) str | None

Return the name of the variable in the neuron that corresponds to the given continuous-time synapse port name, if any.

set_options(options: Mapping[str, Any]) Mapping[str, Any]

Set options. “Eats off” any options that it knows how to set, and returns the rest as “unhandled” options.

transform(models: Iterable[ASTModel], metadata: Dict[str, Dict[str, Any]]) Iterable[ASTModel]
transform_neuron_with_synapses_(neuron: ASTModel, synapses: List[ASTModel], metadata: Dict[str, Dict[str, Any]]) Tuple[ASTModel, List[ASTModel]]

“Co-generation” or in-tandem generation of neuron and synapse code.

Does not modify existing neurons or synapses, but returns lists with additional elements representing new paired neuron/synapse models

pynestml.transformers.transformer module

class pynestml.transformers.transformer.Transformer(options: Mapping[str, Any] | None = None)

Bases: WithOptions

Perform a transformation step on models, for instance, rewriting disallowed variable names like “lambda” because they conflict with keywords in the target language.

Transformers operate on sets of models and produce sets of models (possibly of unequal size). For instance, some transformers accept a pair consisting of neuron and synapse, processing these together to create a new, interlinked model.

Additionally, as a side effect, transformers may produce metadata about models, that contains information that can be helpful for subsequent transformers as well as code generation (for instance, the results of ODE-toolbox, detailing the numerical solver that is to be used for a model). The metadata dictionary is a mapping from the name of the model (as a string) to a dictionary of key/value pairs that contain the metadata (of arbitrary type, indexed by strings as keys) for that model.

abstract transform(models: Iterable[ASTModel], metadata: Dict[str, Dict[str, Any]]) Iterable[ASTModel]