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):
return subfolders
def find_configs(glob_input):
def find(base_folder, total = False, kind = 'config'):
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)
if total:
subfolders = get_subfolders(base_folder)
subfolders = get_subfolders(folders)
else:
from glob import glob
subfolders = glob(glob_input)
subfolders = glob(folders)
configs = []
......@@ -69,7 +66,17 @@ def find(base_folder, total = False, kind = 'config'):
for config in new_configs:
config['folder'] = folder
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
def get_differences(dics, headids = []):
......@@ -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])))
assert (False)
ms = ms_new
return ms
return np.array(ms)
def print_data(data, plot = True):
print_dict(data, plot = plot)
......
......@@ -5,6 +5,9 @@ import pypylon
import matplotlib.pyplot as plt
import numpy as np
from Devices.CameraTools import binArray
'''
config file:
ExposureTimeAbs: exposure time in us
......@@ -29,12 +32,14 @@ class FocusCamera:
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.set_config(config)
self.plot_server_im = None
def set_config(self,config):
self.min_intensity = config['min_intensity']
......@@ -61,47 +66,67 @@ class FocusCamera:
max_int = np.max(im)
if max_int > self.min_intensity:
maximum_pos, contour = LA.find_beam(im, plot = False)
cxmm, cymm = contour
FWHMx = np.max(cxmm) - np.min(cxmm)
FWHMy = np.max(cymm) - np.min(cymm)
cxum, cyum = contour
FWHMx = np.max(cxum) - np.min(cxum)
FWHMy = np.max(cyum) - np.min(cyum)
comment = 'beam detected'
else:
comment = 'No beam detected'
FWHMx = 0.020
FWHMy = 0.020
FWHMx = 20
FWHMy = 20
cxmm = 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
return measurement
def plot_measure(self, measurement, config = None):
im = measurement['im']
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 > 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()
from PlottingServers import Plot2DServer
if self.plot_server_im == None:
self.plot_server_im = Plot2DServer({'title':'FocusCamera', 'xlabel':'y [um]', 'ylabel': 'x [um]', 'image': True})
im = measurement['im'].astype('float32')
shape = im.shape
nFWHM = 2.
cxum = measurement['cxum']
cyum = measurement['cyum']
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:
try:
ax_beam.set_ylim([ymin, ymax])
ax_beam.set_xlim([xmin, xmax])
except:
print ()
LA.showimage(im, axis = ax_beam)
xminum = 0
yminum = 0
xmaxum = shape[0] * self.psize
ymaxum = shape[1] * self.psize
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):
......@@ -115,15 +140,16 @@ class FocusCamera:
print(cam)
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
cam = pypylon.factory.create_device(availca)
print ('camera found =) Be happy')
print ('--> camera (',self.camera_serial_number,') connected')
try:
cam.open()
except:
assert('camera not found')
assert('camera not connectable')
#cam.properties['GevSCPSPacketSize']=9015
......
......@@ -5,19 +5,19 @@ import skimage
psize=1.67
def px2mm(xpx,ypx):
def px2um(xpx,ypx):
#return: xmm, ymm
xmm = xpx * psize /1000
ymm = ypx * psize /1000
xum = xpx * psize
yum = ypx * psize
return xmm, ymm
return xum, yum
def mm2px(xmm,ymm):
def um2px(xum,yum):
#return: xpx, ypx
xpx = np.round(xmm / psize *1000)
ypx = np.round(ymm / psize *1000)
xpx = np.round(xum / psize)
ypx = np.round(yum / psize)
return xpx, ypx
......@@ -26,18 +26,18 @@ def showimage(im, axis = None):
plt.figure()
ax = plt.subplot(111)
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):
#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
itmaxypx, itmaxxpx = np.unravel_index(im.argmax(), im.shape)
itmax = im[itmaxypx, itmaxxpx]
itmaxxmm, itmaxymm = px2mm(itmaxxpx, itmaxypx)
itmaxxum, itmaxyum = px2mm(itmaxxpx, itmaxypx)
from skimage.measure import find_contours
cs = find_contours(im, itmax/2)
......@@ -50,19 +50,26 @@ def find_beam(im, plot = True, nFWHM = 2, axis = None):
cxpx = c.T[1] #+ itmaxxpx-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:
showimage(im, axis = axis)
axis.plot(cxmm, cymm, 'g')
FWHMx = np.max(cxmm) - np.min(cxmm)
FWHMy = np.max(cymm) - np.min(cymm)
print (FWHMx, FWHMy)
axis.set_xlim(np.min(cxmm) - nFWHM * FWHMx, np.max(cxmm) + nFWHM * FWHMx)
axis.set_ylim(np.min(cymm) - nFWHM * FWHMy, np.max(cymm) + nFWHM * FWHMy)
axis.plot(cxum, cyum, 'g')
FWHMx = np.max(cxum) - np.min(cxum)
FWHMy = np.max(cyum) - np.min(cymm)
axis.set_xlim(np.min(cxum) - nFWHM * FWHMx, np.max(cxum) + nFWHM * FWHMx)
axis.set_ylim(np.min(cyum) - nFWHM * FWHMy, np.max(cyum) + nFWHM * FWHMy)
axis.set_xlabel('x [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)
\ No newline at end of file
return (itmax, itmaxxum, itmaxyum),(cxum, cyum)
\ No newline at end of file
......@@ -61,6 +61,10 @@ class Interferometer:
self.psize=1./127.95 #mm
self.mm=int(1./self.psize)
self.ep=1.74/self.psize
self.camera_serial_number = 21547524
print ('connecting camera')
self.connect_camera()
print ('done')
......@@ -275,15 +279,16 @@ class Interferometer:
print(cam)
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
cam = pypylon.factory.create_device(availca)
print ('camera found =) Be happy')
print ('--> camera (',self.camera_serial_number,') connected')
try:
cam.open()
except:
assert('camera not found')
assert('camera not connectable')
#cam.properties['GevSCPSPacketSize']=9014
......
......@@ -18,10 +18,11 @@ intensity: peak of signal on photodiode (mV)
class Laser:
def __init__(self, config, DRS4):
self.old_comp_lin = 0
self.drs = DRS4
self.set_config(config)
self.plot_server = None
self.old_comp_lin = 0
def set_config(self, config):
self.max_angle = config['max_angle']
......
......@@ -52,7 +52,7 @@ class ParabolicMirror:
for motor in self.motors:
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
global intmult
intmult = 1.
......@@ -107,11 +107,11 @@ class ParabolicMirror:
self.set_config(config)
#config_per = per.get_config()
config_per = per.get_config()
#config_per['deltaz_Cz'] = zc
Cz.mAbs(zc)
#per.set_config(config_per)
config_per['deltaz_Cz'] = zc
#Cz.mAbs(zc)
per.set_config(config_per)
return True
def opt_f(x):
......@@ -129,14 +129,13 @@ class ParabolicMirror:
return to_min()[0]
config0 = self.get_config()
#config_per0 = per.get_config()
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'],
Cz.getPos()/30.]
#config_per0['deltaz_Cz']]
config_per0['deltaz_Cz']/30.]
def after_it(res):
print ('###################')
......
......@@ -93,7 +93,7 @@ class Periscope:
return pos_g - self.pos_local_origin_g
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):
return self.Pz.position() - 25 + self.Cz.position()
......@@ -103,8 +103,8 @@ class Periscope:
self.ymin = 0
self.xmax = 0
self.ymax = 25
self.zmax = min((25 + self.deltaz_Mz, 25))
self.zmin = max((self.deltaz_Mz, 0))
self.zmax = min((25 - self.deltaz_Mz, 25))
self.zmin = max((-self.deltaz_Mz, 0))
def connect_motors(self):
self.Mz = Z825B(83829619) #Z825B(83835879)
......@@ -112,19 +112,39 @@ class Periscope:
self.Py = Z825B(83832219)
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():
def __init__(self):
print ('dummmotor used')
self.pos = 0.
self.mot = Cz
def position(self):
return self.pos
return self.mot.getPos()
def goto(self,z):
self.pos = z
self.mot.mAbs(z)
return True
def home(self):
self.pos = 0.
self.mot.home()
return True
self.Cz = dummmotor()#Z825B(83829619)
......@@ -187,10 +207,10 @@ class Periscope:
def _mzAbs(self, z):
deltaz = -self.deltaz_Mz
deltaz = self.deltaz_Mz
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):
deltaz = -self.deltaz_Cz
......
......@@ -36,9 +36,9 @@ class PlasmaCamHorizontal:
self.psize=1./127.95*3/1.70 #mm
self.ep=1.74/self.psize
print ('connecting camera')
self.camera_serial_number = 21145133
self.connect_camera()
print ('done')
self.set_config(config)
......@@ -119,15 +119,16 @@ class PlasmaCamHorizontal:
print(cam)
for availca in available_cameras:
if str(availca)[-3] == '3' and str(availca)[-4] == '3':
int(str(availca)[-10:-2])
if int(str(availca)[-10:-2]) == self.camera_serial_number:
self.cam_to_connect = availca
cam = pypylon.factory.create_device(availca)
print ('camera found =) Be happy')
print ('--> camera (',self.camera_serial_number,') connected')
try:
cam.open()
except:
assert('camera not found')
assert('camera not connectable')
#cam.properties['GevSCPSPacketSize']=9014
......
......@@ -41,9 +41,10 @@ class ScreenCamera:
self.psize=1./127.95*3/1.70 #mm
self.ep=1.74/self.psize
print ('connecting camera')
self.camera_serial_number = 21547524
self.connect_camera()
print ('done')
self.set_config(config)
......@@ -77,13 +78,23 @@ class ScreenCamera:
if self.calibrated:
for img in self.cam.grab_images(2):
D.append(np.fliplr(img))
measurement = {'time': time,'im': D[0], 'im_c': D[1]}
im1 = D[0].astype('float64')
im_c = D[1].astype('float64')
im1 = im1 - im_c
measurement = {'time': time,'im': D[0], 'im_c': D[1], 'max_px': np.max(im1), 'min_px':np.min(im1), 'mean_px':np.mean(im1)}
return measurement
for img in self.cam.grab_images(1):
D.append(np.fliplr(img))
measurement = {'time': time,'im': D[0]}
return measurement
......@@ -128,15 +139,16 @@ class ScreenCamera:
print(cam)
for availca in available_cameras:
if str(availca)[-3] == '3' and str(availca)[-4] == '8':
int(str(availca)[-10:-2])
if int(str(availca)[-10:-2]) == self.camera_serial_number:
self.cam_to_connect = availca
cam = pypylon.factory.create_device(availca)
print ('camera found =) Be happy')
print ('--> camera (',self.camera_serial_number,') connected')
try:
cam.open()
except:
assert('camera not found')
assert('camera not connectable')
#cam.properties['GevSCPSPacketSize']=9014
......
File mode changed from 100644 to 100755
No preview for this file type
File mode changed from 100644 to 100755
This source diff could not be displayed because it is too large. You can view the blob instead.
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