point_sources.py 2 KB
Newer Older
1 2 3 4 5
import numpy as np
from scipy.stats import invgamma, norm
from ..field import Field
from ..sugar import makeOp
from ..multi import MultiField
Philipp Arras's avatar
Fixups  
Philipp Arras committed
6
from ..models import Model
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 57 58 59 60 61 62 63 64

from ..operators import SelectionOperator
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):
        points = self.position['points'].to_global_data()
        points = np.clip(points, None, 8.2)
        points = Field(self.position['points'].domain, points)
        return self.IG(points, self._alpha, self._q)

    @property
    @memo
    def gradient(self):
        u = self.position['points']
        inner = norm.pdf(u.val)
        outer_inv = invgamma.pdf(invgamma.ppf(norm.cdf(u.val),
                                              self._alpha,
                                              scale=self._q),
                                 self._alpha, scale=self._q)
        # FIXME
        outer_inv = np.clip(outer_inv, 1e-20, None)
        outer = 1/outer_inv
        grad = Field(u.domain, val=inner*outer)
        grad = makeOp(MultiField({'points': grad}))
        return SelectionOperator(grad.target, 'points')*grad

    @staticmethod
    def IG(field, alpha, q):
        foo = invgamma.ppf(norm.cdf(field.val), alpha, scale=q)
        return Field(field.domain, val=foo)

    @staticmethod
    def IG_prime(field, alpha, q):
        inner = norm.pdf(field.val)
        outer = invgamma.pdf(invgamma.ppf(norm.cdf(field.val), alpha, scale=q), alpha, scale=q)
        # # FIXME
        # outer = np.clip(outer, 1e-20, None)
        outer = 1/outer
        return Field(field.domain, val=inner*outer)

    @staticmethod
    def inverseIG(u, alpha, q):
        res = norm.ppf(invgamma.cdf(u, alpha, scale=q))
        # # FIXME
        # res = np.clip(res, 0, None)
        return res