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

19
from __future__ import absolute_import, division, print_function
Philipp Arras's avatar
Philipp Arras committed
20

21
22
import numpy as np
from scipy.stats import invgamma, norm
Philipp Arras's avatar
Philipp Arras committed
23
24

from ..compat import *
25
from ..field import Field
Martin Reinecke's avatar
Martin Reinecke committed
26
from ..models.model import Model
Philipp Arras's avatar
Philipp Arras committed
27
from ..multi.multi_field import MultiField
Martin Reinecke's avatar
Martin Reinecke committed
28
from ..operators.selection_operator import SelectionOperator
Philipp Arras's avatar
Philipp Arras committed
29
from ..sugar import makeOp
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from ..utilities import memo


class PointSources(Model):
    def __init__(self, position, alpha, q):
        super(PointSources, self).__init__(position)
        self._alpha = alpha
        self._q = q

    def at(self, position):
        return self.__class__(position, self._alpha, self._q)

    @property
    @memo
    def value(self):
Martin Reinecke's avatar
Martin Reinecke committed
45
        points = self.position['points'].local_data
46
47
        # MR FIXME?!
        points = np.clip(points, None, 8.2)
Martin Reinecke's avatar
Martin Reinecke committed
48
        points = Field.from_local_data(self.position['points'].domain, points)
49
50
51
52
        return self.IG(points, self._alpha, self._q)

    @property
    @memo
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
53
    def jacobian(self):
Martin Reinecke's avatar
Martin Reinecke committed
54
55
56
        u = self.position['points'].local_data
        inner = norm.pdf(u)
        outer_inv = invgamma.pdf(invgamma.ppf(norm.cdf(u),
57
58
59
                                              self._alpha,
                                              scale=self._q),
                                 self._alpha, scale=self._q)
60
61
        # FIXME
        outer_inv = np.clip(outer_inv, 1e-20, None)
62
        outer = 1/outer_inv
Martin Reinecke's avatar
bug fix    
Martin Reinecke committed
63
64
        grad = Field.from_local_data(self.position['points'].domain,
                                     inner*outer)
Martin Reinecke's avatar
Martin Reinecke committed
65
66
        grad = makeOp(MultiField.from_dict({"points": grad},
                                           self.position._domain))
67
68
69
70
        return SelectionOperator(grad.target, 'points')*grad

    @staticmethod
    def IG(field, alpha, q):
Martin Reinecke's avatar
Martin Reinecke committed
71
72
        foo = invgamma.ppf(norm.cdf(field.local_data), alpha, scale=q)
        return Field.from_local_data(field.domain, foo)
73

Martin Reinecke's avatar
Martin Reinecke committed
74
    # MR FIXME: why does this take an np.ndarray instead of a Field?
75
76
77
78
    @staticmethod
    def inverseIG(u, alpha, q):
        res = norm.ppf(invgamma.cdf(u, alpha, scale=q))
        return res