From 0885b6ad42dc8050887b7e6ac38d4dfea76a65be Mon Sep 17 00:00:00 2001 From: "Knollmueller, Jakob (kjako)" Date: Mon, 10 Jul 2017 14:23:40 +0200 Subject: [PATCH] added memo to PowerEnergy and changed check in smoothness and laplace from len(domain) != 0 to 1 --- demos/critical_filtering.py | 3 +- .../critical_filter/critical_power_energy.py | 31 ++++++++++++++----- .../laplace_operator/laplace_operator.py | 2 +- .../smoothness_operator.py | 4 +-- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/demos/critical_filtering.py b/demos/critical_filtering.py index a2d76c0a..8ca2cf92 100644 --- a/demos/critical_filtering.py +++ b/demos/critical_filtering.py @@ -1,6 +1,7 @@ from nifty import * - +from nifty.library.wiener_filter import WienerFilterEnergy +from nifty.library.critical_filter import CriticalPowerEnergy import plotly.offline as pl import plotly.graph_objs as go diff --git a/nifty/library/critical_filter/critical_power_energy.py b/nifty/library/critical_filter/critical_power_energy.py index 3f1021e7..bddac6bb 100644 --- a/nifty/library/critical_filter/critical_power_energy.py +++ b/nifty/library/critical_filter/critical_power_energy.py @@ -1,7 +1,7 @@ from nifty.energies.energy import Energy from nifty.operators.smoothness_operator import SmoothnessOperator from nifty.library.critical_filter import CriticalPowerCurvature - +from nifty.energies.memoization import memo from nifty.sugar import generate_posterior_sample from nifty import Field, exp @@ -77,22 +77,23 @@ class CriticalPowerEnergy(Energy): @property def value(self): - energy = exp(-self.position).vdot(self.q + self.w / 2., bare= True) - energy += self.position.vdot(self.alpha - 1. + self.rho / 2., bare=True) - energy += 0.5 * self.position.vdot(self.T(self.position)) + energy = self._theta.vdot(Field(self.position.domain,val=1.), bare= True) + energy += self.position.vdot(self._rho_prime, bare=True) + energy += 0.5 * self.position.vdot(self._Tt) return energy.real @property def gradient(self): - gradient = - self.theta.weight(-1) - gradient += (self.alpha - 1. + self.rho / 2.).weight(-1) - gradient += self.T(self.position) + gradient = - self._theta.weight(-1) + gradient += (self._rho_prime).weight(-1) + gradient += self._Tt gradient.val = gradient.val.real return gradient @property def curvature(self): - curvature = CriticalPowerCurvature(theta=self.theta.weight(-1), T=self.T, inverter=self.inverter) + curvature = CriticalPowerCurvature(theta=self._theta.weight(-1), T=self.T, + inverter=self.inverter) return curvature def _calculate_w(self, m, D, samples): @@ -114,4 +115,18 @@ class CriticalPowerEnergy(Energy): return w + @property + @memo + def _theta(self): + return (exp(-self.position) * (self.q + self.w / 2.)) + + @property + @memo + def _rho_prime(self): + return self.alpha - 1. + self.rho / 2. + + @property + @memo + def _Tt(self): + return self.T(self.position) diff --git a/nifty/operators/laplace_operator/laplace_operator.py b/nifty/operators/laplace_operator/laplace_operator.py index 2df42dd5..cce914cf 100644 --- a/nifty/operators/laplace_operator/laplace_operator.py +++ b/nifty/operators/laplace_operator/laplace_operator.py @@ -42,7 +42,7 @@ class LaplaceOperator(EndomorphicOperator): def __init__(self, domain, default_spaces=None, logarithmic=True): super(LaplaceOperator, self).__init__(default_spaces) self._domain = self._parse_domain(domain) - if len(self.domain) != 0: + if len(self.domain) != 1: raise ValueError("The domain must contain exactly one PowerSpace.") if not isinstance(self.domain[0], PowerSpace): diff --git a/nifty/operators/smoothness_operator/smoothness_operator.py b/nifty/operators/smoothness_operator/smoothness_operator.py index 4561b658..ae7efd29 100644 --- a/nifty/operators/smoothness_operator/smoothness_operator.py +++ b/nifty/operators/smoothness_operator/smoothness_operator.py @@ -31,7 +31,7 @@ class SmoothnessOperator(EndomorphicOperator): super(SmoothnessOperator, self).__init__(default_spaces=default_spaces) self._domain = self._parse_domain(domain) - if len(self.domain) != 0: + if len(self.domain) != 1: raise ValueError("The domain must contain exactly one PowerSpace.") if not isinstance(self.domain[0], PowerSpace): @@ -68,7 +68,7 @@ class SmoothnessOperator(EndomorphicOperator): return False def _times(self, x, spaces): - res = self._aplace.adjoint_times(self._laplace(x, spaces), spaces) + res = self._laplace.adjoint_times(self._laplace(x, spaces), spaces) return (1./self.sigma)**2*res # ---Added properties and methods--- -- GitLab