<commit_before>"""
Functions for plotting datasets nicely.
"""
# TODO: unify options with xmlinplot                                          #
# TODO: plotly hlines, vlines                                                 #
# TODO: names                                                                 #
# TODO: reverse color                                                         #
# TODO: logarithmic color                                                     #
# TODO: not working currently?                                                #

import itertools
from ..manage import auto_xyz_ds
from .color import calc_colors


def ishow(figs, nb=True, **kwargs):
    """ Show multiple plotly figures in notebook or on web. """
    if isinstance(figs, (list, tuple)):
        fig_main = figs[0]
        for fig in figs[1:]:
            fig_main["data"] += fig["data"]
    else:
        fig_main = figs
    if nb:
        from plotly.offline import init_notebook_mode
        from plotly.offline import iplot as plot
        init_notebook_mode()
    else:
        from plotly.plotly import plot
    plot(fig_main, **kwargs)


# --------------------------------------------------------------------------  #
# Line Plots                                                                  #
# --------------------------------------------------------------------------  #

def ilineplot(ds, y_coo, x_coo, z_coo=None,
              return_fig=False,
              nb=True,
              # Line coloring options
              colors=False,
              colormap="viridis",
              colormap_log=False,
              colormap_reverse=False,
              # Legend options
              legend=None,
              # x-axis options
              xlims=None,
              xlog=False,
              # y-axis options
              ylims=None,
              ylog=False,
              # Misc options
              vlines=[],
              hlines=[],
              go_dict={},
              ly_dict={},
              # TODO padding
              # TODO markers
              # TODO linewidths
              # TODO linestyles
              # TODO gridlines
              # TODO font and labels
              **kwargs):
    """ Take a dataset and plot onse of its variables as a function of two
    coordinates using plotly. """
    # TODO: list of colors, send to calc_colors
    # TODO: mouse scroll zoom

    from plotly.graph_objs import Scatter

    if z_coo is None:
        traces = [Scatter({'x': ds[x_coo].data,
                           'y': ds[y_coo].data.flatten(),
                           **go_dict})]
    else:
        cols = (calc_colors(ds, z_coo,
                            plotly=True,
                            colormap=colormap,
                            log_scale=colormap_log,
                            reverse=colormap_reverse) if colors else
                itertools.repeat(None))
        traces = [Scatter({'x': ds.loc[{z_coo: z}][x_coo].data.flatten(),
                           'y': ds.loc[{z_coo: z}][y_coo].data.flatten(),
                           'name': str(z),
                           'line': {'color': col},
                           'marker': {'color': col},
                           **go_dict})
                  for z, col in zip(ds[z_coo].data, cols)]

    lines = ([{'type': 'line',
               'layer': 'below',
               'line': {'color': 'rgb(128, 128, 128)',<commit_msg>Allow missing data in plotly plotter<commit_after>"""
Functions for plotting datasets nicely.
"""
# TODO: unify options with xmlinplot                                          #
# TODO: plotly hlines, vlines                                                 #
# TODO: names                                                                 #
# TODO: reverse color                                                         #
# TODO: logarithmic color                                                     #
# TODO: not working currently?                                                #

import itertools
import numpy as np
from ..manage import auto_xyz_ds
from .color import calc_colors


def ishow(figs, nb=True, **kwargs):
    """ Show multiple plotly figures in notebook or on web. """
    if isinstance(figs, (list, tuple)):
        fig_main = figs[0]
        for fig in figs[1:]:
            fig_main["data"] += fig["data"]
    else:
        fig_main = figs
    if nb:
        from plotly.offline import init_notebook_mode
        from plotly.offline import iplot as plot
        init_notebook_mode()
    else:
        from plotly.plotly import plot
    plot(fig_main, **kwargs)


# --------------------------------------------------------------------------  #
# Line Plots                                                                  #
# --------------------------------------------------------------------------  #

def ilineplot(ds, y_coo, x_coo, z_coo=None,
              return_fig=False,
              nb=True,
              # Line coloring options
              colors=False,
              colormap="viridis",
              colormap_log=False,
              colormap_reverse=False,
              # Legend options
              legend=None,
              # x-axis options
              xlims=None,
              xlog=False,
              # y-axis options
              ylims=None,
              ylog=False,
              # Misc options
              vlines=[],
              hlines=[],
              go_dict={},
              ly_dict={},
              # TODO padding
              # TODO markers
              # TODO linewidths
              # TODO linestyles
              # TODO gridlines
              # TODO font and labels
              **kwargs):
    """ Take a dataset and plot onse of its variables as a function of two
    coordinates using plotly. """
    # TODO: list of colors, send to calc_colors
    # TODO: mouse scroll zoom

    from plotly.graph_objs import Scatter

    if z_coo is None:
        traces = [Scatter({'x': ds[x_coo].data,
                           'y': ds[y_coo].data.flatten(),
                           **go_dict})]
    else:
        cols = (calc_colors(ds, z_coo,
                            plotly=True,
                            colormap=colormap,
                            log_scale=colormap_log,
                            reverse=colormap_reverse) if colors else
                itertools.repeat(None))

        def gen_traces():
            for z, col in zip(ds[z_coo].data, cols):
                # Select data for current z coord - flatten for singletons
                sds = ds.loc[{z_coo: z}]
                x = sds[x_coo].data.flatten()
                y = sds[y_coo].data.flatten()

                # Trim out missing data
                nans = np.logical_not(np.isnan(x) | np.isnan(y))
                x, y = x[nans], y[nans]

                yield Scatter({'x': x,
                               'y': y,
                               'name': str(z),
                               'line': {'color': col},
                               'marker': {'color': col},
                               **go_dict})

        traces = [*gen_traces()]

    lines = ([{'type': 'line',
               'layer': 'below',
               'line': {'color': 'rgb(128, 128, 128)',
{'fraction_non_alphanumeric': 0.13113623827909543, 'fraction_numerical': 0.003033645890788748, 'mean_word_length': 1.0019321004692243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1854320', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1777, 'n_words': 475}