Skip to content

Commit

Permalink
Change name of log file and print tables
Browse files Browse the repository at this point in the history
Changed the name of the log file to account for the fact that one
of the routines is joint, so hte name cannot depend on the spectra being
fitted (well it could, but that would be unecessary and more complex).

I also noticed that no information about the initial parameters was being
outputed so I created a new function for printing tables and used that to
show the tables for the initial parameters.
  • Loading branch information
GuiMacielPereira committed Jan 7, 2025
1 parent b210fe1 commit eeda2fa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/mvesuvio/main/run_routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
loadRawAndEmptyWsFromUserPath, cropAndMaskWorkspace, \
calculate_h_ratio, name_for_starting_ws, \
scattering_type, ws_history_matches_inputs, save_ws_from_load_vesuvio, \
is_hydrogen_present, create_profiles_table, create_table_for_hydrogen_to_mass_ratios
is_hydrogen_present, create_profiles_table, create_table_for_hydrogen_to_mass_ratios, \
print_table_workspace
from mvesuvio.analysis_reduction import VesuvioAnalysisRoutine

from mantid.api import mtd
Expand Down Expand Up @@ -89,21 +90,24 @@ def run(self):
wsInMtd = [ws in mtd for ws in self.ws_to_fit_y_space] # Bool list
if (len(wsInMtd) > 0) and all(wsInMtd):
self.runAnalysisFitting()
self.make_clean_log_file()
self.make_summarised_log_file()
return self.analysis_result, self.fitting_result

self.runAnalysisRoutine()
self.runAnalysisFitting()

# Return results used only in tests
self.make_clean_log_file()
self.make_summarised_log_file()
return self.analysis_result, self.fitting_result


def make_clean_log_file(self):
def make_summarised_log_file(self):
pattern = re.compile(r"^\d{4}-\d{2}-\d{2}")

log_file_save_path = self.make_log_file_name()

try:
with open(self.mantid_log_file, "r") as infile, open(self.log_file_save_path, "w") as outfile:
with open(self.mantid_log_file, "r") as infile, open(log_file_save_path, "w") as outfile:
for line in infile:
if "VesuvioAnalysisRoutine" in line:
outfile.write(line)
Expand All @@ -116,7 +120,17 @@ def make_clean_log_file(self):
except OSError:
print("Mantid log file not available. Unable to produce a summarized log file for this routine.")
return


def make_log_file_name(self):
filename = ''
if self.bckwd_ai.run_this_scattering_type:
filename += 'bckwd_'
if self.fwd_ai.run_this_scattering_type:
filename += 'fwd_'
filename += self.yFitIC.fitting_model
return self.experiment_path / (filename+ ".log")


def runAnalysisFitting(self):
for wsName, i_cls in zip(self.ws_to_fit_y_space, self.classes_to_fit_y_space):
Expand Down Expand Up @@ -182,6 +196,7 @@ def run_joint_algs(cls, back_alg, front_alg):

# Update original profiles table
RenameWorkspace(fixed_profiles_table, receiving_profiles_table.name())
print_table_workspace(mtd[receiving_profiles_table.name()])
# Even if the name is the same, need to trigger update
front_alg.setPropertyValue("InputProfiles", receiving_profiles_table.name())

Expand Down Expand Up @@ -260,6 +275,7 @@ def _create_analysis_algorithm(self, ai):
maskTOFRange=ai.mask_time_of_flight_range
)
profiles_table = create_profiles_table(cropedWs.name()+"_initial_parameters", ai)
print_table_workspace(profiles_table)
ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder"))
kwargs = {
"InputWorkspace": cropedWs.name(),
Expand Down Expand Up @@ -335,25 +351,21 @@ def _set_output_paths(self, ai):

# Build Filename based on ic
corr = ""
if ai.do_gamma_correction & (ai.number_of_iterations_for_corrections > 0):
corr += "_GC"
if ai.do_multiple_scattering_correction & (ai.number_of_iterations_for_corrections > 0):
corr += "_MS"
corr += "MS"
if ai.do_gamma_correction & (ai.number_of_iterations_for_corrections > 0):
corr += "GC"

fileName = (
f"spec_{ai.detectors.strip()}_iter_{ai.number_of_iterations_for_corrections}{corr}" + ".npz"
f"{ai.detectors.strip()}_{ai.number_of_iterations_for_corrections}{corr}"
)
fileNameYSpace = fileName + "_ySpaceFit" + ".npz"

self.results_save_path = outputPath / fileName
ai.ySpaceFitSavePath = outputPath / fileNameYSpace
self.results_save_path = outputPath / (fileName + ".npz")
ai.ySpaceFitSavePath = outputPath / (fileName + "_yfit.npz")

# Set directories for figures
figSavePath = experimentPath / "figures"
figSavePath.mkdir(exist_ok=True)
self.fig_save_path = figSavePath

self.log_file_save_path = self.experiment_path / "test.log"
return


Expand Down
13 changes: 13 additions & 0 deletions src/mvesuvio/util/analysis_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
import ntpath


def print_table_workspace(table):
max_spacing = [max([len(str(i)) for i in table.column(key)] + [len(key)]) for key in table.keys()]
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))
logger.notice("|" + table_row + "|")
logger.notice(' '+'-'*(len(header)-2)+' ')
return


def create_profiles_table(name, ai):
table = CreateEmptyTableWorkspace(OutputWorkspace=name)
table.addColumn(type="str", name="label")
Expand Down

0 comments on commit eeda2fa

Please sign in to comment.