Commit ab8fc3d0 authored by Natalia Porqueres's avatar Natalia Porqueres

Add comments

parent a9e95d30
...@@ -4,80 +4,142 @@ import numpy as np ...@@ -4,80 +4,142 @@ import numpy as np
import global_newton as gn import global_newton as gn
from nifty5.library.nonlinearities import Exponential from nifty5.library.nonlinearities import Exponential
np.random.seed(42) #DEFINE THE SIGNAL:
N = 2 #Define signal space as a regular grid
Nsamples = 5 #s_space = ift.RGSpace(10024)
s_space = ift.RGSpace([128,128])
s_space = ift.RGSpace(10024) #Define the harmonic space
h_space = s_space.get_default_codomain() h_space = s_space.get_default_codomain()
#Prepare Harmonic transformation between the two spaces
HT = ift.HarmonicTransformOperator(h_space, s_space)
#Define domain
domain = ift.MultiDomain.make({'xi': h_space}) domain = ift.MultiDomain.make({'xi': h_space})
#Define positions from a Gaussian distribution
position = ift.from_random('normal', domain) position = ift.from_random('normal', domain)
HT = ift.HarmonicTransformOperator(h_space, s_space)
#Define a power spectrum
def sqrtpspec(k): def sqrtpspec(k):
return 16. / (20.+k**2) return 16. / (20.+k**2)
#Define a power space
# Define amplitude model
p_space = ift.PowerSpace(h_space) p_space = ift.PowerSpace(h_space)
#Define the power distribution between the harmonic and the power spaces
pd = ift.PowerDistributor(h_space, p_space) pd = ift.PowerDistributor(h_space, p_space)
#Create a field with the defined power spectrum
a = ift.PS_field(p_space, sqrtpspec) a = ift.PS_field(p_space, sqrtpspec)
#Define the amplitudes
A = pd(a) A = pd(a)
# Define sky model #Unpack the positions xi from the Multifield
xi = ift.Variable(position)['xi'] xi = ift.Variable(position)['xi']
#Multiply the positions by the amplitudes in the harmonic domain
logsky_h = xi * A logsky_h = xi * A
#Transform to the real domain
logsky = HT(logsky_h) logsky = HT(logsky_h)
nonlin = Exponential()
#Create a sky model by applying the exponential (Poisson)
sky = ift.PointwiseExponential(logsky) sky = ift.PointwiseExponential(logsky)
R = ift.ScalingOperator(1., s_space)
#Define a mask to cover a patch of the real space
mask = np.ones(s_space.shape)
mask[int(s_space.shape[0]/3):int(s_space.shape[0]/3+10)] = 0.
#Convert the mask into a field
mask = ift.Field(s_space,val=mask)
#Create a diagonal matrix corresponding to the mask
M = ift.DiagonalOperator(mask)
#Create the response operator and apply the mask on it
R = ift.ScalingOperator(1., s_space) * M
#Define the data space
d_space =[0] d_space =[0]
#Apply the response operator to the signal
#lamb corresponds to the mean in the Poisson distribution
lamb = R(sky) lamb = R(sky)
# Generate mock data #Draw coordinates of the mock data from a Gaussian distribution
MOCK_POSITION = ift.from_random('normal', lamb.position.domain) mock_position = ift.from_random('normal', lamb.position.domain)
data = np.random.poisson(
#Generate mock data from a Poisson distribution using lamb as a mean
data = np.random.poisson(
#Store the data as a field
data = ift.Field.from_local_data(d_space, data) data = ift.Field.from_local_data(d_space, data)
# Define Hamiltonian #RECONSTRUCT THE SIGNAL:
#Define the positions where we perform the analysis from a Gaussian distribution
position = ift.from_random('normal', lamb.position.domain) position = ift.from_random('normal', lamb.position.domain)
#Define the Poisson likelihood knowing the mean and the data
likelihood = ift.library.PoissonLogLikelihood(lamb, data) likelihood = ift.library.PoissonLogLikelihood(lamb, data)
#Define a iteration controller with a maximum number of iterations
ic_cg = ift.GradientNormController(iteration_limit=50) ic_cg = ift.GradientNormController(iteration_limit=50)
#Define a iteration controller with convergence criteria
ic_samps = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-4) ic_samps = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-4)
#Define a iteration controller for the minimizer
ic_newton = ift.GradientNormController(name='Newton', tol_abs_gradnorm=1e-3) ic_newton = ift.GradientNormController(name='Newton', tol_abs_gradnorm=1e-3)
minimizer = ift.RelaxedNewton(ic_newton) minimizer = ift.RelaxedNewton(ic_newton)
#Build the Hamiltonian
H = gn.Hamiltonian(likelihood, ic_cg, ic_samps) H = gn.Hamiltonian(likelihood, ic_cg, ic_samps)
#Iterate the reconstruction
for _ in range(N): for _ in range(N):
#Draw samples from the curvature
samples = [H.curvature.draw_sample(from_inverse=True) samples = [H.curvature.draw_sample(from_inverse=True)
for _ in range(Nsamples)] for _ in range(Nsamples)]
sc_samplesky = ift.StatCalculator() sc_samplesky = ift.StatCalculator()
for s in samples: for s in samples:
sc_samplesky.add( sc_samplesky.add(
ift.plot(sc_samplesky.mean, name='sample_mean.png') ift.plot(sc_samplesky.mean, name='sample_mean.png')
#Compute the Kullbachh Leibler divergence
KL = gn.SampledKullbachLeiblerDivergence(H, samples, ic_cg) KL = gn.SampledKullbachLeiblerDivergence(H, samples, ic_cg)
KL, convergence = minimizer(KL)
#Update the position
position = KL.position position = KL.position
#Obtain the value of the KL and the convergence at the new position
KL, convergence = minimizer(KL)
#Evaluate lambda at final position
lamb_recontructed =
#Evaluate lambda at the data position for comparison
lamb_mock =
# Plot results #Plot the data, reconstruction and underlying signal
E = KL ift.plot([data, lamb_mock, lamb_recontructed], name="poisson.png",
l1 =
l2 =
ift.plot([data, l2, l1], name="poisson.png",
label=['Data', 'Mock signal', 'Reconstruction'], label=['Data', 'Mock signal', 'Reconstruction'],
alpha=[.5, 1, 1]) alpha=[.5, 1, 1])
if power_spectrum_estimation:
a_mock = #Plot power spectrum for posterior test
a_recon = a_mock = a
else: a_recon = a
a_mock = a ift.plot([a_mock**2, a_recon**2, ift.power_analyze(],
a_recon = a
ift.plot([a_mock**2, a_recon**2, ift.power_analyze(],
name='power_spectrum.png', label=['Mock', 'Reconstruction', 'power_analyze']) name='power_spectrum.png', label=['Mock', 'Reconstruction', 'power_analyze'])
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