<commit_before># -*- coding: utf-8 -*-
import numpy as np
from scipy import sparse as sp


def argsort_k_largest(x, k):
    """ Return no more than ``k`` indices of largest values. """
    if k == 0:
        return np.array([])
    if k >= len(x):
        return np.argsort(x)[::-1]
    indices = np.argpartition(x, -k)[-k:]
    values = x[indices]
    return indices[np.argsort(-values)]


def argsort_k_smallest(x, k):
    """ Return no more than ``k`` indices of smallest values. """
    if k == 0:
        return np.array([])
    if k >= len(x):
        return np.argsort(x)
    indices = np.argpartition(x, k)[:k]
    values = x[indices]
    return indices[np.argsort(values)]


def mask(x, indices):
    """
    The same as x[indices], but return an empty array if indices are empty,
    instead of returning all x elements.
    """
    if not indices.shape[0]:
        return np.array([])
    return x[indices]


def vstack(blocks, format=None, dtype=None):
    if any(sp.issparse(b) for b in blocks):
        return sp.vstack(blocks, format=format, dtype=dtype)
    else:
        return np.vstack(blocks)


def get_value_indices(names, lookups):
    """
    >>> get_value_indices(['foo', 'bar', 'baz'], ['bar', 'foo'])
    [1, 0]
    >>> get_value_indices(['foo', 'bar', 'baz'], ['spam'])
    Traceback (most recent call last):
    ...
    KeyError: 'spam'
    """
    positions = {name: idx for idx, name in enumerate(names)}
    return [positions[name] for name in lookups]


def get_display_names(original_names=None, target_names=None, target_order=None):
    """
    Return a list of (class_id, display_name) tuples.

    Provide display names:
    >>> get_display_names([0, 2], target_names=['foo', 'bar'])
    [(0, 'foo'), (1, 'bar')]

    Change order of labels:
    >>> get_display_names(['x', 'y'], target_order=['y', 'x'])
    [(1, 'y'), (0, 'x')]

    Provide display names, choose only a subset of labels:
    >>> get_display_names([0, 2], target_names=['foo', 'bar'], target_order=[2])
    [(1, 'bar')]
    """
    if target_names is not None:
        if original_names is not None:
            if len(target_names) != len(original_names):
                raise ValueError("target_names must have the same length as "
                                 "original names: expected {}, got {}".format(
                                     len(original_names), len(target_names)
                                 ))
        display_names = target_names
    else:
        display_names = original_names

    if target_order is None:
        target_order = original_names

    class_indices = get_value_indices(original_names, target_order)
    return list(zip(class_indices, [display_names[i] for i in class_indices]))
<commit_msg>bring back support for dict target_names<commit_after># -*- coding: utf-8 -*-
import numpy as np
from scipy import sparse as sp


def argsort_k_largest(x, k):
    """ Return no more than ``k`` indices of largest values. """
    if k == 0:
        return np.array([])
    if k >= len(x):
        return np.argsort(x)[::-1]
    indices = np.argpartition(x, -k)[-k:]
    values = x[indices]
    return indices[np.argsort(-values)]


def argsort_k_smallest(x, k):
    """ Return no more than ``k`` indices of smallest values. """
    if k == 0:
        return np.array([])
    if k >= len(x):
        return np.argsort(x)
    indices = np.argpartition(x, k)[:k]
    values = x[indices]
    return indices[np.argsort(values)]


def mask(x, indices):
    """
    The same as x[indices], but return an empty array if indices are empty,
    instead of returning all x elements.
    """
    if not indices.shape[0]:
        return np.array([])
    return x[indices]


def vstack(blocks, format=None, dtype=None):
    if any(sp.issparse(b) for b in blocks):
        return sp.vstack(blocks, format=format, dtype=dtype)
    else:
        return np.vstack(blocks)


def get_value_indices(names, lookups):
    """
    >>> get_value_indices(['foo', 'bar', 'baz'], ['bar', 'foo'])
    [1, 0]
    >>> get_value_indices(['foo', 'bar', 'baz'], ['spam'])
    Traceback (most recent call last):
    ...
    KeyError: 'spam'
    """
    positions = {name: idx for idx, name in enumerate(names)}
    return [positions[name] for name in lookups]


def get_display_names(original_names=None, target_names=None, target_order=None):
    """
    Return a list of (class_id, display_name) tuples.

    Provide display names:
    >>> get_display_names([0, 2], target_names=['foo', 'bar'])
    [(0, 'foo'), (1, 'bar')]

    Change order of labels:
    >>> get_display_names(['x', 'y'], target_order=['y', 'x'])
    [(1, 'y'), (0, 'x')]

    Provide display names, choose only a subset of labels:
    >>> get_display_names([0, 2], target_names=['foo', 'bar'], target_order=[2])
    [(1, 'bar')]

    target_names can be a dictionary with {old_name: new_name} labels:
    >>> get_display_names(['x', 'y'], target_order=['y', 'x'],
    ...                   target_names={'x': 'X'})
    [(1, 'y'), (0, 'X')]

    """
    if isinstance(target_names, (list, tuple, np.ndarray)):
        if original_names is not None:
            if len(target_names) != len(original_names):
                raise ValueError("target_names must have the same length as "
                                 "original names: expected {}, got {}".format(
                                     len(original_names), len(target_names)
                                 ))
        display_names = target_names
    else:
        display_names = original_names

    if target_order is None:
        target_order = original_names

    class_indices = get_value_indices(original_names, target_order)
    names = [display_names[i] for i in class_indices]
    if isinstance(target_names, dict):
        names = [target_names.get(name, name) for name in names]
    return list(zip(class_indices, names))

{'fraction_non_alphanumeric': 0.1483497788363389, 'fraction_numerical': 0.006124532153793808, 'mean_word_length': 2.725602027883397, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4573581', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1846, 'n_words': 538}