Skip to content

Commit

Permalink
Fix handling of generate_plot() in pynml module
Browse files Browse the repository at this point in the history
  • Loading branch information
pgleeson committed Nov 15, 2023
1 parent 9d33e03 commit 7700491
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
6 changes: 3 additions & 3 deletions examples/LeakConductance.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ TITLE Mod file for component: Component(id=LeakConductance type=ionChannelHH)
COMMENT

This NEURON file has been generated by org.neuroml.export (see https://github.com/NeuroML/org.neuroml.export)
org.neuroml.export v1.9.1
org.neuroml.model v1.9.1
jLEMS v0.10.8
org.neuroml.export v1.10.0
org.neuroml.model v1.10.0
jLEMS v0.11.0

ENDCOMMENT

Expand Down
53 changes: 42 additions & 11 deletions pyneuroml/pynml.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,54 @@
if "matplotlib" in k:
matplotlib_imported = True
break

if matplotlib_imported is True:
# to maintain API compatibility:
# so that existing scripts that use: from pynml import generate_plot
# continue to work
from pyneuroml.plot import generate_plot, generate_interactive_plot # noqa
else:
logger.warning("Matplotlib has not been imported, not importing plotting functions")
logger.warning("Please import these explicitly from pyneuroml.plot")
warnings.warn(
"""
Please note that these plotting methods will be removed from the pynml
module in the future. Please import plotting methods expliclitly from
the pyneuroml.plot sub module.
""",
FutureWarning,
stacklevel=2,
)
# Define a new method, which only gets called if a user explicitly tries to
# run generate_plot (and so requires matplotlib). If it's never called, matplotlib
# doesn't get imported

def generate_plot(*args, **kwargs):
try:
import matplotlib
from pyneuroml.plot import generate_plot as gp
return gp(*args, **kwargs)

except Exception as e:

logger.error("Matplotlib not found!")
warnings.warn(
"""
Please note that these plotting methods will be removed from the pynml
module in the future. Please import plotting methods explicitly from
the pyneuroml.plot sub module.
""",
FutureWarning,
stacklevel=2,
)

def generate_interactive_plot(*args, **kwargs):
try:
import matplotlib
from pyneuroml.plot import generate_interactive_plot as gp
return gp(*args, **kwargs)

except Exception as e:

logger.error("Matplotlib not found!")
warnings.warn(
"""
Please note that these plotting methods will be removed from the pynml
module in the future. Please import plotting methods explicitly from
the pyneuroml.plot sub module.
""",
FutureWarning,
stacklevel=2,
)

DEFAULTS = {
"v": False,
Expand Down

0 comments on commit 7700491

Please sign in to comment.