point_sources.py 3.28 KB
 Martin Reinecke committed Jul 09, 2018 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 . # # 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. `````` Martin Reinecke committed Jul 09, 2018 19 20 ``````from __future__ import absolute_import, division, print_function from ..compat import * `````` Philipp Arras committed Jun 27, 2018 21 22 23 24 ``````import numpy as np from scipy.stats import invgamma, norm from ..field import Field from ..sugar import makeOp `````` Martin Reinecke committed Jul 03, 2018 25 26 ``````from ..multi.multi_field import MultiField from ..models.model import Model `````` Philipp Arras committed Jun 27, 2018 27 `````` `````` Martin Reinecke committed Jul 03, 2018 28 ``````from ..operators.selection_operator import SelectionOperator `````` Philipp Arras committed Jun 27, 2018 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 committed Jul 02, 2018 44 `````` points = self.position['points'].local_data `````` Martin Reinecke committed Jul 07, 2018 45 `````` # MR FIXME?! `````` Philipp Arras committed Jun 27, 2018 46 `````` points = np.clip(points, None, 8.2) `````` Martin Reinecke committed Jul 02, 2018 47 `````` points = Field.from_local_data(self.position['points'].domain, points) `````` Philipp Arras committed Jun 27, 2018 48 49 50 51 `````` return self.IG(points, self._alpha, self._q) @property @memo `````` Martin Reinecke committed Jul 03, 2018 52 `````` def jacobian(self): `````` Martin Reinecke committed Jul 02, 2018 53 54 55 `````` u = self.position['points'].local_data inner = norm.pdf(u) outer_inv = invgamma.pdf(invgamma.ppf(norm.cdf(u), `````` Philipp Arras committed Jun 27, 2018 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 committed Jul 03, 2018 62 63 `````` grad = Field.from_local_data(self.position['points'].domain, inner*outer) `````` Martin Reinecke committed Jul 07, 2018 64 65 `````` grad = makeOp(MultiField.from_dict({"points": grad}, self.position._domain)) `````` Philipp Arras committed Jun 27, 2018 66 67 68 69 `````` return SelectionOperator(grad.target, 'points')*grad @staticmethod def IG(field, alpha, q): `````` Martin Reinecke committed Jul 02, 2018 70 71 `````` foo = invgamma.ppf(norm.cdf(field.local_data), alpha, scale=q) return Field.from_local_data(field.domain, foo) `````` Philipp Arras committed Jun 27, 2018 72 `````` `````` Martin Reinecke committed Jul 08, 2018 73 `````` # MR FIXME: is this function needed? `````` Philipp Arras committed Jun 27, 2018 74 75 `````` @staticmethod def IG_prime(field, alpha, q): `````` Martin Reinecke committed Jul 02, 2018 76 `````` inner = norm.pdf(field.local_data) `````` Martin Reinecke committed Jul 03, 2018 77 78 `````` outer = invgamma.pdf(invgamma.ppf(norm.cdf(field.local_data), alpha, scale=q), alpha, scale=q) `````` Philipp Arras committed Jun 27, 2018 79 80 `````` # # FIXME # outer = np.clip(outer, 1e-20, None) `````` Martin Reinecke committed Jul 08, 2018 81 `````` return Field.from_local_data(field.domain, inner/outer) `````` Philipp Arras committed Jun 27, 2018 82 `````` `````` Martin Reinecke committed Jul 08, 2018 83 `````` # MR FIXME: why does this take an np.ndarray instead of a Field? `````` Philipp Arras committed Jun 27, 2018 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``````