Skip to content

Commit ca5627a

Browse files
committed
ageBasedContactsSampling contact matrix can be of any size
1 parent d2e0b82 commit ca5627a

4 files changed

Lines changed: 29 additions & 10 deletions

File tree

src/methods/contact_sampling_methods.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ function sample_contacts(contactparameter_sampling::AgeBasedContactSampling, set
105105
return Individual[]
106106
end
107107
interval = contactparameter_sampling.contact_matrix.interval_steps
108-
max_age = contactparameter_sampling.contact_matrix.aggregation_bound
109-
orig_bin = (individual.age ÷ interval) + 1
108+
max_age = contactparameter_sampling.contact_matrix.aggregation_bound - 1
109+
orig_bin = (min(individual.age, max_age) ÷ interval) + 1
110110
contact_matrix::Matrix{Float64} = contactparameter_sampling.contact_matrix.data
111111
age_pyramid = contactparameter_sampling.age_pyramid
112112
# if age_pyramid is not ready compute it
113113
if size(age_pyramid)[1] == 0
114114
age_pyramid = zeros(size(contact_matrix)[1])
115115
for ind in present_inds
116-
interval_id = ind.age ÷ interval + 1
116+
interval_id = min(ind.age, max_age) ÷ interval + 1
117117
age_pyramid[interval_id] += 1
118118
end
119119
age_pyramid = age_pyramid ./ sum(age_pyramid)
@@ -155,7 +155,8 @@ function sample_contacts(contactparameter_sampling::AgeBasedContactSampling, set
155155
# Second order sampling (i.e. structural one)
156156
out = Individual[]
157157
for i = 1:number_of_contacts
158-
dest_bin = (res[i].age ÷ interval) + 1
158+
159+
dest_bin = (min(res[i].age, max_age) ÷ interval) + 1
159160
m = contact_matrix[orig_bin, dest_bin]
160161
if m > 0.0
161162
m = m / m_max # since we multiplied by m_max in line no. 113

src/structs/parameters/contact_sampling_method_structs.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ mutable struct AgeBasedContactSampling <: ContactSamplingMethod
8686
throw(ArgumentError("Sum of row $i in 'contact_matrix' is $s, but the sum has to be equal to 1.0!"))
8787
end
8888
end
89-
contact_matrix = ContactMatrix{Float64}(matrix, interval)
89+
aggregation_bound = size(matrix)[1] * interval
90+
contact_matrix = ContactMatrix{Float64}(matrix, interval, aggregation_bound)
9091
return new(contactparameter, interval, contact_matrix, Float64[])
9192
end
9293

test/contactsamplingtest.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,14 @@
7474
end
7575

7676
@testset "AgeBasedContactSampling" begin
77+
BASE_FOLDER = dirname(dirname(pathof(GEMS)))
7778
m = hcat([[rand() for i = 1:10] for i = 1:10]...)
7879
m = m .* hcat([vec(1 ./ sum(m, dims=2)) for _ =1:10]...) # normalization of each row
79-
abcs_null = AgeBasedContactSampling(0.0, 10, ContactMatrix{Float64}(m, 10), Float64[])
80-
abcs1 = AgeBasedContactSampling(1.0, 10, ContactMatrix{Float64}(m, 10), Float64[])
81-
abcs2 = AgeBasedContactSampling(2.0, 10, ContactMatrix{Float64}(m, 10), Float64[])
82-
abcs3 = AgeBasedContactSampling(3.0, 10, ContactMatrix{Float64}(m, 10), Float64[])
83-
abcs100 = AgeBasedContactSampling(100.0, 10, ContactMatrix{Float64}(m, 10), Float64[])
80+
abcs_null = AgeBasedContactSampling(0.0, 10, ContactMatrix{Float64}(m, 10, 100), Float64[])
81+
abcs1 = AgeBasedContactSampling(1.0, 10, ContactMatrix{Float64}(m, 10, 100), Float64[])
82+
abcs2 = AgeBasedContactSampling(2.0, 10, ContactMatrix{Float64}(m, 10, 100), Float64[])
83+
abcs3 = AgeBasedContactSampling(3.0, 10, ContactMatrix{Float64}(m, 10, 100), Float64[])
84+
abcs100 = AgeBasedContactSampling(contactparameter=100.0, interval=5, contact_matrix_file=BASE_FOLDER * "/test/testdata/contact_matrix.txt")
8485

8586
# initial infectant
8687
i = Individual(id = 1, age = floor(Int, rand(Uniform(1, 100))), sex = floor(Int, rand(Uniform(0, 2))), household=1)

test/testdata/contact_matrix.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
1.078648352651273047e-02 1.406942172548634323e-02 2.172939146734437568e-02 3.996479244037882267e-02 4.821162797228326080e-02 2.317560428528302879e-02 1.563520159223819039e-02 1.947867889161126326e-02 5.010407817973757533e-02 6.443369535456786035e-02 1.087577488223194222e-01 9.607508547398571630e-02 1.218945475670628470e-01 1.218945475670628470e-01 1.218945475670628470e-01 1.218945475670628470e-01
2+
5.337791838349025333e-03 6.058020699575762669e-02 6.700706798002174780e-02 4.627813849057937218e-02 2.796882756473987805e-02 1.747150662744478833e-02 1.245232503687944588e-02 2.008215374412142262e-02 6.453210540984608001e-02 5.629257532632708089e-02 6.294922095666549700e-02 6.901695743419071494e-02 1.225077806487693682e-01 1.225077806487693682e-01 1.225077806487693682e-01 1.225077806487693682e-01
3+
1.567972560745745135e-03 2.511764989914436197e-02 2.681477392651055447e-01 1.043544035029515121e-01 4.925441924479471295e-02 1.311135972000625205e-02 8.835452986259057645e-03 1.533847145063059296e-02 6.728482208518943297e-02 7.760691746976737526e-02 7.120053918988092367e-02 4.684714342352459815e-02 6.283327730049995119e-02 6.283327730049995119e-02 6.283327730049995119e-02 6.283327730049995119e-02
4+
5.512239942347747952e-04 5.228398938837975664e-03 7.342388281952225149e-02 5.482444131466176485e-01 9.471375556528359441e-02 1.394408119041069295e-02 5.155233636719321749e-03 8.961113262981022093e-03 3.486136148636063969e-02 5.629096013311547103e-02 3.548987722931576383e-02 1.864757342904237170e-02 2.612203129188963094e-02 2.612203129188963094e-02 2.612203129188963094e-02 2.612203129188963094e-02
5+
9.270135078969533047e-04 3.557718675192299718e-03 1.600860887760366738e-02 2.566705031839869311e-01 2.962008208340199955e-01 3.685491336490141767e-02 1.274932050698971038e-02 1.229485469237738375e-02 3.632178543007818367e-02 7.690648280356833510e-02 6.522574779423385982e-02 4.570672655998186951e-02 3.514387594229240880e-02 3.514387594229240880e-02 3.514387594229240880e-02 3.514387594229240880e-02
6+
2.323113442306297838e-03 3.638794053954639250e-03 9.887078395478750975e-03 9.623458281669523828e-02 1.918529440288510024e-01 7.706249316247885950e-02 2.558022816271537589e-02 2.486812145373507033e-02 6.316481480568289508e-02 1.038322901947161536e-01 1.225732164959637688e-01 6.954131122971640711e-02 5.236025293942635911e-02 5.236025293942635911e-02 5.236025293942635911e-02 5.236025293942635911e-02
7+
2.198251304751499234e-03 4.707890405563189100e-03 2.158305745595731281e-02 4.283582404494066381e-02 8.093401099132979670e-02 3.745350430696946231e-02 2.742168853366094838e-02 2.842399089839116322e-02 6.794804087986908159e-02 9.425708586741943229e-02 1.335172255332717495e-01 1.019627981702999620e-01 8.918915790189393955e-02 8.918915790189393955e-02 8.918915790189393955e-02 8.918915790189393955e-02
8+
1.699758690940276129e-03 6.037026522668422455e-03 1.372440439645343860e-02 2.529281696399507742e-02 4.299586672557133976e-02 2.375370737799934601e-02 1.735427436269906809e-02 3.069601912721955581e-02 8.130581922142460227e-02 9.474504565642402620e-02 9.019815138288521383e-02 7.564087425793314390e-02 1.241390588284466134e-01 1.241390588284466134e-01 1.241390588284466134e-01 1.241390588284466134e-01
9+
1.439612636551255553e-03 5.629077850275382780e-03 2.661675497843626545e-02 4.255634788764722787e-02 5.451314695924511927e-02 2.060371879902641104e-02 1.660148474251646813e-02 2.604416489994806211e-02 1.073086917540823776e-01 1.186490342013815646e-01 1.136964703663485149e-01 5.658963685348963185e-02 1.024379645177628667e-01 1.024379645177628667e-01 1.024379645177628667e-01 1.024379645177628667e-01
10+
5.029697139939702044e-04 2.491378493572388511e-03 9.794898740090440120e-03 4.649753980343424020e-02 4.561121658769014958e-02 1.782189680340325044e-02 1.275897938048971628e-02 2.069240935891731789e-02 7.541482378767444539e-02 1.403424484859699950e-01 1.525159069981585902e-01 7.388555785574991985e-02 1.004174934977139222e-01 1.004174934977139222e-01 1.004174934977139222e-01 1.004174934977139222e-01
11+
5.576561202371816631e-04 3.153859225736407607e-03 1.080555466317455887e-02 3.998165338881664449e-02 5.210737237981448444e-02 2.082051393709508785e-02 8.907367891564777970e-03 1.254402564429147465e-02 5.392167162722895368e-02 1.093645526721148536e-01 1.199803006802258598e-01 1.005904029778111886e-01 1.168162671979721401e-01 1.168162671979721401e-01 1.168162671979721401e-01 1.168162671979721401e-01
12+
6.750240668709355267e-04 1.810005749348591805e-03 5.876691996831779306e-03 1.736024671140351527e-02 3.102512129857856391e-02 1.646137453455618835e-02 9.958367304732936021e-03 1.199591417519933929e-02 4.453806608089299485e-02 5.699138232295580114e-02 1.168017519255723224e-01 1.153971243049814882e-01 1.427772323820189204e-01 1.427772323820189204e-01 1.427772323820189204e-01 1.427772323820189204e-01
13+
4.743649253853962004e-04 1.563170953537884730e-03 4.060178312799547773e-03 1.605612051539074756e-02 2.414995350194804047e-02 1.169829030579868333e-02 7.048135037079027859e-03 1.226442341918097811e-02 4.017615201145464249e-02 5.907053119171967737e-02 7.465586120853630325e-02 9.790804603188610344e-02 1.627186931463207309e-01 1.627186931463207309e-01 1.627186931463207309e-01 1.627186931463207309e-01
14+
4.743649253853962004e-04 1.563170953537884730e-03 4.060178312799547773e-03 1.605612051539074756e-02 2.414995350194804047e-02 1.169829030579868333e-02 7.048135037079027859e-03 1.226442341918097811e-02 4.017615201145464249e-02 5.907053119171967737e-02 7.465586120853630325e-02 9.790804603188610344e-02 1.627186931463207309e-01 1.627186931463207309e-01 1.627186931463207309e-01 1.627186931463207309e-01
15+
4.743649253853962004e-04 1.563170953537884730e-03 4.060178312799547773e-03 1.605612051539074756e-02 2.414995350194804047e-02 1.169829030579868333e-02 7.048135037079027859e-03 1.226442341918097811e-02 4.017615201145464249e-02 5.907053119171967737e-02 7.465586120853630325e-02 9.790804603188610344e-02 1.627186931463207309e-01 1.627186931463207309e-01 1.627186931463207309e-01 1.627186931463207309e-01
16+
4.743649253853962004e-04 1.563170953537884730e-03 4.060178312799547773e-03 1.605612051539074756e-02 2.414995350194804047e-02 1.169829030579868333e-02 7.048135037079027859e-03 1.226442341918097811e-02 4.017615201145464249e-02 5.907053119171967737e-02 7.465586120853630325e-02 9.790804603188610344e-02 1.627186931463207309e-01 1.627186931463207309e-01 1.627186931463207309e-01 1.627186931463207309e-01

0 commit comments

Comments
 (0)