From 9aa4dfbda6fa6efc19d6722063f3d38ebcb6e77c Mon Sep 17 00:00:00 2001 From: MialLewis <95620982+MialLewis@users.noreply.github.com> Date: Fri, 10 Nov 2023 15:05:35 +0000 Subject: [PATCH 1/8] add default config file --- EVSVesuvio/config/vesuvio.user.properties | 1 + 1 file changed, 1 insertion(+) create mode 100644 EVSVesuvio/config/vesuvio.user.properties diff --git a/EVSVesuvio/config/vesuvio.user.properties b/EVSVesuvio/config/vesuvio.user.properties new file mode 100644 index 00000000..66768fd7 --- /dev/null +++ b/EVSVesuvio/config/vesuvio.user.properties @@ -0,0 +1 @@ +# properties that suit your particular installation. \ No newline at end of file From 3de46ad225dddd67f27aef580ad19fff5c5ca524 Mon Sep 17 00:00:00 2001 From: MialLewis <95620982+MialLewis@users.noreply.github.com> Date: Fri, 10 Nov 2023 15:18:18 +0000 Subject: [PATCH 2/8] add config dir creation --- EVSVesuvio/scripts/__init__.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/EVSVesuvio/scripts/__init__.py b/EVSVesuvio/scripts/__init__.py index dd64ce45..99787b72 100644 --- a/EVSVesuvio/scripts/__init__.py +++ b/EVSVesuvio/scripts/__init__.py @@ -1,9 +1,20 @@ """Package defining top-level application and entry points. """ +import os +from shutil import copyfile def main(): - """Placeholder. - """ - print("TEST_VESUVIO_LAUNCH") + make_config_dir() + + +def make_config_dir(): + config_dir = os.path.join(os.path.expanduser("~"), '.mvesuvio') + if not os.path.isdir(config_dir): + os.mkdir(config_dir) + copyfile('EVSVesuvio/config/vesuvio.user.properties', f'{config_dir}/vesuvio.user.properties') + + +if __name__ == '__main__': + main() From 292adc51647dc6332835ecd4177f243616b8a86c Mon Sep 17 00:00:00 2001 From: MialLewis <95620982+MialLewis@users.noreply.github.com> Date: Fri, 10 Nov 2023 17:12:28 +0000 Subject: [PATCH 3/8] add ability to set up config and caching --- EVSVesuvio/config/vesuvio.user.properties | 4 +- EVSVesuvio/scripts/__init__.py | 68 +++++++++++++++++++++-- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/EVSVesuvio/config/vesuvio.user.properties b/EVSVesuvio/config/vesuvio.user.properties index 66768fd7..912419db 100644 --- a/EVSVesuvio/config/vesuvio.user.properties +++ b/EVSVesuvio/config/vesuvio.user.properties @@ -1 +1,3 @@ -# properties that suit your particular installation. \ No newline at end of file +# properties that suit your particular installation. +caching.location = +caching.experiment = \ No newline at end of file diff --git a/EVSVesuvio/scripts/__init__.py b/EVSVesuvio/scripts/__init__.py index 99787b72..af648857 100644 --- a/EVSVesuvio/scripts/__init__.py +++ b/EVSVesuvio/scripts/__init__.py @@ -1,20 +1,76 @@ """Package defining top-level application and entry points. """ +import argparse import os from shutil import copyfile +VESUVIO_CONFIG_FILE = "vesuvio.user.properties" + def main(): - make_config_dir() + parser = __set_up_parser() + args = parser.parse_args() + config_dir = os.path.join(os.path.expanduser("~"), '.mvesuvio') + cache_dir = config_dir if not args.set_cache else args.set_cache + experiment = "default" if not args.set_experiment else args.set_experiment + if __setup_config_dir(config_dir): + __set_config_vars(config_dir, {'caching.location': cache_dir, + 'caching.experiment': experiment}) + __setup_expr_dir(cache_dir, experiment) -def make_config_dir(): - config_dir = os.path.join(os.path.expanduser("~"), '.mvesuvio') - if not os.path.isdir(config_dir): - os.mkdir(config_dir) - copyfile('EVSVesuvio/config/vesuvio.user.properties', f'{config_dir}/vesuvio.user.properties') + +def __set_up_parser(): + parser = argparse.ArgumentParser(description="Package to analyse Vesuvio instrument data") + parser.add_argument("--set-cache", "-c", help="set the cache directory", default="", type=str) + parser.add_argument("--set-experiment", "-e", help="set the current experiment", default="", type=str) + return parser + + +def __setup_config_dir(config_dir): + success = __mk_dir('config', config_dir) + if success: + copyfile('EVSVesuvio/config/vesuvio.user.properties', f'{config_dir}/{VESUVIO_CONFIG_FILE}') + return success + + +def __setup_expr_dir(cache_dir, experiment): + expr_path = os.path.join(cache_dir, "experiments", experiment) + __mk_dir('experiment', expr_path) + + +def __mk_dir(type, path): + success = False + if not os.path.isdir(path): + try: + os.makedirs(path) + success = True + except: + print(f'Unable to make {type} directory at location: {path}') + return success + + +def __set_config_vars(config_dir, var_dict): + file_path = f'{config_dir}/{VESUVIO_CONFIG_FILE}' + with open(file_path, 'r') as file: + lines = file.readlines() + + updated_lines = [] + for line in lines: + match = False + for var in var_dict: + if line.startswith(var): + updated_lines.append(f'{var}={var_dict[var]}\n') + match = True + break + if not match: + updated_lines.append(line) + + with open(file_path, 'w') as file: + file.writelines(updated_lines) if __name__ == '__main__': + print("test") main() From 6a39a0edaa0513d71c892f843af25a707e910da5 Mon Sep 17 00:00:00 2001 From: MialLewis <95620982+MialLewis@users.noreply.github.com> Date: Fri, 10 Nov 2023 19:21:27 +0000 Subject: [PATCH 4/8] ammend script to work with cache dir --- EVSVesuvio/original_inputs.py | 11 +++--- EVSVesuvio/scripts/__init__.py | 32 +++------------ EVSVesuvio/scripts/handle_config.py | 39 +++++++++++++++++++ .../core_functions/ICHelpers.py | 5 +-- .../core_functions/bootstrap_analysis.py | 5 --- 5 files changed, 52 insertions(+), 40 deletions(-) create mode 100644 EVSVesuvio/scripts/handle_config.py diff --git a/EVSVesuvio/original_inputs.py b/EVSVesuvio/original_inputs.py index cde26a90..5a7cf58e 100644 --- a/EVSVesuvio/original_inputs.py +++ b/EVSVesuvio/original_inputs.py @@ -1,13 +1,14 @@ import time import numpy as np from pathlib import Path -from vesuvio_analysis.core_functions.bootstrap import runBootstrap -from vesuvio_analysis.core_functions.bootstrap_analysis import runAnalysisOfStoredBootstrap -from vesuvio_analysis.core_functions.run_script import runScript +from EVSVesuvio.vesuvio_analysis.core_functions.bootstrap import runBootstrap +from EVSVesuvio.vesuvio_analysis.core_functions.bootstrap_analysis import runAnalysisOfStoredBootstrap +from EVSVesuvio.vesuvio_analysis.core_functions.run_script import runScript from mantid.api import AnalysisDataService +from EVSVesuvio.scripts import handle_config -scriptName = Path(__file__).name.split(".")[0] # Take out .py -experimentPath = Path(__file__).absolute().parent / "experiments" / scriptName # Path to the repository +scriptName = handle_config.read_config_var('caching.experiment') +experimentsPath = Path(handle_config.read_config_var('caching.location')) / "experiments" / scriptName # Path to the repository ipFilesPath = Path(__file__).absolute().parent / "vesuvio_analysis" / "ip_files" diff --git a/EVSVesuvio/scripts/__init__.py b/EVSVesuvio/scripts/__init__.py index af648857..85a14ea5 100644 --- a/EVSVesuvio/scripts/__init__.py +++ b/EVSVesuvio/scripts/__init__.py @@ -1,22 +1,20 @@ -"""Package defining top-level application -and entry points. +"""Package defining entry points. """ import argparse import os from shutil import copyfile - -VESUVIO_CONFIG_FILE = "vesuvio.user.properties" +from EVSVesuvio.scripts import handle_config def main(): parser = __set_up_parser() args = parser.parse_args() - config_dir = os.path.join(os.path.expanduser("~"), '.mvesuvio') + config_dir = handle_config.VESUVIO_CONFIG_PATH cache_dir = config_dir if not args.set_cache else args.set_cache experiment = "default" if not args.set_experiment else args.set_experiment if __setup_config_dir(config_dir): - __set_config_vars(config_dir, {'caching.location': cache_dir, + handle_config.set_config_vars({'caching.location': cache_dir, 'caching.experiment': experiment}) __setup_expr_dir(cache_dir, experiment) @@ -31,7 +29,7 @@ def __set_up_parser(): def __setup_config_dir(config_dir): success = __mk_dir('config', config_dir) if success: - copyfile('EVSVesuvio/config/vesuvio.user.properties', f'{config_dir}/{VESUVIO_CONFIG_FILE}') + copyfile('EVSVesuvio/config/vesuvio.user.properties', f'{config_dir}/{handle_config.VESUVIO_CONFIG_FILE}') return success @@ -51,26 +49,6 @@ def __mk_dir(type, path): return success -def __set_config_vars(config_dir, var_dict): - file_path = f'{config_dir}/{VESUVIO_CONFIG_FILE}' - with open(file_path, 'r') as file: - lines = file.readlines() - - updated_lines = [] - for line in lines: - match = False - for var in var_dict: - if line.startswith(var): - updated_lines.append(f'{var}={var_dict[var]}\n') - match = True - break - if not match: - updated_lines.append(line) - - with open(file_path, 'w') as file: - file.writelines(updated_lines) - - if __name__ == '__main__': print("test") main() diff --git a/EVSVesuvio/scripts/handle_config.py b/EVSVesuvio/scripts/handle_config.py new file mode 100644 index 00000000..7ea62272 --- /dev/null +++ b/EVSVesuvio/scripts/handle_config.py @@ -0,0 +1,39 @@ +import os + +VESUVIO_CONFIG_PATH = os.path.join(os.path.expanduser("~"), '.mvesuvio') +VESUVIO_CONFIG_FILE = "vesuvio.user.properties" + + +def set_config_vars(var_dict): + file_path = f'{VESUVIO_CONFIG_PATH}/{VESUVIO_CONFIG_FILE}' + with open(file_path, 'r') as file: + lines = file.readlines() + + updated_lines = [] + for line in lines: + match = False + for var in var_dict: + if line.startswith(var): + updated_lines.append(f'{var}={var_dict[var]}') + match = True + break + if not match: + updated_lines.append(line) + + with open(file_path, 'w') as file: + file.writelines(updated_lines) + + +def read_config_var(var): + file_path = f'{VESUVIO_CONFIG_PATH}/{VESUVIO_CONFIG_FILE}' + with open(file_path, 'r') as file: + lines = file.readlines() + + result = "" + for line in lines: + if line.startswith(var): + result = line.split("=", 2)[1].strip('\n') + break + if not result: + raise ValueError(f'{var} was not found in the vesuvio config') + return result diff --git a/EVSVesuvio/vesuvio_analysis/core_functions/ICHelpers.py b/EVSVesuvio/vesuvio_analysis/core_functions/ICHelpers.py index a823bc5c..fe5bff8c 100644 --- a/EVSVesuvio/vesuvio_analysis/core_functions/ICHelpers.py +++ b/EVSVesuvio/vesuvio_analysis/core_functions/ICHelpers.py @@ -1,8 +1,8 @@ from mantid.simpleapi import LoadVesuvio, SaveNexus from pathlib import Path +from EVSVesuvio.scripts import handle_config -currentPath = Path(__file__).absolute().parent -experimentsPath = currentPath / ".."/ ".." / "experiments" +experimentsPath = Path(handle_config.read_config_var('caching.location')) / "experiments" def completeICFromInputs(IC, scriptName, wsIC): @@ -163,7 +163,6 @@ def setBootstrapDirs(bckwdIC, fwdIC, bootIC, yFitIC): # Select script name and experiments path sampleName = bckwdIC.scriptName # Name of sample currently running - experimentsPath = currentPath/".."/".."/"experiments" # Used to store running times required to estimate Bootstrap total run time. bootIC.runTimesPath = experimentsPath / sampleName / "running_times.txt" diff --git a/EVSVesuvio/vesuvio_analysis/core_functions/bootstrap_analysis.py b/EVSVesuvio/vesuvio_analysis/core_functions/bootstrap_analysis.py index f143dbe7..b3e738c4 100644 --- a/EVSVesuvio/vesuvio_analysis/core_functions/bootstrap_analysis.py +++ b/EVSVesuvio/vesuvio_analysis/core_functions/bootstrap_analysis.py @@ -4,13 +4,8 @@ from EVSVesuvio.vesuvio_analysis.core_functions.fit_in_yspace import selectModelAndPars import numpy as np import matplotlib .pyplot as plt -from pathlib import Path from scipy import stats -currentPath = Path(__file__).parent.absolute() -experimentsPath = currentPath / ".." / ".. " / "experiments" -IPFilesPath = currentPath / ".." / "ip_files" - def runAnalysisOfStoredBootstrap(bckwdIC, fwdIC, yFitIC, bootIC, analysisIC, userCtr): From 40ca2eea798eabe58cb1640e974c02721ea9496c Mon Sep 17 00:00:00 2001 From: MialLewis <95620982+MialLewis@users.noreply.github.com> Date: Mon, 13 Nov 2023 12:23:44 +0000 Subject: [PATCH 5/8] add config command to cli --- EVSVesuvio/config/__init__.py | 0 .../analysis_inputs.py} | 44 ++++++------- EVSVesuvio/scripts/__init__.py | 56 ++++++++--------- EVSVesuvio/scripts/handle_config.py | 61 ++++++++++++++++--- pyproject.toml | 1 + 5 files changed, 102 insertions(+), 60 deletions(-) create mode 100644 EVSVesuvio/config/__init__.py rename EVSVesuvio/{original_inputs.py => config/analysis_inputs.py} (87%) diff --git a/EVSVesuvio/config/__init__.py b/EVSVesuvio/config/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/EVSVesuvio/original_inputs.py b/EVSVesuvio/config/analysis_inputs.py similarity index 87% rename from EVSVesuvio/original_inputs.py rename to EVSVesuvio/config/analysis_inputs.py index 5a7cf58e..268b1483 100644 --- a/EVSVesuvio/original_inputs.py +++ b/EVSVesuvio/config/analysis_inputs.py @@ -17,10 +17,10 @@ class LoadVesuvioBackParameters: empty_runs="41876-41923" # 77K # The numbers of the empty runs to be subtracted spectra='3-134' # Spectra to be analysed mode='DoubleDifference' - ipfile=ipFilesPath / "ip2019.par" + ipfile=ipFilesPath / "ip2019.par" subEmptyFromRaw = True # Flag to control wether empty ws gets subtracted from raw - scaleEmpty = 1 # None or scaling factor + scaleEmpty = 1 # None or scaling factor scaleRaw = 1 @@ -29,16 +29,16 @@ class LoadVesuvioFrontParameters: empty_runs='43868-43911' # 100K # The numbers of the empty runs to be subtracted spectra='144-182' # Spectra to be analysed mode='SingleDifference' - ipfile=ipFilesPath / "ip2018_3.par" + ipfile=ipFilesPath / "ip2018_3.par" subEmptyFromRaw = False # Flag to control wether empty ws gets subtracted from raw - scaleEmpty = 1 # None or scaling factor + scaleEmpty = 1 # None or scaling factor scaleRaw = 1 class GeneralInitialConditions: """Used to define initial conditions shared by both Back and Forward scattering""" - + transmission_guess = 0.8537 # Experimental value from VesuvioTransmission multiple_scattering_order, number_of_events = 2, 1.e5 # Sample slab parameters @@ -46,7 +46,7 @@ class GeneralInitialConditions: class BackwardInitialConditions(GeneralInitialConditions): - InstrParsPath = ipFilesPath / "ip2018_3.par" + InstrParsPath = ipFilesPath / "ip2018_3.par" HToMassIdxRatio = 19.0620008206 # Set to zero or None when H is not present massIdx = 0 @@ -55,11 +55,11 @@ class BackwardInitialConditions(GeneralInitialConditions): masses = np.array([12, 16, 27]) # noOfMasses = len(masses) - initPars = np.array([ - # Intensities, NCP widths, NCP centers - 1, 12, 0., - 1, 12, 0., - 1, 12.5, 0. + initPars = np.array([ + # Intensities, NCP widths, NCP centers + 1, 12, 0., + 1, 12, 0., + 1, 12.5, 0. ]) bounds = np.array([ [0, np.nan], [8, 16], [-3, 1], @@ -81,7 +81,7 @@ class BackwardInitialConditions(GeneralInitialConditions): # # Parameters of workspaces in input_ws tofBinning='275.,1.,420' # Binning of ToF spectra maskTOFRange = None - + # Original data uses histogram data instead of point data runHistData = True normVoigt = False @@ -89,14 +89,14 @@ class BackwardInitialConditions(GeneralInitialConditions): class ForwardInitialConditions(GeneralInitialConditions): - masses = np.array([1.0079, 12, 16, 27]) + masses = np.array([1.0079, 12, 16, 27]) - initPars = np.array([ - # Intensities, NCP widths, NCP centers - 1, 4.7, 0, - 1, 12.71, 0., - 1, 8.76, 0., - 1, 13.897, 0. + initPars = np.array([ + # Intensities, NCP widths, NCP centers + 1, 4.7, 0, + 1, 12.71, 0., + 1, 8.76, 0., + 1, 13.897, 0. ]) bounds = np.array([ [0, np.nan], [3, 6], [-3, 1], @@ -118,7 +118,7 @@ class ForwardInitialConditions(GeneralInitialConditions): tofBinning="110,1,430" # Binning of ToF spectra maskTOFRange = None - + # Original data uses histogram data instead of point data runHistData = True normVoigt = False @@ -130,7 +130,7 @@ class YSpaceFitInitialConditions: class UserScriptControls: runRoutine = True - + # Choose main procedure to run procedure = "BACKWARD" # Options: None, "BACKWARD", "FORWARD", "JOINT" @@ -145,7 +145,7 @@ class BootstrapInitialConditions: start_time = time.time() wsBackIC = LoadVesuvioBackParameters -wsFrontIC = LoadVesuvioFrontParameters +wsFrontIC = LoadVesuvioFrontParameters bckwdIC = BackwardInitialConditions fwdIC = ForwardInitialConditions yFitIC = YSpaceFitInitialConditions diff --git a/EVSVesuvio/scripts/__init__.py b/EVSVesuvio/scripts/__init__.py index 85a14ea5..c6620ec7 100644 --- a/EVSVesuvio/scripts/__init__.py +++ b/EVSVesuvio/scripts/__init__.py @@ -1,54 +1,50 @@ """Package defining entry points. """ import argparse -import os -from shutil import copyfile from EVSVesuvio.scripts import handle_config def main(): parser = __set_up_parser() args = parser.parse_args() - config_dir = handle_config.VESUVIO_CONFIG_PATH - cache_dir = config_dir if not args.set_cache else args.set_cache - experiment = "default" if not args.set_experiment else args.set_experiment + if args.command == "config": + __setup_config(args) - if __setup_config_dir(config_dir): - handle_config.set_config_vars({'caching.location': cache_dir, - 'caching.experiment': experiment}) - __setup_expr_dir(cache_dir, experiment) + if args.command == "run": + if not handle_config.config_set(): + __setup_config(None) + __run_analysis() def __set_up_parser(): parser = argparse.ArgumentParser(description="Package to analyse Vesuvio instrument data") - parser.add_argument("--set-cache", "-c", help="set the cache directory", default="", type=str) - parser.add_argument("--set-experiment", "-e", help="set the current experiment", default="", type=str) - return parser + subparsers = parser.add_subparsers(dest='command', required=True) + config_parser = subparsers.add_parser("config", help="set mvesuvio configuration") + config_parser.add_argument("--set-cache", "-c", help="set the cache directory", default="", type=str) + config_parser.add_argument("--set-experiment", "-e", help="set the current experiment", default="", type=str) + config_parser = subparsers.add_parser("run", help="run mvesuvio analysis") + return parser -def __setup_config_dir(config_dir): - success = __mk_dir('config', config_dir) - if success: - copyfile('EVSVesuvio/config/vesuvio.user.properties', f'{config_dir}/{handle_config.VESUVIO_CONFIG_FILE}') - return success +def __setup_config(args): + config_dir = handle_config.VESUVIO_CONFIG_PATH + handle_config.setup_config_dir(config_dir) -def __setup_expr_dir(cache_dir, experiment): - expr_path = os.path.join(cache_dir, "experiments", experiment) - __mk_dir('experiment', expr_path) + if handle_config.config_set(): + cache_dir = handle_config.read_config_var('caching.location') if not args or not args.set_cache else args.set_cache + experiment = handle_config.read_config_var('caching.experiment') if not args or not args.set_experiment else args.set_experiment + else: + cache_dir = config_dir if not args or not args.set_cache else args.set_cache + experiment = "default" if not args or not args.set_experiment else args.set_experiment + handle_config.set_config_vars({'caching.location': cache_dir, + 'caching.experiment': experiment}) + handle_config.setup_expr_dir(cache_dir, experiment) -def __mk_dir(type, path): - success = False - if not os.path.isdir(path): - try: - os.makedirs(path) - success = True - except: - print(f'Unable to make {type} directory at location: {path}') - return success +def __run_analysis(): + print("RUNNING ANALYSIS") if __name__ == '__main__': - print("test") main() diff --git a/EVSVesuvio/scripts/handle_config.py b/EVSVesuvio/scripts/handle_config.py index 7ea62272..658fd178 100644 --- a/EVSVesuvio/scripts/handle_config.py +++ b/EVSVesuvio/scripts/handle_config.py @@ -1,22 +1,38 @@ import os +from shutil import copyfile VESUVIO_CONFIG_PATH = os.path.join(os.path.expanduser("~"), '.mvesuvio') VESUVIO_CONFIG_FILE = "vesuvio.user.properties" +VESUVIO_INPUTS_FILE = "analysis_inputs.py" +VESUVIO_PACKAGE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +def __read_config(config_file_path, throw_on_not_found=True): + lines = "" + try: + with open(config_file_path, 'r') as file: + lines = file.readlines() + except IOError: + if throw_on_not_found: + raise RuntimeError(f"Could not read from vesuvio config file: {config_file_path}") + return lines def set_config_vars(var_dict): - file_path = f'{VESUVIO_CONFIG_PATH}/{VESUVIO_CONFIG_FILE}' - with open(file_path, 'r') as file: - lines = file.readlines() + file_path = os.path.join(VESUVIO_CONFIG_PATH, VESUVIO_CONFIG_FILE) + lines = __read_config(file_path) updated_lines = [] for line in lines: match = False for var in var_dict: if line.startswith(var): - updated_lines.append(f'{var}={var_dict[var]}') + new_line = f'{var}={var_dict[var]}' + updated_lines.append(f'{new_line}\n') match = True + print(f'Setting: {new_line}') break + if not match: updated_lines.append(line) @@ -24,16 +40,45 @@ def set_config_vars(var_dict): file.writelines(updated_lines) -def read_config_var(var): +def read_config_var(var, throw_on_not_found=True): file_path = f'{VESUVIO_CONFIG_PATH}/{VESUVIO_CONFIG_FILE}' - with open(file_path, 'r') as file: - lines = file.readlines() + lines = __read_config(file_path, throw_on_not_found) result = "" for line in lines: if line.startswith(var): result = line.split("=", 2)[1].strip('\n') break - if not result: + if not result and throw_on_not_found: raise ValueError(f'{var} was not found in the vesuvio config') return result + + +def setup_config_dir(config_dir): + success = __mk_dir('config', config_dir) + if success: + copyfile(os.path.join(VESUVIO_PACKAGE_PATH, "config", VESUVIO_CONFIG_FILE), os.path.join(config_dir, VESUVIO_CONFIG_FILE)) + + +def setup_expr_dir(cache_dir, experiment): + expr_path = os.path.join(cache_dir, "experiments", experiment) + __mk_dir('experiment', expr_path) + copyfile(os.path.join(VESUVIO_PACKAGE_PATH, "config", VESUVIO_INPUTS_FILE), os.path.join(expr_path, VESUVIO_INPUTS_FILE)) + + +def __mk_dir(type, path): + success = False + if not os.path.isdir(path): + try: + os.makedirs(path) + success = True + except: + print(f'Unable to make {type} directory at location: {path}') + return success + + +def config_set(): + if(not read_config_var('caching.location', False)): + return False + else: + return True diff --git a/pyproject.toml b/pyproject.toml index eed2c3ba..3cdb25f5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,6 +48,7 @@ namespaces = false [tool.setuptools.package-data] "EVSVesuvio.vesuvio_analysis.ip_files" = ["*.OPJ", "*.par", "*.DAT", "*.DAT3", "*.DAT6", "*.txt"] +"EVSVesuvio.config" = ["*.properties"] [tool.pytest.ini_options] pythonpath = ["."] From 95bc2c8f1648f57949176ae96799ffc4fa627d84 Mon Sep 17 00:00:00 2001 From: MialLewis <95620982+MialLewis@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:59:21 +0000 Subject: [PATCH 6/8] add analysis runner --- EVSVesuvio/analysis_runner.py | 51 ++++++++++++++++++++++++++++ EVSVesuvio/config/analysis_inputs.py | 48 ++++++-------------------- EVSVesuvio/scripts/__init__.py | 3 +- EVSVesuvio/scripts/handle_config.py | 12 ++++--- 4 files changed, 71 insertions(+), 43 deletions(-) create mode 100644 EVSVesuvio/analysis_runner.py diff --git a/EVSVesuvio/analysis_runner.py b/EVSVesuvio/analysis_runner.py new file mode 100644 index 00000000..a7aad492 --- /dev/null +++ b/EVSVesuvio/analysis_runner.py @@ -0,0 +1,51 @@ +import time +from pathlib import Path +import importlib +import sys +from os import path +#from EVSVesuvio.vesuvio_analysis.core_functions.bootstrap import runBootstrap +#from EVSVesuvio.vesuvio_analysis.core_functions.bootstrap_analysis import runAnalysisOfStoredBootstrap +from EVSVesuvio.vesuvio_analysis.core_functions.run_script import runScript +from mantid.api import AnalysisDataService +from EVSVesuvio.scripts import handle_config + + +def run(): + scriptName = handle_config.read_config_var('caching.experiment') + experimentsPath = Path(handle_config.read_config_var('caching.location')) / "experiments" / scriptName # Path to the repository + inputs_path = experimentsPath / "analysis_inputs.py" + ai = import_from_path(inputs_path, "analysis_inputs") + + ipFilesPath = Path(path.dirname(path.dirname(handle_config.__file__))) / "vesuvio_analysis" / "ip_files" + + start_time = time.time() + + wsBackIC = ai.LoadVesuvioBackParameters(ipFilesPath) + wsFrontIC = ai.LoadVesuvioFrontParameters(ipFilesPath) + bckwdIC = ai.BackwardInitialConditions(ipFilesPath) + fwdIC = ai.ForwardInitialConditions + yFitIC = ai.YSpaceFitInitialConditions + bootIC = ai.BootstrapInitialConditions + userCtr = ai.UserScriptControls + + runScript(userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC) + + AnalysisDataService.clear() + userCtr.procedure = "FORWARD" + + runScript(userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC) + + end_time = time.time() + print("\nRunning time: ", end_time-start_time, " seconds") + + +def import_from_path(path, name): + spec = importlib.util.spec_from_file_location(name, path) + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + spec.loader.exec_module(module) + return module + + +if __name__ == '__main__': + run() diff --git a/EVSVesuvio/config/analysis_inputs.py b/EVSVesuvio/config/analysis_inputs.py index 268b1483..9d5a1cad 100644 --- a/EVSVesuvio/config/analysis_inputs.py +++ b/EVSVesuvio/config/analysis_inputs.py @@ -1,23 +1,14 @@ -import time import numpy as np -from pathlib import Path -from EVSVesuvio.vesuvio_analysis.core_functions.bootstrap import runBootstrap -from EVSVesuvio.vesuvio_analysis.core_functions.bootstrap_analysis import runAnalysisOfStoredBootstrap -from EVSVesuvio.vesuvio_analysis.core_functions.run_script import runScript -from mantid.api import AnalysisDataService -from EVSVesuvio.scripts import handle_config - -scriptName = handle_config.read_config_var('caching.experiment') -experimentsPath = Path(handle_config.read_config_var('caching.location')) / "experiments" / scriptName # Path to the repository -ipFilesPath = Path(__file__).absolute().parent / "vesuvio_analysis" / "ip_files" class LoadVesuvioBackParameters: + def __init__(self, ipFilesPath): + self.ipfile=ipFilesPath / "ip2019.par" + runs="43066-43076" # 77K # The numbers of the runs to be analysed empty_runs="41876-41923" # 77K # The numbers of the empty runs to be subtracted spectra='3-134' # Spectra to be analysed mode='DoubleDifference' - ipfile=ipFilesPath / "ip2019.par" subEmptyFromRaw = True # Flag to control wether empty ws gets subtracted from raw scaleEmpty = 1 # None or scaling factor @@ -25,11 +16,13 @@ class LoadVesuvioBackParameters: class LoadVesuvioFrontParameters: + def __init__(self, ipFilesPath): + self.ipfile=ipFilesPath / "ip2018_3.par" + runs='43066-43076' # 100K # The numbers of the runs to be analysed empty_runs='43868-43911' # 100K # The numbers of the empty runs to be subtracted spectra='144-182' # Spectra to be analysed mode='SingleDifference' - ipfile=ipFilesPath / "ip2018_3.par" subEmptyFromRaw = False # Flag to control wether empty ws gets subtracted from raw scaleEmpty = 1 # None or scaling factor @@ -46,7 +39,8 @@ class GeneralInitialConditions: class BackwardInitialConditions(GeneralInitialConditions): - InstrParsPath = ipFilesPath / "ip2018_3.par" + def __init__(self, ipFilesPath): + self.InstrParsPath=ipFilesPath / "ip2018_3.par" HToMassIdxRatio = 19.0620008206 # Set to zero or None when H is not present massIdx = 0 @@ -55,8 +49,8 @@ class BackwardInitialConditions(GeneralInitialConditions): masses = np.array([12, 16, 27]) # noOfMasses = len(masses) - initPars = np.array([ # Intensities, NCP widths, NCP centers + initPars = np.array([ 1, 12, 0., 1, 12, 0., 1, 12.5, 0. @@ -91,8 +85,8 @@ class ForwardInitialConditions(GeneralInitialConditions): masses = np.array([1.0079, 12, 16, 27]) - initPars = np.array([ # Intensities, NCP widths, NCP centers + initPars = np.array([ 1, 4.7, 0, 1, 12.71, 0., 1, 8.76, 0., @@ -140,25 +134,3 @@ class UserScriptControls: class BootstrapInitialConditions: runBootstrap = False - - -start_time = time.time() - -wsBackIC = LoadVesuvioBackParameters -wsFrontIC = LoadVesuvioFrontParameters -bckwdIC = BackwardInitialConditions -fwdIC = ForwardInitialConditions -yFitIC = YSpaceFitInitialConditions -bootIC = BootstrapInitialConditions -userCtr = UserScriptControls - -runScript(userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC) - -AnalysisDataService.clear() -userCtr.procedure = "FORWARD" - -runScript(userCtr, scriptName, wsBackIC, wsFrontIC, bckwdIC, fwdIC, yFitIC, bootIC) - - -end_time = time.time() -print("\nRunning time: ", end_time-start_time, " seconds") diff --git a/EVSVesuvio/scripts/__init__.py b/EVSVesuvio/scripts/__init__.py index c6620ec7..1baa0154 100644 --- a/EVSVesuvio/scripts/__init__.py +++ b/EVSVesuvio/scripts/__init__.py @@ -43,7 +43,8 @@ def __setup_config(args): def __run_analysis(): - print("RUNNING ANALYSIS") + from EVSVesuvio import analysis_runner + analysis_runner.run() if __name__ == '__main__': diff --git a/EVSVesuvio/scripts/handle_config.py b/EVSVesuvio/scripts/handle_config.py index 658fd178..aae7afa0 100644 --- a/EVSVesuvio/scripts/handle_config.py +++ b/EVSVesuvio/scripts/handle_config.py @@ -63,7 +63,7 @@ def setup_config_dir(config_dir): def setup_expr_dir(cache_dir, experiment): expr_path = os.path.join(cache_dir, "experiments", experiment) __mk_dir('experiment', expr_path) - copyfile(os.path.join(VESUVIO_PACKAGE_PATH, "config", VESUVIO_INPUTS_FILE), os.path.join(expr_path, VESUVIO_INPUTS_FILE)) + copyfile(os.path.join(VESUVIO_PACKAGE_PATH, "config", VESUVIO_INPUTS_FILE), input_file_path(cache_dir, experiment)) def __mk_dir(type, path): @@ -78,7 +78,11 @@ def __mk_dir(type, path): def config_set(): - if(not read_config_var('caching.location', False)): - return False - else: + if(read_config_var('caching.location', False)): return True + else: + return False + + +def input_file_path(cache_dir, experiment): + return os.path.join(cache_dir, "experiments", experiment, VESUVIO_INPUTS_FILE) From e430772462850f731e425ad3b5efe3d53231a479 Mon Sep 17 00:00:00 2001 From: MialLewis <95620982+MialLewis@users.noreply.github.com> Date: Mon, 13 Nov 2023 17:16:50 +0000 Subject: [PATCH 7/8] add mantid config file --- EVSVesuvio/scripts/__init__.py | 2 ++ EVSVesuvio/scripts/handle_config.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/EVSVesuvio/scripts/__init__.py b/EVSVesuvio/scripts/__init__.py index 1baa0154..2df52d9d 100644 --- a/EVSVesuvio/scripts/__init__.py +++ b/EVSVesuvio/scripts/__init__.py @@ -1,6 +1,7 @@ """Package defining entry points. """ import argparse +from os import environ, path from EVSVesuvio.scripts import handle_config @@ -43,6 +44,7 @@ def __setup_config(args): def __run_analysis(): + environ['MANTIDPROPERTIES'] = path.join(handle_config.VESUVIO_CONFIG_PATH, "Mantid.user.properties") from EVSVesuvio import analysis_runner analysis_runner.run() diff --git a/EVSVesuvio/scripts/handle_config.py b/EVSVesuvio/scripts/handle_config.py index aae7afa0..7e74aec9 100644 --- a/EVSVesuvio/scripts/handle_config.py +++ b/EVSVesuvio/scripts/handle_config.py @@ -5,6 +5,7 @@ VESUVIO_CONFIG_FILE = "vesuvio.user.properties" VESUVIO_INPUTS_FILE = "analysis_inputs.py" VESUVIO_PACKAGE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +MANTID_CONFIG_FILE = "Mantid.user.properties" def __read_config(config_file_path, throw_on_not_found=True): @@ -58,6 +59,7 @@ def setup_config_dir(config_dir): success = __mk_dir('config', config_dir) if success: copyfile(os.path.join(VESUVIO_PACKAGE_PATH, "config", VESUVIO_CONFIG_FILE), os.path.join(config_dir, VESUVIO_CONFIG_FILE)) + copyfile(os.path.join(VESUVIO_PACKAGE_PATH, "config", MANTID_CONFIG_FILE), os.path.join(config_dir, MANTID_CONFIG_FILE)) def setup_expr_dir(cache_dir, experiment): From 0efa30040bc126dd97116c5281bd6cef99bd4826 Mon Sep 17 00:00:00 2001 From: MialLewis <95620982+MialLewis@users.noreply.github.com> Date: Mon, 13 Nov 2023 17:22:58 +0000 Subject: [PATCH 8/8] add mantid properties file --- EVSVesuvio/config/Mantid.user.properties | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 EVSVesuvio/config/Mantid.user.properties diff --git a/EVSVesuvio/config/Mantid.user.properties b/EVSVesuvio/config/Mantid.user.properties new file mode 100644 index 00000000..300b958c --- /dev/null +++ b/EVSVesuvio/config/Mantid.user.properties @@ -0,0 +1,3 @@ +default.facility=ISIS +default.instrument=Vesuvio +datasearch.searcharchive=On