Skip to content

Commit

Permalink
Improve printing of tables
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
GuiMacielPereira committed Jan 7, 2025
1 parent eeda2fa commit d6f1692
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/mvesuvio/analysis_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions src/mvesuvio/util/analysis_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d6f1692

Please sign in to comment.