From d51819018aef09ba5918b5974e2997712ae1b2b9 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 19 Nov 2012 01:46:34 -0500 Subject: [PATCH] FFT_tools: try to import pyplot up front But if the import fails, don't raise the exception unless we actually try to use pyplot. --- FFT_tools.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/FFT_tools.py b/FFT_tools.py index 9a6041a..95b9981 100644 --- a/FFT_tools.py +++ b/FFT_tools.py @@ -30,6 +30,11 @@ import logging as _logging import unittest as _unittest import numpy as _numpy +try: + import matplotlib.pyplot as _pyplot +except ImportError as e: + _pyplot = None + _pyplot_import_error = e __version__ = '0.4' @@ -475,6 +480,8 @@ class TestUnitaryRFFT (_unittest.TestCase): expected[i] = 1.0 / _numpy.abs(a) * _numpy.sinc(f / a) if TEST_PLOTS: + if _pyplot is None: + raise _pyplot_import_error figure = _pyplot.figure() time_axes = figure.add_subplot(2, 1, 1) time_axes.plot(_numpy.arange(0, dt * samples, dt), x) @@ -539,6 +546,8 @@ class TestUnitaryRFFT (_unittest.TestCase): 1.0 / a, _numpy.pi * f) if TEST_PLOTS: + if _pyplot is None: + raise _pyplot_import_error figure = _pyplot.figure() time_axes = figure.add_subplot(2, 1, 1) time_axes.plot(_numpy.arange(0, dt * samples, dt), x) @@ -604,6 +613,8 @@ class TestUnitaryPowerSpectrum (_unittest.TestCase): 'The total power should be {} ({})'.format(Pexp, P)) if TEST_PLOTS: + if _pyplot is None: + raise _pyplot_import_error figure = _pyplot.figure() time_axes = figure.add_subplot(2, 1, 1) time_axes.plot( @@ -657,6 +668,8 @@ class TestUnitaryPowerSpectrum (_unittest.TestCase): expected_amp, power[0])) if TEST_PLOTS: + if _pyplot is None: + raise _pyplot_import_error figure = _pyplot.figure() time_axes = figure.add_subplot(2, 1, 1) time_axes.plot( @@ -726,6 +739,8 @@ class TestUnitaryPowerSpectrum (_unittest.TestCase): expected[0], power[0])) if TEST_PLOTS: + if _pyplot is None: + raise _pyplot_import_error figure = _pyplot.figure() time_axes = figure.add_subplot(2, 1, 1) time_axes.plot( @@ -779,6 +794,8 @@ class TestUnitaryAvgPowerSpectrum (_unittest.TestCase): 'The total power should be {} ({})'.format(Pexp, P)) if TEST_PLOTS: + if _pyplot is None: + raise _pyplot_import_error figure = _pyplot.figure() time_axes = figure.add_subplot(2, 1, 1) time_axes.plot( -- 2.26.2