configuration.py 11.9 KB
Newer Older
1
2
# -*- coding: utf-8 -*-
"""
3
Copyright (C) 2012, 2013 Centre de données Astrophysiques de Marseille
4
5
6
7
8
9
10
11
12
13
14
Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt

@author: Yannick Roehlly <yannick.roehlly@oamp.fr>

"""


import atpy
import configobj
import pkg_resources
import pkgutil
15
import collections
16
import itertools
17
18
import numpy as np
from textwrap import wrap
19
from .tools import param_dict_combine
20
21
22
23
24
25
26
27
from ..data import Database
from ..sed.modules import common as modules
from ..stats import common as analysis


def list_modules(package_name):
    """Lists the modules available in a package

Yannick Roehlly's avatar
Yannick Roehlly committed
28
    Parameters
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    ----------
    package_name : string
        Name of the package (e.g. pcigale.sed.modules).

    Returns
    -------
    module_name : array of strings
        List of the available modules.

    """
    directory = pkg_resources.resource_filename(package_name, '')
    module_names = [name for _, name, _ in pkgutil.iter_modules([directory])]
    if 'common' in module_names:
        module_names.remove('common')

    return module_names


Yannick Roehlly's avatar
Yannick Roehlly committed
47
48
49
50
51
52
def evaluate_description(description):
    """Evaluate a description from the config file as a list.

    The description is read from the config file by configobj that transforms
    coma separated value in a list. From this description, this function try
    to evaluate the desired list of values:
53
54
55
    - If the description is a string beginning with 'eval ', then its content
      (without 'eval ') is evaluated as Python code and its result returned.
      An array is expected.
Yannick Roehlly's avatar
Yannick Roehlly committed
56
57
58
59
    - If the description is a list beginning by 'range', the start, step and
      stop values are then expected and the range is evaluated.
    - Then the function tries to evaluate the description as a Numpy array of
      float and returns the mere list if this fails.
60

Yannick Roehlly's avatar
Yannick Roehlly committed
61
    Parameters
62
    ----------
Yannick Roehlly's avatar
Yannick Roehlly committed
63
    description : string or list
64
65
66
67
        The description to be evaluated.

    Returns
    -------
68
     results : list
Yannick Roehlly's avatar
Yannick Roehlly committed
69
        The evaluated list of values.
70
71
72

    """

Yannick Roehlly's avatar
Yannick Roehlly committed
73
74
    if not type(description) == list:
        description = [description]
75

76
77
78
79
80
    if description[0].startswith('eval '):
        results = eval(description[0][4:])
        # If the evaluation lead to a single value, we put it in a list.
        if not isinstance(results, collections.Iterable):
            results = [results]
Yannick Roehlly's avatar
Yannick Roehlly committed
81
82
83
84
85
86
87
88
89
90
    elif description[0] == 'range':
        start = float(description[1])
        step = float(description[2])
        stop = float(description[3])
        results = np.arange(start, stop, step, float)
    else:
        try:
            results = np.array(description, float)
        except ValueError:
            results = description
91

Yannick Roehlly's avatar
Yannick Roehlly committed
92
    return results
93
94
95
96
97
98
99
100
101


class Configuration(object):
    """This class manages the configuration of pcigale.
    """

    def __init__(self, filename="pcigale.ini"):
        """Initialise a pcigale configuration.

Yannick Roehlly's avatar
Yannick Roehlly committed
102
        Parameters
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        ----------
        filename : string
            Name of the configuration file (pcigale.conf by default).

        """
        self.config = configobj.ConfigObj(filename,
                                          write_empty_values=True,
                                          indent_type='  ')

    def create_blank_conf(self):
        """Create the initial configuration file

        Write the initial pcigale configuration file where the user can state
        which data file to use, which modules to use for the SED creation, as
        well as the method selected for statistical analysis.

        """

        self.config['data_file'] = ""
        self.config.comments['data_file'] = wrap(
            "File containing the observation data to be fitted. Each flux "
            "column must have the name of the corresponding filter, the "
            "error columns are suffixed with '_err'. The values must be "
            "in mJy.")

        self.config['sed_modules'] = []
        self.config.comments['sed_modules'] = [""] + wrap(
            "Order of the modules use for SED creation. Available modules : "
            + ', '.join(list_modules('pcigale.sed.modules')) + ".")

