Commit 5e8fc046 authored by Médéric Boquien's avatar Médéric Boquien

The output directory is a bit of a mess, with it being defined in different...

The output directory is a bit of a mess, with it being defined in different places. As we never make use of the possibility of changing it, let's simply hardcode it to out/. It makes filenames slightly easier to read too.
parent e5444dd8
......@@ -12,8 +12,6 @@ import shutil
import numpy as np
from astropy.table import Column
OUT_DIR = 'out/'
class AnalysisModule(object):
"""Abstract class, the pCigale analysis modules are based on.
"""
......@@ -53,16 +51,16 @@ class AnalysisModule(object):
"""
raise NotImplementedError()
def prepare_dirs(self, directory=OUT_DIR):
def prepare_dirs(self):
# Create a new out/ directory and move existing one if needed
if os.path.exists(directory):
name = datetime.now().strftime("%Y%m%d%H%M") + '_' + directory
os.rename(directory, name)
print("The {} directory was renamed to {}".format(directory, name))
os.mkdir(directory)
shutil.copy('pcigale.ini', directory)
shutil.copy('pcigale.ini.spec', directory)
if os.path.exists('out/'):
name = datetime.now().strftime("%Y%m%d%H%M") + '_out/'
os.rename('out/', name)
print("The out/ directory was renamed to {}".format(name))
os.mkdir('out/')
shutil.copy('pcigale.ini', 'out/')
shutil.copy('pcigale.ini.spec', 'out/')
def process(self, configuration):
"""Process with the analysis
......
......@@ -11,8 +11,6 @@ import numpy as np
from scipy import optimize
from scipy.special import erf
from ..utils import OUT_DIR
log.setLevel('ERROR')
......@@ -29,7 +27,7 @@ def save_best_sed(obsid, sed, norm):
Normalisation factor to scale the scale to the observations
"""
sed.to_fits(OUT_DIR + "{}".format(obsid), mass=norm)
sed.to_fits("out/{}".format(obsid), mass=norm)
def _save_pdf(obsid, name, model_variable, likelihood):
......@@ -80,7 +78,7 @@ def _save_pdf(obsid, name, model_variable, likelihood):
Column(pdf_grid, name=name),
Column(pdf_prob, name="probability density")
))
table.write(OUT_DIR + "{}_{}_pdf.fits".format(obsid, name))
table.write("out/{}_{}_pdf.fits".format(obsid, name))
def save_pdf(obsid, names, mass_proportional, model_variables, scaling,
......@@ -133,7 +131,7 @@ def _save_chi2(obsid, name, model_variable, chi2):
"""
table = Table((Column(model_variable, name=name),
Column(chi2, name="chi2")))
table.write(OUT_DIR + "{}_{}_chi2.fits".format(obsid, name))
table.write("out/{}_{}_chi2.fits".format(obsid, name))
def save_chi2(obsid, names, mass_proportional, model_variables, scaling, chi2):
......@@ -234,9 +232,9 @@ def save_results(filename, obsid, bayes_variables, bayes_mean, bayes_std, chi2,
table.add_column(Column(np_fluxes[:, idx], name="best."+name,
unit='mJy'))
table.write(OUT_DIR+filename+".txt", format='ascii.fixed_width',
table.write('out/'+filename+".txt", format='ascii.fixed_width',
delimiter=None)
table.write(OUT_DIR+filename+".fits", format='fits')
table.write('out/'+filename+".fits", format='fits')
def dchi2_over_ds2(s, obs_fluxes, obs_errors, mod_fluxes):
......
......@@ -10,7 +10,6 @@ import time
import numpy as np
from ...warehouse import SedWarehouse
from ..utils import OUT_DIR
from ..utils import nothread
......@@ -98,7 +97,7 @@ def fluxes(idx):
gbl_variables])
if gbl_save_sed is True:
sed.to_fits(OUT_DIR + "{}".format(idx))
sed.to_fits("out/{}".format(idx))
with gbl_n_computed.get_lock():
gbl_n_computed.value += 1
n_computed = gbl_n_computed.value
......
......@@ -13,11 +13,7 @@ from astropy.table import Table, Column
log.setLevel('ERROR')
# Directory where the output files are stored
OUT_DIR = "out/"
def save_fluxes(model_fluxes, model_parameters, filters, names,
directory=OUT_DIR):
def save_fluxes(model_fluxes, model_parameters, filters, names):
"""Save fluxes and associated parameters into a table.
Parameters
......@@ -30,8 +26,6 @@ def save_fluxes(model_fluxes, model_parameters, filters, names,
Contains the filter names.
names: List
Contains the parameters names.
directory: str
Directory under which the file should be saved.
"""
out_fluxes = np.ctypeslib.as_array(model_fluxes[0])
......@@ -46,9 +40,9 @@ def save_fluxes(model_fluxes, model_parameters, filters, names,
out_table.add_column(Column(np.arange(model_fluxes[1][0]), name='id'),
index=0)
out_table.write("{}/computed_fluxes.fits".format(directory))
out_table.write("{}/computed_fluxes.txt".format(directory),
format='ascii.fixed_width', delimiter=None)
out_table.write("out/computed_fluxes.fits")
out_table.write("out/computed_fluxes.txt", format='ascii.fixed_width',
delimiter=None)
def nothread():
......
......@@ -33,8 +33,6 @@ __version__ = "0.1-alpha"
# Name of the file containing the best models information
BEST_RESULTS = "results.fits"
MOCK_RESULTS = "results_mock.fits"
# Directory where the output files are stored
OUT_DIR = "out/"
# Wavelength limits (restframe) when plotting the best SED.
PLOT_L_MIN = 0.1
PLOT_L_MAX = 5e5
......@@ -51,9 +49,8 @@ def _chi2_worker(obj_name, var_name):
Name of the analysed variable..
"""
if os.path.isfile(OUT_DIR + "{}_{}_chi2.fits".format(obj_name, var_name)):
chi2 = Table.read(OUT_DIR + "{}_{}_chi2.fits".format(obj_name,
var_name))
if os.path.isfile("out/{}_{}_chi2.fits".format(obj_name, var_name)):
chi2 = Table.read("out/{}_{}_chi2.fits".format(obj_name, var_name))
figure = plt.figure()
ax = figure.add_subplot(111)
ax.scatter(chi2[var_name], chi2['chi2'], color='k', s=.1)
......@@ -63,7 +60,7 @@ def _chi2_worker(obj_name, var_name):
ax.minorticks_on()
figure.suptitle("Reduced $\chi^2$ distribution of {} for {}."
.format(var_name, obj_name))
figure.savefig(OUT_DIR + "{}_{}_chi2.pdf".format(obj_name, var_name))
figure.savefig("out/{}_{}_chi2.pdf".format(obj_name, var_name))
plt.close(figure)
else:
print("No chi² found for {}. No plot created.".format(obj_name))
......@@ -80,8 +77,8 @@ def _pdf_worker(obj_name, var_name):
Name of the analysed variable..
"""
if os.path.isfile(OUT_DIR + "{}_{}_pdf.fits".format(obj_name, var_name)):
pdf = Table.read(OUT_DIR + "{}_{}_pdf.fits".format(obj_name, var_name))
if os.path.isfile("out/{}_{}_pdf.fits".format(obj_name, var_name)):
pdf = Table.read("out/{}_{}_pdf.fits".format(obj_name, var_name))
figure = plt.figure()
ax = figure.add_subplot(111)
ax.plot(pdf[var_name], pdf['probability density'], color='k')
......@@ -90,7 +87,7 @@ def _pdf_worker(obj_name, var_name):
ax.minorticks_on()
figure.suptitle("Probability distribution function of {} for {}"
.format(var_name, obj_name))
figure.savefig(OUT_DIR + "{}_{}_pdf.pdf".format(obj_name, var_name))
figure.savefig("out/{}_{}_pdf.pdf".format(obj_name, var_name))
plt.close(figure)
else:
print("No PDF found for {}. No plot created.".format(obj_name))
......@@ -115,9 +112,9 @@ def _sed_worker(obs, mod, filters, sed_type, nologo):
"""
if os.path.isfile(OUT_DIR + "{}_best_model.fits".format(obs['id'])):
if os.path.isfile("out/{}_best_model.fits".format(obs['id'])):
sed = Table.read(OUT_DIR + "{}_best_model.fits".format(obs['id']))
sed = Table.read("out/{}_best_model.fits".format(obs['id']))
filters_wl = np.array([filt.effective_wavelength
for filt in filters.values()])
......@@ -308,7 +305,7 @@ def _sed_worker(obs, mod, filters, sed_type, nologo):
"data/CIGALE.png"))
figure.figimage(image, 75, 330, origin='upper', zorder=10,
alpha=1)
figure.savefig(OUT_DIR + "{}_best_model.pdf".format(obs['id']))
figure.savefig("out/{}_best_model.pdf".format(obs['id']))
plt.close(figure)
else:
print("No valid best SED found for {}. No plot created.".
......@@ -360,7 +357,7 @@ def _mock_worker(exact, estimated, param, nologo):
plt.figimage(image, 510, 55, origin='upper', zorder=10, alpha=1)
plt.tight_layout()
plt.savefig(OUT_DIR + 'mock_{}.pdf'.format(param))
plt.savefig('out/mock_{}.pdf'.format(param))
plt.close()
......@@ -395,7 +392,7 @@ def sed(config, sed_type, nologo):
"""Plot the best SED with associated observed and modelled fluxes.
"""
obs = read_table(config.configuration['data_file'])
mod = Table.read(OUT_DIR + BEST_RESULTS)
mod = Table.read('out/' + BEST_RESULTS)
with Database() as base:
filters = OrderedDict([(name, base.get_filter(name))
......@@ -414,15 +411,15 @@ def mock(config, nologo):
"""
try:
exact = Table.read(OUT_DIR + BEST_RESULTS)
exact = Table.read('out/' + BEST_RESULTS)
except FileNotFoundError:
print("Best models file {} not found.".format(OUT_DIR + BEST_RESULTS))
print("Best models file {} not found.".format('out/' + BEST_RESULTS))
sys.exit(1)
try:
estimated = Table.read(OUT_DIR + MOCK_RESULTS)
estimated = Table.read('out/' + MOCK_RESULTS)
except FileNotFoundError:
print("Mock models file {} not found.".format(OUT_DIR + MOCK_RESULTS))
print("Mock models file {} not found.".format('out/' + MOCK_RESULTS))
sys.exit(1)
params = config.configuration['analysis_params']['variables']
......
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