Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pynml): do not import plotters if mpl is not already imported #269

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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