Advertisement
Guest User

Edited generate_code_for() function

a guest
May 7th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.15 KB | Source Code | 0 0
  1. # -*- coding: utf-8 -*-
  2. #
  3. # nest_code_generator_utils.py
  4. #
  5. # This file is part of NEST.
  6. #
  7. # Copyright (C) 2004 The NEST Initiative
  8. #
  9. # NEST is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # NEST is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with NEST.  If not, see <http://www.gnu.org/licenses/>.
  21. from typing import List, Optional
  22.  
  23. import re
  24. import tempfile
  25. import uuid
  26.  
  27. from pynestml.meta_model.ast_variable import ASTVariable
  28. from pynestml.symbols.variable_symbol import BlockType
  29. from pynestml.symbols.variable_symbol import VariableSymbol
  30.  
  31.  
  32. class NESTCodeGeneratorUtils_edit:
  33.  
  34.     @classmethod
  35.     def print_symbol_origin(cls, variable_symbol: VariableSymbol, variable: ASTVariable) -> str:
  36.         r"""
  37.        Returns a prefix corresponding to the origin of the variable symbol.
  38.        :param variable_symbol: a single variable symbol.
  39.        :return: the corresponding prefix
  40.        """
  41.         if variable_symbol.block_type in [BlockType.STATE, BlockType.EQUATION]:
  42.             if "_is_numeric" in dir(variable) and variable._is_numeric:
  43.                 return "S_.ode_state[State_::%s]"
  44.  
  45.             return "S_.%s"
  46.  
  47.         if variable_symbol.block_type == BlockType.PARAMETERS:
  48.             return "P_.%s"
  49.  
  50.         if variable_symbol.block_type == BlockType.COMMON_PARAMETERS:
  51.             return "cp.%s"
  52.  
  53.         if variable_symbol.block_type == BlockType.INTERNALS:
  54.             return "V_.%s"
  55.  
  56.         if variable_symbol.block_type == BlockType.INPUT:
  57.             return "B_.%s"
  58.  
  59.         return ""
  60.  
  61.     @classmethod
  62.     def generate_code_for(cls,
  63.                           nestml_neuron_model: str,
  64.                           nestml_synapse_model: Optional[str] = None,
  65.                           module_name: Optional[str] = None,
  66.                           target_path: str = "target",
  67.                           post_ports: Optional[List[str]] = None,
  68.                           mod_ports: Optional[List[str]] = None,
  69.                           logging_level: str = "WARNING"):
  70.         """Generate code for a given neuron and synapse model, passed as a string.
  71.        The neuron and synapse models can be passed directly as strings in NESTML syntax, or as filenames, in which case the NESTML model is loaded from the given filename.
  72.  
  73.        Returns
  74.        -------
  75.        If a synapse is specified, returns a tuple (module_name, mangled_neuron_name, mangled_synapse_name) containing the names that can be used in ``nest.Install()``, ``nest.Create()`` and ``nest.Connect()`` calls. If no synapse is specified, returns a tuple (module_name, mangled_neuron_name).
  76.        """
  77.  
  78.         from pynestml.frontend.pynestml_frontend import generate_nest_target
  79.  
  80.         # generate temporary install directory
  81.         install_path = tempfile.mkdtemp(prefix="nestml_target_")
  82.  
  83.         # read neuron model from file?
  84.         if "\n" not in nestml_neuron_model and ".nestml" in nestml_neuron_model:
  85.             with open(nestml_neuron_model, "r") as nestml_model_file:
  86.                 nestml_neuron_model = nestml_model_file.read()
  87.  
  88.         # update neuron model name inside the file
  89.         neuron_model_name = re.findall(r"model [^:\s]*:", nestml_neuron_model)[0][6:-1]
  90.         neuron_fn = neuron_model_name + ".nestml"
  91.         with open(neuron_fn, "w") as f:
  92.             print(nestml_neuron_model, file=f)
  93.  
  94.         input_fns = [neuron_fn]
  95.         codegen_opts = {"neuron_parent_class": "StructuralPlasticityNode",
  96.                         "neuron_parent_class_include": "structural_plasticity_node.h"}
  97.         mangled_neuron_name = neuron_model_name + "_nestml"
  98.  
  99.         input_fns = [neuron_fn]
  100.         codegen_opts = {"neuron_parent_class": "StructuralPlasticityNode",
  101.                         "neuron_parent_class_include": "structural_plasticity_node.h"}
  102.         mangled_neuron_name = neuron_model_name + "_nestml"
  103.  
  104.         if nestml_synapse_model:
  105.             # read synapse model from file?
  106.             if "\n" not in nestml_synapse_model and ".nestml" in nestml_synapse_model:
  107.                 with open(nestml_synapse_model, "r") as nestml_model_file:
  108.                     nestml_synapse_model = nestml_model_file.read()
  109.  
  110.             # update synapse model name inside the file
  111.             synapse_model_name = re.findall(r"synapse [^:\s]*:", nestml_synapse_model)[0][6:-1]
  112.             synapse_fn = synapse_model_name + ".nestml"
  113.             with open(synapse_fn, "w") as f:
  114.                 print(nestml_synapse_model, file=f)
  115.             input_fns += [synapse_fn]
  116.             codegen_opts["neuron_synapse_pairs"] = [{"neuron": neuron_model_name,
  117.                                                      "synapse": synapse_model_name,
  118.                                                      "post_ports": post_ports,
  119.                                                      "vt_ports": mod_ports}]
  120.             mangled_neuron_name = neuron_model_name + "_nestml__with_" + synapse_model_name + "_nestml"
  121.             mangled_synapse_name = synapse_model_name + "_nestml__with_" + neuron_model_name + "_nestml"
  122.  
  123.         if not module_name:
  124.             # generate unique ID
  125.             uniq_id = str(uuid.uuid4().hex)
  126.             module_name = "nestml_" + uniq_id + "_module"
  127.  
  128.         generate_nest_target(input_path=input_fns,
  129.                              install_path=install_path,
  130.                              logging_level=logging_level,
  131.                              module_name=module_name,
  132.                              target_path=target_path,
  133.                              suffix="_nestml",
  134.                              codegen_opts=codegen_opts)
  135.  
  136.         if nestml_synapse_model:
  137.             return module_name, mangled_neuron_name, mangled_synapse_name
  138.         else:
  139.             return module_name, mangled_neuron_name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement