<commit_before>"""Plotting functions for linear models (broadly construed)."""
from __future__ import division
import itertools
import warnings
import numpy as np
import pandas as pd
from scipy.spatial import distance
import statsmodels.api as sm
import statsmodels.formula.api as sf
import matplotlib as mpl
import matplotlib.pyplot as plt
import moss

from six.moves import range

from .utils import color_palette
from .axisgrid import FacetGrid


def lmplot(x, y, data, hue=None, col=None, row=None, palette="husl",
           col_wrap=None, size=5, aspect=1, sharex=True, sharey=True,
           **kwargs):

    # Backwards-compatibility warning layer
    if "color" in kwargs:
        msg = "`color` is deprecated and will be removed; using `hue` instead."
        warnings.warn(msg, UserWarning)
        kwargs["hue"] = kwargs.pop("color")

    # Initialize the grid
    facets = FacetGrid(data, row, col, hue, palette=palette,
                       size=size, aspect=aspect, col_wrap=col_wrap)

    # Hack to set the x limits properly, which needs to happen here
    # because the extent of the regression estimate is determined
    # by the limits of the plot
    if sharex:
        for ax in facets.axes.flat:
            scatter = ax.scatter(data[x], np.ones(len(data)) * data[y].mean())
            scatter.remove()

    # Draw the regression plot on each facet
    facets.map(regplot, x, y, **kwargs)
    return facets


def factorplot(x, y=None, data=None, hue=None, row=None, col=None,
               col_wrap=None, estimator=np.mean, ci=95, n_boot=1000,
               kind="auto", dodge=0, join=True, size=5, aspect=1, palette=None,
               legend=True, legend_out=True, dropna=True,
               sharex=True, sharey=True):
    """Plot a dependent variable with uncertainty sorted by discrete factors.

    Parameters
    ----------
    x : string
        Variable name in `data` for splitting the plot on the x axis.
    y : string, optional
        Variable name in `data` for the dependent variable. If omitted, the
        counts within each bin are plotted (without confidence intervals).
    data : DataFrame
        Long-form (tidy) dataframe with variables in columns and observations
        in rows.
    hue : string, optional
        Variable name in `data` for splitting the plot by color. In the case
        of `kind="bar"`, this also influences the placement on the x axis.
    row, col : strings, optional
        Variable name(s) in `data` for splitting the plot into a facet grid
        along row and columns.
    col_wrap : int or None, optional
        Wrap the column variable at this width (incompatible with `row`).
    estimator : vector -> scalar function, optional
        Function to aggregate `y` values at each level of the factors.
    ci : int in {0, 100}, optional
        Size of confidene interval to draw around the aggregated value.
    n_boot : int, optional
        Number of bootstrap resamples used to compute confidence interval.
    kind : {"auto", "point", "bar"}, optional
        Visual representation of the plot. "auto" uses a few heuristics to
        guess whether "bar" or "point" is more appropriate.
    dodge : positive scalar, optional
        Horizontal offset applies to different `hue` levels. Only relevant
        when kind is "point".
    join : boolean, optional
        Whether points from the same level of `hue` should be joined. Only
        relevant when kind is "point".
    size : positive scalar, optional
        Height (in inches) of each facet.
    aspect : positive scalar, optional
        Ratio of facet width to facet height.
    palette : seaborn color palette, optional
        Palette to map `hue` variable with (or `x` variable when `hue` is
        None).
    legend : boolean, optional
        Draw a legend, only if `hue` is used and does not overlap with other
        variables.
    legend_out : boolean, optional
        Draw the legend outside the grid; otherwise it is placed within the
        first facet.
    dropna : boolean, optional
        Remove observations that are NA within any variables used to make
        the plot.
    share{x, y} : booleans, optional
        Lock the limits of the vertical and/or horizontal axes across the
        facets.

    Returns
    -------
    facet : FacetGrid
        FacetGrid with the plot on it.

    """
    if hue is not None and palette is None:
        palette = "husl"
    elif hue is None and palette is not None:
        hue = x

    facet = FacetGrid(data, row, col, hue, col_wrap=col_wrap, size=size,
                      aspect=aspect, legend=legend, legend_out=legend_out,
                      palette=palette, dropna=True,
                      sharex=sharex, sharey=sharey)

    if kind == "auto":
        if y is None:
            kind = "bar"
        elif (data[y] <= 1).all():
            kind = "point"
        elif (data[y].mean() / data[y].std()) < 2.5:
            kind = "bar"
        else:
            kind = "point"

    mask_gen = facet.facet_data()

    x_order = sorted(data[x].unique())
    if dropna:
        x_order = list(filter(pd.notnull, x_order))

    if y is None:<commit_msg>Add x_order parameter to factorplot<commit_after>"""Plotting functions for linear models (broadly construed)."""
from __future__ import division
import itertools
import warnings
import numpy as np
import pandas as pd
from scipy.spatial import distance
import statsmodels.api as sm
import statsmodels.formula.api as sf
import matplotlib as mpl
import matplotlib.pyplot as plt
import moss

from six.moves import range

from .utils import color_palette
from .axisgrid import FacetGrid


def lmplot(x, y, data, hue=None, col=None, row=None, palette="husl",
           col_wrap=None, size=5, aspect=1, sharex=True, sharey=True,
           **kwargs):

    # Backwards-compatibility warning layer
    if "color" in kwargs:
        msg = "`color` is deprecated and will be removed; using `hue` instead."
        warnings.warn(msg, UserWarning)
        kwargs["hue"] = kwargs.pop("color")

    # Initialize the grid
    facets = FacetGrid(data, row, col, hue, palette=palette,
                       size=size, aspect=aspect, col_wrap=col_wrap)

    # Hack to set the x limits properly, which needs to happen here
    # because the extent of the regression estimate is determined
    # by the limits of the plot
    if sharex:
        for ax in facets.axes.flat:
            scatter = ax.scatter(data[x], np.ones(len(data)) * data[y].mean())
            scatter.remove()

    # Draw the regression plot on each facet
    facets.map(regplot, x, y, **kwargs)
    return facets


def factorplot(x, y=None, data=None, hue=None, row=None, col=None,
               col_wrap=None,  estimator=np.mean, ci=95, n_boot=1000,
               x_order=None, hue_order=None, col_order=None, row_order=None,
               kind="auto", dodge=0, join=True, size=5, aspect=1,
               palette=None, legend=True, legend_out=True, dropna=True,
               sharex=True, sharey=True):
    """Plot a dependent variable with uncertainty sorted by discrete factors.

    Parameters
    ----------
    x : string
        Variable name in `data` for splitting the plot on the x axis.
    y : string, optional
        Variable name in `data` for the dependent variable. If omitted, the
        counts within each bin are plotted (without confidence intervals).
    data : DataFrame
        Long-form (tidy) dataframe with variables in columns and observations
        in rows.
    hue : string, optional
        Variable name in `data` for splitting the plot by color. In the case
        of `kind="bar"`, this also influences the placement on the x axis.
    row, col : strings, optional
        Variable name(s) in `data` for splitting the plot into a facet grid
        along row and columns.
    col_wrap : int or None, optional
        Wrap the column variable at this width (incompatible with `row`).
    estimator : vector -> scalar function, optional
        Function to aggregate `y` values at each level of the factors.
    ci : int in {0, 100}, optional
        Size of confidene interval to draw around the aggregated value.
    n_boot : int, optional
        Number of bootstrap resamples used to compute confidence interval.
    kind : {"auto", "point", "bar"}, optional
        Visual representation of the plot. "auto" uses a few heuristics to
        guess whether "bar" or "point" is more appropriate.
    dodge : positive scalar, optional
        Horizontal offset applies to different `hue` levels. Only relevant
        when kind is "point".
    join : boolean, optional
        Whether points from the same level of `hue` should be joined. Only
        relevant when kind is "point".
    size : positive scalar, optional
        Height (in inches) of each facet.
    aspect : positive scalar, optional
        Ratio of facet width to facet height.
    palette : seaborn color palette, optional
        Palette to map `hue` variable with (or `x` variable when `hue` is
        None).
    legend : boolean, optional
        Draw a legend, only if `hue` is used and does not overlap with other
        variables.
    legend_out : boolean, optional
        Draw the legend outside the grid; otherwise it is placed within the
        first facet.
    dropna : boolean, optional
        Remove observations that are NA within any variables used to make
        the plot.
    share{x, y} : booleans, optional
        Lock the limits of the vertical and/or horizontal axes across the
        facets.

    Returns
    -------
    facet : FacetGrid
        FacetGrid with the plot on it.

    """
    if hue is not None and palette is None:
        palette = "husl"
    elif hue is None and palette is not None:
        hue = x
        hue_order = x_order

    facet = FacetGrid(data, row, col, hue, col_wrap=col_wrap, size=size,
                      aspect=aspect, legend=legend, legend_out=legend_out,
                      palette=palette, dropna=True, hue_order=hue_order,
                      sharex=sharex, sharey=sharey)

    if kind == "auto":
        if y is None:
            kind = "bar"
        elif (data[y] <= 1).all():
            kind = "point"
        elif (data[y].mean() / data[y].std()) < 2.5:
            kind = "bar"
        else:
            kind = "point"

    mask_gen = facet.facet_data()

    if x_order is None:
        x_order = sorted(data[x].unique())
    if dropna:
        x_order = list(filter(pd.notnull, x_order))

    if y is None:
{'fraction_non_alphanumeric': 0.08338873587235256, 'fraction_numerical': 0.003419128122328806, 'mean_word_length': 2.4513274336283186, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1645870', 'n_tokens_mistral': 3031, 'n_tokens_neox': 2901, 'n_words': 1200}