Commit 4826f8fc authored by Gregory Ashton's avatar Gregory Ashton

Resolve 345

Minor improvements to the handling of data saving to disk
1) Writes a result file before and post-processing to ensure data isn't
2) Internal rejig of how the logic of which result file to write is
3) Dumps the dynesty result object to disk (for most other samplers a
simple cache file is produced automatically)
parent b237c32e
Pipeline #54916 passed with stage
in 6 minutes and 33 seconds
......@@ -412,12 +412,17 @@ class Result(object):
outdir: str, optional
Path to the outdir. Default is the one stored in the result object.
extension: str, optional {json, hdf5}
Determines the method to use to store the data
extension: str, optional {json, hdf5, True}
Determines the method to use to store the data (if True defaults
to json)
gzip: bool, optional
If true, and outputing to a json file, this will gzip the resulting
file and add '.gz' to the file extension.
if extension is True:
extension = "json"
outdir = self._safe_outdir_creation(outdir, self.save_to_file)
file_name = result_file_name(outdir, self.label, extension, gzip)
......@@ -167,6 +167,10 @@ def run_sampler(likelihood, priors=None, label='label', outdir='outdir',
result = sampler.run_sampler()
# Initial save of the sampler in case of failure in post-processing
if save:
result.save_to_file(extension=save, gzip=gzip)
end_time =
result.sampling_time = (end_time - start_time).total_seconds()'Sampling time: {}'.format(end_time - start_time))
......@@ -188,12 +192,11 @@ def run_sampler(likelihood, priors=None, label='label', outdir='outdir',
result.samples_to_posterior(likelihood=likelihood, priors=result.priors,
if save == 'hdf5':
result.save_to_file(extension='hdf5')"Results saved to {}/".format(outdir))
elif save:
result.save_to_file(gzip=gzip)"Results saved to {}/".format(outdir))
if save:
# The overwrite here ensures we overwrite the initially stored data
result.save_to_file(overwrite=True, extension=save, gzip=gzip)
if plot:
result.plot_corner()"Summary of results:\n{}".format(result))
......@@ -189,7 +189,10 @@ class Dynesty(NestedSampler):
if self.kwargs["verbose"]:
# self.result.sampler_output = out
dynesty_result = "{}/{}_dynesty.pickle".format(self.outdir, self.label)
with open(dynesty_result, 'wb') as file:
pickle.dump(out, file)
weights = np.exp(out['logwt'] - out['logz'][-1])
nested_samples = DataFrame(
out.samples, columns=self.search_parameter_keys)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment