From 848b516dae68dc5c3624476678ae1a130d7f7779 Mon Sep 17 00:00:00 2001 From: GuiMacielPereira Date: Tue, 10 Dec 2024 11:45:42 +0000 Subject: [PATCH] Rename variables on input script --- src/mvesuvio/analysis_fitting.py | 13 ++-- src/mvesuvio/config/analysis_inputs.py | 62 +++++++++---------- src/mvesuvio/main/run_routine.py | 57 ++++++++--------- src/mvesuvio/util/analysis_helpers.py | 4 +- tests/data/analysis/inputs/analysis_test.py | 60 +++++++++--------- .../data/analysis/inputs/yspace_gauss_test.py | 4 -- tests/data/analysis/inputs/yspace_gc_test.py | 4 -- 7 files changed, 97 insertions(+), 107 deletions(-) diff --git a/src/mvesuvio/analysis_fitting.py b/src/mvesuvio/analysis_fitting.py index 2b6c13b0..0b811d44 100644 --- a/src/mvesuvio/analysis_fitting.py +++ b/src/mvesuvio/analysis_fitting.py @@ -54,7 +54,7 @@ def find_ws_name_fse_first_mass(ic): # Some dirty implementation to be deleted in the future ws_names_fse = [] ws_masses = [] - prefix = ic.name+'_'+str(ic.noOfMSIterations) + prefix = ic.name+'_'+str(ic.number_of_iterations_for_corrections) for ws_name in mtd.getObjectNames(): if ws_name.startswith(prefix) and ws_name.endswith('fse'): name_ending = ws_name.replace(prefix, "") @@ -103,8 +103,8 @@ def subtract_profiles_except_lightest(ic, ws): if len(ic.masses) == 1: return - ws_name_lightest_mass = ic.name + '_' + str(ic.noOfMSIterations) + '_' + str(min(ic.masses)) + '_ncp' - ws_name_profiles = ic.name + '_' + str(ic.noOfMSIterations) + '_total_ncp' + ws_name_lightest_mass = ic.name + '_' + str(ic.number_of_iterations_for_corrections) + '_' + str(min(ic.masses)) + '_ncp' + ws_name_profiles = ic.name + '_' + str(ic.number_of_iterations_for_corrections) + '_total_ncp' wsNcpExceptFirst = Minus(mtd[ws_name_profiles], mtd[ws_name_lightest_mass], OutputWorkspace=ws_name_profiles + '_except_lightest') @@ -126,7 +126,7 @@ def switchFirstTwoAxis(A): def ySpaceReduction(wsTOF, mass0, yFitIC, ic): """Seperate procedures depending on masking specified.""" - ws_name_lightest_mass = ic.name + '_' + str(ic.noOfMSIterations) + '_' + str(min(ic.masses)) + '_ncp' + ws_name_lightest_mass = ic.name + '_' + str(ic.number_of_iterations_for_corrections) + '_' + str(min(ic.masses)) + '_ncp' ncp = mtd[ws_name_lightest_mass].extractY() rebinPars = yFitIC.rebinParametersForYSpaceFit @@ -1503,9 +1503,10 @@ def extractData(ws, wsRes, ic): def loadInstrParsFileIntoArray(ic): ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder")) - data = np.loadtxt(str(ipFilesPath / ic.ipfile), dtype=str)[1:].astype(float) + data = np.loadtxt(str(ipFilesPath / ic.instrument_parameters_file), dtype=str)[1:].astype(float) spectra = data[:, 0] - select_rows = np.where((spectra >= ic.firstSpec) & (spectra <= ic.lastSpec)) + firstSpec, lastSpec = [int(d) for d in ic.detectors.split('-')] + select_rows = np.where((spectra >= firstSpec) & (spectra <= lastSpec)) instrPars = data[select_rows] return instrPars diff --git a/src/mvesuvio/config/analysis_inputs.py b/src/mvesuvio/config/analysis_inputs.py index af287c78..06bacb16 100644 --- a/src/mvesuvio/config/analysis_inputs.py +++ b/src/mvesuvio/config/analysis_inputs.py @@ -11,39 +11,38 @@ class SampleParameters: @dataclass class BackwardAnalysisInputs(SampleParameters): - run_this_scattering_type = False + run_this_scattering_type = True fit_in_y_space = False runs = "43066-43076" empty_runs = "41876-41923" mode = "DoubleDifference" - ipfile = "ip2019.par" - firstSpec = 3 # 3 - lastSpec = 134 # 134 - maskedSpecAllNo = [18, 34, 42, 43, 59, 60, 62, 118, 119, 133] - tofBinning = "275.,1.,420" # Binning of ToF spectra - maskTOFRange = None - subEmptyFromRaw = True # Flag to control wether empty ws gets subtracted from raw - scaleEmpty = 1 # None or scaling factor - scaleRaw = 1 + instrument_parameters_file = "ip2019.par" + detectors = "3-134" + mask_detectors = [18, 34, 42, 43, 59, 60, 62, 118, 119, 133] + time_of_flight_binning = "275.,1.,420" + mask_time_of_flight_range = None + subtract_empty_workspace_from_raw = True + scale_empty_workspace = 1 # None or scaling factor + scale_raw_workspace = 1 masses = [12, 16, 27] # Intensities, NCP widths, NCP centers - initPars = [1, 12, 0.0, 1, 12, 0.0, 1, 12.5, 0.0] - bounds = [ + initial_fitting_parameters = [1, 12, 0.0, 1, 12, 0.0, 1, 12.5, 0.0] + fitting_bounds = [ [0, None], [8, 16], [-3, 1], [0, None], [8, 16], [-3, 1], [0, None], [11, 14], [-3, 1], ] constraints = () - noOfMSIterations = 0 # 4 - MSCorrectionFlag = True - HToMassIdxRatio = 19.0620008206 # Set to zero when H is not present + number_of_iterations_for_corrections = 0 # 4 + do_multiple_scattering_correction = True + intensity_ratio_of_hydrogen_to_lowest_mass = 19.0620008206 # Set to zero to disable transmission_guess = 0.8537 # Experimental value from VesuvioTransmission multiple_scattering_order = 2 - number_of_events = 1.0e5 - GammaCorrectionFlag = False + multiple_scattering_number_of_events = 1.0e5 + do_gamma_correction = False @dataclass @@ -54,20 +53,19 @@ class ForwardAnalysisInputs(SampleParameters): runs = "43066-43076" empty_runs = "43868-43911" mode = "SingleDifference" - ipfile = "ip2018_3.par" - firstSpec = 144 # 144 - lastSpec = 182 # 182 - maskedSpecAllNo = [173, 174, 179] - tofBinning = "110,1,430" # Binning of ToF spectra - maskTOFRange = None - subEmptyFromRaw = False # Flag to control wether empty ws gets subtracted from raw - scaleEmpty = 1 # None or scaling factor - scaleRaw = 1 + instrument_parameters_file = "ip2018_3.par" + detectors = '144-182' + mask_detectors = [173, 174, 179] + time_of_flight_binning = "110,1,430" + mask_time_of_flight_range = None + subtract_empty_workspace_from_raw = False + scale_empty_workspace = 1 # None or scaling factor + scale_raw_workspace = 1 masses = 1.0079, 12, 16, 27 # Intensities, NCP widths, NCP centers - initPars = [1, 4.7, 0, 1, 12.71, 0.0, 1, 8.76, 0.0, 1, 13.897, 0.0] - bounds = [ + initial_fitting_parameters = [1, 4.7, 0, 1, 12.71, 0.0, 1, 8.76, 0.0, 1, 13.897, 0.0] + fitting_bounds = [ [0, None], [3, 6], [-3, 1], [0, None], [12.71, 12.71], [-3, 1], [0, None], [8.76, 8.76], [-3, 1], @@ -75,12 +73,12 @@ class ForwardAnalysisInputs(SampleParameters): ] constraints = () - noOfMSIterations = 0 # 4 - MSCorrectionFlag = True + number_of_iterations_for_corrections = 0 # 4 + do_multiple_scattering_correction = True transmission_guess = 0.8537 # Experimental value from VesuvioTransmission multiple_scattering_order = 2 - number_of_events = 1.0e5 - GammaCorrectionFlag = True + multiple_scattering_number_of_events = 1.0e5 + do_gamma_correction = True @dataclass diff --git a/src/mvesuvio/main/run_routine.py b/src/mvesuvio/main/run_routine.py index 43e335c3..eeb58584 100644 --- a/src/mvesuvio/main/run_routine.py +++ b/src/mvesuvio/main/run_routine.py @@ -39,7 +39,7 @@ def setup(self): self.classes_to_fit_y_space = [] for ai_cls in [self.bckwd_ai, self.fwd_ai]: if ai_cls.fit_in_y_space: - self.ws_to_fit_y_space.append(name_for_starting_ws(ai_cls) + '_' + str(ai_cls.noOfMSIterations)) + self.ws_to_fit_y_space.append(name_for_starting_ws(ai_cls) + '_' + str(ai_cls.number_of_iterations_for_corrections)) self.classes_to_fit_y_space.append(ai_cls) self.analysis_result = None @@ -103,14 +103,14 @@ def runAnalysisRoutine(self): if self.bckwd_ai.run_this_scattering_type: - if is_hydrogen_present(self.fwd_ai.masses) & (self.bckwd_ai.HToMassIdxRatio==0): + if is_hydrogen_present(self.fwd_ai.masses) & (self.bckwd_ai.intensity_ratio_of_hydrogen_to_lowest_mass==0): self.run_estimate_h_ratio() return # TODO: make this automatic assert is_hydrogen_present(self.fwd_ai.masses) != ( - self.bckwd_ai.HToMassIdxRatio==0 - ), "No Hydrogen detected, HToMassIdxRatio has to be set to 0" + self.bckwd_ai.intensity_ratio_of_hydrogen_to_lowest_mass==0 + ), "No Hydrogen detected, intensity_ratio_of_hydrogen_to_lowest_mass has to be set to 0" if self.bckwd_ai.run_this_scattering_type and self.fwd_ai.run_this_scattering_type: self.run_joint_analysis() @@ -220,37 +220,38 @@ def _create_analysis_algorithm(self, ai): ws = loadRawAndEmptyWsFromUserPath( userWsRawPath=raw_path, userWsEmptyPath=empty_path, - tofBinning=ai.tofBinning, + tofBinning=ai.time_of_flight_binning, name=name_for_starting_ws(ai), - scaleRaw=ai.scaleRaw, - scaleEmpty=ai.scaleEmpty, - subEmptyFromRaw=ai.subEmptyFromRaw + scaleRaw=ai.scale_raw_workspace, + scaleEmpty=ai.scale_empty_workspace, + subEmptyFromRaw=ai.subtract_empty_workspace_from_raw ) + first_detector, last_detector = [int(s) for s in ai.detectors.split('-')] cropedWs = cropAndMaskWorkspace( ws, - firstSpec=ai.firstSpec, - lastSpec=ai.lastSpec, - maskedDetectors=ai.maskedSpecAllNo, - maskTOFRange=ai.maskTOFRange + firstSpec=first_detector, + lastSpec=last_detector, + maskedDetectors=ai.mask_detectors, + maskTOFRange=ai.mask_time_of_flight_range ) profiles_table = create_profiles_table(cropedWs.name()+"_initial_parameters", ai) ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder")) kwargs = { "InputWorkspace": cropedWs.name(), "InputProfiles": profiles_table.name(), - "InstrumentParametersFile": str(ipFilesPath / ai.ipfile), - "HRatioToLowestMass": ai.HToMassIdxRatio if hasattr(ai, 'HRatioToLowestMass') else 0, - "NumberOfIterations": int(ai.noOfMSIterations), - "InvalidDetectors": [int(det) for det in ai.maskedSpecAllNo], - "MultipleScatteringCorrection": ai.MSCorrectionFlag, + "InstrumentParametersFile": str(ipFilesPath / ai.instrument_parameters_file), + "HRatioToLowestMass": ai.intensity_ratio_of_hydrogen_to_lowest_mass if hasattr(ai, 'intensity_ratio_of_hydrogen_to_lowest_mass') else 0, + "NumberOfIterations": int(ai.number_of_iterations_for_corrections), + "InvalidDetectors": [int(det) for det in ai.mask_detectors], + "MultipleScatteringCorrection": ai.do_multiple_scattering_correction, "SampleVerticalWidth": ai.vertical_width, "SampleHorizontalWidth": ai.horizontal_width, "SampleThickness": ai.thickness, - "GammaCorrection": ai.GammaCorrectionFlag, + "GammaCorrection": ai.do_gamma_correction, "ModeRunning": scattering_type(ai), "TransmissionGuess": ai.transmission_guess, "MultipleScatteringOrder": int(ai.multiple_scattering_order), - "NumberOfEvents": int(ai.number_of_events), + "NumberOfEvents": int(ai.multiple_scattering_number_of_events), "Constraints": str(dill.dumps(ai.constraints)), "ResultsPath": str(self.results_save_path), "FiguresPath": str(self.fig_save_path), @@ -271,8 +272,8 @@ def _create_analysis_algorithm(self, ai): def _make_neutron_profiles(cls, ai): profiles = [] for mass, intensity, width, center, intensity_bound, width_bound, center_bound in zip( - ai.masses, ai.initPars[::3], ai.initPars[1::3], ai.initPars[2::3], - ai.bounds[::3], ai.bounds[1::3], ai.bounds[2::3] + ai.masses, ai.initial_fitting_parameters[::3], ai.initial_fitting_parameters[1::3], ai.initial_fitting_parameters[2::3], + ai.fitting_bounds[::3], ai.fitting_bounds[1::3], ai.fitting_bounds[2::3] ): profiles.append(NeutronComptonProfile( label=str(float(mass)), mass=float(mass), intensity=float(intensity), width=float(width), center=float(center), @@ -294,11 +295,11 @@ def _save_ws_if_not_on_path(self, load_ai): ipFilesPath = Path(handle_config.read_config_var("caching.ipfolder")) - if not ws_history_matches_inputs(load_ai.runs, load_ai.mode, load_ai.ipfile, rawPath): - save_ws_from_load_vesuvio(load_ai.runs, load_ai.mode, str(ipFilesPath/load_ai.ipfile), rawPath) + if not ws_history_matches_inputs(load_ai.runs, load_ai.mode, load_ai.instrument_parameters_file, rawPath): + save_ws_from_load_vesuvio(load_ai.runs, load_ai.mode, str(ipFilesPath/load_ai.instrument_parameters_file), rawPath) - if not ws_history_matches_inputs(load_ai.empty_runs, load_ai.mode, load_ai.ipfile, emptyPath): - save_ws_from_load_vesuvio(load_ai.empty_runs, load_ai.mode, str(ipFilesPath/load_ai.ipfile), emptyPath) + if not ws_history_matches_inputs(load_ai.empty_runs, load_ai.mode, load_ai.instrument_parameters_file, emptyPath): + save_ws_from_load_vesuvio(load_ai.empty_runs, load_ai.mode, str(ipFilesPath/load_ai.instrument_parameters_file), emptyPath) return rawPath, emptyPath @@ -309,13 +310,13 @@ def _set_output_paths(self, ai): # Build Filename based on ic corr = "" - if ai.GammaCorrectionFlag & (ai.noOfMSIterations > 0): + if ai.do_gamma_correction & (ai.number_of_iterations_for_corrections > 0): corr += "_GC" - if ai.MSCorrectionFlag & (ai.noOfMSIterations > 0): + if ai.do_multiple_scattering_correction & (ai.number_of_iterations_for_corrections > 0): corr += "_MS" fileName = ( - f"spec_{ai.firstSpec}-{ai.lastSpec}_iter_{ai.noOfMSIterations}{corr}" + ".npz" + f"spec_{ai.detectors.strip()}_iter_{ai.number_of_iterations_for_corrections}{corr}" + ".npz" ) fileNameYSpace = fileName + "_ySpaceFit" + ".npz" diff --git a/src/mvesuvio/util/analysis_helpers.py b/src/mvesuvio/util/analysis_helpers.py index 7b4cb07d..300a5670 100644 --- a/src/mvesuvio/util/analysis_helpers.py +++ b/src/mvesuvio/util/analysis_helpers.py @@ -23,8 +23,8 @@ def create_profiles_table(name, ai): table.addColumn(type="float", name="center") table.addColumn(type="str", name="center_bounds") for mass, intensity, width, center, intensity_bound, width_bound, center_bound in zip( - ai.masses, ai.initPars[::3], ai.initPars[1::3], ai.initPars[2::3], - ai.bounds[::3], ai.bounds[1::3], ai.bounds[2::3] + ai.masses, ai.initial_fitting_parameters[::3], ai.initial_fitting_parameters[1::3], ai.initial_fitting_parameters[2::3], + ai.fitting_bounds[::3], ai.fitting_bounds[1::3], ai.fitting_bounds[2::3] ): table.addRow( [str(float(mass)), float(mass), float(intensity), str(list(intensity_bound)), diff --git a/tests/data/analysis/inputs/analysis_test.py b/tests/data/analysis/inputs/analysis_test.py index 46975a20..180be197 100644 --- a/tests/data/analysis/inputs/analysis_test.py +++ b/tests/data/analysis/inputs/analysis_test.py @@ -1,30 +1,29 @@ class SampleParameters: transmission_guess = 0.8537 # Experimental value from VesuvioTransmission - multiple_scattering_order, number_of_events = 2, 1.0e5 + multiple_scattering_order = 2 + multiple_scattering_number_of_events = 1.0e5 vertical_width, horizontal_width, thickness = 0.1, 0.1, 0.001 # Expressed in meters class BackwardAnalysisInputs(SampleParameters): run_this_scattering_type = False fit_in_y_space = False - ipfile = "ip2019.par" + instrument_parameters_file = "ip2019.par" runs = "43066-43076" empty_runs = "41876-41923" - spectra = "3-134" + detectors = "3-134" mode = "DoubleDifference" - tofBinning = "275.,1.,420" # Binning of ToF spectra - maskTOFRange = None - maskedSpecAllNo = [18, 34, 42, 43, 59, 60, 62, 118, 119, 133] - firstSpec = 3 # 3 - lastSpec = 134 # 134 - subEmptyFromRaw = True - scaleEmpty = 1 - scaleRaw = 1 + time_of_flight_binning = "275.,1.,420" # Binning of ToF spectra + mask_time_of_flight_range = None + mask_detectors = [18, 34, 42, 43, 59, 60, 62, 118, 119, 133] + subtract_empty_workspace_from_raw = True + scale_empty_workspace = 1 + scale_raw_workspace = 1 masses = [12, 16, 27] - initPars = [1, 12, 0.0, 1, 12, 0.0, 1, 12.5, 0.0] - bounds =[ + initial_fitting_parameters = [1, 12, 0.0, 1, 12, 0.0, 1, 12.5, 0.0] + fitting_bounds =[ [0, None], [8, 16], [-3, 1], @@ -37,34 +36,33 @@ class BackwardAnalysisInputs(SampleParameters): ] constraints = () - noOfMSIterations = 3 # 4 - MSCorrectionFlag = True - HToMassIdxRatio = 19.0620008206 # Set to zero or None when H is not present - GammaCorrectionFlag = False + number_of_iterations_for_corrections = 3 # 4 + do_multiple_scattering_correction = True + intensity_ratio_of_hydrogen_to_lowest_mass = 19.0620008206 # Set to zero or None when H is not present + do_gamma_correction = False class ForwardAnalysisInputs(SampleParameters): run_this_scattering_type = True fit_in_y_space = False - ipfile = "ip2018_3.par" + instrument_parameters_file = "ip2018_3.par" runs = "43066-43076" empty_runs = "43868-43911" spectra = "144-182" mode = "SingleDifference" - tofBinning = "110,1,430" # Binning of ToF spectra - maskTOFRange = None - maskedSpecAllNo = [173, 174, 179] - firstSpec = 144 # 144 - lastSpec = 182 # 182 - subEmptyFromRaw = False - scaleEmpty = 1 - scaleRaw = 1 + time_of_flight_binning = "110,1,430" # Binning of ToF spectra + mask_time_of_flight_range = None + mask_detectors = [173, 174, 179] + detectors = '144-182' + subtract_empty_workspace_from_raw= False + scale_empty_workspace= 1 + scale_raw_workspace= 1 masses = [1.0079, 12, 16, 27] - initPars = [1, 4.7, 0, 1, 12.71, 0.0, 1, 8.76, 0.0, 1, 13.897, 0.0] - bounds =[ + initial_fitting_parameters= [1, 4.7, 0, 1, 12.71, 0.0, 1, 8.76, 0.0, 1, 13.897, 0.0] + fitting_bounds =[ [0, None], [3, 6], [-3, 1], @@ -79,9 +77,9 @@ class ForwardAnalysisInputs(SampleParameters): [-3, 1], ] constraints = () - noOfMSIterations = 1 # 4 - MSCorrectionFlag = True - GammaCorrectionFlag = True + number_of_iterations_for_corrections= 1 # 4 + do_multiple_scattering_correction= True + do_gamma_correction= True class YSpaceFitInputs: diff --git a/tests/data/analysis/inputs/yspace_gauss_test.py b/tests/data/analysis/inputs/yspace_gauss_test.py index efe7c7ae..72cb1cca 100644 --- a/tests/data/analysis/inputs/yspace_gauss_test.py +++ b/tests/data/analysis/inputs/yspace_gauss_test.py @@ -3,10 +3,6 @@ ForwardAnalysisInputs, YSpaceFitInputs, ) -ForwardAnalysisInputs.noOfMSIterations = 1 -ForwardAnalysisInputs.firstSpec = 164 -ForwardAnalysisInputs.lastSpec = 175 -ForwardAnalysisInputs.maskedSpecAllNo = [173, 174] ForwardAnalysisInputs.fit_in_y_space = True BackwardAnalysisInputs.fit_in_y_space = False ForwardAnalysisInputs.run_this_scattering_type = True diff --git a/tests/data/analysis/inputs/yspace_gc_test.py b/tests/data/analysis/inputs/yspace_gc_test.py index cbd11323..d5fa66cd 100644 --- a/tests/data/analysis/inputs/yspace_gc_test.py +++ b/tests/data/analysis/inputs/yspace_gc_test.py @@ -4,10 +4,6 @@ YSpaceFitInputs, ) -ForwardAnalysisInputs.noOfMSIterations = 1 -ForwardAnalysisInputs.firstSpec = 164 -ForwardAnalysisInputs.lastSpec = 175 -ForwardAnalysisInputs.maskedSpecAllNo = [173, 174] ForwardAnalysisInputs.fit_in_y_space = True BackwardAnalysisInputs.fit_in_y_space = False ForwardAnalysisInputs.run_this_scattering_type = True