def Scan(self): from pyGPGO.covfunc import matern32 from pyGPGO.acquisition import Acquisition from pyGPGO.surrogates.GaussianProcess import GaussianProcess from pyGPGO.GPGO import GPGO spec.integration_time_micros(100 * 1000) def scan_f(Z, Y): l1 = spec.intensities() print("\n") print("Before :", int(Y), int(Z), int(positionvalue(5)), int(positionvalue(6)), int(max(l1))) #mouve(4, S, 'ABSOL') mouve(5, Y, 'ABSOL') mouve(6, Z, 'ABSOL') if positionvalue(5) != Y and positionvalue(6) != Z: time.sleep(0.6) l2 = spec.intensities() lw = spec.wavelengths() W_m = lw[np.array(l2).argmax()] print("After : ", int(Y), int(Z), "Y =", int(positionvalue(5)), "Z =", int(positionvalue(6)), "Imax =", int(max(l2))) print("Longeur d'onde Imax :", W_m) return max(l2) cov = matern32() gp = GaussianProcess(cov, optimize=True, usegrads=True) acq = Acquisition(mode='ExpectedImprovement') param = {'Z': ('cont', [0, 120 * 35]), 'Y': ('cont', [0, 120 * 20])} #np.random.seed(20) gpgo = GPGO(gp, acq, scan_f, param) gpgo.run(max_iter=10) gpgo.getResultZ() # print("Z max :", gpgo.getResultZ()) gpgo.getResultY() print("Y Max:", gpgo.getResultY()) print(gpgo.getResult()) mouve(5, gpgo.getResultY(), 'ABSOL') mouve(6, gpgo.getResultZ(), 'ABSOL') def scan_S(S, Y, Z): execution(ser, 'CRES4') # CRESn -> reset current position for an axis execution(ser, 'CRES5') execution(ser, 'CRES6') mouve(4, S, 'RELAT') mouve(5, Y, 'RELAT') mouve(6, Z, 'RELAT') if positionvalue(4) != S: time.sleep(0.2) l = spec.intensities() # return max(l) # cov = matern32() gp = GaussianProcess(cov, optimize=True, usegrads=True) acq = Acquisition(mode='ProbabilityImprovement') param = { 'S': ('cont', [-150, 150]), 'Z': ('cont', [-50, 50]), 'Y': ('cont', [-50, 50]) } # np.random.seed(20) gpgo = GPGO(gp, acq, scan_S, param) gpgo.run(max_iter=10) mouve(5, gpgo.getResultY(), 'ABSOL') mouve(6, gpgo.getResultZ(), 'ABSOL') mouve(4, gpgo.getResultS(), 'ABSOL')
def Bayesian(self): from statistics import mean spec = sb.Spectrometer(devices[0]) intTval = convert_str_int(self.intT.text(), 1000) spec.integration_time_micros(intTval) if c == 1: diameterZ = convert_str_int(self.Di.text(), 2) diameterY = convert_str_int(self.Di.text(), 1) else: diameterZ = convert_str_int(self.Di.text(), 40) diameterY = convert_str_int(self.Di.text(), 20) print("Diameter Z and Y :", diameterY, diameterZ) S_ini = (int(execution(ser, '?CNT4'))) # Motor 4 Y_ini = (int(execution(ser, '?CNT5'))) Z_ini = (int(execution(ser, '?CNT6'))) print(S_ini, Y_ini, Z_ini) from pyGPGO.covfunc import matern32 from pyGPGO.acquisition import Acquisition from pyGPGO.surrogates.GaussianProcess import GaussianProcess from pyGPGO.GPGO import GPGO if c == 1: range_focus = convert_str_int(self.range_focus.text(), 1) else: range_focus = convert_str_int(self.range_focus.text(), 20) intTval = convert_str_int(self.intT.text(), 1000) spec.integration_time_micros(intTval) def scan_F(S, Y, Z): mouve(4, S_ini + S, 'ABSOL') mouve(5, Y_ini + Y, 'ABSOL') mouve(6, Z_ini + Z, 'ABSOL') while execution(ser, "?ASTAT") != "RRRRRRUUU": time.sleep(0.1) l = spec.intensities() s = slice(1500, 2100) m = slice(2500, 3500) threshold = np.std(l[m]) if max(l[s]) > 3 * threshold: return max(l[s]) else: return None cov = matern32() gp = GaussianProcess(cov, optimize=True, usegrads=True) acq = Acquisition(mode='ExpectedImprovement') param = {'S': ('cont', [-(4 / 5) * range_focus, (4 / 5) * range_focus]), 'Z': ('cont', [-diameterZ/2, diameterZ/2]), 'Y': ('cont', [-diameterY/2, diameterY/2])} N_baye = convert_str_int(self.N_baye.text(), 1) gpgo = GPGO(gp, acq, scan_F, param) gpgo.run(max_iter=N_baye) mouve(5, Y_ini + gpgo.getResultY(), 'ABSOL') mouve(6, Z_ini + gpgo.getResultZ(), 'ABSOL') mouve(4, S_ini + gpgo.getResultS(), 'ABSOL') while execution(ser, "?ASTAT") != "RRRRRRUUU": time.sleep(0.1) if c == 1: self.Yscan.setText(str(-1 * round(Y_ini - gpgo.getResultY(), 2))) self.Zscan.setText(str(round(Z_ini - gpgo.getResultZ(), 2))) self.Imax.setText(str(round(gpgo.getResultI(), 0))) self.Smax.setText(str(round(S_ini + gpgo.getResultS(), 2))) else: self.Yscan.setText(str(-1 * round((Y_ini - gpgo.getResultY()) / 20, 2))) self.Zscan.setText(str(round((Z_ini - gpgo.getResultZ()) / 40, 2))) self.Imax.setText(str(round(gpgo.getResultI(), 0))) self.Smax.setText(str(round((S_ini + gpgo.getResultS()) / 20, 2))) spec.close() devices = []
def zoom_scan(): choice = QtGui.QMessageBox.question(self, 'Precision Scan', "Do you want to get a more accurate point?", # We create a Message box and a choice for the user QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) if choice == QtGui.QMessageBox.Yes: S_ini = (int(execution(ser, '?CNT4'))) # Motor 4 Y_ini = (int(execution(ser, '?CNT5'))) #?CNTn -> Read the position value of axis n Z_ini = (int(execution(ser, '?CNT6'))) print(S_ini, Y_ini, Z_ini) from pyGPGO.covfunc import matern32 # pyGPGO allow us to use the Bayesian optimization from pyGPGO.acquisition import Acquisition from pyGPGO.surrogates.GaussianProcess import GaussianProcess from pyGPGO.GPGO import GPGO if c == 1: # Used to convert µm in step and vice versa range_focus = convert_str_int(self.range_focus.text(), 1) # We let the user choose the focus range else: range_focus = convert_str_int(self.range_focus.text(), 20) intTval = convert_str_int(self.intT.text(),1000) spec.integration_time_micros(intTval) def scan_S(S, Y, Z): mouve(4, S_ini + S, 'ABSOL') #This time it also move on the s axis mouve(5, Y_ini + Y, 'ABSOL') mouve(6, Z_ini + Z, 'ABSOL') while execution(ser, "?ASTAT") != "RRRRRRUUU": #Wait for the end of the movement time.sleep(0.1) l = spec.intensities() # Read all intensities of the spectrum for each point s = slice(1500, 2100) m = slice(2500, 3500) threshold = np.std(l[m]) print(mean(l[m]), max(l[s])) if max(l[s])>3*mean(l[m]): # Work like the other threshold return max(l[s]) # Return the highest intensity else: return max(l[s])/3 # Reduce intensities' value that aren't above the threshold cov = matern32() # We choose the covariance function gp = GaussianProcess(cov, optimize=True, usegrads=True) acq = Acquisition(mode='ExpectedImprovement') param = {'S': ('cont', [-(3 / 5) * range_focus, (2 / 5) * range_focus]), # We set the range for Y, Z and S 'Z': ('cont', [-diameterZ * 0.1, diameterZ * 0.1]), 'Y': ('cont', [-diameterY * 0.1, diameterY * 0.1])} N_baye = convert_str_int(self.N_baye.text(), 1) # We let the user choose for the number of iterations of the bayesian process gpgo = GPGO(gp, acq, scan_S, param) gpgo.run(max_iter=N_baye) # Launch of the bayesian process mouve(5, Y_ini + gpgo.getResultY(), 'ABSOL') mouve(6, Z_ini + gpgo.getResultZ(), 'ABSOL') mouve(4, S_ini + gpgo.getResultS(), 'ABSOL') while execution(ser, "?ASTAT") != "RRRRRRUUU": time.sleep(0.1) if c == 1: self.Yscan.setText(str(-1 * round(Y_ini - gpgo.getResultY(), 2))) # Print all the results on the GUI self.Zscan.setText(str(round(Z_ini - gpgo.getResultZ(), 2))) self.Imax.setText(str(round(gpgo.getResultI(), 0))) self.Smax.setText(str(round(S_ini + gpgo.getResultS(), 2))) else: self.Yscan.setText(str(-1 * round((Y_ini - gpgo.getResultY()) / 20, 2))) self.Zscan.setText(str(round((Z_ini - gpgo.getResultZ()) / 40, 2))) self.Imax.setText(str(round(gpgo.getResultI(), 0))) self.Smax.setText(str(round((S_ini + gpgo.getResultS()) / 20, 2))) else: pass
def Scan(self): from pyGPGO.covfunc import matern32 # pyGPGO est le module qui permet d'éffectuer l'optimisation bayèsienne from pyGPGO.acquisition import Acquisition from pyGPGO.surrogates.GaussianProcess import GaussianProcess from pyGPGO.GPGO import GPGO diameterZ = convert_str_int( self.Di.text(), 35 ) # On définit le diamètre en fonction de l'indication de l'utilisateur, 35 est le facteur de conversion de Z diameterY = convert_str_int( self.Di.text(), 20 ) # La fonction convert_str_int() convertit une chaîne de caractère et la multiplie par le 2ième argument range_focus = convert_str_int( self.focus.text(), 0.5 ) # On divise cette valeur par 2 car l'intervalle va être [-range_focus;range_focus] spec.integration_time_micros( 100 * 1000 ) #On définit le temps d'intégration sans laisser le choix à l'utilisateur def scan_f(Z, Y): l1 = spec.intensities() #print("\n") #print("Before :", int(Y), int(Z), int(positionvalue(5)), int(positionvalue(6)), int(max(l1))) #mouve(4, S, 'ABSOL') mouve(5, Y, 'ABSOL') mouve(6, Z, 'ABSOL') #print(execution(ser, "?ASTAT")) #time.sleep(0.2) #print(execution(ser, "?ASTAT")) #time.sleep(1.2)# #print(execution(ser, "?ASTAT")) ## while execution(ser, "?ASTAT") != "RRRRRRUUU": time.sleep(0.1) l2 = spec.intensities() lw = spec.wavelengths() W_m = lw[np.array(l2).argmax()] #print("After : ", int(Y), int(Z), "Y =",int(positionvalue(5)),"Z =", int(positionvalue(6)),"Imax =", int(max(l2))) #print("Longeur d'onde Imax :",W_m) # return max(l2) cov = matern32() gp = GaussianProcess(cov, optimize=True, usegrads=True) acq = Acquisition(mode='ExpectedImprovement') param = { 'Z': ('cont', [0, diameterZ]), # On définit les intervalles de Z et Y 'Y': ('cont', [0, diameterY]) } #np.random.seed(20) gpgo = GPGO(gp, acq, scan_f, param) gpgo.run( max_iter=10 ) # On lance l'optimisation e nindiquant le nombre d'itérations à réaliser gpgo.getResultZ() print("Z max :", gpgo.getResultZ()) gpgo.getResultY() print("Y Max:", gpgo.getResultY()) print(gpgo.getResult()) mouve(5, gpgo.getResultY(), 'ABSOL') # On déplace la cellule aux coordonnées du rubis mouve(6, gpgo.getResultZ(), 'ABSOL') execution( ser, 'CRES4' ) # CRESn -> reset current position for an axis # On réinitialise l'origine de tout les points execution( ser, 'CRES5' ) # Cela est toujours nécéssaires pour se replacer sur les coordonnées execution(ser, 'CRES6') # du rubis après la nouvelle optimisation def scan_S(S, Y, Z): mouve( 4, S, 'RELAT' ) # Cette fois on se déplace aussi en profondeur, sur l'axe S mouve(5, Y, 'RELAT') mouve(6, Z, 'RELAT') while execution( ser, "?ASTAT" ) != "RRRRRRUUU": # On attend la fin du déplacement time.sleep(0.1) l = spec.intensities() #On relève le spèctre # return max( l) # On renvoie le maximun d'intensité su spectre obtenu # cov = matern32() gp = GaussianProcess(cov, optimize=True, usegrads=True) acq = Acquisition(mode='ProbabilityImprovement') param = { 'S': ('cont', [-range_focus, range_focus ]), # On définit les intervalles de valeurs de S,Y et Z 'Z': ('cont', [-50, 50]), 'Y': ('cont', [-50, 50]) } # #np.random.seed(20) gpgo = GPGO(gp, acq, scan_S, param) gpgo.run(max_iter=10 ) # On lance l'lgorithme qui doit éffectuer 10 itérations mouve(5, gpgo.getResultY(), 'ABSOL') mouve(6, gpgo.getResultZ(), 'ABSOL') mouve(4, gpgo.getResultS(), 'ABSOL') end = time.time() # Permet de mesurer le temps d'optimisation print(end - start) self.Yscan.setText(str(round( -1 * gpgo.getResultY(), 2))) # On affiche tous les résultats sur l'interface graphique self.Zscan.setText(str(round(gpgo.getResultZ(), 2))) self.Imax.setText(str(round(gpgo.getResultI(), 0))) self.Smax.setText(str(round(gpgo.getResultS(), 2)))