-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforest.py
274 lines (227 loc) · 8.66 KB
/
forest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# Source at https://github.com/timdiller/complexity/blob/master/forest.py
import numpy as np
from numpy.random import uniform
from scipy.ndimage.measurements import label
from chaco.api import ArrayPlotData, Plot, VPlotContainer
from enable.api import ComponentEditor
from pyface.timer.api import Timer
from traits.api import (HasTraits, Array, Bool, Button, DelegatesTo, Enum,
Instance, Int, Property, Range,
String)
from traitsui.api import ButtonEditor, HGroup, Item, VGroup, View
history_length = 3000
def randbool(nx, ny, p):
return uniform(size=(nx, ny)) <= p
class Forest(HasTraits):
p_lightning = Range(0., 0.0005, 5.e-6)
p_sapling = Range(0., 0.005, 0.0025)
forest_trees = Array(dtype=bool)
forest_fires = Array(dtype=bool)
size_x = Int(150)
size_y = Int(150)
def _forest_trees_default(self):
return np.zeros((self.size_x, self.size_y))
def _forest_fires_default(self):
return np.zeros((self.size_x, self.size_y))
def advance_one_day(self):
self.grow_trees()
self.start_fires()
self.burn_trees()
def burn_trees(self):
fires = np.zeros((self.size_x + 2, self.size_y + 2), dtype=bool)
fires[1:-1, 1:-1] = self.forest_fires
north = fires[:-2, 1:-1]
south = fires[2:, 1:-1]
east = fires[1:-1, :-2]
west = fires[1:-1, 2:]
new_fires = (north | south | east | west) & self.forest_trees
self.forest_trees[self.forest_fires] = False
self.forest_fires = new_fires
def grow_trees(self):
growth_sites = randbool(self.size_x, self.size_y, self.p_sapling)
self.forest_trees[growth_sites] = True
def start_fires(self):
lightning_strikes = randbool(self.size_x, self.size_y,
self.p_lightning) & self.forest_trees
self.forest_fires[lightning_strikes] = True
class InstantBurnForest(Forest):
def advance_one_day(self):
self.grow_trees()
self.strike_and_burn()
def strike_and_burn(self):
strikes = randbool(self.size_x, self.size_y,
self.p_lightning) & self.forest_trees
groves, num_groves = label(self.forest_trees)
fires = set(groves[strikes])
self.forest_fires.fill(False)
for fire in fires:
self.forest_fires[groves == fire] = True
self.forest_trees[self.forest_fires] = False
class ForestView(HasTraits):
# UI Elements
day = Button("Advance 1 Day")
histograms = Instance(Plot)
fire_time_plot = Instance(Plot)
forest_plot = Instance(Plot)
forest_image = Property(Array, depends_on="forest")
run_label = Property(String, depends_on="run")
run_button = Button
time_plots = Instance(VPlotContainer)
trait_to_histogram = Property(depends_on="which_histogram")
tree_time_plot = Instance(Plot)
which_histogram = Enum("trees", "fire")
# ModelView Elements
density_function = Property(Array)
fractions = Property(Array(dtype=float))
fire_history = Array(dtype=float)
forest = Instance(Forest)
p_sapling = DelegatesTo("forest", "p_sapling")
p_lightning = DelegatesTo("forest", "p_lightning")
plot_data = Instance(ArrayPlotData)
time = Array(dtype=int)
tree_history = Array(dtype=float)
run = Bool
traits_view = View(
HGroup(
VGroup(
VGroup(Item("forest_plot",
editor=ComponentEditor(),
show_label=False),),
Item("p_sapling", label="trees"),
Item("p_lightning", label="fires"),
),
VGroup(
Item("time_plots", editor=ComponentEditor(),
show_label=False),
HGroup(
Item("run_button",
editor=ButtonEditor(label_value="run_label"),
show_label=False),
Item("which_histogram", show_label=False),
Item("day", show_label=False),
),
),
),
resizable=True,
)
def update_fire_history(self):
self.fire_history[1:] = self.fire_history[:-1]
self.fire_history[0] = float(np.sum(self.forest.forest_fires)) / \
self.forest.forest_fires.size
def update_tree_history(self):
self.tree_history[1:] = self.tree_history[:-1]
self.tree_history[0] = float(np.sum(self.forest.forest_trees)) / \
self.forest.forest_trees.size
def update_time(self):
self.time[1:] = self.time[:-1]
self.time[0] = self.time[1] + 1
def _advance(self):
self.forest.advance_one_day()
self.update_fire_history()
self.update_tree_history()
self.update_time()
self.plot_data.set_data("forest_image", self.forest_image)
self.plot_data.set_data("fire_history", self.fire_history)
self.plot_data.set_data("tree_history", self.tree_history)
self.plot_data.set_data("density_function",
self.density_function)
self.plot_data.set_data("time", self.time)
self.plot_data.set_data("fractions", self.fractions)
def _day_fired(self):
self._advance()
def _fire_history_default(self):
return np.zeros((history_length, ), dtype=float)
def _fire_time_plot_default(self):
plot = Plot(self.plot_data, title="Fractional area with fires")
plot.plot(["time", "fire_history"])
return plot
def _forest_plot_default(self):
plot = Plot(self.plot_data)
plot.img_plot("forest_image")
plot.bounds = [0., 2.0]
return plot
def _get_fractions(self):
data = self.trait_to_histogram
return np.linspace(data.min(), data.max(), 50)
def _get_fire_density_function(self):
hist, bins = np.histogram(self.fire_history, bins=self.fractions,
normed=True)
tot = np.sum(hist)
if tot > 0:
hist /= tot
return hist
def _get_density_function(self):
time_since_start = self.time > 0
data = self.trait_to_histogram
hist, bins = np.histogram(data[time_since_start],
bins=self.fractions, normed=True)
tot = np.sum(hist)
if tot > 0:
hist /= tot
return hist
def _get_forest_image(self):
image = np.zeros((self.forest.size_x, self.forest.size_y, 3),
dtype=np.uint8)
image[:, :, 0] = 255 * self.forest.forest_fires
image[:, :, 1] = 128 * self.forest.forest_trees
return image
def _get_run_label(self):
if self.run:
label = "Stop"
else:
label = "Run"
return label
def _get_trait_to_histogram(self):
trait_to_histogram = {
"trees": self.tree_history,
"fire": self.fire_history,
}
return trait_to_histogram[self.which_histogram]
def _histograms_default(self):
plot = Plot(self.plot_data)
plot.plot(["fractions", "density_function"], color="green")
return plot
def _plot_data_default(self):
data = ArrayPlotData(forest_image=self.forest_image,
tree_history=self.tree_history,
fire_history=self.fire_history,
fractions=self.fractions,
density_function=self.density_function,
time=self.time)
return data
def _run_button_fired(self):
if self.run:
self.run = False
else:
self.run = True
def _run_changed(self):
if self.run:
self.timer.Start()
else:
self.timer.Stop()
def _run_default(self):
self.timer = Timer(150, self._timer_tick)
return False
def _time_plots_default(self):
return VPlotContainer(self.fire_time_plot, self.tree_time_plot,
self.histograms, spacing=0.)
def _timer_tick(self):
if not self.run:
raise StopIteration
else:
self._advance()
def _tree_history_default(self):
return np.zeros((history_length, ), dtype=float)
def _tree_time_plot_default(self):
plot = Plot(self.plot_data, title="Fractional area covered by trees")
plot.plot(["time", "tree_history"])
return plot
def _time_default(self):
time = np.zeros((history_length, ), dtype=int)
time[0] = 1
return time
if __name__ == "__main__":
f = InstantBurnForest()
# f = Forest()
fv = ForestView(forest=f)
fv.configure_traits()