configuration.py 10.7 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
3
# Copyright (C) 2012, 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

Médéric Boquien's avatar
Médéric Boquien committed
6
from astropy.table import Table
7
8
9
import configobj
import pkg_resources
import pkgutil
10
import collections
11
import itertools
12
import numpy as np
13
from glob import glob # To allow the use of glob() in "eval..."
14
from textwrap import wrap
15
from .tools import param_dict_combine
16
from ..data import Database
17
18
from .. import creation_modules
from .. import analysis_modules
19
20
21
22
23


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

Yannick Roehlly's avatar
Yannick Roehlly committed
24
    Parameters
25
26
    ----------
    package_name : string
27
        Name of the package (e.g. pcigale.creation_modules).
28
29
30
31
32
33
34
35
36
37
38
39
40

    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])]

    return module_names


Yannick Roehlly's avatar
Yannick Roehlly committed
41
42
43
44
45
46
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:
47
48
49
    - 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.
50
51
52
    - If the description is a string beginning by 'range', the start, step and
      stop values are then expected and the range is evaluated (stop included
      if reached.
Yannick Roehlly's avatar
Yannick Roehlly committed
53
54
    - Then the function tries to evaluate the description as a Numpy array of
      float and returns the mere list if this fails.
55

Yannick Roehlly's avatar
Yannick Roehlly committed
56
    Parameters
57
    ----------
Yannick Roehlly's avatar
Yannick Roehlly committed
58
    description : string or list
59
60
61
62
        The description to be evaluated.

    Returns
    -------
63
     results : list
Yannick Roehlly's avatar
Yannick Roehlly committed
64
        The evaluated list of values.
65
66

    """
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    results = description
    if type(description) == str:
        if description.startswith('eval '):
            results = eval(description[4:])
            # If the evaluation lead to a single value, we put it in a list.
            if not isinstance(results, collections.Iterable):
                results = [results]
        elif description.startswith('range '):
            start, stop, step = [float(item) for item
                                 in description[5:].split()]
            results = np.arange(start, stop+step, step)
        else:
            # We need to return a list to combine the list of possible values
            # for each parameter.
81
            results = [results]
82
83
84
85
86
87
88

    # We prefer to evaluate the parameter as a numpy array of floats if
    # possible.
    try:
        results = np.array(results, float)
    except ValueError:
        pass
89

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


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
100
        Parameters
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        ----------
        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.")

126
127
        self.config['creation_modules'] = []
        self.config.comments['creation_modules'] = [""] + wrap(
128
            "Order of the modules use for SED creation. Available modules : "
129
            + ', '.join(list_modules('pcigale.creation_modules')) + ".")
130
131
132
133

        self.config['analysis_method'] = ""
        self.config.comments['analysis_method'] = [""] + wrap(
            "Method used for statistical analysis. Available methods: "
134
            + ', '.join(list_modules('pcigale.analysis_modules')) + ".")
135
136
137
138
139
140
141
142
143
144
145
146
147

        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
148
149
        with Database() as base:
            filter_list = base.get_filter_list()[0]
150
151

        # Finding the known filters in the data table
152
153
        if self.config['data_file'].endswith(('fits', 'vot', 'xml')):
            obs_table = Table.read(self.config['data_file'])
Médéric Boquien's avatar
Médéric Boquien committed
154
155
        else:
            obs_table = Table.read(self.config['data_file'], format='ascii')
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
        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
175
        # the configuration section from its parameter list.
176
177
178
179
        self.config['sed_creation_modules'] = {}
        self.config.comments['sed_creation_modules'] = ["", ""] + wrap(
            "Configuration of the SED creation modules.")

180
        for module_name in self.config['creation_modules']:
181
182
183
            self.config["sed_creation_modules"][module_name] = {}
            sub_config = self.config["sed_creation_modules"][module_name]

184
            for name, (typ, description, default) in \
185
186
187
                    creation_modules.get_module(
                        module_name,
                        blank=True).parameter_list.items():
188
189
190
191
192
193
                if default is None:
                    default = ''
                sub_config[name] = default
                sub_config.comments[name] = wrap(description)

            self.config['sed_creation_modules'].comments[module_name] = [
194
                creation_modules.get_module(module_name, blank=True).comments]
195
196
197
198
199
200

        # 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']
201
        for name, (typ, desc, default) in \
202
                analysis_modules.get_module(module_name).parameter_list.items():
203
204
205
206
207
208
            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
209

210
211
    @property
    def configuration(self):
Yannick Roehlly's avatar
Yannick Roehlly committed
212
        """Returns a dictionary for the session configuration.
Yannick Roehlly's avatar
Yannick Roehlly committed
213
214
215
216
217
218
219

        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.
220
        configuration['creation_modules'] : list of strings
Yannick Roehlly's avatar
Yannick Roehlly committed
221
            List of the modules (in the right order) used to create the SEDs.
222
        configuration['creation_modules_params'] : list of dictionaries
Yannick Roehlly's avatar
Yannick Roehlly committed
223
224
            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
225
226
227
            one).
        configuration['analysis_method'] : string
            Statistical analysis module used to fit the data.
Yannick Roehlly's avatar
Yannick Roehlly committed
228
229
        configuration['analysis_method_params'] : dictionary
            Parameters for the statistical analysis module. To each parameter
Yannick Roehlly's avatar
Yannick Roehlly committed
230
231
232
233
            is associated a list of possible values.
        """
        configuration = {}

234
        for section in ['data_file', 'column_list', 'creation_modules',
235
                        'analysis_method']:
Yannick Roehlly's avatar
Yannick Roehlly committed
236
237
            configuration[section] = self.config[section]

Yannick Roehlly's avatar
Yannick Roehlly committed
238
        # Parsing the SED modules parameters
239
240
        configuration['creation_modules_params'] = []
        for module in self.config['creation_modules']:
Yannick Roehlly's avatar
Yannick Roehlly committed
241
            module_params = {}
Yannick Roehlly's avatar
Yannick Roehlly committed
242
243
            for key, value in \
                    self.config['sed_creation_modules'][module].items():
Yannick Roehlly's avatar
Yannick Roehlly committed
244
                module_params[key] = evaluate_description(value)
245
            configuration['creation_modules_params'].append(module_params)
246
247

        # Analysis method parameters
248
249
        configuration['analysis_method_params'] = \
            self.config['analysis_configuration']
Yannick Roehlly's avatar
Yannick Roehlly committed
250
251

        return configuration
252
253

    @property
254
    def creation_modules_conf_array(self):
Yannick Roehlly's avatar
Yannick Roehlly committed
255
        """Return the array of all the possible parameter sets from the
256
257
258
        SED creation modules.

        TODO: Maybe it would be more optimal to create an iterator that would
Yannick Roehlly's avatar
Yannick Roehlly committed
259
              iterate over the whole parameter combinations instead of
260
261
262
263
264
              creating the array.

        Returns
        -------
        result : array of arrays of dictionaries
Yannick Roehlly's avatar
Yannick Roehlly committed
265
            The inner arrays contains the various parameter dictionaries
266
            for the modules listed in configuration['creation_modules'].
267
268
269
270

        """

        # First, for each module, we transform the dictionary containing all
Yannick Roehlly's avatar
Yannick Roehlly committed
271
272
        # the possible value for each parameter in a list of dictionaries
        # containing one value for each parameter. We put this list in a list
273
274
        # corresponding to the SED modules one.
        tmp_list = [param_dict_combine(dictionary) for dictionary in
275
                    self.configuration['creation_modules_params']]
276
277
278
279

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