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

optimization cleaned up

parent b1c729ef
......@@ -49,8 +49,9 @@ class ParabolicMirror:
for motor in self.motors:
motor.go_home()
def optimize(self, focus, wanted_focus_um, per, n_av = 10, sleep_time = 0, output = True, maxiter = 100, size_wanted = 6):
def optimize(self, focus, laser, per, wanted_focus_um, n_av = 10, sleep_time = 0.2, output = True, maxiter = 100):
#this variable is necessary to compensate the attenuation of the laser
intmult = 1.
def to_min():
FWHMxs = []
FWHMys = []
......@@ -65,11 +66,26 @@ class ParabolicMirror:
focus.plot_measure(measurement)
plt.pause(0.01)
del measurement
print ('FWHMxs: ',FWHMxs)
print ('maxits: ',maxits)
res = np.sqrt(np.array(FWHMxs)**2 + np.array(FWHMys)**2)/np.sqrt(2)*4096/np.array(maxits)
if output:
print ('FWHMxs: ',FWHMxs)
print ('maxits: ',maxits)
res = np.sqrt(np.array(FWHMxs)**2 + np.array(FWHMys)**2)/np.sqrt(2)*4096/np.array(maxits)*intmult
mean, error = np.mean(res), np.std(res, ddof = -1)
print ('mean: ',mean,'error: ', error)
if mean > 4e3:
config_laser = laser.get_config()
config_laser['attenuation'] = config_laser['attenuation']/1.4
laser.set_config(config_laser)
intmult *= 1.4
if output:
print ('mean: ',mean,'error: ', error)
if mean*1e3 < wanted_focus_um:
if output:
print ('The optimization converged. Final value: ',mean)
return
return mean, error
def change_parameter(y, z, zc):
......@@ -88,9 +104,11 @@ class ParabolicMirror:
y = x[0]
z = x[1]
zc = x[2]
print ('-------------------------------------------')
if output:
print ('-------------------------------------------')
change_parameter(y,z, zc)
#wait for oszillation to decay
import time
time.sleep(sleep_time)
......@@ -104,27 +122,20 @@ class ParabolicMirror:
x0 = [config0['pos_Pay'],
config0['pos_Paz'],
config_per0['deltaz_Cz']]
# directly Nedler-Mead
import scipy.optimize
res = scipy.optimize.minimize(opt_f, x0, method='Nelder-Mead', options={'disp': output,
'initial_simplex': None,
'maxiter': maxiter,
'xatol': 0.00001,
'xatol': 0.00005,
'return_all': False,
'fatol': np.sqrt(size_wanted**2*2)})
'fatol': wanted_focus_um})
if output:
print (res)
return res
# first measure
#loop till focus small enough
# optimize Pax
# optimize Pay
# optimize Cz
......
......@@ -17,7 +17,7 @@ class interaction_tool:
self.device = device
self.config = config
layout_bt = widgets.Layout(width = '20px')
layout_text = widgets.Layout(width = 'auto')
layout_text = widgets.Layout(width = '180px')
if type(value) == bool:
self.cb = widgets.Checkbox(value=value,
description=variable_name, layout = layout_text)
......
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 37,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -529,7 +529,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -555,7 +555,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 48,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -566,7 +566,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Using matplotlib backend: Qt5Agg\n"
"This computer does not support elog\n",
"Using matplotlib backend: MacOSX\n"
]
}
],
......@@ -585,7 +586,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 49,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -593,14 +594,14 @@
},
"outputs": [],
"source": [
"#devices = [delay, inter, drs, fara, per, blade] #use this to run the LWFA\n",
"devices = [per, para, laser, focus]\n",
"#devices = [td1, td2]#[delay, inter, blade] #Use this do debug the gui and data menager"
"#devices = [delay, inter, drs, fara, per, blade] #use this to run the LWFA\n",
"#devices = [per, para, laser, focus] #use this to do focus optimization\n",
"devices = [td1, td2] #Use this do debug the gui and data menager"
]
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 50,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -609,16 +610,139 @@
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"FocusCamera measured\n",
"2.57531650106 2.60730883615\n",
"0.00671749449036 0.0071683946427\n",
"maximal possible rate: 0.8\n",
"set rate cannot be reached.\n",
"config saved as config2017-03-28_16-55-34.214243.npy\n"
]
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8e8af3de41934f48a320330b848139a8"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d7e0387c4dd54762a1093afc72280455"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "73bd2aeff4cf42e7824989917cd120a5"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "89735e9c9fc4446aaf61990b8799a61f"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "40507e43073e49ddb97ff0ea5beda0c2"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9a91ee404b6f4089a10196111d718e40"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6fc4b50e42274687b65938fda23b4703"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "acd38a37411c40d2bf09424c8f4431ec"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "229469f3dd4040258cca303c9d43e690"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e77b5ca8057f4bfc939795375d76ecf1"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8f2d45ace1d64bafa7e085b315bcc203"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "baa2828eef63435b914895319d1bf229"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e98f49002f8a4fef9d1344a3ee46cae8"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0470df650ae64ed5aa2439cca2044243"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5f41c8145fbd484bab2b31d499dc1238"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
......@@ -1413,12 +1537,12 @@
],
"source": [
"%autoreload\n",
"para.optimize(focus, 0.006, per, output = True, n_av = 10, sleep_time= 0.5)"
"para.optimize(focus, laser, per, 0.006, output = True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -1426,26 +1550,199 @@
},
"outputs": [],
"source": [
"for i in range(20):\n",
" "
"class test:\n",
" def __init__(self, du):\n",
" self.du = du\n",
" \n",
" def give(self):\n",
" print (self.du)\n",
" return self.du\n",
" def kase(si):\n",
" print (si.du)\n",
" \n",
" def __str__(self):\n",
" return 'hege: '+str(self.du)\n",
" \n",
" def __add__(self, right):\n",
" return test(self.du + right.du)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"t1 = test(2)\n",
"t2 = test(4)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t1.give()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t1.du"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"t1.kase()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"t1.du = 4"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t1.give()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hege: 2\n"
]
}
],
"source": [
"print (t1)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hege: 6\n"
]
}
],
"source": [
"print (t1 + t2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
......@@ -1459,7 +1756,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
"version": "3.6.0"
},
"widgets": {
"state": {
......
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