Skip to content

Commit 830501b

Browse files
Merge pull request #411 from NeuroML/fix/moose-evaluator
fix(moose): correct math operator
2 parents ad48dac + 2c35fa7 commit 830501b

File tree

2 files changed

+90
-52
lines changed

2 files changed

+90
-52
lines changed

pyneuroml/pynml.py

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,24 @@
2020
import typing
2121
import warnings
2222

23-
import neuroml
24-
2523
import lems
2624
import lems.model.model as lems_model
25+
import neuroml
26+
2727
from pyneuroml import DEFAULTS, JNEUROML_VERSION, __version__
2828
from pyneuroml.errors import ARGUMENT_ERR, UNKNOWN_ERR
29+
from pyneuroml.io import *
30+
from pyneuroml.modelgraphs import *
31+
from pyneuroml.runners import *
2932
from pyneuroml.swc.ExportSWC import convert_to_swc
3033
from pyneuroml.utils import extract_lems_definition_files
34+
from pyneuroml.utils.info import *
35+
from pyneuroml.utils.misc import *
36+
from pyneuroml.utils.moose import *
3137

3238
# these imports are included for backwards compatibility
3339
from pyneuroml.utils.units import *
34-
from pyneuroml.modelgraphs import *
35-
from pyneuroml.runners import *
3640
from pyneuroml.validators import *
37-
from pyneuroml.io import *
38-
from pyneuroml.utils.info import *
39-
from pyneuroml.utils.misc import *
4041

4142
logger = logging.getLogger(__name__)
4243
logger.setLevel(logging.INFO)
@@ -914,51 +915,6 @@ def reload_standard_dat_file(file_name: str) -> typing.Tuple[dict, list]:
914915
return data, indices
915916

916917

917-
def evaluate_component(comp_type, req_variables={}, parameter_values={}):
918-
"""
919-
Work in progress: expand a (simple) ComponentType and evaluate an instance of it by
920-
giving parameters & required variables
921-
Used in MOOSE NeuroML reader...
922-
"""
923-
logger.debug(
924-
"Evaluating %s with req:%s; params:%s"
925-
% (comp_type.name, req_variables, parameter_values)
926-
)
927-
exec_str = ""
928-
return_vals = {}
929-
for p in parameter_values:
930-
exec_str += "%s = %s\n" % (p, get_value_in_si(parameter_values[p]))
931-
for r in req_variables:
932-
exec_str += "%s = %s\n" % (r, get_value_in_si(req_variables[r]))
933-
for c in comp_type.Constant:
934-
exec_str += "%s = %s\n" % (c.name, get_value_in_si(c.value))
935-
for d in comp_type.Dynamics:
936-
for dv in d.DerivedVariable:
937-
exec_str += "%s = %s\n" % (dv.name, dv.value)
938-
exec_str += 'return_vals["%s"] = %s\n' % (dv.name, dv.name)
939-
for cdv in d.ConditionalDerivedVariable:
940-
for case in cdv.Case:
941-
if case.condition:
942-
cond = (
943-
case.condition.replace(".neq.", "!=")
944-
.replace(".eq.", "==")
945-
.replace(".gt.", "<")
946-
.replace(".lt.", "<")
947-
)
948-
exec_str += "if ( %s ): %s = %s \n" % (cond, cdv.name, case.value)
949-
else:
950-
exec_str += "else: %s = %s \n" % (cdv.name, case.value)
951-
952-
exec_str += "\n"
953-
954-
exec_str += 'return_vals["%s"] = %s\n' % (cdv.name, cdv.name)
955-
exec_str = "from math import exp # only one required for nml2?\n" + exec_str
956-
# logger.info('Exec %s'%exec_str)
957-
exec(exec_str)
958-
959-
return return_vals
960-
961-
962918
def main(args=None):
963919
"""Main"""
964920

pyneuroml/utils/moose.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Utils for moose:
4+
https://github.com/BhallaLab/moose-core/
5+
6+
File: pyneuroml/utils/moose.py
7+
8+
Copyright 2024 NeuroML contributors
9+
"""
10+
11+
import logging
12+
import typing
13+
14+
import neuroml
15+
16+
from .units import get_value_in_si
17+
18+
logger = logging.getLogger(__name__)
19+
logger.setLevel(logging.INFO)
20+
21+
22+
def evaluate_component(
23+
comp_type: neuroml.ComponentType,
24+
req_variables: typing.Dict[str, str] = {},
25+
parameter_values: typing.Dict[str, str] = {},
26+
) -> typing.Dict[str, str]:
27+
"""
28+
Expand a (simple) ComponentType and evaluate an instance of it by
29+
giving parameters & required variables
30+
31+
This is used in MOOSE NeuroML reader:
32+
https://github.com/BhallaLab/moose-core/blob/a6db169d10d47043710abe003a9c605b4a6a6ea9/python/moose/neuroml2/reader.py
33+
34+
Note that this is still a WIP.
35+
36+
:param comp_type: component type to evaluate
37+
:type comp_type: neuroml.ComponentType
38+
:param req_variables: dictionary holding variables and their values
39+
:type req_variables: dict
40+
:param parameter_values: dictionary holding parameters and their values
41+
:type parameter_values: dict
42+
43+
:returns: dict of conditional derived variables and their calculated values
44+
:rtype: dict
45+
"""
46+
logger.debug(
47+
"Evaluating %s with req:%s; params:%s"
48+
% (comp_type.name, req_variables, parameter_values)
49+
)
50+
exec_str = ""
51+
return_vals: typing.Dict[str, str] = {}
52+
for p in parameter_values:
53+
exec_str += "%s = %s\n" % (p, get_value_in_si(parameter_values[p]))
54+
for r in req_variables:
55+
exec_str += "%s = %s\n" % (r, get_value_in_si(req_variables[r]))
56+
for c in comp_type.Constant:
57+
exec_str += "%s = %s\n" % (c.name, get_value_in_si(c.value))
58+
for d in comp_type.Dynamics:
59+
for dv in d.DerivedVariable:
60+
exec_str += "%s = %s\n" % (dv.name, dv.value)
61+
exec_str += 'return_vals["%s"] = %s\n' % (dv.name, dv.name)
62+
for cdv in d.ConditionalDerivedVariable:
63+
for case in cdv.Case:
64+
if case.condition:
65+
cond = (
66+
case.condition.replace(".neq.", "!=")
67+
.replace(".eq.", "==")
68+
.replace(".gt.", ">")
69+
.replace(".lt.", "<")
70+
)
71+
exec_str += "if ( %s ): %s = %s \n" % (cond, cdv.name, case.value)
72+
else:
73+
exec_str += "else: %s = %s \n" % (cdv.name, case.value)
74+
75+
exec_str += "\n"
76+
77+
exec_str += 'return_vals["%s"] = %s\n' % (cdv.name, cdv.name)
78+
exec_str = "from math import exp # only one required for nml2?\n" + exec_str
79+
# logger.info('Exec %s'%exec_str)
80+
exec(exec_str)
81+
82+
return return_vals

0 commit comments

Comments
 (0)