Adding "--outdir" command line option.

Now it is possible to choose the folder that contains the output files (the "out" folder). It is expected that the default configuration file pcigale.ini be sibling of the "out" folder.
parent 1c70cfd5
......@@ -8,6 +8,7 @@
import argparse
import sys
from os import path
import multiprocessing as mp
from pcigale.session.configuration import Configuration
......@@ -16,7 +17,7 @@ from .plot_types.pdf import pdf as pdf_action
from .plot_types.sed import sed as sed_action
from .plot_types.mock import mock as mock_action
__version__ = "0.1-alpha"
__version__ = "0.2-alpha"
# Wavelength limits (restframe) when plotting the best SED.
PLOT_L_MIN = 0.1
......@@ -39,41 +40,40 @@ def main():
subparsers = parser.add_subparsers(help="List of commands")
pdf_parser = subparsers.add_parser('pdf', help=pdf_action.__doc__)
pdf_parser.add_argument('--outdir', dest='outdir', default='out')
pdf_parser.set_defaults(parser='pdf')
chi2_parser = subparsers.add_parser('chi2', help=chi2_action.__doc__)
chi2_parser.add_argument('--outdir', dest='outdir', default='out')
chi2_parser.set_defaults(parser='chi2')
sed_parser = subparsers.add_parser('sed', help=sed_action.__doc__)
sed_parser.add_argument('--type', default='mJy')
sed_parser.add_argument('--nologo', action='store_true')
sed_parser.add_argument('--best-results-file', dest='best_results_file',
default='out/results.fits')
sed_parser.add_argument('--outdir', dest='outdir', default='out')
sed_parser.set_defaults(parser='sed')
mock_parser = subparsers.add_parser('mock', help=mock_action.__doc__)
mock_parser.add_argument('--nologo', action='store_true')
mock_parser.add_argument('--best-results-file', dest='best_results_file',
default='out/results.fits')
mock_parser.add_argument('--mock-results-file', dest='mock_results_file',
default='out/results_mock.fits')
mock_parser.add_argument('--outdir', dest='outdir', default='out')
mock_parser.set_defaults(parser='mock')
args = parser.parse_args()
outdir = path.abspath(args.outdir)
if args.config_file:
config = Configuration(args.config_file)
else:
config = Configuration()
config = Configuration(path.join(path.dirname(outdir), 'pcigale.ini'))
if len(sys.argv) == 1:
parser.print_usage()
else:
if args.parser == 'chi2':
chi2_action(config)
chi2_action(config, outdir)
elif args.parser == 'pdf':
pdf_action(config)
pdf_action(config, outdir)
elif args.parser == 'sed':
sed_action(config, args.type, args.best_results_file, args.nologo)
sed_action(config, args.type, args.nologo, outdir)
elif args.parser == 'mock':
mock_action(config, args.best_results_file, args.mock_results_file, args.nologo)
mock_action(config, args.nologo, outdir)
import glob
from itertools import product
from os import path
import matplotlib
......@@ -11,22 +12,22 @@ import numpy as np
from pcigale.utils import read_table
def chi2(config):
def chi2(config, outdir):
"""Plot the χ² values of analysed variables.
"""
input_data = read_table(config.configuration['data_file'])
input_data = read_table(path.join(path.dirname(outdir), config.configuration['data_file']))
chi2_vars = config.configuration['analysis_params']['variables']
chi2_vars += [band for band in config.configuration['bands']
if band.endswith('_err') is False]
with mp.Pool(processes=config.configuration['cores']) as pool:
items = product(input_data['id'], chi2_vars)
items = product(input_data['id'], chi2_vars, outdir)
pool.starmap(_chi2_worker, items)
pool.close()
pool.join()
def _chi2_worker(obj_name, var_name):
def _chi2_worker(obj_name, var_name, outdir):
"""Plot the reduced χ² associated with a given analysed variable
Parameters
......@@ -35,13 +36,15 @@ def _chi2_worker(obj_name, var_name):
Name of the object.
var_name: string
Name of the analysed variable..
outdir: string
The absolute path to outdir
"""
figure = plt.figure()
ax = figure.add_subplot(111)
var_name = var_name.replace('/', '_')
fnames = glob.glob("out/{}_{}_chi2-block-*.npy".format(obj_name, var_name))
fnames = glob.glob("{}/{}_{}_chi2-block-*.npy".format(outdir, obj_name, var_name))
for fname in fnames:
data = np.memmap(fname, dtype=np.float64)
data = np.memmap(fname, dtype=np.float64, shape=(2, data.size // 2))
......@@ -52,5 +55,5 @@ 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/{}_{}_chi2.pdf".format(obj_name, var_name))
figure.savefig("{}/{}_{}_chi2.pdf".format(outdir, obj_name, var_name))
plt.close(figure)
from astropy.table import Table
import matplotlib
import sys
from os import path
matplotlib.use('Agg')
import matplotlib.pyplot as plt
......@@ -9,10 +10,15 @@ import numpy as np
import pkg_resources
from scipy import stats
# Name of the file containing the best models information
BEST_RESULTS = "results.fits"
MOCK_RESULTS = "results_mock.fits"
def mock(config, mock_results_file, best_results_file, nologo):
def mock(config, nologo, outdir):
"""Plot the comparison of input/output values of analysed variables.
"""
best_results_file = path.abspath(path.join(outdir, BEST_RESULTS))
mock_results_file = path.abspath(path.join(outdir, MOCK_RESULTS))
try:
exact = Table.read(best_results_file)
......@@ -36,7 +42,7 @@ def mock(config, mock_results_file, best_results_file, nologo):
logo = False if nologo else plt.imread(pkg_resources.resource_filename(__name__,
"../resources/CIGALE.png"))
arguments = ((exact["best."+param], estimated["bayes."+param], param, logo)
arguments = ((exact["best."+param], estimated["bayes."+param], param, logo, outdir)
for param in params)
with mp.Pool(processes=config.configuration['cores']) as pool:
......@@ -45,7 +51,7 @@ def mock(config, mock_results_file, best_results_file, nologo):
pool.join()
def _mock_worker(exact, estimated, param, logo):
def _mock_worker(exact, estimated, param, logo, outdir):
"""Plot the exact and estimated values of a parameter for the mock analysis
Parameters
......@@ -58,6 +64,8 @@ def _mock_worker(exact, estimated, param, logo):
Name of the parameter
nologo: boolean
Do not add the logo when set to true.
outdir: string
The absolute path to outdir
"""
......@@ -87,6 +95,6 @@ def _mock_worker(exact, estimated, param, logo):
plt.figimage(logo, 510, 55, origin='upper', zorder=10, alpha=1)
plt.tight_layout()
plt.savefig('out/mock_{}.pdf'.format(param))
plt.savefig('{}/mock_{}.pdf'.format(outdir, param))
plt.close()
......@@ -2,6 +2,7 @@
import glob
from itertools import product
import matplotlib
from os import path
matplotlib.use('Agg')
import matplotlib.pyplot as plt
......@@ -10,22 +11,22 @@ import numpy as np
from pcigale.utils import read_table
def pdf(config):
def pdf(config, outdir):
"""Plot the PDF of analysed variables.
"""
input_data = read_table(config.configuration['data_file'])
input_data = read_table(path.join(path.dirname(outdir), config.configuration['data_file']))
pdf_vars = config.configuration['analysis_params']['variables']
pdf_vars += [band for band in config.configuration['bands']
if band.endswith('_err') is False]
with mp.Pool(processes=config.configuration['cores']) as pool:
items = product(input_data['id'], pdf_vars)
items = product(input_data['id'], pdf_vars, outdir)
pool.starmap(_pdf_worker, items)
pool.close()
pool.join()
def _pdf_worker(obj_name, var_name):
def _pdf_worker(obj_name, var_name, outdir):
"""Plot the PDF associated with a given analysed variable
Parameters
......@@ -34,15 +35,17 @@ def _pdf_worker(obj_name, var_name):
Name of the object.
var_name: string
Name of the analysed variable..
outdir: string
The absolute path to outdir
"""
var_name = var_name.replace('/', '_')
if var_name.endswith('_log'):
fnames = glob.glob("out/{}_{}_chi2-block-*.npy".format(obj_name,
fnames = glob.glob("{}/{}_{}_chi2-block-*.npy".format(outdir, obj_name,
var_name[:-4]))
log = True
else:
fnames = glob.glob("out/{}_{}_chi2-block-*.npy".format(obj_name,
fnames = glob.glob("{}/{}_{}_chi2-block-*.npy".format(outdir, obj_name,
var_name))
log = False
likelihood = []
......@@ -86,5 +89,5 @@ 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/{}_{}_pdf.pdf".format(obj_name, var_name))
figure.savefig("{}/{}_{}_pdf.pdf".format(outdir, obj_name, var_name))
plt.close(figure)
......@@ -10,7 +10,7 @@ matplotlib.use('Agg')
import matplotlib.pyplot as plt
import multiprocessing as mp
import numpy as np
import os
from os import path
import pkg_resources
from scipy.constants import c
from pcigale.data import Database
......@@ -19,33 +19,37 @@ import matplotlib.gridspec as gridspec
logger = logging.getLogger(__name__)
# Name of the file containing the best models information
BEST_RESULTS = "results.fits"
MOCK_RESULTS = "results_mock.fits"
# Wavelength limits (restframe) when plotting the best SED.
PLOT_L_MIN = 0.1
PLOT_L_MAX = 5e5
def sed(config, sed_type, best_results_file, nologo):
def sed(config, sed_type, nologo, outdir):
"""Plot the best SED with associated observed and modelled fluxes.
"""
obs = read_table(config.configuration['data_file'])
mod = Table.read(best_results_file)
obs = read_table(path.join(path.dirname(outdir), config.configuration['data_file']))
mod = Table.read(path.join(outdir, BEST_RESULTS))
with Database() as base:
filters = OrderedDict([(name, base.get_filter(name))
for name in config.configuration['bands']
if not (name.endswith('_err') or name.startswith('line')) ])
if not (name.endswith('_err') or name.startswith('line'))])
logo = False if nologo else plt.imread(pkg_resources.resource_filename(__name__,
"../resources/CIGALE.png"))
with mp.Pool(processes=config.configuration['cores']) as pool:
pool.starmap(_sed_worker, zip(obs, mod, repeat(filters),
repeat(sed_type), repeat(logo)))
repeat(sed_type), repeat(logo), repeat(outdir)))
pool.close()
pool.join()
def _sed_worker(obs, mod, filters, sed_type, logo):
def _sed_worker(obs, mod, filters, sed_type, logo, outdir):
"""Plot the best SED with the associated fluxes in bands
Parameters
......@@ -65,13 +69,15 @@ def _sed_worker(obs, mod, filters, sed_type, logo):
(M, N, 3) for RGB images.
(M, N, 4) for RGBA images.
Do not add the logo when set to False.
outdir: string
The absolute path to outdir
"""
logger.debug("Starting worker")
id_best_model_file = path.join(outdir, '{}_best_model.fits'.format(obs['id']))
if path.isfile(id_best_model_file):
if os.path.isfile("out/{}_best_model.fits".format(obs['id'])):
sed = Table.read("out/{}_best_model.fits".format(obs['id']))
sed = Table.read(id_best_model_file)
filters_wl = np.array([filt.pivot_wavelength
for filt in filters.values()])
......@@ -269,7 +275,7 @@ def _sed_worker(obs, mod, filters, sed_type, logo):
figure.figimage(logo, 12, figure_height - 67, origin='upper', zorder=0,
alpha=1)
figure.savefig("out/{}_best_model.pdf".format(obs['id']))
figure.savefig(path.join(outdir, '{}_best_model.pdf'.format(obs['id'])))
plt.close(figure)
else:
logger.error("No valid best SED found for {}. No plot created.".format(obs['id']))
......
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