# Path: megaman/plotter/utils.py
# def _check_backend(backend):
#     def decorator(func):
#         def wrapper(*args,**kwargs):
#             import warnings
#             warnings.warn(
#                 'Be careful in using megaman.plotter modules'
#                 ' API will change in the next release.',
#                 FutureWarning
#             )
#             import pkgutil
#             package = pkgutil.find_loader(backend)
#             if package is not None:
#                 return func(*args,**kwargs)
#             else:
#                 raise ImportError('plotting backend {} not installed'.format(backend))
#         return wrapper
#     return decorator
# 
# Path: megaman/plotter/scatter_3d.py
# @_check_backend('plotly')
# def scatter_plot3d_plotly(embedding, coloring=None,
#                           colorscale='Rainbow', **kwargs):
#     import plotly.graph_objs as go
#     x,y,z = embedding[:,:3].T
#     if isinstance(coloring, str) and coloring.lower() in 'xyz':
#         color_idx = 'xyz'.find(coloring)
#         coloring = embedding[:,color_idx].flatten()
# 
#     marker = kwargs.pop('marker',None)
#     name = kwargs.pop('name','Embedding')
#     scatter_plot = go.Scatter3d(
#         x=x,
#         y=y,
#         z=z,
#         mode='markers',
#         marker=dict(
#             size=2,
#             opacity=0.8,
#         ),
#         name=name,
#         **kwargs
#     )
#     if coloring is not None:
#         scatter_plot['marker'].update(dict(
#             color=coloring,
#             colorscale=colorscale,
#             showscale=True,
#         ))
#     elif marker is not None:
#         scatter_plot['marker'].update(marker)
# 
#     return [scatter_plot]
# 
# @_check_backend('matplotlib')
# def scatter_plot3d_matplotlib(embedding, coloring=None, fig=None,
#                               subplot=False, subplot_grid=None, **kwargs):
#     from mpl_toolkits.mplot3d import art3d, Axes3D
#     if fig is None:
#         import matplotlib.pyplot as plt
#         fig = plt.figure()
#     if subplot and subplot_grid is not None:
#         sx,sy,sz = subplot_grid
#         ax = fig.add_subplot(sx,sy,sz,projection='3d')
#     else:
#         if subplot is None and subplot:
#             import warnings
#             warnings.warn(
#                 'Subplot grid is not provided, switching to non-subplot mode')
#         ax = fig.gca(projection='3d')
# 
#     ax.set_aspect('equal')
#     s = [2 for i in range(embedding.shape[0])]
#     x,y,z = embedding[:,:3].T
# 
#     if isinstance(coloring, str) and coloring.lower() in 'xyz':
#         color_idx = 'xyz'.find(coloring)
#         coloring = embedding[:,color_idx].flatten()
# 
#     if coloring is None:
#         ax.scatter(x,y,z,s=s,**kwargs)
#     else:
#         sc = ax.scatter(x,y,z,c=coloring,cmap='gist_rainbow',s=s,**kwargs)
#         fig.colorbar(sc)
# 
#     max_range = np.array(
#         [x.max()-x.min(), y.max()-y.min(), z.max()-z.min()]).max() / 2.0
# 
#     mid_x = (x.max()+x.min()) * 0.5
#     mid_y = (y.max()+y.min()) * 0.5
#     mid_z = (z.max()+z.min()) * 0.5
#     ax.set_xlim(mid_x - max_range, mid_x + max_range)
#     ax.set_ylim(mid_y - max_range, mid_y + max_range)
#     ax.set_zlim(mid_z - max_range, mid_z + max_range)
# 
#     return fig, ax
# 
# Path: megaman/plotter/covar_plotter3.py
# def covar_plotter3d_plotly(embedding, rieman_metric, inspect_points_idx,
#                            colors, **kwargs):
#     """3 Dimensional Covariance plotter using matplotlib backend."""
#     def rgb2hex(rgb):
#         return '#%02x%02x%02x' % tuple(rgb)
#     return [ plt_data for idx in inspect_points_idx
#              for plt_data in plot_ellipse_plotly(
#                 rieman_metric[idx], embedding[idx],
#                 color=rgb2hex(colors[idx]), **kwargs) ]
# 
# def covar_plotter3d_matplotlib(embedding, rieman_metric, inspect_points_idx, ax,
#                                colors):
#     """3 Dimensional Covariance plotter using matplotlib backend."""
#     for pts_idx in inspect_points_idx:
#         plot_ellipse_matplotlib( cov=rieman_metric[pts_idx],
#                              pos=embedding[pts_idx],
#                              ax=ax, ec='k', lw=1,
#                              color=colors[pts_idx] )
#     return ax


