<commit_before>from __future__ import absolute_import
import matplotlib.pyplot as plt
import matplotlib.colors as mpl_colors
import seaborn.apionly as sns
import numpy as np
import scipy.sparse as sp
import comdet.pme.acontrario as ac


def create_preference_matrix(n_rows):
    return sp.csc_matrix((n_rows, 0))


def add_col(preference_matrix, in_column, value=1):
    col_shape = (preference_matrix.shape[0], 1)
    col_idx = np.where(in_column)[0]
    data_shape = (len(col_idx),)
    column = sp.csc_matrix((value * np.ones(data_shape),
                           (col_idx, np.zeros(data_shape))),
                           col_shape)
    if preference_matrix.shape[1] > 0:
        preference_matrix = sp.hstack([preference_matrix, column])
    else:
        preference_matrix = column
    return preference_matrix


def plot(array, bic_list=[], palette='Set1'):
    white = mpl_colors.colorConverter.to_rgba('w', alpha=1)
    black = mpl_colors.colorConverter.to_rgba('k', alpha=1)

    cmap = mpl_colors.ListedColormap([white, black])
    plt.hold(True)
    try:
        plt.imshow(array, interpolation='none', cmap=cmap)
    except TypeError:
        plt.imshow(array.toarray(), interpolation='none', cmap=cmap)

    if bic_list:
        palette = sns.color_palette(palette, len(bic_list))

    for (u, v), c in zip(bic_list, palette):
        uv = u.dot(v).astype(int)
        white = mpl_colors.colorConverter.to_rgba('w', alpha=0)
        c = mpl_colors.colorConverter.to_rgba(c, alpha=1)
        cmap = mpl_colors.ListedColormap([white, c])
        try:
            plt.imshow(uv, interpolation='none', cmap=cmap)
        except TypeError:
            plt.imshow(uv.toarray(), interpolation='none', cmap=cmap)

    plt.tick_params(
        which='both',  # both major and minor ticks are affected
        bottom='off',
        top='off',
        left='off',
        right='off',
        labelbottom='off',
        labelleft='off')
    plt.axis('image')


def build_preference_matrix(n_elements, ransac_gen, ac_tester):
    filtered = ac.ifilter(ac_tester, ransac_gen)

    pref_matrix = create_preference_matrix(n_elements)
    original_models = []
    for i, (model, inliers) in enumerate(filtered):
        pref_matrix = add_col(pref_matrix, inliers)
        original_models.append(model)

    return pref_matrix, original_models<commit_msg>Simplified code<commit_after>from __future__ import absolute_import
import matplotlib.pyplot as plt
import matplotlib.colors as mpl_colors
import seaborn.apionly as sns
import numpy as np
import scipy.sparse as sp
import comdet.pme.acontrario as ac


def create_preference_matrix(n_rows):
    return sp.csc_matrix((n_rows, 0))


def add_col(preference_matrix, in_column, value=1):
    col_shape = (preference_matrix.shape[0], 1)
    col_idx = np.where(in_column)[0]
    data_shape = (len(col_idx),)
    column = sp.csc_matrix((value * np.ones(data_shape),
                           (col_idx, np.zeros(data_shape))),
                           col_shape)
    if preference_matrix.shape[1] > 0:
        preference_matrix = sp.hstack([preference_matrix, column])
    else:
        preference_matrix = column
    return preference_matrix


def plot(array, bic_list=[], palette='Set1'):
    white = mpl_colors.colorConverter.to_rgba('w', alpha=1)
    black = mpl_colors.colorConverter.to_rgba('k', alpha=1)

    cmap = mpl_colors.ListedColormap([white, black])
    plt.hold(True)
    try:
        plt.imshow(array, interpolation='none', cmap=cmap)
    except TypeError:
        plt.imshow(array.toarray(), interpolation='none', cmap=cmap)

    if bic_list:
        palette = sns.color_palette(palette, len(bic_list))

    for (u, v), c in zip(bic_list, palette):
        uv = u.dot(v).astype(int)
        white = mpl_colors.colorConverter.to_rgba('w', alpha=0)
        c = mpl_colors.colorConverter.to_rgba(c, alpha=1)
        cmap = mpl_colors.ListedColormap([white, c])
        try:
            plt.imshow(uv, interpolation='none', cmap=cmap)
        except TypeError:
            plt.imshow(uv.toarray(), interpolation='none', cmap=cmap)

    plt.tick_params(
        which='both',  # both major and minor ticks are affected
        bottom='off',
        top='off',
        left='off',
        right='off',
        labelbottom='off',
        labelleft='off')
    plt.axis('image')


def build_preference_matrix(n_elements, ransac_gen, ac_tester):
    pref_matrix = create_preference_matrix(n_elements)
    original_models = []
    for model in ac.ifilter(ac_tester, ransac_gen):
        pref_matrix = add_col(pref_matrix, ac_tester.inliers(model))
        original_models.append(model)

    return pref_matrix, original_models
{'fraction_non_alphanumeric': 0.11578722495193335, 'fraction_numerical': 0.005127109591967528, 'mean_word_length': 3.2142214221422143, 'pattern_counts': {'":': 0, '<': 3, '<?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': '778773', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1573, 'n_words': 306}