Commit c7e31fc6 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

Merge branch 'plotclass' into 'NIFTy_5'

Plot class

See merge request ift/nifty-dev!94
parents 2547e3d0 0a61a3de
......@@ -79,8 +79,9 @@ if __name__ == '__main__':
reconstruction = sky(H.position)
ift.plot(reconstruction, title='reconstruction')
ift.plot(GR.adjoint_times(data), title='data')
ift.plot(sky(mock_position), title='truth')
ift.plot_finish(nx=3, xsize=16, ysize=5, title="results",
plot = ift.Plot()
plot.add(reconstruction, title='reconstruction')
plot.add(GR.adjoint_times(data), title='data')
plot.add(sky(mock_position), title='truth')
plot.output(nx=3, xsize=16, ysize=5, title="results",
......@@ -103,18 +103,17 @@ if __name__ == '__main__':
rg = isinstance(position_space, ift.RGSpace)
plot = ift.Plot()
if rg and len(position_space.shape) == 1:
ift.plot([HT(MOCK_SIGNAL), GR.adjoint(data), HT(m)],
plot.add([HT(MOCK_SIGNAL), GR.adjoint(data), HT(m)],
label=['Mock signal', 'Data', 'Reconstruction'],
alpha=[1, .3, 1])
ift.plot(mask_to_nan(mask, HT(m-MOCK_SIGNAL)), title='Residuals')
ift.plot_finish(nx=2, ny=1, xsize=10, ysize=4,
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")
ift.plot(HT(MOCK_SIGNAL), title='Mock Signal')
ift.plot(mask_to_nan(mask, (GR(Mask)).adjoint(data)),
plot.add(HT(MOCK_SIGNAL), title='Mock Signal')
plot.add(mask_to_nan(mask, (GR(Mask)).adjoint(data)),
ift.plot(HT(m), title='Reconstruction')
ift.plot(mask_to_nan(mask, HT(m-MOCK_SIGNAL)), title='Residuals')
ift.plot_finish(nx=2, ny=2, xsize=10, ysize=10,
plot.add(HT(m), title='Reconstruction')
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")
......@@ -99,8 +99,9 @@ if __name__ == '__main__':
# Plot results
signal = sky(mock_position)
reconst = sky(H.position)
ift.plot(signal, title='Signal')
ift.plot(GR.adjoint(data), title='Data')
ift.plot(reconst, title='Reconstruction')
ift.plot(reconst - signal, title='Residuals')
ift.plot_finish(name='getting_started_2.png', xsize=16, ysize=16)
plot = ift.Plot()
plot.add(signal, title='Signal')
plot.add(GR.adjoint(data), title='Data')
plot.add(reconst, title='Reconstruction')
plot.add(reconst - signal, title='Residuals')
plot.output(name='getting_started_2.png', xsize=16, ysize=16)
......@@ -82,10 +82,11 @@ if __name__ == '__main__':
INITIAL_POSITION = ift.from_random('normal', domain)
ift.plot(signal(MOCK_POSITION), title='Ground Truth')
ift.plot(R.adjoint_times(data), title='Data')
ift.plot([A(MOCK_POSITION)], title='Power Spectrum')
ift.plot_finish(ny=1, nx=3, xsize=24, ysize=6, name="setup.png")
plot = ift.Plot()
plot.add(signal(MOCK_POSITION), title='Ground Truth')
plot.add(R.adjoint_times(data), title='Data')
plot.add([A(MOCK_POSITION)], title='Power Spectrum')
plot.output(ny=1, nx=3, xsize=24, ysize=6, name="setup.png")
# number of samples used to estimate the KL
N_samples = 20
......@@ -100,18 +101,20 @@ if __name__ == '__main__':
KL, convergence = minimizer(KL)
position = KL.position
ift.plot(signal(position), title="reconstruction")
ift.plot([A(position), A(MOCK_POSITION)], title="power")
ift.plot_finish(ny=1, ysize=6, xsize=16, name="loop.png")
plot = ift.Plot()
plot.add(signal(position), title="reconstruction")
plot.add([A(position), A(MOCK_POSITION)], title="power")
plot.output(ny=1, ysize=6, xsize=16, name="loop.png")
plot = ift.Plot()
sc = ift.StatCalculator()
for sample in samples:
sc.add(signal(sample + position))
ift.plot(sc.mean, title="Posterior Mean")
ift.plot(ift.sqrt(sc.var), title="Posterior Standard Deviation")
plot.add(sc.mean, title="Posterior Mean")
plot.add(ift.sqrt(sc.var), title="Posterior Standard Deviation")
powers = [A(s + position) for s in samples]
[A(position), A(MOCK_POSITION)] + powers,
powers = [A(s+position) for s in samples]
[A(position), A(MOCK_POSITION)]+powers,
title="Sampled Posterior Power Spectrum")
ift.plot_finish(ny=1, nx=3, xsize=24, ysize=6, name="results.png")
plot.output(ny=1, nx=3, xsize=24, ysize=6, name="results.png")
......@@ -20,21 +20,24 @@ def plot_test():
# Start various plotting tests
ift.plot(field_rg1_1, title='Single plot')
ift.plot(field_rg2, title='2d rg')
ift.plot([field_rg1_1, field_rg1_2], title='list 1d rg', label=['1', '2'])
ift.plot(field_rg1_2, title='1d rg, xmin, ymin', xmin=0.5, ymin=0.,
plot = ift.Plot()
plot.add(field_rg1_1, title='Single plot')
plot = ift.Plot()
plot.add(field_rg2, title='2d rg')
plot.add([field_rg1_1, field_rg1_2], title='list 1d rg', label=['1', '2'])
plot.add(field_rg1_2, title='1d rg, xmin, ymin', xmin=0.5, ymin=0.,
xlabel='xmin=0.5', ylabel='ymin=0')
ift.plot_finish(title='Three plots')
ift.plot(field_hp, title='HP planck-color', colormap='Planck-like')
ift.plot(field_rg1_2, title='1d rg')
ift.plot(field_gl, title='GL')
ift.plot(field_rg2, title='2d rg')
ift.plot_finish(nx=2, ny=3, title='Five plots')
plot.output(title='Three plots')
plot = ift.Plot()
plot.add(field_hp, title='HP planck-color', colormap='Planck-like')
plot.add(field_rg1_2, title='1d rg')
plot.add(field_gl, title='GL')
plot.add(field_rg2, title='2d rg')
plot.output(nx=2, ny=3, title='Five plots')
if __name__ == '__main__':
......@@ -69,7 +69,7 @@ from .minimization.line_energy import LineEnergy
from .minimization.energy_adapter import EnergyAdapter
from .sugar import *
from .plotting.plot import plot, plot_finish
from .plotting.plot import Plot
from .library.amplitude_model import AmplitudeModel
from .library.inverse_gamma_model import InverseGammaModel
......@@ -262,11 +262,13 @@ def _plot(f, ax, **kwargs):
raise ValueError("Field type not(yet) supported")
_plots = []
_kwargs = []
class Plot(object):
def __init__(self):
self._plots = []
self._kwargs = []
def plot(f, **kwargs):
def add(self, f, **kwargs):
"""Add a figure to the current list of plots.
......@@ -279,8 +281,8 @@ def plot(f, **kwargs):
f: Field, or list of Field objects
If `f` is a single Field, it must live over a single `RGSpace`,
`PowerSpace`, `HPSpace`, `GLSPace`.
If it is a list, all list members must be Fields living over the same
one-dimensional `RGSpace` or `PowerSpace`.
If it is a list, all list members must be Fields living over the
same one-dimensional `RGSpace` or `PowerSpace`.
title: string
title of the plot
xlabel: string
......@@ -298,11 +300,11 @@ def plot(f, **kwargs):
alpha: float or list of floats
transparency value
def plot_finish(**kwargs):
def output(self, **kwargs):
"""Plot the accumulated list of figures.
......@@ -318,9 +320,8 @@ def plot_finish(**kwargs):
otherwise it will be written to a file with the given name.
Supported extensions: .png and .pdf
global _plots, _kwargs
import matplotlib.pyplot as plt
nplot = len(_plots)
nplot = len(self._plots)
fig = plt.figure()
if "title" in kwargs:
......@@ -336,8 +337,6 @@ def plot_finish(**kwargs):
fig.set_size_inches(xsize, ysize)
for i in range(nplot):
ax = fig.add_subplot(ny, nx, i+1)
_plot(_plots[i], ax, **_kwargs[i])
_plot(self._plots[i], ax, **self._kwargs[i])
_makeplot(kwargs.pop("name", None))
_plots = []
_kwargs = []
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