__init__.py 5.29 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
import ctypes
18
from datetime import datetime
19
20
21
22
23
24
from itertools import product, repeat
import os
import multiprocessing as mp
from multiprocessing.sharedctypes import RawArray
import time

25
26
import numpy as np

27
28
29
30
from .. import AnalysisModule
from ..utils import ParametersHandler, backup_dir, save_fluxes
from ...utils import read_table
from ...warehouse import SedWarehouse
31
32
from .workers import init_fluxes as init_worker_fluxes
from .workers import fluxes as worker_fluxes
33

34
35
36
# Limit the redshift to this number of decimals
REDSHIFT_DECIMALS = 2

37

Yannick Roehlly's avatar
Yannick Roehlly committed
38
class SaveFluxes(AnalysisModule):
Yannick Roehlly's avatar
Yannick Roehlly committed
39
40
    """Save fluxes analysis module

41
42
43
44
45
    This module saves a table containing all the parameters and desired fluxes
    for all the computed models.

    """

46
    parameter_list = dict([
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, data_file, column_list, creation_modules,
67
                creation_modules_params, parameters, cores):
68
69
70
71
72
73
74
75
76
77
78
79
        """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
        ----------
        data_file: string
            Name of the file containing the observations to fit.
        column_list: list of strings
            Name of the columns from the data file to use for the analysis.
80
        creation_modules: list of strings
81
82
            List of the module names (in the right order) to use for creating
            the SEDs.
83
        creation_modules_params: list of dictionaries
84
85
86
            List of the parameter dictionaries for each module.
        parameters: dictionary
            Dictionary containing the parameters.
87
88
        cores: integer
            Number of cores to run the analysis on
89
90
91

        """

92
93
94
        # Rename the output directory if it exists
        backup_dir()

95
96
        out_file = parameters["output_file"]
        out_format = parameters["output_format"]
97
        save_sed = parameters["save_sed"].lower() == "true"
98

99
        filters = [name for name in column_list if not name.endswith('_err')]
100
101
        n_filters = len(filters)

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        w_redshifting = creation_modules.index('redshifting')
        if creation_modules_params[w_redshifting]['redshift'] == ['']:
            obs_table = read_table(data_file)
            z = np.unique(np.around(obs_table['redshift'],
                                    decimals=REDSHIFT_DECIMALS))
            creation_modules_params[w_redshifting]['redshift'] = z
            del obs_table, z

        # 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

118
        # Retrieve an arbitrary SED to obtain the list of output parameters
119
        warehouse = SedWarehouse()
120
        sed = warehouse.get_sed(creation_modules, params.from_index(0))
121
        info = list(sed.info.keys())
122
        info.sort()
123
124
125
126
127
128
129
130
        n_info = len(sed.info)
        del warehouse, sed

        model_fluxes = (RawArray(ctypes.c_double,
                                 n_params * n_filters),
                        (n_params, n_filters))
        model_parameters = (RawArray(ctypes.c_double,
                                     n_params * n_info),
Médéric Boquien's avatar
Médéric Boquien committed
131
                            (n_params, n_info))
132

133
134
        initargs = (params, filters, save_sed, model_fluxes,
                    model_parameters, time.time(), mp.Value('i', 0))
135
136
137
138
139
140
141
142
143
144
145
        if cores == 1:  # Do not create a new process
            init_worker_fluxes(*initargs)
            for idx in range(n_params):
                worker_fluxes(idx)
        else:  # Analyse observations in parallel
            with mp.Pool(processes=cores, initializer=init_worker_fluxes,
                         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
146
147
148

# AnalysisModule to be returned by get_module
Module = SaveFluxes