diff --git a/netpyne/analysis/csd.py b/netpyne/analysis/csd.py index 73f06ae9b..ba61f7100 100644 --- a/netpyne/analysis/csd.py +++ b/netpyne/analysis/csd.py @@ -20,9 +20,6 @@ import numpy as np import scipy from numbers import Number -import matplotlib -from matplotlib import pyplot as plt -from matplotlib import ticker as ticker import json import sys import os diff --git a/netpyne/plotting/plotCSD.py b/netpyne/plotting/plotCSD.py index b2ddae562..fd9fbb5cf 100644 --- a/netpyne/plotting/plotCSD.py +++ b/netpyne/plotting/plotCSD.py @@ -1,10 +1,13 @@ # PLOTTING CSD +from netpyne import __gui__ +if __gui__: + import matplotlib + from matplotlib import pyplot as plt + from ..analysis.utils import exception, _showFigure import numpy as np import scipy -import matplotlib -from matplotlib import pyplot as plt @exception diff --git a/netpyne/plotting/plotLFPLocations.py b/netpyne/plotting/plotLFPLocations.py index 3c5e5c725..16511a5c4 100644 --- a/netpyne/plotting/plotLFPLocations.py +++ b/netpyne/plotting/plotLFPLocations.py @@ -1,7 +1,5 @@ # Generate plots of LFP (local field potentials) and related analyses -import matplotlib.patches as mpatches -import matplotlib.pyplot as plt import math from ..analysis.utils import exception # , loadData from ..analysis.tools import loadData diff --git a/netpyne/plotting/plotLFPSpectrogram.py b/netpyne/plotting/plotLFPSpectrogram.py index 33e3a91d4..b34558233 100644 --- a/netpyne/plotting/plotLFPSpectrogram.py +++ b/netpyne/plotting/plotLFPSpectrogram.py @@ -1,7 +1,10 @@ # Generate plots of LFP (local field potentials) and related analyses -import matplotlib.patches as mpatches -import matplotlib.pyplot as plt +from netpyne import __gui__ + +if __gui__: + import matplotlib.pyplot as plt + import math from ..analysis.utils import exception # , loadData from ..analysis.tools import loadData diff --git a/netpyne/plotting/plotRaster.py b/netpyne/plotting/plotRaster.py index a5be18c9a..0c3c44e84 100644 --- a/netpyne/plotting/plotRaster.py +++ b/netpyne/plotting/plotRaster.py @@ -1,6 +1,9 @@ # Generate a raster plot of spiking -import matplotlib.patches as mpatches +from netpyne import __gui__ + +if __gui__: + import matplotlib.patches as mpatches from ..analysis.utils import exception # , loadData from ..analysis.tools import loadData from .plotter import ScatterPlotter diff --git a/netpyne/plotting/plotSpikeFreq.py b/netpyne/plotting/plotSpikeFreq.py index fa639a71e..3c2ace377 100644 --- a/netpyne/plotting/plotSpikeFreq.py +++ b/netpyne/plotting/plotSpikeFreq.py @@ -1,7 +1,9 @@ # Generate a spike frequency plot +from netpyne import __gui__ +if __gui__: + import matplotlib.patches as mpatches import numpy as np -import matplotlib.patches as mpatches from numbers import Number from ..analysis.utils import exception, _smooth1d from ..analysis.tools import loadData diff --git a/netpyne/plotting/plotSpikeHist.py b/netpyne/plotting/plotSpikeHist.py index 7c7171f97..8a640083d 100644 --- a/netpyne/plotting/plotSpikeHist.py +++ b/netpyne/plotting/plotSpikeHist.py @@ -1,7 +1,10 @@ # Generate a spike histogram +from netpyne import __gui__ + +if __gui__: + import matplotlib.patches as mpatches import numpy as np -import matplotlib.patches as mpatches from ..analysis.utils import exception from ..analysis.tools import loadData from .plotter import HistPlotter diff --git a/netpyne/plotting/plotTimeSeries.py b/netpyne/plotting/plotTimeSeries.py index 2285ddd2d..9fcf78a7b 100644 --- a/netpyne/plotting/plotTimeSeries.py +++ b/netpyne/plotting/plotTimeSeries.py @@ -1,7 +1,5 @@ # Generate plots of LFP (local field potentials) and related analyses -import matplotlib.patches as mpatches -import matplotlib.pyplot as plt import math from ..analysis.utils import exception # , loadData from ..analysis.tools import loadData diff --git a/netpyne/plotting/plotTimeSeriesPSD.py b/netpyne/plotting/plotTimeSeriesPSD.py index dca158548..f659418ba 100644 --- a/netpyne/plotting/plotTimeSeriesPSD.py +++ b/netpyne/plotting/plotTimeSeriesPSD.py @@ -1,7 +1,5 @@ # Generate plots of LFP (local field potentials) and related analyses -import matplotlib.patches as mpatches -import matplotlib.pyplot as plt import math from ..analysis.utils import exception # , loadData from ..analysis.tools import loadData diff --git a/netpyne/plotting/plotter.py b/netpyne/plotting/plotter.py index be967d265..c8cc2851c 100644 --- a/netpyne/plotting/plotter.py +++ b/netpyne/plotting/plotter.py @@ -3,13 +3,17 @@ """ -import matplotlib as mpl -import matplotlib.pyplot as plt +from netpyne import __gui__ + +if __gui__: + import matplotlib as mpl + import matplotlib.pyplot as plt + from matplotlib.offsetbox import AnchoredOffsetbox + import numpy as np from copy import deepcopy import pickle, json import os -from matplotlib.offsetbox import AnchoredOffsetbox try: basestring @@ -1244,64 +1248,67 @@ def plot(self, **kwargs): return self.fig -class _AnchoredScaleBar(AnchoredOffsetbox): - """ - A class used for adding scale bars to plots - - Modified from here: https://gist.github.com/dmeliza/3251476 - """ - - def __init__( - self, - axis, - sizex=0, - sizey=0, - labelx=None, - labely=None, - loc=4, - pad=0.1, - borderpad=0.1, - sep=2, - prop=None, - barcolor="black", - barwidth=None, - **kwargs - ): +try: + class _AnchoredScaleBar(AnchoredOffsetbox): """ - Draw a horizontal and/or vertical bar with the size in data coordinate - of the give axes. A label will be drawn underneath (center-aligned). - - - transform : the coordinate frame (typically axes.transData) - - sizex,sizey : width of x,y bar, in data units. 0 to omit - - labelx,labely : labels for x,y bars; None to omit - - loc : position in containing axes - - pad, borderpad : padding, in fraction of the legend font size (or prop) - - sep : separation between labels and bars in points. - - **kwargs : additional arguments passed to base class constructor + A class used for adding scale bars to plots + + Modified from here: https://gist.github.com/dmeliza/3251476 """ - from matplotlib.patches import Rectangle - from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea - - bars = AuxTransformBox(axis.transData) - if sizex: - if axis.xaxis_inverted(): - sizex = -sizex - bars.add_artist(Rectangle((0, 0), sizex, 0, ec=barcolor, lw=barwidth, fc="none")) - if sizey: - if axis.yaxis_inverted(): - sizey = -sizey - bars.add_artist(Rectangle((0, 0), 0, sizey, ec=barcolor, lw=barwidth, fc="none")) - - if sizex and labelx: - self.xlabel = TextArea(labelx) - bars = VPacker(children=[bars, self.xlabel], align="center", pad=0, sep=sep) - if sizey and labely: - self.ylabel = TextArea(labely) - bars = HPacker(children=[self.ylabel, bars], align="center", pad=0, sep=sep) - - AnchoredOffsetbox.__init__( - self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs - ) + + def __init__( + self, + axis, + sizex=0, + sizey=0, + labelx=None, + labely=None, + loc=4, + pad=0.1, + borderpad=0.1, + sep=2, + prop=None, + barcolor="black", + barwidth=None, + **kwargs + ): + """ + Draw a horizontal and/or vertical bar with the size in data coordinate + of the give axes. A label will be drawn underneath (center-aligned). + + - transform : the coordinate frame (typically axes.transData) + - sizex,sizey : width of x,y bar, in data units. 0 to omit + - labelx,labely : labels for x,y bars; None to omit + - loc : position in containing axes + - pad, borderpad : padding, in fraction of the legend font size (or prop) + - sep : separation between labels and bars in points. + - **kwargs : additional arguments passed to base class constructor + """ + from matplotlib.patches import Rectangle + from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea + + bars = AuxTransformBox(axis.transData) + if sizex: + if axis.xaxis_inverted(): + sizex = -sizex + bars.add_artist(Rectangle((0, 0), sizex, 0, ec=barcolor, lw=barwidth, fc="none")) + if sizey: + if axis.yaxis_inverted(): + sizey = -sizey + bars.add_artist(Rectangle((0, 0), 0, sizey, ec=barcolor, lw=barwidth, fc="none")) + + if sizex and labelx: + self.xlabel = TextArea(labelx) + bars = VPacker(children=[bars, self.xlabel], align="center", pad=0, sep=sep) + if sizey and labely: + self.ylabel = TextArea(labely) + bars = HPacker(children=[self.ylabel, bars], align="center", pad=0, sep=sep) + + AnchoredOffsetbox.__init__( + self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs + ) +except NameError: + print("-nogui passed, matplotlib is unavailable") def _add_scalebar( diff --git a/netpyne/support/morlet.py b/netpyne/support/morlet.py index ca2df6bff..a58b1b793 100644 --- a/netpyne/support/morlet.py +++ b/netpyne/support/morlet.py @@ -15,9 +15,12 @@ from __future__ import unicode_literals from __future__ import absolute_import +from netpyne import __gui__ +if __gui__: + import matplotlib.pyplot as plt + import numpy as np import scipy.signal as sps -import matplotlib.pyplot as plt def index2ms(idx, sampr): diff --git a/netpyne/support/morphology.py b/netpyne/support/morphology.py index e17d26352..4b2025625 100644 --- a/netpyne/support/morphology.py +++ b/netpyne/support/morphology.py @@ -20,7 +20,9 @@ from builtins import object import numpy as np import pylab as plt -from matplotlib.pyplot import cm +from netpyne import __gui__ +if __gui__: + from matplotlib.pyplot import cm import string from neuron import h import numbers diff --git a/netpyne/support/scalebar.py b/netpyne/support/scalebar.py index e6ec7b375..786835f04 100644 --- a/netpyne/support/scalebar.py +++ b/netpyne/support/scalebar.py @@ -16,56 +16,61 @@ from future import standard_library standard_library.install_aliases() -from matplotlib.offsetbox import AnchoredOffsetbox - - -class AnchoredScaleBar(AnchoredOffsetbox): - def __init__( - self, - transform, - sizex=0, - sizey=0, - labelx=None, - labely=None, - loc=4, - pad=0.1, - borderpad=0.1, - sep=2, - prop=None, - barcolor="black", - barwidth=None, - **kwargs - ): - """ - Draw a horizontal and/or vertical bar with the size in data coordinate - of the give axes. A label will be drawn underneath (center-aligned). - - transform : the coordinate frame (typically axes.transData) - - sizex,sizey : width of x,y bar, in data units. 0 to omit - - labelx,labely : labels for x,y bars; None to omit - - loc : position in containing axes - - pad, borderpad : padding, in fraction of the legend font size (or prop) - - sep : separation between labels and bars in points. - - **kwargs : additional arguments passed to base class constructor - """ - from matplotlib.patches import Rectangle - from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea - - bars = AuxTransformBox(transform) - if sizex: - bars.add_artist(Rectangle((0, 0), sizex, 0, ec=barcolor, lw=barwidth, fc="none")) - if sizey: - bars.add_artist(Rectangle((0, 0), 0, sizey, ec=barcolor, lw=barwidth, fc="none")) - - if sizex and labelx: - self.xlabel = TextArea(labelx, minimumdescent=False) - bars = VPacker(children=[bars, self.xlabel], align="center", pad=0, sep=sep) - if sizey and labely: - self.ylabel = TextArea(labely) - bars = HPacker(children=[self.ylabel, bars], align="center", pad=0, sep=sep) - - AnchoredOffsetbox.__init__( - self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs - ) +from netpyne import __gui__ + +if __gui__: + from matplotlib.offsetbox import AnchoredOffsetbox + +try: + class AnchoredScaleBar(AnchoredOffsetbox): + def __init__( + self, + transform, + sizex=0, + sizey=0, + labelx=None, + labely=None, + loc=4, + pad=0.1, + borderpad=0.1, + sep=2, + prop=None, + barcolor="black", + barwidth=None, + **kwargs + ): + """ + Draw a horizontal and/or vertical bar with the size in data coordinate + of the give axes. A label will be drawn underneath (center-aligned). + - transform : the coordinate frame (typically axes.transData) + - sizex,sizey : width of x,y bar, in data units. 0 to omit + - labelx,labely : labels for x,y bars; None to omit + - loc : position in containing axes + - pad, borderpad : padding, in fraction of the legend font size (or prop) + - sep : separation between labels and bars in points. + - **kwargs : additional arguments passed to base class constructor + """ + from matplotlib.patches import Rectangle + from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea + + bars = AuxTransformBox(transform) + if sizex: + bars.add_artist(Rectangle((0, 0), sizex, 0, ec=barcolor, lw=barwidth, fc="none")) + if sizey: + bars.add_artist(Rectangle((0, 0), 0, sizey, ec=barcolor, lw=barwidth, fc="none")) + + if sizex and labelx: + self.xlabel = TextArea(labelx, minimumdescent=False) + bars = VPacker(children=[bars, self.xlabel], align="center", pad=0, sep=sep) + if sizey and labely: + self.ylabel = TextArea(labely) + bars = HPacker(children=[self.ylabel, bars], align="center", pad=0, sep=sep) + + AnchoredOffsetbox.__init__( + self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs + ) +except NameError: + print("-nogui passed, matplotlib is unavailable") def add_scalebar( diff --git a/netpyne/support/stackedBarGraph.py b/netpyne/support/stackedBarGraph.py index 3f7691a13..176c85bf2 100644 --- a/netpyne/support/stackedBarGraph.py +++ b/netpyne/support/stackedBarGraph.py @@ -48,7 +48,9 @@ ############################################################################### import numpy as np -from matplotlib import pyplot as plt +from netpyne import __gui__ +if __gui__: + from matplotlib import pyplot as plt ############################################################################### diff --git a/requirements.txt b/requirements.txt index 07c0454e9..da53720c0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,7 @@ pandas future tables bokeh -pyneuroml +pyneuroml>=1.1.5 neuron pytest inspyred