Commit f3f34136 authored by Martin Reinecke's avatar Martin Reinecke

Merge branch 'polishing_demos' into 'NIFTy_5'

Polishing demos

See merge request ift/nifty-dev!223
parents c948dcb5 23d2257c
...@@ -51,10 +51,15 @@ def mask_to_nan(mask, field): ...@@ -51,10 +51,15 @@ def mask_to_nan(mask, field):
if __name__ == '__main__': if __name__ == '__main__':
import sys
np.random.seed(42) np.random.seed(42)
# Choose space on which the signal field is defined # Choose space on which the signal field is defined
if len(sys.argv) == 2:
mode = int(sys.argv[1])
else:
mode = 1 mode = 1
if mode == 0: if mode == 0:
# One-dimensional regular grid # One-dimensional regular grid
position_space = ift.RGSpace([1024]) position_space = ift.RGSpace([1024])
...@@ -135,6 +140,7 @@ if __name__ == '__main__': ...@@ -135,6 +140,7 @@ if __name__ == '__main__':
# Plotting # Plotting
rg = isinstance(position_space, ift.RGSpace) rg = isinstance(position_space, ift.RGSpace)
plot = ift.Plot() plot = ift.Plot()
filename = f"getting_started_1_mode_{mode}.png"
if rg and len(position_space.shape) == 1: if rg and len(position_space.shape) == 1:
plot.add( plot.add(
[HT(MOCK_SIGNAL), GR.adjoint(data), [HT(MOCK_SIGNAL), GR.adjoint(data),
...@@ -142,10 +148,11 @@ if __name__ == '__main__': ...@@ -142,10 +148,11 @@ if __name__ == '__main__':
label=['Mock signal', 'Data', 'Reconstruction'], label=['Mock signal', 'Data', 'Reconstruction'],
alpha=[1, .3, 1]) alpha=[1, .3, 1])
plot.add(mask_to_nan(mask, HT(m - MOCK_SIGNAL)), title='Residuals') plot.add(mask_to_nan(mask, HT(m - MOCK_SIGNAL)), title='Residuals')
plot.output(nx=2, ny=1, xsize=10, ysize=4, title="getting_started_1") plot.output(nx=2, ny=1, xsize=10, ysize=4, name=filename)
else: else:
plot.add(HT(MOCK_SIGNAL), title='Mock Signal') plot.add(HT(MOCK_SIGNAL), title='Mock Signal')
plot.add(mask_to_nan(mask, (GR(Mask)).adjoint(data)), title='Data') plot.add(mask_to_nan(mask, (GR(Mask)).adjoint(data)), title='Data')
plot.add(HT(m), title='Reconstruction') plot.add(HT(m), title='Reconstruction')
plot.add(mask_to_nan(mask, HT(m - MOCK_SIGNAL)), title='Residuals') plot.add(mask_to_nan(mask, HT(m - MOCK_SIGNAL)), title='Residuals')
plot.output(nx=2, ny=2, xsize=10, ysize=10, title="getting_started_1") plot.output(nx=2, ny=2, xsize=10, ysize=10, name=filename)
print(f"Saved results as '{filename}'.")
...@@ -42,11 +42,16 @@ def exposure_2d(): ...@@ -42,11 +42,16 @@ def exposure_2d():
if __name__ == '__main__': if __name__ == '__main__':
import sys
# FIXME All random seeds to 42 # FIXME All random seeds to 42
np.random.seed(42) np.random.seed(42)
# Choose space on which the signal field is defined # Choose space on which the signal field is defined
if len(sys.argv) == 2:
mode = int(sys.argv[1])
else:
mode = 1 mode = 1
if mode == 0: if mode == 0:
# One-dimensional regular grid with uniform exposure of 10 # One-dimensional regular grid with uniform exposure of 10
position_space = ift.RGSpace(1024) position_space = ift.RGSpace(1024)
...@@ -107,9 +112,11 @@ if __name__ == '__main__': ...@@ -107,9 +112,11 @@ if __name__ == '__main__':
# Plotting # Plotting
signal = sky(mock_position) signal = sky(mock_position)
reconst = sky(H.position) reconst = sky(H.position)
filename = f"getting_started_2_mode_{mode}.png"
plot = ift.Plot() plot = ift.Plot()
plot.add(signal, title='Signal') plot.add(signal, title='Signal')
plot.add(GR.adjoint(data), title='Data') plot.add(GR.adjoint(data), title='Data')
plot.add(reconst, title='Reconstruction') plot.add(reconst, title='Reconstruction')
plot.add(reconst - signal, title='Residuals') plot.add(reconst - signal, title='Residuals')
plot.output(name='getting_started_2.pdf', xsize=16, ysize=16) plot.output(xsize=12, ysize=10, name=filename)
print(f"Saved results as '{filename}'.")
...@@ -43,11 +43,16 @@ def radial_los(n_los): ...@@ -43,11 +43,16 @@ def radial_los(n_los):
if __name__ == '__main__': if __name__ == '__main__':
import sys
np.random.seed(420) # picked for a nice field realization np.random.seed(420) # picked for a nice field realization
# Choose between random line-of-sight response (mode=0) and radial lines # Choose between random line-of-sight response (mode=0) and radial lines
# of sight (mode=1) # of sight (mode=1)
if len(sys.argv) == 2:
mode = int(sys.argv[1])
else:
mode = 0 mode = 0
filename = f"getting_started_3_mode_{mode}_" + "{}.png"
position_space = ift.RGSpace([128, 128]) position_space = ift.RGSpace([128, 128])
harmonic_space = position_space.get_default_codomain() harmonic_space = position_space.get_default_codomain()
...@@ -113,7 +118,7 @@ if __name__ == '__main__': ...@@ -113,7 +118,7 @@ if __name__ == '__main__':
plot.add(signal(mock_position), title='Ground Truth') plot.add(signal(mock_position), title='Ground Truth')
plot.add(R.adjoint_times(data), title='Data') plot.add(R.adjoint_times(data), title='Data')
plot.add([A.force(mock_position)], title='Power Spectrum') plot.add([A.force(mock_position)], title='Power Spectrum')
plot.output(ny=1, nx=3, xsize=24, ysize=6, name="setup.png") plot.output(ny=1, nx=3, xsize=24, ysize=6, name=filename.format("setup"))
# number of samples used to estimate the KL # number of samples used to estimate the KL
N_samples = 20 N_samples = 20
...@@ -129,7 +134,8 @@ if __name__ == '__main__': ...@@ -129,7 +134,8 @@ if __name__ == '__main__':
plot = ift.Plot() plot = ift.Plot()
plot.add(signal(KL.position), title="reconstruction") plot.add(signal(KL.position), title="reconstruction")
plot.add([A.force(KL.position), A.force(mock_position)], title="power") plot.add([A.force(KL.position), A.force(mock_position)], title="power")
plot.output(ny=1, ysize=6, xsize=16, name="loop-{:02}.png".format(i)) plot.output(ny=1, ysize=6, xsize=16,
name=filename.format(f"loop_{i:02}"))
# Draw posterior samples # Draw posterior samples
KL = ift.MetricGaussianKL(mean, H, N_samples) KL = ift.MetricGaussianKL(mean, H, N_samples)
...@@ -138,6 +144,7 @@ if __name__ == '__main__': ...@@ -138,6 +144,7 @@ if __name__ == '__main__':
sc.add(signal(sample + KL.position)) sc.add(signal(sample + KL.position))
# Plotting # Plotting
filename_res = filename.format("results")
plot = ift.Plot() plot = ift.Plot()
plot.add(sc.mean, title="Posterior Mean") plot.add(sc.mean, title="Posterior Mean")
plot.add(ift.sqrt(sc.var), title="Posterior Standard Deviation") plot.add(ift.sqrt(sc.var), title="Posterior Standard Deviation")
...@@ -148,4 +155,5 @@ if __name__ == '__main__': ...@@ -148,4 +155,5 @@ if __name__ == '__main__':
A.force(mock_position)], A.force(mock_position)],
title="Sampled Posterior Power Spectrum", title="Sampled Posterior Power Spectrum",
linewidth=[1.]*len(powers) + [3., 3.]) linewidth=[1.]*len(powers) + [3., 3.])
plot.output(ny=1, nx=3, xsize=24, ysize=6, name="results.png") plot.output(ny=1, nx=3, xsize=24, ysize=6, name=filename_res)
print(f"Saved results as '{filename_res}'.")
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