nifty_config.py 3.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19

import os
Theo Steininger's avatar
Theo Steininger committed
20
21
from distutils.version import LooseVersion as lv

22
import numpy as np
23
24
25
26
27
import keepers

# Setup the dependency injector
dependency_injector = keepers.DependencyInjector(
                                   [('mpi4py.MPI', 'MPI'),
28
                                    'pyHealpix'])
29
30
31
32
33
34

dependency_injector.register('pyfftw', lambda z: hasattr(z, 'FFTW_MPI'))

# Initialize the variables
variable_fft_module = keepers.Variable(
                               'fft_module',
Theo Steininger's avatar
Theo Steininger committed
35
36
37
                               ['fftw', 'numpy'],
                               lambda z: (('pyfftw' in dependency_injector)
                                          if z == 'fftw' else True))
38

39
40
def _pyHealpix_validator(use_pyHealpix):
    if not isinstance(use_pyHealpix, bool):
Theo Steininger's avatar
Theo Steininger committed
41
        return False
42
    if not use_pyHealpix:
43
        return True
44
    if 'pyHealpix' not in dependency_injector:
Theo Steininger's avatar
Theo Steininger committed
45
        return False
46
    pyHealpix = dependency_injector['pyHealpix']
Theo Steininger's avatar
Theo Steininger committed
47
48
49
    return True


50
51
variable_use_pyHealpix = keepers.Variable(
                          'use_pyHealpix',
52
                          [True, False],
53
                          _pyHealpix_validator,
54
55
                          genus='boolean')

56
57
58
59
60
61
62
63
64
65
66
67
68
69
def _dtype_validator(dtype):
    try:
        np.dtype(dtype)
    except(TypeError):
        return False
    else:
        return True

variable_default_field_dtype = keepers.Variable(
                              'default_field_dtype',
                              ['float'],
                              _dtype_validator,
                              genus='str')

70
71
variable_default_distribution_strategy = keepers.Variable(
                              'default_distribution_strategy',
72
73
74
75
                              ['fftw', 'equal'],
                              lambda z: (('pyfftw' in dependency_injector)
                                         if z == 'fftw' else True),
                              genus='str')
76
77

nifty_configuration = keepers.get_Configuration(
78
79
                 name='NIFTy',
                 variables=[variable_fft_module,
80
                            variable_use_pyHealpix,
81
                            variable_default_field_dtype,
82
                            variable_default_distribution_strategy],
83
                 file_name='NIFTy.conf',
Theo Steininger's avatar
Theo Steininger committed
84
85
86
                 search_paths=[os.path.expanduser('~') + "/.config/nifty/",
                               os.path.expanduser('~') + "/.config/",
                               './'])
87

88
89
90
91
92
93
########

try:
    nifty_configuration.load()
except:
    pass