import numpy as np
    import plotly.graph_objs as go
    import matplotlib.pyplot as plt
from .utils import *
from .utils import _check_backend
from .scatter_3d import scatter_plot3d_plotly, scatter_plot3d_matplotlib
from .covar_plotter3 import covar_plotter3d_plotly, covar_plotter3d_matplotlib
    from plotly.offline import iplot
# Author: Yu-Chia Chen <yuchaz@uw.edu>
# LICENSE: Simplified BSD https://github.com/mmp2/megaman/blob/master/LICENSE


@_check_backend('plotly')
def plot_with_plotly( embedding, rieman_metric, nstd=2,
                      color_by_ratio=True, if_ellipse=False ):
    sigma_norms = get_top_two_sigma_norm(rieman_metric, color_by_ratio)
    colors, colorscale = generate_colors_and_colorscale('gist_rainbow',
                                                        sigma_norms)
    scatter_pt = scatter_plot3d_plotly(embedding, coloring=sigma_norms,
                                       colorscale=colorscale)
    index = generate_grid(embedding.shape[0])

    if if_ellipse:

# Path: megaman/plotter/utils.py
# def _check_backend(backend):
#     def decorator(func):
#         def wrapper(*args,**kwargs):
#             import warnings
#             warnings.warn(
#                 'Be careful in using megaman.plotter modules'
#                 ' API will change in the next release.',
#                 FutureWarning
#             )
#             import pkgutil
#             package = pkgutil.find_loader(backend)
#             if package is not None:
#                 return func(*args,**kwargs)
#             else:
#                 raise ImportError('plotting backend {} not installed'.format(backend))
#         return wrapper
#     return decorator
# 
# Path: megaman/plotter/scatter_3d.py
# @_check_backend('plotly')
# def scatter_plot3d_plotly(embedding, coloring=None,
#                           colorscale='Rainbow', **kwargs):
#     import plotly.graph_objs as go
#     x,y,z = embedding[:,:3].T
#     if isinstance(coloring, str) and coloring.lower() in 'xyz':
#         color_idx = 'xyz'.find(coloring)
#         coloring = embedding[:,color_idx].flatten()
# 
#     marker = kwargs.pop('marker',None)
#     name = kwargs.pop('name','Embedding')
#     scatter_plot = go.Scatter3d(
#         x=x,
#         y=y,
#         z=z,
#         mode='markers',
#         marker=dict(
#             size=2,
#             opacity=0.8,
#         ),
#         name=name,
#         **kwargs
#     )
#     if coloring is not None:
#         scatter_plot['marker'].update(dict(
#             color=coloring,
#             colorscale=colorscale,
#             showscale=True,
#         ))
#     elif marker is not None:
#         scatter_plot['marker'].update(marker)
# 
#     return [scatter_plot]
# 
# @_check_backend('matplotlib')
# def scatter_plot3d_matplotlib(embedding, coloring=None, fig=None,
#                               subplot=False, subplot_grid=None, **kwargs):
#     from mpl_toolkits.mplot3d import art3d, Axes3D
#     if fig is None:
#         import matplotlib.pyplot as plt
#         fig = plt.figure()
#     if subplot and subplot_grid is not None:
#         sx,sy,sz = subplot_grid
#         ax = fig.add_subplot(sx,sy,sz,projection='3d')
#     else:
#         if subplot is None and subplot:
#             import warnings
#             warnings.warn(
#                 'Subplot grid is not provided, switching to non-subplot mode')
#         ax = fig.gca(projection='3d')
# 
#     ax.set_aspect('equal')
#     s = [2 for i in range(embedding.shape[0])]
#     x,y,z = embedding[:,:3].T
# 
#     if isinstance(coloring, str) and coloring.lower() in 'xyz':
#         color_idx = 'xyz'.find(coloring)
#         coloring = embedding[:,color_idx].flatten()
# 
#     if coloring is None:
#         ax.scatter(x,y,z,s=s,**kwargs)
#     else:
#         sc = ax.scatter(x,y,z,c=coloring,cmap='gist_rainbow',s=s,**kwargs)
#         fig.colorbar(sc)
# 
#     max_range = np.array(
#         [x.max()-x.min(), y.max()-y.min(), z.max()-z.min()]).max() / 2.0
# 
#     mid_x = (x.max()+x.min()) * 0.5
#     mid_y = (y.max()+y.min()) * 0.5
#     mid_z = (z.max()+z.min()) * 0.5
#     ax.set_xlim(mid_x - max_range, mid_x + max_range)
#     ax.set_ylim(mid_y - max_range, mid_y + max_range)
#     ax.set_zlim(mid_z - max_range, mid_z + max_range)
# 
#     return fig, ax
# 
# Path: megaman/plotter/covar_plotter3.py
# def covar_plotter3d_plotly(embedding, rieman_metric, inspect_points_idx,
#                            colors, **kwargs):
#     """3 Dimensional Covariance plotter using matplotlib backend."""
#     def rgb2hex(rgb):
#         return '#%02x%02x%02x' % tuple(rgb)
#     return [ plt_data for idx in inspect_points_idx
#              for plt_data in plot_ellipse_plotly(
#                 rieman_metric[idx], embedding[idx],
#                 color=rgb2hex(colors[idx]), **kwargs) ]
# 
# def covar_plotter3d_matplotlib(embedding, rieman_metric, inspect_points_idx, ax,
#                                colors):
#     """3 Dimensional Covariance plotter using matplotlib backend."""
#     for pts_idx in inspect_points_idx:
#         plot_ellipse_matplotlib( cov=rieman_metric[pts_idx],
#                              pos=embedding[pts_idx],
#                              ax=ax, ec='k', lw=1,
#                              color=colors[pts_idx] )
#     return ax


# Path: megaman/plotter/plotter.py
import numpy as np
    import plotly.graph_objs as go
    import matplotlib.pyplot as plt
from .utils import *
from .utils import _check_backend
from .scatter_3d import scatter_plot3d_plotly, scatter_plot3d_matplotlib
from .covar_plotter3 import covar_plotter3d_plotly, covar_plotter3d_matplotlib
    from plotly.offline import iplot

# Author: Yu-Chia Chen <yuchaz@uw.edu>
# LICENSE: Simplified BSD https://github.com/mmp2/megaman/blob/master/LICENSE


@_check_backend('plotly')
def plot_with_plotly( embedding, rieman_metric, nstd=2,
                      color_by_ratio=True, if_ellipse=False ):
    sigma_norms = get_top_two_sigma_norm(rieman_metric, color_by_ratio)
    colors, colorscale = generate_colors_and_colorscale('gist_rainbow',
                                                        sigma_norms)
    scatter_pt = scatter_plot3d_plotly(embedding, coloring=sigma_norms,
                                       colorscale=colorscale)
    index = generate_grid(embedding.shape[0])

    if if_ellipse:

        ellipses_pt = covar_plotter3d_plotly(embedding,