def test_print_statement_with_variables(self): input_path = str( os.path.realpath( os.path.join( os.path.dirname(__file__), os.path.join('resources', 'PrintStatementWithVariables.nestml')))) params = list() params.append('--input_path') params.append(input_path) params.append('--logging_level') params.append('INFO') params.append('--target_path') params.append(self.target_path) params.append('--dev') FrontendConfiguration.parse_config(params) compilation_unit = ModelParser.parse_model(input_path) nestCodeGenerator = NESTCodeGenerator() nestCodeGenerator.generate_code(compilation_unit.get_neuron_list()) with open( str( os.path.realpath( os.path.join( os.path.dirname(__file__), os.path.join(os.pardir, 'target', 'print_test_variables.cpp')))), 'r') as reader: self.assertEqual(reader.read().count('std::cout'), 2)
def test_model_preparation(self): input_path = str( os.path.realpath( os.path.join( os.path.dirname(__file__), os.path.join(os.pardir, 'models', 'iaf_psc_alpha.nestml')))) compilation_unit = ModelParser.parse_model(input_path) assert len(compilation_unit.get_neuron_list()) == 1 ast_neuron = compilation_unit.get_neuron_list()[0] equations_block = ast_neuron.get_equations_block() # the idea here is to go through the rhs, print expressions, use the same mechanism as before, and reread them # again # TODO: add tests for this function # this function changes stuff inplace nestCodeGenerator = NESTCodeGenerator() nestCodeGenerator.make_functions_self_contained( equations_block.get_ode_functions()) nestCodeGenerator.replace_functions_through_defining_expressions( equations_block.get_ode_equations(), equations_block.get_ode_functions()) json_representation = nestCodeGenerator.transform_ode_and_shapes_to_json( equations_block) self.assertTrue("convolve(I_shape_in, in_spikes)" in json_representation["odes"][0]["definition"]) self.assertTrue("convolve(I_shape_ex, ex_spikes)" in json_representation["odes"][0]["definition"])
def generate_code(self, neurons): if self._target == "NEST": from pynestml.codegeneration.nest_codegenerator import NESTCodeGenerator _codeGenerator = NESTCodeGenerator() _codeGenerator.generate_code(neurons) elif self._target == "": # dummy/null target: user requested to not generate any code code, message = Messages.get_no_code_generated() Logger.log_message(None, code, message, None, LoggingLevel.INFO)
def test_solve_odes_and_shapes(self): input_path = str( os.path.realpath( os.path.join( os.path.dirname(__file__), os.path.join(os.pardir, 'models', 'iaf_psc_alpha.nestml')))) compilation_unit = ModelParser.parse_model(input_path) assert len(compilation_unit.get_neuron_list()) == 1 ast_neuron = compilation_unit.get_neuron_list()[0] nestCodeGenerator = NESTCodeGenerator() ast_neuron = nestCodeGenerator.transform_shapes_and_odes( ast_neuron, {})
def test_iaf_psc_alpha_with_codegen_opts(self): input_path = str( os.path.realpath( os.path.join( os.path.dirname(__file__), os.path.join(os.pardir, 'models', 'neurons', 'iaf_psc_alpha.nestml')))) code_opts_path = str( os.path.realpath( os.path.join(os.path.dirname(__file__), os.path.join('resources', 'code_options.json')))) codegen_opts = { "templates": { "path": "point_neuron", "model_templates": { "neuron": ['NeuronClass.cpp.jinja2', 'NeuronHeader.h.jinja2'], "synapse": [] }, "module_templates": [ 'setup/CMakeLists.txt.jinja2', 'setup/ModuleHeader.h.jinja2', 'setup/ModuleClass.cpp.jinja2' ] } } with open(code_opts_path, 'w+') as f: json.dump(codegen_opts, f) params = list() params.append('--input_path') params.append(input_path) params.append('--logging_level') params.append('INFO') params.append('--target_path') params.append(self.target_path) params.append('--dev') params.append('--codegen_opts') params.append(code_opts_path) FrontendConfiguration.parse_config(params) compilation_unit = ModelParser.parse_model(input_path) nestCodeGenerator = NESTCodeGenerator(codegen_opts) nestCodeGenerator.generate_code(compilation_unit.get_neuron_list(), list())
def from_target_name( target_name: str, options: Optional[Mapping[str, Any]] = None) -> CodeGenerator: """Static factory method that returns a new instance of a child class of CodeGenerator""" assert target_name.upper() in CodeGenerator.get_known_targets( ), "Unknown target platform requested: \"" + str(target_name) + "\"" if target_name.upper() == "NEST": from pynestml.codegeneration.nest_codegenerator import NESTCodeGenerator return NESTCodeGenerator(options) elif target_name.upper() == "AUTODOC": from pynestml.codegeneration.autodoc_codegenerator import AutoDocCodeGenerator assert options is None or options == {}, "\"autodoc\" code generator does not support options" return AutoDocCodeGenerator() elif target_name == "": # dummy/null target: user requested to not generate any code code, message = Messages.get_no_code_generated() Logger.log_message(None, code, message, None, LoggingLevel.INFO) return CodeGenerator("", options) assert False # cannot reach here due to earlier assert -- silence static checker warnings
def test_expressions(self): input_path = str( os.path.realpath( os.path.join( os.path.dirname(__file__), os.path.join('resources', 'ExpressionTypeTest.nestml')))) params = list() params.append('--input_path') params.append(input_path) params.append('--logging_level') params.append('INFO') params.append('--target_path') params.append(self.target_path) params.append('--dev') FrontendConfiguration.parse_config(params) compilation_unit = ModelParser.parse_model(input_path) nestCodeGenerator = NESTCodeGenerator() nestCodeGenerator.generate_code(compilation_unit.get_neuron_list())
def generate_code(self, neurons): """ Generate code for the given neurons and (depending on the target) generate an index page, module entrypoint or similar that incorporates an enumeration of all neurons. :param neurons: a list of neurons. :type neurons: List[ASTNode] """ if self._target.upper() == "NEST": from pynestml.codegeneration.nest_codegenerator import NESTCodeGenerator _codeGenerator = NESTCodeGenerator() _codeGenerator.generate_code(neurons) elif self._target.upper() == "AUTODOC": from pynestml.codegeneration.autodoc_codegenerator import AutoDocCodeGenerator _codeGenerator = AutoDocCodeGenerator() _codeGenerator.generate_code(neurons) else: # dummy/null target: user requested to not generate any code assert self._target == "" code, message = Messages.get_no_code_generated() Logger.log_message(None, code, message, None, LoggingLevel.INFO)
def test_vector_code_generation(self): input_path = str( os.path.realpath( os.path.join( os.path.dirname(__file__), os.path.join('valid', 'VectorsDeclarationAndAssignment.nestml')))) params = list() params.append('--input_path') params.append(input_path) params.append('--logging_level') params.append('INFO') params.append('--target_path') params.append(self.target_path) params.append('--dev') FrontendConfiguration.parse_config(params) compilation_unit = ModelParser.parse_model(input_path) nestCodeGenerator = NESTCodeGenerator() nestCodeGenerator.generate_code(compilation_unit.get_neuron_list())
def test_iaf_psc_delta(self): input_path = str( os.path.realpath( os.path.join( os.path.dirname(__file__), os.path.join(os.pardir, 'models', 'neurons', 'iaf_psc_delta.nestml')))) params = list() params.append('--input_path') params.append(input_path) params.append('--logging_level') params.append('INFO') params.append('--target_path') params.append(self.target_path) params.append('--dev') FrontendConfiguration.parse_config(params) compilation_unit = ModelParser.parse_model(input_path) nestCodeGenerator = NESTCodeGenerator() nestCodeGenerator.generate_code(compilation_unit.get_neuron_list(), compilation_unit.get_synapse_list())
def test_iaf_cond_alpha_functional(self): input_path = str( os.path.realpath( os.path.join( os.path.dirname(__file__), os.path.join(os.pardir, 'models', 'iaf_cond_alpha.nestml')))) params = list() params.append('--input_path') params.append(input_path) params.append('--logging_level') params.append('INFO') params.append('--target_path') params.append(self.target_path) params.append('--dev') FrontendConfiguration.parse_config(params) compilation_unit = ModelParser.parse_model(input_path) iaf_cond_alpha_functional = list() iaf_cond_alpha_functional.append(compilation_unit.get_neuron_list()[0]) nestCodeGenerator = NESTCodeGenerator() nestCodeGenerator.generate_code(iaf_cond_alpha_functional, [])