__init__.py 4.7 KB
Newer Older
1 2 3
# -*- coding: utf-8 -*-
# Copyright (C) 2013 Centre de données Astrophysiques de Marseille
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
Yannick Roehlly's avatar
Yannick Roehlly committed
4
# Author: Yannick Roehlly
5 6

"""
Yannick Roehlly's avatar
Yannick Roehlly committed
7 8 9 10 11 12
Save fluxes analysis module
===========================

This module does not perform a statistical analysis. It computes and save the
fluxes in a set of filters for all the possible combinations of input SED
parameters.
13 14 15 16

The data file is used only to get the list of fluxes to be computed.

"""
17 18 19 20 21
import ctypes
import multiprocessing as mp
from multiprocessing.sharedctypes import RawArray
import time

22 23
import numpy as np

24 25 26 27
from .. import AnalysisModule
from ..utils import ParametersHandler, backup_dir, save_fluxes
from ...utils import read_table
from ...warehouse import SedWarehouse
28 29
from .workers import init_fluxes as init_worker_fluxes
from .workers import fluxes as worker_fluxes
30

31

Yannick Roehlly's avatar
Yannick Roehlly committed
32
class SaveFluxes(AnalysisModule):
Yannick Roehlly's avatar
Yannick Roehlly committed
33 34
    """Save fluxes analysis module

35 36 37 38 39
    This module saves a table containing all the parameters and desired fluxes
    for all the computed models.

    """

40
    parameter_list = dict([
41 42 43 44 45 46
        ("variables", (
            "array of strings",
            "List of variables to be saved. If the list is left empty, all"
            "variables will be saved.",
            ['']
        )),
47
        ("output_file", (
48
            "string",
Médéric Boquien's avatar
Médéric Boquien committed
49 50
            "Name of the output file that contains the parameters of the "
            "model(s) and the flux densities in the bands",
51
            "computed_fluxes.txt"
52
        )),
53 54
        ("save_sed", (
            "boolean",
55
            "If True, save the generated spectrum for each model.",
56 57
            "False"
        )),
58
        ("output_format", (
59 60 61
            "string",
            "Format of the output file. Any format supported by astropy.table "
            "e.g. votable or ascii.",
62
            "ascii"
63 64
        ))
    ])
65

66
    def process(self, conf):
67 68 69 70 71 72 73 74
        """Process with the savedfluxes analysis.

        All the possible theoretical SED are created and the fluxes in the
        filters from the column_list are computed and saved to a table,
        alongside the parameter values.

        Parameters
        ----------
75 76
        conf: dictionary
            Contents of pcigale.ini in the form of a dictionary
77 78
        """

79 80
        # Rename the output directory if it exists
        backup_dir()
81 82 83 84 85 86 87 88
        creation_modules = conf['creation_modules']
        creation_modules_params = conf['creation_modules_params']
        out_file = conf['analysis_method_params']["output_file"]
        out_format = conf['analysis_method_params']["output_format"]
        save_sed = conf['analysis_method_params']["save_sed"].lower() == "true"

        filters = [name for name in conf['column_list'] if not
                   name.endswith('_err')]
89 90
        n_filters = len(filters)

91 92 93 94 95 96 97 98
        # The parameters handler allows us to retrieve the models parameters
        # from a 1D index. This is useful in that we do not have to create
        # a list of parameters as they are computed on-the-fly. It also has
        # nice goodies such as finding the index of the first parameter to
        # have changed between two indices or the number of models.
        params = ParametersHandler(creation_modules, creation_modules_params)
        n_params = params.size

99
        if conf['analysis_method_params']["variables"] == '':
100 101 102 103 104 105
            # Retrieve an arbitrary SED to obtain the list of output parameters
            warehouse = SedWarehouse()
            sed = warehouse.get_sed(creation_modules, params.from_index(0))
            info = list(sed.info.keys())
            del warehouse, sed
        else:
106
            info = conf['analysis_method_params']["variables"]
107
            n_info = len(info)
108
        info.sort()
109
        n_info = len(info)
110

111
        model_fluxes = (RawArray(ctypes.c_double, n_params * n_filters),
112
                        (n_params, n_filters))
113
        model_parameters = (RawArray(ctypes.c_double, n_params * n_info),
Médéric Boquien's avatar
Médéric Boquien committed
114
                            (n_params, n_info))
115

116
        initargs = (params, filters, save_sed, info, model_fluxes,
117
                    model_parameters, time.time(), mp.Value('i', 0))
118
        if conf['cores'] == 1:  # Do not create a new process
119 120 121 122
            init_worker_fluxes(*initargs)
            for idx in range(n_params):
                worker_fluxes(idx)
        else:  # Analyse observations in parallel
123 124
            with mp.Pool(processes=conf['cores'],
                         initializer=init_worker_fluxes,
125 126 127 128 129
                         initargs=initargs) as pool:
                pool.map(worker_fluxes, range(n_params))

        save_fluxes(model_fluxes, model_parameters, filters, info, out_file,
                    out_format=out_format)
Yannick Roehlly's avatar
Yannick Roehlly committed
130 131 132

# AnalysisModule to be returned by get_module
Module = SaveFluxes