Пример #1
0
def test_adjacency_extraction():
    from wfc_tiles import make_tile_catalog
    from wfc_patterns import make_pattern_catalog
    import imageio

    # TODO: generalize this to more than the four cardinal directions
    direction_offsets = list(enumerate([(0, -1), (1, 0), (0, 1), (-1, 0)]))

    filename = "../images/samples/Red Maze.png"
    img = imageio.imread(filename)
    tile_size = 1
    pattern_width = 2
    rotations = 0
    tile_catalog, tile_grid, code_list, unique_tiles = make_tile_catalog(
        img, tile_size)
    pattern_catalog, pattern_weights, pattern_list, pattern_grid = make_pattern_catalog(
        tile_grid, pattern_width, rotations)
    adjacency_relations = adjacency_extraction(pattern_grid, pattern_catalog,
                                               direction_offsets)
    assert (((0, -1), -6150964001204120324, -4042134092912931260)
            in adjacency_relations)
    assert (((-1, 0), -4042134092912931260, 3069048847358774683)
            in adjacency_relations)
    assert (((1, 0), -3950451988873469076, -3950451988873469076)
            in adjacency_relations)
    assert (((-1, 0), -3950451988873469076, -3950451988873469076)
            in adjacency_relations)
    assert (((0, 1), -3950451988873469076, 3336256675067683735)
            in adjacency_relations)
    assert (not ((0, -1), -3950451988873469076, -3950451988873469076)
            in adjacency_relations)
    assert (not ((0, 1), -3950451988873469076, -3950451988873469076)
            in adjacency_relations)
Пример #2
0
def test_recurse_vs_loop():
    from wfc_tiles import make_tile_catalog
    from wfc_patterns import make_pattern_catalog, pattern_grid_to_tiles
    from wfc_adjacency import adjacency_extraction
    from wfc_solver import run, makeWave, makeAdj, lexicalLocationHeuristic, lexicalPatternHeuristic
    from wfc_visualize import figure_list_of_tiles, figure_false_color_tile_grid, figure_pattern_catalog, render_tiles_to_output, figure_adjacencies

    import imageio
    img = imageio.imread("../images/samples/Red Maze.png")
    tile_size = 1
    pattern_width = 2
    rotations = 0
    output_size = [84, 84]
    ground = None
    direction_offsets = list(enumerate([(0, -1), (1, 0), (0, 1), (-1, 0)]))
    tile_catalog, tile_grid, code_list, unique_tiles = make_tile_catalog(
        img, tile_size)
    pattern_catalog, pattern_weights, pattern_list, pattern_grid = make_pattern_catalog(
        tile_grid, pattern_width, rotations)
    adjacency_relations = adjacency_extraction(pattern_grid, pattern_catalog,
                                               direction_offsets)
    number_of_patterns = len(pattern_weights)
    encode_patterns = dict(enumerate(pattern_list))
    decode_patterns = {x: i for i, x in enumerate(pattern_list)}
    decode_directions = {j: i for i, j in direction_offsets}
    adjacency_list = {}
    for i, d in direction_offsets:
        adjacency_list[d] = [set() for i in pattern_weights]
    for i in adjacency_relations:
        adjacency_list[i[0]][decode_patterns[i[1]]].add(decode_patterns[i[2]])
    wave = makeWave(number_of_patterns, output_size[0], output_size[1])
    adjacency_matrix = makeAdj(adjacency_list)
    solution_loop = run(wave.copy(),
                        adjacency_matrix,
                        locationHeuristic=lexicalLocationHeuristic,
                        patternHeuristic=lexicalPatternHeuristic,
                        periodic=True,
                        backtracking=False,
                        onChoice=None,
                        onBacktrack=None)
    solution_recurse = run_recurse(wave.copy(),
                                   adjacency_matrix,
                                   locationHeuristic=lexicalLocationHeuristic,
                                   patternHeuristic=lexicalPatternHeuristic,
                                   periodic=True,
                                   backtracking=False,
                                   onChoice=None,
                                   onBacktrack=None)
    assert (numpy.array_equiv(solution_loop, solution_recurse))
