animation - 如何防止“动画”散景图卡顿
问题描述
我正在尝试创建一种用于监控心电图数据和相关功能的仪表板。当通过不断更新两个 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)
我没有发现散景的用户指南对更新绘图很有帮助。某处可能有最佳实践的集合吗?
解决方案
在评论中结束对话:每次将一个点添加到绘图中时,都会重新绘制浏览器中的整个画布区域。这就是浏览器的工作方式。具有sampling_rate
250 个结果会导致每秒 250 次绘图更新,即每 4 毫秒更新一次。这将使浏览器运行速度越来越慢,因为每 4 毫秒要渲染(重新绘制)的点数会增加。
我建议将更新周期从 4 毫秒增加到大约 100 毫秒(可能有更大的数据包)
推荐阅读
- python - 线性模型中的对偶值太少
- google-apps-script - 使用谷歌工作表脚本时如何将电子表格的工作表复制到另一个电子表格而不创建新的工作表副本?
- django - Django 使用上下文管理器禁用低级缓存
- mysql - Homebrew - brew mysql 8 - 错误:无法通过套接字“/tmp/mysql.sock”连接到本地 MySQL 服务器 (2)
- c# - 本地安装 dotnet 工具使用旧命令名
- python - 如何从具有空值的数据框列中提取两个子字符串之间的所有字符串?
- php - Symfony 4 中的功能测试事件和订阅者
- batch-file - 如何以文件名作为存档名称存档文件?
- wordpress - 在 Wordpress 编辑器中显示特色图片
- angular - 什么时候应该使用 unsubscribe()?