chi2.py 2.63 KB
Newer Older
1 2 3 4 5 6 7
# -*- coding: utf-8 -*-
# Copyright (C) 2013 Centre de données Astrophysiques de Marseille
# Copyright (C) 2013-2014 Yannick Roehlly
# Copyright (C) 2013 Institute of Astronomy
# Copyright (C) 2014 Laboratoire d'Astrophysique de Marseille
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly, Médéric Boquien & Denis Burgarella
8 9 10

import glob
from itertools import product
11
from os import path
12 13 14 15 16 17 18 19

import matplotlib

matplotlib.use('Agg')
import matplotlib.pyplot as plt
import multiprocessing as mp
import numpy as np
from pcigale.utils import read_table
20
from pcigale.analysis_modules.utils import Counter
21 22 23 24 25 26 27 28 29


def pool_initializer(counter):
    """Initializer of the pool of processes to share variables between workers.
    Parameters
    ----------
    :param counter: Counter class object for the number of models plotted
    """
    global gbl_counter
30

31
    gbl_counter = counter
32 33


34
def chi2(config, format, outdir):
35 36
    """Plot the χ² values of analysed variables.
    """
37 38
    file = path.join(path.dirname(outdir), config.configuration['data_file'])
    input_data = read_table(file)
39 40 41 42
    chi2_vars = config.configuration['analysis_params']['variables']
    chi2_vars += [band for band in config.configuration['bands']
                  if band.endswith('_err') is False]

43
    items = list(product(input_data['id'], chi2_vars, [format], [outdir]))
44 45 46
    counter = Counter(len(items))
    with mp.Pool(processes=config.configuration['cores'], initializer=pool_initializer,
                 initargs=(counter,)) as pool:
47 48 49 50 51
        pool.starmap(_chi2_worker, items)
        pool.close()
        pool.join()


52
def _chi2_worker(obj_name, var_name, format, outdir):
53 54 55 56 57 58 59 60
    """Plot the reduced χ² associated with a given analysed variable

    Parameters
    ----------
    obj_name: string
        Name of the object.
    var_name: string
        Name of the analysed variable..
61 62
    outdir: string
        The absolute path to outdir
63 64

    """
65
    gbl_counter.inc()
66 67 68 69
    figure = plt.figure()
    ax = figure.add_subplot(111)

    var_name = var_name.replace('/', '_')
70
    fnames = glob.glob(f"{outdir}/{obj_name}_{var_name}_chi2-block-*.npy")
71 72 73 74 75 76 77 78
    for fname in fnames:
        data = np.memmap(fname, dtype=np.float64)
        data = np.memmap(fname, dtype=np.float64, shape=(2, data.size // 2))
        ax.scatter(data[1, :], data[0, :], color='k', s=.1)
    ax.set_xlabel(var_name)
    ax.set_ylabel("Reduced $\chi^2$")
    ax.set_ylim(0., )
    ax.minorticks_on()
79 80 81
    figure.suptitle(f"Reduced $\chi^2$ distribution of {var_name} for "
                    f"{obj_name}.")
    figure.savefig(f"{outdir}/{obj_name}_{var_name}_chi2.{format}")
82
    plt.close(figure)