def create_symmetry_stencils(sym_str):
    #  creating stencils
    so_base = tg.create_stencil("von_neumann", 0, 1)
    sx_base = copy.deepcopy(so_base)
    sx_base.set_index((0, 0, 0), 0)
    sx_base.set_index((1, 0, 0), 1)

    # setting separate direction
    corner_state = dict(OO=so_base,
                        XP=sx_base,
                        XN=np.flip(sx_base, 0),
                        YP=np.transpose(sx_base, (1, 0, 2)),
                        YN=np.transpose(np.flip(sx_base, 0), (1, 0, 2)),
                        ZP=np.transpose(sx_base, (2, 1, 0)),
                        ZN=np.transpose(np.flip(sx_base, 0), (2, 1, 0)))

    for key, value in corner_state.items():
        value.function = tg.sfunc.sum
        corner_state[key] = value

    # setting paired directions
    corner_state["XX"] = corner_state["XN"] + corner_state["XP"]
    corner_state["YY"] = corner_state["YN"] + corner_state["YP"]
    corner_state["ZZ"] = corner_state["ZN"] + corner_state["ZP"]

    stencils = []

    for i, sym in enumerate(sym_str):
        # init the symmetry stencil with the first item
        sym_stencil = copy.deepcopy(corner_state[sym[0]])
        # iteratively add the rest of the symmetry stencils together
        for key in sym[1:]:
            sym_stencil += corner_state[key]
        # append
        stencils.append(sym_stencil)
        # compute the sum of all stencils
        if i == 0:
            stencil_sum = copy.deepcopy(sym_stencil)
        else:
            stencil_sum += sym_stencil

    # check if there is any neighbours that are not included
    stencil_rest = tg.create_stencil("von_neumann", 1, 1) - stencil_sum

    # print(np.zeros((3,3,3)).sum())
    if (np.array(stencil_rest).sum()):
        stencil_rest.function = tg.sfunc.sum
        stencils.append(stencil_rest)

    # add the sum function to all stencils
    for s in stencils:
        s.function = tg.sfunc.sum

    return stencils
def save_design_templates(corner_loc_lattices, corner_neigh_lattices,
                          templates_path):
    interior_zero = tg.to_lattice(np.zeros((2, 2, 2), dtype=np.int8),
                                  [0, 0, 0])
    border_pad = np.pad(interior_zero, (1, 1), 'constant', constant_values=(1))
    base_zero = tg.to_lattice(np.zeros((4, 4, 4), dtype=np.int8), [0, 0, 0])
    s = tg.create_stencil("von_neumann", 1, 1)
    s.set_index([0, 0, 0], 0)

    for i, core, neigh in zip(range(len(corner_loc_lattices)),
                              corner_loc_lattices, corner_neigh_lattices):
        # construct saving paths
        core_path = os.path.join(templates_path, 'core_' + f'{i:02}' + '.csv')
        neigh_path = os.path.join(templates_path,
                                  'neighs_' + f'{i:02}' + '.csv')
        e_neigh_path = os.path.join(templates_path,
                                    'e_neighs_' + f'{i:02}' + '.csv')

        # aggregate neighbours
        neigh = tg.to_lattice(
            np.pad(neigh, (1, 1), 'constant', constant_values=(0)), [0, 0, 0])

        # construct the padded core
        core_pad = tg.to_lattice(
            np.pad(core, (1, 1), 'constant', constant_values=(0)), [0, 0, 0])
        # extract the outer neighbours of the core
        core_3ind = np.array(np.where(core_pad == 1)).flatten()
        pals = base_zero.find_neighbours_masked(s, loc=core_3ind)
        extra_neighs = np.copy(base_zero).flatten()
        # set the extra neighbours as the current state of the core
        extra_neighs[pals] = neigh[tuple(core_3ind)]
        extra_neighs = tg.to_lattice(extra_neighs.reshape((4, 4, 4)),
                                     [0, 0, 0])
        # remove interior pals
        extra_neighs *= border_pad

        # save to csv
        to_csv(core_pad, core_path)
        to_csv(neigh, neigh_path)
        to_csv(extra_neighs, e_neigh_path)
Пример #3
0
import topogenesis as tg
import numpy as np
import click  # for cleaning the command line
from time import sleep  # for delaying between iterations
np.random.seed(0)

# create a step one moore neighbourhood
s = tg.create_stencil("von_neumann", 1)

# set the center to 0, to prevent staying at the same point
s.set_index([0, 0, 0], 0)

# set the x-dimension to 0, since we are working in 2d
s.set_index([1, 0, 0], 0)
s.set_index([-1, 0, 0], 0)

# assign the random choice function
s.function = tg.sfunc.random_choice
"""
print(s)
[[[0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 1 0]
  [1 0 1]
  [0 1 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]]]
Пример #4
0
import topogenesis as tg
import numpy as np
import click  # for cleaning the command line
from time import sleep  # for delaying between iterations

# create a step one moore neighbourhood
s = tg.create_stencil("moore", 1)

# set the center to 0
s.set_index([0, 0, 0], 0)

# assign the sum function
s.function = tg.sfunc.sum  # np.sum
"""
print(s)
[[[1 1 1]
  [1 1 1]
  [1 1 1]]

 [[1 1 1]
  [1 0 1]
  [1 1 1]]

 [[1 1 1]
  [1 1 1]
  [1 1 1]]]
"""

# initiate the lattice
l = tg.lattice([[0, -1, -1], [0, 1, 1]], default_value=0, dtype=int)
l[0, :, 1] += 1