Commit 75c2d3ab authored by Nick Sauerwein's avatar Nick Sauerwein
Browse files

before improvement to gui

parent ab63d04a
...@@ -51,16 +51,13 @@ def get_subfolders(base_folder): ...@@ -51,16 +51,13 @@ def get_subfolders(base_folder):
return subfolders return subfolders
def find_configs(glob_input): def find(folders, total = False, kind = 'config',accepted_differences = [['time'], ['comment'], ['folder']], security = True):
# add conditions here (what can be different in the setting, this is a security that no to different measurements are compared)
def find(base_folder, total = False, kind = 'config'):
if total: if total:
subfolders = get_subfolders(base_folder) subfolders = get_subfolders(folders)
else: else:
from glob import glob from glob import glob
subfolders = glob(glob_input) subfolders = glob(folders)
configs = [] configs = []
...@@ -69,7 +66,17 @@ def find(base_folder, total = False, kind = 'config'): ...@@ -69,7 +66,17 @@ def find(base_folder, total = False, kind = 'config'):
for config in new_configs: for config in new_configs:
config['folder'] = folder config['folder'] = folder
configs += new_configs configs += new_configs
if security:
actual_differences = get_differences(configs)
differences = [x for x in actual_differences if x not in accepted_differences]
if len(differences) != 0:
raise Exception('The configs differ also in '+str(differences)+'. You have to accept them in order to find them.')
return configs return configs
def get_differences(dics, headids = []): def get_differences(dics, headids = []):
...@@ -173,7 +180,7 @@ def extract(measurements, list_of_ids): ...@@ -173,7 +180,7 @@ def extract(measurements, list_of_ids):
print ("id ",i," doesn't exsist. Choose from: ",set(itertools.chain(*[m.keys() for m in ms_wo_none]))) print ("id ",i," doesn't exsist. Choose from: ",set(itertools.chain(*[m.keys() for m in ms_wo_none])))
assert (False) assert (False)
ms = ms_new ms = ms_new
return ms return np.array(ms)
def print_data(data, plot = True): def print_data(data, plot = True):
print_dict(data, plot = plot) print_dict(data, plot = plot)
......
...@@ -5,6 +5,9 @@ import pypylon ...@@ -5,6 +5,9 @@ import pypylon
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
from Devices.CameraTools import binArray
''' '''
config file: config file:
ExposureTimeAbs: exposure time in us ExposureTimeAbs: exposure time in us
...@@ -29,12 +32,14 @@ class FocusCamera: ...@@ -29,12 +32,14 @@ class FocusCamera:
self.psize=1.67 #um self.psize=1.67 #um
self.um=int(1./self.psize)
self.ep=1.74/self.psize self.camera_serial_number = 22005848
self.connect_camera() self.connect_camera()
self.set_config(config) self.set_config(config)
self.plot_server_im = None
def set_config(self,config): def set_config(self,config):
self.min_intensity = config['min_intensity'] self.min_intensity = config['min_intensity']
...@@ -61,47 +66,67 @@ class FocusCamera: ...@@ -61,47 +66,67 @@ class FocusCamera:
max_int = np.max(im) max_int = np.max(im)
if max_int > self.min_intensity: if max_int > self.min_intensity:
maximum_pos, contour = LA.find_beam(im, plot = False) maximum_pos, contour = LA.find_beam(im, plot = False)
cxmm, cymm = contour cxum, cyum = contour
FWHMx = np.max(cxmm) - np.min(cxmm) FWHMx = np.max(cxum) - np.min(cxum)
FWHMy = np.max(cymm) - np.min(cymm) FWHMy = np.max(cyum) - np.min(cyum)
comment = 'beam detected' comment = 'beam detected'
else: else:
comment = 'No beam detected' comment = 'No beam detected'
FWHMx = 0.020 FWHMx = 20
FWHMy = 0.020 FWHMy = 20
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['cxum'], measurement['cyum'] = (comment, FWHMx, FWHMy, cxmm, cymm)
measurement['maxit'] = max_int measurement['maxit'] = max_int
return measurement return measurement
def plot_measure(self, measurement, config = None): def plot_measure(self, measurement, config = None):
im = measurement['im']
from PlottingServers import Plot2DServer
plt.figure('measurement: FocusCamera')
ymin, ymax = plt.gca().get_ylim() if self.plot_server_im == None:
xmin, xmax = plt.gca().get_xlim() self.plot_server_im = Plot2DServer({'title':'FocusCamera', 'xlabel':'y [um]', 'ylabel': 'x [um]', 'image': True})
print (ymin, ymax)
if ymin == 0 and ymax == 1: im = measurement['im'].astype('float32')
ymin, ymax, xmin, xmax = (None, None, None, None)
plt.clf()
ax_beam = plt.subplot(111) shape = im.shape
max_int = np.max(im)
nFWHM = 2.
if max_int > self.min_intensity:
maximum_pos, contour = LA.find_beam(im, axis = ax_beam) cxum = measurement['cxum']
ymin, ymax = ax_beam.get_ylim() cyum = measurement['cyum']
xmin, xmax = ax_beam.get_xlim() FWHMx = measurement['FWHMx']
FWHMy = measurement['FWHMy']
if cxum:
xminum, xmaxum = np.min(cxum) - nFWHM * FWHMx, np.max(cxum) + nFWHM * FWHMx
yminum, ymaxum = np.min(cyum) - nFWHM * FWHMy, np.max(cyum) + nFWHM * FWHMy
im = im[xminum/self.psize:xmaxum/self.psize, yminum/self.psize:ymaxum/self.psize]
else: else:
try: xminum = 0
ax_beam.set_ylim([ymin, ymax]) yminum = 0
ax_beam.set_xlim([xmin, xmax]) xmaxum = shape[0] * self.psize
except: ymaxum = shape[1] * self.psize
print ()
LA.showimage(im, axis = ax_beam) import math
#autobinning
n_binning = math.ceil(((xmaxum - xminum)/self.psize)/500)
print (n_binning)
im = binArray(im, 0, n_binning, n_binning, np.mean)
im = binArray(im, 1, n_binning, n_binning, np.mean)
print ('bla')
self.plot_server_im.update(np.flipud(im),extent=(yminum,ymaxum, yminum, ymaxum))
def connect_camera(self): def connect_camera(self):
...@@ -115,15 +140,16 @@ class FocusCamera: ...@@ -115,15 +140,16 @@ class FocusCamera:
print(cam) print(cam)
for availca in available_cameras: for availca in available_cameras:
if str(availca)[-3] == '8': int(str(availca)[-10:-2])
if int(str(availca)[-10:-2]) == self.camera_serial_number:
self.cam_to_connect = availca self.cam_to_connect = availca
cam = pypylon.factory.create_device(availca) cam = pypylon.factory.create_device(availca)
print ('camera found =) Be happy') print ('--> camera (',self.camera_serial_number,') connected')
try: try:
cam.open() cam.open()
except: except:
assert('camera not found') assert('camera not connectable')
#cam.properties['GevSCPSPacketSize']=9015 #cam.properties['GevSCPSPacketSize']=9015
......
...@@ -5,19 +5,19 @@ import skimage ...@@ -5,19 +5,19 @@ import skimage
psize=1.67 psize=1.67
def px2mm(xpx,ypx): def px2um(xpx,ypx):
#return: xmm, ymm #return: xmm, ymm
xmm = xpx * psize /1000 xum = xpx * psize
ymm = ypx * psize /1000 yum = ypx * psize
return xmm, ymm return xum, yum
def mm2px(xmm,ymm): def um2px(xum,yum):
#return: xpx, ypx #return: xpx, ypx
xpx = np.round(xmm / psize *1000) xpx = np.round(xum / psize)
ypx = np.round(ymm / psize *1000) ypx = np.round(yum / psize)
return xpx, ypx return xpx, ypx
...@@ -26,18 +26,18 @@ def showimage(im, axis = None): ...@@ -26,18 +26,18 @@ def showimage(im, axis = None):
plt.figure() plt.figure()
ax = plt.subplot(111) ax = plt.subplot(111)
ny, nx = im.shape ny, nx = im.shape
axis.imshow(im,extent = [0-0.5*psize/1e3,nx*psize /1000-0.5*psize/1e3,ny*psize /1000-0.5*psize/1e3,0-0.5*psize/1e3])#, vmin = 0, vmax = 2e-4) axis.imshow(im,extent = [0-0.5*psize,nx*psize -0.5*psize,ny*psize -0.5*psize,0-0.5*psize])#, vmin = 0, vmax = 2e-4)
def find_beam(im, plot = True, nFWHM = 2, axis = None): def find_beam(im, plot = True, nFWHM = 2, axis = None):
#returns: (Maximum intensity, Positionx (mm), Positiony (mm)), HalfMaximum contour of beam #returns: (Maximum intensity, Positionx (um), Positiony (um)), HalfMaximum contour of beam
#ny, nx = im.shape #ny, nx = im.shape
itmaxypx, itmaxxpx = np.unravel_index(im.argmax(), im.shape) itmaxypx, itmaxxpx = np.unravel_index(im.argmax(), im.shape)
itmax = im[itmaxypx, itmaxxpx] itmax = im[itmaxypx, itmaxxpx]
itmaxxmm, itmaxymm = px2mm(itmaxxpx, itmaxypx) itmaxxum, itmaxyum = px2mm(itmaxxpx, itmaxypx)
from skimage.measure import find_contours from skimage.measure import find_contours
cs = find_contours(im, itmax/2) cs = find_contours(im, itmax/2)
...@@ -50,19 +50,26 @@ def find_beam(im, plot = True, nFWHM = 2, axis = None): ...@@ -50,19 +50,26 @@ def find_beam(im, plot = True, nFWHM = 2, axis = None):
cxpx = c.T[1] #+ itmaxxpx-framepx cxpx = c.T[1] #+ itmaxxpx-framepx
cypx = c.T[0] #+ itmaxypx-framepx cypx = c.T[0] #+ itmaxypx-framepx
cxmm, cymm = px2mm(cxpx, cypx) cxum, cyum = px2um(cxpx, cypx)
FWHMx = np.max(cxum) - np.min(cxum)
FWHMy = np.max(cyum) - np.min(cyum)
if plot: if plot:
showimage(im, axis = axis) showimage(im, axis = axis)
axis.plot(cxmm, cymm, 'g') axis.plot(cxum, cyum, 'g')
FWHMx = np.max(cxmm) - np.min(cxmm) FWHMx = np.max(cxum) - np.min(cxum)
FWHMy = np.max(cymm) - np.min(cymm) FWHMy = np.max(cyum) - np.min(cymm)
print (FWHMx, FWHMy)
axis.set_xlim(np.min(cxmm) - nFWHM * FWHMx, np.max(cxmm) + nFWHM * FWHMx) axis.set_xlim(np.min(cxum) - nFWHM * FWHMx, np.max(cxum) + nFWHM * FWHMx)
axis.set_ylim(np.min(cymm) - nFWHM * FWHMy, np.max(cymm) + nFWHM * FWHMy) axis.set_ylim(np.min(cyum) - nFWHM * FWHMy, np.max(cyum) + nFWHM * FWHMy)
axis.set_xlabel('x [mm]') axis.set_xlabel('x [mm]')
axis.set_ylabel('y [mm]') axis.set_ylabel('y [mm]')
axis.set_title('Center: ('+str(itmaxxmm)+', '+str(itmaxymm)+'), Intensity: '+str(itmax)) axis.set_title('Center: ('+str(itmaxxum)+', '+str(itmaxyum)+'), Intensity: '+str(itmax))
return (itmax, itmaxxmm, itmaxymm),(cxmm, cymm) return (itmax, itmaxxum, itmaxyum),(cxum, cyum)
\ No newline at end of file \ No newline at end of file
...@@ -61,6 +61,10 @@ class Interferometer: ...@@ -61,6 +61,10 @@ class Interferometer:
self.psize=1./127.95 #mm self.psize=1./127.95 #mm
self.mm=int(1./self.psize) self.mm=int(1./self.psize)
self.ep=1.74/self.psize self.ep=1.74/self.psize
self.camera_serial_number = 21547524
print ('connecting camera') print ('connecting camera')
self.connect_camera() self.connect_camera()
print ('done') print ('done')
...@@ -275,15 +279,16 @@ class Interferometer: ...@@ -275,15 +279,16 @@ class Interferometer:
print(cam) print(cam)
for availca in available_cameras: for availca in available_cameras:
if str(availca)[-3] == '4': int(str(availca)[-10:-2])
if int(str(availca)[-10:-2]) == self.camera_serial_number:
self.cam_to_connect = availca self.cam_to_connect = availca
cam = pypylon.factory.create_device(availca) cam = pypylon.factory.create_device(availca)
print ('camera found =) Be happy') print ('--> camera (',self.camera_serial_number,') connected')
try: try:
cam.open() cam.open()
except: except:
assert('camera not found') assert('camera not connectable')
#cam.properties['GevSCPSPacketSize']=9014 #cam.properties['GevSCPSPacketSize']=9014
......
...@@ -18,10 +18,11 @@ intensity: peak of signal on photodiode (mV) ...@@ -18,10 +18,11 @@ intensity: peak of signal on photodiode (mV)
class Laser: class Laser:
def __init__(self, config, DRS4): def __init__(self, config, DRS4):
self.old_comp_lin = 0
self.drs = DRS4 self.drs = DRS4
self.set_config(config) self.set_config(config)
self.plot_server = None self.plot_server = None
self.old_comp_lin = 0
def set_config(self, config): def set_config(self, config):
self.max_angle = config['max_angle'] self.max_angle = config['max_angle']
......
...@@ -52,7 +52,7 @@ class ParabolicMirror: ...@@ -52,7 +52,7 @@ class ParabolicMirror:
for motor in self.motors: for motor in self.motors:
motor.home() motor.home()
def optimize(self, focus, laser, Cz, wanted_focus_um, n_av = 1, sleep_time = 0.2, output = True, maxiter = 100): def optimize(self, focus, laser, per, wanted_focus_um, n_av = 1, sleep_time = 0.2, output = True, maxiter = 100):
#this variable is necessary to compensate the attenuation of the laser #this variable is necessary to compensate the attenuation of the laser
global intmult global intmult
intmult = 1. intmult = 1.
...@@ -107,11 +107,11 @@ class ParabolicMirror: ...@@ -107,11 +107,11 @@ class ParabolicMirror:
self.set_config(config) self.set_config(config)
#config_per = per.get_config() config_per = per.get_config()
#config_per['deltaz_Cz'] = zc config_per['deltaz_Cz'] = zc
Cz.mAbs(zc) #Cz.mAbs(zc)
#per.set_config(config_per) per.set_config(config_per)
return True return True
def opt_f(x): def opt_f(x):
...@@ -129,14 +129,13 @@ class ParabolicMirror: ...@@ -129,14 +129,13 @@ class ParabolicMirror:
return to_min()[0] return to_min()[0]
config0 = self.get_config() config0 = self.get_config()
#config_per0 = per.get_config() config_per0 = per.get_config()
config0_focus = focus.get_config() config0_focus = focus.get_config()
config0_focus['measure_full']=True config0_focus['measure_full']=True
focus.set_config(config0_focus) focus.set_config(config0_focus)
x0 = [config0['pos_Pay'], x0 = [config0['pos_Pay'],
config0['pos_Paz'], config0['pos_Paz'],
Cz.getPos()/30.] config_per0['deltaz_Cz']/30.]
#config_per0['deltaz_Cz']]
def after_it(res): def after_it(res):
print ('###################') print ('###################')
......
...@@ -93,7 +93,7 @@ class Periscope: ...@@ -93,7 +93,7 @@ class Periscope:
return pos_g - self.pos_local_origin_g return pos_g - self.pos_local_origin_g
def get_deltaz_Mz(self): def get_deltaz_Mz(self):
return self.Pz.position() - 25 + self.Mz.position() return - self.Pz.position() + self.Mz.position()
def get_deltaz_Cz(self): def get_deltaz_Cz(self):
return self.Pz.position() - 25 + self.Cz.position() return self.Pz.position() - 25 + self.Cz.position()
...@@ -103,8 +103,8 @@ class Periscope: ...@@ -103,8 +103,8 @@ class Periscope:
self.ymin = 0 self.ymin = 0
self.xmax = 0 self.xmax = 0
self.ymax = 25 self.ymax = 25
self.zmax = min((25 + self.deltaz_Mz, 25)) self.zmax = min((25 - self.deltaz_Mz, 25))
self.zmin = max((self.deltaz_Mz, 0)) self.zmin = max((-self.deltaz_Mz, 0))
def connect_motors(self): def connect_motors(self):
self.Mz = Z825B(83829619) #Z825B(83835879) self.Mz = Z825B(83829619) #Z825B(83835879)
...@@ -112,19 +112,39 @@ class Periscope: ...@@ -112,19 +112,39 @@ class Periscope:
self.Py = Z825B(83832219) self.Py = Z825B(83832219)
self.Px = Z825B(83835886) self.Px = Z825B(83835886)
import Pyro4
ns = Pyro4.locateNS(host='pc9730.psi.ch', port = 9090)
Cz = Pyro4.Proxy("PYRONAME:APTserver")
Cz.add_motor(27501952)
#class dummmotor():
# def __init__(self):
# print ('dummmotor used')
# self.pos = 0.
# def position(self):
# return self.pos
#
# def goto(self,z):
# self.pos = z
# return True
#
# def home(self):
# self.pos = 0.
# return True
class dummmotor(): class dummmotor():
def __init__(self): def __init__(self):
print ('dummmotor used') print ('dummmotor used')
self.pos = 0. self.mot = Cz
def position(self): def position(self):
return self.pos return self.mot.getPos()
def goto(self,z): def goto(self,z):
self.pos = z self.mot.mAbs(z)
return True return True
def home(self): def home(self):
self.pos = 0. self.mot.home()
return True return True
self.Cz = dummmotor()#Z825B(83829619) self.Cz = dummmotor()#Z825B(83829619)
...@@ -187,10 +207,10 @@ class Periscope: ...@@ -187,10 +207,10 @@ class Periscope:
def _mzAbs(self, z): def _mzAbs(self, z):
deltaz = -self.deltaz_Mz deltaz = self.deltaz_Mz
assert (self.Pz.goto(z)), 'Motor Pz failed' assert (self.Pz.goto(z)), 'Motor Pz failed'
assert (self.Mz.goto(25 - z - deltaz)), 'Motor Mz failed' assert (self.Mz.goto(z + deltaz)), 'Motor Mz failed'
def _mCamAbs(self, z): def _mCamAbs(self, z):
deltaz = -self.deltaz_Cz deltaz = -self.deltaz_Cz
......
...@@ -36,9 +36,9 @@ class PlasmaCamHorizontal: ...@@ -36,9 +36,9 @@ class PlasmaCamHorizontal:
self.psize=1./127.95*3/1.70 #mm self.psize=1./127.95*3/1.70 #mm
self.ep=1.74/self.psize self.ep=1.74/self.psize
print ('connecting camera') self.camera_serial_number = 21145133
self.connect_camera() self.connect_camera()
print ('done')
self.set_config(config) self.set_config(config)
...@@ -119,15 +119,16 @@ class PlasmaCamHorizontal: ...@@ -119,15 +119,16 @@ class PlasmaCamHorizontal:
print(cam) print(cam)