Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to set name of campaign output #111

Merged
merged 4 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions cosmosis/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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"):
Expand All @@ -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.

Expand All @@ -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
-------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -514,15 +522,15 @@ 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", {})

# 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

Expand Down
1 change: 1 addition & 0 deletions cosmosis/test/campaign.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
output_dir: output/campaign-test
output_name: my_project_{name}_suite1
include: []

runs:
Expand Down
2 changes: 2 additions & 0 deletions cosmosis/test/test_campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.txt"

for name in runs:
print(name)

Expand Down