Commit bc725416 authored by Gregory Ashton's avatar Gregory Ashton
Browse files

Unify the creation of the outdir

Previously, outdir was checked and if-not-existing created in a few
different places in two different ways. This (a) makes them all use the
same method (namely the utils function) and (b) places a
check-and-creation call before every tupak output. This will ensure that
changes elsewhere in the code won't cause bugs in the future. Also, if
you choose not to save anything, it won't just make an empty directory.
parent d3f16893
Pipeline #26429 failed with stage
in 35 seconds
......@@ -9,6 +9,7 @@ import scipy.stats
import os
from tupak.core.utils import logger
from tupak.core import utils
class PriorSet(dict):
......@@ -39,6 +40,7 @@ class PriorSet(dict):
Output file naming scheme
"""
utils.check_directory_exists_and_if_not_mkdir(outdir)
prior_file = os.path.join(outdir, "{}_prior.txt".format(label))
logger.debug("Writing priors to {}".format(prior_file))
with open(prior_file, "w") as outfile:
......
......@@ -161,8 +161,7 @@ class Result(dict):
def save_to_file(self):
""" Writes the Result to a deepdish h5 file """
file_name = result_file_name(self.outdir, self.label)
if os.path.isdir(self.outdir) is False:
os.makedirs(self.outdir)
utils.check_directory_exists_and_if_not_mkdir(self.outdir)
if os.path.isfile(file_name):
logger.debug(
'Renaming existing file {} to {}.old'.format(file_name,
......@@ -307,6 +306,7 @@ class Result(dict):
fig = corner.corner(xs, **kwargs)
if save:
utils.check_directory_exists_and_if_not_mkdir(self.outdir)
filename = '{}/{}_corner.png'.format(self.outdir, self.label)
logger.debug('Saving corner plot to {}'.format(filename))
fig.savefig(filename, dpi=dpi)
......@@ -339,6 +339,7 @@ class Result(dict):
fig.tight_layout()
filename = '{}/{}_walkers.png'.format(self.outdir, self.label)
logger.debug('Saving walkers plot to {}'.format('filename'))
utils.check_directory_exists_and_if_not_mkdir(self.outdir)
fig.savefig(filename)
def samples_to_posterior(self, likelihood=None, priors=None,
......
......@@ -97,9 +97,6 @@ class Sampler(object):
self._log_summary_for_sampler()
if os.path.isdir(outdir) is False:
os.makedirs(outdir)
self.result = self._initialise_result()
@property
......@@ -1013,7 +1010,6 @@ def run_sampler(likelihood, priors=None, label='label', outdir='outdir',
priors.fill_priors(likelihood, default_priors_file=default_priors_file)
if save:
utils.check_directory_exists_and_if_not_mkdir(outdir)
priors.write_to_file(outdir, label)
if implemented_samplers.__contains__(sampler.title()):
......
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