forked from czbiohub-sf/random-walk-epidemic-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.jl
executable file
·115 lines (98 loc) · 3.62 KB
/
utils.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
using Distributions
using Random
using XLSX
function moore_neighbors(site)
[
[site[1] + 1, site[2]],
[site[1] + 1, site[2] + 1],
[site[1] - 1, site[2] + 1],
[site[1] - 1, site[2]-1],
[site[1] - 1, site[2]],
[site[1], site[2] + 1],
[site[1], site[2] - 1],
[site[1] + 1, site[2] - 1]
]
end
function probinfected(x, p1, p2)
if x <= 0
return p1
else
return p2
end
end
function fill_lattice_data(sheet, arr, savetimes)
#=
We have four dimensions in the removed array:
data[1][2][3][4]
1: the iteration in n_iter
2: the timestep in savetimes
3: the site in the data (not in any particular order except for how it was added chronologically)
4: the element in the data, e.g. for an infected site there are 4 pieces of data
[the x coor, y coor, lifetime, and # of ppl inf or something]
for a removed site data, its just two pieces of data, [x coor, y coor]
=#
dim1 = size(arr); dim1 = dim1[1] # This number should be constant
dim2 = size(arr[1]); dim2 = dim2[1] # This number should be constant
# dim 3 is not constant - the number of sites in each realization or timestep is not fixed
# dim 4 is not necessary for our purposes - we just join the array elements into a single string
for i in 1:dim1
for j in 1:dim2
# calculate the value of dim3 since it changes each time
dim3 = size(arr[i][j]); dim3 = dim3[1]
XLSX.setdata!(sheet, XLSX.CellRef( 1, (dim2+1)*(i-1) + j ), savetimes[j])
for k in 1:dim3
sitedata = arr[i][j][k]
sitedata = join(sitedata, ",")
XLSX.setdata!(sheet, XLSX.CellRef( 1 + k, (dim2+1)*(i-1) + j ), sitedata)
end
end
end
end
function simpler_jump(x, y, a = 1)
theta = rand(Uniform(0, 2 * π))
u = rand(Uniform(0, 1))
r = a/(3 * u)^(1/3)
xfl = r * sin(theta)
yfl = r * cos(theta)
(floor(Int, x + xfl + 1/2),
floor(Int, y + yfl + 1/2), r)
end
function get_cluster_points(start_point, removed)
neighboringremoved = true
cluster = Set([start_point])
cluster_added_old = cluster
cluster_added_new = Set()
neighbors_visited = Set()
#cluster_added_old contains the new sites added to the central cluster in the previous time step
#cluster_added_new contains the new sites which are currently being added in a given time step
#each loop, we check the neighbors of cluster_added_old
#if cluster_added_new is empty, we end the while loop
while neighboringremoved
for site in cluster_added_old
neighbor_sites = moore_neighbors(site)
for neighbor_site in neighbor_sites
if (neighbor_site in removed) && !(neighbor_site in neighbors_visited)
push!(cluster, neighbor_site)
push!(cluster_added_new, neighbor_site)
push!(neighbors_visited, neighbor_site)
end
end
end
if isempty(cluster_added_new) # we have reached limit of central cluster
neighboringremoved = false
end
cluster_added_old = cluster_added_new
cluster_added_new = Set()
end
cluster
end
function surrounded_by_removed(site, removed)
[site[1]+1, site[2]] in removed &&
[site[1]+1, site[2]+1] in removed &&
[site[1], site[2]+1] in removed &&
[site[1]-1, site[2]+1] in removed &&
[site[1]-1, site[2]] in removed &&
[site[1]-1, site[2]-1] in removed &&
[site[1], site[2]-1] in removed &&
[site[1]+1, site[2]-1] in removed
end