From 797994a11ac5ec6bee9ea01c092947d0c34115f1 Mon Sep 17 00:00:00 2001
From: Marcus Müller <marcus@hostalia.de>
Date: Fri, 24 Aug 2018 23:00:55 +0200
Subject: Replace scipy/pylab where numpy/pyplot is sufficient

This should reduce the number of times users are prompted to install
pylab || scipy when they'd actually get away with functionality fully
contained in numpy and matplotlib.

This only solves the obvious cases. There's some usage of `pylab.mlab`
that would need more than 20s of consideration.
---
 gr-utils/python/utils/plot_data.py     | 19 ++++-------------
 gr-utils/python/utils/plot_fft_base.py | 26 ++++++++++-------------
 gr-utils/python/utils/plot_psd_base.py | 39 ++++++++++++++--------------------
 3 files changed, 31 insertions(+), 53 deletions(-)

(limited to 'gr-utils/python')

diff --git a/gr-utils/python/utils/plot_data.py b/gr-utils/python/utils/plot_data.py
index a054147114..dc9346c484 100644
--- a/gr-utils/python/utils/plot_data.py
+++ b/gr-utils/python/utils/plot_data.py
@@ -26,18 +26,7 @@ from __future__ import print_function
 from __future__ import division
 from __future__ import unicode_literals
 
-try:
-    import scipy
-except ImportError:
-    print("Please install SciPy to run this script (http://www.scipy.org/)")
-    raise SystemExit(1)
-
-try:
-    from pylab import *
-except ImportError:
-    print("Please install Matplotlib to run this script (http://matplotlib.sourceforge.net/)")
-    raise SystemExit(1)
-
+import numpy
 
 class plot_data(object):
     def __init__(self, datatype, filenames, options):
