-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_ho.py
63 lines (53 loc) · 1.77 KB
/
simulation_ho.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
"""
Simulate herbivore population in single lowland cell for several seeds.
"""
__author__ = "Tonje Martine Lorgen Kirkholt", "Sougata Bhattacharya"
__email__ = "[email protected]", "[email protected]"
import re
import sys
import textwrap
from pathlib import Path
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from biosim.simulation import BioSim
geogr = """\
WWW
WLW
WWW"""
geogr = textwrap.dedent(geogr)
ini_herbs = [{'loc': (2, 2),
'pop': [{'species': 'Herbivore',
'age': 5,
'weight': 20}
for _ in range(50)]}]
for seed in range(100, 150):
sim = BioSim(geogr, ini_herbs, seed=seed,
log_file=f'data/simulation_ho_{seed:05d}',
img_dir='results',
img_base=f'mono_ho_{seed:05d}',
img_years=300)
sim.simulate(301)
# Analyze logs:
data = []
plt.rcParams['figure.figsize'] = (12, 6)
for logfile in Path(f"{sys.path[0]}/data").glob('simulation_ho_*.csv'):
d = pd.read_csv(logfile,
skiprows=1,
usecols=[0, 1],
index_col=0,
names=['Year', 'Herbivores'])
d['Seed'] = int(re.match(r'.*_(\d+)\.csv', str(logfile))[1])
data.append(d)
hd = pd.concat(data).pivot(columns='Seed')
print(hd.head())
hd.Herbivores.plot(legend=False, alpha=0.8)
plt.show()
hd_eq = hd.loc[hd.index >= 100, :]
print(f"Mean list: {hd_eq.mean()}")
print(f"Std list: {hd_eq.std()}")
print(f"Mean: {hd_eq.unstack().mean()}")
print(f"Std: {hd_eq.unstack().std()}")
bins = np.arange(160, 240, 2)
plt.hist(hd_eq.Herbivores.unstack(), bins=bins, fc='b', histtype='stepfilled', alpha=0.4)
plt.show()