From 8cad4431059ffa7c709d83faa8b72d5ec1a350b4 Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Thu, 2 Nov 2023 11:59:44 +0000 Subject: [PATCH] feat(pynml): do not import plotters if mpl is not already imported Note: remove these functions from pynml in the future See also: https://github.com/suny-downstate-medical-center/netpyne/pull/787 --- pyneuroml/pynml.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/pyneuroml/pynml.py b/pyneuroml/pynml.py index df1f45bb..6d025873 100644 --- a/pyneuroml/pynml.py +++ b/pyneuroml/pynml.py @@ -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, @@ -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 )