From 62e8cea4d617fc36c4551d0174f417a60d9b7f1c Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Wed, 19 May 2021 14:31:09 +0200 Subject: [PATCH] some prototypes --- prototypes/c2cache.py | 20 ++++++++ prototypes/cabokeh1.py | 57 +++++++++++++++++++++ prototypes/cabokeh2.py | 93 ++++++++++++++++++++++++++++++++++ prototypes/stream-bokeh.py | 39 ++++++++++++++ prototypes/stream-streamlit.py | 73 ++++++++++++++++++++++++++ 5 files changed, 282 insertions(+) create mode 100644 prototypes/c2cache.py create mode 100644 prototypes/cabokeh1.py create mode 100644 prototypes/cabokeh2.py create mode 100644 prototypes/stream-bokeh.py create mode 100755 prototypes/stream-streamlit.py diff --git a/prototypes/c2cache.py b/prototypes/c2cache.py new file mode 100644 index 0000000..9cea36c --- /dev/null +++ b/prototypes/c2cache.py @@ -0,0 +1,20 @@ +from collections import deque +from time import time + + +class Cache: + + def __init__(self, size=100): + self.times = deque(maxlen=size) + self.values = deque(maxlen=size) + + def append(self, value): + self.times.append(time()) + self.values.append(value) + + + +cache = Cache() + + + diff --git a/prototypes/cabokeh1.py b/prototypes/cabokeh1.py new file mode 100644 index 0000000..ad08f0a --- /dev/null +++ b/prototypes/cabokeh1.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python + +from time import sleep, time +from random import random +import numpy as np +from epics import PV +from bokeh.plotting import curdoc, figure +from bokeh.models import ColumnDataSource +from bokeh.layouts import row + + +data = { + "x": [], + "y": [] +} + +source = ColumnDataSource(data=data) +histo = ColumnDataSource(dict(left=[], right=[], top=[])) + +fig1 = figure() +fig1.step(x="x", y="y", source=source, mode="after") +fig1.circle(x="x", y="y", source=source) + +fig2 = figure() +fig2.quad(left="left", right="right", top="top", bottom=0, source=histo) + +pv = PV("MTEST:RAND0") + +cache = { + "x": [], + "y": [] +} + +def cb(value=None, **kwargs): + x = time() + y = value + cache["x"].append(x) + cache["y"].append(y) + +def update(): + source.stream(cache, rollover=50) + cache["x"] = [] + cache["y"] = [] + data = source.data["y"] + counts, edges = np.histogram(data, bins="auto") + histo.data.update(left=edges[:-1], right=edges[1:], top=counts) + + + +pv.add_callback(cb) +doc = curdoc() +doc.add_periodic_callback(update, 1000) +plt = row(fig1, fig2) +doc.add_root(plt) + + + diff --git a/prototypes/cabokeh2.py b/prototypes/cabokeh2.py new file mode 100644 index 0000000..8ca7fd1 --- /dev/null +++ b/prototypes/cabokeh2.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python + +from time import sleep, time +from random import random +import numpy as np +from epics import PV +from bokeh.plotting import curdoc, figure +from bokeh.models import ColumnDataSource +from bokeh.layouts import row + +from c2cache import cache + + +data = { + "x": [], + "y": [] +} + +source = ColumnDataSource(data=data) +histo = ColumnDataSource(dict(left=[], right=[], top=[])) +image = ColumnDataSource(dict(image=[np.random.random((1000, 1000))])) + +fig1 = figure() +fig1.step(x="x", y="y", source=source, mode="after") +fig1.circle(x="x", y="y", source=source) + +fig2 = figure() +fig2.quad(left="left", right="right", top="top", bottom=0, source=histo) + +fig3 = figure() +fig3.image(source=image, x=0, y=0, dw=1, dh=1, palette="Spectral11") + +pv = PV("MTEST:RAND0") + +pv_img = PV("MTEST:CHAN-IMAGE:FPICTURE", auto_monitor=True) +pv_height = PV("MTEST:CHAN-IMAGE:HEIGHT") +pv_width = PV("MTEST:CHAN-IMAGE:WIDTH") + + +def cb(value=None, **kwargs): + cache.append(value) + + +image_data = None + +def cb_img(value=None, **kwargs): + print(len(value)) + global image_data + if value is None: + return + height = pv_height.get() + width = pv_width.get() + if height is None or value is None: + return + image_data = value + image_data.shape = (height, width) + +pv_img.add_callback(cb_img) + + +def update(): +# print(cache.values) + times = cache.times + values = cache.values + data = { + "x": times, + "y": values + } + source.data.update(data) + + counts, edges = np.histogram(values, bins="auto") + histo.data.update(left=edges[:-1], right=edges[1:], top=counts) + + if image_data is not None: +# print(image_data.shape) + image.data.update(image=[image_data]) + + + +pv.add_callback(cb) +doc = curdoc() +doc.add_periodic_callback(update, 1000) + +plt = row( + fig1, + fig2, + fig3 +) + +doc.add_root(plt) + + + diff --git a/prototypes/stream-bokeh.py b/prototypes/stream-bokeh.py new file mode 100644 index 0000000..4786ba5 --- /dev/null +++ b/prototypes/stream-bokeh.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +from time import sleep +from random import random +from bokeh.plotting import curdoc, figure +from bokeh.models import ColumnDataSource + +data = { + "x": [0, 1, 2, 3, 4], + "y": [6, 7, 2, 3, 5] +} + +i = len(data["x"]) + +source = ColumnDataSource(data=data) + +p = figure() +p.line(x="x", y="y", source=source) + + +def update(): + global i + i += 1 + + new = { + "x": [i], + "y": [random()*10] + } + + source.stream(new, rollover=50) + + + +doc = curdoc() +doc.add_periodic_callback(update, 500) +doc.add_root(p) + + + diff --git a/prototypes/stream-streamlit.py b/prototypes/stream-streamlit.py new file mode 100755 index 0000000..3358512 --- /dev/null +++ b/prototypes/stream-streamlit.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python + +from time import sleep, time +import streamlit as st +import altair as alt +import numpy as np +import pandas as pd + + +st.title(__file__.rsplit("/")[-1]) + + +chart_data = pd.DataFrame( + np.random.randn(20, 3), + columns=['a', 'b', 'c'], + index=np.arange(10, 30) +) + +ch1 = st.line_chart(chart_data) + +c = alt.Chart(chart_data.reset_index()).mark_line().encode( + x='index', + y='a', + tooltip="a" +) + +ch2 = st.altair_chart(c) + + +img_data = np.random.randn(2500, 2500) +img_data -= img_data.min() +img_data /= img_data.max() + +img = st.image(img_data) + + +times = [] +last_time = time() + +for i in range(1000): + cd = pd.DataFrame( + np.random.randn(1, 3), + columns=['a', 'b', 'c'], + index=[chart_data.index.max()+1] + ) + + ch1.add_rows(cd) + + chart_data = chart_data.append(cd) + chart_data = chart_data.iloc[1:] + + c = alt.Chart(chart_data.reset_index()).mark_line().encode( + x='index', + y='a', + tooltip="a" + ) + + ch2.altair_chart(c) + + img_data = np.random.randn(250, 250) + img_data -= img_data.min() + img_data /= img_data.max() + img.image(img_data) + + current_time = time() + delta_t = current_time - last_time + times.append(delta_t) + last_time = current_time + print(delta_t, np.mean(times)) + + sleep(1) + +