import numpy as np
from bqplot import *
from ipywidgets import Layout

"""
Explanation: Heatmap
The HeatMap mark represents a 2d matrix of values as a color image. It can be used to visualize a 2d function, or a grayscale image for instance.
HeatMap is very similar to the GridHeatMap, but should be preferred for a greater number of points (starting at around 100x100), to avoid overloading the browser. GridHeatMap offers more control (interactions, selections), and is better suited for a smaller number of points.
End of explanation
"""


x = np.linspace(-5, 5, 200)
y = np.linspace(-5, 5, 200)
X, Y = np.meshgrid(x, y)
color = np.cos(X**2 + Y**2)

"""
Explanation: Data Input

x is a 1d array, corresponding to the abscissas of the points (size N)
y is a 1d array, corresponding to the ordinates of the points (size M)
color is a 2d array, $\text{color}_{ij}$ is the intensity of the point $(x_i, y_j)$ (size (N, M))

Scales must be defined for each attribute:
- a LinearScale, LogScale or OrdinalScale for x and y
- a ColorScale for color
End of explanation
"""


x_sc, y_sc, col_sc = LinearScale(), LinearScale(), ColorScale(scheme='RdYlBu')
heat = HeatMap(x=x, y=y, color=color,
               scales={'x': x_sc, 'y': y_sc, 'color': col_sc})
ax_x = Axis(scale=x_sc)
ax_y = Axis(scale=y_sc, orientation='vertical')
ax_c = ColorAxis(scale=col_sc)
fig = Figure(marks=[heat], axes=[ax_x, ax_y, ax_c],
             title='Cosine',
             layout=Layout(width='650px', height='650px'),
             min_aspect_ratio=1, max_aspect_ratio=1, padding_y=0)
fig

"""
Explanation: Plotting a 2-dimensional function
This is a visualization of the function $f(x, y) = \text{cos}(x^2+y^2)$
End of explanation
"""


from scipy.misc import ascent
Z = ascent()
Z = Z[::-1, :] 
aspect_ratio = Z.shape[1]/Z.shape[0]

col_sc = ColorScale(scheme='Greys', reverse=True)
scales = {'color': col_sc};
ascent = HeatMap(color=Z, scales=scales)
img = Figure(title='Ascent', marks=[ascent],
             layout=Layout(width='650px', height='650px'),
             min_aspect_ratio=aspect_ratio, max_aspect_ratio=aspect_ratio, padding_y=0)
img

"""
Explanation: Displaying an image
The HeatMap can be used as is to display a 2d grayscale image, by feeding the matrix of pixel intensities to the color attribute
End of explanation
"""


