def perform_single_normalization_and_export(self, data=[], ob=[], coeff=[], output_file_name=''): ob = ob.astype(float) data = data.astype(float) coeff = coeff.astype(float) # sample / ob ob[ob == 0] = np.NAN _step1 = np.divide(data, ob) _step1[_step1 == np.NaN] = 0 _step1[_step1 == np.inf] = 0 # _term1 * coeff _data = _step1 * coeff FileHandler.make_fits(data=_data, filename=output_file_name) return _data
def populate_list_widget(self, o_loader): list_of_files = o_loader.list_of_files _list_ui = self.list_ui[self.data_type]['list'] _list_ui.clear() for _row, _file in enumerate(list_of_files): _item = QtGui.QListWidgetItem(_file) _list_ui.insertItem(_row, _item) _folder = o_loader.folder self.folder = _folder _parent_folder = FileHandler.get_parent_folder(_folder) self.list_ui[self.data_type]['folder'].setText(_parent_folder)
def retrieve_list_of_files(self, list_of_files=None): _folder = self.folder _image_ext = self.image_ext if list_of_files is None: _list_of_files = glob.glob(_folder + '/*' + _image_ext) else: _list_of_files = list_of_files self.list_of_files_full_name = _list_of_files short_list_of_files = [] self.folder = os.path.dirname(_list_of_files[0]) + '/' for _file in _list_of_files: _short_file = os.path.basename(_file) short_list_of_files.append(_short_file) short_list_of_files = FileHandler.cleanup_list_of_files(short_list_of_files) self.list_of_files = short_list_of_files
def run(self): output_folder_name = self.select_and_create_output_folder() if output_folder_name == '': return # hourglass cursor QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) # load table self.load_table() # retrieve x-axis range selected x_axis = self.retrieve_x_axis() # loop over all bins for _key in self.table: _entry = self.table[_key] # create file name row_index = _entry['row_index'] column_index = _entry['column_index'] full_entry_file_name = self.build_entry_file_name( output_folder_name=output_folder_name, row_index=row_index, column_index=column_index) # retrieve exp y_axis exp_y_axis = self.retrieve_y_axis(_entry) # retrieve fitted y_axis d_spacing = _entry['d_spacing']['val'] sigma = _entry['sigma']['val'] alpha = _entry['alpha']['val'] a1 = _entry['a1']['val'] a2 = _entry['a2']['val'] if self.parent.fitting_ui.ui.advanced_table_checkBox.isChecked(): a5 = _entry['a5']['val'] a6 = _entry['a6']['val'] else: a5 = 'N/A' a6 = 'N/A' fit_y_axis = [] for _x in x_axis: if self.parent.fitting_ui.ui.advanced_table_checkBox.isChecked( ): _y = advanced_fit(_x, d_spacing, alpha, sigma, a1, a2, a5, a6) else: _y = basic_fit(_x, d_spacing, alpha, sigma, a1, a2) fit_y_axis.append(_y) # export file metadata = ['# bin parameters'] metadata.append('# x0: {}'.format(self.xy_parameters['x0'])) metadata.append('# x1: {}'.format(self.xy_parameters['x1'])) metadata.append('# y0: {}'.format(self.xy_parameters['y0'])) metadata.append('# y1: {}'.format(self.xy_parameters['y1'])) metadata.append('# fitting parameters') metadata.append('# d_spacing: {}'.format(d_spacing)) metadata.append('# sigma: {}'.format(sigma)) metadata.append('# alpha: {}'.format(alpha)) metadata.append('# a1: {}'.format(a1)) metadata.append('# a2: {}'.format(a2)) metadata.append('# a5: {}'.format(a5)) metadata.append('# a6: {}'.format(a6)) metadata.append('#') metadata.append('# lambda(Angstroms), experimental, fitting') data = list(zip(x_axis, exp_y_axis, fit_y_axis)) FileHandler.make_ascii_file(metadata=metadata, data=data, output_file_name=full_entry_file_name) QApplication.restoreOverrideCursor()
def retrieve_time_spectra(self, auto_load=True): if auto_load: if self.data_type == 'sample': folder = self.parent.data_metadata['sample']['folder'] else: folder = self.parent.data_metadata['normalized']['folder'] o_time_spectra = LoadTimeSpectra(folder=folder, auto_load=auto_load) if o_time_spectra.file_found: time_spectra = o_time_spectra.time_spectra base_time_spectra = FileHandler.get_base_filename(time_spectra) folder_name = FileHandler.get_parent_folder(time_spectra) self.parent.time_spectra_folder = os.path.dirname(time_spectra) if self.data_type == 'sample': self.list_ui['time_spectra']['text'].setText( base_time_spectra) self.list_ui['time_spectra']['folder'].setText(folder_name) elif self.data_type == 'normalized': self.parent.data_metadata['time_spectra'][ 'full_file_name'] = time_spectra self.list_ui['time_spectra']['text2'].setText( base_time_spectra) self.list_ui['time_spectra']['folder2'].setText( folder_name) self.parent.data_metadata['time_spectra'][ 'normalized_folder'] = folder_name self.parent.time_spectra_normalized_folder = os.path.dirname( time_spectra) else: if self.data_type == 'sample': folder = self.parent.data_metadata['time_spectra']['folder'] else: folder = self.parent.data_metadata['time_spectra'][ 'normalized_folder'] time_spectra_name_format = '*_Spectra.txt' file_name = str( QFileDialog.getOpenFileName( caption="Select the Time Spectra File", directory=folder, filter="Txt ({});;All (*.*)".format( time_spectra_name_format))) if file_name: folder_name = FileHandler.get_parent_folder(file_name) base_file_name = FileHandler.get_base_filename(file_name) self.parent.time_spectra_folder = os.path.dirname(file_name) if self.data_type == 'sample': self.list_ui['time_spectra']['text'].setText( base_file_name) self.list_ui['time_spectra']['folder'].setText(folder_name) self.parent.data_metadata['time_spectra'][ 'folder'] = folder_name elif self.data_type == 'normalized': self.parent.data_metadata['time_spectra'][ 'full_file_name'] = file_name self.list_ui['time_spectra']['text2'].setText( base_file_name) self.list_ui['time_spectra']['folder2'].setText( folder_name) self.parent.data_metadata['time_spectra'][ 'normalized_folder'] = folder_name self.parent.time_spectra_normalized_folder = os.path.dirname( file_name)
def populate_text(self): _file_contain = FileHandler.retrieve_ascii_contain(self.full_filename) self.ui.time_spectra_text.setText(_file_contain)
def normalize_full_set(self, output_folder='', base_folder_name=''): output_folder = os.path.join(output_folder, base_folder_name + '_normalized') self.parent.time_spectra_normalized_folder = output_folder if os.path.exists(output_folder): # if folder does exist already, we first remove it shutil.rmtree(output_folder) os.mkdir(output_folder) self.parent.ui.normalized_folder.setText(base_folder_name + '_normalized') self.parent.data_metadata['normalized']['folder'] = output_folder # get range we want to normalize range_to_normalize = self.parent.range_files_to_normalized_step2[ 'file_index'] # get short list of data file names list_samples_names = self.parent.data_files['sample'] list_samples_names = list_samples_names[ range_to_normalize[0]:range_to_normalize[1] + 1] data = self.parent.data_metadata['sample']['data'] data = data[range_to_normalize[0]:range_to_normalize[1] + 1] ob = self.parent.data_metadata['ob']['data'] ob = ob[range_to_normalize[0]:range_to_normalize[1] + 1] array_coeff = self.coeff_array array_coeff = array_coeff[range_to_normalize[0]:range_to_normalize[1] + 1] # progress bar self.parent.eventProgress.setMinimum(0) self.parent.eventProgress.setMaximum(len(list_samples_names) - 1) self.parent.eventProgress.setValue(0) self.parent.eventProgress.setVisible(True) QtGui.QApplication.processEvents() # list of file name (short) normalized_array = [] normalized_file_name = [] normalized_sum_counts = [] for _index_file, _short_file in enumerate(list_samples_names): _long_file_name = os.path.join(output_folder, _short_file) _data = data[_index_file] _ob = ob[_index_file] _coeff = array_coeff[_index_file] normalized_data = self.perform_single_normalization_and_export( data=_data, ob=_ob, coeff=_coeff, output_file_name=_long_file_name) normalized_data[np.isnan(normalized_data)] = 0 normalized_array.append(normalized_data) _sum = np.nansum(normalized_data) normalized_sum_counts.append(_sum) normalized_file_name.append(_short_file) self.parent.eventProgress.setValue(_index_file + 1) QtGui.QApplication.processEvents() self.parent.data_metadata['normalized']['data'] = normalized_array self.parent.data_files['normalized'] = normalized_file_name # tof array tof_array = self.parent.data_metadata['time_spectra']['data'] tof_array = tof_array[range_to_normalize[0]:range_to_normalize[1] + 1] short_tof_file_name = '{}_Spectra.txt'.format(base_folder_name) tof_file_name = os.path.join(output_folder, short_tof_file_name) tof_array = list(zip(tof_array, normalized_sum_counts)) FileHandler.make_ascii_file(data=tof_array, output_file_name=tof_file_name, sep='\t') self.parent.ui.time_spectra_folder_2.setText( os.path.basename(output_folder)) self.parent.ui.time_spectra_2.setText(short_tof_file_name) o_time_handler = TimeSpectraHandler(parent=self.parent, normalized_tab=True) o_time_handler.load() o_time_handler.calculate_lambda_scale() tof_array = o_time_handler.tof_array lambda_array = o_time_handler.lambda_array self.parent.data_metadata['time_spectra'][ 'normalized_data'] = tof_array self.parent.data_metadata['time_spectra'][ 'normalized_lambda'] = lambda_array # populate normalized tab list_ui = self.parent.ui.list_normalized list_ui.clear() for _row, _file in enumerate(normalized_file_name): _item = QtGui.QListWidgetItem(_file) list_ui.insertItem(_row, _item) self.parent.eventProgress.setVisible(False)
def _save_image(self, filename='', data=[]): if os.path.exists(filename): os.remove(filename) FileHandler.make_fits(data=data, filename=filename)