Skip to content

Commit

Permalink
Ready for 0.5.0 (#79)
Browse files Browse the repository at this point in the history
* Fix Correlation selection.. again

* Enforce int on numcores

* Make default n_cores int

* Change temp dir location for html plots
An attempt at #78

* Delete the --gaintype arguement
Change -kx, --k-xaxis to -x, --xaxis for xaxis selection for any table
Add -y, --yaxis as alternative options to -d, --doplot
Remove choice restrictions on doplot and xaxis

* Update docs and changelog

* Allow Kcross, Df, Xf tables
Allow any combination of ys

* Change to minor release version 0.5.0
  • Loading branch information
Mulan-94 committed Oct 2, 2020
1 parent 2db0a42 commit 5c42882
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 93 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
0.5.0
-----
**ragavi-vis**

- Fix corr selector again

**ragavi-gains**

- Remove the `--gaintype` argument
- Adds polcal tables Kcross, Xf and Df to allowed plots
- Changes `-kx/--k-xaxis` to `-x\--xaxis` to allow for arbitrary specification of desired x-axis
- Adds `-y/ --yaxis` as an alternative to `-d/--doplot`
- Forces the temp directory ragavi uses to be the same as where ragavi is being ran. Attempt to fix "Out of memory error"


0.4.3
-----
**ragavi-gains**
Expand Down
10 changes: 8 additions & 2 deletions docs/source/gains.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Currently, gain tables supported for visualisation by ``ragavi-gains`` are :
* Delay calibration (K tables)
* Gain calibration (G tables)
* D-Jones Leakage tables (D tables)
* Pol-cal tables (Kcross, Xf, Df)

Mandatory argument is :code:`--table`.

Expand Down Expand Up @@ -41,6 +42,11 @@ This will yield an output HTML file, with plots in the order
* Unless **all** the available fields, SPWs and correlations have not been selected, the antenna legends will appear greyed-out. This is because a single legend is attached to multiple data for each of the available categories. Therefore, clicking on legends without meeting the preceeding condition may lead to some awkward results (a toggling effect).


By default, `ragavi-gains` automatically determines an appropriate x-axis for
a given input table. This behaviour can be changed by specifying the
`-x/xaxis` argument.


Use in Jupyter Notebooks
========================
To use ``ragavi-gains`` in a notebook environment, run in a notebook cell
Expand Down Expand Up @@ -181,8 +187,8 @@ Command:
:width: 800
:alt: Static plot with ``--doplot all`` and ``--plotname test.png``
Useful function
===============
Jupyter Notebook function
=========================
.. autofunction:: ragavi.ragavi.plot_table
Expand Down
2 changes: 1 addition & 1 deletion ragavi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ragavi.raglog

# set ragavi version number (single source)
__version__ = "0.4.3"
__version__ = "0.5.0"
34 changes: 14 additions & 20 deletions ragavi/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,30 +284,24 @@ def gains_argparser():
Defaults to coolwarm""",
default="coolwarm")

pconfig.add_argument("-d", "--doplot", dest="doplot", type=str,
metavar='',
choices=["ap", "ri", "all"],
help="""Plot complex values as amplitude & phase
(ap) or real and imaginary (ri) or both (all).
Defaults to ap.""",
pconfig.add_argument("-y", "--yaxis", "-d", "--doplot", dest="doplot",
type=str, metavar='',
help="""Plot complex values as any amplitude (a),
phase (p), real (r), imaginary (i). For a combination
of multiple plots, specify as a single string. e.g.
To plot both amplitude and phase, set this to 'ap'.
To plot all at once, set this to 'all'.
Defaults to ap.""",
default="ap")
pconfig.add_argument("--debug", dest="debug",
action="store_true",
help="""Enable debug messages""")
pconfig.add_argument("-g", "--gaintype", nargs='*', type=str,
metavar=' ', dest="gain_types",
choices=['B', 'D', 'G', 'K', 'F'],
help="""Type of table(s) to be plotted. Can be
specified as a single character e.g. "B" if a
single table has been provided or space
separated list e.g B D G if multiple tables have
been specified. Valid choices are B D G K & F""",
default=[])
pconfig.add_argument("-kx", "--k-xaxis", dest="kx", type=str, metavar='',
choices=["time", "antenna"],
help="""Choose the x-xaxis for the K table. Valid
choices are: time or antenna. Defaults to time.""",
default="time")
pconfig.add_argument("-x", "--xaxis", dest="kx", type=str, metavar='',
help="""Choose an x-xaxis Valid choices are:
time, antenna, channel. If this is not supplied, an
appropriate one will be selected automatically
depending on the type of gains being plotted.""",
default=None)
pconfig.add_argument("-lf", "--logfile", dest="logfile", type=str,
metavar="",
help="""The name of resulting log file (with
Expand Down
112 changes: 65 additions & 47 deletions ragavi/ragavi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from pyrap.tables import table

import ragavi.utils as vu

from ragavi import __version__
from ragavi.plotting import create_bk_fig, add_axis

Expand Down Expand Up @@ -96,15 +95,18 @@ def __repr__(self):
self.xds_table_obj, self.ms_name, self.gtype, self.yaxis)

def set_xaxis(self):
if self.gtype in ['B', 'D']:
xaxis = "channel"
elif self.gtype in ['G', 'F']:
xaxis = "time"
elif self.gtype == "K":
if self.kx == "time":
xaxis = "time"
else:
xaxis = "antenna"
gains = {
"B": "channel",
"D": "channel",
"Xf": "channel",
"Df": "channel",
"G": "time",
"F": "time",
"K": "time",
"Kcross": "time",
}
# kx should take precedence over the other
xaxis = self.kx or gains[self.gtype]
return xaxis

def process_data(self, ydata, yaxis=None):
Expand All @@ -131,7 +133,7 @@ def process_data(self, ydata, yaxis=None):
y = vu.calc_phase(ydata, unwrap=False)
elif yaxis == "real":
y = vu.calc_real(ydata)
elif yaxis == "delay" or yaxis == "error":
elif yaxis in ["delay", "error"]:
y = ydata

return y
Expand Down Expand Up @@ -207,7 +209,6 @@ def get_xaxis_data(self):
else:
logger.error("Invalid x-axis name")
return

return xdata, x_label

def prep_xaxis_data(self, xdata, freq=None):
Expand Down Expand Up @@ -398,16 +399,19 @@ def y_only(self, add_error=True):

Data = namedtuple("Data", "y y_label y_err")
ydata, y_label = self.get_yaxis_data()
p_ydata = self.prep_yaxis_data(ydata)

# prepare the y-data
p_ydata = self.prep_yaxis_data(ydata).data
if add_error:
err_data = self.get_errors()
hi = self.prep_yaxis_data(ydata + err_data)
lo = self.prep_yaxis_data(ydata - err_data)
# add error to the raw complex value beforehand
hi = self.prep_yaxis_data(ydata + err_data).data
lo = self.prep_yaxis_data(ydata - err_data).data
else:
hi = None
lo = None

d = Data(y=p_ydata.data, y_label=y_label, y_err=(hi.data, lo.data))
d = Data(y=p_ydata, y_label=y_label, y_err=(hi, lo))
return d


Expand Down Expand Up @@ -517,13 +521,13 @@ def get_time_range(tab_name, unix_time=True):
return i_time, f_time


def get_tooltip_data(xds_table_obj, gtype, freqs):
def get_tooltip_data(xds_table_obj, xaxis, freqs):
"""Get the data to be displayed on the tool-tip of the plots
Parameters
----------
gtype: :obj:`str`
Type of gain table being plotted
xaxis: :obj:`str`
Current xaxis
xds_table_obj : `xarray.Dataset`
xarray-ms table object
freqs : :obj:`np.array`
Expand All @@ -538,7 +542,6 @@ def get_tooltip_data(xds_table_obj, gtype, freqs):
"""
logger.debug("Getting tooltip data")

scan_no = xds_table_obj.SCAN_NUMBER.values
scan_no = scan_no.astype(np.uint16)
spw_id = np.full(scan_no.shape, xds_table_obj.SPECTRAL_WINDOW_ID,
Expand All @@ -547,12 +550,11 @@ def get_tooltip_data(xds_table_obj, gtype, freqs):
# get the number of channels
nchan = freqs.size

if gtype in ['B', 'D']:
if xaxis == "channel":
# extend array to accommodate for multiple time slots
spw_id = np.array(spw_id).repeat(nchan, axis=0)
# tile because of different scan numbers available
scan_no = np.tile(scan_no, nchan)

logging.debug("Tooltip data ready")
return spw_id, scan_no

Expand Down Expand Up @@ -1522,6 +1524,16 @@ def gen_checkbox_labels(batch_size, num_leg_objs, antnames):

################## Saving Plot funcs ###################################

def set_tempdir(name):
"""Set the current dir as the temp dir also"""
import tempfile
if os.path.isfile(name):
t_dir = os.path.dirname(os.path.abspath(name))
else:
t_dir = name
tempfile.tempdir = t_dir


def save_html(name, plot_layout):
"""Save plots in HTML format
Expand Down Expand Up @@ -1583,6 +1595,10 @@ def save_static_image(fname, figs=None, batch_size=16, cmap="viridis",
ax = fi.subplots(nrows=1, ncols=ncols, sharex="row",
squeeze=True,
gridspec_kw=dict(wspace=0.2, hspace=0.3))

# Ensure ax is iterable because of plotting
if not isinstance(ax, (np.ndarray, list)):
ax = np.array([ax])
for y, cds in enumerate(row):

ants = np.unique([x.data_source.data["antname"][0]
Expand Down Expand Up @@ -1769,20 +1785,20 @@ def main(**kwargs):
subs, gain = get_table(tab, spwid=ddid, where=where, fid=fields,
antenna=plotants)

if doplot == "ap":
y_axes = ["amplitude", "phase"]
elif doplot == "ri":
y_axes = ["real", "imaginary"]
elif doplot == "all":
y_axes = ["amplitude", "phase", "real", "imaginary"]

if gain == "K":
y_axes = ["delay"]
doplots = {
"a": "amplitude",
"p": "phase",
"r": "real",
"i": "imaginary",
}

if gain in ["G", "F"] or (options.kx == "time" and gain == "K"):
x_axis_type = "datetime"
if doplot == "all":
y_axes = [doplots[_] for _ in "apri"]
else:
x_axis_type = "linear"
y_axes = [doplots[_] for _ in doplot]

if gain in ["K", "Kcross"]:
y_axes = ["delay"]

# confirm a populous table is selected
try:
Expand Down Expand Up @@ -1835,18 +1851,6 @@ def main(**kwargs):
if (sub.SPECTRAL_WINDOW_ID == spw and
sub.FIELD_ID == fid and
sub.ANTENNA1 == ant):
# for tooltips
spw_id, scan = get_tooltip_data(sub, gain, freqs)
source = ColumnDataSource(
data={"scanid": scan,
"corr": np.full(scan.shape, corr,
dtype=np.uint8),
"field": np.full(scan.shape, fname),
"spw": spw_id,
"antname": np.full(scan.shape, legend)
})
inv_source = ColumnDataSource(data={})

logger.debug(vu.ctext(f"Processing un-flagged data for {legend}"))

data_obj = DataCoreProcessor(
Expand All @@ -1873,7 +1877,17 @@ def main(**kwargs):
y_label = data.y_label

iy = infl_data.y

# for tooltips
spw_id, scan = get_tooltip_data(sub, xaxis, freqs)
source = ColumnDataSource(
data={"scanid": scan,
"corr": np.full(scan.shape, corr,
dtype=np.uint8),
"field": np.full(scan.shape, fname),
"spw": spw_id,
"antname": np.full(scan.shape, legend)
})
inv_source = ColumnDataSource(data={})
source.add(x, name='x')
source.add(y, name=f"y{_y}")

Expand All @@ -1896,6 +1910,7 @@ def main(**kwargs):
sub.close()

title = f"{yaxis.capitalize()} vs {xaxis.capitalize()}"
x_axis_type = "datetime" if xaxis == "time" else "linear"
fig = create_bk_fig(xlab=x_label, ylab=y_label, title=title,
x_min=x.min(), x_max=x.max(),
x_axis_type=x_axis_type, x_name=xaxis,
Expand Down Expand Up @@ -2191,6 +2206,9 @@ def main(**kwargs):
if _NB_RENDER_:
return final_layout
else:
# set output dir to temp dir also
set_tempdir((options.html_name or os.getcwd()))

if options.image_name and options.html_name:
save_html(options.html_name, final_layout)
save_static_image(fname=options.image_name, figs=final_plots,
Expand Down

0 comments on commit 5c42882

Please sign in to comment.