<commit_before>
"""
Visualize possible stitches with the outcome of the validator.
"""

import math

import matplotlib.pyplot as plt
import networkx as nx

TYPE_FORMAT = {'a': '^', 'b': 's', 'c': 'v'}


def show(graphs, new_nodes, results, prog='neato', size=None,
         type_format=None):
    """
    Display the results using matplotlib.
    """
    if not size:
        size = _get_size(len(graphs))
    fig, axarr = plt.subplots(size[0], size[1], figsize=(18, 10))
    fig.set_facecolor('white')
    x_val = 0
    y_val = 0
    index = 0

    if size[0] == 1:
        axarr = axarr.reshape((1, size[1]))

    for candidate in graphs:
        # axarr[x_val, y_val].axis('off')
        axarr[x_val, y_val].xaxis.set_major_formatter(plt.NullFormatter())
        axarr[x_val, y_val].yaxis.set_major_formatter(plt.NullFormatter())
        axarr[x_val, y_val].xaxis.set_ticks([])
        axarr[x_val, y_val].yaxis.set_ticks([])
        axarr[x_val, y_val].set_title(results[index])
        axarr[x_val, y_val].set_axis_bgcolor("white")
        if not type_format:
            type_format = TYPE_FORMAT
        _plot_sub_plot(candidate, new_nodes, prog, type_format,
                       axarr[x_val, y_val])
        y_val += 1
        if y_val > size[1] - 1:
            y_val = 0
            x_val += 1
        index += 1
    fig.tight_layout()
    plt.show()


def _plot_sub_plot(graph, new_nodes, prog, type_format, axes):
    """
    Plot a single candidate graph.
    """
    pos = nx.graphviz_layout(graph, prog=prog)

    green_nodes = []
    yellow_nodes = []
    red_nodes = []
    blue_nodes = []
    for node, values in graph.nodes(data=True):
        shape = 'o'
        if values['type'] in type_format:
            shape = type_format[values['type']]
        color = 'g'
        alpha = 0.8
        if node in new_nodes:
            color = 'b'
            alpha = 0.2
        elif 'rank' in values and values['rank'] > 7:
            color = 'r'
        elif 'rank' in values and values['rank'] < 7 and values['rank'] > 3:
            color = 'y'
        nx.draw_networkx_nodes(graph, pos, nodelist=[node], node_color=color,
                               node_shape=shape, alpha=alpha, ax=axes)

        if node in new_nodes:
            blue_nodes.append(node)
        elif 'rank' in values and values['rank'] > 7:
            red_nodes.append(node)
        elif 'rank' in values and values['rank'] < 7 and values['rank'] > 3:
            yellow_nodes.append(node)
        else:
            green_nodes.append(node)

    # draw the edges
    dotted_line = []
    normal_line = []
    for src, trg in graph.edges():
        if src in new_nodes and trg not in new_nodes:
            dotted_line.append((src, trg))
        else:
            normal_line.append((src, trg))
    nx.draw_networkx_edges(graph, pos, edgelist=dotted_line, style='dotted',
                           ax=axes)
    nx.draw_networkx_edges(graph, pos, edgelist=normal_line, ax=axes)

    # draw labels
    nx.draw_networkx_labels(graph, pos, ax=axes)
<commit_msg>Update vis.py<commit_after>
"""
Visualize possible stitches with the outcome of the validator.
"""

import math

import matplotlib.pyplot as plt
import networkx as nx

TYPE_FORMAT = {'a': '^', 'b': 's', 'c': 'v'}


def show(graphs, new_nodes, results, prog='neato', size=None,
         type_format=None):
    """
    Display the results using matplotlib.
    """
    if not size:
        size = _get_size(len(graphs))
    fig, axarr = plt.subplots(size[0], size[1], figsize=(18, 10))
    fig.set_facecolor('white')
    x_val = 0
    y_val = 0
    index = 0

    if size[0] == 1:
        axarr = axarr.reshape((1, size[1]))

    for candidate in graphs:
        # axarr[x_val, y_val].axis('off')
        axarr[x_val, y_val].xaxis.set_major_formatter(plt.NullFormatter())
        axarr[x_val, y_val].yaxis.set_major_formatter(plt.NullFormatter())
        axarr[x_val, y_val].xaxis.set_ticks([])
        axarr[x_val, y_val].yaxis.set_ticks([])
        axarr[x_val, y_val].set_title(results[index])
        axarr[x_val, y_val].set_axis_bgcolor("white")
        if not type_format:
            type_format = TYPE_FORMAT
        _plot_sub_plot(candidate, new_nodes, prog, type_format,
                       axarr[x_val, y_val])
        y_val += 1
        if y_val > size[1] - 1:
            y_val = 0
            x_val += 1
        index += 1
    fig.tight_layout()
    plt.show()


def _plot_sub_plot(graph, new_nodes, prog, type_format, axes):
    """
    Plot a single candidate graph.
    """
    pos = nx.graphviz_layout(graph, prog=prog)

    # draw the nodes
    for node, values in graph.nodes(data=True):
        shape = 'o'
        if values['type'] in type_format:
            shape = type_format[values['type']]
        color = 'g'
        alpha = 0.8
        if node in new_nodes:
            color = 'b'
            alpha = 0.2
        elif 'rank' in values and values['rank'] > 7:
            color = 'r'
        elif 'rank' in values and values['rank'] < 7 and values['rank'] > 3:
            color = 'y'
        nx.draw_networkx_nodes(graph, pos, nodelist=[node], node_color=color,
                               node_shape=shape, alpha=alpha, ax=axes)

    # draw the edges
    dotted_line = []
    normal_line = []
    for src, trg in graph.edges():
        if src in new_nodes and trg not in new_nodes:
            dotted_line.append((src, trg))
        else:
            normal_line.append((src, trg))
    nx.draw_networkx_edges(graph, pos, edgelist=dotted_line, style='dotted',
                           ax=axes)
    nx.draw_networkx_edges(graph, pos, edgelist=normal_line, ax=axes)

    # draw labels
    nx.draw_networkx_labels(graph, pos, ax=axes)

{'fraction_non_alphanumeric': 0.12602691837091418, 'fraction_numerical': 0.009613703897919944, 'mean_word_length': 2.252984650369528, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 11, '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': '298343', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1921, 'n_words': 454}