Пример #3
0
def test_pattern_to_tile():
    from wfc_tiles import make_tile_catalog
    import imageio
    filename = "../images/samples/Red Maze.png"
    img = imageio.imread(filename)
    tile_size = 1
    pattern_width = 2
    rotations = 0
    tile_catalog, tile_grid, code_list, unique_tiles = make_tile_catalog(
        img, tile_size)

    pattern_catalog, pattern_weights, pattern_list, pattern_grid = make_pattern_catalog(
        tile_grid, pattern_width)
    new_tile_grid = pattern_grid_to_tiles(pattern_grid, pattern_catalog)
    assert (np.array_equal(tile_grid, new_tile_grid))
Пример #4
0
def test_unique_patterns_2d():
    from wfc_tiles import make_tile_catalog
    import imageio
    filename = "../images/samples/Red Maze.png"
    img = imageio.imread(filename)
    tile_size = 1
    pattern_width = 2
    rotations = 0
    tile_catalog, tile_grid, code_list, unique_tiles = make_tile_catalog(
        img, tile_size)

    patterns_in_grid, pattern_contents_list, patch_codes = unique_patterns_2d(
        tile_grid, pattern_width, True)
    assert (patch_codes[1][2] == 4867810695119132864)
    assert (pattern_contents_list[7][1][1] == 8253868773529191888)
Пример #5
0
def test_make_pattern_catalog():
    from wfc_tiles import make_tile_catalog
    import imageio
    filename = "../images/samples/Red Maze.png"
    img = imageio.imread(filename)
    tile_size = 1
    pattern_width = 2
    rotations = 0
    tile_catalog, tile_grid, code_list, unique_tiles = make_tile_catalog(
        img, tile_size)

    pattern_catalog, pattern_weights, pattern_list, pattern_grid = make_pattern_catalog(
        tile_grid, pattern_width)
    assert (pattern_weights[-6150964001204120324] == 1)
    assert (pattern_list[3] == 2800765426490226432)
    assert (pattern_catalog[5177878755649963747][0][1] == -8754995591521426669)
Пример #6
0
wfc_ns_chess = find_pattern_center(wfc_ns_chess)

wfc_ns_chess = wfc_utilities.load_visualizer(wfc_ns_chess)

##
## Load image and make tile data structures
##

wfc_ns_chess.img = load_source_image(wfc_ns_chess.img_filename)
wfc_ns_chess.tiles = image_to_tiles(wfc_ns_chess.img, wfc_ns_chess.tile_size)
(
    wfc_ns_chess.tile_catalog,
    wfc_ns_chess.tile_grid,
    wfc_ns_chess.code_list,
    wfc_ns_chess.unique_tiles,
) = make_tile_catalog(wfc_ns_chess)
wfc_ns_chess.tile_ids = {
    v: k
    for k, v in dict(enumerate(wfc_ns_chess.unique_tiles[0])).items()
}
print(wfc_ns_chess.unique_tiles)
wfc_ns_chess.tile_weights = {
    a: b
    for a, b in zip(wfc_ns_chess.unique_tiles[0], wfc_ns_chess.unique_tiles[1])
}
print(wfc_ns_chess.tile_weights)

# print("wfc_ns_chess.tile_catalog")
# print(wfc_ns_chess.tile_catalog)
# print("wfc_ns_chess.tile_grid")
# print(wfc_ns_chess.tile_grid)
Пример #7
0
     periodic_input=True,
     periodic_output=True,
     generated_size=(3, 3),
     screenshots=1,
     iteration_limit=0,
     allowed_attempts=1,
 )
 test_ns = wfc_utilities.find_pattern_center(test_ns)
 test_ns = wfc_utilities.load_visualizer(test_ns)
 test_ns.img = wfc_tiles.load_source_image(test_ns.img_filename)
 (
     test_ns.tile_catalog,
     test_ns.tile_grid,
     test_ns.code_list,
     test_ns.unique_tiles,
 ) = wfc_tiles.make_tile_catalog(test_ns)
 test_ns.tile_ids = {
     v: k
     for k, v in dict(enumerate(test_ns.unique_tiles[0])).items()
 }
 test_ns.tile_weights = {
     a: b
     for a, b in zip(test_ns.unique_tiles[0], test_ns.unique_tiles[1])
 }
 (
     test_ns.pattern_catalog,
     test_ns.pattern_weights,
     test_ns.patterns,
 ) = make_pattern_catalog_no_rotations(test_ns.tile_grid,
                                       test_ns.pattern_width)
 import doctest
