prober.py 4.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13
14
15
16
17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
theos's avatar
theos committed
18

Martin Reinecke's avatar
Martin Reinecke committed
19
20
21
from builtins import str
from builtins import range
from builtins import object
theos's avatar
theos committed
22
23
import numpy as np

theos's avatar
theos committed
24
from nifty.field import Field
25
26
27
import nifty.nifty_utilities as utilities

from nifty import nifty_configuration as nc
theos's avatar
theos committed
28
29
30
31

from d2o import STRATEGIES as DISTRIBUTION_STRATEGIES


32
class Prober(object):
Theo Steininger's avatar
Theo Steininger committed
33
    """
34
35
36
37
38
39
    See the following webpages for the principles behind the usage of
    mixin-classes

    https://www.python.org/download/releases/2.2.3/descrintro/#cooperation
    https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

Theo Steininger's avatar
Theo Steininger committed
40
41
    """

42
    def __init__(self, domain=None, distribution_strategy=None, probe_count=8,
43
44
                 random_type='pm1', probe_dtype=np.float,
                 compute_variance=False):
theos's avatar
theos committed
45

46
        self._domain = utilities.parse_domain(domain)
Theo Steininger's avatar
Theo Steininger committed
47
48
        self._distribution_strategy = \
            self._parse_distribution_strategy(distribution_strategy)
49
50
        self._probe_count = self._parse_probe_count(probe_count)
        self._random_type = self._parse_random_type(random_type)
theos's avatar
theos committed
51
        self.compute_variance = bool(compute_variance)
52
        self.probe_dtype = np.dtype(probe_dtype)
theos's avatar
theos committed
53

54
    # ---Properties---
theos's avatar
theos committed
55

theos's avatar
theos committed
56
    @property
theos's avatar
theos committed
57
    def domain(self):
theos's avatar
theos committed
58
59
60
        return self._domain

    @property
theos's avatar
theos committed
61
    def distribution_strategy(self):
theos's avatar
theos committed
62
        return self._distribution_strategy
theos's avatar
theos committed
63

Theo Steininger's avatar
Theo Steininger committed
64
    def _parse_distribution_strategy(self, distribution_strategy):
65
66
67
68
        if distribution_strategy is None:
            distribution_strategy = nc['default_distribution_strategy']
        else:
            distribution_strategy = str(distribution_strategy)
theos's avatar
theos committed
69
        if distribution_strategy not in DISTRIBUTION_STRATEGIES['global']:
70
71
            raise ValueError("distribution_strategy must be a global-type "
                             "strategy.")
72
        self._distribution_strategy = distribution_strategy
theos's avatar
theos committed
73

theos's avatar
theos committed
74
    @property
theos's avatar
theos committed
75
76
    def probe_count(self):
        return self._probe_count
theos's avatar
theos committed
77

78
79
    def _parse_probe_count(self, probe_count):
        return int(probe_count)
theos's avatar
theos committed
80
81
82
83
84

    @property
    def random_type(self):
        return self._random_type

85
    def _parse_random_type(self, random_type):
theos's avatar
theos committed
86
        if random_type not in ["pm1", "normal"]:
87
88
            raise ValueError(
                "unsupported random type: '" + str(random_type) + "'.")
89
        return random_type
theos's avatar
theos committed
90
91
92

    # ---Probing methods---

theos's avatar
theos committed
93
    def probing_run(self, callee):
theos's avatar
theos committed
94
        """ controls the generation, evaluation and finalization of probes """
95
        self.reset()
Martin Reinecke's avatar
Martin Reinecke committed
96
        for index in range(self.probe_count):
theos's avatar
theos committed
97
            current_probe = self.get_probe(index)
theos's avatar
theos committed
98
            pre_result = self.process_probe(callee, current_probe, index)
99
            self.finish_probe(current_probe, pre_result)
theos's avatar
theos committed
100

101
    def reset(self):
102
        pass
theos's avatar
theos committed
103
104
105
106

    def get_probe(self, index):
        """ layer of abstraction for potential probe-caching """
        return self.generate_probe()
theos's avatar
theos committed
107

theos's avatar
theos committed
108
109
    def generate_probe(self):
        """ a random-probe generator """
theos's avatar
theos committed
110
        f = Field.from_random(random_type=self.random_type,
theos's avatar
theos committed
111
                              domain=self.domain,
112
                              dtype=self.probe_dtype,
theos's avatar
theos committed
113
                              distribution_strategy=self.distribution_strategy)
theos's avatar
theos committed
114
115
        uid = np.random.randint(1e18)
        return (uid, f)
theos's avatar
theos committed
116

theos's avatar
theos committed
117
118
119
    def process_probe(self, callee, probe, index):
        """ layer of abstraction for potential result-caching/recycling """
        return self.evaluate_probe(callee, probe[1])
theos's avatar
theos committed
120

theos's avatar
theos committed
121
    def evaluate_probe(self, callee, probe, **kwargs):
theos's avatar
theos committed
122
        """ processes a probe """
theos's avatar
theos committed
123
        return callee(probe, **kwargs)
theos's avatar
theos committed
124

theos's avatar
theos committed
125
    def finish_probe(self, probe, pre_result):
126
        pass
theos's avatar
theos committed
127

theos's avatar
theos committed
128
129
    def __call__(self, callee):
        return self.probing_run(callee)