Commit b1c729ef authored by Sauerwein Nick Jacob's avatar Sauerwein Nick Jacob
Browse files

finished optimization with optimizer

parent 528a7d16
......@@ -19,6 +19,7 @@ FWHMx: FWHM of intensity in x
FWHMy: FWHM of intensity in y
cxmm: x coordinate of contour [mm]
cymm: y coordinate of contour [mm]
maxit: maximal intensity [0 - 4096]
'''
......@@ -66,12 +67,13 @@ class FocusCamera:
comment = 'beam detected'
else:
comment = 'No beam detected'
FWHMx = None
FWHMy = None
FWHMx = 0.020
FWHMy = 0.020
cxmm = None
cymm = None
measurement['comment'], measurement['FWHMx'], measurement['FWHMy'], measurement['cxmm'], measurement['cymm'] = (comment, FWHMx, FWHMy, cxmm, cymm)
measurement['maxit'] = max_int
return measurement
......@@ -81,18 +83,24 @@ class FocusCamera:
plt.figure('measurement: FocusCamera')
ymin, ymax = plt.gca().get_ylim()
xmin, xmax = plt.gca().get_xlim()
print (ymin, ymax)
if ymin == 0 and ymax == 1:
ymin, ymax, xmin, xmax = (None, None, None, None)
plt.clf()
ax_beam = plt.subplot(111)
max_int = np.max(im)
if max_int > 1000:
if max_int > self.min_intensity:
maximum_pos, contour = LA.find_beam(im, axis = ax_beam)
ymin, ymax = ax_beam.get_ylim()
xmin, xmax = ax_beam.get_xlim()
else:
ax_beam.set_ylim([ymin, ymax])
ax_beam.set_xlim([xmin, xmax])
try:
ax_beam.set_ylim([ymin, ymax])
ax_beam.set_xlim([xmin, xmax])
except:
print ()
LA.showimage(im, axis = ax_beam)
......
......@@ -41,7 +41,7 @@ def find_beam(im, plot = True, nFWHM = 2, axis = None):
from skimage.measure import find_contours
cs = find_contours(im, itmax/2)
print ('# of contours: ',len(cs))
#print ('# of contours: ',len(cs))
clens = [len(con[0]) for con in cs]
arg = np.argmax(clens)
c = cs[arg]
......
......@@ -31,21 +31,21 @@ class Laser:
config = {}
config['max_angle'] = self.max_angle
config['attenuation'] = self.get_attenuation()
config['lin_comp'] = self.get_lin_comp()
config['comp_lin'] = self.get_lin_comp()
config['channel'] = self.channel
return config
def set_attenuation(self, percent):
phi = np.arccos(percent/100)*180/np.pi + self.max_angle
phi = np.arccos(percent/100)*90/np.pi + self.max_angle
print (phi)
caput('F10HU-LMOT715:MOT.VAL', phi)
def get_attenuation(self):
phi = caget('F10HU-LMOT715:MOT.VAL')
return 100 * np.cos((phi - self.max_angle)/180*np.pi)
return 100 * np.cos((phi - self.max_angle)/90*np.pi)
def set_lin_comp(self, pos):
......
......@@ -49,51 +49,69 @@ class ParabolicMirror:
for motor in self.motors:
motor.go_home()
def optimize(self, focus, wanted_focus_um, n_av = 10, sleep_time = 0, output = True, maxiter = 100, size_wanted = 6):
def optimize(self, focus, wanted_focus_um, per, n_av = 10, sleep_time = 0, output = True, maxiter = 100, size_wanted = 6):
def to_min():
FWHMxs = []
FWHMys = []
maxits = []
for i in range(n_av):
measurement = focus.measure(full = True)
measurement = focus.measure()
FWHMxs.append(measurement['FWHMx'])
FWHMys.append(measurement['FWHMy'])
del measurement
res = np.array(FWHMx)**2 + np.array(FWHMy)**2
return np.mean(res), np.std(res, ddof = -1)
def change_parameter(y, z):
maxits.append(measurement['maxit'])
if output:
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)
mean, error = np.mean(res), np.std(res, ddof = -1)
print ('mean: ',mean,'error: ', error)
return mean, error
def change_parameter(y, z, zc):
print ('Parameters: ',(y, z, zc))
config = {'pos_Pay': y, 'pos_Paz': z}
self.set_config(config)
config_per = per.get_config()
config_per['deltaz_Cz'] = zc
per.set_config(config_per)
return True
def opt_f(x):
y = x[0]
z = x[1]
change_parameter(y, z)
zc = x[2]
print ('-------------------------------------------')
change_parameter(y,z, zc)
import time
time.sleep(sleep_time)
return to_min()
return to_min()[0]
config0 = self.get_config()
x0 = [config0['pos_Pay'], config0['pos_Paz']]
config_per0 = per.get_config()
config0_focus = focus.get_config()
config0_focus['measure_full']=True
focus.set_config(config0_focus)
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,
'return_all': False,
'fatol': size_wanted**2*2})
'fatol': np.sqrt(size_wanted**2*2)})
return res
......
......@@ -33,7 +33,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Using matplotlib backend: MacOSX\n"
"Using matplotlib backend: Qt5Agg\n"
]
}
],
......@@ -54,25 +54,13 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'io' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-a807224b676d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mconfig\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mio\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m \u001b[1;33m,\u001b[0m\u001b[0mconfig\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'comment'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'io' is not defined"
]
}
],
"outputs": [],
"source": [
"for i in range(-1,0):\n",
" config = io.load(i)\n",
......@@ -92,7 +80,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"start of GUI\n"
"before optimization 2\n"
]
}
],
......@@ -115,7 +103,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 4,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -128,8 +116,8 @@
"text": [
"available cameras:\n",
"<DeviceInfo Basler scA1400-17gm (21145133)>\n",
"<DeviceInfo Basler avA2300-25gmDSY (21547524)>\n",
"<DeviceInfo Basler acA3800-10gm (22005848)>\n",
"<DeviceInfo Basler avA2300-25gmDSY (21547524)>\n",
"camera found =) Be happy\n"
]
}
......@@ -141,7 +129,7 @@
" para_config = config['FocusCamera']\n",
"except:\n",
" para_config = {'ExposureTimeAbs': 10000,\n",
" 'min_intensity': 10000,\n",
" 'min_intensity': 2000,\n",
" 'measure_full' : True}\n",
" \n",
"focus = FocusCamera(para_config)"
......@@ -149,26 +137,44 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 21,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# of contours: 1\n"
]
}
],
"source": [
"meas = focus.measure()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 22,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.35225725265 2.40014815635\n",
"# of contours: 1\n",
"0.00924077391017 0.00951045659382\n"
]
}
],
"source": [
"focus.plot_measure(meas)\n",
"plt.show()"
......@@ -186,7 +192,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -227,7 +233,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -274,7 +280,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -293,24 +299,13 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'pos_L': -2.7956549894271179, 'pos_R': 344.25}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"blade.get_config()"
]
......@@ -327,24 +322,13 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"available cameras:\n",
"<DeviceInfo Basler avA2300-25gmDSY (21547524)>\n",
"<DeviceInfo Basler acA3800-10gm (22005848)>\n",
"camera found =) Be happy\n"
]
}
],
"outputs": [],
"source": [
"from Devices.Interferometer import Interferometer\n",
"\n",
......@@ -361,7 +345,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -374,7 +358,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -397,7 +381,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -443,7 +427,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -474,13 +458,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-4.46505117708\n"
]
}
],
"source": [
"from Devices.Laser import Laser\n",
"\n",
......@@ -537,7 +529,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -563,7 +555,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 11,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -574,8 +566,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"This computer does not support elog\n",
"Using matplotlib backend: MacOSX\n"
"Using matplotlib backend: Qt5Agg\n"
]
}
],
......@@ -594,7 +585,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 12,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -603,13 +594,13 @@
"outputs": [],
"source": [
"#devices = [delay, inter, drs, fara, per, blade] #use this to run the LWFA\n",
"#devices = [per, para, focus]\n",
"devices = [td1, td2]#[delay, inter, blade] #Use this do debug the gui and data menager"
"devices = [per, para, laser, focus]\n",
"#devices = [td1, td2]#[delay, inter, blade] #Use this do debug the gui and data menager"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 17,
"metadata": {
"collapsed": false,
"deletable": true,
......@@ -621,38 +612,29 @@
"name": "stdout",
"output_type": "stream",
"text": [
"maximal possible rate: 504.8\n",
"test_device1 measured\n"
"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"
]
}
],
"source": [
"plt.ion()\n",
"GUI.main(devices, io, parallel = True)"
"GUI.main(devices, io, parallel = False)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"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'"
]
}
],
"outputs": [],
"source": [
"dm.set_config(devices,io.load(29, elog = True))"
]
......@@ -685,242 +667,767 @@
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'DelayGenerator': {'jet_triggered': True,\n",
" 'mode': 'single shot',\n",
" 'rate': 10.204081632653061,\n",
" 't_sleep': 0.05},\n",
" 'Interferometer': {'ExposureTimeAbs': 35.0,\n",
" 'xmax': 6.0,\n",
" 'xmin': 2.6,\n",
" 'ymax': 12,\n",
" 'ymin': 3},\n",
" 'time': '2017-03-21_11-16-45.819604'}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dm.get_config(devices)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 35,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
"editable": true,
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function std in module numpy.core.fromnumeric:\n",
"\n",
"std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<class 'numpy._globals._NoValue'>)\n",
" Compute the standard deviation along the specified axis.\n",
" \n",
" Returns the standard deviation, a measure of the spread of a distribution,\n",
" of the array elements. The standard deviation is computed for the\n",
" flattened array by default, otherwise over the specified axis.\n",
" \n",
" Parameters\n",
" ----------\n",
" a : array_like\n",
" Calculate the standard deviation of these values.\n",
" axis : None or int or tuple of ints, optional\n",
" Axis or axes along which the standard deviation is computed. The\n",
" default is to compute the standard deviation of the flattened array.\n",
" \n",
" .. versionadded:: 1.7.0\n",
" \n",
" If this is a tuple of ints, a standard deviation is performed over\n",
" multiple axes, instead of a single axis or all the axes as before.\n",
" dtype : dtype, optional\n",
" Type to use in computing the standard deviation. For arrays of\n",
" integer type the default is float64, for arrays of float types it is\n",
" the same as the array type.\n",
" out : ndarray, optional\n",
" Alternative output array in which to place the result. It must have\n",
" the same shape as the expected output but the type (of the calculated\n",
" values) will be cast if necessary.\n",
" ddof : int, optional\n",
" Means Delta Degrees of Freedom. The divisor used in calculations\n",
" is ``N - ddof``, where ``N`` represents the number of elements.\n",
" By default `ddof` is zero.\n",
" keepdims : bool, optional\n",
" If this is set to True, the axes which are reduced are left\n",
" in the result as dimensions with size one. With this option,\n",
" the result will broadcast correctly against the input array.\n",
" \n",
" If the default value is passed, then `keepdims` will not be\n",
" passed through to the `std` method of sub-classes of\n",
" `ndarray`, however any non-default value will be. If the\n",
" sub-classes `sum` method does not implement `keepdims` any\n",
" exceptions will be raised.\n",
" \n",
" Returns\n",
" -------\n",
" standard_deviation : ndarray, see dtype parameter above.\n",
" If `out` is None, return a new array containing the standard deviation,\n",
" otherwise return a reference to the output array.\n",
" \n",
" See Also\n",