Пример #8
0
  generated_size = (16,8), #Size of the final image
  screenshots = 1, # Number of times to run the algorithm, will produce this many distinct outputs
  iteration_limit = 0, # After this many iterations, time out. 0 = never time out.
  allowed_attempts = 1) # Give up after this many contradictions

wfc_ns_chess = find_pattern_center(wfc_ns_chess)

wfc_ns_chess = wfc_utilities.load_visualizer(wfc_ns_chess)

##
## Load image and make tile data structures
##

wfc_ns_chess.img = load_source_image(wfc_ns_chess.img_filename)
wfc_ns_chess.tiles = image_to_tiles(wfc_ns_chess.img, wfc_ns_chess.tile_size)
wfc_ns_chess.tile_catalog, wfc_ns_chess.tile_grid, wfc_ns_chess.code_list, wfc_ns_chess.unique_tiles = make_tile_catalog(wfc_ns_chess)
wfc_ns_chess.tile_ids = {v:k for k,v in dict(enumerate(wfc_ns_chess.unique_tiles[0])).items()}
print(wfc_ns_chess.unique_tiles)
wfc_ns_chess.tile_weights = {a:b for a,b in zip(wfc_ns_chess.unique_tiles[0], wfc_ns_chess.unique_tiles[1])}
print(wfc_ns_chess.tile_weights)

#print("wfc_ns_chess.tile_catalog")
#print(wfc_ns_chess.tile_catalog)
#print("wfc_ns_chess.tile_grid")
#print(wfc_ns_chess.tile_grid)
#print("wfc_ns_chess.code_list")
#print(wfc_ns_chess.code_list)
#print("wfc_ns_chess.unique_tiles")    
#print(wfc_ns_chess.unique_tiles)    

#assert False
Пример #9
0
    s_columns = 24 // min(24, img_ns.pattern_width)
    s_rows = 1 + (int(len(img_ns.pattern_catalog)) // s_columns)
    fig = figure(figsize=(s_columns, s_rows * 1.5))
    title('Extracted Patterns')
    for i,tcode in img_ns.pattern_catalog.items():
        pat_cat = img_ns.pattern_catalog[i]
        ptr = render_pattern(pat_cat, img_ns).astype(np.uint8)
        sp = subplot(s_rows, s_columns, i+1)
        spi = sp.imshow(ptr)
        spi.axes.xaxis.set_label_text(f'({img_ns.pattern_weights[i]})')
        sp.set_title(i)
        spi.axes.tick_params(labelleft=False,labelbottom=False, left=False, bottom=False)
        spi.axes.grid(False)
    plt.savefig(img_ns.output_filename + "_patterns.pdf", bbox_inches="tight")
    plt.close()


if __name__ == "__main__":
    import types
    import wfc_tiles
    test_ns = types.SimpleNamespace(img_filename = "red_maze.png", seed = 87386, tile_size = 1, pattern_width = 2, channels = 3, adjacency_directions = dict(enumerate([CoordXY(x=0,y=-1),CoordXY(x=1,y=0),CoordXY(x=0,y=1),CoordXY(x=-1,y=0)])), periodic_input = True, periodic_output = True, generated_size = (3,3), screenshots = 1, iteration_limit = 0, allowed_attempts = 1) 
    test_ns = wfc_utilities.find_pattern_center(test_ns)
    test_ns = wfc_utilities.load_visualizer(test_ns)
    test_ns.img = wfc_tiles.load_source_image(test_ns.img_filename)
    test_ns.tile_catalog, test_ns.tile_grid, test_ns.code_list, test_ns.unique_tiles = wfc_tiles.make_tile_catalog(test_ns)
    test_ns.tile_ids = {v:k for k,v in dict(enumerate(test_ns.unique_tiles[0])).items()}
    test_ns.tile_weights = {a:b for a,b in zip(test_ns.unique_tiles[0], test_ns.unique_tiles[1])}
    test_ns.pattern_catalog, test_ns.pattern_weights, test_ns.patterns = make_pattern_catalog_no_rotations(test_ns.tile_grid, test_ns.pattern_width)
    import doctest
    doctest.testmod()