def run(self): experiment_length = self.experiment_length exposures = self.old_database_content[:,self.db_column_names.index('exposure')] time = self.old_database_content[:,self.db_column_names.index('time')] try: z_position = self.old_database_content[:,self.db_column_names.index('zPoss')] except Exception: z_position = np.ones((experiment_length,1)) columns = [] for i in self.column_names: columns.append(i) self.num_columns = len(columns) con = sqlite3.connect(self.database_path + '/mologram_data.db') cur = con.cursor() cur.execute('DROP TABLE mologram_data') con.commit() con.close() con = sqlite3.connect(self.database_path + '/mologram_data.db') cur = con.cursor() cur.execute("CREATE TABLE mologram_data(" + ','.join(columns) + ")") con.commit() con.close() #self.database.create_sql_database(self.column_names,self.experiment_folder_path + '/database') # Go through all available images and re-process them for index in range(experiment_length): # Send signal to update progress bar in main thread self.emit(QtCore.SIGNAL("update_progress_bar(int,int)"),index,experiment_length) # keys in SQL database start at 1 key = index + 1 # If abort signal was received, stop processing if self.abort_processing: break # Get next image path image_path,image_name = self.image_path(key,exposures) # If image exists, read data and process it if os.path.isfile(image_path): im = Image.open(self.experiment_folder_path + '/images/' + image_name) im = np.array(im.getdata()).reshape(im.size[0], im.size[1]) im = im.astype('uint16') entry = mologram_processing.calc_datapoint_entry(key,time[index],z_position[index,0],exposures[index],im,1) elif os.path.isfile(image_path + '.tiff'): im = Image.open(self.experiment_folder_path + '/images/' + image_name + '.tiff') im = np.array(im.getdata()).reshape(im.size[1], im.size[0]) im = im.astype('uint16') entry = mologram_processing.calc_datapoint_entry(key,time[index],z_position[index,0],exposures[index],im,1) else: print('Unable to find image file: ' + self.experiment_folder_path + '/images/' + image_name) mologram_intensity = -1 con = sqlite3.connect(self.database_path + '/mologram_data.db') cur = con.cursor() cur.execute('INSERT INTO mologram_data VALUES (' + entry + ')') con.commit() con.close() #print(image_name + ': ' + str(mologram_intensity)) # Send signal to update progress bar in main thread self.emit(QtCore.SIGNAL("update_progress_bar(int,int)"),key,experiment_length) # fetch all rows from database # if not new_column_name in self.db_column_names(): # executed = False # while executed == False: # try: # cur.execute('ALTER TABLE mologram_data ADD COLUMN ' + new_column_name + ' real') # con.commit() # executed = True # except Exception as inst: # print(inst) # for i in range(len(new_column)): # key = i+1 # val = new_column[key] # cmd = "UPDATE mologram_data SET " + new_column_name + "=" + str(val) + " where key=" + str(key) # print(cmd) # cur.execute(cmd) # con.commit() # Close connection with SQL databse # con.close() # let GUI know that movement is finished self.emit(QtCore.SIGNAL("done_processing"))
def updateImage(self,mask = False,Stack = False): # acquire image from camera, this should not be the case, you get images multiple times # like this if Stack: # get the last image that was saved in the folder images print("Stack") # experiment_folder_path = self.main_widget.get_experiment_folder_path() # image_path = experiment_folder_path + '/images/' # file_names = [] # index = [] # for file in os.listdir(image_path): # index.append(int(file.split('_')[-1])) # file_names.append(file) # print(file_names) # print(np.argmax(np.asarray(index))) # max_index = np.argmax(np.asarray(index)) # im = Image.open(experiment_folder_path + '/images/' + file_names[max_index]) # im = np.array(im.getdata()).reshape(im.size[0], im.size[1]) # self.img = im.astype('uint16') # retval = 1 else: if mask == False: self.img,retval = self.main_widget.setup.camera.get_raw_img() else: img,retval = self.main_widget.setup.camera.get_raw_img() self.img = processing.processed_image(img) self.img *= 1024 retval = None # if there was an error, display a text box if retval == -1: msg = QtGui.QMessageBox() msg.setWindowTitle("Camera Error") msg.setIcon(QtGui.QMessageBox.Critical) msg.setStandardButtons(QtGui.QMessageBox.Ok) msg.setText("Cannot fetch image from camera") msg.setInformativeText("You must restart the program to fix the issue, your current position will be saved") msg.exec_() else: self.settings.set_Magnification(int(str(self.microscopy_mag.currentText()))) self.im.set_data(self.img) self.scale_bar() #self.molo_circle() molo_intensity,Airy_disk_radius, bkg_mean,bkg_std = mologram_processing.calc_IntAiryDisk(self.img) self.molo_intensity.setText(str(molo_intensity)) self.bg_int.setText(str(bkg_mean) + ',' + str(bkg_std)) self.sum_int.setText(str(np.sum(self.img)/(2560*1920))) self.max_pixel.setText(str(np.max(self.img))) print(self.settings.magnification) # draw a circle, where you detect the mologram # update the image self.canvas.draw()
def run(self): # get cursor and connection to new SQL database experiment_start_time = time.time() # depending on stack type change image aquisition method exposure = self.setup.camera.exposure if self.stack_type == self.stack_type_list[0]: # time stack for i in range(self.num_captures): # If stop button was pressed, exit loop if self.stop_thread: break # If pause button was pressed, wait until it is pressed again while self.pause_thread: continue z_position = self.setup.motors.z.pos('um') time.sleep(0.2) # Record start time start_time = time.time() # Determine file name for image #self.cameraControl.update_exposure_slider(exposure*0.9) image_name = self.setup.chip.file_name() image_name += '_' + 'exposure-' + str(exposure) + '-ms' image_name += '_' + str(i+1) # Try a few times to capture/save print('Capturing ( ' + image_name + ' ): ' + str(i+1)) num_attempts = 10 for j in range(num_attempts): img,retval = self.setup.camera.get_raw_img() curr_time = start_time - experiment_start_time if retval: if self.crop_image: self.setup.camera.crop_and_save_img(self.image_path+'/'+image_name, img) break else: self.setup.camera.save_img(self.image_path+'/'+image_name, img) break entry = mologram_processing.calc_datapoint_entry(i,curr_time,z_position,exposure,img,retval) if retval: if self.adjust_exposure: if np.amax(img) > 3000: print("Adjusted Exposure") exposure = exposure*0.9 self.setup.camera.set_exposure(exposure) db.add_entry(entry,self.database_path) if self.update_plots: print("lauched Signal to update GUI") self.emit(QtCore.SIGNAL("database_updated")) # Wait until delay is passed while time.time() - start_time < self.time_interval: continue elif self.stack_type == self.stack_type_list[1]: z_start = self.setup.motors.z.pos('um') time.sleep(0.2) for i in range(self.num_captures): exposure = self.setup.camera.exposure z_position = self.setup.motors.z.pos('um') time.sleep(0.2) image_name = self.setup.chip.file_name() image_name += '_' + 'exposure-' + str(exposure) + '-ms' image_name += 'z_' + str(z_position-z_start) image_name += '_' + str(i+1) for k in range(self.num_avg): # If stop button was pressed, exit loop if self.stop_thread: break # If pause button was pressed, wait until it is pressed again while self.pause_thread: continue # Record start time start_time = time.time() # Try a few times to capture/save print('Capturing ( ' + image_name + ' ): ' + str(i+1)) num_attempts = 10 for j in range(num_attempts): img,retval = self.setup.camera.get_raw_img() curr_time = start_time - experiment_start_time if retval: if k == 0: avg_image = img else: avg_image += img if k == (self.num_avg-1): avg_image = avg_image/self.num_avg if self.crop_image: self.setup.camera.crop_and_save_img(self.image_path+'/'+image_name, avg_image) break else: self.setup.camera.save_img(self.image_path+'/'+image_name, avg_image) break # Store data in SQL database #update Database entry = mologram_processing.calc_datapoint_entry(i,curr_time,z_position,exposure,img,retval) db.add_entry(entry,self.database_path) if self.update_plots: self.emit(QtCore.SIGNAL("database_updated")) # Wait until delay is passed while time.time() - start_time < self.time_interval: continue self.main_widget.axisControl.z.start_moving(self.stack_incr,'um',relative = True) time.sleep(2) elif self.stack_type == self.stack_type_list[2]: time.sleep(0.2) for i in range(self.num_captures): exposure = self.setup.camera.exposure time.sleep(0.2) image_name = self.setup.chip.file_name() image_name += '_' + 'exposure-' + str(exposure) + '-ms' image_name += '_' + str(i+1) for k in range(self.num_avg): # If stop button was pressed, exit loop if self.stop_thread: break # If pause button was pressed, wait until it is pressed again while self.pause_thread: continue # Record start time start_time = time.time() # Try a few times to capture/save print('Capturing ( ' + image_name + ' ): ' + str(i+1)) num_attempts = 10 for j in range(num_attempts): img,retval = self.setup.camera.get_raw_img() curr_time = start_time - experiment_start_time if retval: if k == 0: avg_image = img else: avg_image += img if k == (self.num_avg-1): avg_image = avg_image/self.num_avg if self.crop_image: self.setup.camera.crop_and_save_img(self.image_path+'/'+image_name, avg_image) break else: self.setup.camera.save_img(self.image_path+'/'+image_name, avg_image) break # Store data in SQL database # update database entry = mologram_processing.calc_datapoint_entry(i,curr_time,z_position,exposure,img,retval) db.add_entry(entry,self.database_path) if self.update_plots: self.emit(QtCore.SIGNAL("database_updated")) while time.time() - start_time < self.time_interval: continue self.main_widget.axisControl.TE.start_moving(self.stack_incr,'deg',relative = True) time.sleep(2) self.stop_thread = False self.emit(QtCore.SIGNAL("done_experiment"))