Skip to content

Commit

Permalink
Merge pull request #411 from NeuroML/fix/moose-evaluator
Browse files Browse the repository at this point in the history
fix(moose): correct math operator
  • Loading branch information
sanjayankur31 authored Jul 31, 2024
2 parents ad48dac + 2c35fa7 commit 830501b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 52 deletions.
60 changes: 8 additions & 52 deletions pyneuroml/pynml.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@
import typing
import warnings

import neuroml

import lems
import lems.model.model as lems_model
import neuroml

from pyneuroml import DEFAULTS, JNEUROML_VERSION, __version__
from pyneuroml.errors import ARGUMENT_ERR, UNKNOWN_ERR
from pyneuroml.io import *
from pyneuroml.modelgraphs import *
from pyneuroml.runners import *
from pyneuroml.swc.ExportSWC import convert_to_swc
from pyneuroml.utils import extract_lems_definition_files
from pyneuroml.utils.info import *
from pyneuroml.utils.misc import *
from pyneuroml.utils.moose import *

# these imports are included for backwards compatibility
from pyneuroml.utils.units import *
from pyneuroml.modelgraphs import *
from pyneuroml.runners import *
from pyneuroml.validators import *
from pyneuroml.io import *
from pyneuroml.utils.info import *
from pyneuroml.utils.misc import *

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


def evaluate_component(comp_type, req_variables={}, parameter_values={}):
"""
Work in progress: expand a (simple) ComponentType and evaluate an instance of it by
giving parameters & required variables
Used in MOOSE NeuroML reader...
"""
logger.debug(
"Evaluating %s with req:%s; params:%s"
% (comp_type.name, req_variables, parameter_values)
)
exec_str = ""
return_vals = {}
for p in parameter_values:
exec_str += "%s = %s\n" % (p, get_value_in_si(parameter_values[p]))
for r in req_variables:
exec_str += "%s = %s\n" % (r, get_value_in_si(req_variables[r]))
for c in comp_type.Constant:
exec_str += "%s = %s\n" % (c.name, get_value_in_si(c.value))
for d in comp_type.Dynamics:
for dv in d.DerivedVariable:
exec_str += "%s = %s\n" % (dv.name, dv.value)
exec_str += 'return_vals["%s"] = %s\n' % (dv.name, dv.name)
for cdv in d.ConditionalDerivedVariable:
for case in cdv.Case:
if case.condition:
cond = (
case.condition.replace(".neq.", "!=")
.replace(".eq.", "==")
.replace(".gt.", "<")
.replace(".lt.", "<")
)
exec_str += "if ( %s ): %s = %s \n" % (cond, cdv.name, case.value)
else:
exec_str += "else: %s = %s \n" % (cdv.name, case.value)

exec_str += "\n"

exec_str += 'return_vals["%s"] = %s\n' % (cdv.name, cdv.name)
exec_str = "from math import exp # only one required for nml2?\n" + exec_str
# logger.info('Exec %s'%exec_str)
exec(exec_str)

return return_vals


def main(args=None):
"""Main"""

Expand Down
82 changes: 82 additions & 0 deletions pyneuroml/utils/moose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python3
"""
Utils for moose:
https://github.com/BhallaLab/moose-core/
File: pyneuroml/utils/moose.py
Copyright 2024 NeuroML contributors
"""

import logging
import typing

import neuroml

from .units import get_value_in_si

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def evaluate_component(
comp_type: neuroml.ComponentType,
req_variables: typing.Dict[str, str] = {},
parameter_values: typing.Dict[str, str] = {},
) -> typing.Dict[str, str]:
"""
Expand a (simple) ComponentType and evaluate an instance of it by
giving parameters & required variables
This is used in MOOSE NeuroML reader:
https://github.com/BhallaLab/moose-core/blob/a6db169d10d47043710abe003a9c605b4a6a6ea9/python/moose/neuroml2/reader.py
Note that this is still a WIP.
:param comp_type: component type to evaluate
:type comp_type: neuroml.ComponentType
:param req_variables: dictionary holding variables and their values
:type req_variables: dict
:param parameter_values: dictionary holding parameters and their values
:type parameter_values: dict
:returns: dict of conditional derived variables and their calculated values
:rtype: dict
"""
logger.debug(
"Evaluating %s with req:%s; params:%s"
% (comp_type.name, req_variables, parameter_values)
)
exec_str = ""
return_vals: typing.Dict[str, str] = {}
for p in parameter_values:
exec_str += "%s = %s\n" % (p, get_value_in_si(parameter_values[p]))
for r in req_variables:
exec_str += "%s = %s\n" % (r, get_value_in_si(req_variables[r]))
for c in comp_type.Constant:
exec_str += "%s = %s\n" % (c.name, get_value_in_si(c.value))
for d in comp_type.Dynamics:
for dv in d.DerivedVariable:
exec_str += "%s = %s\n" % (dv.name, dv.value)
exec_str += 'return_vals["%s"] = %s\n' % (dv.name, dv.name)
for cdv in d.ConditionalDerivedVariable:
for case in cdv.Case:
if case.condition:
cond = (
case.condition.replace(".neq.", "!=")
.replace(".eq.", "==")
.replace(".gt.", ">")
.replace(".lt.", "<")
)
exec_str += "if ( %s ): %s = %s \n" % (cond, cdv.name, case.value)
else:
exec_str += "else: %s = %s \n" % (cdv.name, case.value)

exec_str += "\n"

exec_str += 'return_vals["%s"] = %s\n' % (cdv.name, cdv.name)
exec_str = "from math import exp # only one required for nml2?\n" + exec_str
# logger.info('Exec %s'%exec_str)
exec(exec_str)

return return_vals

0 comments on commit 830501b

Please sign in to comment.