Commit af22bd3f authored by Yan's avatar Yan

Speedup of the GUI

* annotation routine optimized
* reduced constrained_layout calls
parent 25860ed2
......@@ -377,6 +377,8 @@ def main():
main_window.setAcceptDrops(True)
main_window.keyPressEvent = lambda event: key_pressed(
event, augCanvas, config)
main_window.resizeEvent = lambda event: augCanvas.constrained_draw()
update.signal.connect(lambda: augCanvas.constrained_draw())
main_window.addToolBar(QtCore.Qt.TopToolBarArea, toolBar)
main_window.addDockWidget(QtCore.Qt.LeftDockWidgetArea, treedock)
......
......@@ -54,6 +54,11 @@ class AugFigureCanvas(FigureCanvas):
self.chromplot, lambda x_min, x_max: pick_times(
x_min, x_max, self), 'horizontal', useblit=True,
rectprops=dict(alpha=0.15, facecolor='purple'), button=3)
self.figure.set_constrained_layout(False)
def constrained_draw(self):
self.figure.execute_constrained_layout()
self.draw()
class AugSpanSelector(SpanSelector):
......@@ -118,6 +123,16 @@ class AugSpanSelector(SpanSelector):
ann_spec(self.ax, self.data)
class FixedScalarFormatter(matplotlib.ticker.ScalarFormatter):
def __init__(self):
super().__init__()
self._powerlimits = (0,0)
def _set_format(self):
"""_set_format override"""
self.format = "%.2f"
def zoom_factory(axis, base_scale, plot_data=None):
"""returns zooming functionality to axis"""
def zoom_fun(event, pd, ax, scale):
......@@ -187,7 +202,6 @@ def pan_factory(axis, plot=None):
action, id_drag, id_release, pd, ax))
def drag_fun(event, ax):
ax.figure.set_constrained_layout(False)
ax.drag_pan(1, 'x', event.x, event.y)
ax.figure.canvas.draw()
......@@ -198,7 +212,6 @@ def pan_factory(axis, plot=None):
if type(pd) is dict and "annotation" in pd:
ann_spec(ax, pd)
ax.figure.canvas.draw()
ax.figure.set_constrained_layout(True)
fig = axis.get_figure()
fig.canvas.mpl_connect('button_press_event',
......@@ -341,20 +354,18 @@ def ann_spec(ms_spec, msdata, ann_limit=0.01):
"""annotate spectrum
First define the array, in which the annotation should occur.
Then remove values which are invalid as local maximas. Then select
local maximas from the array by forcycling. Local maximas are then
Then remove values which are invalid as local maximas. Local maximas are then
reduced to a representation of the important ones by the sub_peaks
function"""
def sub_peaks(peakz, hardpeaks, xrange, yrange, coef_x=10, coef_y=10):
"""Returns reasonable subselection of local maximas"""
hardxy = np.array(([i.xy for i in hardpeaks]), dtype=[
('x', float), ('y', float)])
sort_peaks = np.flipud(np.sort(np.array(peakz, dtype=[
('x', float), ('y', float)]), order='y')).copy()
hardxy = np.array([i.xy for i in hardpeaks], dtype=[
('x', np.float32), ('y', np.float32)])
sort_peaks = np.flipud(np.sort(np.array(peakz))).copy()
red_x = xrange / coef_x
red_y = yrange / coef_y
big_peaks = np.array([], dtype=[('x', float), ('y', float)])
big_peaks = np.array([], dtype=[('x', np.float32), ('y', np.float32)])
for peak in np.nditer(sort_peaks, flags=["zerosize_ok"]):
if not (np.any((abs(peak['y'] - big_peaks['y']) < red_y)
& (abs(peak['x'] - big_peaks['x']) < red_x)) or
......@@ -366,14 +377,19 @@ def ann_spec(ms_spec, msdata, ann_limit=0.01):
peaks = []
for line in ms_spec.lines:
xdata, ydata = line.get_data()
argvis = dt.argsubselect(xdata, *ms_spec.get_xlim())
# remove tails which cannot be evaluated as maximas
argvis = argvis[np.where((argvis != 0)
& (argvis != (len(xdata)-1)))]
lim = ms_spec.get_ylim()[1] * ann_limit
for i in argvis:
if ydata[i] > lim and ydata[i] > max(ydata[i-1], ydata[i+1]):
peaks.append((xdata[i], ydata[i]))
# Thanks to:
# https://gist.github.com/ben741/d8c70b608d96d9f7ed231086b237ba6b
minlim = ms_spec.get_ylim()[1] * ann_limit
lims = [*ms_spec.get_xlim(),*ms_spec.get_ylim()]
maxargs = np.where((xdata[1:-1] > lims[0]) & (xdata[1:-1] < lims[1]) &
(ydata[1:-1] > minlim) & (ydata[1:-1] < lims[3]) &
(ydata[1:-1] > ydata[0:-2]) &
(ydata[1:-1] > ydata[2:]))[0] + 1
peakline = np.empty([len(maxargs)], dtype=[('x', np.float32), ('y', np.float32)])
peakline['x'], peakline['y'] = xdata[maxargs], ydata[maxargs]
peaks.append(peakline)
peaks = np.concatenate(peaks)
s_peaks = sub_peaks(peaks, msdata['texts'],
np.diff(ms_spec.get_xlim()),
np.diff(ms_spec.get_ylim()))
......@@ -384,11 +400,10 @@ def ann_spec(ms_spec, msdata, ann_limit=0.01):
# remove them from tracking
msdata['annotation'].clear()
dispints = cf.settings().value("view/intensities", type=bool)
for peak in s_peaks:
if cf.settings().value("view/intensities", type=bool):
annotation = '{0:.2f}\n{1: .2e}'.format(peak['x'], peak['y'])
else:
annotation = '{0:.2f}'.format(peak['x'])
annotation = '{0:.2f}\n{1: .2e}'.format(peak[0], peak[1])\
if dispints else '{0:.2f}'.format(peak[0])
peaktext = ms_spec.annotate(
annotation, xy=(peak['x'], peak['y']), textcoords='data',
picker=True, in_layout=False)
......@@ -404,7 +419,7 @@ def pop_plot(xdata, ydata, plot, plot_data, colornum=0, legend=None):
plot.set_ylabel(plot_data['ylabel'])
plot.set_ylim(plot.get_ylim()[1] * -0.01,
plot.get_ylim()[1] * 1.1)
plot.ticklabel_format(scilimits=(0, 0), axis='y')
plot.yaxis.set_major_formatter(FixedScalarFormatter())
# put hardcoded annotation if there is some
if "texts" in plot_data and not any(
data in plot.get_children() for data in plot_data['texts']):
......@@ -506,7 +521,7 @@ def populate(augCanvas):
ax.get_legend().set_in_layout(False)
ax.autoscale(True)
ax.set_ylim(ax.get_ylim()[1]*-0.01, ax.get_ylim()[1]*1.1)
augCanvas.draw()
augCanvas.constrained_draw()
return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment