Skip to content

Commit

Permalink
Cleanup in waterfall.py, test_waterfall.py, and plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
texadactyl committed May 17, 2022
1 parent 911b8bc commit 3b6f68f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 85 deletions.
6 changes: 0 additions & 6 deletions blimpy/plotting/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
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.
"""
Expand All @@ -25,16 +24,13 @@ 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":
Expand All @@ -57,5 +53,3 @@ def get_mpl_backend():
def set_mpl_backend(backend):
matplotlib.use(backend)


print_plotting_backend("import config.py definitions")
131 changes: 65 additions & 66 deletions blimpy/waterfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
SHOWING_BACKEND = False

from blimpy.io import file_wrapper as fw
from .plotting.config import *
from .plotting.config import plt, get_mpl_backend, set_mpl_backend, print_plotting_backend, ok_to_show
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:
Expand Down Expand Up @@ -74,49 +74,44 @@ def __init__(self, filename=None, f_start=None, f_stop=None, t_start=None, t_sto
fb.data # blimpy data, as a numpy array
Args:
filename (str): filename of blimpy file.
filename (str): filename of blimpy file. REQUIRED.
f_start (float): start frequency in MHz
f_stop (float): stop frequency in MHz
t_start (int): start integration ID
t_stop (int): stop integration ID
load_data (bool): load data. If set to False, only header will be read.
max_load (float): maximum data to load in GB.
header_dict (dict): Create blimpy from header dictionary + data array
data_array (np.array): Create blimpy from header dict + data array
header_dict (dict): *NOT CURRENTLY SUPPORTED*
data_array (np.array): *NOT CURRENTLY SUPPORTED*
"""

if filename:
self.filename = filename
self.ext = os.path.splitext(filename)[-1].lower()
self.container = fw.open_file(filename, f_start=f_start, f_stop=f_stop, t_start=t_start, t_stop=t_stop,
load_data=load_data, max_load=max_load)
self.file_header = self.container.header
self.header = self.file_header
self.n_ints_in_file = self.container.n_ints_in_file
self.file_shape = self.container.file_shape
self.file_size_bytes = self.container.file_size_bytes
self.selection_shape = self.container.selection_shape
self.n_channels_in_file = self.container.n_channels_in_file

# These values will be modified once code for multi_beam and multi_stokes observations are possible.
self.freq_axis = 2
self.time_axis = 0
self.beam_axis = 1 # Place holder
self.stokes_axis = 4 # Place holder

self.logger = logger

self.__load_data()

elif header_dict is not None and data_array is not None:
self.filename = ''
self.header = header_dict
self.data = data_array
self.n_ints_in_file = 0
self._setup_freqs()
if (header_dict is not None) or (data_array is not None):
raise ValueError("Neither header_dict nor data_array is currently supported.")

else:
self.filename = ''
if filename is None:
raise ValueError("Currently, a value for filename must be supplied.")

self.filename = filename
self.ext = os.path.splitext(filename)[-1].lower()
self.container = fw.open_file(filename, f_start=f_start, f_stop=f_stop, t_start=t_start, t_stop=t_stop,
load_data=load_data, max_load=max_load)
self.file_header = self.container.header
self.header = self.file_header
self.n_ints_in_file = self.container.n_ints_in_file
self.file_shape = self.container.file_shape
self.file_size_bytes = self.container.file_size_bytes
self.selection_shape = self.container.selection_shape
self.n_channels_in_file = self.container.n_channels_in_file

# These values will be modified once code for multi_beam and multi_stokes observations are possible.
self.freq_axis = 2
self.time_axis = 0
self.beam_axis = 1 # Place holder # Polarisation?
self.stokes_axis = 4 # Place holder

self.logger = logger

self.__load_data()

