aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/configs/checkers.py
blob: 9759ef1d21934a389bfde1c54c722bb3ee38ae58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# Copyright (c) 2016-2018 Weitian LI <weitian@aaronly.me>
# MIT license

"""
Custom checkers to further validate the configurations.

NOTE
----
These functions further check the configurations as a whole, which means
one config option may be checked against its context.
Therefore, they are very different to the checker functions used in the
``validate.Validator``.
"""

import os

from ..errors import ConfigError


def _check_missing(configs, keys):
    """
    Check whether the required config is provided by the user.
    """
    results = {}
    if isinstance(keys, str):
        keys = [keys, ]
    for key in keys:
        if not configs.getn(key):
            results[key] = "Value required but missing"
    return results


def _check_existence(configs, keys):
    """
    Check whether the file/directory corresponding to the config exists.
    """
    if isinstance(keys, str):
        keys = [keys, ]
    results = {}
    for key in keys:
        res = _check_missing(configs, key)
        if res == {}:
            # Both "key" and "dir_key" are valid
            path = configs.get_path(key)
            if not os.path.exists(path):
                res[key] = 'File/directory not exist: "%s"' % path
        results.update(res)
    return results


def _check_file_existence(configs, key, ext=None):
    """
    Check the existence of the file corresponding to the given config key.

    If the ``ext`` is specified, then the original file extension is
    first replaced with the given one, then to check its existence.
    """
    res = _check_missing(configs, key)
    if res == {}:
        filepath = configs.get_path(key)
        if ext:
            filepath = os.path.splitext(filepath)[0] + ext
            key += "#%s" % ext
        if not os.path.exists(filepath):
            res[key] = 'File not exist: "%s"' % filepath
    return res


def _is_power2(n):
    """
    Check a number whether a power of 2

    Credit: https://stackoverflow.com/a/29489026/4856091
    """
    return (n and not (n & (n-1)))


def check_foregrounds(configs):
    """
    Check the "[foregrounds]" section of the configurations.
    """
    results = {}
    # Check enabled foreground components
    fg = configs.foregrounds
    if len(fg[0]) == 0:
        results["foregrounds"] = "no foreground components enabled"
    return results


def check_sky(configs):
    """
    Check the "[sky]" section of the configurations.
    """
    results = {}
    skytype = configs.getn("sky/type")
    if skytype == "patch":
        sec = "sky/patch"
    elif skytype == "healpix":
        sec = "sky/healpix"
        # "nside" must be a power of 2
        key = sec + "/nside"
        res = _check_missing(configs, key)
        if res == {}:
            if not _is_power2(configs.getn(key)):
                results[key] = "not a power of 2"
        else:
            results.update(res)
    return results


def check_frequency(configs):
    """
    Check the "[frequency]" section of the configurations.
    """
    results = {}
    if configs.getn("frequency/type") == "custom":
        results.update(_check_missing(configs, "frequency/frequencies"))
    elif configs.getn("frequency/type") == "calc":
        results.update(
            _check_missing(configs, ["frequency/start",
                                     "frequency/stop",
                                     "frequency/step"])
        )
    return results


def check_galactic_synchrotron(configs):
    """
    Check the "[galactic][synchrotron]" section of the configurations.
    """
    comp = "galactic/synchrotron"
    comp_enabled = configs.foregrounds[0]
    if comp not in comp_enabled:
        return {}

    results = {}
    # Only validate the configs if this component is enabled
    results.update(
        _check_missing(configs, comp+"/template_freq")
    )
    results.update(
        _check_existence(configs, [comp+"/template", comp+"/indexmap"])
    )
    if configs.getn(comp+"/add_smallscales"):
        # "lmax" must be greater than "lmin"
        key = comp + "/lmax"
        res = _check_missing(configs, [key, comp+"/lmin"])
        if res == {}:
            if configs.getn(key) <= configs.getn(comp+"/lmin"):
                results[key] = "not greater than 'lmin'"
        else:
            results.update(res)
    results.update(_check_missing(configs, comp+"/output_dir"))
    return results


def check_galactic_freefree(configs):
    """Check the "[galactic][freefree]" section of the configurations."""
    comp = "galactic/freefree"
    comp_enabled = configs.foregrounds[0]
    results = {}
    if comp in comp_enabled:
        # Only validate the configs if this component is enabled
        results.update(
            _check_existence(configs, [comp+"/halphamap", comp+"/dustmap"])
        )
        results.update(_check_missing(configs, comp+"/output_dir"))
    return results


def check_galactic_snr(configs):
    """
    Check the "[galactic][snr]" section of the configurations.
    """
    comp = "galactic/snr"
    comp_enabled = configs.foregrounds[0]
    results = {}
    if comp in comp_enabled:
        # Only validate the configs if this component is enabled
        results.update(_check_existence(configs, comp+"/catalog"))
        results.update(_check_missing(configs, comp+"/output_dir"))
    return results


def check_extragalactic_clusters(configs):
    """
    Check the "[extragalactic][clusters]" section of the configurations.

    The related sections ("[extragalactic][psformalism]",
    "[extragalactic][halos]") are also checked.
    """
    comp = "extragalactic/clusters"
    comp_enabled = configs.foregrounds[0]
    results = {}
    if comp in comp_enabled:
        # output dndlnm data file required
        key = "extragalactic/psformalism/dndlnm_outfile"
        results.update(_check_missing(configs, key))
        # catalog required when enabled to use it
        if configs.get(comp+"/use_dump_catalog_data"):
            results.update(_check_file_existence(
                configs, comp+"/catalog_outfile", ext=".pkl"))
        else:
            results.update(_check_missing(configs, comp+"/catalog_outfile"))
        # dumped halos data required when enabled to use it
        if configs.get(comp+"/use_dump_halos_data"):
            results.update(_check_file_existence(
                configs, comp+"/halos_catalog_outfile", ext=".pkl"))
        else:
            results.update(_check_missing(
                configs, comp+"/halos_catalog_outfile"))
        results.update(_check_missing(configs, comp+"/output_dir"))
    return results


# Available checkers to validate the configurations
_CHECKERS = [
    check_foregrounds,
    check_sky,
    check_frequency,
    check_galactic_synchrotron,
    check_galactic_freefree,
    check_galactic_snr,
    check_extragalactic_clusters,
]


def check_configs(configs, raise_exception=True, checkers=_CHECKERS):
    """
    Check/validate the whole configurations through all the supplied
    checker functions.

    These checker functions may check one config option against its context
    if necessary to determine whether it has a valid value.

    Parameters
    ----------
    configs : `~ConfigManager`
        An ``ConfigManager`` instance contains both default and user
        configurations.
    raise_exception : bool, optional
        Whether raise a ``ConfigError`` exception if there is any invalid
        config options?
    checkers : list[function], optional
        List of checker functions through which the configurations
        will be checked.

    Returns
    -------
    validity : bool
        ``True`` if the configurations pass all checker functions.
    errors : dict
        An dictionary containing the details about the invalid config options,
        with the keys identifying the config options and values indicating
        the error message.
        If above ``validity=True``, then this is an empty dictionary ``{}``.

    Raises
    ------
    ConfigError :
        With ``raise_exception=True``, if any configuration option failed
        to pass all checkers, the ``ConfigError`` with details is raised.
    """
    errors = {}
    for checker in checkers:
        errors.update(checker(configs))
    #
    if errors == {}:
        validity = True
    else:
        validity = False
        if raise_exception:
            msg = "\n".join(['Config "{key}": {val}'.format(key=key, val=val)
                             for key, val in errors.items()])
            raise ConfigError(msg)
    return (validity, errors)