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

bokeh live plot mit popup

parent a78073f1
{ {
"cells": [], "cells": [
"metadata": {}, {
"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": 4,
"nbformat_minor": 2 "nbformat_minor": 2
} }
...@@ -51,6 +51,11 @@ def plot_measure(data, devices, plot_config = {}, sleep = 0): ...@@ -51,6 +51,11 @@ def plot_measure(data, devices, plot_config = {}, sleep = 0):
g.plot_hist g.plot_hist
except: except:
g.plot_hist = {} g.plot_hist = {}
try:
g.plot_servers
except:
g.plot_servers = {}
measure, config = (data['measure'], data['config']) measure, config = (data['measure'], data['config'])
...@@ -72,14 +77,18 @@ def plot_measure(data, devices, plot_config = {}, sleep = 0): ...@@ -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]]] g.plot_hist[device_name][item[0]] =g.plot_hist[device_name][item[0]] + [measure[device_name][item[0]]]
except: except:
g.plot_hist[device_name][item[0]] = [measure[device_name][item[0]]] 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) xx = range(-len(g.plot_hist[device_name][item[0]])+1,1)
plt.plot(xx,g.plot_hist[device_name][item[0]]) ps1d.update(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)
#important: sort by time and plot sepecific elements #important: sort by time and plot sepecific elements
......
...@@ -22,7 +22,7 @@ class FaradayCup: ...@@ -22,7 +22,7 @@ class FaradayCup:
self.drs = DRS4 self.drs = DRS4
self.set_config(config) self.set_config(config)
self.plot_server = None
def set_config(self,config): def set_config(self,config):
self.channel = config['channel'] self.channel = config['channel']
self.R = config['R'] self.R = config['R']
...@@ -42,11 +42,10 @@ class FaradayCup: ...@@ -42,11 +42,10 @@ class FaradayCup:
return measurement return measurement
def plot_measure(self, measurement, config = None): def plot_measure(self, measurement, config = None):
plt.figure('measurement: FaradayCup') from PlottingServers import Plot1DServer
plt.clf()
plt.plot(measurement['sig_time'], measurement['sig_uvolt']) if self.plot_server == None:
plt.xlabel('Time [ns]') self.plot_server = Plot1DServer({'title':'FaradayCup', 'xlabel':'Time [ns]', 'ylabel': 'Signal [uV]'})
plt.ylabel('Signal [uV]')
plt.title('Measured Charge: '+str(measurement['charge'])+' fC') self.plot_server.update(measurement['sig_time'], measurement['sig_uvolt'])
plt.tight_layout() \ No newline at end of file
\ 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: ...@@ -6,6 +6,7 @@ class test_device1:
self.id = id self.id = id
self.ok = 1. self.ok = 1.
self.list = {1:1, 2:2, 3:3, 4:4} self.list = {1:1, 2:2, 3:3, 4:4}
self.plot_server = None
def get_config(self): def get_config(self):
config = {} config = {}
...@@ -25,15 +26,19 @@ class test_device1: ...@@ -25,15 +26,19 @@ class test_device1:
measurement['random'] = np.random.rand() measurement['random'] = np.random.rand()
return measurement return measurement
def plot_measure(self, measurement, config = None): def plot_measure(self, measurement, config = None):
plt.figure('measurement: testdevice1') from PlottingServers import Plot1DServer
plt.clf()
plt.plot(measurement['value']) if self.plot_server == None:
plt.title('very random') self.plot_server = Plot1DServer({'title':'test_device1', 'xlabel':'num', 'ylabel': 'rand'})
self.plot_server.update(range(len(measurement['value'])),measurement['value'])
class test_device2: class test_device2:
def __init__(self, id): def __init__(self, id):
self.id = id self.id = id
self.cool = True self.cool = True
self.plot_server = None
def get_config(self): def get_config(self):
config = {} config = {}
config['id'] = self.id config['id'] = self.id
...@@ -48,10 +53,13 @@ class test_device2: ...@@ -48,10 +53,13 @@ class test_device2:
measurement = {} measurement = {}
from skimage import data from skimage import data
measurement['value'] = data.coins() measurement['value'] = np.random.random((100,100))
return measurement return measurement
def plot_measure(self, measurement, config = None): def plot_measure(self, measurement, config = None):
plt.figure('measurement: testdevice2') from PlottingServers import Plot2DServer
plt.clf()
plt.imshow(measurement['value']) if self.plot_server == None:
plt.title('great coins, so great') self.plot_server = Plot2DServer({'title':'test_device2', 'xlabel':'x', 'ylabel': 'y'})
\ No newline at end of file
self.plot_server.update(measurement['value'], [0,1,0,1])
\ No newline at end of file
global plot_hist global plot_hist
\ No newline at end of file global plot_servers
\ No newline at end of file
...@@ -2,21 +2,12 @@ ...@@ -2,21 +2,12 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 1,
"metadata": { "metadata": {
"hideCode": false, "hideCode": false,
"hidePrompt": false "hidePrompt": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
"source": [ "source": [
"import numpy as np\n", "import numpy as np\n",
"import matplotlib.pyplot as plt\n", "import matplotlib.pyplot as plt\n",
...@@ -28,7 +19,7 @@ ...@@ -28,7 +19,7 @@
"import ipywidgets as widgets\n", "import ipywidgets as widgets\n",
"\n", "\n",
"%autoreload 2\n", "%autoreload 2\n",
"%matplotlib qt5" "%matplotlib inline"
] ]
}, },
{ {
...@@ -345,6 +336,7 @@ ...@@ -345,6 +336,7 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"collapsed": true,
"hideCode": false, "hideCode": false,
"hidePrompt": false "hidePrompt": false
}, },
...@@ -365,7 +357,7 @@ ...@@ -365,7 +357,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": true, "collapsed": true,
"hideCode": false, "hideCode": false,
...@@ -396,7 +388,7 @@ ...@@ -396,7 +388,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 3,
"metadata": { "metadata": {
"hideCode": false, "hideCode": false,
"hidePrompt": false "hidePrompt": false
...@@ -436,7 +428,7 @@ ...@@ -436,7 +428,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 4,
"metadata": { "metadata": {
"collapsed": true, "collapsed": true,
"hideCode": false, "hideCode": false,
...@@ -469,9 +461,8 @@ ...@@ -469,9 +461,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"metadata": { "metadata": {
"collapsed": true,
"hideCode": false, "hideCode": false,
"hidePrompt": false "hidePrompt": false
}, },
...@@ -495,8 +486,9 @@ ...@@ -495,8 +486,9 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 6,
"metadata": { "metadata": {
"collapsed": true,
"hideCode": false, "hideCode": false,
"hidePrompt": false "hidePrompt": false
}, },
...@@ -508,14 +500,13 @@ ...@@ -508,14 +500,13 @@
"\n", "\n",
"io = dm.IO('Data/')\n", "io = dm.IO('Data/')\n",
"\n", "\n",
"%matplotlib qt5\n",
"\n", "\n",
"import GUI" "import GUI"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 7,
"metadata": { "metadata": {
"hideCode": false, "hideCode": false,
"hidePrompt": false, "hidePrompt": false,
...@@ -526,14 +517,14 @@ ...@@ -526,14 +517,14 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"#devices = [delay,inter,laser, drs, fara, blade, focus]#, per]#, blade] #use this to run the LWFA\n", "#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 = [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", "cell_type": "code",
"execution_count": 13, "execution_count": 8,
"metadata": { "metadata": {
"hideCode": false, "hideCode": false,
"hidePrompt": false, "hidePrompt": false,
...@@ -544,9 +535,16 @@ ...@@ -544,9 +535,16 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "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 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(): def hide():
print ('hu')
HTML('''<script> display_html('''<script>
code_show=false; code_show=false;
function code_toggle() { function code_toggle() {
if (code_show){ if (code_show){
...@@ -14,4 +14,13 @@ def hide(): ...@@ -14,4 +14,13 @@ def hide():
} }
$( document ).ready(code_toggle); $( document ).ready(code_toggle);
</script> </script>
<h1><a href="javascript:code_toggle()">Hide Code</a></h1>''') <h1><a href="javascript:code_toggle()">Hide Code</a></h1>''')
\ No newline at end of file
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