-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcovid19abm.jl
1599 lines (1410 loc) · 58.1 KB
/
covid19abm.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module covid19abm
using Parameters, Distributions, StatsBase, StaticArrays, Random, Match, DataFrames
@enum HEALTH SUS LAT PRE ASYMP MILD MISO INF IISO HOS ICU REC DED LAT2 PRE2 ASYMP2 MILD2 MISO2 INF2 IISO2 HOS2 ICU2 REC2 DED2 UNDEF
Base.@kwdef mutable struct Human
idx::Int64 = 0
health::HEALTH = SUS
swap::HEALTH = UNDEF
sickfrom::HEALTH = UNDEF
wentTo::HEALTH = UNDEF
sickby::Int64 = -1
nextday_meetcnt::Int16 = 0 ## how many contacts for a single day
age::Int16 = 0 # in years. don't really need this but left it incase needed later
ag::Int16 = 0
tis::Int16 = 0 # time in state
exp::Int16 = 0 # max statetime
dur::NTuple{4, Int8} = (0, 0, 0, 0) # Order: (latents, asymps, pres, infs) TURN TO NAMED TUPS LATER
doi::Int16 = 999 # day of infection.
iso::Bool = false ## isolated (limited contacts)
isovia::Symbol = :null ## isolated via quarantine (:qu), preiso (:pi), intervention measure (:im), or contact tracing (:ct)
tracing::Bool = false ## are we tracing contacts for this individual?
tracestart::Int16 = -1 ## when to start tracing, based on values sampled for x.dur
traceend::Int16 = -1 ## when to end tracing
tracedby::UInt32 = 0 ## is the individual traced? property represents the index of the infectious person
tracedxp::Int16 = 0 ## the trace is killed after tracedxp amount of days
comorbidity::Int8 = 0 ##does the individual has any comorbidity?
vac_status::Int8 = 0 ##
vac_ef_symp::Float16 = 0.0
vac_ef_inf::Float16 = 0.0
vac_ef_sev::Float16 = 0.0
index_day::Int16 = 1 ## this index is used to account for the change in vaccine efficacy
got_inf::Bool = false
herd_im::Bool = false
hospicu::Int8 = -1
ag_new::Int16 = -1
hcw::Bool = false
days_vac::Int64 = -1
vac_red::Float64 = 0.0
first_one::Bool = false
strain::Int16 = -1
end
## default system parameters
@with_kw mutable struct ModelParameters @deftype Float64 ## use @with_kw from Parameters
β = 0.0345
seasonal::Bool = false ## seasonal betas or not
popsize::Int64 = 10000
prov::Symbol = :usa
calibration::Bool = false
calibration2::Bool = false
heatmap::Bool = false
ignore_cal::Bool = false
start_several_inf::Bool = false
modeltime::Int64 = 500
initialinf::Int64 = 1
initialhi::Int64 = 0 ## initial herd immunity, inserts number of REC individuals
τmild::Int64 = 0 ## days before they self-isolate for mild cases
fmild::Float64 = 0.0 ## percent of people practice self-isolation
fsevere::Float64 = 0.0 #
eldq::Float64 = 0.0 ## complete isolation of elderly
eldqag::Int8 = 5 ## default age group, if quarantined(isolated) is ag 5.
fpreiso::Float64 = 0.0 ## percent that is isolated at the presymptomatic stage
tpreiso::Int64 = 0## preiso is only turned on at this time.
frelasymp::Float64 = 0.26 ## relative transmission of asymptomatic
ctstrat::Int8 = 0 ## strategy
fctcapture::Float16 = 0.0 ## how many symptomatic people identified
fcontactst::Float16 = 0.0 ## fraction of contacts being isolated/quarantined
cidtime::Int8 = 0 ## time to identification (for CT) post symptom onset
cdaysback::Int8 = 0 ## number of days to go back and collect contacts
#vaccine_ef::Float16 = 0.0 ## change this to Float32 typemax(Float32) typemax(Float64)
apply_vac::Bool = true
apply_vac_com::Bool = true #will we focus vaccination on comorbidity?
vac_com_dec_max::Float16 = 0.0 # how much the comorbidity decreases the vac eff
vac_com_dec_min::Float16 = 0.0 # how much the comorbidity decreases the vac eff
herd::Int8 = 0 #typemax(Int32) ~ millions
set_g_cov::Bool = false ###Given proportion for coverage
cov_val::Float64 = 0.2
dont_vac_20::Bool = false
vaccinating_appendix::Bool = false
hcw_vac_comp::Float64 = 0.95
hcw_prop::Float64 = 0.05 #prop que é trabalhador da saude
comor_comp::Float64 = 0.7 #prop comorbidade tomam
eld_comp::Float64 = 0.95
young_comp::Float64 = 0.22
gen_cov::Float64 = 0.7
vac_period::Int64 = 21
daily_cov::Float64 = 0.008 ####also run for 0.008 per day
n_comor_comp::Float64 = 0.387
days_to_protection::Array{Array{Int64,1},1} = [[14],[0;14]]
vac_efficacy_inf::Array{Array{Float64,1},1} = [[0.46],[0.6;0.93]] #### 50:5:80
vac_efficacy_symp::Array{Array{Float64,1},1} = [[0.921],[0.921;0.941]] #### 50:5:80
vac_efficacy_sev::Array{Array{Float64,1},1} = [[0.802],[0.941;1.0]] #### 50:5:80
#vac_efficacy_fd::Float64 = vac_efficacy/2.0
#days_to_protection::Array{Int64,1} = [14;7]
vaccinating::Bool = false
days_before::Int64 = 0 ### six weeks of vaccination
single_dose::Bool = false
drop_rate::Float64 = 0.0
fixed_cov::Float64 = 0.5
no_cap::Bool = true
red_risk_perc::Float64 = 1.0
reduction_protection::Float64 = 0.0
fd_1::Int64 = 30 #capacidade diaria de vacinação
fd_2::Int64 = 0
sd1::Int64 = 30
sec_dose_delay::Int64 = vac_period
days_Rt::Array{Int64,1} = [100;200;300]
priority::Bool = false
sec_strain_trans::Float64 = 1.25
ins_sec_strain::Bool = false
initialinf2::Int64 = 1
max_vac_delay::Int64 = 42
min_eff = 0.02
ef_decrease_per_week = 0.05
vac_effect::Int64 = 2
end
Base.@kwdef mutable struct ct_data_collect
total_symp_id::Int64 = 0 # total symptomatic identified
totaltrace::Int64 = 0 # total contacts traced
totalisolated::Int64 = 0 # total number of people isolated
iso_sus::Int64 = 0 # total susceptible isolated
iso_lat::Int64 = 0 # total latent isolated
iso_asymp::Int64 = 0 # total asymp isolated
iso_symp::Int64 = 0 # total symp (mild, inf) isolated
end
Base.show(io::IO, ::MIME"text/plain", z::Human) = dump(z)
## constants
const humans = Array{Human}(undef, 0)
const p = ModelParameters() ## setup default parameters
const agebraks = @SVector [0:4, 5:19, 20:49, 50:64, 65:99]
const BETAS = Array{Float64, 1}(undef, 0) ## to hold betas (whether fixed or seasonal), array will get resized
const ct_data = ct_data_collect()
export ModelParameters, HEALTH, Human, humans, BETAS
function runsim(simnum, ip::ModelParameters)
# function runs the `main` function, and collects the data as dataframes.
hmatrix,hh1,hh2 = main(ip,simnum)
# get infectors counters
infectors = _count_infectors()
ct_numbers = (ct_data.total_symp_id, ct_data.totaltrace, ct_data.totalisolated,
ct_data.iso_sus, ct_data.iso_lat, ct_data.iso_asymp, ct_data.iso_symp)
###use here to create the vector of comorbidity
# get simulation age groups
#ags = [x.ag for x in humans] # store a vector of the age group distribution
ags = [x.ag_new for x in humans] # store a vector of the age group distribution
all = _collectdf(hmatrix)
spl = _splitstate(hmatrix, ags)
ag1 = _collectdf(spl[1])
ag2 = _collectdf(spl[2])
ag3 = _collectdf(spl[3])
ag4 = _collectdf(spl[4])
ag5 = _collectdf(spl[5])
ag6 = _collectdf(spl[6])
insertcols!(all, 1, :sim => simnum); insertcols!(ag1, 1, :sim => simnum); insertcols!(ag2, 1, :sim => simnum);
insertcols!(ag3, 1, :sim => simnum); insertcols!(ag4, 1, :sim => simnum); insertcols!(ag5, 1, :sim => simnum);
insertcols!(ag6, 1, :sim => simnum);
##getting info about vac, comorbidity
# vac_idx = [x.vac_status for x in humans]
#vac_ef_i = [x.vac_ef for x in humans]
# comorb_idx = [x.comorbidity for x in humans]
# ageg = [x.ag for x = humans ]
#n_vac = sum(vac_idx)
n_vac_sus1::Int64 = 0
n_vac_rec1::Int64 = 0
n_inf_vac1::Int64 = 0
n_dead_vac1::Int64 = 0
n_hosp_vac1::Int64 = 0
n_icu_vac1::Int64 = 0
n_hosp_nvac::Int64 = 0
n_dead_nvac::Int64 = 0
n_inf_nvac::Int64 = 0
n_icu_nvac::Int64 = 0
n_vac_sus2::Int64 = 0
n_vac_rec2::Int64 = 0
n_inf_vac2::Int64 = 0
n_dead_vac2::Int64 = 0
n_hosp_vac2::Int64 = 0
n_icu_vac2::Int64 = 0
n_com_vac1 = zeros(Int64,5)
n_ncom_vac1 = zeros(Int64,5)
n_com_vac2 = zeros(Int64,5)
n_ncom_vac2 = zeros(Int64,5)
n_com_total = zeros(Int64,5)
n_ncom_total = zeros(Int64,5)
for x in humans
if x.vac_status == 1
if x.herd_im
n_vac_rec1 += 1
else
n_vac_sus1 += 1
end
if x.got_inf
n_inf_vac1 += 1
end
if x.health == DED
n_dead_vac1 += 1
end
if x.hospicu == 1
n_hosp_vac1 += 1
elseif x.hospicu == 2
n_icu_vac1 += 1
end
if x.comorbidity == 1
n_com_vac1[x.ag] += 1
n_com_total[x.ag] += 1
else
n_ncom_vac1[x.ag] += 1
n_ncom_total[x.ag] += 1
end
elseif x.vac_status == 2
if x.herd_im
n_vac_rec2 += 1
else
n_vac_sus2 += 1
end
if x.got_inf
n_inf_vac2 += 1
end
if x.health == DED
n_dead_vac2 += 1
end
if x.hospicu == 1
n_hosp_vac2 += 1
elseif x.hospicu == 2
n_icu_vac2 += 1
end
if x.comorbidity == 1
n_com_vac2[x.ag] += 1
n_com_total[x.ag] += 1
else
n_ncom_vac2[x.ag] += 1
n_ncom_total[x.ag] += 1
end
else
if x.got_inf
n_inf_nvac += 1
end
if x.health == DED
n_dead_nvac += 1
end
if x.hospicu == 1
n_hosp_nvac += 1
elseif x.hospicu == 2
n_icu_nvac += 1
end
if x.comorbidity == 1
n_com_total[x.ag] += 1
else
n_ncom_total[x.ag] += 1
end
end
end
R01 = zeros(Float64,size(hh1,1))
for i = 1:size(hh1,1)
if length(hh1[i]) > 0
R01[i] = length(findall(k -> k.sickby in hh1[i],humans))/length(hh1[i])
end
end
R02 = zeros(Float64,size(hh2,1))
for i = 1:size(hh2,1)
if length(hh2[i]) > 0
R02[i] = length(findall(k -> k.sickby in hh2[i],humans))/length(hh2[i])
end
end
#return (a=all, g1=ag1, g2=ag2, g3=ag3, g4=ag4, g5=ag5, infectors=infectors, vi = vac_idx,ve=vac_ef_i,com = comorb_idx,n_vac = n_vac,n_inf_vac = n_inf_vac,n_inf_nvac = n_inf_nvac)
return (a=all, g1=ag1, g2=ag2, g3=ag3, g4=ag4, g5=ag5,g6=ag6, infectors=infectors,
com_v1 = n_com_vac1,ncom_v1 = n_ncom_vac1,
com_v2 = n_com_vac2,ncom_v2 = n_ncom_vac2,
com_t=n_com_total,ncom_t=n_ncom_total,
n_vac_sus1 = n_vac_sus1,
n_vac_rec1 = n_vac_rec1,
n_inf_vac1 = n_inf_vac1,
n_dead_vac1 = n_dead_vac1,
n_hosp_vac1 = n_hosp_vac1,
n_icu_vac1 = n_icu_vac1,
n_vac_sus2 = n_vac_sus2,
n_vac_rec2 = n_vac_rec2,
n_inf_vac2 = n_inf_vac2,
n_dead_vac2 = n_dead_vac2,
n_hosp_vac2 = n_hosp_vac2,
n_icu_vac2 = n_icu_vac2,
n_inf_nvac = n_inf_nvac,
n_dead_nvac = n_dead_nvac,
n_hosp_nvac = n_hosp_nvac,
n_icu_nvac = n_icu_nvac,
iniiso = ct_data.totalisolated,
R01 = R01,
R02 = R02)
end
export runsim
function main(ip::ModelParameters,sim::Int64)
Random.seed!(sim*726)
## datacollection
# matrix to collect model state for every time step
# reset the parameters for the simulation scenario
reset_params(ip) #logic: outside "ip" parameters are copied to internal "p" which is a global const and available everywhere.
p.popsize == 0 && error("no population size given")
hmatrix = zeros(Int16, p.popsize, p.modeltime)
initialize() # initialize population
#h_init::Int64 = 0
# insert initial infected agents into the model
# and setup the right swap function.
if p.calibration && !p.start_several_inf
insert_infected(PRE, p.initialinf, 4,1)[1]
#insert_infected(REC, p.initialhi, 4)
elseif p.start_several_inf
N = herd_immu_dist_4(sim,1)
insert_infected(PRE, p.initialinf, 4, 1)[1]
#findall(x->x.health in (MILD,INF,LAT,PRE,ASYMP),humans)
else
#applying_vac(sim)
herd_immu_dist_4(sim,1)
insert_infected(PRE, 1, 4,1)[1]
end
h_init1 = findall(x->x.health in (LAT,MILD,MISO,INF,PRE,ASYMP),humans)
if p.ins_sec_strain
insert_infected(PRE, p.initialinf2, 4, 2)[1]
end
h_init2 = findall(x->x.health in (LAT2,MILD2,INF2,PRE2,ASYMP2),humans)
## save the preisolation isolation parameters
h_init1 = [h_init1]
h_init2 = [h_init2]
_fpreiso = p.fpreiso
p.fpreiso = 0
# split population in agegroups
grps = get_ag_dist()
# start the time loop
if p.vaccinating
vac_ind2 = vac_selection()
vac_ind = Array{Int64,1}(undef,length(vac_ind2))
for i = 1:length(vac_ind2)
vac_ind[i] = vac_ind2[i]
end
v1,v2 = vac_index_new(length(vac_ind))
time_vac::Int64 = 1
for st = 1:p.modeltime
# start of day
#println("$st")
if time_vac<=(length(v1)-1)
#if st%7 > 0 #we are vaccinating everyday
vac_ind2 = vac_time!(vac_ind,time_vac,v1,v2)
#vac_ind = [vac_ind vac_ind2]
resize!(vac_ind, length(vac_ind2))
for i = 1:length(vac_ind2)
vac_ind[i] = vac_ind2[i]
end
time_vac += 1
#end
end
#=if st == p.tpreiso ## time to introduce testing
global p.fpreiso = _fpreiso
end=#
_get_model_state(st, hmatrix) ## this datacollection needs to be at the start of the for loop
dyntrans(st, grps)
if st in p.days_Rt
aux1 = findall(x->x.swap == LAT,humans)
h_init1 = vcat(h_init1,[aux1])
aux2 = findall(x->x.swap == LAT2,humans)
h_init2 = vcat(h_init2,[aux2])
end
sw = time_update()
# end of day
end
else
for st = 1:p.modeltime
# start of day
#println("$st")
#=if st == p.tpreiso ## time to introduce testing
global p.fpreiso = _fpreiso
end=#
_get_model_state(st, hmatrix) ## this datacollection needs to be at the start of the for loop
dyntrans(st, grps)
if st in p.days_Rt
aux1 = findall(x->x.swap == LAT,humans)
h_init1 = vcat(h_init1,[aux1])
aux2 = findall(x->x.swap == LAT2,humans)
h_init2 = vcat(h_init2,[aux2])
end
sw = time_update()
# end of day
end
end
return hmatrix,h_init1,h_init2 ## return the model state as well as the age groups.
end
export main
function vac_selection()
pos = findall(x-> humans[x].age>=20 && humans[x].age<65,1:length(humans))
pos_hcw = sample(pos,Int(round(p.hcw_vac_comp*p.hcw_prop*p.popsize)),replace = false)
for i in pos_hcw
humans[i].hcw = true
end
pos_com = findall(x->humans[x].comorbidity == 1 && !(x in pos_hcw) && humans[x].age<65 && humans[x].age>=18, 1:length(humans))
pos_com = sample(pos_com,Int(round(p.comor_comp*length(pos_com))),replace=false)
pos_eld = findall(x-> humans[x].age>=65, 1:length(humans))
pos_eld = sample(pos_eld,Int(round(p.eld_comp*length(pos_eld))),replace=false)
pos_n_com = findall(x->humans[x].comorbidity == 0 && !(x in pos_hcw) && humans[x].age<65 && humans[x].age>=18, 1:length(humans))
pos_n_com = sample(pos_n_com,Int(round(p.gen_cov*length(pos_n_com))),replace=false)
#pos_y = findall(x-> humans[x].age<18, 1:length(humans))
#pos_y = sample(pos_y,Int(round(p.young_comp*length(pos_y))),replace=false)
if p.priority
aux1 = findall(y->humans[y].comorbidity==1,pos_eld)
pos1 = shuffle([pos_com;pos_eld[aux1]])
aux1 = findall(y->humans[y].comorbidity==0,pos_eld)
pos1 = [pos1;pos_eld[aux1]]
else
pos1 = shuffle([pos_com;pos_eld])
end
#pos2 = shuffle([pos_n_com;pos_y])
pos2 = shuffle(pos_n_com)
v = [pos_hcw; pos1; pos2]
if p.set_g_cov
if p.cov_val*p.popsize > length(v)
error("general population compliance is not enough to reach the coverage.")
exit(1)
else
aux = Int(round(p.cov_val*p.popsize))
v = v[1:aux]
end
elseif p.no_cap
##nothing to do v is v
else
if p.fixed_cov*p.popsize > length(v)
error("general population compliance is not enough to reach the coverage.")
exit(1)
else
aux = Int(round(p.fixed_cov*p.popsize))
v = v[1:aux]
end
end
return v
end
function vac_index_new(l::Int64)
v1 = Array{Int64,1}(undef,p.modeltime);
v2 = Array{Int64,1}(undef,p.modeltime);
n::Int64 = p.fd_2+p.sd1
v1_aux::Bool = false
v2_aux::Bool = false
kk::Int64 = 2
if p.single_dose
for i = 1:p.modeltime
v1[i] = -1
v2[i] = -1
end
v1[1] = 0
while !v1_aux
v1[kk] = v1[kk-1]+n
if v1[kk] >= l
v1[kk] = l
v1_aux = true
end
kk += 1
end
a = findfirst(x-> x == l, v1)
for i = (a+1):length(v1)
v1[i] = -1
end
a = a+1
else
for i = 1:p.modeltime
v1[i] = -1
v2[i] = -1
end
v1[1] = 0
v2[1] = 0
eligible::Int64 = 0
for i = 2:(p.sec_dose_delay+1)
v1[i] = (i-1)*p.fd_1
v2[i] = 0
if i > (p.vac_period+1)
eligible = eligible+(v1[i-p.vac_period]-v1[i-p.vac_period-1])
end
end
kk = p.sec_dose_delay+2
#eligible::Int64 = 0
last_v2::Int64 = 0
while !v1_aux || !v2_aux
eligible = eligible+(v1[kk-p.vac_period]-v1[kk-p.vac_period-1])
n1_a = v1_aux ? n : p.sd1
v2_1 = min(n1_a,eligible-last_v2)
v2[kk] = last_v2+v2_1
last_v2 = v2[kk]
n_aux = n-v2_1
v1[kk] = v1[kk-1]+n_aux
if v1[kk] >= l
v1[kk] = l
v1_aux = true
end
if v2[kk] >= l
v2[kk] = l
v2_aux = true
end
kk += 1
end
a = findfirst(x-> x == l, v1)
for i = (a+1):length(v1)
v1[i] = -1
end
a = findfirst(x-> x == -1, v2)
end
return v1[1:(a-1)],v2[1:(a-1)]
end
function vac_time!(vac_ind::Array{Int64,1},t::Int64,n_1_dose::Array{Int64,1},n_2_dose::Array{Int64,1})
##first dose
for i = (n_1_dose[t]+1):1:n_1_dose[t+1]
x = humans[vac_ind[i]]
if x.vac_status == 0
if x.health in (MILD, MISO, INF, IISO, HOS, ICU, DED,MILD2, MISO2, INF2, IISO2, HOS2, ICU2, DED2)
pos = findall(k-> !(humans[vac_ind[k]].health in (MILD, MISO, INF, IISO, HOS, ICU, DED,MILD2, MISO2, INF2, IISO2, HOS2, ICU2, DED2)) && k>n_1_dose[t+1],1:length(vac_ind))
if length(pos) > 0
r = rand(pos)
aux = vac_ind[i]
vac_ind[i] = vac_ind[r]
vac_ind[r] = aux
x = humans[vac_ind[i]]
x.days_vac = 0
x.vac_status = 1
x.index_day = 1
end
else
x.days_vac = 0
x.vac_status = 1
x.index_day = 1
end
end
end
for i = (n_2_dose[t]+1):1:n_2_dose[t+1]
x = humans[vac_ind[i]]
if x.health in (MILD, MISO, INF, IISO, HOS, ICU, DED,MILD2, MISO2, INF2, IISO2, HOS2, ICU2, DED2)
if t != (length(n_2_dose)-1)
vac_ind = [vac_ind; x.idx]
n_2_dose[end] += 1
end
else
if !x.hcw
drop_out_rate = [p.drop_rate;p.drop_rate;p.drop_rate]
#= drop_out_rate = [0;0;0] =#
ages_drop = [17;64;999]
age_ind = findfirst(k->k>=x.age,ages_drop)
if rand() < (1-drop_out_rate[age_ind])#p.sec_dose_comp
x = humans[vac_ind[i]]
#red_com = p.vac_com_dec_min+rand()*(p.vac_com_dec_max-p.vac_com_dec_min)
#x.vac_ef = ((1-red_com)^x.comorbidity)*(p.vac_efficacy/2.0)+(p.vac_efficacy/2.0)
x.days_vac = 0
x.vac_status = 2
x.index_day = 1
end
else
#red_com = p.vac_com_dec_min+rand()*(p.vac_com_dec_max-p.vac_com_dec_min)
#x.vac_ef = ((1-red_com)^x.comorbidity)*(p.vac_efficacy/2.0)+(p.vac_efficacy/2.0)
x.days_vac = 0
x.vac_status = 2
x.index_day = 1
end
end
end
return vac_ind
end
function vac_update(x::Human)
comm::Int64 = 0
if x.age >= 65
comm = 1
else
comm = x.comorbidity
end
#= if x.vac_status > 0
if x.days_vac == p.days_to_protection[x.vac_status]
if x.vac_status == 1
red_com = x.vac_red #p.vac_com_dec_min+rand()*(p.vac_com_dec_max-p.vac_com_dec_min)
aux = p.single_dose ? ((1-red_com)^comm)*(p.vac_efficacy) : ((1-red_com)^comm)*p.vac_efficacy_fd
x.vac_ef = aux
else
red_com = x.vac_red#p.vac_com_dec_min+rand()*(p.vac_com_dec_max-p.vac_com_dec_min)
x.vac_ef = ((1-red_com)^comm)*(p.vac_efficacy-p.vac_efficacy_fd)+x.vac_ef
end
end
x.days_vac += 1
end =#
if x.vac_status == 1
if x.days_vac == p.days_to_protection[x.vac_status][1]#14
red_com = x.vac_red #p.vac_com_dec_min+rand()*(p.vac_com_dec_max-p.vac_com_dec_min)
x.vac_ef_inf = p.vac_efficacy_inf[x.vac_status][1]
x.vac_ef_symp = p.vac_efficacy_symp[x.vac_status][1]
x.vac_ef_sev = p.vac_efficacy_sev[x.vac_status][1]
x.index_day = min(length(p.days_to_protection[x.vac_status]),x.index_day+1)
elseif x.days_vac == p.days_to_protection[x.vac_status][x.index_day]#14
red_com = x.vac_red #p.vac_com_dec_min+rand()*(p.vac_com_dec_max-p.vac_com_dec_min)
x.vac_ef_inf = (p.vac_efficacy_inf[x.vac_status][x.index_day]-p.vac_efficacy_inf[x.vac_status][x.index_day-1])+x.vac_ef_inf
x.vac_ef_symp = (p.vac_efficacy_symp[x.vac_status][x.index_day]-p.vac_efficacy_symp[x.vac_status][x.index_day-1])+x.vac_ef_symp
x.vac_ef_sev = (p.vac_efficacy_sev[x.vac_status][x.index_day]-p.vac_efficacy_sev[x.vac_status][x.index_day-1])+x.vac_ef_sev
x.index_day = min(length(p.days_to_protection[x.vac_status]),x.index_day+1)
end
if !p.single_dose
if x.days_vac > p.max_vac_delay #42
x.vac_ef_inf = x.vac_ef_inf-(p.ef_decrease_per_week/7)#0.05/7
if x.vac_ef_inf < p.min_eff #0.02
x.vac_ef_inf = p.min_eff
end
x.vac_ef_symp = x.vac_ef_symp-(p.ef_decrease_per_week/7)#0.05/7
if x.vac_ef_symp < p.min_eff #0.02
x.vac_ef_symp = p.min_eff
end
x.vac_ef_sev = x.vac_ef_sev-(p.ef_decrease_per_week/7)#0.05/7
if x.vac_ef_sev < p.min_eff #0.02
x.vac_ef_sev = p.min_eff
end
end
end
x.days_vac += 1
elseif x.vac_status == 2
if x.days_vac == p.days_to_protection[x.vac_status][1]#0
if p.vac_effect == 1
aux = (p.vac_efficacy_inf[x.vac_status][1]-p.vac_efficacy_inf[1][length(p.vac_efficacy_inf[1])])+x.vac_ef_inf #0.43 + x = second dose
if aux < p.vac_efficacy_inf[1][length(p.vac_efficacy_inf[1])]
aux = p.vac_efficacy_inf[1][length(p.vac_efficacy_inf[1])]
end
aux1 = ((1-x.vac_red)^comm)*aux
##symp
aux = (p.vac_efficacy_symp[x.vac_status][1]-p.vac_efficacy_symp[1][length(p.vac_efficacy_symp[1])])+x.vac_ef_symp #0.43 + x = second dose
if aux < p.vac_efficacy_symp[1][length(p.vac_efficacy_symp[1])]
aux = p.vac_efficacy_symp[1][length(p.vac_efficacy_symp[1])]
end
aux2 = ((1-x.vac_red)^comm)*aux
##sev
aux = (p.vac_efficacy_sev[x.vac_status][1]-p.vac_efficacy_sev[1][length(p.vac_efficacy_sev[1])])+x.vac_ef_sev #0.43 + x = second dose
if aux < p.vac_efficacy_sev[1][length(p.vac_efficacy_sev[1])]
aux = p.vac_efficacy_sev[1][length(p.vac_efficacy_sev[1])]
end
aux3 = ((1-x.vac_red)^comm)*aux
elseif p.vac_effect == 2
aux1 = ((1- x.vac_red)^comm)*p.vac_efficacy_inf[x.vac_status][1] #0.95
aux2 = ((1- x.vac_red)^comm)*p.vac_efficacy_symp[x.vac_status][1] #0.95
aux3 = ((1- x.vac_red)^comm)*p.vac_efficacy_sev[x.vac_status][1] #0.95
else
error("Vaccinating but no vac effect")
end
#p.vac_com_dec_min+rand()*(p.vac_com_dec_max-p.vac_com_dec_min)
x.vac_ef_inf = aux1
x.vac_ef_symp = aux2
x.vac_ef_sev = aux3
x.index_day = min(length(p.days_to_protection[x.vac_status]),x.index_day+1)
elseif x.days_vac == p.days_to_protection[x.vac_status][x.index_day]#7
if p.vac_effect == 1
aux = (p.vac_efficacy_inf[x.vac_status][x.index_day]-p.vac_efficacy_inf[x.vac_status][x.index_day-1])+x.vac_ef_inf #0.43 + x = second dose
#= if aux < p.vac_efficacy_inf[1][length(p.vac_efficacy_inf[1])]
aux = p.vac_efficacy_inf[1][length(p.vac_efficacy_inf[1])]
end =#
aux1 = ((1-x.vac_red)^comm)*aux
##symp
aux = (p.vac_efficacy_symp[x.vac_status][x.index_day]-p.vac_efficacy_symp[x.vac_status][x.index_day-1])+x.vac_ef_symp #0.43 + x = second dose
#= if aux < p.vac_efficacy_symp[1][length(p.vac_efficacy_symp[1])]
aux = p.vac_efficacy_symp[1][length(p.vac_efficacy_symp[1])]
end =#
aux2 = ((1-x.vac_red)^comm)*aux
##sev
aux = (p.vac_efficacy_sev[x.vac_status][x.index_day]-p.vac_efficacy_sev[x.vac_status][x.index_day-1])+x.vac_ef_sev #0.43 + x = second dose
#= if aux < p.vac_efficacy_sev[1][length(p.vac_efficacy_sev[1])]
aux = p.vac_efficacy_sev[1][length(p.vac_efficacy_sev[1])]
end =#
aux3 = ((1-x.vac_red)^comm)*aux
elseif p.vac_effect == 2
aux1 = ((1- x.vac_red)^comm)*p.vac_efficacy_inf[x.vac_status][x.index_day] #0.95
aux2 = ((1- x.vac_red)^comm)*p.vac_efficacy_symp[x.vac_status][x.index_day] #0.95
aux3 = ((1- x.vac_red)^comm)*p.vac_efficacy_sev[x.vac_status][x.index_day] #0.95
else
error("Vaccinating but no vac effect")
end
#p.vac_com_dec_min+rand()*(p.vac_com_dec_max-p.vac_com_dec_min)
x.vac_ef_inf = aux1
x.vac_ef_symp = aux2
x.vac_ef_sev = aux3
x.index_day = min(length(p.days_to_protection[x.vac_status]),x.index_day+1)
end
x.days_vac += 1
end
end
function reset_params(ip::ModelParameters)
# the p is a global const
# the ip is an incoming different instance of parameters
# copy the values from ip to p.
for x in propertynames(p)
setfield!(p, x, getfield(ip, x))
end
# reset the contact tracing data collection structure
for x in propertynames(ct_data)
setfield!(ct_data, x, 0)
end
# resize and update the BETAS constant array
init_betas()
# resize the human array to change population size
resize!(humans, p.popsize)
end
export reset_params, reset_params_default
function _model_check()
## checks model parameters before running
(p.fctcapture > 0 && p.fpreiso > 0) && error("Can not do contact tracing and ID/ISO of pre at the same time.")
(p.fctcapture > 0 && p.maxtracedays == 0) && error("maxtracedays can not be zero")
end
## Data Collection/ Model State functions
function _get_model_state(st, hmatrix)
# collects the model state (i.e. agent status at time st)
for i=1:length(humans)
hmatrix[i, st] = Int(humans[i].health)
end
end
export _get_model_state
function _collectdf(hmatrix)
## takes the output of the humans x time matrix and processes it into a dataframe
#_names_inci = Symbol.(["lat_inc", "mild_inc", "miso_inc", "inf_inc", "iiso_inc", "hos_inc", "icu_inc", "rec_inc", "ded_inc"])
#_names_prev = Symbol.(["sus", "lat", "mild", "miso", "inf", "iiso", "hos", "icu", "rec", "ded"])
mdf_inc, mdf_prev = _get_incidence_and_prev(hmatrix)
mdf = hcat(mdf_inc, mdf_prev)
_names_inc = Symbol.(string.((Symbol.(instances(HEALTH)[1:end - 1])), "_INC"))
_names_prev = Symbol.(string.((Symbol.(instances(HEALTH)[1:end - 1])), "_PREV"))
_names = vcat(_names_inc..., _names_prev...)
datf = DataFrame(mdf, _names)
insertcols!(datf, 1, :time => 1:p.modeltime) ## add a time column to the resulting dataframe
return datf
end
function _splitstate(hmatrix, ags)
#split the full hmatrix into 4 age groups based on ags (the array of age group of each agent)
#sizes = [length(findall(x -> x == i, ags)) for i = 1:4]
matx = []#Array{Array{Int64, 2}, 1}(undef, 4)
for i = 1:maximum(ags)#length(agebraks)
idx = findall(x -> x == i, ags)
push!(matx, view(hmatrix, idx, :))
end
return matx
end
export _splitstate
function _get_incidence_and_prev(hmatrix)
cols = instances(HEALTH)[1:end - 1] ## don't care about the UNDEF health status
inc = zeros(Int64, p.modeltime, length(cols))
pre = zeros(Int64, p.modeltime, length(cols))
for i = 1:length(cols)
inc[:, i] = _get_column_incidence(hmatrix, cols[i])
pre[:, i] = _get_column_prevalence(hmatrix, cols[i])
end
return inc, pre
end
function _get_column_incidence(hmatrix, hcol)
inth = Int(hcol)
timevec = zeros(Int64, p.modeltime)
for r in eachrow(hmatrix)
idx = findfirst(x -> x == inth, r)
if idx !== nothing
timevec[idx] += 1
end
end
return timevec
end
function herd_immu_dist_4(sim::Int64,strain::Int64)
rng = MersenneTwister(200*sim)
vec_n = zeros(Int32,6)
N::Int64 = 0
if p.herd == 5
vec_n = [9; 148; 262; 68; 4; 9]
N = 5
elseif p.herd == 10
vec_n = [32; 279; 489; 143; 24; 33]
N = 9
elseif p.herd == 20
vec_n = [71; 531; 962; 302; 57; 77]
N = 14
elseif p.herd == 30
vec_n = [105; 757; 1448; 481; 87; 122]
N = 16
elseif p.herd == 0
vec_n = [0;0;0;0;0;0]
else
error("No herd immunity")
end
for g = 1:6
pos = findall(y->y.ag_new == g && y.health == SUS,humans)
n_dist = min(length(pos),vec_n[g])
pos2 = sample(rng,pos,n_dist,replace=false)
for i = pos2
humans[i].strain = strain
humans[i].swap = strain == 1 ? REC : REC2
move_to_recovered(humans[i])
humans[i].sickfrom = INF
humans[i].herd_im = true
end
end
return N
end
function _get_column_prevalence(hmatrix, hcol)
inth = Int(hcol)
timevec = zeros(Int64, p.modeltime)
for (i, c) in enumerate(eachcol(hmatrix))
idx = findall(x -> x == inth, c)
if idx !== nothing
ps = length(c[idx])
timevec[i] = ps
end
end
return timevec
end
function _count_infectors()
pre_ctr = asymp_ctr = mild_ctr = inf_ctr = pre_ctr2 = asymp_ctr2 = mild_ctr2 = inf_ctr2 = 0
for x in humans
if x.health != SUS ## meaning they got sick at some point
if x.sickfrom == PRE
pre_ctr += 1
elseif x.sickfrom == ASYMP
asymp_ctr += 1
elseif x.sickfrom == MILD || x.sickfrom == MISO
mild_ctr += 1
elseif x.sickfrom == INF || x.sickfrom == IISO
inf_ctr += 1
elseif x.sickfrom == PRE2
pre_ctr2 += 1
elseif x.sickfrom == ASYMP2
asymp_ctr2 += 1
elseif x.sickfrom == MILD2 || x.sickfrom == MISO2
mild_ctr2 += 1
elseif x.sickfrom == INF2 || x.sickfrom == IISO2
inf_ctr2 += 1
else
error("sickfrom not set right: $(x.sickfrom)")
end
end
end
return (pre_ctr, asymp_ctr, mild_ctr, inf_ctr,pre_ctr2, asymp_ctr2, mild_ctr2, inf_ctr2)
end
export _collectdf, _get_incidence_and_prev, _get_column_incidence, _get_column_prevalence, _count_infectors
## initialization functions
function get_province_ag(prov)
ret = @match prov begin
#=:alberta => Distributions.Categorical(@SVector [0.0655, 0.1851, 0.4331, 0.1933, 0.1230])
:bc => Distributions.Categorical(@SVector [0.0475, 0.1570, 0.3905, 0.2223, 0.1827])
:canada => Distributions.Categorical(@SVector [0.0540, 0.1697, 0.3915, 0.2159, 0.1689])
:manitoba => Distributions.Categorical(@SVector [0.0634, 0.1918, 0.3899, 0.1993, 0.1556])
:newbruns => Distributions.Categorical(@SVector [0.0460, 0.1563, 0.3565, 0.2421, 0.1991])
:newfdland => Distributions.Categorical(@SVector [0.0430, 0.1526, 0.3642, 0.2458, 0.1944])
:nwterrito => Distributions.Categorical(@SVector [0.0747, 0.2026, 0.4511, 0.1946, 0.0770])
:novasco => Distributions.Categorical(@SVector [0.0455, 0.1549, 0.3601, 0.2405, 0.1990])
:nunavut => Distributions.Categorical(@SVector [0.1157, 0.2968, 0.4321, 0.1174, 0.0380])
:pei => Distributions.Categorical(@SVector [0.0490, 0.1702, 0.3540, 0.2329, 0.1939])
:quebec => Distributions.Categorical(@SVector [0.0545, 0.1615, 0.3782, 0.2227, 0.1831])
:saskat => Distributions.Categorical(@SVector [0.0666, 0.1914, 0.3871, 0.1997, 0.1552])
:yukon => Distributions.Categorical(@SVector [0.0597, 0.1694, 0.4179, 0.2343, 0.1187])=#
:ontario => Distributions.Categorical(@SVector [0.0519, 0.1727, 0.3930, 0.2150, 0.1674])
:usa => Distributions.Categorical(@SVector [0.059444636404977,0.188450296592341,0.396101793107413,0.189694011721906,0.166309262173363])
:newyork => Distributions.Categorical(@SVector [0.064000, 0.163000, 0.448000, 0.181000, 0.144000])
_ => error("shame for not knowing your canadian provinces and territories")
end
return ret
end
export get_province_ag
function comorbidity(ag::Int16)
a = [4;19;49;64;79;999]
g = findfirst(x->x>=ag,a)
prob = [0.05; 0.1; 0.28; 0.55; 0.74; 0.81]
com = rand() < prob[g] ? 1 : 0
return com
end
export comorbidity
function initialize()
agedist = get_province_ag(p.prov)
for i = 1:p.popsize
humans[i] = Human() ## create an empty human
x = humans[i]
x.idx = i
x.ag = rand(agedist)
x.age = rand(agebraks[x.ag])
a = [4;19;49;64;79;999]
g = findfirst(y->y>=x.age,a)
x.ag_new = g
x.exp = 999 ## susceptible people don't expire.
x.dur = sample_epi_durations() # sample epi periods
if rand() < p.eldq && x.ag == p.eldqag ## check if elderly need to be quarantined.
x.iso = true
x.isovia = :qu
end
x.comorbidity = comorbidity(x.age)
x.vac_red = p.vac_com_dec_min+rand()*(p.vac_com_dec_max-p.vac_com_dec_min)
# initialize the next day counts (this is important in initialization since dyntrans runs first)
get_nextday_counts(x)
end
end