def update_fitting_plot(self): self.parent.ui.fitting.clear() if self.parent.fitting_peak_ui: self.parent.ui.fitting.removeItem(self.parent.fitting_peak_ui) o_get = Get(parent=self.parent) xaxis_checked = o_get.x_axis_checked() xaxis = o_get.x_axis_data(x_axis_selected=xaxis_checked) xaxis_label = o_get.x_axis_label(x_axis_selected=xaxis_checked) o_table = TableHandler( table_ui=self.parent.ui.march_dollase_result_table) list_row_selected = o_table.get_rows_of_table_selected() if list_row_selected is None: return for row_selected in list_row_selected: yaxis = o_get.y_axis_data_of_selected_row( row_selected=row_selected) self.parent.ui.fitting.plot(xaxis, yaxis, pen=(self.parent.selection_roi_rgb[0], self.parent.selection_roi_rgb[1], self.parent.selection_roi_rgb[2]), symbol='o') self.parent.ui.fitting.setLabel("bottom", xaxis_label) self.parent.ui.fitting.setLabel("left", "Average transmission") if self.parent.ui.march_dollase_toolBox.currentIndex() == 0: move_bragg_peak_range = True else: move_bragg_peak_range = False peak_range = self.parent.march_dollase_fitting_range_selected local_peak_range = [xaxis[peak_range[0]], xaxis[peak_range[1]]] if self.parent.march_dollase_fitting_peak_ui: self.parent.ui.fitting.removeItem( self.parent.march_dollase_fitting_peak_ui) self.parent.march_dollase_fitting_peak_ui = pg.LinearRegionItem( values=local_peak_range, orientation=None, brush=None, movable=move_bragg_peak_range, bounds=None) self.parent.march_dollase_fitting_peak_ui.sigRegionChanged.connect( self.parent.march_dollase_fitting_range_changed) self.parent.march_dollase_fitting_peak_ui.setZValue(-10) self.parent.ui.fitting.addItem( self.parent.march_dollase_fitting_peak_ui)
def export_data_of_selected_rows(self): base_folder = Path(self.parent.working_dir) directory = str(base_folder.parent) _export_folder = QFileDialog.getExistingDirectory( self.parent, directory=directory, caption="Select Output Folder") if _export_folder: o_table = TableHandler( table_ui=self.parent.ui.march_dollase_result_table) list_of_rows_selected = o_table.get_rows_of_table_selected() str_list_of_rows_selected = [ str(_row) for _row in list_of_rows_selected ] str_rows = "_".join(str_list_of_rows_selected) output_file_name = os.path.join( str(_export_folder), "march_dollase_result_fitting_row{}.txt".format(str_rows)) fitting_input_dictionary = self.parent.fitting_input_dictionary o_get = Get(parent=self.parent) xaxis_index = o_get.x_axis_data(x_axis_selected='index') xaxis_tof = o_get.x_axis_data(x_axis_selected='tof') xaxis_lambda = o_get.x_axis_data(x_axis_selected='lambda') # metadata metadata = ["#Marche Dollase Result of Fitting"] is_advance_mode = self.parent.ui.march_dollase_advanced_mode_checkBox.isChecked( ) metadata.append( "#Using advanced fitting mode: {}".format(is_advance_mode)) data_label = "#image index, TOF(micros), Lambda(Angstroms)" temp_data = [] for _row in list_of_rows_selected: _entry = fitting_input_dictionary['rois'][_row]['fitting'][ 'march_dollase'] _top_entry = fitting_input_dictionary['rois'][_row] _line = "#-> row {}: x0: {}, y0:{}, width:{}, height:{}, " \ "d_spacing:{}, sigma:{}, alpha:{}, a1:{}, " \ "a2:{}".format(_row, _top_entry['x0'], _top_entry['y0'], _top_entry['width'], _top_entry['height'], _entry['d_spacing'], _entry['sigma'], _entry['alpha'], _entry['a1'], _entry['a2']) if is_advance_mode: _line += ", a5:{}, a6:{}".format(_entry['a5'], _entry['a6']) metadata.append(_line) data_label += ", row {}".format(_row) temp_data.append( o_get.y_axis_data_of_selected_row(row_selected=_row)) metadata.append("#") metadata.append(data_label) # data data = [] self.parent.debug_data = temp_data for _index in np.arange(len(xaxis_index)): str_data = "{}, {}, {}".format(xaxis_index[_index], xaxis_tof[_index], xaxis_lambda[_index]) for _col_index in np.arange(len(list_of_rows_selected)): str_data += ", {}".format(temp_data[_col_index][_index]) data.append(str_data) make_ascii_file(metadata=metadata, data=data, output_file_name=output_file_name, dim='1d') message = "{} has been created!".format(output_file_name) self.parent.ui.statusbar.showMessage(message, 10000) # 10s
def prepare(self): self.initialize_fitting_input_dictionary() fitting_input_dictionary = self.parent.fitting_input_dictionary march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table _is_advanced_mode = self.is_advanced_mode() if _is_advanced_mode: gmodel = Model(march_dollase_advanced_fit, missing='drop') else: # gmodel = Model(march_dollase_basic_fit, missing='drop') gmodel = Model(march_dollase_basic_fit, nan_policy='propagate') march_dollase_fitting_history_table = self.parent.march_dollase_fitting_history_table nbr_row_in_fitting_scenario = len(march_dollase_fitting_history_table) self.parent.ui.eventProgress.setValue(0) nbr_roi_row = len(fitting_input_dictionary['rois'].keys()) self.parent.ui.eventProgress.setMaximum(nbr_roi_row) self.parent.ui.eventProgress.setVisible(True) def set_params(params_object, name_of_parameter, dict_entry, parameter_flag): params_object.add(name_of_parameter, value=np.float(dict_entry[name_of_parameter]), vary=parameter_flag) def record_result_into_dict(entry_dict, result_object, name_of_parameter, parameter_flag): if parameter_flag: print(f"-> name_of_parameter: {name_of_parameter}") [value, error] = result_object.get_value_err(tag=name_of_parameter) entry_dict[name_of_parameter] = value entry_dict[name_of_parameter + "_error"] = error print(f" - value: {value}") print(f" - error: {error}") for _roi_row in np.arange(nbr_roi_row): _entry = fitting_input_dictionary['rois'][_roi_row]['fitting'][ 'march_dollase'] o_get = Get(parent=self.parent) xaxis = o_get.x_axis_data(x_axis_selected='lambda') yaxis = o_get.y_axis_data_of_selected_row(_roi_row) for _history_row, _row_entry in enumerate( march_dollase_fitting_history_table): [ d_spacing_flag, sigma_flag, alpha_flag, a1_flag, a2_flag, a5_flag, a6_flag ] = _row_entry if _is_advanced_mode: a5_flag = _entry['a5'] a6_flag = _entry['a6'] params = gmodel.make_params() set_params(params, 'd_spacing', _entry, d_spacing_flag) set_params(params, 'sigma', _entry, sigma_flag) set_params(params, 'alpha', _entry, alpha_flag) set_params(params, 'a1', _entry, a1_flag) set_params(params, 'a2', _entry, a2_flag) if _is_advanced_mode: set_params(params, 'a5', _entry, a5_flag) set_params(params, 'a6', _entry, a6_flag) print("Parameters pre-initialized:") if not d_spacing_flag: print(f"- d_spacing: {_entry['d_spacing']}") if not sigma_flag: print(f"- sigma: {_entry['sigma']}") if not alpha_flag: print(f"- alpha: {_entry['alpha']}") if not a1_flag: print(f"- a1: {_entry['a1']}") if not a2_flag: print(f"- a2: {_entry['a2']}") # try: result = gmodel.fit(yaxis, params, t=xaxis) # except ValueError: # print(f"we are having an error row:{_roi_row}") print(f"in _history_row: {_history_row}") o_result = ResultValueError(result=result) record_result_into_dict(_entry, o_result, 'd_spacing', d_spacing_flag) record_result_into_dict(_entry, o_result, 'sigma', sigma_flag) record_result_into_dict(_entry, o_result, 'alpha', alpha_flag) record_result_into_dict(_entry, o_result, 'a1', a1_flag) record_result_into_dict(_entry, o_result, 'a2', a2_flag) if _is_advanced_mode: record_result_into_dict(_entry, o_result, 'a5', a5_flag) record_result_into_dict(_entry, o_result, 'a6', a6_flag) break # for debugging only (stop only after first row) self.parent.ui.eventProgress.setValue(_roi_row + 1) QApplication.processEvents() self.parent.ui.eventProgress.setVisible(False) self.parent.fitting_procedure_started['march-dollase'] = True