<commit_before>"""
Utility functions for plotting the feature statistics stored in Hopsworks for Feature Groups and Training Datasets
"""

import numpy as np
import math
import pandas as pd

# for backwards compatibility
try:
    import matplotlib.pyplot as plt
    from matplotlib import rc
    # Set the global font to be DejaVu Sans, size 10 (or any other sans-serif font of your choice!)
    rc('font',**{'family':'sans-serif','sans-serif':['DejaVu Sans'],'size':10})
    # Set the font used for MathJax - more on this later
    rc('mathtext',**{'default':'regular'})
    # The following %config line changes the inline figures to have a higher DPI.
    # You can comment out (#) this line if you don't have a high-DPI (~220) display.
    import seaborn as sns
except:
    pass


def _plot_feature_distribution(ax, feature_distribution, color='lightblue', log=False, align="center"):
    """
    Creates a single bar plot for a feature distribution

    Args:
        :ax: the axes object to add the plot to
        :feature_distribution: the feature distribution to plot
        :color: the color of the bar plot
        :log: whether to use logarithmic scaling on the y-axes
        :align: how to align the bars, defaults to center.

    Returns:
        None

    """
    frequency_distribution = feature_distribution.frequency_distribution
    frequencies = list(map(lambda feature_frequency: int(feature_frequency.frequency), frequency_distribution))
    bins = list(map(lambda feature_frequency: float(feature_frequency.bin), frequency_distribution))
    width = np.min(np.diff(bins))
    ax.bar(bins, frequencies, color=color, width=width, align=align, log=log)


def _plot_feature_correlations(ax, correlation_matrix, cmap="coolwarm", annot=True, fmt=".2f", linewidths=.05):
    """
    Creates a heatmap plot of the feature correlations

    Args:
        :ax: the axes object to add the plot to
        :correlation_matrix: the feature correlations
        :cmap: the color map
        :annot: whether to annotate the heatmap
        :fmt: how to format the annotations
        :linewidths: line width in the plot

    Returns:
        The heatmap
    """
    hm = sns.heatmap(correlation_matrix, ax=ax, cmap=cmap, annot=annot, fmt=fmt,
                     linewidths=linewidths)
    return hm


def _plot_feature_clusters(ax, cluster_analysis):
    """
    Creates a scatter plot of the feature clusters

    Args:
        :ax: the axes object to add the plot to
        :cluster_analysis: the cluster analysis data to plot

    Returns:
        None

    """
    data, colors, groups = _get_cluster_data(cluster_analysis)
    for dataset, color, group in zip(data, colors, groups):
        x, y = dataset
        ax.scatter(x, y, alpha=0.8, c=color, edgecolors='none', label=group)


def _stylize_axes(ax, title, xlabel, ylabel):
    """
    Customize axes spines, title, labels, ticks, and ticklabels.

    Args:
        :ax: the axes to customize
        :title: the title to set
        :xlabel: the label on the x axis
        :ylabel: the label on the y axis

    Returns:
        None
    """
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.xaxis.set_tick_params(top='off', direction='out', width=1)
    ax.yaxis.set_tick_params(right='off', direction='out', width=1)
    ax.set_title(title)
    ax.set_xlabel(xlabel)<commit_msg>[HOPSWORKS-1309] Fix deprecated maptplotlib calls (#114)<commit_after>"""
Utility functions for plotting the feature statistics stored in Hopsworks for Feature Groups and Training Datasets
"""

import numpy as np
import math
import pandas as pd

# for backwards compatibility
try:
    import matplotlib.pyplot as plt
    from matplotlib import rc
    # Set the global font to be DejaVu Sans, size 10 (or any other sans-serif font of your choice!)
    rc('font',**{'family':'sans-serif','sans-serif':['DejaVu Sans'],'size':10})
    # Set the font used for MathJax - more on this later
    rc('mathtext',**{'default':'regular'})
    # The following %config line changes the inline figures to have a higher DPI.
    # You can comment out (#) this line if you don't have a high-DPI (~220) display.
    import seaborn as sns
except:
    pass


def _plot_feature_distribution(ax, feature_distribution, color='lightblue', log=False, align="center"):
    """
    Creates a single bar plot for a feature distribution

    Args:
        :ax: the axes object to add the plot to
        :feature_distribution: the feature distribution to plot
        :color: the color of the bar plot
        :log: whether to use logarithmic scaling on the y-axes
        :align: how to align the bars, defaults to center.

    Returns:
        None

    """
    frequency_distribution = feature_distribution.frequency_distribution
    frequencies = list(map(lambda feature_frequency: int(feature_frequency.frequency), frequency_distribution))
    bins = list(map(lambda feature_frequency: float(feature_frequency.bin), frequency_distribution))
    width = np.min(np.diff(bins))
    ax.bar(bins, frequencies, color=color, width=width, align=align, log=log)


def _plot_feature_correlations(ax, correlation_matrix, cmap="coolwarm", annot=True, fmt=".2f", linewidths=.05):
    """
    Creates a heatmap plot of the feature correlations

    Args:
        :ax: the axes object to add the plot to
        :correlation_matrix: the feature correlations
        :cmap: the color map
        :annot: whether to annotate the heatmap
        :fmt: how to format the annotations
        :linewidths: line width in the plot

    Returns:
        The heatmap
    """
    hm = sns.heatmap(correlation_matrix, ax=ax, cmap=cmap, annot=annot, fmt=fmt,
                     linewidths=linewidths)
    return hm


def _plot_feature_clusters(ax, cluster_analysis):
    """
    Creates a scatter plot of the feature clusters

    Args:
        :ax: the axes object to add the plot to
        :cluster_analysis: the cluster analysis data to plot

    Returns:
        None

    """
    data, colors, groups = _get_cluster_data(cluster_analysis)
    for dataset, color, group in zip(data, colors, groups):
        x, y = dataset
        ax.scatter(x, y, alpha=0.8, c=color, edgecolors='none', label=group)


def _stylize_axes(ax, title, xlabel, ylabel):
    """
    Customize axes spines, title, labels, ticks, and ticklabels.

    Args:
        :ax: the axes to customize
        :title: the title to set
        :xlabel: the label on the x axis
        :ylabel: the label on the y axis

    Returns:
        None
    """
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.xaxis.set_tick_params(top=False, direction='out', width=1)
    ax.yaxis.set_tick_params(right=False, direction='out', width=1)
    ax.set_title(title)
    ax.set_xlabel(xlabel)
{'fraction_non_alphanumeric': 0.09515444298052994, 'fraction_numerical': 0.005123700775874689, 'mean_word_length': 3.2303405572755417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4666982', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1976, 'n_words': 694}