def update_dict_profile_to_fit(self): [left_range, right_range] = self.bragg_edge_range o_selection = BraggEdgeSelectionTab(parent=self) [x0, y0, x1, y1] = o_selection.get_shrinking_roi_dimension() o_get = Get(parent=self) profile = o_get.profile_of_roi(x0=x0, y0=y0, x1=x1, y1=y1) yaxis = profile[left_range: right_range] all_x_axis = o_get.all_x_axis() index_array = all_x_axis['index'][0] tof_array = all_x_axis['tof'][0] lambda_array = all_x_axis['lambda'][0] index_selected = index_array[left_range: right_range] tof_selected = tof_array[left_range: right_range] lambda_selected = lambda_array[left_range: right_range] profile_to_fit = {'yaxis': yaxis, 'xaxis': {'index': index_selected, 'tof': tof_selected, 'lambda': lambda_selected}, } self.dict_profile_to_fit = profile_to_fit
def configuration(self): # bring file dialog to locate where the file will be saved base_folder = Path(self.parent.working_dir) directory = str(base_folder.parent) _export_folder = QFileDialog.getExistingDirectory( self.parent, directory=directory, caption="Select Output Folder", options=QFileDialog.ShowDirsOnly) if _export_folder: data, metadata = self.get_data_metadata_from_selection_tab() # collect initial selection size (x0, y0, width, height) o_get = Get(parent=self.parent) [x0, y0, x1, y1, width, height] = o_get.selection_roi_dimension() name_of_ascii_file = ExportHandler.makeup_name_of_profile_ascii_file( base_name=str(base_folder.name), export_folder=_export_folder, x0=x0, y0=y0, width=width, height=height) make_ascii_file(metadata=metadata, data=data, output_file_name=name_of_ascii_file, dim='1d') self.parent.ui.statusbar.showMessage( "{} has been created!".format(name_of_ascii_file), 10000) # 10s self.parent.ui.statusbar.setStyleSheet("color: green")
def update_roi_labels(self): [global_left_range, global_right_range] = self.parent.bragg_edge_range [left_range, right_range] = list(self.parent.fitting_peak_ui.getRegion()) o_get = Get(parent=self.parent) x_axis_selected = o_get.x_axis_checked() xaxis_dict = self.parent.fitting_input_dictionary['xaxis'] xaxis_index, _ = xaxis_dict[x_axis_selected] [left_xaxis_index, right_xaxis_index] = [global_left_range, global_right_range] xaxis = xaxis_index[left_xaxis_index: right_xaxis_index] left_index = find_nearest_index(array=xaxis, value=left_range) right_index = find_nearest_index(array=xaxis, value=right_range) xaxis_in_selected_axis = self.parent.fitting_input_dictionary['xaxis'][x_axis_selected][0][ global_left_range: global_right_range] real_left_value = xaxis_in_selected_axis[left_index] real_right_value = xaxis_in_selected_axis[right_index] if x_axis_selected == 'lambda': str_format = "{:02f}" elif x_axis_selected == 'tof': str_format = "{:04.2f}" else: str_format = "{}" real_left_value = str_format.format(real_left_value) real_right_value = str_format.format(real_right_value) units = Get.units(name=x_axis_selected) self.parent.ui.bragg_peak_range_from_value.setText(str(real_left_value)) self.parent.ui.bragg_peak_range_to_value.setText(str(real_right_value)) self.parent.ui.from_bragg_peak_range_units.setText(units) self.parent.ui.to_bragg_peak_range_units.setText(units)
def update_selection_plot(self): self.parent.ui.profile.clear() o_get = Get(parent=self.parent) x_axis, x_axis_label = o_get.x_axis() max_value = self.parent.ui.profile_of_bin_size_slider.maximum() roi_selected = max_value - self.parent.ui.profile_of_bin_size_slider.value( ) y_axis = self.parent.fitting_input_dictionary['rois'][roi_selected][ 'profile'] self.parent.ui.profile.plot(x_axis, y_axis, pen=(self.parent.shrinking_roi_rgb[0], self.parent.shrinking_roi_rgb[1], self.parent.shrinking_roi_rgb[2])) self.parent.ui.profile.setLabel("bottom", x_axis_label) self.parent.ui.profile.setLabel("left", 'Mean transmission') # full region y_axis = self.parent.fitting_input_dictionary['rois'][0]['profile'] self.parent.ui.profile.plot(x_axis, y_axis, pen=(self.parent.selection_roi_rgb[0], self.parent.selection_roi_rgb[1], self.parent.selection_roi_rgb[2]))
def bragg_edge_range_changed(self): [left_range, right_range] = list(self.bragg_edge_range_ui.getRegion()) o_get = Get(parent=self) x_axis, _ = o_get.x_axis() left_index = find_nearest_index(array=x_axis, value=left_range) right_index = find_nearest_index(array=x_axis, value=right_range) self.bragg_edge_range = [left_index, right_index]
def add_profile_to_dict_of_all_regions(self, dict_regions=None): for _key in dict_regions.keys(): current_region = dict_regions[_key] x0 = current_region['x0'] y0 = current_region['y0'] width = current_region['width'] height = current_region['height'] o_get = Get(parent=self) profile = o_get.profile_of_roi(x0=x0, y0=y0, x1=x0 + width, y1=y0 + height) current_region['profile'] = profile
def update_vertical_line_in_profile_plot(self): o_get = Get(parent=self) x_axis, x_axis_label = o_get.x_axis() bragg_edge_range = [x_axis[self.bragg_edge_range[0]], x_axis[self.bragg_edge_range[1]]] if self.bragg_edge_range_ui: self.ui.profile.removeItem(self.bragg_edge_range_ui) self.bragg_edge_range_ui = pg.LinearRegionItem(values=bragg_edge_range, orientation=None, brush=None, movable=True, bounds=None) self.bragg_edge_range_ui.sigRegionChanged.connect(self.bragg_edge_range_changed) self.bragg_edge_range_ui.setZValue(-10) self.ui.profile.addItem(self.bragg_edge_range_ui)
def get_data_metadata_from_selection_tab(self): base_folder = Path(self.parent.working_dir) o_get = Get(parent=self.parent) index_axis, _ = o_get.specified_x_axis(xaxis='index') tof_axis, _ = o_get.specified_x_axis(xaxis='tof') lambda_axis, _ = o_get.specified_x_axis('lambda') fitting_peak_range = self.parent.bragg_edge_range distance_detector_sample = str( self.parent.ui.distance_detector_sample.text()) detector_offset = str(self.parent.ui.detector_offset.text()) kropff_fitting_values = self.collect_all_kropff_fitting_values() march_dollase_fitting_values = self.collect_all_march_dollase_fitting_values( ) dict_regions = o_get.all_russian_doll_region_full_infos() metadata = ExportHandler.make_metadata( base_folder=base_folder, fitting_peak_range=fitting_peak_range, dict_regions=dict_regions, distance_detector_sample=distance_detector_sample, detector_offset=detector_offset, kropff_fitting_values=kropff_fitting_values, march_dollase_fitting_values=march_dollase_fitting_values) self.add_fitting_infos_to_metadata(metadata) metadata.append("#") metadata.append( "#File Index, TOF(micros), lambda(Angstroms), ROIs (see above)") data = ExportHandler.format_data(col1=index_axis, col2=tof_axis, col3=lambda_axis, dict_regions=dict_regions) return data, metadata
def fit_that_selection_pushed_by_program(self, initialize_region=True): o_get = Get(parent=self) x_axis = o_get.all_x_axis() dict_regions = o_get.all_russian_doll_region_full_infos() o_init = PeakFittingInitialization(parent=self) fitting_input_dictionary = o_init.fitting_input_dictionary(nbr_rois=len(dict_regions)) o_init.set_top_keys_values(fitting_input_dictionary, {'xaxis': x_axis, 'bragg_edge_range': self.bragg_edge_range}) self.append_dict_regions_to_fitting_input_dictionary(dict_regions, fitting_input_dictionary) # fitting_input_dictionary['xaxis'] = x_axis # fitting_input_dictionary['bragg_edge_range'] = self.bragg_edge_range self.fitting_input_dictionary = fitting_input_dictionary o_kropff = Kropff(parent=self) o_kropff.reset_all_table() o_march = MarchDollase(parent=self) o_march.reset_table() if initialize_region: self.initialize_default_peak_regions() else: if self.fitting_procedure_started['kropff']: o_kropff.fill_table_with_fitting_information() o_march.fill_tables_with_fitting_information() if initialize_region: o_march_fitting = MarchDollaseFittingJobHandler(parent=self) o_march_fitting.initialize_fitting_input_dictionary() self.ui.tabWidget.setTabEnabled(1, True) self.ui.actionExport.setEnabled(True) self.select_first_row_of_all_fitting_table()
def update_selection_profile_plot(self): if self.parent.is_file_imported: self.update_selection_plot() self.parent.update_vertical_line_in_profile_plot() else: o_get = Get(parent=self.parent) x_axis, x_axis_label = o_get.x_axis() self.parent.ui.profile.clear() # large selection region [x0, y0, x1, y1, _, _] = o_get.selection_roi_dimension() profile = o_get.profile_of_roi(x0, y0, x1, y1) x_axis, y_axis = check_size(x_axis=x_axis, y_axis=profile) self.parent.ui.profile.plot(x_axis, y_axis, pen=(self.parent.selection_roi_rgb[0], self.parent.selection_roi_rgb[1], self.parent.selection_roi_rgb[2])) # shrinkable region shrinking_roi = self.get_coordinates_of_new_inside_selection_box() x0 = shrinking_roi['x0'] y0 = shrinking_roi['y0'] x1 = shrinking_roi['x1'] y1 = shrinking_roi['y1'] profile = o_get.profile_of_roi(x0, y0, x1, y1) x_axis, y_axis = check_size(x_axis=x_axis, y_axis=profile) self.parent.ui.profile.plot(x_axis, y_axis, pen=(self.parent.shrinking_roi_rgb[0], self.parent.shrinking_roi_rgb[1], self.parent.shrinking_roi_rgb[2])) self.parent.ui.profile.setLabel("bottom", x_axis_label) self.parent.ui.profile.setLabel("left", 'Mean transmission') # vertical line showing peak to fit bragg_edge_range = [ x_axis[self.parent.bragg_edge_range[0]], x_axis[self.parent.bragg_edge_range[1]] ] self.parent.bragg_edge_range_ui = pg.LinearRegionItem( values=bragg_edge_range, orientation=None, brush=None, movable=True, bounds=None) self.parent.bragg_edge_range_ui.sigRegionChanged.connect( self.parent.bragg_edge_range_changed) self.parent.bragg_edge_range_ui.setZValue(-10) self.parent.ui.profile.addItem(self.parent.bragg_edge_range_ui)
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 update_fitting_plot(self): self.parent.ui.fitting.clear() o_get = Get(parent=self.parent) part_of_fitting_dict = o_get.part_of_fitting_selected() name_of_page = part_of_fitting_dict['name_of_page'] table_ui = part_of_fitting_dict['table_ui'] o_table = TableHandler(table_ui=table_ui) list_row_selected = o_table.get_rows_of_table_selected() x_axis_selected = o_get.x_axis_checked() if list_row_selected is None: # first fitting tab where we only display the full data with bragg peak selection if self.parent.fitting_peak_ui: self.parent.ui.fitting.removeItem(self.parent.fitting_peak_ui) xaxis_dict = self.parent.fitting_input_dictionary['xaxis'] xaxis_index, xaxis_label = xaxis_dict[x_axis_selected] [left_xaxis_index, right_xaxis_index] = self.parent.bragg_edge_range xaxis = xaxis_index[left_xaxis_index: right_xaxis_index] selected_roi = self.parent.fitting_input_dictionary['rois'][0] yaxis = selected_roi['profile'] yaxis = yaxis[left_xaxis_index: right_xaxis_index] yaxis = -np.log(yaxis) 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) peak_range_index = self.parent.kropff_fitting_range['bragg_peak'] if peak_range_index[0] is None: peak_range = self.parent.bragg_edge_range else: peak_range = [xaxis[peak_range_index[0]], xaxis[peak_range_index[1]]] if self.parent.fitting_peak_ui: self.parent.ui.fitting.removeItem(self.parent.fitting_peak_ui) self.parent.fitting_peak_ui = pg.LinearRegionItem(values=peak_range, orientation=None, brush=None, movable=True, bounds=None) self.parent.fitting_peak_ui.sigRegionChanged.connect(self.parent.fitting_range_changed) self.parent.fitting_peak_ui.setZValue(-10) self.parent.ui.fitting.addItem(self.parent.fitting_peak_ui) else: for row_selected in list_row_selected: selected_roi = self.parent.fitting_input_dictionary['rois'][row_selected] xaxis_dict = self.parent.fitting_input_dictionary['xaxis'] [left_xaxis_index, right_xaxis_index] = self.parent.bragg_edge_range yaxis = selected_roi['profile'] xaxis_index, xaxis_label = xaxis_dict[x_axis_selected] xaxis = xaxis_index[left_xaxis_index: right_xaxis_index] yaxis = yaxis[left_xaxis_index: right_xaxis_index] self.parent.ui.fitting.setLabel("bottom", xaxis_label) self.parent.ui.fitting.setLabel("left", 'Cross Section (arbitrary units)') yaxis = -np.log(yaxis) 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') peak_range_index = self.parent.kropff_fitting_range[name_of_page] if peak_range_index[0] is None: peak_range = self.parent.bragg_edge_range else: peak_range = [xaxis[peak_range_index[0]], xaxis[peak_range_index[1]]] if self.parent.fitting_peak_ui: self.parent.ui.fitting.removeItem(self.parent.fitting_peak_ui) self.parent.fitting_peak_ui = pg.LinearRegionItem(values=peak_range, orientation=None, brush=None, movable=False, bounds=None) self.parent.fitting_peak_ui.sigRegionChanged.connect(self.parent.fitting_range_changed) self.parent.fitting_peak_ui.setZValue(-10) self.parent.ui.fitting.addItem(self.parent.fitting_peak_ui) o_gui = GuiUtility(parent=self.parent) algo_name = o_gui.get_tab_selected(self.parent.ui.tab_algorithm).lower() if key_path_exists_in_dictionary(dictionary=self.parent.fitting_input_dictionary, tree_key=['rois', row_selected, 'fitting', algo_name, name_of_page, 'xaxis_to_fit']): # show fit only if tof scale selected if x_axis_selected == 'lambda': _entry = self.parent.fitting_input_dictionary['rois'][row_selected]['fitting'][algo_name][name_of_page] xaxis = _entry['xaxis_to_fit'] yaxis = _entry['yaxis_fitted'] yaxis = -np.log(yaxis) self.parent.ui.fitting.plot(xaxis, yaxis, pen=(self.parent.fit_rgb[0], self.parent.fit_rgb[1], self.parent.fit_rgb[2])) if peak_range_index[0] is None: self.parent.fitting_range_changed() self.parent.ui.fitting.setLabel("left", "Cross Section (arbitrary Units)")
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