Commit 24c5b7c8 authored by Theo Steininger's avatar Theo Steininger

Merge branch 'working_on_demos' of gitlab.mpcdf.mpg.de:ift/NIFTy into working_on_demos

parents 65e28306 e47faf2d
......@@ -3,6 +3,7 @@ setup.cfg
.idea
.DS_Store
*.pyc
*.html
# from https://github.com/github/gitignore/blob/master/Python.gitignore
# Byte-compiled / optimized / DLL files
......
......@@ -11,18 +11,16 @@ rank = comm.rank
np.random.seed(42)
def plot_parameters(m,t,t_true, t_real, t_d):
def plot_parameters(m,t,p, p_d):
x = log(t.domain[0].kindex)
m = fft.adjoint_times(m)
m_data = m.val.get_full_data().real
t_data = t.val.get_full_data().real
t_d_data = t_d.val.get_full_data().real
t_true_data = t_true.val.get_full_data().real
t_real_data = t_real.val.get_full_data().real
pl.plot([go.Heatmap(z=m_data)], filename='map.html')
pl.plot([go.Scatter(x=x,y=t_data), go.Scatter(x=x ,y=t_true_data),
go.Scatter(x=x, y=t_real_data), go.Scatter(x=x, y=t_d_data)], filename="t.html")
m = m.val.get_full_data().real
t = t.val.get_full_data().real
p = p.val.get_full_data().real
p_d = p_d.val.get_full_data().real
pl.plot([go.Heatmap(z=m)], filename='map.html')
pl.plot([go.Scatter(x=x,y=t), go.Scatter(x=x ,y=p), go.Scatter(x=x, y=p_d)], filename="t.html")
class AdjointFFTResponse(LinearOperator):
......@@ -63,11 +61,11 @@ if __name__ == "__main__":
h_space = fft.target[0]
# Setting up power space
p_space = PowerSpace(h_space, logarithmic=False,
distribution_strategy=distribution_strategy)#, nbin=5)
p_space = PowerSpace(h_space, logarithmic=True,
distribution_strategy=distribution_strategy)
# Choosing the prior correlation structure and defining correlation operator
p_spec = (lambda k: (.05 / (k + 1) ** 3))
p_spec = (lambda k: (.5 / (k + 1) ** 3))
S = create_power_operator(h_space, power_spectrum=p_spec,
distribution_strategy=distribution_strategy)
......@@ -87,7 +85,7 @@ if __name__ == "__main__":
#Adding a harmonic transformation to the instrument
R = AdjointFFTResponse(fft, Instrument)
noise = .1
noise = 1.
N = DiagonalOperator(s_space, diagonal=noise, bare=True)
n = Field.from_random(domain=s_space,
random_type='normal',
......@@ -97,6 +95,8 @@ if __name__ == "__main__":
# Creating the mock data
d = R(sh) + n
# The information source
j = R.adjoint_times(N.inverse_times(d))
realized_power = log(sh.power_analyze(logarithmic=p_space.config["logarithmic"],
nbin=p_space.config["nbin"]))
data_power = log(fft(d).power_analyze(logarithmic=p_space.config["logarithmic"],
......@@ -112,21 +112,25 @@ if __name__ == "__main__":
print (x, iteration)
minimizer1 = RelaxedNewton(convergence_tolerance=0,
convergence_level=1,
minimizer1 = RelaxedNewton(convergence_tolerance=10e-2,
convergence_level=2,
iteration_limit=3,
callback=convergence_measure)
minimizer2 = VL_BFGS(convergence_tolerance=0,
iteration_limit=7,
callback=convergence_measure,
max_history_length=3)
inverter = ConjugateGradient(convergence_level=1,
convergence_tolerance=10e-4,
preconditioner=None)
# Setting starting position
flat_power = Field(p_space,val=10e-8)
m0 = flat_power.power_synthesize(real_signal=True)
# t0 = Field(p_space, val=log(1./(1+p_space.kindex)**2))
t0 = data_power- 1.
t0 = Field(p_space, val=log(1./(1+p_space.kindex)**2))
for i in range(500):
......@@ -134,50 +138,20 @@ if __name__ == "__main__":
distribution_strategy=distribution_strategy)
# Initializing the nonlinear Wiener Filter energy
map_energy = WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S0)
# Minimization with chosen minimizer
map_energy = map_energy.analytic_solution()
# Updating parameters for correlation structure reconstruction
m0 = map_energy.position
map_energy = WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S0, inverter=inverter)
# Solving the Wiener Filter analytically
D0 = map_energy.curvature
m0 = D0.inverse_times(j)
# Initializing the power energy with updated parameters
power_energy = CriticalPowerEnergy(position=t0, m=m0, D=D0, sigma=100., samples=5)
power_energy = CriticalPowerEnergy(position=t0, m=m0, D=D0, sigma=10., samples=3, inverter=inverter)
(power_energy, convergence) = minimizer1(power_energy)
# Setting new power spectrum
t0.val = power_energy.position.val.real
# Plotting current estimate
plot_parameters(m0,t0,log(sp),realized_power, data_power)
# Transforming fields to position space for plotting
plot_parameters(m0,t0,log(sp), data_power)
ss = fft.adjoint_times(sh)
m = fft.adjoint_times(map_energy.position)
# Plotting
d_data = d.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=d_data)], filename='data.html')
tt_data = power_energy.position.val.get_full_data().real
t_data = log(sp**2).val.get_full_data().real
if rank == 0:
pl.plot([go.Scatter(y=t_data),go.Scatter(y=tt_data)], filename="t.html")
ss_data = ss.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=ss_data)], filename='ss.html')
sh_data = sh.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=sh_data)], filename='sh.html')
m_data = m.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=m_data)], filename='map.html')
from nifty import *
import numpy as np
from nifty import Field,\
EndomorphicOperator,\
PowerSpace
import plotly.offline as pl
import plotly.graph_objs as go
import numpy as np
from nifty import Field, \
EndomorphicOperator, \
PowerSpace
class TestEnergy(Energy):
def __init__(self, position, Op):
super(TestEnergy, self).__init__(position)
self.Op = Op
def at(self, position):
return self.__class__(position=position, Op=self.Op)
@property
def value(self):
return 0.5 * self.position.dot(self.Op(self.position))
@property
def gradient(self):
return self.Op(self.position)
@property
def curvature(self):
curv = CurvOp(self.Op)
return curv
class CurvOp(InvertibleOperatorMixin, EndomorphicOperator):
def __init__(self, Op,inverter=None, preconditioner=None):
self.Op = Op
self._domain = Op.domain
super(CurvOp, self).__init__(inverter=inverter,
preconditioner=preconditioner)
def _times(self, x, spaces):
return self.Op(x)
if __name__ == "__main__":
distribution_strategy = 'not'
# Set up position space
s_space = RGSpace([128,128])
# s_space = HPSpace(32)
# Define harmonic transformation and associated harmonic space
fft = FFTOperator(s_space)
h_space = fft.target[0]
# Setting up power space
p_space = PowerSpace(h_space, logarithmic=False,
distribution_strategy=distribution_strategy, nbin=200)
# Choosing the prior correlation structure and defining correlation operator
pow_spec = (lambda k: (.05 / (k + 1) ** 2))
# t = Field(p_space, val=pow_spec)
t= Field.from_random("normal", domain=p_space)
lap = LaplaceOperator(p_space, logarithmic=True)
T = SmoothnessOperator(p_space,sigma=1., logarithmic=True)
test_energy = TestEnergy(t,T)
def convergence_measure(a_energy, iteration): # returns current energy
x = a_energy.value
print (x, iteration)
minimizer1 = VL_BFGS(convergence_tolerance=0,
iteration_limit=10,
callback=convergence_measure,
max_history_length=10)
def explicify(op, domain):
space = domain
d = space.dim
res = np.zeros((d, d))
for i in range(d):
x = np.zeros(d)
x[i] = 1.
f = Field(space, val=x)
res[:, i] = op(f).val
return res
A = explicify(lap.times, p_space)
B = explicify(lap.adjoint_times, p_space)
test_energy,convergence = minimizer1(test_energy)
data = test_energy.position.val.get_full_data()
pl.plot([go.Scatter(x=(p_space.kindex)[1:], y=data[1:])], filename="t.html")
tt = Field.from_random("normal", domain=t.domain)
print "adjointness"
print t.dot(lap(tt))
print tt.dot(lap.adjoint_times(t))
print "log kindex"
aa = Field(p_space, val=p_space.kindex.copy())
aa.val[0] = 1
print lap(log(aa)**2).val
print "######################"
print test_energy.position.val
\ No newline at end of file
from nifty import *
import plotly.offline as pl
import plotly.graph_objs as go
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.rank
np.random.seed(62)
class NonlinearResponse(LinearOperator):
def __init__(self, FFT, Instrument, function, derivative, default_spaces=None):
super(NonlinearResponse, self).__init__(default_spaces)
self._domain = FFT.target
self._target = Instrument.target
self.function = function
self.derivative = derivative
self.I = Instrument
self.FFT = FFT
def _times(self, x, spaces=None):
return self.I(self.function(self.FFT.adjoint_times(x)))
def _adjoint_times(self, x, spaces=None):
return self.FFT(self.function(self.I.adjoint_times(x)))
def derived_times(self, x, position):
position_derivative = self.derivative(self.FFT.adjoint_times(position))
return self.I(position_derivative * self.FFT.adjoint_times(x))
def derived_adjoint_times(self, x, position):
position_derivative = self.derivative(self.FFT.adjoint_times(position))
return self.FFT(position_derivative * self.I.adjoint_times(x))
@property
def domain(self):
return self._domain
@property
def target(self):
return self._target
@property
def unitary(self):
return False
def plot_parameters(m,t, t_real):
m = fft.adjoint_times(m)
x = log(t.domain[0].kindex[1:])
m_data = m.val.get_full_data().real
t_data = t.val.get_full_data().real
t_real_data = t_real.val.get_full_data().real
pl.plot([go.Scatter(y=m_data)], filename='map.html')
pl.plot([go.Scatter(x = x,y=t_data),
go.Scatter(x= x, y=t_real_data[1:])], filename="t.html")
class AdjointFFTResponse(LinearOperator):
def __init__(self, FFT, R, default_spaces=None):
super(AdjointFFTResponse, self).__init__(default_spaces)
self._domain = FFT.target
self._target = R.target
self.R = R
self.FFT = FFT
def _times(self, x, spaces=None):
return self.R(self.FFT.adjoint_times(x))
def _adjoint_times(self, x, spaces=None):
return self.FFT(self.R.adjoint_times(x))
@property
def domain(self):
return self._domain
@property
def target(self):
return self._target
@property
def unitary(self):
return False
if __name__ == "__main__":
distribution_strategy = 'not'
full_data = np.genfromtxt("train_data.csv", delimiter = ',')
d = full_data.T[2]
d[0] = 0.
d -= d.mean()
d[0] = 0.
# Set up position space
s_space = RGSpace([len(d)])
# s_space = HPSpace(32)
d = Field(s_space, val=d)
# Define harmonic transformation and associated harmonic space
fft = FFTOperator(s_space)
h_space = fft.target[0]
p_space = PowerSpace(h_space, logarithmic=False,
distribution_strategy=distribution_strategy)#, nbin=50)
# Choosing the measurement instrument
# Instrument = SmoothingOperator(s_space, sigma=0.01)
Instrument = DiagonalOperator(s_space, diagonal=1.)
# Instrument._diagonal.val[200:400, 200:400] = 0
#Adding a harmonic transformation to the instrument
R = AdjointFFTResponse(fft, Instrument)
noise = .1
N = DiagonalOperator(s_space, diagonal=noise, bare=True)
d_data = d.val.get_full_data().real
if rank == 0:
pl.plot([go.Scatter(y=d_data)], filename='data.html')
# Choosing the minimization strategy
def convergence_measure(a_energy, iteration): # returns current energy
x = a_energy.value
print (x, iteration)
# minimizer1 = SteepestDescent(convergence_tolerance=0,
# iteration_limit=50,
# callback=convergence_measure)
minimizer1 = RelaxedNewton(convergence_tolerance=0,
convergence_level=1,
iteration_limit=2,
callback=convergence_measure)
minimizer2 = RelaxedNewton(convergence_tolerance=0,
convergence_level=1,
iteration_limit=30,
callback=convergence_measure)
minimizer3 = VL_BFGS(convergence_tolerance=0,
iteration_limit=50,
callback=convergence_measure,
max_history_length=10)
# Setting starting position
flat_power = Field(p_space,val=10e-8)
m0 = flat_power.power_synthesize(real_signal=True)
t0 = Field(p_space, val=log(1./(1+p_space.kindex)**2))
t0 = Field(p_space,val=-13.)
# t0 = log(sp.copy()**2)
S0 = create_power_operator(h_space, power_spectrum=exp(t0),
distribution_strategy=distribution_strategy)
data_power = log(fft(d).power_analyze(logarithmic=p_space.config["logarithmic"],
nbin=p_space.config["nbin"]))
for i in range(500):
S0 = create_power_operator(h_space, power_spectrum=exp(t0),
distribution_strategy=distribution_strategy)
# Initializing the nonlinear Wiener Filter energy
map_energy = WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S0)
# Minimization with chosen minimizer
map_energy = map_energy.analytic_solution()
# Updating parameters for correlation structure reconstruction
m0 = map_energy.position
D0 = map_energy.curvature
# Initializing the power energy with updated parameters
power_energy = CriticalPowerEnergy(position=t0, m=m0, D=D0, sigma=100000., samples=20)
(power_energy, convergence) = minimizer3(power_energy)
# Setting new power spectrum
t0.val = power_energy.position.val.real
# t0.val[-1] = t0.val[-2]
# Plotting current estimate
plot_parameters(m0, t0, data_power)
# Transforming fields to position space for plotting
ss = fft.adjoint_times(sh)
m = fft.adjoint_times(map_energy.position)
# Plotting
d_data = d.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=d_data)], filename='data.html')
tt_data = power_energy.position.val.get_full_data().real
t_data = log(sp**2).val.get_full_data().real
if rank == 0:
pl.plot([go.Scatter(y=t_data),go.Scatter(y=tt_data)], filename="t.html")
ss_data = ss.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=ss_data)], filename='ss.html')
sh_data = sh.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=sh_data)], filename='sh.html')
m_data = m.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=m_data)], filename='map.html')
f_m_data = function(m).val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=f_m_data)], filename='f_map.html')
f_ss_data = function(ss).val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=f_ss_data)], filename='f_ss.html')
......@@ -42,7 +42,7 @@ if __name__ == "__main__":
distribution_strategy = 'not'
# Set up position space
s_space = RGSpace([512,512])
s_space = RGSpace([128,128])
# s_space = HPSpace(32)
# Define harmonic transformation and associated harmonic space
......@@ -80,6 +80,7 @@ if __name__ == "__main__":
# Creating the mock data
d = R(sh) + n
j = R.adjoint_times(N.inverse_times(d))
# Choosing the minimization strategy
......@@ -101,46 +102,27 @@ if __name__ == "__main__":
# max_history_length=3)
#
inverter = ConjugateGradient(convergence_level=3,
convergence_tolerance=10e-5,
preconditioner=None)
# Setting starting position
m0 = Field(h_space, val=.0)
# Initializing the Wiener Filter energy
energy = WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S)
energy = WienerFilterEnergy(position=m0, d=d, R=R, N=N, S=S, inverter=inverter)
D0 = energy.curvature
# Solving the problem analytically
solution = energy.analytic_solution()
# Solving the problem with chosen minimization strategy
(energy, convergence) = minimizer(energy)
# Transforming fields to position space for plotting
ss = fft.adjoint_times(sh)
m = fft.adjoint_times(energy.position)
m_wf = fft.adjoint_times(solution.position)
# Plotting
d_data = d.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=d_data)], filename='data.html')
ss_data = ss.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=ss_data)], filename='ss.html')
sh_data = sh.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=sh_data)], filename='sh.html')
m_data = m.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=m_data)], filename='map.html')
m_wf_data = m_wf.val.get_full_data().real
if rank == 0:
pl.plot([go.Heatmap(z=m_wf_data)], filename='map_wf.html')
m0 = D0.inverse_times(j)
sample_variance = Field(sh.domain,val=0. + 0j)
sample_mean = Field(sh.domain,val=0. + 0j)
# sampling the uncertainty map
n_samples = 1
for i in range(n_samples):
sample = sugar.generate_posterior_sample(m0,D0)
sample_variance += sample**2
sample_mean += sample
variance = sample_variance/n_samples - (sample_mean/n_samples)
......@@ -67,11 +67,3 @@ if __name__ == "__main__":
D = PropagatorOperator(S=S, N=N, R=R)
m = D(j)
d_data = d.val.get_full_data().real
m_data = m.val.get_full_data().real
ss_data = ss.val.get_full_data().real
# if rank == 0:
# pl.plot([go.Heatmap(z=d_data)], filename='data.html')
# pl.plot([go.Heatmap(z=m_data)], filename='map.html')
# pl.plot([go.Heatmap(z=ss_data)], filename='map_orig.html')
......@@ -109,7 +109,7 @@ class LineEnergy(Energy):
@property
def gradient(self):
return self.energy.gradient.dot(self.line_direction)
return self.energy.gradient.vdot(self.line_direction)
@property
def curvature(self):
......
......@@ -1040,7 +1040,7 @@ class Field(Loggable, Versionable, object):
new_field.set_val(new_val=new_val, copy=False)
return new_field
def dot(self, x=None, spaces=None, bare=False):
def vdot(self, x=None, spaces=None, bare=False):
""" Computes the volume-factor-aware dot product of 'self' with x.
Parameters
......@@ -1100,7 +1100,7 @@ class Field(Loggable, Versionable, object):
The Lq-norm of the field values.
"""
return np.sqrt(np.abs(self.dot(x=self)))
return np.sqrt(np.abs(self.vdot(x=self)))
def conjugate(self, inplace=False):
""" Retruns the complex conjugate of the field.
......
......@@ -42,10 +42,13 @@ class CriticalPowerEnergy(Energy):
The contribution from the map with or without uncertainty. It is used
to pass on the result of the costly sampling during the minimization.
default : None
inverter : ConjugateGradient
The inversion strategy to invert the curvature and to generate samples.
default : None
"""
def __init__(self, position, m, D=None, alpha =1.0, q=0., sigma=0.,
logarithmic = True, samples=3, w=None):
logarithmic = True, samples=3, w=None, inverter=None):
super(CriticalPowerEnergy, self).__init__(position = position)
self.m = m
self.D = D
......@@ -56,11 +59,13 @@ class CriticalPowerEnergy(Energy):
self.T = SmoothnessOperator(domain=self.position.domain[0], sigma=self.sigma,
logarithmic=logarithmic)
self.rho = self.position.domain[0].rho
self.inverter = inverter