首页 > 解决方案 > 如何防止“动画”散景图卡顿

问题描述

我正在尝试创建一种用于监控心电图数据和相关功能的仪表板。当通过不断更新两个 s 来“动画化”心电图时ColumnDataSource,情节很快就会开始卡顿并变慢。目前,我在笔记本上运行它,但在散景服务器上也是如此。

心电图本身显示最近的一秒,一行中大约有 400 个数据点。就其本身而言,这几乎可以顺利运行,但这在仪表板方面并没有真正的帮助。另一个图显示了心率变异性,可能有十几个圆圈,但来自最后一分钟的数据,因此有两个来源。

似乎数据源中收集的越多,更新发生的越快,绘图就越慢。rollover参数削减了所收集内容的ColumnDataSource.stream()总长度,但最终并不能防止口吃。

我的代码中可能存在一些新手错误,因为我觉得散景应该具备很好的能力来可视化如此大量的数据。所以,这是我为绘图所做的:

dashboard_source = ColumnDataSource(record[:1]) # initialize with first row
ecg_source = ColumnDataSource(record[:1]) # initialize with first row

# some options
time_window = 12 # seconds to keep in view
ecg_length = 1 # seconds to keep in view in ECG plot
update_rate = 1000 / sampling_rate # number of milliseconds between each plot update

# update function in which source data is fed from record dataframe
current_record_pos = 1
def update_dashboard_source():
    global current_record_pos
    new_row = record.iloc[current_record_pos]
    dashboard_source.stream(new_row, rollover = sampling_rate * time_window)
    ecg_source.stream(new_row, rollover = sampling_rate * ecg_length)
    current_record_pos += 1

def ecg_dashboard(doc):

    # dashboard element: ECG plot/s ---- ------- ---- ------- ---- ------- ---- -------

    ecg_plot = figure(width=800, height=400, title='ECG', x_axis_label='time in ms', y_range=(-1, 1.5))

    # plot ECG channels
    for record_channel, color in zip(record_channels, ['green', 'blue']):
        ecg_plot.line(source=ecg_source, x='time_ms', y=record_channel, alpha=.3, legend=record_channel+' ', color=color)

    # dashboard element: heart rate variability ---- ------- ---- ------- ---- ------- ---- -------

    hrv_plot = figure(width=400, height=400, title='heart rate variability', x_axis_label="r'r''", y_axis_label="r''r'''")
    hrv_plot.circle(source=dashboard_source, x='r_diff_1', y='r_diff_2', size=10, alpha=.23)

    # gather everything in a dashboard element and add it to the document
    ecg_row = row(ecg_plot)
    feature_row = row(hrv_plot)
    dashboard = column(ecg_row, feature_row)
    doc.add_root(dashboard)
    doc.add_periodic_callback(update_dashboard_source, update_rate)

show(ecg_dashboard)

我没有发现散景的用户指南对更新绘图很有帮助。某处可能有最佳实践的集合吗?

标签: animationplotbokeh

解决方案


在评论中结束对话:每次将一个点添加到绘图中时,都会重新绘制浏览器中的整个画布区域。这就是浏览器的工作方式。具有sampling_rate250 个结果会导致每秒 250 次绘图更新,即每 4 毫秒更新一次。这将使浏览器运行速度越来越慢,因为每 4 毫秒要渲染(重新绘制)的点数会增加。

我建议将更新周期从 4 毫秒增加到大约 100 毫秒(可能有更大的数据包)


推荐阅读