diff --git a/VERSION-HISTORY.md b/VERSION-HISTORY.md index a515706c..a2b5f03d 100644 --- a/VERSION-HISTORY.md +++ b/VERSION-HISTORY.md @@ -3,6 +3,7 @@ This file is a version history of blimpy amendments, beginning with version 2.0.
| Date | Version | Contents | | :--: | :--: | :-- | +| 2022-04-19 | 2.0.40 | Fixed blimpy to show plots when the display supports it (issue #263). | | 2022-03-30 | 2.0.39 | examine_h5 in hdf_reader.py is loading too much data (issue #261). | | | | Version/Release 2.0.38 cannot be used. | | 2022-02-03 | 2.0.37 | Having trouble with publishing on pypi. Seems to be stuck on blimpy v2.0.35. | diff --git a/blimpy/plotting/config.py b/blimpy/plotting/config.py index 2e8df479..8bc50fb1 100644 --- a/blimpy/plotting/config.py +++ b/blimpy/plotting/config.py @@ -1,19 +1,61 @@ +""" +Blimpy Plotting Configuration +This file is imported by the other plotting source files and blimpy/waterfall.py. + +matplotlib backends info: +https://matplotlib.org/3.5.0/users/explain/backends.html#:~:text=By%20default%2C%20Matplotlib%20should%20automatically,to%20worry%20about%20the%20backend. +""" import os import numpy as np - -# Check if $DISPLAY is set (for handling plotting on remote machines with no X-forwarding) import matplotlib -if 'DISPLAY' in os.environ.keys(): - import pylab as plt -else: - matplotlib.use('Agg') - import pylab as plt +# Define plt for caller. +import matplotlib.pyplot as plt +plt.rcParams['axes.formatter.useoffset'] = False +# Define NullFormatter for caller. from matplotlib.ticker import NullFormatter -plt.rcParams['axes.formatter.useoffset'] = False +#Define some constants for caller. +MAX_PLT_POINTS = 65536 # Max number of points in matplotlib plot +MAX_IMSHOW_POINTS = (8192, 4096) # Max number of points in imshow plot -MAX_PLT_POINTS = 65536 # Max number of points in matplotlib plot -MAX_IMSHOW_POINTS = (8192, 4096) # Max number of points in imshow plot \ No newline at end of file +def ok_to_show(): + """ + Tell caller if the DISPLAY environment variable is set + and therefore if plt.show() can be executed. + + Parameters + ---------- + None. + + Returns + ------- + bool + Can plt.show() be executed (True/False)? + + """ + display = os.environ.get("DISPLAY", "empty") + if display == "empty": + print("blimpy plotting config.py setup_plotting_backend: DISPLAY is *empty*") + return False + print(f"blimpy plotting config.py setup_plotting_backend: DISPLAY is {display}") + return True + + +def print_plotting_backend(arg_context): + """ Show which matplotlib backend is in use.""" + ok_to_show() + print(f"blimpy plotting config.py ({arg_context}): matplotlib backend is {matplotlib.get_backend()}") + + +def get_mpl_backend(): + return matplotlib.get_backend() + + +def set_mpl_backend(backend): + matplotlib.use(backend) + + +print_plotting_backend("import config.py definitions") diff --git a/blimpy/plotting/plot_utils.py b/blimpy/plotting/plot_utils.py index 7f0a5db6..9d3a3b02 100644 --- a/blimpy/plotting/plot_utils.py +++ b/blimpy/plotting/plot_utils.py @@ -1,5 +1,3 @@ -from .config import * - def calc_extent(self, plot_f=None, plot_t=None, MJD_time=False): """ Setup plotting edges. """ diff --git a/blimpy/plotting/plot_waterfall.py b/blimpy/plotting/plot_waterfall.py index 8631e566..56bf845a 100644 --- a/blimpy/plotting/plot_waterfall.py +++ b/blimpy/plotting/plot_waterfall.py @@ -13,7 +13,6 @@ def plot_waterfall(wf, f_start=None, f_stop=None, if_id=0, logged=True, cb=True, cb (bool): for plotting the colorbar kwargs: keyword args to be passed to matplotlib imshow() """ - plot_f, plot_data = wf.grab_data(f_start, f_stop, if_id) # imshow does not support int8, so convert to floating point diff --git a/blimpy/waterfall.py b/blimpy/waterfall.py index d2cb9cac..526db5d9 100755 --- a/blimpy/waterfall.py +++ b/blimpy/waterfall.py @@ -23,8 +23,14 @@ import numpy as np import six +SHOWING_BACKEND = False + from blimpy.io import file_wrapper as fw -from .plotting import * +from .plotting.config import * +from .plotting import plot_all, plot_kurtosis, plot_spectrum_min_max, plot_spectrum, plot_time_series, plot_waterfall +MPL_BACKEND = get_mpl_backend() +if SHOWING_BACKEND: + print(f"after importing plot_all etc: {MPL_BACKEND}") from astropy.time import Time from astropy import units as u @@ -377,6 +383,10 @@ def cmd_tool(args=None): from argparse import ArgumentParser + set_mpl_backend(MPL_BACKEND) + if SHOWING_BACKEND: + print_plotting_backend("entered cmd_tool") + parser = ArgumentParser(description="Command line utility for reading and plotting blimpy files.") parser.add_argument('filename', type=str, @@ -433,15 +443,15 @@ def cmd_tool(args=None): # And if we want to plot data, then plot data. if not info_only: - from .plotting.config import plt - - print('') if parse_args.blank_dc: logger.info("Blanking DC bin") n_coarse_chan = fil.calc_n_coarse_chan() fil.blank_dc(n_coarse_chan) + if SHOWING_BACKEND: + print_plotting_backend("before plotting") + if parse_args.what_to_plot == "s": plt.figure("Spectrum", figsize=(8, 6)) fil.plot_spectrum(logged=True, f_start=parse_args.f_start, f_stop=parse_args.f_stop, t='all') @@ -467,8 +477,10 @@ def cmd_tool(args=None): if parse_args.plt_filename != '': plt.savefig(parse_args.plt_filename) + if SHOWING_BACKEND: + print_plotting_backend("before plt.show") if not parse_args.save_only: - if 'DISPLAY' in os.environ.keys(): + if ok_to_show(): plt.show() else: logger.warning("No $DISPLAY available.") @@ -501,4 +513,4 @@ def cmd_tool(args=None): if __name__ == "__main__": - cmd_tool() \ No newline at end of file + cmd_tool() diff --git a/setup.py b/setup.py index 76db2bba..330cb6e1 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ """ from setuptools import setup, find_packages -__version__ = '2.0.39' +__version__ = '2.0.40' with open("README.md", "r") as fh: long_description = fh.read() diff --git a/tests/test_waterfall.py b/tests/test_waterfall.py index cec18f8e..ad615a07 100644 --- a/tests/test_waterfall.py +++ b/tests/test_waterfall.py @@ -9,6 +9,7 @@ OUTDIR = os.path.dirname(voyager_h5) + "/" def test_info(): + print("\n===== test_info") a = bl.Waterfall(voyager_h5) print(a) a.info() @@ -26,6 +27,7 @@ def test_info(): del a def test_get_freqs(): + print("\n===== test_get_freqs") wf = bl.Waterfall(voyager_h5) freqs = wf.container.populate_freqs() sum1 = np.sum(freqs) @@ -45,8 +47,8 @@ def test_get_freqs(): assert np.isclose(last_1, last_2, rtol=0.0001) def test_cmdline(): - # Plots - + print("\n===== test_cmdline") + args = [voyager_h5, '-S', '-p', 'w', '-s', OUTDIR + 'test.png'] cmd_tool(args) @@ -101,17 +103,20 @@ def test_cmdline(): os.remove(OUTDIR + 'test.fil') def test_cmd_arguments(): + print("\n===== test_cmd_arguments") args = [voyager_h5, '-H', '-F', '-o', OUTDIR + 'test.fil'] with pytest.raises(ValueError): cmd_tool(args) def test_neg_blank_dc(): + print("\n===== test_neg_blank_dc") wf = bl.Waterfall(voyager_h5) wf.blank_dc(0) wf.blank_dc(1.1) del wf def test_get_chunk_dimensions(): + print("\n===== test_get_chunk_dimensions") wf = bl.Waterfall(voyager_h5) wf.header['foff'] = 0.99e-5 @@ -131,12 +136,14 @@ def test_get_chunk_dimensions(): del wf def test_neg_info_foff(): + print("\n===== test_neg_info_foff") wf = bl.Waterfall(voyager_h5) wf.header['foff'] = -1 wf.info() del wf def test_no_filename(): + print("\n===== test_no_filename") header = { "banana": "is a fruit" } data = np.arange(1, 11) class Puddle(bl.Waterfall):