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 ...@@ -19,6 +19,7 @@ FWHMx: FWHM of intensity in x
FWHMy: FWHM of intensity in y FWHMy: FWHM of intensity in y
cxmm: x coordinate of contour [mm] cxmm: x coordinate of contour [mm]
cymm: y coordinate of contour [mm] cymm: y coordinate of contour [mm]
maxit: maximal intensity [0 - 4096]
''' '''
...@@ -66,12 +67,13 @@ class FocusCamera: ...@@ -66,12 +67,13 @@ class FocusCamera:
comment = 'beam detected' comment = 'beam detected'
else: else:
comment = 'No beam detected' comment = 'No beam detected'
FWHMx = None FWHMx = 0.020
FWHMy = None FWHMy = 0.020
cxmm = None cxmm = None
cymm = None cymm = None
measurement['comment'], measurement['FWHMx'], measurement['FWHMy'], measurement['cxmm'], measurement['cymm'] = (comment, FWHMx, FWHMy, cxmm, cymm) measurement['comment'], measurement['FWHMx'], measurement['FWHMy'], measurement['cxmm'], measurement['cymm'] = (comment, FWHMx, FWHMy, cxmm, cymm)
measurement['maxit'] = max_int
return measurement return measurement
...@@ -81,18 +83,24 @@ class FocusCamera: ...@@ -81,18 +83,24 @@ class FocusCamera:
plt.figure('measurement: FocusCamera') plt.figure('measurement: FocusCamera')
ymin, ymax = plt.gca().get_ylim() ymin, ymax = plt.gca().get_ylim()
xmin, xmax = plt.gca().get_xlim() 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() plt.clf()
ax_beam = plt.subplot(111) ax_beam = plt.subplot(111)
max_int = np.max(im) 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) maximum_pos, contour = LA.find_beam(im, axis = ax_beam)
ymin, ymax = ax_beam.get_ylim() ymin, ymax = ax_beam.get_ylim()
xmin, xmax = ax_beam.get_xlim() xmin, xmax = ax_beam.get_xlim()
else: else:
ax_beam.set_ylim([ymin, ymax]) try:
ax_beam.set_xlim([xmin, xmax]) ax_beam.set_ylim([ymin, ymax])
ax_beam.set_xlim([xmin, xmax])
except:
print ()
LA.showimage(im, axis = ax_beam) LA.showimage(im, axis = ax_beam)
......
...@@ -41,7 +41,7 @@ def find_beam(im, plot = True, nFWHM = 2, axis = None): ...@@ -41,7 +41,7 @@ def find_beam(im, plot = True, nFWHM = 2, axis = None):
from skimage.measure import find_contours from skimage.measure import find_contours
cs = find_contours(im, itmax/2) cs = find_contours(im, itmax/2)
print ('# of contours: ',len(cs)) #print ('# of contours: ',len(cs))
clens = [len(con[0]) for con in cs] clens = [len(con[0]) for con in cs]
arg = np.argmax(clens) arg = np.argmax(clens)
c = cs[arg] c = cs[arg]
......
...@@ -31,21 +31,21 @@ class Laser: ...@@ -31,21 +31,21 @@ class Laser:
config = {} config = {}
config['max_angle'] = self.max_angle config['max_angle'] = self.max_angle
config['attenuation'] = self.get_attenuation() config['attenuation'] = self.get_attenuation()
config['lin_comp'] = self.get_lin_comp() config['comp_lin'] = self.get_lin_comp()
config['channel'] = self.channel config['channel'] = self.channel
return config return config
def set_attenuation(self, percent): 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) caput('F10HU-LMOT715:MOT.VAL', phi)
def get_attenuation(self): def get_attenuation(self):
phi = caget('F10HU-LMOT715:MOT.VAL') 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): def set_lin_comp(self, pos):
......
...@@ -49,51 +49,69 @@ class ParabolicMirror: ...@@ -49,51 +49,69 @@ class ParabolicMirror:
for motor in self.motors: for motor in self.motors:
motor.go_home() 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(): def to_min():
FWHMxs = [] FWHMxs = []
FWHMys = [] FWHMys = []
maxits = []
for i in range(n_av): for i in range(n_av):
measurement = focus.measure(full = True) measurement = focus.measure()
FWHMxs.append(measurement['FWHMx']) FWHMxs.append(measurement['FWHMx'])
FWHMys.append(measurement['FWHMy']) FWHMys.append(measurement['FWHMy'])
del measurement maxits.append(measurement['maxit'])
res = np.array(FWHMx)**2 + np.array(FWHMy)**2 if output:
focus.plot_measure(measurement)
return np.mean(res), np.std(res, ddof = -1) plt.pause(0.01)
del measurement
def change_parameter(y, z): 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} config = {'pos_Pay': y, 'pos_Paz': z}
self.set_config(config) self.set_config(config)
config_per = per.get_config()
config_per['deltaz_Cz'] = zc
per.set_config(config_per)
return True return True
def opt_f(x): def opt_f(x):
y = x[0] y = x[0]
z = x[1] z = x[1]
zc = x[2]
change_parameter(y, z) print ('-------------------------------------------')
change_parameter(y,z, zc)
import time import time
time.sleep(sleep_time) time.sleep(sleep_time)
return to_min() return to_min()[0]
config0 = self.get_config() 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 # directly Nedler-Mead
import scipy.optimize
res = scipy.optimize.minimize(opt_f, x0, method='Nelder-Mead', options={'disp': output, res = scipy.optimize.minimize(opt_f, x0, method='Nelder-Mead', options={'disp': output,
'initial_simplex': None, 'initial_simplex': None,
'maxiter': maxiter, 'maxiter': maxiter,
'xatol': 0.00001, 'xatol': 0.00001,
'return_all': False, 'return_all': False,
'fatol': size_wanted**2*2}) 'fatol': np.sqrt(size_wanted**2*2)})
return res 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