@@ -88,12 +77,12 @@ class plot_data(object):
     def get_data(self, hfile):
         self.text_file_pos.set_text("File Position: %d" % (hfile.tell()//self.sizeof_data))
         try:
-            f = scipy.fromfile(hfile, dtype=self.datatype, count=self.block_length)
+            f = numpy.fromfile(hfile, dtype=self.datatype, count=self.block_length)
         except MemoryError:
             print("End of File")
         else:
-            self.f = scipy.array(f)
-            self.time = scipy.array([i*(1 / self.sample_rate) for i in range(len(self.f))])
+            self.f = numpy.array(f)
+            self.time = numpy.array([i*(1 / self.sample_rate) for i in range(len(self.f))])
 
     def make_plots(self):
         self.sp_f = self.fig.add_subplot(2,1,1, position=[0.075, 0.2, 0.875, 0.6])
diff --git a/gr-utils/python/utils/plot_fft_base.py b/gr-utils/python/utils/plot_fft_base.py
index 5040aefa87..ec26f059f9 100644
--- a/gr-utils/python/utils/plot_fft_base.py
+++ b/gr-utils/python/utils/plot_fft_base.py
@@ -24,12 +24,8 @@ from __future__ import print_function
 from __future__ import division
 from __future__ import unicode_literals
 
-try:
-    import scipy
-    from scipy import fftpack
-except ImportError:
-    print("Please install SciPy to run this script (http://www.scipy.org/)")
-    raise SystemExit(1)
+import numpy
+from numpy.fft import fftpack
 
 try:
     from pylab import *
@@ -46,7 +42,7 @@ class plot_fft_base(object):
         self.start = options.start
         self.sample_rate = options.sample_rate
 
-        self.datatype = getattr(scipy, datatype)
+        self.datatype = numpy.complex64
         self.sizeof_data = self.datatype().nbytes    # number of bytes per sample in file
 
         self.axis_font_size = 16
@@ -86,22 +82,22 @@ class plot_fft_base(object):
         self.position = self.hfile.tell() / self.sizeof_data
         self.text_file_pos.set_text("File Position: %d" % (self.position))
         try:
-            self.iq = scipy.fromfile(self.hfile, dtype=self.datatype, count=self.block_length)
+            self.iq = numpy.fromfile(self.hfile, dtype=self.datatype, count=self.block_length)
         except MemoryError:
             print("End of File")
         else:
             self.iq_fft = self.dofft(self.iq)
 
             tstep = 1.0 / self.sample_rate
-            #self.time = scipy.array([tstep*(self.position + i) for i in range(len(self.iq))])
-            self.time = scipy.array([tstep*(i) for i in range(len(self.iq))])
+            #self.time = numpy.array([tstep*(self.position + i) for i in range(len(self.iq))])
+            self.time = numpy.array([tstep*(i) for i in range(len(self.iq))])
 
             self.freq = self.calc_freq(self.time, self.sample_rate)
 
     def dofft(self, iq):
         N = len(iq)
-        iq_fft = scipy.fftpack.fftshift(scipy.fft(iq))       # fft and shift axis
-        iq_fft = 20*scipy.log10(abs((iq_fft+1e-15) / N)) # convert to decibels, adjust power
+        iq_fft = numpy.fft.fftshift(fftpack.fft(iq))       # fft and shift axis
+        iq_fft = 20*numpy.log10(abs((iq_fft+1e-15) / N)) # convert to decibels, adjust power
         # adding 1e-15 (-300 dB) to protect against value errors if an item in iq_fft is 0
         return iq_fft
 
@@ -109,7 +105,7 @@ class plot_fft_base(object):
         N = len(time)
         Fs = 1.0 / (time.max( - time.min()))
         Fn = 0.5 * sample_rate
-        freq = scipy.array([-Fn + i*Fs for i in range(N)])
+        freq = numpy.array([-Fn + i*Fs for i in range(N)])
         return freq
 
     def make_plots(self):
@@ -161,8 +157,8 @@ class plot_fft_base(object):
         draw()
 
     def zoom(self, event):
-        newxlim = scipy.array(self.sp_iq.get_xlim())
-        curxlim = scipy.array(self.xlim)
+        newxlim = numpy.array(self.sp_iq.get_xlim())
+        curxlim = numpy.array(self.xlim)
         if(newxlim[0] != curxlim[0] or newxlim[1] != curxlim[1]):
             self.xlim = newxlim
             #xmin = max(0, int(ceil(self.sample_rate*(self.xlim[0] - self.position))))
diff --git a/gr-utils/python/utils/plot_psd_base.py b/gr-utils/python/utils/plot_psd_base.py
index 0a0f3cab34..eb9a5a6431 100644
--- a/gr-utils/python/utils/plot_psd_base.py
+++ b/gr-utils/python/utils/plot_psd_base.py
@@ -23,13 +23,7 @@
 from __future__ import print_function
 from __future__ import division
 from __future__ import unicode_literals
-
-try:
-    import scipy
-    from scipy import fftpack
-except ImportError:
-    print("Please install SciPy to run this script (http://www.scipy.org/)")
-    raise SystemExit(1)
+import numpy
 
 try:
     from pylab import *
@@ -38,7 +32,6 @@ except ImportError:
     raise SystemExit(1)
 
 from argparse import ArgumentParser
-from scipy import log10
 from gnuradio.eng_arg import eng_float, intx
 
 class plot_psd_base(object):
@@ -52,7 +45,7 @@ class plot_psd_base(object):
 
         self.dospec = options.enable_spec  # if we want to plot the spectrogram
 
-        self.datatype = getattr(scipy, datatype) #scipy.complex64
+        self.datatype = numpy.complex64
         self.sizeof_data = self.datatype().nbytes    # number of bytes per sample in file
 
         self.axis_font_size = 16
@@ -83,7 +76,7 @@ class plot_psd_base(object):
         self.button_right = Button(self.button_right_axes, ">")
         self.button_right_callback = self.button_right.on_clicked(self.button_right_click)
 
-        self.xlim = scipy.array(self.sp_iq.get_xlim())
+        self.xlim = numpy.array(self.sp_iq.get_xlim())
 
         self.manager = get_current_fig_manager()
         connect('draw_event', self.zoom)
@@ -94,17 +87,17 @@ class plot_psd_base(object):
         self.position = self.hfile.tell() / self.sizeof_data
         self.text_file_pos.set_text("File Position: %d" % self.position)
         try:
-            self.iq = scipy.fromfile(self.hfile, dtype=self.datatype, count=self.block_length)
+            self.iq = numpy.fromfile(self.hfile, dtype=self.datatype, count=self.block_length)
         except MemoryError:
             print("End of File")
             return False
         else:
-            # retesting length here as newer version of scipy does not throw a MemoryError, just
+            # retesting length here as newer version of numpy does not throw a MemoryError, just
             # returns a zero-length array
             if(len(self.iq) > 0):
                 tstep = 1.0 / self.sample_rate
-                #self.time = scipy.array([tstep*(self.position + i) for i in range(len(self.iq))])
-                self.time = scipy.array([tstep*(i) for i in range(len(self.iq))])
+                #self.time = numpy.array([tstep*(self.position + i) for i in range(len(self.iq))])
+                self.time = numpy.array([tstep*(i) for i in range(len(self.iq))])
 
                 self.iq_psd, self.freq = self.dopsd(self.iq)
                 return True
@@ -115,11 +108,11 @@ class plot_psd_base(object):
     def dopsd(self, iq):
         ''' Need to do this here and plot later so we can do the fftshift '''
         overlap = self.psdfftsize / 4
-        winfunc = scipy.blackman
+        winfunc = numpy.blackman
         psd,freq = mlab.psd(iq, self.psdfftsize, self.sample_rate,
                             window = lambda d: d*winfunc(self.psdfftsize),
                             noverlap = overlap)
-        psd = 10.0*log10(abs(psd))
+        psd = 10.0*numpy.log10(abs(psd))
         return (psd, freq)
 
     def make_plots(self):
@@ -179,7 +172,7 @@ class plot_psd_base(object):
 
     def draw_spec(self, t, s):
         overlap = self.specfftsize / 4
-        winfunc = scipy.blackman
+        winfunc = numpy.blackman
         self.sp_spec.clear()
         self.sp_spec.specgram(s, self.specfftsize, self.sample_rate,
                               window = lambda d: d*winfunc(self.specfftsize),
@@ -192,26 +185,26 @@ class plot_psd_base(object):
         if self.dospec:
             self.draw_spec(self.time, self.iq)
 
-        self.xlim = scipy.array(self.sp_iq.get_xlim()) # so zoom doesn't get called
+        self.xlim = numpy.array(self.sp_iq.get_xlim()) # so zoom doesn't get called
 
         draw()
 
     def zoom(self, event):
-        newxlim = scipy.array(self.sp_iq.get_xlim())
-        curxlim = scipy.array(self.xlim)
+        newxlim = numpy.array(self.sp_iq.get_xlim())
+        curxlim = numpy.array(self.xlim)
         if(newxlim[0] != curxlim[0] or newxlim[1] != curxlim[1]):
             #xmin = max(0, int(ceil(self.sample_rate*(newxlim[0] - self.position))))
             #xmax = min(int(ceil(self.sample_rate*(newxlim[1] - self.position))), len(self.iq))
             xmin = max(0, int(ceil(self.sample_rate*(newxlim[0]))))
             xmax = min(int(ceil(self.sample_rate*(newxlim[1]))), len(self.iq))
 
-            iq = scipy.array(self.iq[xmin : xmax])
-            time = scipy.array(self.time[xmin : xmax])
+            iq = numpy.array(self.iq[xmin : xmax])
+            time = numpy.array(self.time[xmin : xmax])
 
             iq_psd, freq = self.dopsd(iq)
 
             self.draw_psd(freq, iq_psd)
-            self.xlim = scipy.array(self.sp_iq.get_xlim())
+            self.xlim = numpy.array(self.sp_iq.get_xlim())
 
             draw()
 
-- 
cgit v1.2.3