Skip to content

Stencils

In this tutorial we will look at the Stencils in topoGenesis and the computational concept behind them.

import topogenesis as tg

Creation and basic properties

We can create a stencil based on well-known neighbourhood definitions: Von Neumann or Moore

von_neumann_stencil = tg.create_stencil("von_neumann", 1, 1)

print(type(von_neumann_stencil))
print(von_neumann_stencil)
<class 'topogenesis.datastructures.datastructures.stencil'>
[[[0 0 0]
  [0 1 0]
  [0 0 0]]

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

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

We can also expand the stencil into the local addresses

print(von_neumann_stencil.expand())
[[ 0  0  0]
 [ 1  0  0]
 [ 0  1  0]
 [ 0  0  1]
 [ 0  0 -1]
 [ 0 -1  0]
 [-1  0  0]]

Neigbourhood type in stencils

Check the neighborhoud type and origin

print(von_neumann_stencil.ntype)
print(von_neumann_stencil.origin)
von_neumann
[1 1 1]

Create a moore neighbourhood stencil

moore_stencil = tg.create_stencil("moore", 1, 1)
print(moore_stencil)
[[[1 1 1]
  [1 1 1]
  [1 1 1]]

 [[1 1 1]
  [1 1 1]
  [1 1 1]]

 [[1 1 1]
  [1 1 1]
  [1 1 1]]]

Customizing stencils

Customizing the stencil with the help of global indicies: in this case removing the up-center cell

moore_stencil[1,0,1] = 0
print(moore_stencil)
[[[1 1 1]
  [1 1 1]
  [1 1 1]]

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

 [[1 1 1]
  [1 1 1]
  [1 1 1]]]

Customizing the stencil with the help of local indicies: in this case removing the down-center cell.

moore_stencil.set_index([0,1,0], 0)
print(moore_stencil)
[[[1 1 1]
  [1 1 1]
  [1 1 1]]

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

 [[1 1 1]
  [1 1 1]
  [1 1 1]]]

Stencils and Universal Functions

We can peform all universal functions(addition, subtraction, multiplication, etc) on stencils.

custom_stencil = moore_stencil - von_neumann_stencil

print(custom_stencil.ntype)
print(custom_stencil)
custom
[[[1 1 1]
  [1 0 1]
  [1 1 1]]

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

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