Skip to content

Commit

Permalink
Merge pull request #172 from joezuntz/repeating-samplers
Browse files Browse the repository at this point in the history
Rename output files from repeated samplers
  • Loading branch information
joezuntz authored Feb 14, 2025
2 parents 7ecbf10 + 33df111 commit c171bc1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
23 changes: 18 additions & 5 deletions cosmosis/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def write_header_output(output, params, values, pipeline, values_override=None):
prior_ini.write(comment_wrapper)
output.comment("END_OF_PRIORS_INI")

def setup_output(sampler_class, sampler_number, ini, pool, number_samplers, sample_method, resume, output):
def setup_output(sampler_class, sampler_number, ini, pool, sample_methods, sample_method, resume, output):

output_original = output

Expand All @@ -139,17 +139,30 @@ def setup_output(sampler_class, sampler_number, ini, pool, number_samplers, samp
output_options['rank'] = pool.rank
output_options['parallel'] = pool.size

number_samplers = len(sample_methods)

#Give different output filenames to the different sampling steps
#Only change if this is not the last sampling step - the final
#one retains the name in the output file.
# Change, e.g. demo17.txt to demo17.fisher.txt
if ("filename" in output_options) and (sampler_number<number_samplers-1):

sampler_count = sample_methods.count(sample_method)
filename = output_options['filename']
filename, ext = os.path.splitext(filename)
filename += '.' + sampler_class.name
filename += ext
if sampler_count == 1:
filename += '.' + sampler_class.name
filename += ext
else:
sampler_repeat_index = 0
for i in range(sampler_number):
if sample_methods[i] == sample_method:
sampler_repeat_index += 1
filename += '.' + sampler_class.name + '.' + str(sampler_repeat_index)
filename += ext
output_options['filename'] = filename


if ("filename" in output_options):
print("* Saving output -> {}".format(output_options['filename']))

Expand Down Expand Up @@ -349,7 +362,7 @@ def run_cosmosis(ini, pool=None, pipeline=None, values=None, priors=None, overri
#Now that we have a sampler we know whether we will need an
#output file or not. By default new samplers do need one.
for sampler_number, (sampler_class, sample_method) in enumerate(
zip(sampler_classes, sample_methods)):
zip(sampler_classes, sample_methods[:])):
sampler_name = sampler_class.__name__[:-len("Sampler")].lower()

# The resume feature lets us restart from an existing file.
Expand Down Expand Up @@ -390,7 +403,7 @@ def run_cosmosis(ini, pool=None, pipeline=None, values=None, priors=None, overri
else:
print("* Running in serial mode.")

output = setup_output(sampler_class, sampler_number, ini, pool, number_samplers, sample_method, resume, output_original)
output = setup_output(sampler_class, sampler_number, ini, pool, sample_methods, sample_method, resume, output_original)

if is_root:
print("****************************************************")
Expand Down
11 changes: 8 additions & 3 deletions cosmosis/test/test_chaining.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ def test_sampler_chain():

with tempfile.TemporaryDirectory() as dirname:
values_file = f"{dirname}/values.txt"
maxlike_file = f"{dirname}/chain.maxlike.txt"

# we run maxlike twice and check the files are different
maxlike_file0 = f"{dirname}/chain.maxlike.0.txt"
maxlike_file1 = f"{dirname}/chain.maxlike.1.txt"
fisher_file = f"{dirname}/chain.fisher.txt"
emcee_file = f"{dirname}/chain.txt"
with open(values_file, "w") as values:
Expand All @@ -23,7 +26,7 @@ def test_sampler_chain():

params = {
('runtime', 'root'): os.path.split(os.path.abspath(__file__))[0],
('runtime', 'sampler'): "maxlike fisher emcee",
('runtime', 'sampler'): "maxlike maxlike fisher emcee",
("pipeline", "debug"): "T",
("pipeline", "modules"): "test1",
("pipeline", "extra_output"): "parameters/p3",
Expand All @@ -44,7 +47,9 @@ def test_sampler_chain():
data = np.loadtxt(fisher_file)
print(data.shape)

data = np.loadtxt(maxlike_file)
data = np.loadtxt(maxlike_file0)
print(data.shape)
data = np.loadtxt(maxlike_file1)
print(data.shape)

data = np.loadtxt(emcee_file)
Expand Down

0 comments on commit c171bc1

Please sign in to comment.