Commit df6ffa1a authored by Advanced Instrumentation's avatar Advanced Instrumentation
Browse files

plotting parallelized

parent 49268fa4
......@@ -36,7 +36,7 @@ def print_dict(dictionary, ident = '', braces=1, plot = True):
def print_data(data, plot = True):
print_dict(data, plot = plot)
def plot_measure(data, devices, plot_config = {}):
def plot_measure(data, devices, plot_config = {}, sleep = 0):
if plot_config == {}:
measure, config = (data['measure'], data['config'])
......@@ -75,7 +75,7 @@ def plot_measure(data, devices, plot_config = {}):
plt.figure('history: '+device_name+' '+item[0])
plt.clf()
plt.plot(g.plot_hist[device_name][item[0]])
plt.pause(sleep)
#important: sort by time and plot sepecific elements
......
......@@ -9,6 +9,7 @@ import numpy as np
config file:
ExposureTimeAbs: exposure time in us
min_intensity: minimal intensity over which contour of FWHM will be fitted
measure_full: boolean to indicate if image should be analysed (FWHMs, contour, ...)
measurement file:
......@@ -39,38 +40,43 @@ class FocusCamera:
self.cam.properties['ExposureTimeAbs']=config['ExposureTimeAbs']
self.measure_full = config['measure_full']
def get_config(self):
config = {}
config['ExposureTimeAbs'] = self.cam.properties['ExposureTimeAbs']
config['xmin'], config['xmax'],config['ymin'], config['ymax']= (self.xmin, self.xmax, self.ymin, self.ymax)
config['measure_full'] = self.measure_full
return config
def measure(self):
import datetime
time = datetime.datetime.now()
im = self.cam.grab_image():
if max_int > self.min_intensity:
maximum_pos, contour = LA.find_beam(im, plot = False)
ymin, ymax = ax_beam.get_ylim()
xmin, xmax = ax_beam.get_xlim()
cxmm, cymm = contour
FWHMx = np.max(cxmm) - np.min(cxmm)
FWHMy = np.max(cymm) - np.min(cymm)
else:
ax_beam.set_ylim([ymin, ymax])
ax_beam.set_xlim([xmin, xmax])
LA.showimage(im, axis = ax_beam)
comment = 'No beam detected'
print (comment)
FWHMx = None
FWHMy = None
cxmm = None, cymm = None
im = self.cam.grab_image()
measurement = {'time': time,'im': im}
if self.measure_full:
if max_int > self.min_intensity:
maximum_pos, contour = LA.find_beam(im, plot = False)
ymin, ymax = ax_beam.get_ylim()
xmin, xmax = ax_beam.get_xlim()
cxmm, cymm = contour
FWHMx = np.max(cxmm) - np.min(cxmm)
FWHMy = np.max(cymm) - np.min(cymm)
else:
ax_beam.set_ylim([ymin, ymax])
ax_beam.set_xlim([xmin, xmax])
LA.showimage(im, axis = ax_beam)
comment = 'No beam detected'
print (comment)
FWHMx = None
FWHMy = None
cxmm = None, cymm = None
measurement['comment'], measurement['FWHMx'], measurement['FWHMy'], measurement['cxmm'], measurement['cymm'] = (comment, FWHMx, FWHMy, cxmm, cymm)
measurement = {'time': time,'im': im, 'comment' = comment, 'FWHMx': FWHMx, 'FWHMy' = FWHMy, 'cxmm': cxmm, 'cymm': cymm}
return measurement
def plot_measure(self, measurement, config = None):
......@@ -80,7 +86,7 @@ class FocusCamera:
LA.find_beam(im,axis = ax)
def connect_camera(self):
def connect_camera(self):
available_cameras = pypylon.factory.find_devices()
if len(available_cameras) == 0:
......
......@@ -14,7 +14,7 @@ measurement file (n.a)
class ParabolicMirror:
def __init__(self, config, home = False):
import time
time.sleep(2)
time.sleep(1)
try:
self.connect_motors()
print ('Connnection with motors sucessful!')
......
......@@ -27,7 +27,7 @@ measurement file (n.a)
class Periscope:
def __init__(self, config, home = False):
import time
time.sleep(2)
time.sleep(1)
try:
self.connect_motors()
print ('Connnection with motors sucessful!')
......
......@@ -187,7 +187,7 @@ def main(devices, io):
bt_browse = widgets.Button(description='Browse configs')
bt_browse.on_click(find_config)
display(bt_browse)
#display(bt_browse)
text_comment = widgets.Text(description= 'Comment:')
check_elog = widgets.Checkbox(value=False,
......@@ -325,16 +325,34 @@ def main(devices, io):
kind = 'measure', comment = comment, name = name+'_'+str(i))
if output_check.value:
global plot_config
dm.plot_measure(measurement, devices2measure,
plot_config = plot_config)
plt.draw_all(force = True)
print ('maximal possible rate: ',np.round(1/(time.time() -t),1))
if (1/rate_text.value - (time.time() -t) < 0):
print ('set rate cannot be reached.')
import threading
still_plotting = True
while still_plotting:
still_plotting = False
for thread in threading.enumerate():
if thread.name == 'plotting':
print (thread.name)
still_plotting = True
continue
th = threading.Thread(target = dm.plot_measure, args = (measurement, devices2measure,
plot_config, 0.01), name = 'plotting')
th.start()
#dm.plot_measure(measurement, devices2measure,
# plot_config = plot_config)
plt.draw_all(force = True)
print ('maximal possible rate: ',np.round(1/(time.time() -t),1))
if (1/rate_text.value - (time.time() -t) < 0):
print ('set rate cannot be reached.')
else:
plt.pause(1/rate_text.value - (time.time() -t))
else:
plt.pause(1/rate_text.value - (time.time() -t))
print ('maximal possible rate: ',np.round(1/(time.time() -t),1))
if (1/rate_text.value - (time.time() -t) < 0):
print ('set rate cannot be reached.')
else:
time.sleep(1/rate_text.value - (time.time() -t))
plt.pause(0.0001)
import IPython
IPython.display.clear_output(wait = True)
......
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -47,7 +47,9 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
......@@ -105,7 +107,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"## ParabolicMirror"
]
......@@ -114,7 +119,9 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
......@@ -461,7 +468,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -487,18 +494,26 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 2,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:root:Line magic function `%autoreload` not found.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using matplotlib backend: Qt5Agg\n"
"This computer does not support elog\n",
"Using matplotlib backend: MacOSX\n"
]
}
],
......@@ -507,7 +522,7 @@
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"io = dm.IO('Data/jet_stability_test/')\n",
"io = dm.IO('Data/')\n",
"\n",
"%matplotlib\n",
"\n",
......@@ -517,7 +532,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 3,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -526,12 +541,12 @@
"outputs": [],
"source": [
"#devices = [delay, inter, drs, fara, per, blade] #use this to run the LWFA\n",
"devices = [delay, inter, blade] #Use this do debug the gui and data menager"
"devices = [td1, td2]#[delay, inter, blade] #Use this do debug the gui and data menager"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 4,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -543,10 +558,54 @@
"name": "stdout",
"output_type": "stream",
"text": [
"DelayGenerator measured\n",
"Interferometer measured\n",
"measure saved as scan_rate_5.025125628140703-5.025125628140703_1_2017-03-21_12-49-24.398333/rate=5.02512562814_999.npy\n",
"maximal possible rate: 4.1\n"
"test_device1 measured\n",
"test_device2 measured\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"plotting\n",
"maximal possible rate: 9.9\n"
]
}
],
......@@ -557,13 +616,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"outputs": [
{
"ename": "AttributeError",
"evalue": "'IO' object has no attribute 'logbook'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-dcd1f467520d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_config\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdevices\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m29\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0melog\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/Users/ai/Desktop/lwfatools/DataManager.py\u001b[0m in \u001b[0;36mload\u001b[0;34m(self, name_id, kind, gui, elog)\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0melog\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mrequests\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m response = requests.get(self.logbook.read(name_id)[-1][0],\n\u001b[0m\u001b[1;32m 171\u001b[0m \u001b[0mverify\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 172\u001b[0m auth=('sauerwein_n', 'Dark374Fields!'))\n",
"\u001b[0;31mAttributeError\u001b[0m: 'IO' object has no attribute 'logbook'"
]
}
],
"source": [
"dm.set_config(devices,io.load(29, elog = True))"
]
......@@ -627,6 +699,297 @@
"dm.get_config(devices)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import threading"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"threading.active_count()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function wait in module threading:\n",
"\n",
"wait(self, timeout=None)\n",
" Wait for the barrier.\n",
" \n",
" When the specified number of threads have started waiting, they are all\n",
" simultaneously awoken. If an 'action' was provided for the barrier, one\n",
" of the threads will have executed that callback prior to returning.\n",
" Returns an individual index number from 0 to 'parties-1'.\n",
"\n"
]
}
],
"source": [
"help(threading.Barrier.wait)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[<_MainThread(MainThread, started 140735297941504)>,\n",
" <Thread(Thread-2, started daemon 123145308631040)>,\n",
" <Heartbeat(Thread-3, started daemon 123145313886208)>,\n",
" <HistorySavingThread(IPythonHistorySavingThread, started 123145320214528)>,\n",
" <ParentPollerUnix(Thread-1, started daemon 123145325469696)>]"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lth = threading.enumerate()\n",
"lth"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am doing my stuff\n"
]
}
],
"source": [
"th.start()"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"th = lth[1]\n",
"def test():\n",
" import time\n",
" print ('I am doing my stuff')\n",
" time.sleep(5)\n",
"\n",
"th = threading.Thread(target = test, name = 'test')"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'Thread-2'"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"th.getName()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class Thread in module threading:\n",
"\n",
"class Thread(builtins.object)\n",
" | A class that represents a thread of control.\n",
" | \n",
" | This class can be safely subclassed in a limited fashion. There are two ways\n",
" | to specify the activity: by passing a callable object to the constructor, or\n",
" | by overriding the run() method in a subclass.\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None)\n",
" | This constructor should always be called with keyword arguments. Arguments are:\n",
" | \n",
" | *group* should be None; reserved for future extension when a ThreadGroup\n",
" | class is implemented.\n",
" | \n",
" | *target* is the callable object to be invoked by the run()\n",
" | method. Defaults to None, meaning nothing is called.\n",
" | \n",
" | *name* is the thread name. By default, a unique name is constructed of\n",
" | the form \"Thread-N\" where N is a small decimal number.\n",
" | \n",
" | *args* is the argument tuple for the target invocation. Defaults to ().\n",
" | \n",
" | *kwargs* is a dictionary of keyword arguments for the target\n",
" | invocation. Defaults to {}.\n",
" | \n",
" | If a subclass overrides the constructor, it must make sure to invoke\n",
" | the base class constructor (Thread.__init__()) before doing anything\n",
" | else to the thread.\n",
" | \n",
" | __repr__(self)\n",
" | Return repr(self).\n",
" | \n",
" | getName(self)\n",
" | \n",
" | isAlive = is_alive(self)\n",
" | \n",
" | isDaemon(self)\n",
" | \n",
" | is_alive(self)\n",
" | Return whether the thread is alive.\n",
" | \n",
" | This method returns True just before the run() method starts until just\n",
" | after the run() method terminates. The module function enumerate()\n",
" | returns a list of all alive threads.\n",
" | \n",
" | join(self, timeout=None)\n",
" | Wait until the thread terminates.\n",
" | \n",
" | This blocks the calling thread until the thread whose join() method is\n",
" | called terminates -- either normally or through an unhandled exception\n",
" | or until the optional timeout occurs.\n",
" | \n",
" | When the timeout argument is present and not None, it should be a\n",
" | floating point number specifying a timeout for the operation in seconds\n",
" | (or fractions thereof). As join() always returns None, you must call\n",
" | isAlive() after join() to decide whether a timeout happened -- if the\n",
" | thread is still alive, the join() call timed out.\n",
" | \n",
" | When the timeout argument is not present or None, the operation will\n",
" | block until the thread terminates.\n",
" | \n",
" | A thread can be join()ed many times.\n",
" | \n",
" | join() raises a RuntimeError if an attempt is made to join the current\n",
" | thread as that would cause a deadlock. It is also an error to join() a\n",
" | thread before it has been started and attempts to do so raises the same\n",
" | exception.\n",
" | \n",
" | run(self)\n",
" | Method representing the thread's activity.\n",
" | \n",
" | You may override this method in a subclass. The standard run() method\n",
" | invokes the callable object passed to the object's constructor as the\n",
" | target argument, if any, with sequential and keyword arguments taken\n",
" | from the args and kwargs arguments, respectively.\n",
" | \n",
" | setDaemon(self, daemonic)\n",
" | \n",
" | setName(self, name)\n",
" | \n",
" | start(self)\n",
" | Start the thread's activity.\n",
" | \n",
" | It must be called at most once per thread object. It arranges for the\n",
" | object's run() method to be invoked in a separate thread of control.\n",
" | \n",
" | This method will raise a RuntimeError if called more than once on the\n",
" | same thread object.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors defined here:\n",
" | \n",
" | __dict__\n",
" | dictionary for instance variables (if defined)\n",
" | \n",
" | __weakref__\n",
" | list of weak references to the object (if defined)\n",
" | \n",
" | daemon\n",
" | A boolean value indicating whether this thread is a daemon thread.\n",
" | \n",
" | This must be set before start() is called, otherwise RuntimeError is\n",
" | raised. Its initial value is inherited from the creating thread; the\n",
" | main thread is not a daemon thread and therefore all threads created in\n",
" | the main thread default to daemon = False.\n",
" | \n",
" | The entire Python program exits when no alive non-daemon threads are\n",
" | left.\n",
" | \n",
" | ident\n",
" | Thread identifier of this thread or None if it has not been started.\n",
" | \n",
" | This is a nonzero integer. See the thread.get_ident() function. Thread\n",
" | identifiers may be recycled when a thread exits and another thread is\n",
" | created. The identifier is available even after the thread has exited.\n",