-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into resolution_to_timestep
- Loading branch information
Showing
8 changed files
with
298 additions
and
22 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
pynestml/cocos/co_co_nest_random_functions_legally_used.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# co_co_nest_random_functions_legally_used.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/>. | ||
|
||
from pynestml.cocos.co_co import CoCo | ||
from pynestml.meta_model.ast_model import ASTModel | ||
from pynestml.meta_model.ast_node import ASTNode | ||
from pynestml.meta_model.ast_on_condition_block import ASTOnConditionBlock | ||
from pynestml.meta_model.ast_on_receive_block import ASTOnReceiveBlock | ||
from pynestml.meta_model.ast_update_block import ASTUpdateBlock | ||
from pynestml.symbols.predefined_functions import PredefinedFunctions | ||
from pynestml.utils.logger import LoggingLevel, Logger | ||
from pynestml.utils.messages import Messages | ||
from pynestml.visitors.ast_visitor import ASTVisitor | ||
|
||
|
||
class CoCoNestRandomFunctionsLegallyUsed(CoCo): | ||
""" | ||
This CoCo ensure that the random functions are used only in the ``update``, ``onReceive``, and ``onCondition`` blocks. | ||
This CoCo is only checked for the NEST Simulator target. | ||
""" | ||
|
||
@classmethod | ||
def check_co_co(cls, node: ASTNode): | ||
""" | ||
Checks the coco. | ||
:param node: a single node (typically, a neuron or synapse) | ||
""" | ||
visitor = CoCoNestRandomFunctionsLegallyUsedVisitor() | ||
visitor.neuron = node | ||
node.accept(visitor) | ||
|
||
|
||
class CoCoNestRandomFunctionsLegallyUsedVisitor(ASTVisitor): | ||
def visit_function_call(self, node): | ||
""" | ||
Visits a function call | ||
:param node: a function call | ||
""" | ||
function_name = node.get_name() | ||
if function_name == PredefinedFunctions.RANDOM_NORMAL or function_name == PredefinedFunctions.RANDOM_UNIFORM \ | ||
or function_name == PredefinedFunctions.RANDOM_POISSON: | ||
parent = node | ||
while parent: | ||
parent = parent.get_parent() | ||
|
||
if isinstance(parent, ASTUpdateBlock) or isinstance(parent, ASTOnReceiveBlock) \ | ||
or isinstance(parent, ASTOnConditionBlock): | ||
# the random function is correctly defined, hence return | ||
return | ||
|
||
if isinstance(parent, ASTModel): | ||
# the random function is defined in other blocks (parameters, state, internals). Hence, an error. | ||
code, message = Messages.get_random_functions_legally_used(function_name) | ||
Logger.log_message(node=self.neuron, code=code, message=message, error_position=node.get_source_position(), | ||
log_level=LoggingLevel.ERROR) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# nest_random_functions_test.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 os | ||
|
||
import pytest | ||
|
||
from pynestml.frontend.pynestml_frontend import generate_nest_target | ||
|
||
|
||
class TestNestRandomFunctions: | ||
""" | ||
Tests that, for the NEST target, random number functions are called only in ``update``, ``onReceive``, and ``onCondition`` block | ||
""" | ||
|
||
@pytest.mark.xfail(strict=True, raises=Exception) | ||
def test_nest_random_function_neuron_illegal(self): | ||
input_path = os.path.realpath(os.path.join(os.path.dirname(__file__), | ||
"resources", "random_functions_illegal_neuron.nestml")) | ||
generate_nest_target(input_path=input_path, | ||
target_path="target", | ||
logging_level="INFO", | ||
suffix="_nestml") | ||
|
||
@pytest.mark.xfail(strict=True, raises=Exception) | ||
def test_nest_random_function_synapse_illegal(self): | ||
input_path = [ | ||
os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, "models", "neurons", | ||
"iaf_psc_exp_neuron.nestml")), | ||
os.path.realpath(os.path.join(os.path.dirname(__file__), | ||
"resources", "random_functions_illegal_synapse.nestml"))] | ||
|
||
generate_nest_target(input_path=input_path, | ||
target_path="target", | ||
logging_level="INFO", | ||
suffix="_nestml", | ||
codegen_opts={"neuron_synapse_pairs": [{"neuron": "iaf_psc_exp_neuron", | ||
"synapse": "random_functions_illegal_synapse", | ||
"post_ports": ["post_spikes"]}], | ||
"weight_variable": {"stdp_synapse": "w"}}) |
46 changes: 46 additions & 0 deletions
46
tests/nest_tests/resources/random_functions_illegal_neuron.nestml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
random_functions_illegal_neuron.nestml | ||
###################################### | ||
|
||
|
||
Copyright statement | ||
+++++++++++++++++++ | ||
|
||
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/>. | ||
""" | ||
|
||
model random_functions_illegal_neuron: | ||
state: | ||
noise real = random_normal(0,sigma_noise) | ||
v mV = -15 mV | ||
|
||
parameters: | ||
rate ms**-1 = 15.5 s**-1 | ||
sigma_noise real = 16. | ||
u real = random_uniform(0,1) | ||
|
||
internals: | ||
poisson_input integer = random_poisson(rate * resolution() * 1E-3) | ||
|
||
update: | ||
if u < 0.5: | ||
noise = 0. | ||
else: | ||
noise = random_normal(0,sigma_noise) | ||
|
||
v += (poisson_input + noise) * mV |
73 changes: 73 additions & 0 deletions
73
tests/nest_tests/resources/random_functions_illegal_synapse.nestml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
random_functions_illegal_synapse.nestml | ||
####################################### | ||
|
||
|
||
Copyright statement | ||
+++++++++++++++++++ | ||
|
||
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/>. | ||
""" | ||
|
||
model random_functions_illegal_synapse: | ||
state: | ||
w real = 1 # Synaptic weight | ||
pre_trace real = 0. | ||
post_trace real = 0. | ||
|
||
parameters: | ||
d ms = 1 ms # Synaptic transmission delay | ||
lambda real = .01 | ||
tau_tr_pre ms = random_normal(110 ms, 55 ms) | ||
tau_tr_post ms = random_normal(5 ms, 2.5 ms) | ||
alpha real = 1 | ||
mu_plus real = 1 | ||
mu_minus real = 1 | ||
Wmax real = 100. | ||
Wmin real = 0. | ||
|
||
equations: | ||
pre_trace' = -pre_trace / tau_tr_pre | ||
post_trace' = -post_trace / tau_tr_post | ||
|
||
input: | ||
pre_spikes <- spike | ||
post_spikes <- spike | ||
|
||
output: | ||
spike | ||
|
||
onReceive(post_spikes): | ||
post_trace += 1 | ||
|
||
# potentiate synapse | ||
w_ real = Wmax * ( w / Wmax + (lambda * ( 1. - ( w / Wmax ) )**mu_plus * pre_trace )) | ||
w = min(Wmax, w_) | ||
|
||
onReceive(pre_spikes): | ||
pre_trace += 1 | ||
|
||
# depress synapse | ||
w_ real = Wmax * ( w / Wmax - ( alpha * lambda * ( w / Wmax )**mu_minus * post_trace )) | ||
w = max(Wmin, w_) | ||
|
||
# deliver spike to postsynaptic partner | ||
emit_spike(w, d) | ||
|
||
update: | ||
integrate_odes() |