Skip to content

Commit

Permalink
Merge 1fae90c into b845a11
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Aug 25, 2020
2 parents b845a11 + 1fae90c commit e55d522
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions apstools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
~replay
~run_in_thread
~safe_ophyd_name
~select_live_plot
~select_mpl_figure
~show_ophyd_symbols
~split_quoted_line
~summarize_runs
~text_encode
~to_unicode_or_bust
~trim_plot
~trim_string_for_EPICS
~unix
Expand Down Expand Up @@ -1276,7 +1279,16 @@ def plot_prune_fifo(bec, n, y, x):
*object* :
instance of ophyd.Signal (or subclass),
independent (x) axis
DEPRECATED: Will be removed by end of 2020-12.
Use :meth:`trim_plot` instead.
Note the order of parameters is different in :meth:`trim_plot`.
"""
warnings.warn(
"DEPRECATED: plot_prune_fifo() will be removed"
" in a future release. Use trim_plot() instead."
" Note the order of parameters is different in trim_plot()."
)

if n < 0:
raise ValueError(
Expand Down Expand Up @@ -1310,6 +1322,119 @@ def plot_prune_fifo(bec, n, y, x):
return lp


def select_mpl_figure(x, y):
"""
get the MatPlotLib Figure window for y vs x
PARAMETERS
x
*object*:
X axis object (an ``ophyd.Signal``)
y
ophyd object:
X axis object (an ``ophyd.Signal``)
RETURNS
object or ``None``:
Instance of ``matplotlib.pyplot.Figure()``
"""
import matplotlib.pyplot as plt
figure_name = f"{y.name} vs {x.name}"
if figure_name in plt.get_figlabels():
return plt.figure(figure_name)


def select_live_plot(bec, signal):
"""
get the *first* live plot that matches ``signal``
PARAMETERS
bec
*object*:
instance of ``bluesky.callbacks.best_effort.BestEffortCallback``
signal
*object*:
The Y axis object (an ``ophyd.Signal``)
RETURNS
*object*:
Instance of ``bluesky.callbacks.best_effort.LivePlotPlusPeaks()``
or ``None``
"""
for live_plot_dict in bec._live_plots.values():
live_plot = live_plot_dict.get(signal.name)
if live_plot is not None:
return live_plot


def trim_plot(bec, n, x, y):
"""
find the plot with axes x and y and replot with at most the last *n* lines
Note: this is not a bluesky plan. Call it as normal Python function.
EXAMPLE::
trim_plot(bec, 1, m1, noisy)
PARAMETERS
bec
*object* :
instance of BestEffortCallback
n
*int* :
number of plots to keep
x
*object* :
instance of ophyd.Signal (or subclass),
independent (x) axis
y
*object* :
instance of ophyd.Signal (or subclass),
dependent (y) axis
(new in release 1.3.5, replaces :meth:`plot_prune_fifo`)
"""
# liveplot = select_live_plot(bec, y)
# if liveplot is None:
# logger.debug("no live plot found with signal '%s'", y.name)
# return

fig = select_mpl_figure(x, y)
if fig is None:
logger.debug("no figure found with '%s vs %s'", y.name, x.name)
return
if len(fig.axes) == 0:
logger.debug("no plots on figure: '%s vs %s'", y.name, x.name)
return

ax = fig.axes[0]
while len(ax.lines) > n:
try:
ax.lines[0].remove()
except ValueError as exc:
if not str(exc).endswith("x not in list"):
logger.warning(
"%s vs %s: mpl remove() error: %s",
y.name, x.name, str(exc))
ax.legend()
# liveplot.update_plot()
# Rescale and redraw.
# (from bluesky.callbacks.mpl_plotting.update_plot)
ax.relim(visible_only=True)
ax.autoscale_view(tight=True)
ax.figure.canvas.draw_idle()
logger.debug("trim complete")


def print_snapshot_list(db, printing=True, **search_criteria):
"""
print (stdout) a list of all snapshots in the databroker
Expand Down

0 comments on commit e55d522

Please sign in to comment.