From 05f07dfe46fdc9a1921aa31343b4967b29335325 Mon Sep 17 00:00:00 2001 From: MaNyh <nyhuis@tfd.uni-hannover.de> Date: Sat, 5 Feb 2022 03:44:51 +0100 Subject: [PATCH] running casecreation --- .../gwk_compressor_casegeneration/Snakefile | 55 ++++++++++++++---- .../case_creation.py | 56 +++++++++++-------- .../casesettings.yaml | 34 ++++++++++- .../gwk_compressor_casegeneration/params.tsv | 3 + 4 files changed, 112 insertions(+), 36 deletions(-) create mode 100644 examples/gwk_compressor_casegeneration/params.tsv diff --git a/examples/gwk_compressor_casegeneration/Snakefile b/examples/gwk_compressor_casegeneration/Snakefile index 506d3645..b019c22e 100644 --- a/examples/gwk_compressor_casegeneration/Snakefile +++ b/examples/gwk_compressor_casegeneration/Snakefile @@ -1,4 +1,7 @@ from snakemake.utils import validate +from snakemake.utils import Paramspace +import pandas as pd + import os #todo move this snakepipe to according dir in sourcecode @@ -7,30 +10,58 @@ configfile : "casesettings.yaml" validate(config,"config.schema.yaml") basedir = workflow.current_basedir +# declare a dataframe to be a paramspace +paramspace = Paramspace(pd.read_csv("params.tsv", sep="\t")) +paramspace.param_sep="-" +#paramspace.pattern="{}~{}~{}" + +def create_filelist_from_template(template): + """ + :param template: path + :return: list of files + """ + import ntrfc + from ntrfc.utils.dictionaries.dict_utils import nested_dict_pairs_iterator + from case_creation import get_directory_structure + templatepath = os.path.join(os.path.dirname(ntrfc.__file__),"database","case_templates",template) + assert os.path.isdir(templatepath) + files = list(nested_dict_pairs_iterator(get_directory_structure(templatepath))) + fpaths = [os.path.join(*i[:-1]) for i in files] + return fpaths + + +TEMPLATE = config["case_params"]["case_type"] +DATASETS = create_filelist_from_template(TEMPLATE) + rule all: input: - "done.txt" + # Aggregate over entire parameter space (or a subset thereof if needed) + # of course, something like this can happen anywhere in the workflow (not + # only at the end). + #create_filelist_from_template(TEMPLATE) + "99_Report/parametrization.txt" + #expand("results/plots/{params}.pdf", params=paramspace.instance_patterns) + rule copy_template: output: #todo: is there a readable dataformat alternative (csv) instead of pickle? #dynamic("01_case/{case_type}/{case_files}"), - dynamic("01_case/{level_1}/{level_2}"), - "01_case/check.txt" + create_filelist_from_template(TEMPLATE) + #dynamic("01_case/{level_1}/{level_2}"), +# "01_case/check.txt" params: case_type = config["case_params"]["case_type"], case_parameters = config["case_params"]["parameters"] run: - from case_creation import create_case_fromtemplate - create_case_fromtemplate(params["case_type"],params["case_parameters"],os.path.join(basedir,"01_case")) - - + from case_creation import copy_template + copy_template(output,params["case_type"],params["case_parameters"]) -rule check_template: +rule set_paras: input: - #dynamic("01_case/{case_type}/{case_files}") - dynamic("01_case/{level_1}/{level_2}") + create_filelist_from_template(TEMPLATE) output: - "done.txt" + "99_Report/parametrization.txt" run: - print("tbd") + with open(output[0],"w") as fhandle: + fhandle.write(" ") diff --git a/examples/gwk_compressor_casegeneration/case_creation.py b/examples/gwk_compressor_casegeneration/case_creation.py index 690bc713..6afd2caf 100644 --- a/examples/gwk_compressor_casegeneration/case_creation.py +++ b/examples/gwk_compressor_casegeneration/case_creation.py @@ -4,8 +4,10 @@ import shutil from pathlib import Path import copy import re +import warnings -from ntrfc.utils.dictionaries.dict_utils import nested_dict_pairs_iterator, setInDict +from ntrfc.utils.dictionaries.dict_utils import setInDict +from ntrfc.utils.dictionaries.dict_utils import nested_dict_pairs_iterator TEMPLATEDIR = r"D:\CodingProjects\NTRfC\ntrfc\database\case_templates" path_to_sim=r"D:\CodingProjects\NTRfC\examples\gwk_compressor_casegeneration\01_case" @@ -33,6 +35,9 @@ def find_vars_opts(case_structure, sign, all_pairs, path_to_sim): # allowing names like JOB_NUMBERS, only capital letters and underlines - no digits, no whitespaces datadict = copy.deepcopy(case_structure) varsignature = r"<PLACEHOLDER [A-Z]{3,}(_{1,1}[A-Z]{3,}){,} PLACEHOLDER>".replace(r'PLACEHOLDER', sign) + #int + #float + #string siglim = (len(sign)+2, -(len(sign)+2)) for pair in all_pairs: @@ -110,8 +115,21 @@ def check_settings_necessarities(case_structure, settings_dict): used.append(variable) return defined, undefined, used, unused - -def create_case_fromtemplate(template, settings, path_to_sim): +def inplace_change(filename, old_string, new_string): + # Safely read the input filename using 'with' + with open(filename) as f: + s = f.read() + if old_string not in s: + print('"{old_string}" not found in {filename}.'.format(**locals())) + return + + # Safely write the changed content, if found in the file + with open(filename, 'w') as f: + print('Changing "{old_string}" to "{new_string}" in {filename}'.format(**locals())) + s = s.replace(old_string, new_string) + f.write(s) + +def copy_template(output, template, paras): """ :param template: str - template-name @@ -124,31 +142,25 @@ def create_case_fromtemplate(template, settings, path_to_sim): assert found, "template unknown. check ntrfc.database.casetemplates directory" case_structure = get_directory_structure(os.path.join(TEMPLATEDIR, template)) - case_files = [i[:-1] for i in list(nested_dict_pairs_iterator(case_structure)) if os.path.isfile(os.path.join(TEMPLATEDIR,*list(i[:-1])))] variables = find_vars_opts(case_structure, "var", list(nested_dict_pairs_iterator(case_structure)),os.path.join(TEMPLATEDIR)) - for fpath in case_files: - filename = fpath[-1] - dirstructure = fpath[:-1] - if dirstructure == (): - dirstructure = "" - - template_fpath = os.path.join(TEMPLATEDIR, - *dirstructure, - filename) - create_simdirstructure(case_structure,path_to_sim) - sim_fpath = os.path.join(path_to_sim, *dirstructure) - - shutil.copyfile(template_fpath, os.path.join(sim_fpath,filename)) - - defined, undefined, used, unused = check_settings_necessarities(variables, settings) + defined, undefined, used, unused = check_settings_necessarities(variables, paras) print("found ", str(len(defined)), " defined parameters") print("found ", str(len(undefined)), " undefined parameters") print("used ", str(len(used)), " parameters") print("unused ", str(len(unused)), " parameters") + if len(undefined)>0: + warnings.warn("undefined variables") + warnings.warn(str(undefined)) + return -1 + if len(unused)>0: + warnings.warn("unused "+str(len(unused))) + warnings.warn(str(unused)) - -settings = {} -case_structure = create_case_fromtemplate('trace-compressor-cascade-ras', settings, path_to_sim) + for fpath in output: + template_fpath = os.path.join(TEMPLATEDIR,fpath) + shutil.copyfile(template_fpath, fpath) + for para in used: + inplace_change(fpath,para,paras[para]) diff --git a/examples/gwk_compressor_casegeneration/casesettings.yaml b/examples/gwk_compressor_casegeneration/casesettings.yaml index b70478af..076ae935 100644 --- a/examples/gwk_compressor_casegeneration/casesettings.yaml +++ b/examples/gwk_compressor_casegeneration/casesettings.yaml @@ -1,4 +1,34 @@ case_params: - case_type: trace-compressor-cascade-ras + case_type: "trace-compressor-cascade-ras" parameters: - haha : nope + TRACE_CONTROLINPUT : nope + MESHNAME: nope + OUTFLOW_PRESSURE: nope + GLOBAL_TOLERANCE: nope + LENGTH_YPER: nope + STAGPOINT_ANNOMALLYFIX: nope + QUASIUNSTEADYMODEL: nope + WAVINESSMODEL: nope + VERSION_TWONULLNULLNINE: nope + HEATFLUXMODEL: nope + PRANDTLTURBULENT: nope + ROTATIONAL_EFF: nope + TRANSITIONMODEL: nope + VGJMODEL: nope + ACCURACY_ORDER: nope + OUTLET_MFLOW: nope + OUTLET_RADIAL_EQUI: nope + INLET_TOTAL_PRESSURE: nope + VEL_YANGLE: nope + ABS_STAGN_TEMP: nope + TURBULENTINTENSITY: nope + TURBULENTLENGTHSCALE: nope + INLET_TEMP: nope + INLET_VELX: nope + INLET_VELY: nope + INLET_VELZ: nope + OUTFLOW_TEMP: nope + OUTFLOW_PRESSURE: nope + OUTFLOW_VELX: nope + OUTFLOW_VELY: nope + OUTFLOW_VELZ: nope diff --git a/examples/gwk_compressor_casegeneration/params.tsv b/examples/gwk_compressor_casegeneration/params.tsv new file mode 100644 index 00000000..14dee8cd --- /dev/null +++ b/examples/gwk_compressor_casegeneration/params.tsv @@ -0,0 +1,3 @@ +param_1 param_2 param_3 +1 2 3 +4 3 2 -- GitLab