Skip to content

Commit

Permalink
Fix Python lib issue with sympy solver (#1165)
Browse files Browse the repository at this point in the history
* Fix for Python sympy solver

* Add corresponding test

* Update requirements

---------

Co-authored-by: Luc Grosheintz <luc.grosheintz@gmail.com>
  • Loading branch information
JCGoran and 1uc committed Feb 27, 2024
1 parent 2cff7e0 commit 1baa906
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
34 changes: 34 additions & 0 deletions nmodl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
import os
import sys

if sys.version_info >= (3, 9):
from importlib.resources import files
else:
from importlib_resources import files

from find_libpython import find_libpython

# try to add libpython*.so path to environment if not already set
try:
os.environ["NMODL_PYLIB"] = os.environ.get(
"NMODL_PYLIB",
find_libpython(),
)
except TypeError as exc:
raise RuntimeError(
"find_libpython was unable to find the Python library on this platform; "
"please make sure that the Python library is installed correctly\n"
"You can also try to manually set the NMODL_PYLIB environmental variable "
"to the Python library"
) from exc

# add nmodl home to environment (i.e. necessary for nrnunits.lib) if not
# already set
# `files` will automatically raise a `ModuleNotFoundError`
os.environ["NMODLHOME"] = os.environ.get(
"NMODLHOME",
str(files("nmodl") / ".data"),
)


try:
# Try importing but catch exception in case bindings are not available
from ._nmodl import NmodlDriver, to_json, to_nmodl # noqa
from ._nmodl import __version__

__all__ = ["NmodlDriver", "to_json", "to_nmodl"]
except ImportError:
print("[NMODL] [warning] :: Python bindings are not available")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ sympy
numpy
find_libpython
scikit-build
importlib_resources;python_version<"3.9"
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def run(self, *args, **kwargs):
install_requirements = [
"PyYAML>=3.13",
"sympy>=1.3",
"find_libpython"
"find_libpython",
"importlib_resources;python_version<'3.9'",
]


Expand Down
15 changes: 15 additions & 0 deletions test/unit/pybind/test_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,18 @@ def visit_range_var(self, node):
}
"""
assert str(modast) == one_var_after


def test_sympy_conductance_visitor():
"""
Make sure NMODL sets the correct env variables to be able to run the sympy visitor
"""
program = """NEURON {
USEION na READ ena WRITE ina
RANGE gna
}
BREAKPOINT {
ina = gna*(v - ena)
}"""
driver = nmodl.NmodlDriver()
visitor.SympyConductanceVisitor().visit_program(driver.parse_string(program))

0 comments on commit 1baa906

Please sign in to comment.