This repository has been archived by the owner on Feb 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ep-main.py
executable file
·280 lines (269 loc) · 11.6 KB
/
ep-main.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
275
276
277
278
279
280
#!/usr/bin/python
# encoding: utf-8
import networkx as nx
import numpy as np
import scipy as sp
import getopt
import sys
import os
def GetProd(graph):
bm = 0
nprod = 0
bmprod = 0
ncons = 0
bmcons = 0
for sp in graph:
bm += sp.n
if graph.out_degree(sp) == 0:
bmprod += sp.n
nprod += 1
else:
bmcons += sp.n
ncons += 1
return [len(graph),bm,bm/float(len(graph)),bmprod/float(nprod),bmcons/float(ncons)]
class species:
def __init__(self,uname,n):
self.id = uname
self.n = n
self.dn = 0
self.tl = 1
self.pos = (0,0)
self.s1 = 0
self.s2 = 0
self.s3 = 0
self.s4 = 0
self.s5 = 0
def UpdateTrophicLevel(graph):
for n in graph:
if graph.out_degree(n) == 0:
## Primary producer
for alt in graph:
if graph.out_degree(alt) > 0:
if nx.has_path(graph, alt, n):
TL = nx.shortest_path_length(graph, alt, n) + 1
if (alt.tl == 1) or (alt.tl > TL):
alt.tl = TL
return 0
def DiGraphFromList(fname):
G = nx.DiGraph()
Dat = np.loadtxt(fname)
# We loop through the file and record the species names
SpNames = []
for Line in Dat:
SpNames.append(str(int(Line[0])))
SpNames.append(str(int(Line[1])))
# We keep the unique species
UniqueSp = list(set(SpNames))
# Each unique species becomes a node in the graph
SpObj = []
for Sp in UniqueSp:
SpObj.append(species(Sp,np.random.random()))
G.add_nodes_from(SpObj)
# We loop through the link list
for Line in Dat:
Pred = str(int(Line[0]))
Prey = str(int(Line[1]))
for sp in G:
if sp.id == Pred:
PredObj = sp
if sp.id == Prey:
PreyObj = sp
G.add_edge(PredObj, PreyObj)
UpdateTrophicLevel(G)
return G
def SimulWeb(graph,p):
Record = [0,0,0,0,0]
nRecord = 0
for t in xrange(p['timesteps']):
for sp in graph:
if graph.out_degree(sp) == 0:
## Primary producer
sp.dn += sp.n * (1 - sp.n / float(p['K']))
## Find all of its predators
for pred in graph.pred[sp]:
degPred = graph.out_degree(pred)
Omega = 1 / float(degPred)
preyBiomass = 0
## Get the prey relative consumption for all preys of this predator
for prey_of_pred in graph.succ[pred]:
preyBiomass += Omega * np.power(prey_of_pred.n, p['h'])
Fij = (Omega * np.power(sp.n,p['h'])) / float( (np.power(p['Bo'],p['h']) + p['c'] * pred.n * np.power(p['Bo'],p['h'])) + preyBiomass )
Mc = np.power(p['Z'], pred.tl)
Mp = np.power(p['Z'], sp.tl)
if Mp == 0:
MRatio = 1
else:
MRatio = Mc/float(Mp)
sp.dn -= (p['xi'] * np.power( MRatio , p['scexpo']) * p['y'] * pred.n * Fij) / float(p['eij'])
else:
## If not a primary producer
Mc = np.power(p['Z'], sp.tl)
Mp = np.power(p['Z'], 1)
if Mp == 0:
MRatio = 1
else:
MRatio = Mc/float(Mp)
sp.dn -= p['xi'] * np.power( MRatio , p['scexpo']) * sp.n
if graph.in_degree(sp) > 0:
## Find all its predators
for pred in graph.pred[sp]:
degPred = graph.out_degree(pred)
Omega = 1 / float(degPred)
preyBiomass = 0
## Get the prey relative consumption for all preys of this predator
for prey_of_pred in graph.succ[pred]:
preyBiomass += Omega * np.power(prey_of_pred.n, p['h'])
Fij = (Omega * np.power(sp.n,p['h'])) / float( (np.power(p['Bo'],p['h']) + p['c'] * pred.n * np.power(p['Bo'],p['h'])) + preyBiomass )
Mc = np.power(p['Z'], pred.tl)
Mp = np.power(p['Z'], sp.tl)
if Mp == 0:
MRatio = 1
else:
MRatio = Mc/float(Mp)
sp.dn -= (p['xi'] * np.power( MRatio, p['scexpo']) * p['y'] * pred.n * Fij) / float(p['eij'])
## Find all of its preys
Omega = 1 / float(graph.out_degree(sp))
## Get the prey relative consumption for all preys of this predator
preyBiomass = 0
for prey_of_pred in graph.succ[sp]:
preyBiomass += Omega * np.power(prey_of_pred.n, p['h'])
for prey_of_pred in graph.succ[sp]:
Fij = (Omega * np.power(prey_of_pred.n,p['h'])) / float( (np.power(p['Bo'],p['h']) + p['c'] * sp.n * np.power(p['Bo'],p['h'])) + preyBiomass )
Mc = np.power(p['Z'], prey_of_pred.tl)
Mp = np.power(p['Z'], sp.tl)
if Mp == 0:
MRatio = 1
else:
MRatio = Mc/float(Mp)
sp.dn += (p['ax'] / float(p['ar'])) * np.power( MRatio, p['scexpo']) * p['y'] * sp.n * Fij
for n in graph:
n.n += n.dn * p['SCALAR']
n.dn = 0
if (p['timesteps']-t) <= p['record']:
tRecord = GetProd(graph)
nRecord += 1
for i in xrange(len(tRecord)):
Record[i] += tRecord[i]
for i in xrange(len(tRecord)):
Record[i] = Record[i] / float(nRecord)
return Record
def MotifCount(graph):
# Number of each motif, third order, simple and double link
s1 = 0
s2 = 0
s3 = 0
s4 = 0
s5 = 0
d1 = 0
d2 = 0
d3 = 0
d4 = 0
d5 = 0
d6 = 0
d7 = 0
d8 = 0
for n1 in graph:
for n2 in graph:
for n3 in graph:
#### Simple linkage motifs
# Motif S1 : Linear food chain,
if (graph.has_edge(n1,n2) and (not graph.has_edge(n2,n1))) and (graph.has_edge(n2,n3) and (not graph.has_edge(n3,n2))) and (not graph.has_edge(n3,n1)) and (not graph.has_edge(n1,n3)):
s1 += 1
n1.s1 += 1
n2.s1 += 1
n3.s1 += 1
# Motif S2 : Omnivory
if (graph.has_edge(n1,n2) and (not graph.has_edge(n2,n1))) and (graph.has_edge(n2,n3) and (not graph.has_edge(n3,n2))) and (not graph.has_edge(n3,n1)) and (graph.has_edge(n1,n3)):
s2 += 1
n1.s2 += 1
n2.s2 += 1
n3.s2 += 1
# Motif S3 : Directed loop
if (graph.has_edge(n1,n2) and (not graph.has_edge(n2,n1))) and (graph.has_edge(n2,n3) and (not graph.has_edge(n3,n2))) and (graph.has_edge(n3,n1)) and (not graph.has_edge(n1,n3)):
s3 += 1
n1.s3 += 1
n2.s3 += 1
n3.s3 += 1
# Motif S4 : Exploitative competition
if (graph.has_edge(n1,n2) and (not graph.has_edge(n2,n1))) and (graph.has_edge(n3,n2) and (not graph.has_edge(n2,n3))) and (not graph.has_edge(n3,n1)) and (not graph.has_edge(n1,n3)):
s4 += 1
n1.s4 += 1
n2.s4 += 1
n3.s4 += 1
# Motif S5 : Apparent competition
if (graph.has_edge(n1,n2) and (not graph.has_edge(n2,n1))) and (graph.has_edge(n1,n3) and (not graph.has_edge(n3,n1))) and (not graph.has_edge(n3,n2)) and (not graph.has_edge(n2,n3)):
s5 += 1
n1.s5 += 1
n2.s5 += 1
n3.s5 += 1
#### Double linkage motifs
# Motif D1
if (graph.has_edge(n1,n2) and graph.has_edge(n2,n1)) and (graph.has_edge(n1,n3) and (not graph.has_edge(n3,n1))) and (graph.has_edge(n2,n3) and (not graph.has_edge(n3,n2))):
d1 += 1
# Motif D2
if (graph.has_edge(n1,n2) and graph.has_edge(n1,n3)) and (graph.has_edge(n2,n3) and graph.has_edge(n3,n2)) and (not graph.has_edge(n2,n1) and (not graph.has_edge(n3,n1))):
d2 += 1
# Motif D3
if (graph.has_edge(n1,n2) and graph.has_edge(n2,n1)) and (graph.has_edge(n1,n3) and (not graph.has_edge(n3,n1))) and (not graph.has_edge(n2,n3) and (not graph.has_edge(n3,n2))):
d3 += 1
# Motif D4
if (graph.has_edge(n1,n2) and (not graph.has_edge(n1,n3))) and (graph.has_edge(n2,n3) and graph.has_edge(n3,n2)) and (not graph.has_edge(n2,n1) and (not graph.has_edge(n3,n1))):
d4 += 1
# Motif D5
if (graph.has_edge(n1,n2) and (graph.has_edge(n2,n1))) and (graph.has_edge(n2,n3) and (not graph.has_edge(n3,n2))) and (graph.has_edge(n3,n1)) and (not graph.has_edge(n1,n3)):
d5 += 1
# Motif D6
if (graph.has_edge(n1,n2) and graph.has_edge(n2,n1)) and (graph.has_edge(n1,n3) and graph.has_edge(n3,n1)) and (graph.has_edge(n3,n2) and graph.has_edge(n2,n3)):
d6 += 1
# Motif D7
if (graph.has_edge(n1,n2) and (not graph.has_edge(n2,n1))) and (graph.has_edge(n1,n3) and graph.has_edge(n3,n1)) and (graph.has_edge(n3,n2) and graph.has_edge(n2,n3)):
d7 += 1
# Motif D8
if ((not graph.has_edge(n1,n2)) and (not graph.has_edge(n2,n1))) and (graph.has_edge(n1,n3) and graph.has_edge(n3,n1)) and (graph.has_edge(n3,n2) and graph.has_edge(n2,n3)):
d8 += 1
return [s1,s2,s3,s4,s5,d1,d2,d3,d4,d5,d6,d7,d8]
## Analysis
def main(argv=None):
## PARAMS
p = {'y':4.0, 'eij':0.85, 'K':1.0, 'r':1.0, 'ar':1.0, 'ax':0.88, 'xi':0.88, 'scexpo':-0.25, 'Z':2.0, 'c':1.0, 'h':1.0, 'Bo':0.5, 'SCALAR':0.02, 'timesteps':10000, 'record':1000}
webID = 1
repltodo = 5
## FIX OPTIONS
opts, args = getopt.getopt(sys.argv[1:], "h", ["w=","t=","rec=","repl="])
for o in opts:
if o[0] in ('--w'):
webID = int(o[1])
elif o[0] in ('--t'):
p['timesteps'] = int(o[1])
elif o[0] in ('--rec'):
p['record'] = int(o[1])
elif o[0] in ('--repl'):
repltodo = int(o[1])
##
fname= 'webs/EP'+str(webID)+'.txt'
Gr = DiGraphFromList(fname)
Motifs = MotifCount(Gr)
Motifs = map(str,Motifs)
for repl in xrange(repltodo):
for Z in [0.0]:
p['Z'] = Z
for n in Gr:
n.n = np.random.uniform(low=0.05, high=1.0, size=1)[0]
print "Web "+str(webID)+", Z = "+str(p['Z'])+", replicate "+str(repl+1)
Out = SimulWeb(Gr, p)
## Write the web-wide results
Fname = 'output/EP'+str(webID)+'-WEB.dat'
f = open(Fname, 'a')
Out = map(str,Out)
f.write(str(webID)+' ')
f.write(str(repl+1)+' ')
f.write(str(p['Z'])+' ')
f.write(str(len(Gr.edges()))+' ')
for motif in Motifs:
f.write('{0} '.format(motif))
for record in Out:
f.write('{0} '.format(record))
f.write('\n')
f.close()
if __name__ == "__main__":
main()