__init__.py 4.19 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

"""
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

from collections import OrderedDict
19 20
import multiprocessing as mp

21
from .. import AnalysisModule
22
from ..utils import Counter
23 24
from .workers import init_fluxes as init_worker_fluxes
from .workers import fluxes as worker_fluxes
25 26
from ...managers.models import ModelsManager
from ...managers.observations import ObservationsManager
27
from ...managers.parameters import ParametersManager
28

29

30
class SaveFluxes(AnalysisModule):
31 32
    """Save fluxes analysis module

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

    """

38
    parameter_list = OrderedDict([
39
        ("variables", (
40
            "cigale_string_list()",
41 42 43 44
            "List of the physical properties to save. Leave empty to save all "
            "the physical properties (not recommended when there are many "
            "models).",
            None
45
        )),
46
        ("save_sed", (
47
            "boolean()",
48
            "If True, save the generated spectrum for each model.",
49
            False
50 51 52 53 54 55 56
        )),
        ("blocks", (
            "integer(min=1)",
            "Number of blocks to compute the models. Having a number of blocks"
            " larger than 1 can be useful when computing a very large number "
            "of models or to split the result file into smaller files.",
            1
57 58
        ))
    ])
59

60
    @staticmethod
61 62 63 64 65 66 67
    def _parallel_job(worker, items, initargs, initializer, ncores):
        if ncores == 1:  # Do not create a new process
            initializer(*initargs)
            for idx, item in enumerate(items):
                worker(idx, item)
        else:  # run in parallel
            with mp.Pool(processes=ncores, initializer=initializer,
68
                         initargs=initargs) as pool:
69 70 71 72 73
                pool.starmap(worker, enumerate(items))

    def _compute_models(self, conf, obs, params):
        nblocks = len(params.blocks)
        for iblock in range(nblocks):
74
            print(f"Computing models for block {iblock + 1}/{nblocks}...")
75 76

            models = ModelsManager(conf, obs, params, iblock)
77
            counter = Counter(len(params.blocks[iblock]), 50, 250)
78

79
            initargs = (models, counter)
80 81 82
            self._parallel_job(worker_fluxes, params.blocks[iblock], initargs,
                               init_worker_fluxes, conf['cores'])

83
            # Print the final value as it may not otherwise be printed
84 85
            if counter.global_counter.value % 250 != 0:
                counter.pprint(len(params.blocks[iblock]))
86

87
            print("Saving the models ....")
88
            models.save(f"models-block-{iblock}")
89 90


91
    def process(self, conf):
92 93 94
        """Process with the savedfluxes analysis.

        All the possible theoretical SED are created and the fluxes in the
95
        filters from the list of bands are computed and saved to a table,
96 97 98 99
        alongside the parameter values.

        Parameters
        ----------
100 101
        conf: dictionary
            Contents of pcigale.ini in the form of a dictionary
102 103
        """

104
        # Rename the output directory if it exists
105
        self.prepare_dirs()
106

107 108 109
        # Read the observations information in order to retrieve the list of
        # bands to compute the fluxes.
        observations = ObservationsManager(conf)
110

111
        # The parameters manager allows us to retrieve the models parameters
112 113 114 115
        # 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.
116
        params = ParametersManager(conf)
117

118
        self._compute_models(conf, observations, params)
119

120

121 122
# AnalysisModule to be returned by get_module
Module = SaveFluxes