def saveFileNameDialog(self): """[summary] Returns ------- [type] [description] """ try: fmt_choices = { "All Files(*)": ".png", #default "png (*.png)": ".png", "pdf (*.pdf)": ".pdf", "ascii (*.txt)": ".txt" } choices_str = ";;".join([] + [k for k in fmt_choices.keys()]) options = qtw.QFileDialog.Options() options |= qtw.QFileDialog.DontUseNativeDialog fileName, fmtChoice = qtw.QFileDialog.getSaveFileName( self, "Save File", "", choices_str, options=options) if fileName: return fileName except Exception as e: handle_exception(e) return None
def update_from_selection_list(self): """[summary] """ selectedListEntries = self.fileList.selectedItems() if len(selectedListEntries) < 1: self.fileList.setCurrentRow(self.fileList.count() - 1) try: if self.subtractCheckBox.isChecked(): Imap = self.subtract_intensities_from_selected_files() else: Imap = self.sum_intensities_from_selected_files() self.compute_Q() self.graphView.update_graph(Y=self.experiment.qymatrix, X=self.experiment.qzmatrix, Z=Imap, Xc=self.experiment.qzc_corr, Xs=self.experiment.qzc_spec, Yc=self.experiment.qyc, zmin=self.experiment.min_intensity, zmax=self.experiment.max_intensity, reset_limits_required=False, x1=self.experiment.x0, y1=self.experiment.y0, x2=self.experiment.xf, y2=self.experiment.yf, title=self.build_ascii_header()) except Exception as e: handle_exception(e)
def safe_parse_numpy(self, parse_func, file_path, dtype='i', delimiter=' '): """[summary] Parameters ---------- parse_func : [type] [description] file_path : [type] [description] dtype : str, optional [description], by default 'i' delimiter : str, optional [description], by default ' ' Returns ------- [type] [description] """ try: nparray = np.loadtxt(file_path, dtype=dtype, delimiter=delimiter) print(f"{file_path}:") print(f"Loaded array with shape: {nparray.shape}") tf = parse_func(nparray) print(f" Parsed {file_path}") return tf except Exception as e: handle_exception(e) return False
def on_click_loglinear(self): """[summary] """ try: self.graphView.update_graph( log_scale=not self.graphView.data.log_scale, reset_limits_required=False) except Exception as e: handle_exception(e) return
def on_file_double_clicked(self): """[summary] """ try: if len(self.dirtree.selectedIndexes()) < 1: return datFilePath = self.dirtree.model().filePath( self.dirtree.currentIndex()) if is_file(datFilePath): self.doStuff(datFilePath) except Exception as e: handle_exception(e)
def on_click_open_file(self): try: if __DEBUG__: datFilePath = os.path.join(".", "notToVersion", "Archive", "p15496_00000992.dat") else: datFilePath = self.openFileNameDialog() if is_file(datFilePath): self.doStuff(datFilePath) except Exception as e: handle_exception(e)
def on_click_save_ascii(self): """[summary] """ try: filepath = self.saveFileNameDialog() if filepath is None: return noextpath, ext = os.path.splitext(filepath) if ext == '': ext = ".txt" x1, x2 = self.graphView.data.x1, self.graphView.data.x2 y1, y2 = self.graphView.data.y1, self.graphView.data.y2 header = self.build_ascii_header() filename = noextpath + "_xyI_" + ext original_shape = self.graphView.data.X[y1:y2, x1:x2].shape x_to_save = self.graphView.data.X[y1:y2, x1:x2].flatten() y_to_save = self.graphView.data.Y[y1:y2, x1:x2].flatten() z_to_save = self.graphView.data.Z[y1:y2, x1:x2].flatten() columns = np.vstack((x_to_save, y_to_save, z_to_save)).T np.savetxt(filename, columns, header=header + f"\n#Original shape: {original_shape}", fmt='%.3e') filename = noextpath + "_xI_" + ext original_shape = self.graphView.data.X[0, x1:x2].shape x_to_save = self.graphView.data.X[0, x1:x2].flatten() z_to_save = self.graphView.data.Z[y1:y2, x1:x2].sum(axis=0).flatten() columns = np.vstack((x_to_save, z_to_save)).T np.savetxt(filename, columns, header=header + f"\n#Original shape: {original_shape}", fmt='%.3e') filename = noextpath + "_yI_" + ext original_shape = self.graphView.data.X[y1:y2, 0].shape y_to_save = self.graphView.data.Y[y1:y2, 0].flatten() z_to_save = self.graphView.data.Z[y1:y2, x1:x2].sum(axis=1).flatten() columns = np.vstack((y_to_save, z_to_save)).T np.savetxt(filename, columns, header=header + f"\n#Original shape: {original_shape}", fmt='%.3e') print(f"Arrays saved:\n {filename}\n") except Exception as e: handle_exception(e)
def doStuff(self, datFilePath): """[summary] Parameters ---------- datFilePath : [type] [description] """ try: self.thread.datFilePath = datFilePath self.thread.start() except Exception as e: handle_exception(e) return return
def update_graph(self, **kwargs): try: self.canvas.figure.clear() self.define_axes() self.update_data(**kwargs) self.build_norm(**kwargs) self.update_axes(**kwargs) self.update_area_selector(**kwargs) self.canvas.figure.suptitle("", fontsize=5) self.canvas.draw() self.save(**kwargs) self.finishedUpdating.emit() except Exception as e: handle_exception(e) return
def update_from_info_table(self): """[summary] Returns ------- [type] [description] """ try: self.update_multi_experiment_values() self.color_outdated() self.update_from_selection_list() except Exception as e: handle_exception(e) return False return True
def on_graph_updated(self): """[summary] """ try: selectedListEntries = self.fileList.selectedItems() for currentListItem in selectedListEntries: currentListEntry = currentListItem.text() self.experiment = self.experiment_dict[currentListEntry] self.experiment.min_intensity = self.graphView.data.zmin self.experiment.max_intensity = self.graphView.data.zmax self.experiment.x0 = self.graphView.data.x1 self.experiment.y0 = self.graphView.data.y1 self.experiment.xf = self.graphView.data.x2 self.experiment.yf = self.graphView.data.y2 self.update_table() except Exception as e: handle_exception(e) return
def color_outdated(self): """[summary] Returns ------- [type] [description] """ expdict = self.experiment.__dict__ try: for i in range(self.infoTable.rowCount()): key = self.infoTable.verticalHeaderItem(i).text() current_item = self.infoTable.item(i, 0) try: if key in [ 'qyc', 'qzc', 'x0', 'y0', 'xf', 'yf', 'sample_detector_distance_mm' ]: value = int(current_item.text()) elif key in [ "min_intensity", "max_intensity", "pixel_size_mm" ]: value = float(current_item.text()) elif key in ['instrument_name']: value = str(current_item.text()) else: continue self.color_validate(current_item, value, expdict[key]) except Exception as e: red = QColor(255, 000, 0, 127) current_item.setBackground(red) current_item.setSelected(False) return False except Exception as e: handle_exception(e) return False return True
def openFileNameDialog(self): """[summary] Returns ------- [type] [description] """ try: options = qtw.QFileDialog.Options() options |= qtw.QFileDialog.DontUseNativeDialog fileName, _ = qtw.QFileDialog.getOpenFileName( self, "Open File", "", "Measurement dat file (*.dat);;All Files (*)", options=options) # self.openFileNamesDialog() if fileName: return fileName except Exception as e: handle_exception(e) return None
def safe_parse(self, parse_func, file_path): """[summary] Parameters ---------- parse_func : [type] [description] file_path : [type] [description] Returns ------- [type] [description] """ try: with open(file_path, 'r') as fp: tf = parse_func(fp) print(f" Parsed {file_path}") return tf except Exception as e: handle_exception(e) return False
def update_gui(self): """[summary] Returns ------- [type] [description] Raises ------ Exception [description] """ try: did_stuff, why_not = self.thread.retval if not did_stuff: raise Exception(f"Did not complete data processing: {why_not}") for gzfn in self.settings.gzFileNames: self.fileList.addItem(gzfn) sum1 = self.experiment_dict[gzfn].Imatrix.sum() sum2 = self.experiment_dict[gzfn].cut_Iy.sum() sum3 = self.experiment_dict[gzfn].cut_Iz.sum() print( "If the following three sums are not equal, there's an error:" ) print(sum1, sum2, sum3) self.update_table() self.update_from_info_table() self.progress_bar.setValue(100) except Exception as e: handle_exception(e) return False finally: self.progress_bar.setValue(0) return True
def on_click_save_png(self): """[summary] Returns ------- [type] [description] """ try: fmt_choices = { "All Files(*)": ".png", #default "png (*.png)": ".png", "pdf (*.pdf)": ".pdf", "ascii (*.txt)": ".txt" } choices_str = ";;".join([] + [k for k in fmt_choices.keys()]) options = qtw.QFileDialog.Options() options |= qtw.QFileDialog.DontUseNativeDialog filePath, fmtChoice = qtw.QFileDialog.getSaveFileName( self, "Save File", "", choices_str, options=options) if not filePath: return None extension = os.path.splitext(filePath)[-1] if extension not in fmt_choices.values(): extension = fmt_choices[fmtChoice] filePath += extension self.graphView.update_graph(save_to_file=filePath, header=self.build_ascii_header(), title=self.build_ascii_header()) except Exception as e: handle_exception(e) return #on_click_save_png
def on_mouse_click(self, event): if event.dblclick: try: self.show_figures() except Exception as e: handle_exception(e)