Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ift
NIFTy
Commits
c7e31fc6
Commit
c7e31fc6
authored
Aug 24, 2018
by
Martin Reinecke
Browse files
Merge branch 'plotclass' into 'NIFTy_5'
Plot class See merge request ift/nifty-dev!94
parents
2547e3d0
0a61a3de
Changes
7
Hide whitespace changes
Inline
Side-by-side
demos/bernoulli_demo.py
View file @
c7e31fc6
...
...
@@ -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"
,
name
=
"bernoulli.png"
)
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"
,
name
=
"bernoulli.png"
)
demos/getting_started_1.py
View file @
c7e31fc6
...
...
@@ -103,18 +103,17 @@ if __name__ == '__main__':
# PLOTTING
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
,
title
=
"getting_started_1"
)
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"
)
else
:
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
)),
title
=
'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
,
title
=
"getting_started_1"
)
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"
)
demos/getting_started_2.py
View file @
c7e31fc6
...
...
@@ -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
)
demos/getting_started_3.py
View file @
c7e31fc6
...
...
@@ -82,10 +82,11 @@ if __name__ == '__main__':
INITIAL_POSITION
=
ift
.
from_random
(
'normal'
,
domain
)
position
=
INITIAL_POSITION
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"
)
sc
.
add
(
signal
(
sample
+
position
))
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
]
ift
.
plot
(
[
A
(
position
),
A
(
MOCK_POSITION
)]
+
powers
,
powers
=
[
A
(
s
+
position
)
for
s
in
samples
]
plot
.
add
(
[
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"
)
demos/plot_test.py
View file @
c7e31fc6
...
...
@@ -20,21 +20,24 @@ def plot_test():
# Start various plotting tests
ift
.
plot
(
field_rg1_1
,
title
=
'Single plot'
)
ift
.
plot_finish
()
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
.
output
()
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_ps
)
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_ps
)
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__'
:
...
...
nifty5/__init__.py
View file @
c7e31fc6
...
...
@@ -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
...
...
nifty5/plotting/plot.py
View file @
c7e31fc6
...
...
@@ -262,82 +262,81 @@ def _plot(f, ax, **kwargs):
raise
ValueError
(
"Field type not(yet) supported"
)
_plots
=
[]
_kwargs
=
[]
def
plot
(
f
,
**
kwargs
):
"""Add a figure to the current list of plots.
Notes
-----
After doing one or more calls `plot()`, one also needs to call
`plot_finish()` to output the result.
Parameters
----------
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`.
title: string
title of the plot
xlabel: string
label for the x axis
ylabel: string
label for the y axis
[xyz]min, [xyz]max: float
limits for the values to plot
colormap: string
color map to use for the plot (if it is a 2D plot)
linewidth: float or list of floats
line width
label: string of list of strings
annotation string
alpha: float or list of floats
transparency value
"""
_plots
.
append
(
f
)
_kwargs
.
append
(
kwargs
)
def
plot_finish
(
**
kwargs
):
"""Plot the accumulated list of figures.
Parameters
----------
title: string
title of the full plot
nx, ny: integer (default: square root of the numer of plots, rounded up)
number of subplots to use in x- and y-direction
xsize, ysize: float (default: 6)
dimensions of the full plot in inches
name: string (default: "")
if left empty, the plot will be shown on the screen,
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
)
fig
=
plt
.
figure
()
if
"title"
in
kwargs
:
plt
.
suptitle
(
kwargs
.
pop
(
"title"
))
nx
=
kwargs
.
pop
(
"nx"
,
int
(
np
.
ceil
(
np
.
sqrt
(
nplot
))))
ny
=
kwargs
.
pop
(
"ny"
,
int
(
np
.
ceil
(
np
.
sqrt
(
nplot
))))
if
nx
*
ny
<
nplot
:
raise
ValueError
(
'Figure dimensions not sufficient for number of plots. '
'Available plot slots: {}, number of plots: {}'
.
format
(
nx
*
ny
,
nplot
))
xsize
=
kwargs
.
pop
(
"xsize"
,
6
)
ysize
=
kwargs
.
pop
(
"ysize"
,
6
)
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
])
fig
.
tight_layout
()
_makeplot
(
kwargs
.
pop
(
"name"
,
None
))
_plots
=
[]
_kwargs
=
[]
class
Plot
(
object
):
def
__init__
(
self
):
self
.
_plots
=
[]
self
.
_kwargs
=
[]
def
add
(
self
,
f
,
**
kwargs
):
"""Add a figure to the current list of plots.
Notes
-----
After doing one or more calls `plot()`, one also needs to call
`plot_finish()` to output the result.
Parameters
----------
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`.
title: string
title of the plot
xlabel: string
label for the x axis
ylabel: string
label for the y axis
[xyz]min, [xyz]max: float
limits for the values to plot
colormap: string
color map to use for the plot (if it is a 2D plot)
linewidth: float or list of floats
line width
label: string of list of strings
annotation string
alpha: float or list of floats
transparency value
"""
self
.
_plots
.
append
(
f
)
self
.
_kwargs
.
append
(
kwargs
)
def
output
(
self
,
**
kwargs
):
"""Plot the accumulated list of figures.
Parameters
----------
title: string
title of the full plot
nx, ny: integer (default: square root of the numer of plots, rounded up)
number of subplots to use in x- and y-direction
xsize, ysize: float (default: 6)
dimensions of the full plot in inches
name: string (default: "")
if left empty, the plot will be shown on the screen,
otherwise it will be written to a file with the given name.
Supported extensions: .png and .pdf
"""
import
matplotlib.pyplot
as
plt
nplot
=
len
(
self
.
_plots
)
fig
=
plt
.
figure
()
if
"title"
in
kwargs
:
plt
.
suptitle
(
kwargs
.
pop
(
"title"
))
nx
=
kwargs
.
pop
(
"nx"
,
int
(
np
.
ceil
(
np
.
sqrt
(
nplot
))))
ny
=
kwargs
.
pop
(
"ny"
,
int
(
np
.
ceil
(
np
.
sqrt
(
nplot
))))
if
nx
*
ny
<
nplot
:
raise
ValueError
(
'Figure dimensions not sufficient for number of plots. '
'Available plot slots: {}, number of plots: {}'
.
format
(
nx
*
ny
,
nplot
))
xsize
=
kwargs
.
pop
(
"xsize"
,
6
)
ysize
=
kwargs
.
pop
(
"ysize"
,
6
)
fig
.
set_size_inches
(
xsize
,
ysize
)
for
i
in
range
(
nplot
):
ax
=
fig
.
add_subplot
(
ny
,
nx
,
i
+
1
)
_plot
(
self
.
_plots
[
i
],
ax
,
**
self
.
_kwargs
[
i
])
fig
.
tight_layout
()
_makeplot
(
kwargs
.
pop
(
"name"
,
None
))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment