-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_pathogen_pred.py
96 lines (70 loc) · 3.81 KB
/
model_pathogen_pred.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
from pcraster import *
from pcraster.framework import *
import numpy as np
class PredPreyModel(DynamicModel):
def __init__(self):
DynamicModel.__init__(self)
# set clone directly:
# size 200x200, cell size 1, west coord: 0, north coord: 200
setclone(200, 200, 1, 0, 200)
def initial(self):
# create maps with that percentage of total pred/prey present
self.prey = uniform(1) < percPrey
pred = uniform(1) < percPred
# infect some of the predators
infPred = pcrand(uniform(1) < percInf, pred)
# create a nominal predators map, consisting of infected predators (with the value of 2),
# sound predators (1), and empty cells (0)
self.pred = ifthenelse(infPred, nominal(2), ifthenelse(pred, nominal(1), nominal(0)))
self.report(self.prey, 'outputInfectedPred/preyInit')
self.report(self.pred, 'outputInfectedPred/predInit')
def dynamic(self):
# find locations of all predators, infect neighbours, define the sound ones
allPred = self.pred != 0
infPred = pcrand(window4total(scalar(self.pred == 2)) >=1, allPred)
soundPred = pcrand(allPred, pcrnot(infPred))
# find locations where infected or sound predators catch prey
infEats = pcrand(infPred, self.prey)
soundEats = pcrand(soundPred, self.prey)
# find all locations with both predator and prey
both = pcror(infEats, soundEats)
self.report(both, 'outputInfectedPred/both')
# predators reproduce at these locations: sound ones in own cell and neighbourhood,
# infected ones only in own cell
reprSoundPred = (window4total(scalar(soundEats)) + scalar(soundEats)) >= 1
reprInfPred = (scalar(infEats) >= 1)
# save and report the nominal map
self.pred = ifthenelse(reprInfPred, nominal(2), ifthenelse(reprSoundPred, nominal(1), nominal(0)))
self.report(self.pred, 'outputInfectedPred/pred')
# find all surviving prey
survive = pcrand(self.prey, ~both)
# all survivors reproduce
self.prey = (window4total(scalar(survive)) + scalar(survive)) >= 1
self.report(self.prey, 'outputInfectedPred/prey')
#define number of Time Steps
nrOfTimeSteps=100
#define proportion (prey and predator) step size
propstepSize=0.1
# Open file to save results
f= open("DataInfectedPredator_propstep_"+str(propstepSize)+".txt","w+")
f.write("PercPrey-Ini,PercPred-Ini,PercPredInf-Ini,PercPredSound,PercPrey-Final,PercPredInf-Final,PercPredSound-Final"+"\n")
# set percentage of infected predators
# set percentages for prey and predator populations
for percInf in np.arange(0,1.1,propstepSize):
for percPrey in np.arange(0,1,propstepSize):
for percPred in np.arange(0,1,propstepSize):
myModel = PredPreyModel()
dynamicModel = DynamicFramework(myModel,nrOfTimeSteps)
dynamicModel.run()
# extract information from the map
preyEq = readmap("outputInfectedPred/prey0000."+str(nrOfTimeSteps))
predEq = readmap("outputInfectedPred/pred0000."+str(nrOfTimeSteps))
# compute calculations
PercPreyFinal= maptotal(scalar(preyEq==1))/(200*200)
PercInfectedPredFinal= maptotal(scalar(predEq==2))/(200*200)
PercSoundPredFinal= maptotal(scalar(predEq==1))/(200*200)
# Writting individial results into the file - Initial and final conditions are saved
f.write(str(float(percPrey)) + "," + str(float(percPred)) + "," + str(float(percPred*percInf)) + "," +str(float(percPred-percPred*percInf))+ ","+ str(float(PercPreyFinal)) + "," + str(float(PercInfectedPredFinal))+ "," + str(float(PercSoundPredFinal))+"\n")
print(str(float(percPrey)) + "," + str(float(percPred)) + "," + str(float(percPred*percInf)) + "," +str(float(percPred-percPred*percInf))+ ","+ str(float(PercPreyFinal)) + "," + str(float(PercInfectedPredFinal))+ "," + str(float(PercSoundPredFinal))+"\n")
#close file
f.close()