__init__.py 4.34 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
from .. import AnalysisModule
from ..utils import ParametersHandler, backup_dir, save_fluxes
from ...utils import read_table
27
28
from .workers import init_fluxes as init_worker_fluxes
from .workers import fluxes as worker_fluxes
29

30

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

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

    """

39
    parameter_list = dict([
40
41
        ("variables", (
            "array of strings",
42
43
44
45
            "List of the physical properties to save. Leave empty to save all "
            "the physical properties (not recommended when there are many "
            "models).",
            None
46
        )),
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
        creation_modules = conf['creation_modules']
        creation_modules_params = conf['creation_modules_params']
83
84
85
        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"
86
87
88

        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
        info = conf['analysis_method_params']['variables']
100
        n_info = len(info)
101

102
        model_fluxes = (RawArray(ctypes.c_double, n_params * n_filters),
103
                        (n_params, n_filters))
104
        model_parameters = (RawArray(ctypes.c_double, n_params * n_info),
Médéric Boquien's avatar
Médéric Boquien committed
105
                            (n_params, n_info))
106

107
        initargs = (params, filters, save_sed, info, model_fluxes,
108
                    model_parameters, time.time(), mp.Value('i', 0))
109
        if conf['cores'] == 1:  # Do not create a new process
110
111
112
113
            init_worker_fluxes(*initargs)
            for idx in range(n_params):
                worker_fluxes(idx)
        else:  # Analyse observations in parallel
114
115
            with mp.Pool(processes=conf['cores'],
                         initializer=init_worker_fluxes,
116
117
118
119
120
                         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
121
122
123

# AnalysisModule to be returned by get_module
Module = SaveFluxes