point_sources.py 3.28 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
20
from __future__ import absolute_import, division, print_function
from ..compat import *
21
22
23
24
import numpy as np
from scipy.stats import invgamma, norm
from ..field import Field
from ..sugar import makeOp
Martin Reinecke's avatar
Martin Reinecke committed
25
26
from ..multi.multi_field import MultiField
from ..models.model import Model
27

Martin Reinecke's avatar
Martin Reinecke committed
28
from ..operators.selection_operator import SelectionOperator
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
44
        points = self.position['points'].local_data
Martin Reinecke's avatar
Martin Reinecke committed
45
        # MR FIXME?!
46
        points = np.clip(points, None, 8.2)
Martin Reinecke's avatar
Martin Reinecke committed
47
        points = Field.from_local_data(self.position['points'].domain, points)
48
49
50
51
        return self.IG(points, self._alpha, self._q)

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

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

Martin Reinecke's avatar
Martin Reinecke committed
73
    # MR FIXME: is this function needed?
74
75
    @staticmethod
    def IG_prime(field, alpha, q):
Martin Reinecke's avatar
Martin Reinecke committed
76
        inner = norm.pdf(field.local_data)
Martin Reinecke's avatar
Martin Reinecke committed
77
78
        outer = invgamma.pdf(invgamma.ppf(norm.cdf(field.local_data), alpha,
                                          scale=q), alpha, scale=q)
79
80
        # # FIXME
        # outer = np.clip(outer, 1e-20, None)
Martin Reinecke's avatar
Martin Reinecke committed
81
        return Field.from_local_data(field.domain, inner/outer)
82

Martin Reinecke's avatar
Martin Reinecke committed
83
    # MR FIXME: why does this take an np.ndarray instead of a Field?
84
85
86
87
88
89
    @staticmethod
    def inverseIG(u, alpha, q):
        res = norm.ppf(invgamma.cdf(u, alpha, scale=q))
        # # FIXME
        # res = np.clip(res, 0, None)
        return res