133
134
135
136
137
138
        self.config['redshift_module'] = ""
        self.config.comments['redshift_module'] = [""] + wrap(
            "Module used to redshift the SED before the integration in the "
            "filters. This is a SED creation module that accepts a 'redshift' "
            "parameter (see the documentation).")

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
        self.config['analysis_method'] = ""
        self.config.comments['analysis_method'] = [""] + wrap(
            "Method used for statistical analysis. Available methods: "
            + ', '.join(list_modules('pcigale.stats')) + ".")

        self.config.write()

    def generate_conf(self):
        """Generate the full configuration file

        Reads the user entries in the initial configuration file and add the
        configuration options of all selected modules as well as the filter
        selection based on the filters identified in the data table file.

        """

        # Getting the list of the filters available in pcigale database
        base = Database()
        filter_list = base.get_filter_list()[0]
        base.close()

        # Finding the known filters in the data table
Yannick Roehlly's avatar
Yannick Roehlly committed
161
        obs_table = atpy.Table(self.config['data_file'], verbose=False)
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
        column_list = []
        for column in obs_table.columns:
            filter_name = column[:-4] if column.endswith('_err') else column
            if filter_name in filter_list:
                column_list.append(column)

        # Check that we don't have an error column without the associated flux
        for column in column_list:
            if column.endswith('_err') and (column[:-4] not in column_list):
                raise StandardError("The observation table as a {} column "
                                    "but no {} column.".format(column,
                                                               column[:-4]))

        self.config['column_list'] = column_list
        self.config.comments['column_list'] = [""] + wrap(
            "List of the columns in the observation data file to use for "
            "the fitting.")

        # SED creation modules configurations. For each module, we generate
Yannick Roehlly's avatar
Yannick Roehlly committed
181
        # the configuration section from its parameter list.
182
183
184
185
186
187
188
189
        self.config['sed_creation_modules'] = {}
        self.config.comments['sed_creation_modules'] = ["", ""] + wrap(
            "Configuration of the SED creation modules.")

        for module_name in self.config['sed_modules']:
            self.config["sed_creation_modules"][module_name] = {}
            sub_config = self.config["sed_creation_modules"][module_name]

190
            for name, (typ, description, default) in \
191
192
                    modules.get_module(module_name,
                                       blank=True).parameter_list.items():
193
194
195
196
197
198
                if default is None:
                    default = ''
                sub_config[name] = default
                sub_config.comments[name] = wrap(description)

            self.config['sed_creation_modules'].comments[module_name] = [
199
                modules.get_module(module_name, blank=True).comments]
200

201
202
203
204
205
206
207
208
        # Configuration for the redshift module
        self.config['redshift_configuration'] = {}
        self.config.comments['redshift_configuration'] = ["", ""] + wrap(
            "Set the 'redshift' parameter to None (or delete the line). If "
            "there are other parameters, you must give only one value for "
            "each.")
        module_name = self.config['redshift_module']
        for name, (typ, desc, default) in \
209
210
                modules.get_module(module_name,
                                   blank=True).parameter_list.items():
211
212
213
214
215
            if default is None:
                default = ''
            self.config['redshift_configuration'][name] = default
            self.config['redshift_configuration'].comments[name] = wrap(desc)

216
217
218
219
220
        # Configuration for the analysis method
        self.config['analysis_configuration'] = {}
        self.config.comments['analysis_configuration'] = ["", ""] + wrap(
            "Configuration of the statistical analysis method.")
        module_name = self.config['analysis_method']
221
        for name, (typ, desc, default) in \
222
                analysis.get_module(module_name).parameter_list.items():
223
224
225
226
227
228
            if default is None:
                default = ''
            self.config['analysis_configuration'][name] = default
            self.config['analysis_configuration'].comments[name] = wrap(desc)

        self.config.write()
Yannick Roehlly's avatar
Yannick Roehlly committed
229

230
231
    @property
    def configuration(self):
