chi2.py 2.77 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 21 22 23 24 25 26 27 28 29 30 31 32 33
from pcigale.analysis_modules.utils import Counter, nothread


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
    # Limit the number of threads to 1 if we use MKL in order to limit the
    # oversubscription of the CPU/RAM.
    nothread()
    gbl_counter = counter
34 35


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

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


54
def _chi2_worker(obj_name, var_name, outdir):
55 56 57 58 59 60 61 62
    """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..
63 64
    outdir: string
        The absolute path to outdir
65 66

    """
67
    gbl_counter.inc()
68 69 70 71
    figure = plt.figure()
    ax = figure.add_subplot(111)

    var_name = var_name.replace('/', '_')
72
    fnames = glob.glob("{}/{}_{}_chi2-block-*.npy".format(outdir, obj_name, var_name))
73 74 75 76 77 78 79 80 81 82
    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()
    figure.suptitle("Reduced $\chi^2$ distribution of {} for {}."
                    .format(var_name, obj_name))
83
    figure.savefig("{}/{}_{}_chi2.pdf".format(outdir, obj_name, var_name))
84
    plt.close(figure)