# Attach methods
self.plot_spectrum = six.create_bound_method(plot_spectrum, self)
Expand All @@ -125,7 +120,8 @@ def __init__(self, filename=None, f_start=None, f_stop=None, t_start=None, t_sto
self.plot_time_series = six.create_bound_method(plot_time_series, self)
self.plot_all = six.create_bound_method(plot_all, self)
self.plot_spectrum_min_max = six.create_bound_method(plot_spectrum_min_max, self)



def __load_data(self):
""" Helper for loading data from a container. Should not be called manually. """

Expand Down Expand Up @@ -431,23 +427,29 @@ def cmd_tool(args=None):
# Open blimpy data
filename = parse_args.filename
load_data = not parse_args.info_only
info_only = parse_args.info_only
filename_out = parse_args.filename_out

fil = Waterfall(filename, f_start=parse_args.f_start, f_stop=parse_args.f_stop, t_start=parse_args.t_start, t_stop=parse_args.t_stop, load_data=load_data, max_load=parse_args.max_load)
fil = Waterfall(filename,
f_start=parse_args.f_start,
f_stop=parse_args.f_stop,
t_start=parse_args.t_start,
t_stop=parse_args.t_stop,
load_data=load_data,
max_load=parse_args.max_load)
fil.info()
if parse_args.info_only:
return

# Blank DC.

#Check the size of selection.
if fil.container.isheavy() or parse_args.to_hdf5 or parse_args.to_fil:
info_only = True
if parse_args.blank_dc:
logger.info("Blanking DC bin")
n_coarse_chan = fil.calc_n_coarse_chan()
fil.blank_dc(n_coarse_chan)

# And if we want to plot data, then plot data.
if not info_only:
# Plotting

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 parse_args.what_to_plot is not None:

if SHOWING_BACKEND:
print_plotting_backend("before plotting")
Expand Down Expand Up @@ -485,31 +487,28 @@ def cmd_tool(args=None):
else:
logger.warning("No $DISPLAY available.")

else:
# Save in specified Filterbank format

fileroot = os.path.splitext(filename)[0]
fileroot = os.path.splitext(filename)[0]

if parse_args.to_hdf5 and parse_args.to_fil:
# If this is the last statement anyway,
# and consequently, an output file is not generated,
# why do we not raise an error?
raise ValueError('Either provide to_hdf5 or to_fil, but not both.')
if parse_args.to_hdf5 and parse_args.to_fil:
raise ValueError('Either provide to_hdf5 or to_fil, but not both.')

if parse_args.to_hdf5:
if not filename_out:
filename_out = fileroot + '.h5'
if parse_args.to_hdf5:
if not filename_out:
filename_out = fileroot + '.h5'

logger.info('Writing file : %s'% filename_out)
fil.write_to_hdf5(filename_out)
logger.info('File written.')
logger.info(f"Writing FBH5 file : {filename_out}")
fil.write_to_hdf5(filename_out)
logger.info('File written.')

elif parse_args.to_fil:
if not filename_out:
filename_out = fileroot + '.fil'
if parse_args.to_fil:
if not filename_out:
filename_out = fileroot + '.fil'

logger.info('Writing file : %s'% filename_out)
fil.write_to_fil(filename_out)
logger.info('File written.')
logger.info(f"Writing SIGPROC Filterbank file : {filename_out}")
fil.write_to_fil(filename_out)
logger.info('File written.')


if __name__ == "__main__":
Expand Down
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.40'
__version__ = '2.0.41'

with open("README.md", "r") as fh:
long_description = fh.read()
Expand Down
1 change: 1 addition & 0 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
export MPLBACKEND=agg
coverage run --source=blimpy -m pytest
EXITCODE=$?
if [ $EXITCODE -ne 0 ]; then
Expand Down
27 changes: 15 additions & 12 deletions tests/test_waterfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,20 @@ def test_neg_info_foff():
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):
def _setup_freqs(self):
print("Hello from function _setup_freqs")
puddle = Puddle(header_dict=header, data_array=data)
puddle = Puddle(header_dict=header)
del puddle

test_cmdline()
def test_bug_no_filename():
print("\n===== test_bug_no_filename")
with pytest.raises(ValueError):
bl.Waterfall()

def test_bug_header_dict():
print("\n===== test_bug_header")
header_dict = { "banana": "is a fruit" }
with pytest.raises(ValueError):
bl.Waterfall(voyager_h5, header_dict=header_dict)

def test_bug_data_array():
print("\n===== test_bug_data_array")
data_array = np.arange(1, 11)
with pytest.raises(ValueError):
bl.Waterfall(voyager_h5, data_array=data_array)

0 comments on commit 3b6f68f

Please sign in to comment.