Yannick Roehlly's avatar
Yannick Roehlly committed
232
        """Returns a dictionary for the session configuration.
Yannick Roehlly's avatar
Yannick Roehlly committed
233
234
235
236
237
238
239
240
241
242

        Returns
        -------
        configuration['data_file'] : string
            File containing the observations to fit.
        configuration['column_list'] : list of strings
            List of the columns of data_file to use in the fitting.
        configuration['sed_modules'] : list of strings
            List of the modules (in the right order) used to create the SEDs.
        configuration['sed_modules_params'] : list of dictionaries
Yannick Roehlly's avatar
Yannick Roehlly committed
243
244
            Configuration parameters for each module. To each parameter, the
            dictionary associates a list of possible values (possibly only
Yannick Roehlly's avatar
Yannick Roehlly committed
245
            one).
246
247
248
        configuration['redshift_module'] : string
        configuration['redshift_configuration'] : dictionary
            Parameters for the redshift module.
Yannick Roehlly's avatar
Yannick Roehlly committed
249
250
        configuration['analysis_method'] : string
            Statistical analysis module used to fit the data.
Yannick Roehlly's avatar
Yannick Roehlly committed
251
252
        configuration['analysis_method_params'] : dictionary
            Parameters for the statistical analysis module. To each parameter
Yannick Roehlly's avatar
Yannick Roehlly committed
253
254
255
256
257
            is associated a list of possible values.
        """
        configuration = {}

        for section in ['data_file', 'column_list', 'sed_modules',
258
                        'redshift_module', 'analysis_method']:
Yannick Roehlly's avatar
Yannick Roehlly committed
259
260
            configuration[section] = self.config[section]

Yannick Roehlly's avatar
Yannick Roehlly committed
261
        # Parsing the SED modules parameters
Yannick Roehlly's avatar
Yannick Roehlly committed
262
263
264
        configuration['sed_modules_params'] = []
        for module in self.config['sed_modules']:
            module_params = {}
Yannick Roehlly's avatar
Yannick Roehlly committed
265
266
            for key, value in \
                    self.config['sed_creation_modules'][module].items():
Yannick Roehlly's avatar
Yannick Roehlly committed
267
268
                module_params[key] = evaluate_description(value)
            configuration['sed_modules_params'].append(module_params)
269
270
271
272
273
        # Parsing the redshift module parameters
        configuration['redshift_configuration'] = {}
        for key, value in self.config['redshift_configuration'].items():
            configuration['redshift_configuration'][key] = \
                evaluate_description(value)
Yannick Roehlly's avatar
Yannick Roehlly committed
274
        # Parsing the statistical analysis parameters
Yannick Roehlly's avatar
Yannick Roehlly committed
275
        configuration['analysis_method_params'] = {}
Yannick Roehlly's avatar
Yannick Roehlly committed
276
        for key, value in self.config['analysis_configuration'].items():
Yannick Roehlly's avatar
Yannick Roehlly committed
277
            configuration['analysis_method_params'][key] = \
Yannick Roehlly's avatar
Yannick Roehlly committed
278
279
280
                evaluate_description(value)

        return configuration
281
282
283

    @property
    def sed_modules_conf_array(self):
Yannick Roehlly's avatar
Yannick Roehlly committed
284
        """Return the array of all the possible parameter sets from the
285
286
287
        SED creation modules.

        TODO: Maybe it would be more optimal to create an iterator that would
Yannick Roehlly's avatar
Yannick Roehlly committed
288
              iterate over the whole parameter combinations instead of
289
290
291
292
293
              creating the array.

        Returns
        -------
        result : array of arrays of dictionaries
Yannick Roehlly's avatar
Yannick Roehlly committed
294
            The inner arrays contains the various parameter dictionaries
295
296
297
298
299
            for the modules listed in configuration['sed_modules'].

        """

        # First, for each module, we transform the dictionary containing all
Yannick Roehlly's avatar
Yannick Roehlly committed
300
301
        # the possible value for each parameter in a list of dictionaries
        # containing one value for each parameter. We put this list in a list
302
303
304
305
306
307
308
        # corresponding to the SED modules one.
        tmp_list = [param_dict_combine(dictionary) for dictionary in
                    self.configuration['sed_modules_params']]

        # The we use itertools to create an array of all possible
        # combinations.
        return [x for x in itertools.product(*tmp_list)]