From d6f16922677a3c4359753bc29d85876a0d127f8b Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Mon, 6 Jan 2025 17:23:51 +0000 Subject: [PATCH] Improve printing of tables Convert to integers where possible so that priting of zeros is avoided. Changed names of columns of fit parameters because they were taking too much space when the table got print, which made it very difficult to read. Now table occupies much less space and is much clearer to read. --- src/mvesuvio/analysis_reduction.py | 20 ++++++++++++-------- src/mvesuvio/util/analysis_helpers.py | 10 ++++++++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/mvesuvio/analysis_reduction.py b/src/mvesuvio/analysis_reduction.py index 1aa1ca5..b516900 100644 --- a/src/mvesuvio/analysis_reduction.py +++ b/src/mvesuvio/analysis_reduction.py @@ -13,7 +13,7 @@ CreateWorkspace from mvesuvio.util.analysis_helpers import numerical_third_derivative, load_resolution, load_instrument_params, \ - extend_range_of_array + extend_range_of_array, print_table_workspace np.set_printoptions(suppress=True, precision=4, linewidth=200) @@ -238,13 +238,13 @@ def _initialize_table_fit_parameters(self): OutputWorkspace=self._workspace_being_fit.name()+ "_fit_results" ) table.setTitle("SciPy Fit Parameters") - table.addColumn(type="float", name="Spectrum") + table.addColumn(type="float", name="Spec") for label in self._profiles_table.column("label"): - table.addColumn(type="float", name=f"{label} intensity") - table.addColumn(type="float", name=f"{label} width") - table.addColumn(type="float", name=f"{label} center ") - table.addColumn(type="float", name="normalised chi2") - table.addColumn(type="float", name="no of iterations") + table.addColumn(type="float", name=f"{label} i") + table.addColumn(type="float", name=f"{label} w") + table.addColumn(type="float", name=f"{label} c") + table.addColumn(type="float", name="NChi2") + table.addColumn(type="float", name="Iter") return table @@ -324,9 +324,11 @@ def _fit_neutron_compton_profiles(self): self._row_being_fit = 0 while self._row_being_fit != len(self._dataY): self._fit_neutron_compton_profiles_to_row() + self.log().notice('') self._row_being_fit += 1 assert np.any(self._fit_parameters), "Fitting parameters cannot be zero for all spectra!" + print_table_workspace(self._table_fit_results) return @@ -486,6 +488,8 @@ def _fit_neutron_compton_profiles_to_row(self): if np.all(self._dataY[self._row_being_fit] == 0): self._table_fit_results.addRow(np.zeros(3*self._profiles_table.rowCount()+3)) + spectrum_number = self._instrument_params[self._row_being_fit, 0] + self.log().notice(f"Skip spectrum {int(spectrum_number):3d}") return result = scipy.optimize.minimize( @@ -508,7 +512,7 @@ def _fit_neutron_compton_profiles_to_row(self): # Store results for easier access when calculating means self._fit_parameters[self._row_being_fit] = tableRow - self.log().notice(' '.join(str(tableRow).split(",")).replace('[', '').replace(']', '')) + self.log().notice(f"Fit spectrum {int(spectrum_number):3d}: \u2713") # Pass fit profiles into workspaces ncp_for_each_mass, fse_for_each_mass = self._neutron_compton_profiles(fitPars) diff --git a/src/mvesuvio/util/analysis_helpers.py b/src/mvesuvio/util/analysis_helpers.py index 18592ca..b73c6a1 100644 --- a/src/mvesuvio/util/analysis_helpers.py +++ b/src/mvesuvio/util/analysis_helpers.py @@ -13,13 +13,19 @@ def print_table_workspace(table): - max_spacing = [max([len(str(i)) for i in table.column(key)] + [len(key)]) for key in table.keys()] + table_dict = table.toDict() + # Convert floats into strings + for key, values in table_dict.items(): + new_column = [int(item) if (not isinstance(item, str) and item.is_integer()) else item for item in values] + table_dict[key] = [f"{item:.3f}" if isinstance(item, float) else str(item) for item in new_column] + + max_spacing = [max([len(item) for item in values] + [len(key)]) for key, values in table_dict.items()] header = "|" + "|".join(f"{item}{' '*(spacing-len(item))}" for item, spacing in zip(table.keys(), max_spacing)) + "|" logger.notice(f"Table {table.name()}:") logger.notice(' '+'-'*(len(header)-2)+' ') logger.notice(header) for i in range(table.rowCount()): - table_row = "|".join(f"{item}{' '*(spacing-len(str(item)))}" for item, spacing in zip(table.row(i).values(), max_spacing)) + table_row = "|".join(f"{values[i]}{' '*(spacing-len(str(values[i])))}" for values, spacing in zip(table_dict.values(), max_spacing)) logger.notice("|" + table_row + "|") logger.notice(' '+'-'*(len(header)-2)+' ') return