-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlyphosateGrowthRate.jl
175 lines (140 loc) · 4.47 KB
/
GlyphosateGrowthRate.jl
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
# packages
using BioEnergeticFoodWebs , EcologicalNetworks , JLD2 , Statistics , Plots , CSV , DataFrames , Random
# random seed
Random.seed!(21)
# vector of growth rate ,r, (-50%, -20%, -10%, 0, +5%, +20%)
r = [0.50, 0.80, 0.90, 1.0, 1.05, 1.20]
# vector of carrying capacity
k = [0.50, 0.80, 0.90, 1.0]
# set repititions and DataFrame
reps = 5
df = DataFrame(r = [], K = [], network = [], diversity = [], stability = [], biomass = [], biomass2 = [], time = [])
# make while loop
begin
# list to store networks
global networks = []
# monitoring variable
global l = length(networks)
# while loop
while l < reps
# generate network
A_bool = EcologicalNetworks.nichemodel(20,0.15)
# convert the UN object into a matrix of 1s and 0s
Ad = adjacency(A_bool)
A = Int.(Ad)
# calculate connectance
co = sum(A)/(size(A,1)^2)
# ensure that connectance = 0.15
if co == 0.15
push!(networks, A)
# save network if co = 0.15
end
global l = length(networks)
end
end
# loop over networks
for h in 1:reps
A = networks[h]
# loop over r
for i in 1:length(r)
# loop over K
for j in 1:length(k)
# create model parameters
p = model_parameters(A, r = [r[i]], K = [k[j]], productivity=:competitive)
# assign biomass
bm = rand(size(A,1))
# simulate
out = simulate(p, bm, start=0, stop=2000)
# calculate output metrics
diversity = foodweb_evenness(out, last = 1000)
stability = population_stability(out, last = 1000)
biomass = total_biomass(out, last = 1000)
biomass2 = out[:B]
time = out[:t]
# dummy naming variables
r_num = r[i]
K_num = k[j]
# push to df
push!(df, [r[i], k[j], h, diversity, stability, biomass, biomass2, time])
# print some stuff
println(("r=$r_num", "network = $h"))
end
end
end
# see output
df
first(df,6)
last(df,6)
# make plot
Plots.plot(out[:t], out[:B], legend = true, ylabel = "Biomass", xlabel = "Time")
Plots.plot(r, biomass, legends = true, ylabel = "Biomass", xlabel = "growth rate")
# make plot 2: Biomass vs Growth Rate
# initialise empty plot
pl = Plots.plot([NaN], [NaN],
label = "",
ylims = (0,1.1),
xlims = (0, 1.5),
leg = :bottomright,
foreground_colour_legend = nothing,
xlabel = "Growth Rate",
ylabel = "Biomass",
title = "How biomass changes at different growth rates")
# marker and line type
shp = [:circle]
ls = [:solid]
for (i, r) in enumerate(r)
# subset
tmp = df[df.r .== r, :]
# remove NaN values
tmp = tmp[.!(isnan.(tmp.biomass)), :]
# calculate mean across reps
gdf = groupby(tmp, :r)
meandf = combine(gdf, :biomass => mean)
# command to avoid printing legends multiple times
l = i == 1 ? lbl[i] : ""
# add to pl
plot!(pl, log10.(meandf.r), meandf.biomass_mean,
mc = :white,
msw = 3,
markershape = shp[i],
linestyle = ls[i],
lw = 2,
seriestype = [:line :scatter])
end
# CODE BELOW HERE NOT USING AT THE MOMENT
# initialise empty plot
pl = Plots.plot([NaN], [NaN],
label = "",
ylims = (0,1.1),
xlims = (0, 2000),
leg = :bottomright,
foreground_colour_legend = nothing,
xlabel = "Time",
ylabel = "Biomass",
title = "How biomass changes at different growth rates")
# set colours (red, orange, yellow, blue, light blue, green)
clr = [RGB(200/255, 0/255, 0/255), RGB(230/255, 140/255, 22/255), RGB(210/255, 220/255, 65/255), RGB(0/255, 0/255, 255/255), RGB(22/255, 223/255, 230/255), RGB(0/255, 205/255, 0/255)]
# set legend labels
lbl = ["-50%","-20%", "-10%", "0%", "+5%", "+20%"]
for (i, r) in enumerate(r)
# subset
tmp = df[df.α .== α, :]
# remove NaN values
tmp = tmp[.!(isnan.(tmp.diversity)), :]
# calculate mean across reps
gdf = groupby(tmp, :K)
meandf = combine(gdf, :diversity => mean)
# command to avoid printing legends multiple times
l = i == 1 ? lbl[i] : ""
# add to pl
plot!(pl, log10.(meandf.K), meandf.diversity_mean,
msc = clr[i],
mc = :white,
msw = 3,
markershape = shp[i],
linestyle = ls[i],
lc = clr[i],
lw = 2,
label = lbl[i],
seriestype = [:line :scatter])
end