-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from mantidproject/add_config_and_flexible_cac…
…hing Add flexible caching and CLI
- Loading branch information
Showing
10 changed files
with
225 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
default.facility=ISIS | ||
default.instrument=Vesuvio | ||
datasearch.searcharchive=On |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# properties that suit your particular installation. | ||
caching.location = | ||
caching.experiment = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,53 @@ | ||
"""Package defining top-level application | ||
and entry points. | ||
"""Package defining entry points. | ||
""" | ||
import argparse | ||
from os import environ, path | ||
from EVSVesuvio.scripts import handle_config | ||
|
||
|
||
def main(): | ||
"""Placeholder. | ||
""" | ||
print("TEST_VESUVIO_LAUNCH") | ||
parser = __set_up_parser() | ||
args = parser.parse_args() | ||
if args.command == "config": | ||
__setup_config(args) | ||
|
||
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") | ||
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(args): | ||
config_dir = handle_config.VESUVIO_CONFIG_PATH | ||
handle_config.setup_config_dir(config_dir) | ||
|
||
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 __run_analysis(): | ||
environ['MANTIDPROPERTIES'] = path.join(handle_config.VESUVIO_CONFIG_PATH, "Mantid.user.properties") | ||
from EVSVesuvio import analysis_runner | ||
analysis_runner.run() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
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__))) | ||
MANTID_CONFIG_FILE = "Mantid.user.properties" | ||
|
||
|
||
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 = 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): | ||
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) | ||
|
||
with open(file_path, 'w') as file: | ||
file.writelines(updated_lines) | ||
|
||
|
||
def read_config_var(var, throw_on_not_found=True): | ||
file_path = f'{VESUVIO_CONFIG_PATH}/{VESUVIO_CONFIG_FILE}' | ||
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 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)) | ||
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): | ||
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), input_file_path(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 config_set(): | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.