Skip to content

Commit

Permalink
Fix issue #263 (plotting displays)
Browse files Browse the repository at this point in the history
  • Loading branch information
texadactyl committed Apr 19, 2022
1 parent cce9d0a commit 18a8131
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 22 deletions.
1 change: 1 addition & 0 deletions VERSION-HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ This file is a version history of blimpy amendments, beginning with version 2.0.
<br>
| 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. |
Expand Down
62 changes: 52 additions & 10 deletions blimpy/plotting/config.py
Original file line number Diff line number Diff line change
@@ -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
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")
2 changes: 0 additions & 2 deletions blimpy/plotting/plot_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from .config import *

def calc_extent(self, plot_f=None, plot_t=None, MJD_time=False):
""" Setup plotting edges.
"""
Expand Down
1 change: 0 additions & 1 deletion blimpy/plotting/plot_waterfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions blimpy/waterfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand 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.")
Expand Down Expand Up @@ -501,4 +513,4 @@ def cmd_tool(args=None):


if __name__ == "__main__":
cmd_tool()
cmd_tool()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 9 additions & 2 deletions tests/test_waterfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 18a8131

Please sign in to comment.