Grid
Generate snailz sample grids using invasion percolation.
To create a grid:
- Generate an MxN grid of random numbers. (Our are always square and odd-sized.)
- Mark the center cell as "filled" by negating its value.
- On each iteration:
- Find the lowest-valued cell adjacent to the filled region.
- Fill that in by negating its value. (If several cells tie for lowest value, pick one at random.)
- Stop when the filled region hits the edge of the grid.
Instead of repeatedly searching for cells adjacent to the filled region, the grid keeps a value-to-coordinates dictionary. When a cell is filled in, neighbors not already recorded are added.
The grid is saved as comma-separated values. 0 shows unfilled cells; filled cells contain their original (positive) value.
Grid
Represent a 2D grid that supports lazy filling.
Source code in snailz/grid.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
depth: int
property
Get depth of grid.
height: int
property
Get height of grid.
width: int
property
Get width of grid.
__init__(params)
Record shared state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
GridParams
|
grid creation parameters. |
required |
Source code in snailz/grid.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
__getitem__(key)
Get value at location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
list | tuple
|
(x, y) coordinates. |
required |
Returns:
Type | Description |
---|---|
int
|
Value at that location. |
Source code in snailz/grid.py
48 49 50 51 52 53 54 55 56 57 58 |
|
__setitem__(key, value)
Set value at location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
list | tuple
|
(x, y) coordinates. |
required |
value
|
int
|
new value. |
required |
Source code in snailz/grid.py
60 61 62 63 64 65 66 67 68 |
|
__str__()
Convert to printable string.
Source code in snailz/grid.py
70 71 72 73 74 75 |
|
fill()
Fill grid one cell at a time.
Source code in snailz/grid.py
92 93 94 95 96 97 98 99 100 101 |
|
add_candidates(x, y)
Add candidates next to a newly-filled cell.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
int
|
X coordinate of newly-filled cell. |
required |
y
|
int
|
Y coordinate of newly-filled cell. |
required |
Source code in snailz/grid.py
103 104 105 106 107 108 109 110 111 112 113 114 |
|
add_one_candidate(x, y)
Add a point to the set of candidates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
int
|
X coordinate of potential candidate. |
required |
y
|
int
|
Y coordinate of potential candidate. |
required |
Source code in snailz/grid.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
adjacent(x, y)
Is (x, y) adjacent to a filled cell?
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
int
|
X coordinate of cell to check. |
required |
y
|
int
|
Y coordinate of cell to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether or not this cell is adjacent to a filled cell. |
Source code in snailz/grid.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
choose_cell()
Choose the next cell to fill.
Returns:
Type | Description |
---|---|
tuple
|
(x, y) coordinates of next cell to fill. |
Source code in snailz/grid.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
on_border(x, y)
Is this cell on the border of the grid?
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
int
|
X coordinate of cell to check. |
required |
y
|
int
|
Y coordinate of cell to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether or not this cell is on the border of the grid. |
Source code in snailz/grid.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
grid(options)
Main driver for grid generation.
- options.outdir: path to output directory.
- options.params: path to grid parameter file.
- options.sites: path to sites parameter file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
options
|
Namespace
|
see above. |
required |
Source code in snailz/grid.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
_save(outdir, site_id, grid)
Save grid as CSV.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
outdir
|
str
|
output directory. |
required |
site_id
|
str
|
site identifier (used to construct output filename). |
required |
grid
|
Grid
|
what to save. |
required |
Source code in snailz/grid.py
208 209 210 211 212 213 214 215 216 217 218 219 220 |
|