This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
/
migrateexample.py
executable file
·168 lines (143 loc) · 5.53 KB
/
migrateexample.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
#!/usr/bin/python3
import lisreadpy as lr
import numpy as np
import pandas as pd
from siteibm import WESA
import time
def main():
'''Simple model with only one bird'''
a = lr.lisreadpy('siteibm.ctl')
if a['print_out']:
outfile = open(a['outfile_name'], 'w')
print("Bird\ttime\tfuel\tsite\tgroup\tLoS", file=outfile)
bird = WESA('A', **a)
print(bird.get_variable('_fuel'))
print(bird.get_variable('_time'))
bird.migrate(outfile,bird_id = 1, **a)
if a['print_out']:
outfile.close()
def sim_migration():
'''Simulate movement through a single site'''
variables = lr.lisreadpy('siteibm.ctl', False)
if variables['print_out']:
outfile = open(variables['outfile_name'], 'w')
print("Bird\ttime\tsite\tgroup \
\tLoS\tPrSkip\tGroupSize\tArrival\tWinterRes", file=outfile) #\tfuel
birds = []
i = 0
output = []
groups = ['A', 'B']
for j in groups:
i = 0
print(j)
variables['Proportion_B'] = 1 - variables['Proportion_A']
while i < variables['totalbirds'] * variables['Proportion_{}'.format(j)]:
bird = WESA(j, i, **variables)
bird.migrate(outfile, bird_id = i, **variables)
if i % 1000 == 0:
print("Bird", i," in group", j)
i += 1
if variables['print_out']:
outfile.close()
print('ALLL Done!')
# import rpy2.robjects as ro
# r = ro.r
# r.source('histogram.r')
def sim_migration_w_WinteringBirds(variables):
'''Simulate movement through a single site'''
# variables = lr.lisreadpy('siteibm.ctl', False)
if variables['print_out']:
outfile = open(variables['outfile_name'], 'w')
print("Bird\ttime\tsite\tgroup \
\tLoS\tPrSkip\tGroupSize\tArrival\tWinterRes", file=outfile) #\tfuel
birds = []
i = 0
output = []
groups = ['A', 'B']
variables['Proportion_B'] = 1 - variables['Proportion_A']
for j in groups:
i = 0
print(variables['Proportion_{}'.format(j)])
numberOfmigratingBirds = variables['Proportion_{}'.format(j)] * (1-variables['proportionWinterResidents']) * variables['totalbirds']
while i < numberOfmigratingBirds:
bird = WESA(j, i, winteringBird = False, **variables)
bird.migrate(outfile, bird_id = i, **variables)
if i % 1000 == 0:
print("Bird", i," in group", j)
i += 1
while i >= numberOfmigratingBirds and i < variables['totalbirds'] * variables['Proportion_{}'.format(j)]:
bird = WESA(j, i, winteringBird = True, **variables)
bird.migrate(outfile, bird_id = i, **variables)
if i % 1000 == 0:
print("Wintering Bird", i," in group", j)
i += 1
if variables['print_out']:
outfile.close()
print('ALLL Done!')
# import rpy2.robjects as ro
# r = ro.r
# r.source('histogram.r')
def sim_migration_by_time():
"""Simulate as above, but step through all birds each step"""
timeA = time.time()
variables = lr.lisreadpy('siteibm.ctl', False)
columns = ['bird_id', 'Time', 'Site']
# Initialize Birds
totalnumberofBirds = variables['{}_numbirds'.format('A')] +\
variables['{}_numbirds'.format('A')]
birds = [None] * totalnumberofBirds
i = 0
# output = []
def step_through_birds(focal_bird, t , variables = variables):
conditions = (t >= focal_bird.true_arrivaldate
and focal_bird._dead is False)
if conditions:
focal_bird.migrate_step(t, **variables)
else:
pass
groups = ['A', 'B']
timeB = time.time()
numberA = variables['{}_numbirds'.format('A')]
birds = [WESA(groups[j], (i + j * numberA) , **variables) for j in range(len(groups)) for i in range(variables['{}_numbirds'.format(groups[j])]) ]
timeC = time.time()
t = variables['baseline_arrival_date']
while t < variables['finalDate']:
[step_through_birds(focal_bird, t, variables) for focal_bird in birds]
t += 1
timeD = time.time()
bird_id_ = 0
dates_final = []
sites_final = []
outfile = open('counts2.csv', 'w')
print('time\tsite\tgroup\tBird', file = outfile)
def append_results(focal_bird, outfile = outfile):
dates, site_hist, group = focal_bird.migrate_step(time = t, **variables)
idout = [focal_bird.ident] * len(dates)
group_out = [group] * len(dates)
output = [ dates, site_hist, group_out, idout ]
def print_fun(list_i, outfile):
# print(list_i)
[print(item, end = "\t", file = outfile) for item in list_i]
print('', file = outfile)
for i in range(len(output[0])):
list_i = [sublist[i] for sublist in output ]
print_fun(list_i, outfile)
output = [append_results(focal_bird) for focal_bird in birds]
outfile.close()
timeE = time.time()
timeF = time.time()
print(timeB - timeA, timeC - timeB, timeD - timeC, timeE - timeD, timeF - timeE)
if __name__ == '__main__':
import time
import numpy as np
np.random.seed(911)
variables = lr.lisreadpy('siteibm.ctl', False)
time1 = time.time()
# sim_migration()
sim_migration_w_WinteringBirds()
time2 = time.time()
print('Model by individual took ', time2 - time1, " seconds")
# time2 = time.time()
# sim_migration_by_time()
# time3 = time.time()
# print('Model by timestep took ', time3 - time2, " seconds")