Commit 386d2f0e authored by Martin Reinecke's avatar Martin Reinecke
Browse files

first traces of new plotting

parent 102c7597
Pipeline #17978 failed with stage
in 4 minutes and 11 seconds
......@@ -66,12 +66,8 @@ if __name__ == "__main__":
m = wiener_curvature.inverse_times(j)
m_s = fft(m)
plotter = ift.plotting.RG2DPlotter()
plotter.path = 'mock_signal.html'
plotter(mock_signal.real)
plotter.path = 'data.html'
plotter(ift.Field(
signal_space,
val=data.val.real.reshape(signal_space.shape)))
plotter.path = 'map.html'
plotter(m_s.real)
ift.plotting.plot(mock_signal.real,"mock_signal.pdf")
ift.plotting.plot(mock_power.real,"power.pdf")
ift.plotting.plot(ift.Field(signal_space,
val=data.val.real.reshape(signal_space.shape)), "data.pdf")
ift.plotting.plot(m_s.real, "map.pdf")
from .descriptors import *
from .plots import *
from .figures import *
from .colormap import *
from .plotter import *
from .plot import plot
from .colormap import Colormap
from . import colormaps
from __future__ import division
from builtins import str
from ..plotly_wrapper import PlotlyWrapper
class Colormap(PlotlyWrapper):
def __init__(self, name, red, green, blue):
self.name = name
self.red = red
self.green = green
self.blue = blue
def validate_map(self):
def validade(m):
#TODO: implement validation
pass
# no discontinuities only
@staticmethod
def from_matplotlib_colormap_internal(name, mpl_cmap):
red = [(c[0], c[2]) for c in mpl_cmap['red']]
green = [(c[0], c[2]) for c in mpl_cmap['green']]
blue = [(c[0], c[2]) for c in mpl_cmap['blue']]
return Colormap(name, red, green, blue)
def to_plotly(self):
r, g, b = 0, 0, 0
converted = list()
prev_split, prev_r, prev_g, prev_b = 0., 0., 0., 0.
while prev_split < 1:
next_split = min(self.red[r][0], self.blue[b][0], self.green[g][0])
if next_split == self.red[r][0]:
red_val = self.red[r][1]
r += 1
else:
slope = (self.red[r][1]-prev_r) / (self.red[r][0] - prev_split)
y = prev_r - slope * prev_split
red_val = slope * next_split + y
if next_split == self.green[g][0]:
green_val = self.green[g][1]
g += 1
else:
slope = ((self.green[g][1] - prev_g) /
(self.green[g][0] - prev_split))
y = prev_g - slope * prev_split
green_val = slope * next_split + y
if next_split == self.blue[b][0]:
blue_val = self.blue[b][1]
b += 1
else:
slope = ((self.blue[b][1] - prev_b) /
(self.blue[b][0] - prev_split))
y = prev_r - slope * prev_split
blue_val = slope * next_split + y
prev_split = next_split
prev_r = red_val
prev_g = green_val
prev_b = blue_val
converted.append([next_split,
'rgb(' +
str(int(red_val*255)) + "," +
str(int(green_val*255)) + "," +
str(int(blue_val*255)) + ")"])
return converted
from ..colormap.colormap import Colormap
def HighEnergyCmap():
"""
Returns a color map often used in High Energy Astronomy.
"""
red = [(0.0, 0.0),
(0.167, 0.0),
(0.333, 0.5),
(0.5, 1.0),
(0.667, 1.0),
(0.833, 1.0),
(1.0, 1.0)]
green = [(0.0, 0.0),
(0.167, 0.0),
(0.333, 0.0),
(0.5, 0.0),
(0.667, 0.5),
(0.833, 1.0),
(1.0, 1.0)]
blue = [(0.0, 0.0),
(0.167, 1.0),
(0.333, 0.5),
(0.5, 0.0),
(0.667, 0.0),
(0.833, 0.0),
(1.0, 1.0)]
return Colormap("High Energy", red, green, blue)
def FaradayMapCmap():
"""
Returns a color map used in reconstruction of the "Faraday Map".
References
----------
.. [#] N. Opermann et. al.,
"An improved map of the Galactic Faraday sky",
Astronomy & Astrophysics, Volume 542, id.A93, 06/2012;
`arXiv:1111.6186 <http://www.arxiv.org/abs/1111.6186>`_
"""
red = [(0.0, 0.35),
(0.1, 0.4),
(0.2, 0.25),
(0.41, 0.47),
(0.5, 0.8),
(0.56, 0.96),
(0.59, 1.0),
(0.74, 0.8),
(0.8, 0.8),
(0.9, 0.5),
(1.0, 0.4)]
green = [(0.0, 0.0),
(0.2, 0.0),
(0.362, 0.88),
(0.5, 1.0),
(0.638, 0.88),
(0.8, 0.25),
(0.9, 0.3),
(1.0, 0.2)]
blue = [(0.0, 0.35),
(0.1, 0.4),
(0.2, 0.8),
(0.26, 0.8),
(0.41, 1.0),
(0.44, 0.96),
(0.5, 0.8),
(0.59, 0.47),
(0.8, 0.0),
(1.0, 0.0)]
return Colormap("Faraday Map", red, green, blue)
def FaradayUncertaintyCmap():
"""
Returns a color map used for the "Faraday Map Uncertainty".
References
----------
.. [#] N. Opermann et. al.,
"An improved map of the Galactic Faraday sky",
Astronomy & Astrophysics, Volume 542, id.A93, 06/2012;
`arXiv:1111.6186 <http://www.arxiv.org/abs/1111.6186>`_
"""
red = [(0.0, 1.0),
(0.1, 0.8),
(0.2, 0.65),
(0.41, 0.6),
(0.5, 0.7),
(0.56, 0.96),
(0.59, 1.0),
(0.74, 0.8),
(0.8, 0.8),
(0.9, 0.5),
(1.0, 0.4)]
green = [(0.0, 0.9),
(0.2, 0.65),
(0.362, 0.95),
(0.5, 1.0),
(0.638, 0.88),
(0.8, 0.25),
(0.9, 0.3),
(1.0, 0.2)]
blue = [(0.0, 1.0),
(0.1, 0.8),
(0.2, 1.0),
(0.41, 1.0),
(0.44, 0.96),
(0.5, 0.7),
(0.59, 0.42),
(0.8, 0.0),
(1.0, 0.0)]
return Colormap("Faraday Uncertainty", red, green, blue)
def PlusMinusCmap():
"""
Returns a color map useful for a zero-centerd range of values.
"""
red = [(0.0, 1.0),
(0.1, 0.96),
(0.2, 0.84),
(0.3, 0.64),
(0.4, 0.36),
(0.5, 0.0),
(0.6, 0.0),
(0.7, 0.0),
(0.8, 0.0),
(0.9, 0.0),
(1.0, 0.0)]
green = [(0.0, 0.5),
(0.1, 0.32),
(0.2, 0.18),
(0.3, 0.08),
(0.4, 0.02),
(0.5, 0.0),
(0.6, 0.02),
(0.7, 0.08),
(0.8, 0.18),
(0.9, 0.32),
(1.0, 0.5)]
blue = [(0.0, 0.0),
(0.1, 0.0),
(0.2, 0.0),
(0.3, 0.0),
(0.4, 0.0),
(0.5, 0.0),
(0.6, 0.36),
(0.7, 0.64),
(0.8, 0.84),
(0.9, 0.96),
(1.0, 1.0)]
return Colormap("Plus Minus", red, green, blue)
def PlankCmap():
"""
Returns a color map similar to the one used for the "Planck CMB Map".
"""
red = [(0.0, 0.0),
(0.1, 0.0),
(0.2, 0.0),
(0.3, 0.0),
(0.4, 0.0),
(0.5, 1.0),
(0.6, 1.0),
(0.7, 1.0),
(0.8, 0.83),
(0.9, 0.67),
(1.0, 0.5)]
green = [(0.0, 0.0),
(0.1, 0.0),
(0.2, 0.0),
(0.3, 0.3),
(0.4, 0.7),
(0.5, 1.0),
(0.6, 0.7),
(0.7, 0.3),
(0.8, 0.0),
(0.9, 0.0),
(1.0, 0.0)]
blue = [(0.0, 0.5),
(0.1, 0.67),
(0.2, 0.83),
(0.3, 1.0),
(0.4, 1.0),
(0.5, 1.0),
(0.6, 0.0),
(0.7, 0.0),
(0.8, 0.0),
(0.9, 0.0),
(1.0, 0.0)]
return Colormap("Planck-like", red, green, blue)
from .axis import Axis
from .line import Line
from .marker import Marker
# -*- coding: utf-8 -*-
from ..plotly_wrapper import PlotlyWrapper
class Axis(PlotlyWrapper):
def __init__(self, label=None, font='Balto', color='', log=False,
font_size=22, show_grid=True, visible=True):
self.label = str(label) if label is not None else None
self.font = font
self.color = color
self.log = log
self.font_size = int(font_size)
self.show_grid = show_grid
self.visible = visible
def to_plotly(self):
ply_object = dict()
if self.label:
ply_object.update(dict(
title=self.label,
titlefont=dict(
family=self.font,
color=self.color,
size=self.font_size
)
))
if self.log:
ply_object['type'] = 'log'
if not self.show_grid:
ply_object['showgrid'] = False
ply_object['visible'] = self.visible
ply_object['tickfont'] = {'size': self.font_size,
'family': self.font}
ply_object['exponentformat'] = 'power'
# ply_object['domain'] = {'0': '0.04',
# '1': '1'}
return ply_object
# -*- coding: utf-8 -*-
from ..plotly_wrapper import PlotlyWrapper
class Line(PlotlyWrapper):
def __init__(self, color=None, width=None):
self.color = color
self.width = width
def to_plotly(self):
return dict(color=self.color,
width=self.width)
# -*- coding: utf-8 -*-
from ..plotly_wrapper import PlotlyWrapper
class Marker(PlotlyWrapper):
# find symbols at: https://plot.ly/python/reference/#scatter-marker-symbol
def __init__(self, color=None, size=None, symbol=None, opacity=None):
self.color = color
self.size = size
self.symbol = symbol
self.opacity = opacity
def to_plotly(self):
return dict(color=self.color,
size=self.size,
symbol=self.symbol,
opacity=self.opacity)
from .figure_2D import Figure2D
from .figure_3D import Figure3D
from .multi_figure import MultiFigure
# -*- coding: utf-8 -*-
from .figure_from_plot import FigureFromPlot
from ..plots import Heatmap, HPMollweide, GLMollweide
class Figure2D(FigureFromPlot):
def __init__(self, plots, title=None, width=None, height=None,
xaxis=None, yaxis=None):
if plots is not None:
width = width if width is not None else plots[0].default_width()
height = \
height if height is not None else plots[0].default_height()
xaxis = xaxis if xaxis is not None else plots[0].default_axes()[0]
yaxis = yaxis if yaxis is not None else plots[0].default_axes()[1]
if isinstance(plots[0], Heatmap) and width is None and \
height is None:
(y, x) = plots[0].data.shape
width = 500
height = int(500*y/x)
if isinstance(plots[0], GLMollweide) or \
isinstance(plots[0], HPMollweide):
xaxis = False if (xaxis is None) else xaxis
yaxis = False if (yaxis is None) else yaxis
super(Figure2D, self).__init__(plots, title, width, height)
self.xaxis = xaxis
self.yaxis = yaxis
def at(self, plots, title=None):
title = title if title is not None else self.title
return Figure2D(plots=plots,
title=title,
width=self.width,
height=self.height,
xaxis=self.xaxis,
yaxis=self.yaxis)
def to_plotly(self):
plotly_object = super(Figure2D, self).to_plotly()
if self.xaxis or self.yaxis:
plotly_object['layout']['scene']['aspectratio'] = {}
if self.xaxis:
plotly_object['layout']['xaxis'] = self.xaxis.to_plotly()
elif not self.xaxis:
plotly_object['layout']['xaxis'] = dict(
autorange=True,
showgrid=False,
zeroline=False,
showline=False,
autotick=True,
ticks='',
showticklabels=False
)
if self.yaxis:
plotly_object['layout']['yaxis'] = self.yaxis.to_plotly()
elif not self.yaxis:
plotly_object['layout']['yaxis'] = dict(showline=False)
return plotly_object
# -*- coding: utf-8 -*-
from .figure_from_plot import FigureFromPlot
class Figure3D(FigureFromPlot):
def __init__(self, plots, title=None, width=None, height=None,
xaxis=None, yaxis=None, zaxis=None):
if plots is not None:
width = width if width is not None else plots[0].default_width()
height = \
height if height is not None else plots[0].default_height()
xaxis = xaxis if xaxis is not None else plots[0].default_axes()[0]
yaxis = yaxis if yaxis is not None else plots[0].default_axes()[1]
zaxis = zaxis if zaxis is not None else plots[0].default_axes()[2]
super(Figure3D, self).__init__(plots, title, width, height)
self.xaxis = xaxis
self.yaxis = yaxis
self.zaxis = zaxis
def at(self, plots, title=None):
title = title if title is not None else self.title
return Figure3D(plots=plots,
title=title,
width=self.width,
height=self.height,
xaxis=self.xaxis,
yaxis=self.yaxis,
zaxis=self.zaxis)
def to_plotly(self):
plotly_object = super(Figure3D, self).to_plotly()
if self.xaxis or self.yaxis or self.zaxis:
plotly_object['layout']['scene']['aspectratio'] = dict()
if self.xaxis:
plotly_object['layout']['scene']['xaxis'] = self.xaxis.to_plotly()
elif not self.xaxis:
plotly_object['layout']['scene']['xaxis'] = dict(showline=False)
if self.yaxis:
plotly_object['layout']['scene']['yaxis'] = self.yaxis.to_plotly()
elif not self.yaxis:
plotly_object['layout']['scene']['yaxis'] = dict(showline=False)
if self.zaxis:
plotly_object['layout']['scene']['zaxis'] = self.zaxis.to_plotly()
elif not self.zaxis:
plotly_object['layout']['scene']['zaxis'] = dict(showline=False)
return plotly_object
# -*- coding: utf-8 -*-
import abc
from ..plotly_wrapper import PlotlyWrapper
class FigureBase(PlotlyWrapper):
def __init__(self, title, width, height):
self.title = title
self.width = width
self.height = height
@abc.abstractmethod
def at(self, title=None):
raise NotImplementedError
@abc.abstractmethod
def to_plotly(self):
raise NotImplementedError
# -*- coding: utf-8 -*-
from .figure_base import FigureBase
class FigureFromPlot(FigureBase):
def __init__(self, plots, title, width, height):
super(FigureFromPlot, self).__init__(title, width, height)
self.plots = plots
def to_plotly(self):
data = [plt.to_plotly() for plt in self.plots]
layout = {'title': self.title,
'scene': {'aspectmode': 'cube'},
'autosize': False,
'width': self.width,
'height': self.height,
}
plotly_object = {'data': data,
'layout': layout}
return plotly_object
# -*- coding: utf-8 -*-
from builtins import map
from builtins import str
import numpy as np
from .figure_base import FigureBase
from .figure_3D import Figure3D
import plotly
# TODO: add nice height and width defaults for multifigure
class MultiFigure(FigureBase):
def __init__(self, subfigures, title=None, width=None, height=None):
super(MultiFigure, self).__init__(title, width, height)
if subfigures is not None:
self.subfigures = np.asarray(subfigures, dtype=np.object)
if len(self.subfigures.shape) != 2:
raise ValueError("Subfigures must be a two-dimensional array.")
def at(self, subfigures):
return MultiFigure(subfigures=subfigures,
title=self.title,
width=self.width,
height=self.height)
@property
def rows(self):
return self.subfigures.shape[0]