Skip to content

Commit

Permalink
Merge pull request #787 from sanjayankur31/fix/786
Browse files Browse the repository at this point in the history
fix(imports): only import matplotlib where needed and if gui is enabled
  • Loading branch information
pgleeson authored Nov 2, 2023
2 parents 71860e9 + 0b40a34 commit 9dd3591
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 129 deletions.
3 changes: 0 additions & 3 deletions netpyne/analysis/csd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions netpyne/plotting/plotCSD.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions netpyne/plotting/plotLFPLocations.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 5 additions & 2 deletions netpyne/plotting/plotLFPSpectrogram.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion netpyne/plotting/plotRaster.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion netpyne/plotting/plotSpikeFreq.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion netpyne/plotting/plotSpikeHist.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions netpyne/plotting/plotTimeSeries.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions netpyne/plotting/plotTimeSeriesPSD.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
125 changes: 66 additions & 59 deletions netpyne/plotting/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion netpyne/support/morlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion netpyne/support/morphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
105 changes: 55 additions & 50 deletions netpyne/support/scalebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion netpyne/support/stackedBarGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

###############################################################################

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pandas
future
tables
bokeh
pyneuroml
pyneuroml>=1.1.5
neuron
pytest
inspyred
Expand Down

0 comments on commit 9dd3591

Please sign in to comment.