Geo
dissmodel.geo.CellularAutomaton
Bases: Model, ABC
Base class for spatial cellular automata backed by a GeoDataFrame.
Extends :class:~dissmodel.core.Model with neighborhood management and
a cell-by-cell transition rule loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
GeoDataFrame with geometries and a state attribute. |
required |
state_attr
|
str
|
Column name representing the cell state, by default |
'state'
|
step
|
float
|
Time increment per execution step, by default 1. |
1
|
start_time
|
float
|
Simulation start time, by default 0. |
0
|
end_time
|
float
|
Simulation end time, by default |
inf
|
name
|
str
|
Optional model name, by default |
''
|
dim
|
tuple of int
|
Grid dimensions as |
None
|
**kwargs
|
Any
|
Extra keyword arguments forwarded to :class: |
{}
|
Examples:
>>> class MyCA(CellularAutomaton):
... def rule(self, idx):
... return self.gdf.loc[idx, self.state_attr]
Source code in dissmodel/geo/celullar_automaton.py
19 20 21 22 23 24 25 26 27 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
create_neighborhood(strategy=Queen, neighbors_dict=None, **kwargs)
Build and attach the neighborhood structure to the GeoDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
type
|
Libpysal weight class (e.g. |
Queen
|
neighbors_dict
|
dict or str
|
Precomputed |
None
|
**kwargs
|
Any
|
Extra keyword arguments forwarded to the strategy. |
{}
|
Source code in dissmodel/geo/celullar_automaton.py
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 | |
execute()
Execute one simulation step by applying :meth:rule to every cell.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the neighborhood has not been created yet. |
Notes
Because :meth:rule is an arbitrary Python function, the update
cannot be vectorized automatically. Performance-critical subclasses
should prefer :meth:neighbor_values over :meth:neighs inside
rule to avoid geometry overhead on every lookup.
Source code in dissmodel/geo/celullar_automaton.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
initialize()
Set up the initial model state.
Override in subclasses to define the starting conditions.
Source code in dissmodel/geo/celullar_automaton.py
76 77 78 79 80 81 82 | |
neighbor_values(idx, col)
Return the values of col for all neighbors of cell idx.
Faster than neighs(idx)[col] because it skips geometry overhead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
any
|
Index of the cell in the GeoDataFrame. |
required |
col
|
str
|
Column name to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of neighbor values. |
Source code in dissmodel/geo/celullar_automaton.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
neighs(idx)
Return the neighboring cells of idx as a GeoDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
any
|
Index of the cell in the GeoDataFrame. |
required |
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
GeoDataFrame containing the neighboring rows. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the neighborhood has not been created yet. |
ValueError
|
If the |
Notes
Returns a GeoDataFrame slice, which involves Pandas overhead.
For performance-critical rule evaluation inside simulation loops,
prefer :meth:neighbor_values which returns a NumPy array directly.
Source code in dissmodel/geo/celullar_automaton.py
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 | |
neighs_id(idx)
Return the neighbor indices for cell idx.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
any
|
Index of the cell in the GeoDataFrame. |
required |
Returns:
| Type | Description |
|---|---|
list
|
List of neighbor indices. |
Source code in dissmodel/geo/celullar_automaton.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
rule(idx)
abstractmethod
Transition rule applied to each cell.
Must be overridden in subclasses to define the state transition logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
any
|
Index of the cell being evaluated. |
required |
Returns:
| Type | Description |
|---|---|
any
|
New state value for the cell. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If not overridden by the subclass. |
Source code in dissmodel/geo/celullar_automaton.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
dissmodel.geo.regular_grid
parse_idx(idx)
Extract x and y from an index string in 'y-x' format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
str
|
Index string in |
required |
Returns:
| Type | Description |
|---|---|
tuple of int
|
|
Examples:
>>> parse_idx('3-4')
(4, 3)
>>> parse_idx('0-0')
(0, 0)
Source code in dissmodel/geo/regular_grid.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
regular_grid(gdf=None, bounds=None, resolution=None, dimension=None, attrs=None, crs=None)
Create a regular grid of fixed-size cells.
Exactly one of the following input combinations must be provided:
dimension+resolution— abstract grid with no geographic locationbounds+resolution— grid fitted to a bounding box by cell sizebounds+dimension— grid fitted to a bounding box by cell countgdf— bounds are extracted from the GeoDataFrame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
GeoDataFrame used to extract the bounding box. |
None
|
bounds
|
tuple of float
|
Bounding box as |
None
|
resolution
|
float
|
Cell size in coordinate units. |
None
|
dimension
|
tuple of int
|
Grid shape as |
None
|
attrs
|
dict
|
Extra attributes added to every cell, e.g. |
None
|
crs
|
str or int
|
Coordinate reference system. If |
None
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
Regular grid where each row is a cell with a Polygon geometry,
indexed by |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input combination is insufficient to define the grid. |
Examples:
>>> gdf = regular_grid(dimension=(3, 3), resolution=1.0)
>>> len(gdf)
9
>>> gdf.index[0]
'0-0'
Source code in dissmodel/geo/regular_grid.py
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 | |
dissmodel.geo.fill
FillStrategy
Bases: str, Enum
Available fill strategies for populating GeoDataFrame attributes.
Attributes:
| Name | Type | Description |
|---|---|---|
ZONAL_STATS |
str
|
Fill cells with statistics extracted from a raster. |
MIN_DISTANCE |
str
|
Fill cells with the minimum distance to a target GeoDataFrame. |
RANDOM_SAMPLE |
str
|
Fill cells with random samples drawn from a distribution. |
PATTERN |
str
|
Fill cells using a 2-D pattern matrix. |
Examples:
>>> FillStrategy.RANDOM_SAMPLE
<FillStrategy.RANDOM_SAMPLE: 'random_sample'>
>>> FillStrategy("pattern")
<FillStrategy.PATTERN: 'pattern'>
Source code in dissmodel/geo/fill.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
fill(strategy, **kwargs)
Execute a fill strategy by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
FillStrategy or str
|
Strategy to execute. Accepted values: |
required |
**kwargs
|
Any
|
Arguments forwarded to the chosen strategy function. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Whatever the strategy function returns. Most strategies mutate the
GeoDataFrame in place and return |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> fill(FillStrategy.RANDOM_SAMPLE, gdf=grid, attr="state",
... data=[0, 1], seed=42)
>>> fill("min_distance", from_gdf=grid, to_gdf=roads,
... attr_name="dist_road")
>>> fill(FillStrategy.PATTERN, gdf=grid, attr="zone",
... pattern=[[1, 2], [3, 4]])
Source code in dissmodel/geo/fill.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
register_strategy(name)
Register a fill strategy under the given name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
FillStrategy
|
Key under which the strategy will be registered. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
Decorator that registers and returns the decorated function. |
Source code in dissmodel/geo/fill.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
dissmodel.geo.neighborhood.attach_neighbors(gdf, strategy=None, neighbors_dict=None, **kwargs)
Attach a neighborhood structure to a GeoDataFrame.
Adds a '_neighs' column containing the list of neighbor indices for
each cell. Mutates and returns the same GeoDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
GeoDataFrame whose cells will receive the neighborhood column. |
required |
strategy
|
WeightStrategy
|
Libpysal weight class (e.g. |
None
|
neighbors_dict
|
dict or str
|
Precomputed neighborhood. Accepted formats:
|
None
|
**kwargs
|
Any
|
Extra keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
The same |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If a string path is provided in |
ValueError
|
If |
ValueError
|
If neither |
Examples:
>>> from libpysal.weights import Queen
>>> gdf = attach_neighbors(gdf, strategy=Queen)
>>> gdf = attach_neighbors(gdf, neighbors_dict="neighborhood.json")
>>> gdf = attach_neighbors(gdf, strategy=Queen, ids="cell_id")
Source code in dissmodel/geo/neighborhood.py
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 | |