<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Measurement and plotting tools - numpy + mpl helpers
"""
# TODO:
#     - figure.tight_layout doesn't seem to work??
#     - make legend malleable
#     - consider a way to easily move lines to different axes and
import matplotlib.pyplot as plt
import numpy as np


def multiplot(metrics, fieldspec=None, fig=None, mng=None):
    '''Plot all columns in appropriate axes on a figure
    '''
    fig = fig if fig else plt.figure()
    mng = mng if mng else plt.get_current_fig_manager()
    if not fieldspec:
        # place each array on a separate axes
        fieldspec = [
            (name, (i, 1)) for i, name in enumerate(metrics.dtype.fields)
        ]
    elif hasattr(fieldspec, 'items'):
        fieldspec = fieldspec.items()

    rows, cols = max(tup[1] for tup in fieldspec)

    # plot loop
    artists = []  # store the artists for later use
    for name, loc in fieldspec:
        if loc is None:
            continue
        else:
            row, col = loc
        # generate plots
        ax = fig.add_subplot(rows, cols, row * col)
        try:
            array = getattr(metrics, name)
        except AttributeError:
            array = metrics[name]
        print("plotting '{}'".format(name))
        artists.append(ax.plot(array, label=name)[0])  # , **plot_opts)
        # set legend
        ax.legend(loc='upper right')
        # set titles
        # ax.set_title(name, fontdict={'size': 'small'})
        ax.set_xlabel('Call Event Index', fontdict={'size': 'small'})

    # show in a window full size
    fig.tight_layout()
    fig.show()
    return mng, fig, artists


def gen_hist(arr, col='invite_latency'):
    arr = arr[col]
    fig = plt.figure()  # always render new plots
    bins = np.arange(float(np.ceil(arr.max())))
    n, bins, patches = plt.hist(arr, bins=bins, normed=True)
    fig.show()
    return n, bins, patches<commit_msg>Better handle arrays with missing named rows<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Measurement and plotting tools - numpy + mpl helpers
"""
# TODO:
#     - figure.tight_layout doesn't seem to work??
#     - make legend malleable
#     - consider a way to easily move lines to different axes and
from os.path import basename
import matplotlib.pyplot as plt
import numpy as np
from ... import utils

log = utils.get_logger(__name__)


def multiplot(metrics, fieldspec=None, fig=None, mng=None):
    '''Plot all columns in appropriate axes on a figure
    (talk about reimplementing `pandas` like an dufus...)
    '''
    fig = fig if fig else plt.figure()
    mng = mng if mng else plt.get_current_fig_manager()
    if not fieldspec:
        # place each array on a separate axes
        fieldspec = [
            (name, (i, 1)) for i, name in enumerate(metrics.dtype.fields)
        ]
    elif hasattr(fieldspec, 'items'):
        fieldspec = fieldspec.items()

    rows, cols = max(tup[1] for tup in fieldspec)

    # plot loop
    artists = []  # store the artists for later use
    for name, loc in fieldspec:
        if loc is None:
            continue
        else:
            row, col = loc
        # generate plots
        ax = fig.add_subplot(rows, cols, row * col)
        try:
            array = getattr(metrics, name)
        except AttributeError:
            try:
                array = metrics[name]
            except ValueError:
                log.warn("no row '{}' exists for '{}".format(name, metrics))
                print("no row '{}' exists for '{}".format(name, metrics))
                continue
        log.info("plotting '{}'".format(name))
        artists.append(
            ax.plot(
                array,
                label=name,
                # linewidth=2.0,
            )[0]
        )
        # set legend
        ax.legend(loc='upper left', fontsize='large')
        # set titles
        # ax.set_title(name, fontdict={'size': 'small'})
        ax.set_xlabel('Call Event Index', fontdict={'size': 'large'})

    # show in a window full size
    fig.tight_layout()
    fig.show()
    if getattr(metrics, 'title', None):
        fig.suptitle(basename(metrics.title), fontsize=15)
    return mng, fig, artists


def gen_hist(arr, col='invite_latency'):
    arr = arr[col]
    fig = plt.figure()  # always render new plots
    bins = np.arange(float(np.ceil(arr.max())))
    n, bins, patches = plt.hist(arr, bins=bins, normed=True)
    fig.show()
    return n, bins, patches
{'fraction_non_alphanumeric': 0.11304165794431652, 'fraction_numerical': 0.0037680552648105504, 'mean_word_length': 2.331938633193863, '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': '3380930', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1454, 'n_words': 487}