Skip to content

Commit

Permalink
Merge pull request #29 from moritz-ritter/0_6_5_no_matplotlib
Browse files Browse the repository at this point in the history
Made matplotlib+pylab dependencies optional
  • Loading branch information
cokelaer committed Aug 7, 2017
2 parents 6fda258 + 61e5d98 commit af4be8a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 30 deletions.
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
package_dir={ '' : 'src' },

# Dependencies
install_requires = ['matplotlib', 'numpy', 'scipy', 'easydev'],
install_requires=['numpy', 'scipy', 'easydev'],
extras_require={
'plot': ['matplotlib']
},
data_files = data_files,
platforms=["Linux"],
classifiers=["Development Status :: 1 - Planning",
Expand Down
16 changes: 12 additions & 4 deletions src/spectrum/correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@
"""#from numpy.fft import fft, ifft
import numpy
from numpy import arange, isrealobj
from pylab import rms_flat
# from pylab import rms_flat


__all__ = ['CORRELATION', 'xcorr']


def pylab_rms_flat(a):
"""
Return the root mean square of all the elements of *a*, flattened out.
(Copied 1:1 from matplotlib.mlab.)
"""
return numpy.sqrt(numpy.mean(numpy.absolute(a) ** 2))


def CORRELATION(x, y=None, maxlags=None, norm='unbiased'):
r"""Correlation function
Expand Down Expand Up @@ -104,8 +112,8 @@ def CORRELATION(x, y=None, maxlags=None, norm='unbiased'):
r = numpy.zeros(maxlags, dtype=complex)

if norm == 'coeff':
rmsx = rms_flat(x)
rmsy = rms_flat(y)
rmsx = pylab_rms_flat(x)
rmsy = pylab_rms_flat(y)

for k in range(0, maxlags+1):
nk = N - k - 1
Expand Down Expand Up @@ -208,7 +216,7 @@ def xcorr(x, y=None, maxlags=None, norm='biased'):
res = res[lags] / (float(N)-abs(arange(-N+1, N)))[lags]
elif norm == 'coeff':
Nf = float(N)
rms = rms_flat(x) * rms_flat(y)
rms = pylab_rms_flat(x) * pylab_rms_flat(y)
res = res[lags] / rms / Nf
else:
res = res[lags]
Expand Down
8 changes: 1 addition & 7 deletions src/spectrum/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
:Reference: [Marple]_
"""
from pylab import plot, linspace, xlabel, ylabel, grid
import numpy
from numpy import arange, pi, cos

Expand Down Expand Up @@ -150,14 +149,9 @@ def __init__(self, data, sampling=1):
def plot(self, **kargs):
"""Plot the data set, using the sampling information to set the x-axis
correctly."""
from pylab import plot, linspace, xlabel, ylabel, grid
time = linspace(1*self.dt, self.N*self.dt, self.N)
plot(time, self.data, **kargs)
xlabel('Time')
ylabel('Amplitude')
grid(True)






4 changes: 2 additions & 2 deletions src/spectrum/mtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import os
from os.path import join as pj
from spectrum.tools import nextpow2
from pylab import semilogy
from numpy.ctypeslib import load_library

"""
Expand Down Expand Up @@ -53,7 +52,7 @@
print("Library %s not found" % lib_name)


def pmtm(x, NW=None, k=None, NFFT=None, e=None, v=None, method='adapt', show=True):
def pmtm(x, NW=None, k=None, NFFT=None, e=None, v=None, method='adapt', show=False):
"""Multitapering spectral estimation
:param array x: the data
Expand Down Expand Up @@ -173,6 +172,7 @@ def pmtm(x, NW=None, k=None, NFFT=None, e=None, v=None, method='adapt', show=Tru
weights=wk

if show is True:
from pylab import semilogy
if method == "adapt":
Sk = np.mean(Sk * weights, axis=1)
else:
Expand Down
12 changes: 6 additions & 6 deletions src/spectrum/periodogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@
"""
from .window import Window
from .psd import Spectrum, FourierSpectrum
from pylab import pi, fft, mean, rfft
from numpy import array, ceil
from numpy import array, ceil, pi, mean, linspace
from numpy.fft import fft, rfft
import numpy
import pylab as plt



Expand Down Expand Up @@ -191,9 +190,10 @@ def WelchPeriodogram(data, NFFT=None, sampling=1., **kargs):
"""
from pylab import psd
spectrum = Spectrum(data, sampling=1.)

P = plt.psd(data, NFFT, Fs=sampling, **kargs)
P = psd(data, NFFT, Fs=sampling, **kargs)
spectrum.psd = P[0]
#spectrum.__Spectrum_sides = 'twosided'

Expand Down Expand Up @@ -305,10 +305,10 @@ def DaniellPeriodogram(data, P, NFFT=None, detrend='mean', sampling=1.,

#todo: check this
if datatype == 'complex':
freq = plt.linspace(0,sampling, len(newpsd))
freq = linspace(0,sampling, len(newpsd))
else:
df = 1./sampling
freq = plt.linspace(0,sampling/2., len(newpsd))
freq = linspace(0,sampling/2., len(newpsd))
#psd.refreq(2*psd.size()/A.freq());
#psd.retime(-1./psd.freq()+1./A.size());

Expand Down
17 changes: 8 additions & 9 deletions src/spectrum/psd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""This module provides the Base class for PSDs"""
import pylab as plt
import pylab
import numpy

from spectrum.tools import nextpow2
Expand Down Expand Up @@ -521,7 +519,7 @@ def _getdatatype(self):

def scale(self):
if self.scale_by_freq is True:
self.psd *= 2*plt.pi/self.df
self.psd *= 2*numpy.pi/self.df

def frequencies(self, sides=None):

Expand Down Expand Up @@ -632,6 +630,7 @@ def plot(self, filename=None, norm=False, ylim=None,
p.plot(norm=True, marker='o')
"""
import pylab
#First, check that psd attribute is up-to-date
if self.modified is True:
raise errors.SpectrumModifiedError
Expand Down Expand Up @@ -667,8 +666,8 @@ def plot(self, filename=None, norm=False, ylim=None,
from pylab import ylim as plt_ylim

if 'ax' in list(kargs.keys()):
save_ax = plt.gca()
plt.sca(kargs['ax'])
save_ax = pylab.gca()
pylab.sca(kargs['ax'])
rollback = True
del kargs['ax']
else:
Expand All @@ -679,8 +678,8 @@ def plot(self, filename=None, norm=False, ylim=None,
else:
pylab.plot(frequencies, 10*pylab.log10(psd),**kargs)

plt.xlabel('Frequency')
plt.ylabel('Power (dB)')
pylab.xlabel('Frequency')
pylab.ylabel('Power (dB)')
pylab.grid(True)
if ylim:
plt_ylim(ylim)
Expand All @@ -693,7 +692,7 @@ def plot(self, filename=None, norm=False, ylim=None,
if filename:
pylab.savefig(filename)
if rollback:
plt.sca(save_ax)
pylab.sca(save_ax)
del psd, frequencies #is it needed?

def power(self):
Expand All @@ -714,7 +713,7 @@ def power(self):
if self.scale_by_freq == False:
return sum(self.psd) * len(self.psd)
else:
return sum(self.psd) * self.df/(2.*plt.pi)
return sum(self.psd) * self.df/(2.*numpy.pi)

def _str_title(self):
return "Spectrum summary\n"
Expand Down
3 changes: 2 additions & 1 deletion src/spectrum/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def compute_response(self, **kargs):
"""
from pylab import fft, fftshift, log10
from numpy import log10
from numpy.fft import fft, fftshift

norm = kargs.get('norm', self.norm)

Expand Down

0 comments on commit af4be8a

Please sign in to comment.