<commit_before>"""
Chart
-----

Tools for making nice and readable plots using matplotlib

"""
from __future__ import (absolute_import, division, print_function, unicode_literals)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from functools import wraps

__all__ = ['plotwrapper', 'image', 'hist', 'hist2d', 'errorplot', 'random_slice',
           'setfontsize', 'noticks', 'nospines', 'breathe', 'play']


def plotwrapper(fun):
    """
    Decorator that adds sane plotting defaults to the kwargs of a function
    """

    @wraps(fun)
    def wrapper(*args, **kwargs):

        if 'ax' not in kwargs:

            if 'fig' not in kwargs:
                kwargs['fig'] = plt.figure()

            kwargs['ax'] = kwargs['fig'].add_subplot(111)

        else:

            if 'fig' not in kwargs:
                kwargs['fig'] = kwargs['ax'].get_figure()

        fun(*args, **kwargs)
        plt.show()
        plt.draw()
        return kwargs['ax']

    return wrapper


def axwrapper(fun):
    """
    Decorator that adds axis arguments, used for functions that modify
    and existing plot (this decorator will never create a new plot)
    """

    @wraps(fun)
    def wrapper(*args, **kwargs):

        if 'ax' not in kwargs:

            if 'fig' not in kwargs:
                kwargs['fig'] = plt.gcf()

            kwargs['ax'] = plt.gca()

        else:

            if 'fig' not in kwargs:
                kwargs['fig'] = kwargs['ax'].get_figure()

        fun(*args, **kwargs)
        plt.show()
        plt.draw()
        return kwargs['ax']

    return wrapper


@plotwrapper
def image(data, mode='div', center=True, cmap=None, aspect='equal', vmin=None, vmax=None, **kwargs):
    """
    Visualize a matrix as an image

    Parameters
    ----------
    img : array_like
        The matrix to visualize

    mode : string, optional
        Either 'div' for a diverging image or 'seq' for sequential (default: 'div')

    center : boolean, optional
        Whether or not to mean-subtract the image

    cmap : string, optional
        A matplotlib colormap to use (default: 'seismic' for 'div', 'gray' for 'seq')

    aspect : string, optional
        Same as in plt.imshow, either 'equal' or 'auto'

    ax : matplotlib axes handle, optional
        A handle to a matplotlib axes to use for plotting

    """

    # work with a copy of the original image data
    img = data.copy()

    # subtract off image mean
    if center:
        img -= np.mean(img)

    # image bounds
    img_min = np.min(img)
    img_max = np.max(img)
    abs_max = np.max(np.abs(img))

    if mode == 'div':
        if vmin is None:
            vmin = -abs_max
        if vmax is None:
            vmax = abs_max
        if cmap is None:
            cmap = 'seismic'
    elif mode == 'seq':
        if vmin is None:
            vmin = img_min
        if vmax is None:
            vmax = img_max
        if cmap is None:
            cmap = 'gray'
    else:
        raise ValueError("Unrecognized mode: '" + mode + "'")

    # make the image
    kwargs['ax'].imshow(img, cmap=cmap, interpolation=None,
                        vmin=vmin, vmax=vmax, aspect=aspect)

    # clear ticks
    noticks(ax=kwargs['ax'])

    # display
    plt.show()
    plt.draw()


def play(images, cmap='viridis', interval=50, clim=None, **kwargs):
<commit_msg>adds play docstring<commit_after>"""
Chart
-----

Tools for making nice and readable plots using matplotlib

"""
from __future__ import (absolute_import, division, print_function, unicode_literals)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from functools import wraps

__all__ = ['plotwrapper', 'image', 'hist', 'hist2d', 'errorplot', 'random_slice',
           'setfontsize', 'noticks', 'nospines', 'breathe', 'play']


def plotwrapper(fun):
    """
    Decorator that adds sane plotting defaults to the kwargs of a function
    """

    @wraps(fun)
    def wrapper(*args, **kwargs):

        if 'ax' not in kwargs:

            if 'fig' not in kwargs:
                kwargs['fig'] = plt.figure()

            kwargs['ax'] = kwargs['fig'].add_subplot(111)

        else:

            if 'fig' not in kwargs:
                kwargs['fig'] = kwargs['ax'].get_figure()

        fun(*args, **kwargs)
        plt.show()
        plt.draw()
        return kwargs['ax']

    return wrapper


def axwrapper(fun):
    """
    Decorator that adds axis arguments, used for functions that modify
    and existing plot (this decorator will never create a new plot)
    """

    @wraps(fun)
    def wrapper(*args, **kwargs):

        if 'ax' not in kwargs:

            if 'fig' not in kwargs:
                kwargs['fig'] = plt.gcf()

            kwargs['ax'] = plt.gca()

        else:

            if 'fig' not in kwargs:
                kwargs['fig'] = kwargs['ax'].get_figure()

        fun(*args, **kwargs)
        plt.show()
        plt.draw()
        return kwargs['ax']

    return wrapper


@plotwrapper
def image(data, mode='div', center=True, cmap=None, aspect='equal', vmin=None, vmax=None, **kwargs):
    """
    Visualize a matrix as an image

    Parameters
    ----------
    img : array_like
        The matrix to visualize

    mode : string, optional
        Either 'div' for a diverging image or 'seq' for sequential (default: 'div')

    center : boolean, optional
        Whether or not to mean-subtract the image

    cmap : string, optional
        A matplotlib colormap to use (default: 'seismic' for 'div', 'gray' for 'seq')

    aspect : string, optional
        Same as in plt.imshow, either 'equal' or 'auto'

    ax : matplotlib axes handle, optional
        A handle to a matplotlib axes to use for plotting

    """

    # work with a copy of the original image data
    img = data.copy()

    # subtract off image mean
    if center:
        img -= np.mean(img)

    # image bounds
    img_min = np.min(img)
    img_max = np.max(img)
    abs_max = np.max(np.abs(img))

    if mode == 'div':
        if vmin is None:
            vmin = -abs_max
        if vmax is None:
            vmax = abs_max
        if cmap is None:
            cmap = 'seismic'
    elif mode == 'seq':
        if vmin is None:
            vmin = img_min
        if vmax is None:
            vmax = img_max
        if cmap is None:
            cmap = 'gray'
    else:
        raise ValueError("Unrecognized mode: '" + mode + "'")

    # make the image
    kwargs['ax'].imshow(img, cmap=cmap, interpolation=None,
                        vmin=vmin, vmax=vmax, aspect=aspect)

    # clear ticks
    noticks(ax=kwargs['ax'])

    # display
    plt.show()
    plt.draw()


def play(images, cmap='gray', interval=100, clim=None, **kwargs):
    """Plays an animation of the given stack of images"""
{'fraction_non_alphanumeric': 0.11870133685911562, 'fraction_numerical': 0.001909798736594682, 'mean_word_length': 2.130114942528736, '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': '2921633', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2050, 'n_words': 681}