some prototypes

This commit is contained in:
2021-05-19 14:31:09 +02:00
parent 00ff746c74
commit 62e8cea4d6
5 changed files with 282 additions and 0 deletions

20
prototypes/c2cache.py Normal file
View File

@ -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()

57
prototypes/cabokeh1.py Normal file
View File

@ -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)

93
prototypes/cabokeh2.py Normal file
View File

@ -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)

View File

@ -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)

73
prototypes/stream-streamlit.py Executable file
View File

@ -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)