-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirus_simul.py
149 lines (96 loc) · 3.12 KB
/
virus_simul.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
#!/usr/bin/python
#Virus pop simul
import numpy
import random
import pylab
class NoChildException(Exception):
class SimpleVirus(object):
"""
Representation of a simple virus (does not model drug effects/resistance).
"""
def __init__(self, maxBirthProb, clearProb):
"""
maxBirthProb: Maximum reproduction probability (a float between 0-1)
clearProb: Maximum clearance probability (a float between 0-1).
"""
self.maxBirthProb=maxBirthProb
self.clearProb=clearProb
def doesClear(self):
if (random.random()<=self.clearProb):
return True
return False
def reproduce(self, popDensity):
if(random.random()>=self.maxBirthProb * (1 - popDensity)):
raise NoChildException
newVirus=SimpleVirus(self.maxBirthProb,self.clearProb)
return newVirus
class SimplePatient(object):
"""
Patient model
"""
def __init__(self, viruses, maxPop):
"""
viruses: list of virus instances
maxPop: int
"""
self.viruses=viruses
self.maxPop=maxPop
def getTotalPop(self):
return len(self.viruses)
def update(self):
"""
returns viruses pop count
"""
virusList=self.viruses[:]
for virus in virusList:
if(virus.doesClear()):
self.viruses.remove(virus)
if(not self.getTotalPop()):
return 0
popDensity=float(self.getTotalPop())/self.maxPop
virusList=self.viruses[:]
for virus in virusList:
try:
self.viruses.append(virus.reproduce(popDensity))
except NoChildException:
continue
return self.getTotalPop()
def simulationWithoutDrug():
initPop=100
maxPop=1000
maxBirthProb=0.1
clearProb=0.05
maxSteps=300
yValues=[]
xValues=[n for n in range(1,301,1)]
virusList=[SimpleVirus(maxBirthProb,clearProb) for i in range(initPop)]
patient=SimplePatient(virusList,maxPop)
for i in range(maxSteps):
patient.update()
yValues.append(patient.getTotalPop())
pylab.plot(xValues,yValues,'g^')
pylab.title("Virus population dynamics")
pylab.xlabel("Elapsed timeticks")
pylab.ylabel("Virus population size")
pylab.show()
def noTherapyMean():
initPop=100
maxPop=1000
maxBirthProb=0.1
clearProb=0.05
maxSteps=300
trials=100
yValues=[[] for i in range(maxSteps)]
xValues=[n for n in range(1,maxSteps+1,1)]
for i in range(trials):
virusList=[SimpleVirus(maxBirthProb,clearProb) for i in range(initPop)]
patient=SimplePatient(virusList,maxPop)
for it in range(maxSteps):
patient.update()
yValues[it].append(patient.getTotalPop())
yMeanValues=[ sum(yValues[it])/float( len(yValues[0]) ) for it in range(maxSteps) ]
pylab.plot(xValues,yMeanValues)
pylab.title("Virus population dynamics")
pylab.xlabel("Elapsed timeticks")
pylab.ylabel("Virus population size")
pylab.show()