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
......
This diff is collapsed.
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