pipeline.py 3.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
# -*- coding: utf-8 -*-

import numpy as np

from keepers import Loggable

from likelihoods import Likelihood
from observers import Observer
from priors import Prior

11
12
from carrier_mapper import carrier_mapper

13
14

class Pipeline(Loggable, object):
15
16
    def __init__(self, observer, likelihood, prior, ensemble_size=1,
                 active_parameters=[], parameter_mapping={}):
17
18
19
20
        self.logger.debug("Setting up pipeline.")
        self.observer = observer
        self.likelihood = likelihood
        self.prior = prior
21
22
        self.active_parameters = active_parameters
        self.parameter_mapping = parameter_mapping
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
        self.ensemble_size = ensemble_size

    @property
    def observer(self):
        return self._observer

    @observer.setter
    def observer(self, observer):
        if not isinstance(observer, Observer):
            raise TypeError("observer must be an instance of Observer-class.")
        self.logger.debug("Setting observer.")
        self._observer = observer

    @property
    def likelihood(self):
        return self._likelihood

    @likelihood.setter
    def likelihood(self, likelihood):
        if not isinstance(likelihood, Likelihood):
            raise TypeError(
                "likelihood must be an instance of likelihood-class.")

    @property
    def prior(self):
        return self._prior

    @prior.setter
    def prior(self, prior):
        if not isinstance(prior, Prior):
            raise TypeError(
                "prior must be an instance of prior-class.")

    @property
57
58
    def parameter_mapping(self):
        return self._parameter_mapping
59

60
61
    @parameter_mapping.setter
    def parameter_mapping(self, parameters):
62
        """
63
64
65
        The parameter-mapping must be a dictionary with
        key: parameter-name
        value: [min, mean, max]
66
        """
67
        new_mapping = {}
68
        for p in parameters:
69
70
71
72
73
74
            new_key = str(p[0])
            new_value = [p[1], p[2], p[3]]
            new_mapping[new_key] = new_value
            self.logger.debug("Setting parameter_mapping %s to %s." %
                              (new_key, new_mapping[new_key]))
        self._parameter_mapping = new_mapping
75
76
77
78
79
80
81
82
83
84
85
86
87
88

    @property
    def ensemble_size(self):
        return self._ensemble_size

    @ensemble_size.setter
    def ensemble_size(self, ensemble_size):

        ensemble_size = int(ensemble_size)
        if ensemble_size <= 0:
            raise ValueError("ensemble_size must be positive!")
        self.logger.debug("Setting ensemble size to %i." % ensemble_size)
        self._ensemble_size = ensemble_size

89
90
91
92
93
94
95
96
97
    def _map_parameters(self, parameter_list):
        parameter_dict = {}
        for i, name in enumerate(self.active_parameters):
            if name in self.parameter_mapping:
                mapping = self.parameter_mapping[name]
                mapped_parameter = carrier_mapper(parameter_list[i],
                                                  a=mapping[1],
                                                  m=mapping[2],
                                                  b=mapping[3])
98
            else:
99
100
101
                mapped_parameter = np.float(parameter_list[i])
            parameter_dict[name] = mapped_parameter
        return parameter_dict
102
103

    def __call__(self, parameter_list):
104
        mapped_parameters = self._map_parameters(parameter_list)