Commit b3b3a72a authored by Philipp Arras's avatar Philipp Arras
Browse files

Do not pass inverters around anymore where it is not necessary

parent 0ac48f64
Pipeline #31375 failed with stages
in 4 minutes and 14 seconds
......@@ -18,6 +18,7 @@
import numpy as np
from ..minimization.conjugate_gradient import ConjugateGradient
from ..minimization.quadratic_energy import QuadraticEnergy
from .endomorphic_operator import EndomorphicOperator
......@@ -33,9 +34,9 @@ class SamplingEnabler(EndomorphicOperator):
The InversionEnabler object will support the same operation modes as
`op`, and additionally the inverse set. The newly-added modes will
be computed by iterative inversion.
inverter : :class:`Minimizer`
The minimizer to use for the iterative numerical inversion.
Typically, this is a :class:`ConjugateGradient` object.
iteration_controller : :class:`IterationController`
The iteration controller to use for the iterative numerical inversion
done by a :class:`ConjugateGradient` object.
approximation : :class:`LinearOperator`, optional
if not None, this operator should be an approximation to `op`, which
supports the operation modes that `op` doesn't have. It is used as a
......@@ -43,13 +44,13 @@ class SamplingEnabler(EndomorphicOperator):
convergence.
"""
def __init__(self, likelihood, prior, sampling_inverter,
def __init__(self, likelihood, prior, iteration_controller,
approximation=None):
self._op = likelihood + prior
super(SamplingEnabler, self).__init__()
self._likelihood = likelihood
self._prior = prior
self._sampling_inverter = sampling_inverter
self._ic = iteration_controller
self._approximation = approximation
def draw_sample(self, from_inverse=False, dtype=np.float64):
......@@ -61,8 +62,9 @@ class SamplingEnabler(EndomorphicOperator):
nj = self._likelihood.draw_sample()
energy = QuadraticEnergy(s, self._op, sp + nj,
_grad=self._likelihood(s) - nj)
energy, convergence = self._sampling_inverter(
energy, preconditioner=self._approximation.inverse)
inverter = ConjugateGradient(self._ic)
energy, convergence = inverter(energy,
preconditioner=self._approximation.inverse)
return energy.position
@property
......
......@@ -56,11 +56,10 @@ class Energy_Tests(unittest.TestCase):
IC = ift.GradientNormController(
iteration_limit=100,
tol_abs_gradnorm=1e-5)
inverter = ift.ConjugateGradient(IC)
S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
energy = ift.library.WienerFilterEnergy(
position=s0, d=d, R=R, N=N, S=S, inverter=inverter)
position=s0, d=d, R=R, N=N, S=S, iteration_controller=IC)
ift.extra.check_value_gradient_curvature_consistency(
energy, tol=1e-4, ntries=10)
......
......@@ -65,7 +65,6 @@ class Noise_Energy_Tests(unittest.TestCase):
IC = ift.GradientNormController(
iteration_limit=100,
tol_abs_gradnorm=1e-5)
inverter = ift.ConjugateGradient(IC)
S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
C = ift.library.NonlinearWienerFilterEnergy(
......@@ -77,7 +76,7 @@ class Noise_Energy_Tests(unittest.TestCase):
power=A,
N=N,
S=S,
inverter=inverter).curvature
iteration_controller=IC).curvature
res_sample_list = [d - R(f(ht(C.draw_sample(from_inverse=True) + xi)))
for _ in range(10)]
......
......@@ -57,7 +57,6 @@ class Energy_Tests(unittest.TestCase):
IC = ift.GradientNormController(
iteration_limit=100,
tol_abs_gradnorm=1e-5)
inverter = ift.ConjugateGradient(IC)
S = ift.create_power_operator(hspace, power_spectrum=_flat_PS)
D = ift.library.NonlinearWienerFilterEnergy(
......@@ -69,7 +68,7 @@ class Energy_Tests(unittest.TestCase):
N=N,
S=S,
ht=ht,
inverter=inverter).curvature
iteration_controller=IC).curvature
energy = ift.library.NonlinearPowerEnergy(
position=tau0,
......
......@@ -116,9 +116,7 @@ class Test_Minimizers(unittest.TestCase):
t1 = ift.GradientNormController(tol_abs_gradnorm=1e-5,
iteration_limit=1000)
t2 = ift.ConjugateGradient(controller=t1)
return ift.InversionEnabler(RBCurv(self._position),
inverter=t2)
return ift.InversionEnabler(RBCurv(self._position), t1)
try:
minimizer = eval(minimizer)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment