<commit_before>
import re
import point
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import scipy.spatial as spatial


def get(grid, i, j):
    h = grid.info.height
    return grid.data[h * i + j]


def read_pgm(filename, byteorder='>'):
    with open(filename, 'rb') as f:
        buffer = f.read()
    try:
        header, width, height, maxval = re.search(
            b"(^P5\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups()
    except AttributeError:
        raise ValueError("Not a raw PGM file: '%s'" % filename)
    return np.frombuffer(
        buffer, dtype='u1' if int(maxval) < 256 else byteorder + 'u2',
        count=int(width) * int(height), offset=len(header))\
        .reshape((int(height), int(width)))


def nn_prm(N, k, grid):
    h, w = grid.shape
    xs = np.random.uniform(low=0, high=w - 1, size=N)
    ys = np.random.uniform(low=0, high=h - 1, size=N)
    data = zip(xs, ys)
    return nn_prm_given_points(N, k, grid, data)


def nn_prm_given_points(N, k, grid, data):
    G = nx.Graph()
    tree = spatial.KDTree(data)
    for i, vec in enumerate(data):
        x = vec[0]
        y = vec[1]
        if grid[y, x] > 0:
            G.add_node(i, position=point.Point(x, y))
            ds, inds = tree.query(vec, k=k)
            for d, j in zip(ds, inds):
                if grid[int(data[j][1]), int(data[j][0])] > 0:
                    p2 = point.Point(data[j][0], data[j][1])
                    if not crow_collision(grid, G.node[i]["position"], p2):
                        G.add_edge(i, j, distance=d)
    return G


def topo_prm(N, k, grid):
    G = nn_prm(N, k, grid)
    H = nx.Graph()
    S0 = [G.nodes()[0]]
    already_seen_0 = set()
    while len(S0) > 0:
        i = S0.pop()
        print i
        already_seen_0.add(i)
        S1 = [G.neighbors(i)[0]]
        already_seen_1 = set()
        while len(S1) > 0:
            j = S1.pop()
            imd = G.node[j]["position"]
            for k in G.neighbors(j):
                cur = G.node[i]["position"]
                nxt = G.node[k]["position"]
                if crow_collision(grid, cur, nxt):
                    if not j in already_seen_0:
                        H.add_node(i, position=cur)
                        H.add_node(j, position=imd)
                        H.add_edge(i, j)
                        S0.append(j)
                        already_seen_0.add(j)
                else:
                    if not k in already_seen_1:
                        S1.append(k)
                        already_seen_1.add(k)
    return H<commit_msg>STUPID ALGORITHM<commit_after>
import re
import point
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import scipy.spatial as spatial


def get(grid, i, j):
    h = grid.info.height
    return grid.data[h * i + j]


def read_pgm(filename, byteorder='>'):
    with open(filename, 'rb') as f:
        buffer = f.read()
    try:
        header, width, height, maxval = re.search(
            b"(^P5\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n])*"
            b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups()
    except AttributeError:
        raise ValueError("Not a raw PGM file: '%s'" % filename)
    return np.frombuffer(
        buffer, dtype='u1' if int(maxval) < 256 else byteorder + 'u2',
        count=int(width) * int(height), offset=len(header))\
        .reshape((int(height), int(width)))


def nn_prm(N, k, grid):
    h, w = grid.shape
    xs = np.random.uniform(low=0, high=w - 1, size=N)
    ys = np.random.uniform(low=0, high=h - 1, size=N)
    data = zip(xs, ys)
    return nn_prm_given_points(N, k, grid, data)


def nn_prm_given_points(N, k, grid, data):
    G = nx.Graph()
    tree = spatial.KDTree(data)
    for i, vec in enumerate(data):
        x = vec[0]
        y = vec[1]
        if grid[y, x] > 0:
            G.add_node(i, position=point.Point(x, y))
            ds, inds = tree.query(vec, k=k)
            for d, j in zip(ds, inds):
                if grid[int(data[j][1]), int(data[j][0])] > 0:
                    p2 = point.Point(data[j][0], data[j][1])
                    if not crow_collision(grid, G.node[i]["position"], p2):
                        G.add_edge(i, j, distance=d)
    return G


def topo_prm(N, k, grid):
    G = nn_prm(N, k, grid)
    H = nx.Graph()
    S0 = [G.nodes()[0]]
    already_seen_0 = set()
    counter = 0
    while len(S0) > 0:
        i = S0.pop()
        print i
        already_seen_0.add(i)
        S1 = [G.neighbors(i)[0]]
        already_seen_1 = set()
        while len(S1) > 0:
            j = S1.pop()
            imd = G.node[j]["position"]
            for k in G.neighbors(j):
                cur = G.node[i]["position"]
                nxt = G.node[k]["position"]
                if crow_collision(grid, cur, nxt):
                    if not j in already_seen_0:
                        H.add_node(i, position=cur)
                        H.add_node(j, position=imd)
                        H.add_edge(i, j)
                        S0.append(j)
                        already_seen_0.add(j)
                else:
                    if not k in already_seen_1:
                        S1.append(k)
                        already_seen_1.add(k)
    return H
{'fraction_non_alphanumeric': 0.1600222510661969, 'fraction_numerical': 0.014648618579640274, 'mean_word_length': 1.6584524396254312, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '3991244', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1946, 'n_words': 446}