From 09ddacd4e17cd51e1c1701fb991724e0373f12ba Mon Sep 17 00:00:00 2001 From: Joe Zuntz Date: Thu, 4 Jan 2024 11:36:59 +0000 Subject: [PATCH 1/4] allow user to set name of campaign output --- bin/cosmosis-extract | 2 +- cosmosis/campaign.py | 22 +++++++++++++++------- cosmosis/main.py | 10 +++++++++- cosmosis/test/campaign.yml | 1 + cosmosis/test/test_campaign.py | 2 ++ 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/bin/cosmosis-extract b/bin/cosmosis-extract index fa7a1435..0fb36c6a 100755 --- a/bin/cosmosis-extract +++ b/bin/cosmosis-extract @@ -32,7 +32,7 @@ def extract_section(lines, section): return output_lines def save(lines, section, prefix): - filename = "{}_{}.ini".format(prefix, section).lower() + filename = "{}_{}.ini".format(prefix, section) open(filename,'w').writelines(lines) def main(chain, prefix): diff --git a/cosmosis/campaign.py b/cosmosis/campaign.py index 27e255f4..01b5d496 100644 --- a/cosmosis/campaign.py +++ b/cosmosis/campaign.py @@ -305,7 +305,7 @@ def temporary_environment(env): os.environ.clear() os.environ.update(original_environment) -def set_output_dir(params, name, output_dir): +def set_output_dir(params, name, output_dir, output_name): """ Modify a parameters file to set the output directory and file names to be based on the run name and output directory. @@ -318,7 +318,10 @@ def set_output_dir(params, name, output_dir): The name of the run output_dir : str The output directory to use - + output_name : str + The format string to use to generate the output file name, using {name} + for the run name. + Returns ------- None @@ -329,7 +332,8 @@ def set_output_dir(params, name, output_dir): if not params.has_section("test"): params.add_section("test") - params.set("output", "filename", os.path.join(output_dir, f"{name}.txt")) + output_name = output_name.format(name=name) + ".txt" + params.set("output", "filename", os.path.join(output_dir, output_name)) params.set("test", "save_dir", os.path.join(output_dir, name)) if params.has_section("multinest"): @@ -338,7 +342,7 @@ def set_output_dir(params, name, output_dir): params.set("polychord", "polychord_outfile_root", f"{name}.polychord") params.set("polychord", "base_dir", output_dir) -def build_run(name, run_info, runs, components, output_dir): +def build_run(name, run_info, runs, components, output_dir, output_name="{name}"): """ Generate a dictionary specifying a CosmoSIS run from a run_info dictioary. @@ -362,6 +366,9 @@ def build_run(name, run_info, runs, components, output_dir): A dictionary of previously built components output_dir : str The output directory to use + output_name : str + The format string to use to generate the output file name, using {name} + for the run name. Returns ------- @@ -433,7 +440,7 @@ def build_run(name, run_info, runs, components, output_dir): apply_updates(priors, prior_updates) apply_pipeline_updates(params, pipeline_updates) - set_output_dir(params, name, output_dir) + set_output_dir(params, name, output_dir, output_name) run["params"] = params run["values"] = values @@ -500,6 +507,7 @@ def parse_yaml_run_file(run_config): info = yaml.safe_load(f) output_dir = info.get("output_dir", ".") + output_name = info.get("output_name", "{name}") include = info.get("include", []) if isinstance(include, str): @@ -514,7 +522,7 @@ def parse_yaml_run_file(run_config): # But we override the output directory # of any imported runs with the one we have here for name, run in runs.items(): - set_output_dir(run["params"], name, output_dir) + set_output_dir(run["params"], name, output_dir, output_name) # deal with re-usable components components = info.get("components", {}) @@ -522,7 +530,7 @@ def parse_yaml_run_file(run_config): # Build the parameter, value, and prior objects for this run for run_dict in info["runs"]: name = run_dict["name"] - runs[name] = build_run(name, run_dict, runs, components, output_dir) + runs[name] = build_run(name, run_dict, runs, components, output_dir, output_name) return runs diff --git a/cosmosis/main.py b/cosmosis/main.py index 713d10db..8f065034 100755 --- a/cosmosis/main.py +++ b/cosmosis/main.py @@ -176,7 +176,8 @@ def setup_output(sampler_class, sampler_number, ini, pool, number_samplers, samp def run_cosmosis(ini, pool=None, pipeline=None, values=None, priors=None, override=None, - profile_mem=0, profile_cpu="", variables=None, only=None, output=None): + profile_mem=0, profile_cpu="", variables=None, only=None, output=None, + extra_metadata=None): """ Execute cosmosis. @@ -225,6 +226,9 @@ def run_cosmosis(ini, pool=None, pipeline=None, values=None, priors=None, overri output: None or cosmosis.Output If set, use this output object to save the results. If not set, create an output object from the ini file. + + extra_metadata: None or dict + If set, add these key-value pairs to the output metadata. """ no_subprocesses = os.environ.get("COSMOSIS_NO_SUBPROCESS", "") not in ["", "0"] @@ -384,6 +388,10 @@ def run_cosmosis(ini, pool=None, pipeline=None, values=None, priors=None, overri output = setup_output(sampler_class, sampler_number, ini, pool, number_samplers, sample_method, resume, output_original) + if output and (extra_metadata is not None): + for k, v in extra_metadata.items(): + output.metadata(k, v) + if is_root: print("****************************************************") diff --git a/cosmosis/test/campaign.yml b/cosmosis/test/campaign.yml index 77603e53..9bf79682 100644 --- a/cosmosis/test/campaign.yml +++ b/cosmosis/test/campaign.yml @@ -1,4 +1,5 @@ output_dir: output/campaign-test +output_name: my_project_{name}_suite1 include: [] runs: diff --git a/cosmosis/test/test_campaign.py b/cosmosis/test/test_campaign.py index 31b1d382..26445e11 100644 --- a/cosmosis/test/test_campaign.py +++ b/cosmosis/test/test_campaign.py @@ -80,6 +80,8 @@ def test_campaign_functions(): assert not runs["v4"]["priors"].has_option("parameters", "p2") + assert runs["v3"]["params"].get("output", "filename") == "output/campaign-test/my_project_v3_suite1" + for name in runs: print(name) From a820b7596d520a0a1d6c50bba1873e143f9b6a41 Mon Sep 17 00:00:00 2001 From: Joe Zuntz Date: Thu, 4 Jan 2024 11:43:53 +0000 Subject: [PATCH 2/4] fix test --- cosmosis/test/test_campaign.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cosmosis/test/test_campaign.py b/cosmosis/test/test_campaign.py index 26445e11..37d2e829 100644 --- a/cosmosis/test/test_campaign.py +++ b/cosmosis/test/test_campaign.py @@ -80,7 +80,7 @@ def test_campaign_functions(): assert not runs["v4"]["priors"].has_option("parameters", "p2") - assert runs["v3"]["params"].get("output", "filename") == "output/campaign-test/my_project_v3_suite1" + assert runs["v3"]["params"].get("output", "filename") == "output/campaign-test/my_project_v3_suite1.txt" for name in runs: print(name) From 8160d7c8a5a6ed748248c24e396ac3dd3fbd322f Mon Sep 17 00:00:00 2001 From: Joe Zuntz Date: Thu, 4 Jan 2024 11:56:59 +0000 Subject: [PATCH 3/4] Undo extra-metadata commit --- cosmosis/main.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/cosmosis/main.py b/cosmosis/main.py index 8f065034..713d10db 100755 --- a/cosmosis/main.py +++ b/cosmosis/main.py @@ -176,8 +176,7 @@ def setup_output(sampler_class, sampler_number, ini, pool, number_samplers, samp def run_cosmosis(ini, pool=None, pipeline=None, values=None, priors=None, override=None, - profile_mem=0, profile_cpu="", variables=None, only=None, output=None, - extra_metadata=None): + profile_mem=0, profile_cpu="", variables=None, only=None, output=None): """ Execute cosmosis. @@ -226,9 +225,6 @@ def run_cosmosis(ini, pool=None, pipeline=None, values=None, priors=None, overri output: None or cosmosis.Output If set, use this output object to save the results. If not set, create an output object from the ini file. - - extra_metadata: None or dict - If set, add these key-value pairs to the output metadata. """ no_subprocesses = os.environ.get("COSMOSIS_NO_SUBPROCESS", "") not in ["", "0"] @@ -388,10 +384,6 @@ def run_cosmosis(ini, pool=None, pipeline=None, values=None, priors=None, overri output = setup_output(sampler_class, sampler_number, ini, pool, number_samplers, sample_method, resume, output_original) - if output and (extra_metadata is not None): - for k, v in extra_metadata.items(): - output.metadata(k, v) - if is_root: print("****************************************************") From 98be95c30f0df8d2cf2115616fe155363881d85a Mon Sep 17 00:00:00 2001 From: Joe Zuntz Date: Thu, 4 Jan 2024 11:57:34 +0000 Subject: [PATCH 4/4] Undo extract commit --- bin/cosmosis-extract | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cosmosis-extract b/bin/cosmosis-extract index 0fb36c6a..fa7a1435 100755 --- a/bin/cosmosis-extract +++ b/bin/cosmosis-extract @@ -32,7 +32,7 @@ def extract_section(lines, section): return output_lines def save(lines, section, prefix): - filename = "{}_{}.ini".format(prefix, section) + filename = "{}_{}.ini".format(prefix, section).lower() open(filename,'w').writelines(lines) def main(chain, prefix):