Commit 26c53b33 authored by Nick Sauerwein's avatar Nick Sauerwein
Browse files

bokeh live plot mit popup

parent a78073f1
{
"cells": [],
"metadata": {},
"cells": [
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
"\n",
"from PlottingServers import Plot1DServer, Plot2DServer"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"http://localhost:5006/?bokeh-session-id=aVLQCbgQkJCVnxR54qR4jkhW4Qzc4QGQHXcLhkMKlo2e\n"
]
},
{
"data": {
"text/html": [
"<script type='text/javascript'>window.open('http://localhost:5006/?bokeh-session-id=aVLQCbgQkJCVnxR54qR4jkhW4Qzc4QGQHXcLhkMKlo2e', '_blank');</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ps1 = Plot1DServer({'title':'test', 'xlabel':'numbers', 'ylabel':'ja'})"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"ps1.update(np.linspace(0,1,100), np.random.rand(100))"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"http://localhost:5006/?bokeh-session-id=hVPTw4AmHaVbC7yvfyHlW48GSCsqAG7v4dHmVTFwOYYy\n"
]
},
{
"data": {
"text/html": [
"<script type='text/javascript'>window.open('http://localhost:5006/?bokeh-session-id=hVPTw4AmHaVbC7yvfyHlW48GSCsqAG7v4dHmVTFwOYYy', '_blank');</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ps2d = Plot2DServer({'title':'test', 'xlabel':'numbers', 'ylabel':'ja'})"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
"ps2d.update(np.random.random((50,100))*100,[0,2,0,1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
......@@ -51,6 +51,11 @@ def plot_measure(data, devices, plot_config = {}, sleep = 0):
g.plot_hist
except:
g.plot_hist = {}
try:
g.plot_servers
except:
g.plot_servers = {}
measure, config = (data['measure'], data['config'])
......@@ -72,14 +77,18 @@ def plot_measure(data, devices, plot_config = {}, sleep = 0):
g.plot_hist[device_name][item[0]] =g.plot_hist[device_name][item[0]] + [measure[device_name][item[0]]]
except:
g.plot_hist[device_name][item[0]] = [measure[device_name][item[0]]]
plt.figure('history: '+device_name+' '+item[0])
plt.clf()
try:
ps1d = g.plot_servers['history: '+device_name+' '+item[0]]
except:
from PlottingServers import Plot1DServer
ps1d = Plot1DServer({'xlabel' : 'last measurements','ylabel' : 'value', 'title': 'history: '+device_name+' '+item[0]})
g.plot_servers['history: '+device_name+' '+item[0]] = ps1d
xx = range(-len(g.plot_hist[device_name][item[0]])+1,1)
plt.plot(xx,g.plot_hist[device_name][item[0]])
plt.xlabel('last measurements')
plt.ylabel('value')
plt.hlines([min(g.plot_hist[device_name][item[0]]), max(g.plot_hist[device_name][item[0]])],min(xx), max(xx))
plt.pause(sleep)
ps1d.update(xx,g.plot_hist[device_name][item[0]])
#important: sort by time and plot sepecific elements
......
......@@ -22,7 +22,7 @@ class FaradayCup:
self.drs = DRS4
self.set_config(config)
self.plot_server = None
def set_config(self,config):
self.channel = config['channel']
self.R = config['R']
......@@ -42,11 +42,10 @@ class FaradayCup:
return measurement
def plot_measure(self, measurement, config = None):
plt.figure('measurement: FaradayCup')
plt.clf()
plt.plot(measurement['sig_time'], measurement['sig_uvolt'])
plt.xlabel('Time [ns]')
plt.ylabel('Signal [uV]')
plt.title('Measured Charge: '+str(measurement['charge'])+' fC')
plt.tight_layout()
\ No newline at end of file
from PlottingServers import Plot1DServer
if self.plot_server == None:
self.plot_server = Plot1DServer({'title':'FaradayCup', 'xlabel':'Time [ns]', 'ylabel': 'Signal [uV]'})
self.plot_server.update(measurement['sig_time'], measurement['sig_uvolt'])
\ No newline at end of file
No preview for this file type
No preview for this file type
......@@ -6,6 +6,7 @@ class test_device1:
self.id = id
self.ok = 1.
self.list = {1:1, 2:2, 3:3, 4:4}
self.plot_server = None
def get_config(self):
config = {}
......@@ -25,15 +26,19 @@ class test_device1:
measurement['random'] = np.random.rand()
return measurement
def plot_measure(self, measurement, config = None):
plt.figure('measurement: testdevice1')
plt.clf()
plt.plot(measurement['value'])
plt.title('very random')
from PlottingServers import Plot1DServer
if self.plot_server == None:
self.plot_server = Plot1DServer({'title':'test_device1', 'xlabel':'num', 'ylabel': 'rand'})
self.plot_server.update(range(len(measurement['value'])),measurement['value'])
class test_device2:
def __init__(self, id):
self.id = id
self.cool = True
self.plot_server = None
def get_config(self):
config = {}
config['id'] = self.id
......@@ -48,10 +53,13 @@ class test_device2:
measurement = {}
from skimage import data
measurement['value'] = data.coins()
measurement['value'] = np.random.random((100,100))
return measurement
def plot_measure(self, measurement, config = None):
plt.figure('measurement: testdevice2')
plt.clf()
plt.imshow(measurement['value'])
plt.title('great coins, so great')
\ No newline at end of file
from PlottingServers import Plot2DServer
if self.plot_server == None:
self.plot_server = Plot2DServer({'title':'test_device2', 'xlabel':'x', 'ylabel': 'y'})
self.plot_server.update(measurement['value'], [0,1,0,1])
\ No newline at end of file
global plot_hist
\ No newline at end of file
global plot_hist
global plot_servers
\ No newline at end of file
......@@ -2,21 +2,12 @@
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
......@@ -28,7 +19,7 @@
"import ipywidgets as widgets\n",
"\n",
"%autoreload 2\n",
"%matplotlib qt5"
"%matplotlib inline"
]
},
{
......@@ -345,6 +336,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"hideCode": false,
"hidePrompt": false
},
......@@ -365,7 +357,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {
"collapsed": true,
"hideCode": false,
......@@ -396,7 +388,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {
"hideCode": false,
"hidePrompt": false
......@@ -436,7 +428,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 4,
"metadata": {
"collapsed": true,
"hideCode": false,
......@@ -469,9 +461,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {
"collapsed": true,
"hideCode": false,
"hidePrompt": false
},
......@@ -495,8 +486,9 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 6,
"metadata": {
"collapsed": true,
"hideCode": false,
"hidePrompt": false
},
......@@ -508,14 +500,13 @@
"\n",
"io = dm.IO('Data/')\n",
"\n",
"%matplotlib qt5\n",
"\n",
"import GUI"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 7,
"metadata": {
"hideCode": false,
"hidePrompt": false,
......@@ -526,14 +517,14 @@
"outputs": [],
"source": [
"#devices = [delay,inter,laser, drs, fara, blade, focus]#, per]#, blade] #use this to run the LWFA\n",
"devices = [fara]\n",
"devices = [td1, td2,fara]\n",
"#devices = [per, para, laser, focus] #use this to do focus optimization\n",
"#devices = [td1, td2, delay]#, inter] #Use this do debug the gui and data menager"
"#devices = [td1, td2]#, delay]#, inter] #Use this do debug the gui and data menager"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 8,
"metadata": {
"hideCode": false,
"hidePrompt": false,
......@@ -544,9 +535,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
"FaradayCup config time: 2.6226043701171875e-06\n",
"test_device1 config time: 2.6226043701171875e-06\n",
"test_device2 config time: 4.0531158447265625e-06\n",
"FaradayCup config time: 3.0994415283203125e-06\n",
"test_device1 measured\n",
"test_device1 measure time: 0.00029778480529785156\n",
"test_device2 measured\n",
"test_device2 measure time: 0.00030231475830078125\n",
"FaradayCup measured\n",
"FaradayCup measure time: 0.001667022705078125\n"
"FaradayCup measure time: 0.001695394515991211\n",
"maximal possible rate: 155.8\n"
]
}
],
......
from IPython.display import HTML
from IPython.display import display_html
def hide():
print ('hu')
HTML('''<script>
display_html('''<script>
code_show=false;
function code_toggle() {
if (code_show){
......@@ -14,4 +14,13 @@ def hide():
}
$( document ).ready(code_toggle);
</script>
<h1><a href="javascript:code_toggle()">Hide Code</a></h1>''')
\ No newline at end of file
<h1><a href="javascript:code_toggle()">Hide Code</a></h1>''')
def open_tab(url):
print (url)
display_html("<script type='text/javascript'>window.open('"+url+"', '_blank');</script>", raw = True)
def open_window(url, width = 800, height = 500):
print (url)
display_html("<script type='text/javascript'>window.open('"+url+"', '_blank','width="+str(width)+", height="+str(height)+"');</script>", raw = True)
\ No newline at end of file
import numpy as np
from bokeh.client import push_session
from bokeh.plotting import figure
from bokeh.document import Document
from multiprocessing import Process, Pipe
from bokeh.driving import count
from NotebookTools import open_window
class DataStreamProcess_1D(Process):
def __init__(self,server, connecx, connecy, config, *args, **kwargs):
self.connecx = connecx
self.connecy = connecy
self.config = config
Process.__init__(self, *args, **kwargs)
def run(self):
plot_server_1d(connecx = self.connecx, connecy = self.connecy, config = self.config)
def plot_server_1d( connecx = None, connecy = None, config = {'xlabel' : '','ylabel' : '', 'title': ''}):
doc = Document()
doc.title = config['title']
x = np.linspace(0,1,2)
y = np.linspace(0,1,2)
p = figure(title = config['title'],tools =
'box_zoom,,wheel_zoom,save,crosshair,resize,reset,hover')
p.plot_height = 250
p.plot_width = 500
r1 = p.square(x, y, fill_color=None, line_color="green")
r2 = p.line(x, y, line_color="green")
p.xaxis.axis_label = config['xlabel']
p.yaxis.axis_label = config['ylabel']
# open a session to keep our local document in sync with server
session = push_session(doc)
def update():
# updating a single column of the the *same length* is OK
if connecx.poll(0.1):
r1.data_source.data["x"] = connecx.recv()
r2.data_source.data["x"] = r1.data_source.data["x"]
r1.data_source.data["y"] = connecy.recv()
r2.data_source.data["y"] = r1.data_source.data["y"]
doc.add_periodic_callback(update, 50)
session.show(p)# open the document in a browser
open_window('http://localhost:5006/?bokeh-session-id='+str(session.id))
session.loop_until_closed() # run forever
class Plot1DServer:
def __init__(self, config = {'xlabel' : '','ylabel' : '', 'title': ''}):
self.connecx_ch, self.connecx_par = Pipe()
self.connecy_ch, self.connecy_par = Pipe()
self.data_stream = DataStreamProcess_1D(self, self.connecx_par, self.connecy_par, config)
self.data_stream.start()
def update(self, x,y):
self.connecx_ch.send(x)
self.connecy_ch.send(y)
def close():
self.data_stream.close()
class DataStreamProcess_2D(Process):
def __init__(self,connecim,connecext, config, *args, **kwargs):
self.connecim = connecim
self.connecext = connecext
self.config = config
Process.__init__(self, *args, **kwargs)
def run(self):
plot_server_2d( connecim = self.connecim,connecext = self.connecext, config = self.config)
def plot_server_2d(connecim = None,connecext = None, config = {'xlabel' : '','ylabel' : '', 'title': ''}):
doc = Document()
doc.title = config['title']
img = np.array([[0,0],[0,0]])
p = figure(title = config['title'],
x_range=[0, img.shape[0]], y_range=[0, img.shape[1]],
tools = 'box_zoom,wheel_zoom,save,crosshair,resize,reset')
p.plot_height = 270
p.plot_width = 270
from bokeh.palettes import Viridis256
r1 = p.image(image=[np.flipud(img)], x=[0], y=[0],
dw=[img.shape[0]], dh=[img.shape[1]],palette = Viridis256)
p.xaxis.axis_label = config['xlabel']
p.yaxis.axis_label = config['ylabel']
# open a session to keep our local document in sync with server
session = push_session(doc)
def update():
# updating a single column of the the *same length* is OK
if connecim.poll(0.1):
r1.data_source.data["image"] = [connecim.recv()]
extent = connecext.recv()
p.x_range.start = extent[0]
p.x_range.end = extent[1]
p.y_range.start = extent[2]
p.y_range.end = extent[3]
r1.data_source.data["x"] = [extent[0]]
r1.data_source.data["dw"] = [extent[1] - extent[0]]
r1.data_source.data["y"] = [extent[2]]
r1.data_source.data["dh"] = [extent[3] - extent[2]]
doc.add_periodic_callback(update, 50)
session.show(p)# open the document in a browser
open_window('http://localhost:5006/?bokeh-session-id='+str(session.id), width = 500)
session.loop_until_closed() # run forever
class Plot2DServer:
def __init__(self, config = {'xlabel' : '','ylabel' : '', 'title': ''}):
self.connecext_ch, self.connecext_par = Pipe()
self.connecim_ch, self.connecim_par = Pipe()
self.data_stream = DataStreamProcess_2D(self.connecim_par,self.connecext_par, config)
self.data_stream.start()
def update(self,image ,extent):
self.connecext_ch.send(extent)
self.connecim_ch.send(image)
def close(self):
self.data_stream.join()
\ No newline at end of file
No preview for this file type
No preview for this file type
No preview for this file type
This diff is collapsed.
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {
"collapsed": true
},
......@@ -11,6 +11,15 @@
"import pypylon"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pypylon.factory.find_devices()"
]
},
{
"cell_type": "code",
"execution_count": null,
......
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