def fit_multi_peaks(self): QApplication.setOverrideCursor(Qt.WaitCursor) _peak_range_list = [ tuple(_range) for _range in self.parent._ui_graphicsView_fitSetup.list_peak_ranges ] _peak_center_list = [ np.mean([left, right]) for (left, right) in _peak_range_list ] _peak_tag_list = [ "peak{}".format(_index) for _index, _ in enumerate(_peak_center_list) ] _peak_function_name = str( self.parent.ui.comboBox_peakType.currentText()) _peak_xmin_list = [left for (left, _) in _peak_range_list] _peak_xmax_list = [right for (_, right) in _peak_range_list] # Fit peak hd_ws = self.parent.hidra_workspace _wavelength = hd_ws.get_wavelength(True, True) fit_engine = PeakFitEngineFactory.getInstance(hd_ws, _peak_function_name, 'Linear', wavelength=_wavelength) fit_result = fit_engine.fit_multiple_peaks(_peak_tag_list, _peak_xmin_list, _peak_xmax_list) self.parent.fit_result = fit_result self.parent.populate_fit_result_table(fit_result=fit_result) # self.parent.update_list_of_2d_plots_axis() o_gui = GuiUtilities(parent=self.parent) o_gui.set_1D_2D_axis_comboboxes(with_clear=True, fill_raw=True, fill_fit=True) o_gui.initialize_combobox() o_gui.enabled_export_csv_widgets(enabled=True) o_gui.enabled_2dplot_widgets(enabled=True) o_plot = Plot(parent=self.parent) o_plot.plot_2d() QApplication.restoreOverrideCursor()
def load(self, project_file=None): if project_file is None: return try: self.__set_up_project_name(project_file=project_file) ws = self.parent._core.load_hidra_project( project_file, project_name=self.parent._project_name, load_detector_counts=False, load_diffraction=True) # Record data key and next self.parent._curr_file_name = project_file self.parent.hidra_workspace = ws self.parent.create_plot_color_range() except (RuntimeError, TypeError) as run_err: pop_message(self, 'Unable to load {}'.format(project_file), detailed_message=str(run_err), message_type='error') # # Edit information on the UI for user to visualize # self.parent.ui.label_loadedFileInfo.setText('Loaded {}; Project name: {}' # .format(project_file, self.parent._project_name)) # Get and set the range of sub runs o_utility = Utilities(parent=self.parent) sub_run_list = o_utility.get_subruns_limit() o_gui = GuiUtilities(parent=self.parent) o_gui.initialize_fitting_slider(max=len(sub_run_list)) o_gui.set_1D_2D_axis_comboboxes(with_clear=True, fill_raw=True) o_gui.enabled_1dplot_widgets(enabled=True) o_gui.initialize_combobox()
def fit_peaks(self, all_sub_runs=False): """ Fit peaks either all peaks or selected peaks The workflow includes 1. parse sub runs, peak and background type 2. fit peaks 3. show the fitting result in table 4. plot the peak in first sub runs that is fit :param all_sub_runs: Flag to fit peaks of all sub runs without checking :return: """ # Get the sub runs to fit or all the peaks # if not all_sub_runs: # # Parse sub runs specified in lineEdit_scanNumbers # o_utilities = Utilities(parent=self.parent) # sub_run_list = o_utilities.parse_sub_runs() # else: sub_run_list = None # Get peak function and background function peak_function = str(self.parent.ui.comboBox_peakType.currentText()) bkgd_function = str( self.parent.ui.comboBox_backgroundType.currentText()) # Get peak fitting range from the range of figure fit_range = self.parent._ui_graphicsView_fitSetup.get_x_limit() print('[INFO] Peak fit range: {0}'.format(fit_range)) # Fit Peaks: It is better to fit all the peaks at the same time after testing guessed_peak_center = 0.5 * (fit_range[0] + fit_range[1]) peak_info_dict = { 'Peak 1': { 'Center': guessed_peak_center, 'Range': fit_range } } self.parent._core.fit_peaks(project_name=self.parent._project_name, sub_run_list=sub_run_list, peak_type=peak_function, background_type=bkgd_function, peaks_fitting_setup=peak_info_dict) # Process fitted peaks # TEST - #84 - This shall be reviewed! try: # FIXME - effective_parameter=True will fail! # FIXME - other than return_format=dict will fail! # FIXME - need to give a real value to default_tag # FIXME - this only works if fitting 1 peak a time default_tag = peak_info_dict.keys()[0] function_params, fit_values = self.parent._core.get_peak_fitting_result( self.parent._project_name, default_tag, return_format=dict, effective_parameter=False, fitting_function=peak_function) except AttributeError as err: pop_message(self, 'Zoom in/out to only show peak to fit!', str(err), "error") return # TODO - #84+ - Need to implement the option as effective_parameter=True print('[DB...BAT...FITWINDOW....FIT] returned = {}, {}'.format( function_params, fit_values)) self.parent._sample_log_names_mutex = True for param_name in function_params: self.parent._function_param_name_set.add(param_name) # # log index and center of mass # size_x = len(self.parent._sample_log_names) + len(self.parent._function_param_name_set) + 2 # # # center of mass # size_y = len(self.parent._sample_log_names) + len(self.parent._function_param_name_set) + 1 # release the mutex: because re-plot is required anyway self.parent._sample_log_names_mutex = False # Show fitting result in Table # TODO - could add an option to show native or effective peak parameters try: self.show_fit_result_table(peak_function, function_params, fit_values, is_effective=False) except IndexError: return # plot the model and difference if sub_run_list is None: o_plot = Plot(parent=self.parent) o_plot.plot_diff_and_fitted_data(1, True) o_gui = GuiUtilities(parent=self.parent) o_gui.set_1D_2D_axis_comboboxes(fill_fit=True) o_gui.enabled_export_csv_widgets(True)