Skip to content

Commit

Permalink
Merge pull request #269 from NeuroML/feat/do-not-import-mpl
Browse files Browse the repository at this point in the history
feat(pynml): do not import plotters if mpl is not already imported
  • Loading branch information
sanjayankur31 authored Nov 2, 2023
2 parents 9e1adce + d32547f commit 6a9fa8e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
11 changes: 6 additions & 5 deletions pyneuroml/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pyneuroml.lems.LEMSSimulation import LEMSSimulation
from pyneuroml.lems import generate_lems_file_for_neuroml
from pyneuroml.utils.plot import get_next_hex_color
from pyneuroml.plot import generate_plot
import neuroml as nml
from pyelectro.analysis import max_min
from pyelectro.analysis import mean_spike_frequency
Expand Down Expand Up @@ -381,7 +382,7 @@ def generate_current_vs_frequency_curve(
iv_results[stims[i]] = v_end

if plot_voltage_traces:
traces_ax = pynml.generate_plot(
traces_ax = generate_plot(
times_results,
volts_results,
"Membrane potential traces for: %s" % nml2_file,
Expand All @@ -403,7 +404,7 @@ def generate_current_vs_frequency_curve(
stims = sorted(if_results.keys())
stims_pA = [ii * 1000 for ii in stims]
freqs = [if_results[s] for s in stims]
if_ax = pynml.generate_plot(
if_ax = generate_plot(
[stims_pA],
[freqs],
"Firing frequency versus injected current for: %s" % nml2_file,
Expand Down Expand Up @@ -455,7 +456,7 @@ def generate_current_vs_frequency_curve(
xs[-1].append(stim * 1000)
ys[-1].append(iv_results[stim])

iv_ax = pynml.generate_plot(
iv_ax = generate_plot(
xs,
ys,
"V at %sms versus I below threshold for: %s" % (end_stim, nml2_file),
Expand Down Expand Up @@ -621,7 +622,7 @@ def analyse_spiketime_vs_dt(
markers.append("")
colors.append("k")

pynml.generate_plot(
generate_plot(
spxs,
spys,
"Spike times vs dt",
Expand All @@ -635,7 +636,7 @@ def analyse_spiketime_vs_dt(
)

if verbose:
pynml.generate_plot(
generate_plot(
xs,
ys,
"Membrane potentials in %s for %s" % (simulator, dts),
Expand Down
32 changes: 25 additions & 7 deletions pyneuroml/pynml.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,31 @@
import neuroml.loaders as loaders
import neuroml.writers as writers

# 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
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

matplotlib_imported = False
for k in sys.modules.keys():
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,
)

DEFAULTS = {
"v": False,
Expand All @@ -58,9 +79,6 @@

lems_model_with_units = None

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

version_string = "pyNeuroML v{} (libNeuroML v{}, jNeuroML v{})".format(
__version__, neuroml.__version__, JNEUROML_VERSION
)
Expand Down

0 comments on commit 6a9fa8e

Please sign in to comment.