-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Github_synthpops_synthpops_contact_networks_py.html
1061 lines (1061 loc) · 300 KB
/
_Github_synthpops_synthpops_contact_networks_py.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=emulateIE7" />
<title>Coverage for D:\Github\synthpops\synthpops\contact_networks.py: 86%</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.hotkeys.js"></script>
<script type="text/javascript" src="jquery.isonscreen.js"></script>
<script type="text/javascript" src="coverage_html.js"></script>
<script type="text/javascript">
jQuery(document).ready(coverage.pyfile_ready);
</script>
</head>
<body class="pyfile">
<div id="header">
<div class="content">
<h1>Coverage for <b>D:\Github\synthpops\synthpops\contact_networks.py</b> :
<span class="pc_cov">86%</span>
</h1>
<img id="keyboard_icon" src="keybd_closed.png" alt="Show keyboard shortcuts" />
<h2 class="stats">
474 statements
<button type="button" class="run shortkey_r button_toggle_run" title="Toggle lines run">407 run</button>
<button type="button" class="mis show_mis shortkey_m button_toggle_mis" title="Toggle lines missing">67 missing</button>
<button type="button" class="exc show_exc shortkey_x button_toggle_exc" title="Toggle lines excluded">0 excluded</button>
</h2>
</div>
</div>
<div class="help_panel">
<img id="panel_icon" src="keybd_open.png" alt="Hide keyboard shortcuts" />
<p class="legend">Hot-keys on this page</p>
<div>
<p class="keyhelp">
<span class="key">r</span>
<span class="key">m</span>
<span class="key">x</span>
<span class="key">p</span> toggle line displays
</p>
<p class="keyhelp">
<span class="key">j</span>
<span class="key">k</span> next/prev highlighted chunk
</p>
<p class="keyhelp">
<span class="key">0</span> (zero) top of page
</p>
<p class="keyhelp">
<span class="key">1</span> (one) first highlighted chunk
</p>
</div>
</div>
<div id="source">
<p id="t1" class="pln"><span class="n"><a href="#t1">1</a></span><span class="t"><span class="str">"""</span> </span><span class="r"></span></p>
<p id="t2" class="pln"><span class="n"><a href="#t2">2</a></span><span class="t"><span class="str">This module generates the household, school, and workplace contact networks.</span> </span><span class="r"></span></p>
<p id="t3" class="pln"><span class="n"><a href="#t3">3</a></span><span class="t"><span class="str">"""</span> </span><span class="r"></span></p>
<p id="t4" class="pln"><span class="n"><a href="#t4">4</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t5" class="pln"><span class="n"><a href="#t5">5</a></span><span class="t"><span class="com"># import os</span> </span><span class="r"></span></p>
<p id="t6" class="run"><span class="n"><a href="#t6">6</a></span><span class="t"><span class="key">from</span> <span class="nam">copy</span> <span class="key">import</span> <span class="nam">deepcopy</span> </span><span class="r"></span></p>
<p id="t7" class="run"><span class="n"><a href="#t7">7</a></span><span class="t"><span class="key">from</span> <span class="nam">collections</span> <span class="key">import</span> <span class="nam">Counter</span> </span><span class="r"></span></p>
<p id="t8" class="pln"><span class="n"><a href="#t8">8</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t9" class="run"><span class="n"><a href="#t9">9</a></span><span class="t"><span class="key">import</span> <span class="nam">sciris</span> <span class="key">as</span> <span class="nam">sc</span> </span><span class="r"></span></p>
<p id="t10" class="run"><span class="n"><a href="#t10">10</a></span><span class="t"><span class="key">import</span> <span class="nam">numpy</span> <span class="key">as</span> <span class="nam">np</span> </span><span class="r"></span></p>
<p id="t11" class="pln"><span class="n"><a href="#t11">11</a></span><span class="t"><span class="com"># import pandas as pd</span> </span><span class="r"></span></p>
<p id="t12" class="pln"><span class="n"><a href="#t12">12</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t13" class="run"><span class="n"><a href="#t13">13</a></span><span class="t"><span class="key">import</span> <span class="nam">matplotlib</span> <span class="key">as</span> <span class="nam">mplt</span> </span><span class="r"></span></p>
<p id="t14" class="run"><span class="n"><a href="#t14">14</a></span><span class="t"><span class="key">import</span> <span class="nam">matplotlib</span><span class="op">.</span><span class="nam">pyplot</span> <span class="key">as</span> <span class="nam">plt</span> </span><span class="r"></span></p>
<p id="t15" class="run"><span class="n"><a href="#t15">15</a></span><span class="t"><span class="key">import</span> <span class="nam">cmocean</span> </span><span class="r"></span></p>
<p id="t16" class="pln"><span class="n"><a href="#t16">16</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t17" class="run"><span class="n"><a href="#t17">17</a></span><span class="t"><span class="key">from</span> <span class="op">.</span> <span class="key">import</span> <span class="nam">base</span> <span class="key">as</span> <span class="nam">spb</span> </span><span class="r"></span></p>
<p id="t18" class="run"><span class="n"><a href="#t18">18</a></span><span class="t"><span class="key">from</span> <span class="op">.</span> <span class="key">import</span> <span class="nam">data_distributions</span> <span class="key">as</span> <span class="nam">spdata</span> </span><span class="r"></span></p>
<p id="t19" class="run"><span class="n"><a href="#t19">19</a></span><span class="t"><span class="key">from</span> <span class="op">.</span> <span class="key">import</span> <span class="nam">sampling</span> <span class="key">as</span> <span class="nam">spsamp</span> </span><span class="r"></span></p>
<p id="t20" class="run"><span class="n"><a href="#t20">20</a></span><span class="t"><span class="key">from</span> <span class="op">.</span> <span class="key">import</span> <span class="nam">contacts</span> <span class="key">as</span> <span class="nam">spct</span> </span><span class="r"></span></p>
<p id="t21" class="run"><span class="n"><a href="#t21">21</a></span><span class="t"><span class="key">from</span> <span class="op">.</span> <span class="key">import</span> <span class="nam">read_write</span> <span class="key">as</span> <span class="nam">sprw</span> </span><span class="r"></span></p>
<p id="t22" class="pln"><span class="n"><a href="#t22">22</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t23" class="pln"><span class="n"><a href="#t23">23</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t24" class="run"><span class="n"><a href="#t24">24</a></span><span class="t"><span class="key">def</span> <span class="nam">generate_household_sizes</span><span class="op">(</span><span class="nam">Nhomes</span><span class="op">,</span> <span class="nam">hh_size_distr</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t25" class="pln"><span class="n"><a href="#t25">25</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t26" class="pln"><span class="n"><a href="#t26">26</a></span><span class="t"><span class="str"> Given a number of homes and a household size distribution, generate the number of homes of each size.</span> </span><span class="r"></span></p>
<p id="t27" class="pln"><span class="n"><a href="#t27">27</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t28" class="pln"><span class="n"><a href="#t28">28</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t29" class="pln"><span class="n"><a href="#t29">29</a></span><span class="t"><span class="str"> Nhomes (int) : The number of homes.</span> </span><span class="r"></span></p>
<p id="t30" class="pln"><span class="n"><a href="#t30">30</a></span><span class="t"><span class="str"> hh_size_distr (dict) : The distribution of household sizes.</span> </span><span class="r"></span></p>
<p id="t31" class="pln"><span class="n"><a href="#t31">31</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t32" class="pln"><span class="n"><a href="#t32">32</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t33" class="pln"><span class="n"><a href="#t33">33</a></span><span class="t"><span class="str"> An array with the count of households of size s at index s-1.</span> </span><span class="r"></span></p>
<p id="t34" class="pln"><span class="n"><a href="#t34">34</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t35" class="run"><span class="n"><a href="#t35">35</a></span><span class="t"> <span class="nam">max_size</span> <span class="op">=</span> <span class="nam">max</span><span class="op">(</span><span class="nam">hh_size_distr</span><span class="op">.</span><span class="nam">keys</span><span class="op">(</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t36" class="run"><span class="n"><a href="#t36">36</a></span><span class="t"> <span class="nam">hh_sizes</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">multinomial</span><span class="op">(</span><span class="nam">Nhomes</span><span class="op">,</span> <span class="op">[</span><span class="nam">hh_size_distr</span><span class="op">[</span><span class="nam">s</span><span class="op">]</span> <span class="key">for</span> <span class="nam">s</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="nam">max_size</span><span class="op">+</span><span class="num">1</span><span class="op">)</span><span class="op">]</span><span class="op">,</span> <span class="nam">size</span><span class="op">=</span><span class="num">1</span><span class="op">)</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t37" class="run"><span class="n"><a href="#t37">37</a></span><span class="t"> <span class="key">return</span> <span class="nam">hh_sizes</span> </span><span class="r"></span></p>
<p id="t38" class="pln"><span class="n"><a href="#t38">38</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t39" class="pln"><span class="n"><a href="#t39">39</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t40" class="run"><span class="n"><a href="#t40">40</a></span><span class="t"><span class="key">def</span> <span class="nam">generate_household_sizes_from_fixed_pop_size</span><span class="op">(</span><span class="nam">N</span><span class="op">,</span> <span class="nam">hh_size_distr</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t41" class="pln"><span class="n"><a href="#t41">41</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t42" class="pln"><span class="n"><a href="#t42">42</a></span><span class="t"><span class="str"> Given a number of people and a household size distribution, generate the number of homes of each size needed to place everyone in a household.</span> </span><span class="r"></span></p>
<p id="t43" class="pln"><span class="n"><a href="#t43">43</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t44" class="pln"><span class="n"><a href="#t44">44</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t45" class="pln"><span class="n"><a href="#t45">45</a></span><span class="t"><span class="str"> N (int) : The number of people in the population.</span> </span><span class="r"></span></p>
<p id="t46" class="pln"><span class="n"><a href="#t46">46</a></span><span class="t"><span class="str"> hh_size_distr (dict) : The distribution of household sizes.</span> </span><span class="r"></span></p>
<p id="t47" class="pln"><span class="n"><a href="#t47">47</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t48" class="pln"><span class="n"><a href="#t48">48</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t49" class="pln"><span class="n"><a href="#t49">49</a></span><span class="t"><span class="str"> An array with the count of households of size s at index s-1.</span> </span><span class="r"></span></p>
<p id="t50" class="pln"><span class="n"><a href="#t50">50</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t51" class="pln"><span class="n"><a href="#t51">51</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t52" class="pln"><span class="n"><a href="#t52">52</a></span><span class="t"> <span class="com"># Quickly produce number of expected households for a population of size N</span> </span><span class="r"></span></p>
<p id="t53" class="run"><span class="n"><a href="#t53">53</a></span><span class="t"> <span class="nam">ss</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">[</span><span class="nam">hh_size_distr</span><span class="op">[</span><span class="nam">s</span><span class="op">]</span> <span class="op">*</span> <span class="nam">s</span> <span class="key">for</span> <span class="nam">s</span> <span class="key">in</span> <span class="nam">hh_size_distr</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t54" class="run"><span class="n"><a href="#t54">54</a></span><span class="t"> <span class="nam">f</span> <span class="op">=</span> <span class="nam">N</span> <span class="op">/</span> <span class="nam">np</span><span class="op">.</span><span class="nam">round</span><span class="op">(</span><span class="nam">ss</span><span class="op">,</span> <span class="num">1</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t55" class="run"><span class="n"><a href="#t55">55</a></span><span class="t"> <span class="nam">hh_sizes</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">zeros</span><span class="op">(</span><span class="nam">len</span><span class="op">(</span><span class="nam">hh_size_distr</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t56" class="pln"><span class="n"><a href="#t56">56</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t57" class="run"><span class="n"><a href="#t57">57</a></span><span class="t"> <span class="key">for</span> <span class="nam">s</span> <span class="key">in</span> <span class="nam">hh_size_distr</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t58" class="run"><span class="n"><a href="#t58">58</a></span><span class="t"> <span class="nam">hh_sizes</span><span class="op">[</span><span class="nam">s</span><span class="op">-</span><span class="num">1</span><span class="op">]</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="nam">hh_size_distr</span><span class="op">[</span><span class="nam">s</span><span class="op">]</span> <span class="op">*</span> <span class="nam">f</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t59" class="run"><span class="n"><a href="#t59">59</a></span><span class="t"> <span class="nam">N_gen</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">[</span><span class="nam">hh_sizes</span><span class="op">[</span><span class="nam">s</span><span class="op">-</span><span class="num">1</span><span class="op">]</span> <span class="op">*</span> <span class="nam">s</span> <span class="key">for</span> <span class="nam">s</span> <span class="key">in</span> <span class="nam">hh_size_distr</span><span class="op">]</span><span class="op">,</span> <span class="nam">dtype</span><span class="op">=</span><span class="nam">int</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t60" class="pln"><span class="n"><a href="#t60">60</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t61" class="pln"><span class="n"><a href="#t61">61</a></span><span class="t"> <span class="com"># Check what population size was created from the drawn count of household sizes</span> </span><span class="r"></span></p>
<p id="t62" class="run"><span class="n"><a href="#t62">62</a></span><span class="t"> <span class="nam">people_to_add_or_remove</span> <span class="op">=</span> <span class="nam">N_gen</span> <span class="op">-</span> <span class="nam">N</span> </span><span class="r"></span></p>
<p id="t63" class="pln"><span class="n"><a href="#t63">63</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t64" class="pln"><span class="n"><a href="#t64">64</a></span><span class="t"> <span class="com"># did not create household sizes to match or exceed the population size so add count for households needed</span> </span><span class="r"></span></p>
<p id="t65" class="run"><span class="n"><a href="#t65">65</a></span><span class="t"> <span class="nam">hh_size_keys</span> <span class="op">=</span> <span class="op">[</span><span class="nam">k</span> <span class="key">for</span> <span class="nam">k</span> <span class="key">in</span> <span class="nam">hh_size_distr</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t66" class="run"><span class="n"><a href="#t66">66</a></span><span class="t"> <span class="nam">hh_size_distr_array</span> <span class="op">=</span> <span class="op">[</span><span class="nam">hh_size_distr</span><span class="op">[</span><span class="nam">k</span><span class="op">]</span> <span class="key">for</span> <span class="nam">k</span> <span class="key">in</span> <span class="nam">hh_size_keys</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t67" class="run"><span class="n"><a href="#t67">67</a></span><span class="t"> <span class="key">if</span> <span class="nam">people_to_add_or_remove</span> <span class="op"><</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t68" class="pln"><span class="n"><a href="#t68">68</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t69" class="run"><span class="n"><a href="#t69">69</a></span><span class="t"> <span class="nam">people_to_add</span> <span class="op">=</span> <span class="op">-</span><span class="nam">people_to_add_or_remove</span> </span><span class="r"></span></p>
<p id="t70" class="run"><span class="n"><a href="#t70">70</a></span><span class="t"> <span class="key">while</span> <span class="nam">people_to_add</span> <span class="op">></span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t71" class="run"><span class="n"><a href="#t71">71</a></span><span class="t"> <span class="nam">new_household_size</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">choice</span><span class="op">(</span><span class="nam">hh_size_keys</span><span class="op">,</span> <span class="nam">p</span><span class="op">=</span><span class="nam">hh_size_distr_array</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t72" class="pln"><span class="n"><a href="#t72">72</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t73" class="run"><span class="n"><a href="#t73">73</a></span><span class="t"> <span class="key">if</span> <span class="nam">new_household_size</span> <span class="op">></span> <span class="nam">people_to_add</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t74" class="mis show_mis"><span class="n"><a href="#t74">74</a></span><span class="t"> <span class="nam">new_household_size</span> <span class="op">=</span> <span class="nam">people_to_add</span> </span><span class="r"></span></p>
<p id="t75" class="run"><span class="n"><a href="#t75">75</a></span><span class="t"> <span class="nam">people_to_add</span> <span class="op">-=</span> <span class="nam">new_household_size</span> </span><span class="r"></span></p>
<p id="t76" class="pln"><span class="n"><a href="#t76">76</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t77" class="run"><span class="n"><a href="#t77">77</a></span><span class="t"> <span class="nam">hh_sizes</span><span class="op">[</span><span class="nam">new_household_size</span><span class="op">-</span><span class="num">1</span><span class="op">]</span> <span class="op">+=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t78" class="pln"><span class="n"><a href="#t78">78</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t79" class="pln"><span class="n"><a href="#t79">79</a></span><span class="t"> <span class="com"># created households that result in too many people</span> </span><span class="r"></span></p>
<p id="t80" class="run"><span class="n"><a href="#t80">80</a></span><span class="t"> <span class="key">elif</span> <span class="nam">people_to_add_or_remove</span> <span class="op">></span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t81" class="run"><span class="n"><a href="#t81">81</a></span><span class="t"> <span class="nam">people_to_remove</span> <span class="op">=</span> <span class="nam">people_to_add_or_remove</span> </span><span class="r"></span></p>
<p id="t82" class="run"><span class="n"><a href="#t82">82</a></span><span class="t"> <span class="key">while</span> <span class="nam">people_to_remove</span> <span class="op">></span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t83" class="pln"><span class="n"><a href="#t83">83</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t84" class="run"><span class="n"><a href="#t84">84</a></span><span class="t"> <span class="nam">new_household_size_to_remove</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">choice</span><span class="op">(</span><span class="nam">hh_size_keys</span><span class="op">,</span> <span class="nam">p</span><span class="op">=</span><span class="nam">hh_size_distr_array</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t85" class="run"><span class="n"><a href="#t85">85</a></span><span class="t"> <span class="key">if</span> <span class="nam">new_household_size_to_remove</span> <span class="op">></span> <span class="nam">people_to_remove</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t86" class="run"><span class="n"><a href="#t86">86</a></span><span class="t"> <span class="nam">new_household_size_to_remove</span> <span class="op">=</span> <span class="nam">people_to_remove</span> </span><span class="r"></span></p>
<p id="t87" class="pln"><span class="n"><a href="#t87">87</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t88" class="run"><span class="n"><a href="#t88">88</a></span><span class="t"> <span class="nam">people_to_remove</span> <span class="op">-=</span> <span class="nam">new_household_size_to_remove</span> </span><span class="r"></span></p>
<p id="t89" class="run"><span class="n"><a href="#t89">89</a></span><span class="t"> <span class="nam">hh_sizes</span><span class="op">[</span><span class="nam">new_household_size_to_remove</span><span class="op">-</span><span class="num">1</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t90" class="pln"><span class="n"><a href="#t90">90</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t91" class="run"><span class="n"><a href="#t91">91</a></span><span class="t"> <span class="nam">hh_sizes</span> <span class="op">=</span> <span class="nam">hh_sizes</span><span class="op">.</span><span class="nam">astype</span><span class="op">(</span><span class="nam">int</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t92" class="run"><span class="n"><a href="#t92">92</a></span><span class="t"> <span class="key">return</span> <span class="nam">hh_sizes</span> </span><span class="r"></span></p>
<p id="t93" class="pln"><span class="n"><a href="#t93">93</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t94" class="pln"><span class="n"><a href="#t94">94</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t95" class="run"><span class="n"><a href="#t95">95</a></span><span class="t"><span class="key">def</span> <span class="nam">get_totalpopsize_from_household_sizes</span><span class="op">(</span><span class="nam">hh_sizes</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t96" class="pln"><span class="n"><a href="#t96">96</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t97" class="pln"><span class="n"><a href="#t97">97</a></span><span class="t"><span class="str"> Sum the population of a specific household size from the count array.</span> </span><span class="r"></span></p>
<p id="t98" class="pln"><span class="n"><a href="#t98">98</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t99" class="pln"><span class="n"><a href="#t99">99</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t100" class="pln"><span class="n"><a href="#t100">100</a></span><span class="t"><span class="str"> hh_sizes (array): The count of household size s at index s-1.</span> </span><span class="r"></span></p>
<p id="t101" class="pln"><span class="n"><a href="#t101">101</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t102" class="pln"><span class="n"><a href="#t102">102</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t103" class="pln"><span class="n"><a href="#t103">103</a></span><span class="t"><span class="str"> An integer indicating the total number of people in household size s.</span> </span><span class="r"></span></p>
<p id="t104" class="pln"><span class="n"><a href="#t104">104</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t105" class="run"><span class="n"><a href="#t105">105</a></span><span class="t"> <span class="key">return</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">[</span><span class="nam">hh_sizes</span><span class="op">[</span><span class="nam">s</span><span class="op">]</span> <span class="op">*</span> <span class="op">(</span><span class="nam">s</span><span class="op">+</span><span class="num">1</span><span class="op">)</span> <span class="key">for</span> <span class="nam">s</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">len</span><span class="op">(</span><span class="nam">hh_sizes</span><span class="op">)</span><span class="op">)</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t106" class="pln"><span class="n"><a href="#t106">106</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t107" class="pln"><span class="n"><a href="#t107">107</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t108" class="run"><span class="n"><a href="#t108">108</a></span><span class="t"><span class="key">def</span> <span class="nam">generate_household_head_age_by_size</span><span class="op">(</span><span class="nam">hha_by_size_counts</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">,</span> <span class="nam">hh_size</span><span class="op">,</span> <span class="nam">single_year_age_distr</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t109" class="pln"><span class="n"><a href="#t109">109</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t110" class="pln"><span class="n"><a href="#t110">110</a></span><span class="t"><span class="str"> Generate the age of the head of the household, also known as the reference person of the household,</span> </span><span class="r"></span></p>
<p id="t111" class="pln"><span class="n"><a href="#t111">111</a></span><span class="t"><span class="str"> conditional on the size of the household.</span> </span><span class="r"></span></p>
<p id="t112" class="pln"><span class="n"><a href="#t112">112</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t113" class="pln"><span class="n"><a href="#t113">113</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t114" class="pln"><span class="n"><a href="#t114">114</a></span><span class="t"><span class="str"> hha_by_size_counts (matrix) : A matrix in which each row contains the age distribution of the reference person for household size s at index s-1.</span> </span><span class="r"></span></p>
<p id="t115" class="pln"><span class="n"><a href="#t115">115</a></span><span class="t"><span class="str"> hha_brackets (dict) : The age brackets for the heads of household.</span> </span><span class="r"></span></p>
<p id="t116" class="pln"><span class="n"><a href="#t116">116</a></span><span class="t"><span class="str"> hh_size (int) : The household size.</span> </span><span class="r"></span></p>
<p id="t117" class="pln"><span class="n"><a href="#t117">117</a></span><span class="t"><span class="str"> single_year_age_distr (dict) : The age distribution.</span> </span><span class="r"></span></p>
<p id="t118" class="pln"><span class="n"><a href="#t118">118</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t119" class="pln"><span class="n"><a href="#t119">119</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t120" class="pln"><span class="n"><a href="#t120">120</a></span><span class="t"><span class="str"> Age of the head of the household or reference person.</span> </span><span class="r"></span></p>
<p id="t121" class="pln"><span class="n"><a href="#t121">121</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t122" class="run"><span class="n"><a href="#t122">122</a></span><span class="t"> <span class="nam">distr</span> <span class="op">=</span> <span class="nam">hha_by_size_counts</span><span class="op">[</span><span class="nam">hh_size</span><span class="op">-</span><span class="num">1</span><span class="op">,</span> <span class="op">:</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t123" class="run"><span class="n"><a href="#t123">123</a></span><span class="t"> <span class="nam">b</span> <span class="op">=</span> <span class="nam">spsamp</span><span class="op">.</span><span class="nam">sample_single_arr</span><span class="op">(</span><span class="nam">distr</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t124" class="run"><span class="n"><a href="#t124">124</a></span><span class="t"> <span class="nam">hha</span> <span class="op">=</span> <span class="nam">spsamp</span><span class="op">.</span><span class="nam">sample_from_range</span><span class="op">(</span><span class="nam">single_year_age_distr</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">[</span><span class="nam">b</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">[</span><span class="nam">b</span><span class="op">]</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t125" class="pln"><span class="n"><a href="#t125">125</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t126" class="run"><span class="n"><a href="#t126">126</a></span><span class="t"> <span class="key">return</span> <span class="nam">hha</span> </span><span class="r"></span></p>
<p id="t127" class="pln"><span class="n"><a href="#t127">127</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t128" class="pln"><span class="n"><a href="#t128">128</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t129" class="run"><span class="n"><a href="#t129">129</a></span><span class="t"><span class="key">def</span> <span class="nam">generate_living_alone</span><span class="op">(</span><span class="nam">hh_sizes</span><span class="op">,</span> <span class="nam">hha_by_size_counts</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">,</span> <span class="nam">single_year_age_distr</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t130" class="pln"><span class="n"><a href="#t130">130</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t131" class="pln"><span class="n"><a href="#t131">131</a></span><span class="t"><span class="str"> Generate the ages of those living alone.</span> </span><span class="r"></span></p>
<p id="t132" class="pln"><span class="n"><a href="#t132">132</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t133" class="pln"><span class="n"><a href="#t133">133</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t134" class="pln"><span class="n"><a href="#t134">134</a></span><span class="t"><span class="str"> hh_sizes (array) : The count of household size s at index s-1.</span> </span><span class="r"></span></p>
<p id="t135" class="pln"><span class="n"><a href="#t135">135</a></span><span class="t"><span class="str"> hha_by_size_counts (matrix) : A matrix in which each row contains the age distribution of the reference person for household size s at index s-1.</span> </span><span class="r"></span></p>
<p id="t136" class="pln"><span class="n"><a href="#t136">136</a></span><span class="t"><span class="str"> hha_brackets (dict) : The age brackets for the heads of household.</span> </span><span class="r"></span></p>
<p id="t137" class="pln"><span class="n"><a href="#t137">137</a></span><span class="t"><span class="str"> single_year_age_distr (dict) : The age distribution.</span> </span><span class="r"></span></p>
<p id="t138" class="pln"><span class="n"><a href="#t138">138</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t139" class="pln"><span class="n"><a href="#t139">139</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t140" class="pln"><span class="n"><a href="#t140">140</a></span><span class="t"><span class="str"> An array of households of size 1 where each household is a row and the value in the row is the age of the household member.</span> </span><span class="r"></span></p>
<p id="t141" class="pln"><span class="n"><a href="#t141">141</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t142" class="pln"><span class="n"><a href="#t142">142</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t143" class="run"><span class="n"><a href="#t143">143</a></span><span class="t"> <span class="nam">size</span> <span class="op">=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t144" class="run"><span class="n"><a href="#t144">144</a></span><span class="t"> <span class="nam">homes</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">zeros</span><span class="op">(</span><span class="op">(</span><span class="nam">hh_sizes</span><span class="op">[</span><span class="nam">size</span><span class="op">-</span><span class="num">1</span><span class="op">]</span><span class="op">,</span> <span class="num">1</span><span class="op">)</span><span class="op">,</span> <span class="nam">dtype</span><span class="op">=</span><span class="nam">int</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t145" class="pln"><span class="n"><a href="#t145">145</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t146" class="run"><span class="n"><a href="#t146">146</a></span><span class="t"> <span class="key">for</span> <span class="nam">h</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">hh_sizes</span><span class="op">[</span><span class="nam">size</span><span class="op">-</span><span class="num">1</span><span class="op">]</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t147" class="run"><span class="n"><a href="#t147">147</a></span><span class="t"> <span class="nam">hha</span> <span class="op">=</span> <span class="nam">generate_household_head_age_by_size</span><span class="op">(</span><span class="nam">hha_by_size_counts</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">,</span> <span class="nam">size</span><span class="op">,</span> <span class="nam">single_year_age_distr</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t148" class="run"><span class="n"><a href="#t148">148</a></span><span class="t"> <span class="nam">homes</span><span class="op">[</span><span class="nam">h</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="nam">hha</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t149" class="pln"><span class="n"><a href="#t149">149</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t150" class="run"><span class="n"><a href="#t150">150</a></span><span class="t"> <span class="key">return</span> <span class="nam">homes</span> </span><span class="r"></span></p>
<p id="t151" class="pln"><span class="n"><a href="#t151">151</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t152" class="pln"><span class="n"><a href="#t152">152</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t153" class="run"><span class="n"><a href="#t153">153</a></span><span class="t"><span class="key">def</span> <span class="nam">generate_larger_households</span><span class="op">(</span><span class="nam">size</span><span class="op">,</span> <span class="nam">hh_sizes</span><span class="op">,</span> <span class="nam">hha_by_size_counts</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">,</span> <span class="nam">age_brackets</span><span class="op">,</span> <span class="nam">age_by_brackets_dic</span><span class="op">,</span> <span class="nam">contact_matrix_dic</span><span class="op">,</span> <span class="nam">single_year_age_distr</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t154" class="pln"><span class="n"><a href="#t154">154</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t155" class="pln"><span class="n"><a href="#t155">155</a></span><span class="t"><span class="str"> Generate ages of those living in households of greater than one individual. Reference individual is sampled conditional on the household size.</span> </span><span class="r"></span></p>
<p id="t156" class="pln"><span class="n"><a href="#t156">156</a></span><span class="t"><span class="str"> All other household members have their ages sampled conditional on the reference person's age and the age mixing contact matrix</span> </span><span class="r"></span></p>
<p id="t157" class="pln"><span class="n"><a href="#t157">157</a></span><span class="t"><span class="str"> in households for the population under study.</span> </span><span class="r"></span></p>
<p id="t158" class="pln"><span class="n"><a href="#t158">158</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t159" class="pln"><span class="n"><a href="#t159">159</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t160" class="pln"><span class="n"><a href="#t160">160</a></span><span class="t"><span class="str"> size (int) : The household size.</span> </span><span class="r"></span></p>
<p id="t161" class="pln"><span class="n"><a href="#t161">161</a></span><span class="t"><span class="str"> hh_sizes (array) : The count of household size s at index s-1.</span> </span><span class="r"></span></p>
<p id="t162" class="pln"><span class="n"><a href="#t162">162</a></span><span class="t"><span class="str"> hha_by_size_counts (matrix) : A matrix in which each row contains the age distribution of the reference person for household size s at index s-1.</span> </span><span class="r"></span></p>
<p id="t163" class="pln"><span class="n"><a href="#t163">163</a></span><span class="t"><span class="str"> hha_brackets (dict) : The age brackets for the heads of household.</span> </span><span class="r"></span></p>
<p id="t164" class="pln"><span class="n"><a href="#t164">164</a></span><span class="t"><span class="str"> age_brackets (dict) : A dictionary mapping age bracket keys to age bracket range.</span> </span><span class="r"></span></p>
<p id="t165" class="pln"><span class="n"><a href="#t165">165</a></span><span class="t"><span class="str"> age_by_brackets_dic (dict) : A dictionary mapping age to the age bracket range it falls within.</span> </span><span class="r"></span></p>
<p id="t166" class="pln"><span class="n"><a href="#t166">166</a></span><span class="t"><span class="str"> contact_matrix_dic (dict) : A dictionary of the age-specific contact matrix for different physical contact settings.</span> </span><span class="r"></span></p>
<p id="t167" class="pln"><span class="n"><a href="#t167">167</a></span><span class="t"><span class="str"> single_year_age_distr (dict) : The age distribution.</span> </span><span class="r"></span></p>
<p id="t168" class="pln"><span class="n"><a href="#t168">168</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t169" class="pln"><span class="n"><a href="#t169">169</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t170" class="pln"><span class="n"><a href="#t170">170</a></span><span class="t"><span class="str"> An array of households for size ``size`` where each household is a row and the values in the row are the ages of the household members.</span> </span><span class="r"></span></p>
<p id="t171" class="pln"><span class="n"><a href="#t171">171</a></span><span class="t"><span class="str"> The first age in the row is the age of the reference individual.</span> </span><span class="r"></span></p>
<p id="t172" class="pln"><span class="n"><a href="#t172">172</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t173" class="run"><span class="n"><a href="#t173">173</a></span><span class="t"> <span class="nam">ya_coin</span> <span class="op">=</span> <span class="num">0.15</span> <span class="com"># produces far too few young adults without this for Seattle, Washington. This is a placeholder value. Users will need to change to fit whatever population they are working with.</span> </span><span class="r"></span></p>
<p id="t174" class="pln"><span class="n"><a href="#t174">174</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t175" class="run"><span class="n"><a href="#t175">175</a></span><span class="t"> <span class="nam">homes</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">zeros</span><span class="op">(</span><span class="op">(</span><span class="nam">hh_sizes</span><span class="op">[</span><span class="nam">size</span><span class="op">-</span><span class="num">1</span><span class="op">]</span><span class="op">,</span> <span class="nam">size</span><span class="op">)</span><span class="op">,</span> <span class="nam">dtype</span><span class="op">=</span><span class="nam">int</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t176" class="pln"><span class="n"><a href="#t176">176</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t177" class="run"><span class="n"><a href="#t177">177</a></span><span class="t"> <span class="key">for</span> <span class="nam">h</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">hh_sizes</span><span class="op">[</span><span class="nam">size</span><span class="op">-</span><span class="num">1</span><span class="op">]</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t178" class="pln"><span class="n"><a href="#t178">178</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t179" class="run"><span class="n"><a href="#t179">179</a></span><span class="t"> <span class="nam">hha</span> <span class="op">=</span> <span class="nam">generate_household_head_age_by_size</span><span class="op">(</span><span class="nam">hha_by_size_counts</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">,</span> <span class="nam">size</span><span class="op">,</span> <span class="nam">single_year_age_distr</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t180" class="pln"><span class="n"><a href="#t180">180</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t181" class="run"><span class="n"><a href="#t181">181</a></span><span class="t"> <span class="nam">homes</span><span class="op">[</span><span class="nam">h</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="nam">hha</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t182" class="pln"><span class="n"><a href="#t182">182</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t183" class="run"><span class="n"><a href="#t183">183</a></span><span class="t"> <span class="nam">b</span> <span class="op">=</span> <span class="nam">age_by_brackets_dic</span><span class="op">[</span><span class="nam">hha</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t184" class="run"><span class="n"><a href="#t184">184</a></span><span class="t"> <span class="nam">b</span> <span class="op">=</span> <span class="nam">min</span><span class="op">(</span><span class="nam">b</span><span class="op">,</span> <span class="nam">contact_matrix_dic</span><span class="op">[</span><span class="str">'H'</span><span class="op">]</span><span class="op">.</span><span class="nam">shape</span><span class="op">[</span><span class="num">0</span><span class="op">]</span><span class="op">-</span><span class="num">1</span><span class="op">)</span> <span class="com"># Ensure it doesn't go past the end of the array</span> </span><span class="r"></span></p>
<p id="t185" class="run"><span class="n"><a href="#t185">185</a></span><span class="t"> <span class="nam">b_prob</span> <span class="op">=</span> <span class="nam">contact_matrix_dic</span><span class="op">[</span><span class="str">'H'</span><span class="op">]</span><span class="op">[</span><span class="nam">b</span><span class="op">,</span> <span class="op">:</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t186" class="pln"><span class="n"><a href="#t186">186</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t187" class="run"><span class="n"><a href="#t187">187</a></span><span class="t"> <span class="nam">age_distr_vals</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">array</span><span class="op">(</span><span class="nam">list</span><span class="op">(</span><span class="nam">single_year_age_distr</span><span class="op">.</span><span class="nam">values</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><span class="op">,</span> <span class="nam">dtype</span><span class="op">=</span><span class="nam">np</span><span class="op">.</span><span class="nam">float64</span><span class="op">)</span> <span class="com"># Convert to an array for faster processing</span> </span><span class="r"></span></p>
<p id="t188" class="pln"><span class="n"><a href="#t188">188</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t189" class="run"><span class="n"><a href="#t189">189</a></span><span class="t"> <span class="key">for</span> <span class="nam">n</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="nam">size</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t190" class="run"><span class="n"><a href="#t190">190</a></span><span class="t"> <span class="nam">bi</span> <span class="op">=</span> <span class="nam">spsamp</span><span class="op">.</span><span class="nam">sample_single_arr</span><span class="op">(</span><span class="nam">b_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t191" class="run"><span class="n"><a href="#t191">191</a></span><span class="t"> <span class="nam">ai</span> <span class="op">=</span> <span class="nam">spsamp</span><span class="op">.</span><span class="nam">sample_from_range</span><span class="op">(</span><span class="nam">single_year_age_distr</span><span class="op">,</span> <span class="nam">age_brackets</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span><span class="op">,</span> <span class="nam">age_brackets</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t192" class="pln"><span class="n"><a href="#t192">192</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t193" class="run"><span class="n"><a href="#t193">193</a></span><span class="t"> <span class="key">if</span> <span class="nam">ai</span> <span class="op">></span> <span class="num">5</span> <span class="key">and</span> <span class="nam">ai</span> <span class="op"><=</span> <span class="num">20</span><span class="op">:</span> <span class="com"># This a placeholder range. Users will need to change to fit whatever population they are working with.</span> </span><span class="r"></span></p>
<p id="t194" class="run"><span class="n"><a href="#t194">194</a></span><span class="t"> <span class="key">if</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">binomial</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="nam">ya_coin</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t195" class="run"><span class="n"><a href="#t195">195</a></span><span class="t"> <span class="nam">ai</span> <span class="op">=</span> <span class="nam">spsamp</span><span class="op">.</span><span class="nam">sample_from_range</span><span class="op">(</span><span class="nam">single_year_age_distr</span><span class="op">,</span> <span class="num">25</span><span class="op">,</span> <span class="num">32</span><span class="op">)</span> <span class="com"># This a placeholder range. Users will need to change to fit whatever population they are working with.</span> </span><span class="r"></span></p>
<p id="t196" class="pln"><span class="n"><a href="#t196">196</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t197" class="pln"><span class="n"><a href="#t197">197</a></span><span class="t"> <span class="com"># ai = spsamp.resample_age(age_distr_vals, ai)</span> </span><span class="r"></span></p>
<p id="t198" class="pln"><span class="n"><a href="#t198">198</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t199" class="run"><span class="n"><a href="#t199">199</a></span><span class="t"> <span class="nam">homes</span><span class="op">[</span><span class="nam">h</span><span class="op">]</span><span class="op">[</span><span class="nam">n</span><span class="op">]</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="nam">ai</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t200" class="pln"><span class="n"><a href="#t200">200</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t201" class="run"><span class="n"><a href="#t201">201</a></span><span class="t"> <span class="key">return</span> <span class="nam">homes</span> </span><span class="r"></span></p>
<p id="t202" class="pln"><span class="n"><a href="#t202">202</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t203" class="pln"><span class="n"><a href="#t203">203</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t204" class="run"><span class="n"><a href="#t204">204</a></span><span class="t"><span class="key">def</span> <span class="nam">generate_all_households</span><span class="op">(</span><span class="nam">N</span><span class="op">,</span> <span class="nam">hh_sizes</span><span class="op">,</span> <span class="nam">hha_by_size_counts</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">,</span> <span class="nam">age_brackets</span><span class="op">,</span> <span class="nam">age_by_brackets_dic</span><span class="op">,</span> <span class="nam">contact_matrix_dic</span><span class="op">,</span> <span class="nam">single_year_age_distr</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t205" class="pln"><span class="n"><a href="#t205">205</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t206" class="pln"><span class="n"><a href="#t206">206</a></span><span class="t"><span class="str"> Generate the ages of those living in households together. First create households of people living alone, then larger households.</span> </span><span class="r"></span></p>
<p id="t207" class="pln"><span class="n"><a href="#t207">207</a></span><span class="t"><span class="str"> For households larger than 1, a reference individual's age is sampled conditional on the household size, while all other household</span> </span><span class="r"></span></p>
<p id="t208" class="pln"><span class="n"><a href="#t208">208</a></span><span class="t"><span class="str"> members have their ages sampled conditional on the reference person's age and the age mixing contact matrix in households</span> </span><span class="r"></span></p>
<p id="t209" class="pln"><span class="n"><a href="#t209">209</a></span><span class="t"><span class="str"> for the population under study.</span> </span><span class="r"></span></p>
<p id="t210" class="pln"><span class="n"><a href="#t210">210</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t211" class="pln"><span class="n"><a href="#t211">211</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t212" class="pln"><span class="n"><a href="#t212">212</a></span><span class="t"><span class="str"> N (int) : The number of people in the population.</span> </span><span class="r"></span></p>
<p id="t213" class="pln"><span class="n"><a href="#t213">213</a></span><span class="t"><span class="str"> hh_sizes (array) : The count of household size s at index s-1.</span> </span><span class="r"></span></p>
<p id="t214" class="pln"><span class="n"><a href="#t214">214</a></span><span class="t"><span class="str"> hha_by_size_counts (matrix) : A matrix in which each row contains the age distribution of the reference person for household size s at index s-1.</span> </span><span class="r"></span></p>
<p id="t215" class="pln"><span class="n"><a href="#t215">215</a></span><span class="t"><span class="str"> hha_brackets (dict) : The age brackets for the heads of household.</span> </span><span class="r"></span></p>
<p id="t216" class="pln"><span class="n"><a href="#t216">216</a></span><span class="t"><span class="str"> age_brackets (dict) : The dictionary mapping age bracket keys to age bracket range.</span> </span><span class="r"></span></p>
<p id="t217" class="pln"><span class="n"><a href="#t217">217</a></span><span class="t"><span class="str"> age_by_brackets_dic (dict) : The dictionary mapping age to the age bracket range it falls within.</span> </span><span class="r"></span></p>
<p id="t218" class="pln"><span class="n"><a href="#t218">218</a></span><span class="t"><span class="str"> contact_matrix_dic (dict) : The dictionary of the age-specific contact matrix for different physical contact settings.</span> </span><span class="r"></span></p>
<p id="t219" class="pln"><span class="n"><a href="#t219">219</a></span><span class="t"><span class="str"> single_year_age_distr (dict) : The age distribution.</span> </span><span class="r"></span></p>
<p id="t220" class="pln"><span class="n"><a href="#t220">220</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t221" class="pln"><span class="n"><a href="#t221">221</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t222" class="pln"><span class="n"><a href="#t222">222</a></span><span class="t"><span class="str"> An array of all households where each household is a row and the values in the row are the ages of the household members.</span> </span><span class="r"></span></p>
<p id="t223" class="pln"><span class="n"><a href="#t223">223</a></span><span class="t"><span class="str"> The first age in the row is the age of the reference individual. Households are randomly shuffled by size.</span> </span><span class="r"></span></p>
<p id="t224" class="pln"><span class="n"><a href="#t224">224</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t225" class="run"><span class="n"><a href="#t225">225</a></span><span class="t"> <span class="nam">homes_dic</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t226" class="run"><span class="n"><a href="#t226">226</a></span><span class="t"> <span class="nam">homes_dic</span><span class="op">[</span><span class="num">1</span><span class="op">]</span> <span class="op">=</span> <span class="nam">generate_living_alone</span><span class="op">(</span><span class="nam">hh_sizes</span><span class="op">,</span> <span class="nam">hha_by_size_counts</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">,</span> <span class="nam">single_year_age_distr</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t227" class="pln"><span class="n"><a href="#t227">227</a></span><span class="t"> <span class="com"># remove living alone from the distribution to choose from!</span> </span><span class="r"></span></p>
<p id="t228" class="run"><span class="n"><a href="#t228">228</a></span><span class="t"> <span class="key">for</span> <span class="nam">h</span> <span class="key">in</span> <span class="nam">homes_dic</span><span class="op">[</span><span class="num">1</span><span class="op">]</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t229" class="run"><span class="n"><a href="#t229">229</a></span><span class="t"> <span class="nam">single_year_age_distr</span><span class="op">[</span><span class="nam">h</span><span class="op">[</span><span class="num">0</span><span class="op">]</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1.0</span><span class="op">/</span><span class="nam">N</span> </span><span class="r"></span></p>
<p id="t230" class="pln"><span class="n"><a href="#t230">230</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t231" class="run"><span class="n"><a href="#t231">231</a></span><span class="t"> <span class="key">for</span> <span class="nam">s</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="num">2</span><span class="op">,</span> <span class="num">8</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t232" class="run"><span class="n"><a href="#t232">232</a></span><span class="t"> <span class="nam">homes_dic</span><span class="op">[</span><span class="nam">s</span><span class="op">]</span> <span class="op">=</span> <span class="nam">generate_larger_households</span><span class="op">(</span><span class="nam">s</span><span class="op">,</span> <span class="nam">hh_sizes</span><span class="op">,</span> <span class="nam">hha_by_size_counts</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">,</span> <span class="nam">age_brackets</span><span class="op">,</span> <span class="nam">age_by_brackets_dic</span><span class="op">,</span> <span class="nam">contact_matrix_dic</span><span class="op">,</span> <span class="nam">single_year_age_distr</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t233" class="pln"><span class="n"><a href="#t233">233</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t234" class="run"><span class="n"><a href="#t234">234</a></span><span class="t"> <span class="nam">homes</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t235" class="run"><span class="n"><a href="#t235">235</a></span><span class="t"> <span class="key">for</span> <span class="nam">s</span> <span class="key">in</span> <span class="nam">homes_dic</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t236" class="run"><span class="n"><a href="#t236">236</a></span><span class="t"> <span class="nam">homes</span> <span class="op">+=</span> <span class="nam">list</span><span class="op">(</span><span class="nam">homes_dic</span><span class="op">[</span><span class="nam">s</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t237" class="pln"><span class="n"><a href="#t237">237</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t238" class="run"><span class="n"><a href="#t238">238</a></span><span class="t"> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">shuffle</span><span class="op">(</span><span class="nam">homes</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t239" class="run"><span class="n"><a href="#t239">239</a></span><span class="t"> <span class="key">return</span> <span class="nam">homes_dic</span><span class="op">,</span> <span class="nam">homes</span> </span><span class="r"></span></p>
<p id="t240" class="pln"><span class="n"><a href="#t240">240</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t241" class="pln"><span class="n"><a href="#t241">241</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t242" class="run"><span class="n"><a href="#t242">242</a></span><span class="t"><span class="key">def</span> <span class="nam">assign_uids_by_homes</span><span class="op">(</span><span class="nam">homes</span><span class="op">,</span> <span class="nam">id_len</span><span class="op">=</span><span class="num">16</span><span class="op">,</span> <span class="nam">use_int</span><span class="op">=</span><span class="key">True</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t243" class="pln"><span class="n"><a href="#t243">243</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t244" class="pln"><span class="n"><a href="#t244">244</a></span><span class="t"><span class="str"> Assign IDs to everyone in order by their households.</span> </span><span class="r"></span></p>
<p id="t245" class="pln"><span class="n"><a href="#t245">245</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t246" class="pln"><span class="n"><a href="#t246">246</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t247" class="pln"><span class="n"><a href="#t247">247</a></span><span class="t"><span class="str"> homes (array): The generated synthetic ages of household members.</span> </span><span class="r"></span></p>
<p id="t248" class="pln"><span class="n"><a href="#t248">248</a></span><span class="t"><span class="str"> id_len (int) : The length of the UID.</span> </span><span class="r"></span></p>
<p id="t249" class="pln"><span class="n"><a href="#t249">249</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t250" class="pln"><span class="n"><a href="#t250">250</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t251" class="pln"><span class="n"><a href="#t251">251</a></span><span class="t"><span class="str"> A copy of the generated households with IDs in place of ages, and a dictionary mapping ID to age.</span> </span><span class="r"></span></p>
<p id="t252" class="pln"><span class="n"><a href="#t252">252</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t253" class="run"><span class="n"><a href="#t253">253</a></span><span class="t"> <span class="nam">age_by_uid_dic</span> <span class="op">=</span> <span class="nam">dict</span><span class="op">(</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t254" class="pln"><span class="n"><a href="#t254">254</a></span><span class="t"> <span class="com"># age_by_uid_dic = sc.objdict()</span> </span><span class="r"></span></p>
<p id="t255" class="run"><span class="n"><a href="#t255">255</a></span><span class="t"> <span class="nam">homes_by_uids</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t256" class="pln"><span class="n"><a href="#t256">256</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t257" class="run"><span class="n"><a href="#t257">257</a></span><span class="t"> <span class="key">for</span> <span class="nam">h</span><span class="op">,</span> <span class="nam">home</span> <span class="key">in</span> <span class="nam">enumerate</span><span class="op">(</span><span class="nam">homes</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t258" class="pln"><span class="n"><a href="#t258">258</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t259" class="run"><span class="n"><a href="#t259">259</a></span><span class="t"> <span class="nam">home_ids</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t260" class="run"><span class="n"><a href="#t260">260</a></span><span class="t"> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">home</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t261" class="run"><span class="n"><a href="#t261">261</a></span><span class="t"> <span class="key">if</span> <span class="nam">use_int</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t262" class="run"><span class="n"><a href="#t262">262</a></span><span class="t"> <span class="nam">uid</span> <span class="op">=</span> <span class="nam">len</span><span class="op">(</span><span class="nam">age_by_uid_dic</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t263" class="pln"><span class="n"><a href="#t263">263</a></span><span class="t"> <span class="key">else</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t264" class="mis show_mis"><span class="n"><a href="#t264">264</a></span><span class="t"> <span class="nam">uid</span> <span class="op">=</span> <span class="nam">sc</span><span class="op">.</span><span class="nam">uuid</span><span class="op">(</span><span class="nam">length</span><span class="op">=</span><span class="nam">id_len</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t265" class="run"><span class="n"><a href="#t265">265</a></span><span class="t"> <span class="nam">age_by_uid_dic</span><span class="op">[</span><span class="nam">uid</span><span class="op">]</span> <span class="op">=</span> <span class="nam">a</span> </span><span class="r"></span></p>
<p id="t266" class="run"><span class="n"><a href="#t266">266</a></span><span class="t"> <span class="nam">home_ids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t267" class="pln"><span class="n"><a href="#t267">267</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t268" class="run"><span class="n"><a href="#t268">268</a></span><span class="t"> <span class="nam">homes_by_uids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">home_ids</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t269" class="pln"><span class="n"><a href="#t269">269</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t270" class="run"><span class="n"><a href="#t270">270</a></span><span class="t"> <span class="key">return</span> <span class="nam">homes_by_uids</span><span class="op">,</span> <span class="nam">age_by_uid_dic</span> </span><span class="r"></span></p>
<p id="t271" class="pln"><span class="n"><a href="#t271">271</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t272" class="pln"><span class="n"><a href="#t272">272</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t273" class="run"><span class="n"><a href="#t273">273</a></span><span class="t"><span class="key">def</span> <span class="nam">get_uids_in_school</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">n</span><span class="op">,</span> <span class="nam">location</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">,</span> <span class="nam">age_by_uid_dic</span><span class="op">=</span><span class="key">None</span><span class="op">,</span> <span class="nam">homes_by_uids</span><span class="op">=</span><span class="key">None</span><span class="op">,</span> <span class="nam">folder_name</span><span class="op">=</span><span class="key">None</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="key">False</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t274" class="pln"><span class="n"><a href="#t274">274</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t275" class="pln"><span class="n"><a href="#t275">275</a></span><span class="t"><span class="str"> Identify who in the population is attending school based on enrollment rates by age.</span> </span><span class="r"></span></p>
<p id="t276" class="pln"><span class="n"><a href="#t276">276</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t277" class="pln"><span class="n"><a href="#t277">277</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t278" class="pln"><span class="n"><a href="#t278">278</a></span><span class="t"><span class="str"> datadir (string) : The file path to the data directory.</span> </span><span class="r"></span></p>
<p id="t279" class="pln"><span class="n"><a href="#t279">279</a></span><span class="t"><span class="str"> n (int) : The number of people in the population.</span> </span><span class="r"></span></p>
<p id="t280" class="pln"><span class="n"><a href="#t280">280</a></span><span class="t"><span class="str"> location (string) : The name of the location.</span> </span><span class="r"></span></p>
<p id="t281" class="pln"><span class="n"><a href="#t281">281</a></span><span class="t"><span class="str"> state_location (string) : The name of the state the location is in.</span> </span><span class="r"></span></p>
<p id="t282" class="pln"><span class="n"><a href="#t282">282</a></span><span class="t"><span class="str"> country_location (string) : The name of the country the location is in.</span> </span><span class="r"></span></p>
<p id="t283" class="pln"><span class="n"><a href="#t283">283</a></span><span class="t"><span class="str"> age_by_uid_dic (dict) : A dictionary mapping ID to age for all individuals in the population.</span> </span><span class="r"></span></p>
<p id="t284" class="pln"><span class="n"><a href="#t284">284</a></span><span class="t"><span class="str"> homes_by_uids (list) : A list of lists where each sublist is a household and the IDs of the household members.</span> </span><span class="r"></span></p>
<p id="t285" class="pln"><span class="n"><a href="#t285">285</a></span><span class="t"><span class="str"> use_default (bool) : If True, try to first use the other parameters to find data specific to the location under study; otherwise, return default data drawing from Seattle, Washington.</span> </span><span class="r"></span></p>
<p id="t286" class="pln"><span class="n"><a href="#t286">286</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t287" class="pln"><span class="n"><a href="#t287">287</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t288" class="pln"><span class="n"><a href="#t288">288</a></span><span class="t"><span class="str"> A dictionary of students in schools mapping their ID to their age, a dictionary of students in school mapping age to the list of IDs with that age, and a dictionary mapping age to the number of students with that age.</span> </span><span class="r"></span></p>
<p id="t289" class="pln"><span class="n"><a href="#t289">289</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t290" class="run"><span class="n"><a href="#t290">290</a></span><span class="t"> <span class="nam">uids_in_school</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t291" class="run"><span class="n"><a href="#t291">291</a></span><span class="t"> <span class="nam">uids_in_school_by_age</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t292" class="run"><span class="n"><a href="#t292">292</a></span><span class="t"> <span class="nam">ages_in_school_count</span> <span class="op">=</span> <span class="nam">dict</span><span class="op">.</span><span class="nam">fromkeys</span><span class="op">(</span><span class="nam">np</span><span class="op">.</span><span class="nam">arange</span><span class="op">(</span><span class="num">101</span><span class="op">)</span><span class="op">,</span> <span class="num">0</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t293" class="pln"><span class="n"><a href="#t293">293</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t294" class="run"><span class="n"><a href="#t294">294</a></span><span class="t"> <span class="nam">rates</span> <span class="op">=</span> <span class="nam">spdata</span><span class="op">.</span><span class="nam">get_school_enrollment_rates</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">location</span><span class="op">=</span><span class="nam">location</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">=</span><span class="nam">state_location</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">=</span><span class="nam">country_location</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="nam">use_default</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t295" class="pln"><span class="n"><a href="#t295">295</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t296" class="run"><span class="n"><a href="#t296">296</a></span><span class="t"> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">np</span><span class="op">.</span><span class="nam">arange</span><span class="op">(</span><span class="num">101</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t297" class="run"><span class="n"><a href="#t297">297</a></span><span class="t"> <span class="nam">uids_in_school_by_age</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t298" class="pln"><span class="n"><a href="#t298">298</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t299" class="run"><span class="n"><a href="#t299">299</a></span><span class="t"> <span class="key">if</span> <span class="nam">age_by_uid_dic</span> <span class="key">is</span> <span class="key">None</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t300" class="run"><span class="n"><a href="#t300">300</a></span><span class="t"> <span class="key">if</span> <span class="nam">folder_name</span> <span class="key">is</span> <span class="key">None</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t301" class="mis show_mis"><span class="n"><a href="#t301">301</a></span><span class="t"> <span class="nam">errormsg</span> <span class="op">=</span> <span class="str">f'The variable folder_name is not given. Please provide this variable so that synthpops knows where to read in the dictionary age_by_uid_dic.'</span> </span><span class="r"></span></p>
<p id="t302" class="mis show_mis"><span class="n"><a href="#t302">302</a></span><span class="t"> <span class="key">raise</span> <span class="nam">ValueError</span><span class="op">(</span><span class="nam">errormsg</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t303" class="pln"><span class="n"><a href="#t303">303</a></span><span class="t"> <span class="com"># age_by_uid_dic = read_in_age_by_uid(datadir, location, state_location, country_location, n)</span> </span><span class="r"></span></p>
<p id="t304" class="run"><span class="n"><a href="#t304">304</a></span><span class="t"> <span class="nam">age_by_uid_dic</span> <span class="op">=</span> <span class="nam">sprw</span><span class="op">.</span><span class="nam">read_in_age_by_uid</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">location</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">,</span> <span class="nam">folder_name</span><span class="op">,</span> <span class="nam">n</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t305" class="pln"><span class="n"><a href="#t305">305</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t306" class="run"><span class="n"><a href="#t306">306</a></span><span class="t"> <span class="key">if</span> <span class="nam">homes_by_uids</span> <span class="key">is</span> <span class="key">None</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t307" class="run"><span class="n"><a href="#t307">307</a></span><span class="t"> <span class="key">if</span> <span class="nam">folder_name</span> <span class="key">is</span> <span class="key">None</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t308" class="mis show_mis"><span class="n"><a href="#t308">308</a></span><span class="t"> <span class="nam">errormsg</span> <span class="op">=</span> <span class="str">f'The variable folder_name is not given. Please provide this variable so that synthpops knows where to read in the list of lists homes_by_uids'</span> </span><span class="r"></span></p>
<p id="t309" class="mis show_mis"><span class="n"><a href="#t309">309</a></span><span class="t"> <span class="key">raise</span> <span class="nam">ValueError</span><span class="op">(</span><span class="nam">errormsg</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t310" class="run"><span class="n"><a href="#t310">310</a></span><span class="t"> <span class="key">try</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t311" class="pln"><span class="n"><a href="#t311">311</a></span><span class="t"> <span class="com"># homes_by_uids = read_setting_groups(datadir, location, state_location, country_location, 'households', folder_name, n)</span> </span><span class="r"></span></p>
<p id="t312" class="run"><span class="n"><a href="#t312">312</a></span><span class="t"> <span class="nam">homes_by_uids</span> <span class="op">=</span> <span class="nam">sprw</span><span class="op">.</span><span class="nam">read_setting_groups</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">location</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">,</span> <span class="nam">setting</span><span class="op">=</span><span class="str">'households'</span><span class="op">,</span> <span class="nam">folder_name</span><span class="op">=</span><span class="nam">folder_name</span><span class="op">,</span> <span class="nam">n</span><span class="op">=</span><span class="nam">n</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t313" class="mis show_mis"><span class="n"><a href="#t313">313</a></span><span class="t"> <span class="key">except</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t314" class="mis show_mis"><span class="n"><a href="#t314">314</a></span><span class="t"> <span class="key">raise</span> <span class="nam">ValueError</span><span class="op">(</span><span class="str">'No households to bring in. Create people through those first.'</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t315" class="pln"><span class="n"><a href="#t315">315</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t316" class="pln"><span class="n"><a href="#t316">316</a></span><span class="t"> <span class="com"># # go through all people at random and make a list of uids going to school as students</span> </span><span class="r"></span></p>
<p id="t317" class="pln"><span class="n"><a href="#t317">317</a></span><span class="t"> <span class="com"># for uid in age_by_uid_dic:</span> </span><span class="r"></span></p>
<p id="t318" class="pln"><span class="n"><a href="#t318">318</a></span><span class="t"> <span class="com"># a = age_by_uid_dic[uid]</span> </span><span class="r"></span></p>
<p id="t319" class="pln"><span class="n"><a href="#t319">319</a></span><span class="t"> <span class="com"># if a <= 50:</span> </span><span class="r"></span></p>
<p id="t320" class="pln"><span class="n"><a href="#t320">320</a></span><span class="t"> <span class="com"># b = np.random.binomial(1,rates[a])</span> </span><span class="r"></span></p>
<p id="t321" class="pln"><span class="n"><a href="#t321">321</a></span><span class="t"> <span class="com"># if b:</span> </span><span class="r"></span></p>
<p id="t322" class="pln"><span class="n"><a href="#t322">322</a></span><span class="t"> <span class="com"># uids_in_school[uid] = a</span> </span><span class="r"></span></p>
<p id="t323" class="pln"><span class="n"><a href="#t323">323</a></span><span class="t"> <span class="com"># uids_in_school_by_age[a].append(uid)</span> </span><span class="r"></span></p>
<p id="t324" class="pln"><span class="n"><a href="#t324">324</a></span><span class="t"> <span class="com"># ages_in_school_count[a] += 1</span> </span><span class="r"></span></p>
<p id="t325" class="pln"><span class="n"><a href="#t325">325</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t326" class="pln"><span class="n"><a href="#t326">326</a></span><span class="t"> <span class="com"># go through homes and make a list of uids going to school as students, this should preserve ordering of students by homes and so create schools with siblings going to the same school</span> </span><span class="r"></span></p>
<p id="t327" class="run"><span class="n"><a href="#t327">327</a></span><span class="t"> <span class="key">for</span> <span class="nam">home</span> <span class="key">in</span> <span class="nam">homes_by_uids</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t328" class="run"><span class="n"><a href="#t328">328</a></span><span class="t"> <span class="key">for</span> <span class="nam">uid</span> <span class="key">in</span> <span class="nam">home</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t329" class="pln"><span class="n"><a href="#t329">329</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t330" class="run"><span class="n"><a href="#t330">330</a></span><span class="t"> <span class="nam">a</span> <span class="op">=</span> <span class="nam">age_by_uid_dic</span><span class="op">[</span><span class="nam">uid</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t331" class="run"><span class="n"><a href="#t331">331</a></span><span class="t"> <span class="key">if</span> <span class="nam">rates</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">></span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t332" class="run"><span class="n"><a href="#t332">332</a></span><span class="t"> <span class="nam">b</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">binomial</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="nam">rates</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span><span class="op">)</span> <span class="com"># ask each person if they'll be a student - probably could be done in a faster, more aggregate way.</span> </span><span class="r"></span></p>
<p id="t333" class="run"><span class="n"><a href="#t333">333</a></span><span class="t"> <span class="key">if</span> <span class="nam">b</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t334" class="run"><span class="n"><a href="#t334">334</a></span><span class="t"> <span class="nam">uids_in_school</span><span class="op">[</span><span class="nam">uid</span><span class="op">]</span> <span class="op">=</span> <span class="nam">a</span> </span><span class="r"></span></p>
<p id="t335" class="run"><span class="n"><a href="#t335">335</a></span><span class="t"> <span class="nam">uids_in_school_by_age</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t336" class="run"><span class="n"><a href="#t336">336</a></span><span class="t"> <span class="nam">ages_in_school_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">+=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t337" class="pln"><span class="n"><a href="#t337">337</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t338" class="run"><span class="n"><a href="#t338">338</a></span><span class="t"> <span class="key">return</span> <span class="nam">uids_in_school</span><span class="op">,</span> <span class="nam">uids_in_school_by_age</span><span class="op">,</span> <span class="nam">ages_in_school_count</span> </span><span class="r"></span></p>
<p id="t339" class="pln"><span class="n"><a href="#t339">339</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t340" class="pln"><span class="n"><a href="#t340">340</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t341" class="run"><span class="n"><a href="#t341">341</a></span><span class="t"><span class="key">def</span> <span class="nam">generate_school_sizes</span><span class="op">(</span><span class="nam">school_size_distr_by_bracket</span><span class="op">,</span> <span class="nam">school_size_brackets</span><span class="op">,</span> <span class="nam">uids_in_school</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t342" class="pln"><span class="n"><a href="#t342">342</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t343" class="pln"><span class="n"><a href="#t343">343</a></span><span class="t"><span class="str"> Given a number of students in school, generate a list of school sizes to place everyone in a school.</span> </span><span class="r"></span></p>
<p id="t344" class="pln"><span class="n"><a href="#t344">344</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t345" class="pln"><span class="n"><a href="#t345">345</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t346" class="pln"><span class="n"><a href="#t346">346</a></span><span class="t"><span class="str"> school_size_distr_by_bracket (dict) : The distribution of binned school sizes.</span> </span><span class="r"></span></p>
<p id="t347" class="pln"><span class="n"><a href="#t347">347</a></span><span class="t"><span class="str"> school_size_brackets (dict) : A dictionary of school size brackets.</span> </span><span class="r"></span></p>
<p id="t348" class="pln"><span class="n"><a href="#t348">348</a></span><span class="t"><span class="str"> uids_in_school (dict) : A dictionary of students in school mapping ID to age.</span> </span><span class="r"></span></p>
<p id="t349" class="pln"><span class="n"><a href="#t349">349</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t350" class="pln"><span class="n"><a href="#t350">350</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t351" class="pln"><span class="n"><a href="#t351">351</a></span><span class="t"><span class="str"> A list of school sizes whose sum is the length of ``uids_in_school``.</span> </span><span class="r"></span></p>
<p id="t352" class="pln"><span class="n"><a href="#t352">352</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t353" class="run"><span class="n"><a href="#t353">353</a></span><span class="t"> <span class="nam">ns</span> <span class="op">=</span> <span class="nam">len</span><span class="op">(</span><span class="nam">uids_in_school</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t354" class="run"><span class="n"><a href="#t354">354</a></span><span class="t"> <span class="nam">sorted_brackets</span> <span class="op">=</span> <span class="nam">sorted</span><span class="op">(</span><span class="nam">school_size_brackets</span><span class="op">.</span><span class="nam">keys</span><span class="op">(</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t355" class="run"><span class="n"><a href="#t355">355</a></span><span class="t"> <span class="nam">prob_by_sorted_brackets</span> <span class="op">=</span> <span class="op">[</span><span class="nam">school_size_distr_by_bracket</span><span class="op">[</span><span class="nam">b</span><span class="op">]</span> <span class="key">for</span> <span class="nam">b</span> <span class="key">in</span> <span class="nam">sorted_brackets</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t356" class="pln"><span class="n"><a href="#t356">356</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t357" class="run"><span class="n"><a href="#t357">357</a></span><span class="t"> <span class="nam">school_sizes</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t358" class="pln"><span class="n"><a href="#t358">358</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t359" class="run"><span class="n"><a href="#t359">359</a></span><span class="t"> <span class="key">while</span> <span class="nam">ns</span> <span class="op">></span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t360" class="run"><span class="n"><a href="#t360">360</a></span><span class="t"> <span class="nam">size_bracket</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">choice</span><span class="op">(</span><span class="nam">sorted_brackets</span><span class="op">,</span> <span class="nam">p</span><span class="op">=</span><span class="nam">prob_by_sorted_brackets</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t361" class="pln"><span class="n"><a href="#t361">361</a></span><span class="t"> <span class="com"># size = np.random.choice(school_size_brackets[size_bracket]) # creates some schools that are much smaller than expected so use average instead</span> </span><span class="r"></span></p>
<p id="t362" class="run"><span class="n"><a href="#t362">362</a></span><span class="t"> <span class="nam">size</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="nam">np</span><span class="op">.</span><span class="nam">mean</span><span class="op">(</span><span class="nam">school_size_brackets</span><span class="op">[</span><span class="nam">size_bracket</span><span class="op">]</span><span class="op">)</span><span class="op">)</span> <span class="com"># use average school size to avoid schools with very small sizes</span> </span><span class="r"></span></p>
<p id="t363" class="run"><span class="n"><a href="#t363">363</a></span><span class="t"> <span class="nam">ns</span> <span class="op">-=</span> <span class="nam">size</span> </span><span class="r"></span></p>
<p id="t364" class="run"><span class="n"><a href="#t364">364</a></span><span class="t"> <span class="nam">school_sizes</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">size</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t365" class="run"><span class="n"><a href="#t365">365</a></span><span class="t"> <span class="key">if</span> <span class="nam">ns</span> <span class="op"><</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t366" class="run"><span class="n"><a href="#t366">366</a></span><span class="t"> <span class="nam">school_sizes</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span> <span class="op">=</span> <span class="nam">school_sizes</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span> <span class="op">+</span> <span class="nam">ns</span> </span><span class="r"></span></p>
<p id="t367" class="run"><span class="n"><a href="#t367">367</a></span><span class="t"> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">shuffle</span><span class="op">(</span><span class="nam">school_sizes</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t368" class="run"><span class="n"><a href="#t368">368</a></span><span class="t"> <span class="key">return</span> <span class="nam">school_sizes</span> </span><span class="r"></span></p>
<p id="t369" class="pln"><span class="n"><a href="#t369">369</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t370" class="pln"><span class="n"><a href="#t370">370</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t371" class="run"><span class="n"><a href="#t371">371</a></span><span class="t"><span class="key">def</span> <span class="nam">send_students_to_school</span><span class="op">(</span><span class="nam">school_sizes</span><span class="op">,</span> <span class="nam">uids_in_school</span><span class="op">,</span> <span class="nam">uids_in_school_by_age</span><span class="op">,</span> <span class="nam">ages_in_school_count</span><span class="op">,</span> <span class="nam">age_brackets</span><span class="op">,</span> <span class="nam">age_by_brackets_dic</span><span class="op">,</span> <span class="nam">contact_matrix_dic</span><span class="op">,</span> <span class="nam">verbose</span><span class="op">=</span><span class="key">False</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t372" class="pln"><span class="n"><a href="#t372">372</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t373" class="pln"><span class="n"><a href="#t373">373</a></span><span class="t"><span class="str"> A method to send students to school together. Using the matrices to construct schools is not a perfect method so some things are more forced than the matrix method alone would create.</span> </span><span class="r"></span></p>
<p id="t374" class="pln"><span class="n"><a href="#t374">374</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t375" class="pln"><span class="n"><a href="#t375">375</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t376" class="pln"><span class="n"><a href="#t376">376</a></span><span class="t"><span class="str"> school_sizes (list): A list of school sizes.</span> </span><span class="r"></span></p>
<p id="t377" class="pln"><span class="n"><a href="#t377">377</a></span><span class="t"><span class="str"> uids_in_school (dict): A dictionary of students in school mapping ID to age.</span> </span><span class="r"></span></p>
<p id="t378" class="pln"><span class="n"><a href="#t378">378</a></span><span class="t"><span class="str"> uids_in_school_by_age (dict): A dictionary of students in school mapping age to the list of IDs with that age.</span> </span><span class="r"></span></p>
<p id="t379" class="pln"><span class="n"><a href="#t379">379</a></span><span class="t"><span class="str"> ages_in_school_count (dict): A dictionary mapping age to the number of students with that age.</span> </span><span class="r"></span></p>
<p id="t380" class="pln"><span class="n"><a href="#t380">380</a></span><span class="t"><span class="str"> age_brackets (dict) : A dictionary mapping age bracket keys to age bracket range.</span> </span><span class="r"></span></p>
<p id="t381" class="pln"><span class="n"><a href="#t381">381</a></span><span class="t"><span class="str"> age_by_brackets_dic (dict) : A dictionary mapping age to the age bracket range it falls within.</span> </span><span class="r"></span></p>
<p id="t382" class="pln"><span class="n"><a href="#t382">382</a></span><span class="t"><span class="str"> contact_matrix_dic (dict) : A dictionary of age specific contact matrix for different physical contact settings.</span> </span><span class="r"></span></p>
<p id="t383" class="pln"><span class="n"><a href="#t383">383</a></span><span class="t"><span class="str"> verbose (bool): If True, print statements about the generated schools as they're being generated.</span> </span><span class="r"></span></p>
<p id="t384" class="pln"><span class="n"><a href="#t384">384</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t385" class="pln"><span class="n"><a href="#t385">385</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t386" class="pln"><span class="n"><a href="#t386">386</a></span><span class="t"><span class="str"> Two lists of lists, the first where each sublist is the ages of students in the same school, and the second is the same list but with the IDs of each student in place of their age.</span> </span><span class="r"></span></p>
<p id="t387" class="pln"><span class="n"><a href="#t387">387</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t388" class="run"><span class="n"><a href="#t388">388</a></span><span class="t"> <span class="nam">syn_schools</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t389" class="run"><span class="n"><a href="#t389">389</a></span><span class="t"> <span class="nam">syn_school_uids</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t390" class="pln"><span class="n"><a href="#t390">390</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t391" class="run"><span class="n"><a href="#t391">391</a></span><span class="t"> <span class="nam">ages_in_school_distr</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">ages_in_school_count</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t392" class="run"><span class="n"><a href="#t392">392</a></span><span class="t"> <span class="nam">left_in_bracket</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">get_aggregate_ages</span><span class="op">(</span><span class="nam">ages_in_school_count</span><span class="op">,</span> <span class="nam">age_by_brackets_dic</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t393" class="pln"><span class="n"><a href="#t393">393</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t394" class="run"><span class="n"><a href="#t394">394</a></span><span class="t"> <span class="key">for</span> <span class="nam">n</span><span class="op">,</span> <span class="nam">size</span> <span class="key">in</span> <span class="nam">enumerate</span><span class="op">(</span><span class="nam">school_sizes</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t395" class="pln"><span class="n"><a href="#t395">395</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t396" class="run"><span class="n"><a href="#t396">396</a></span><span class="t"> <span class="key">if</span> <span class="nam">len</span><span class="op">(</span><span class="nam">uids_in_school</span><span class="op">)</span> <span class="op">==</span> <span class="num">0</span><span class="op">:</span> <span class="com"># no more students left to send to school!</span> </span><span class="r"></span></p>
<p id="t397" class="mis show_mis"><span class="n"><a href="#t397">397</a></span><span class="t"> <span class="key">break</span> </span><span class="r"></span></p>
<p id="t398" class="pln"><span class="n"><a href="#t398">398</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t399" class="run"><span class="n"><a href="#t399">399</a></span><span class="t"> <span class="nam">ages_in_school_distr</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">ages_in_school_count</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t400" class="pln"><span class="n"><a href="#t400">400</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t401" class="run"><span class="n"><a href="#t401">401</a></span><span class="t"> <span class="nam">new_school</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t402" class="run"><span class="n"><a href="#t402">402</a></span><span class="t"> <span class="nam">new_school_uids</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t403" class="pln"><span class="n"><a href="#t403">403</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t404" class="run"><span class="n"><a href="#t404">404</a></span><span class="t"> <span class="nam">achoice</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">multinomial</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="op">[</span><span class="nam">ages_in_school_distr</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">ages_in_school_distr</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t405" class="run"><span class="n"><a href="#t405">405</a></span><span class="t"> <span class="nam">aindex</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">where</span><span class="op">(</span><span class="nam">achoice</span><span class="op">)</span><span class="op">[</span><span class="num">0</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t406" class="run"><span class="n"><a href="#t406">406</a></span><span class="t"> <span class="nam">bindex</span> <span class="op">=</span> <span class="nam">age_by_brackets_dic</span><span class="op">[</span><span class="nam">aindex</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t407" class="pln"><span class="n"><a href="#t407">407</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t408" class="pln"><span class="n"><a href="#t408">408</a></span><span class="t"> <span class="com"># reference students under 20 to prevent older adults from being reference students (otherwise we end up with schools with too many adults and kids mixing because the matrices represent the average of the patterns and not the bimodal mixing of adult students together at school and a small number of teachers at school with their students)</span> </span><span class="r"></span></p>
<p id="t409" class="run"><span class="n"><a href="#t409">409</a></span><span class="t"> <span class="key">if</span> <span class="nam">bindex</span> <span class="op">>=</span> <span class="num">4</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t410" class="run"><span class="n"><a href="#t410">410</a></span><span class="t"> <span class="key">if</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">binomial</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="nam">p</span><span class="op">=</span><span class="num">0.7</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t411" class="run"><span class="n"><a href="#t411">411</a></span><span class="t"> <span class="nam">achoice</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">multinomial</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="op">[</span><span class="nam">ages_in_school_distr</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">ages_in_school_distr</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t412" class="run"><span class="n"><a href="#t412">412</a></span><span class="t"> <span class="nam">aindex</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">where</span><span class="op">(</span><span class="nam">achoice</span><span class="op">)</span><span class="op">[</span><span class="num">0</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t413" class="pln"><span class="n"><a href="#t413">413</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t414" class="run"><span class="n"><a href="#t414">414</a></span><span class="t"> <span class="nam">uid</span> <span class="op">=</span> <span class="nam">uids_in_school_by_age</span><span class="op">[</span><span class="nam">aindex</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t415" class="run"><span class="n"><a href="#t415">415</a></span><span class="t"> <span class="nam">uids_in_school_by_age</span><span class="op">[</span><span class="nam">aindex</span><span class="op">]</span><span class="op">.</span><span class="nam">remove</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t416" class="run"><span class="n"><a href="#t416">416</a></span><span class="t"> <span class="nam">uids_in_school</span><span class="op">.</span><span class="nam">pop</span><span class="op">(</span><span class="nam">uid</span><span class="op">,</span> <span class="key">None</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t417" class="run"><span class="n"><a href="#t417">417</a></span><span class="t"> <span class="nam">ages_in_school_count</span><span class="op">[</span><span class="nam">aindex</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t418" class="run"><span class="n"><a href="#t418">418</a></span><span class="t"> <span class="nam">ages_in_school_distr</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">ages_in_school_count</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t419" class="pln"><span class="n"><a href="#t419">419</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t420" class="run"><span class="n"><a href="#t420">420</a></span><span class="t"> <span class="nam">new_school</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">aindex</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t421" class="run"><span class="n"><a href="#t421">421</a></span><span class="t"> <span class="nam">new_school_uids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t422" class="pln"><span class="n"><a href="#t422">422</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t423" class="run"><span class="n"><a href="#t423">423</a></span><span class="t"> <span class="key">if</span> <span class="nam">verbose</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t424" class="mis show_mis"><span class="n"><a href="#t424">424</a></span><span class="t"> <span class="nam">print</span><span class="op">(</span><span class="str">'reference school age'</span><span class="op">,</span> <span class="nam">aindex</span><span class="op">,</span> <span class="str">'school size'</span><span class="op">,</span> <span class="nam">size</span><span class="op">,</span> <span class="str">'students left'</span><span class="op">,</span> <span class="nam">len</span><span class="op">(</span><span class="nam">uids_in_school</span><span class="op">)</span><span class="op">,</span> <span class="nam">left_in_bracket</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t425" class="pln"><span class="n"><a href="#t425">425</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t426" class="run"><span class="n"><a href="#t426">426</a></span><span class="t"> <span class="nam">bindex</span> <span class="op">=</span> <span class="nam">age_by_brackets_dic</span><span class="op">[</span><span class="nam">aindex</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t427" class="run"><span class="n"><a href="#t427">427</a></span><span class="t"> <span class="nam">b_prob</span> <span class="op">=</span> <span class="nam">contact_matrix_dic</span><span class="op">[</span><span class="str">'S'</span><span class="op">]</span><span class="op">[</span><span class="nam">bindex</span><span class="op">,</span> <span class="op">:</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t428" class="pln"><span class="n"><a href="#t428">428</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t429" class="run"><span class="n"><a href="#t429">429</a></span><span class="t"> <span class="nam">left_in_bracket</span><span class="op">[</span><span class="nam">bindex</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t430" class="pln"><span class="n"><a href="#t430">430</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t431" class="pln"><span class="n"><a href="#t431">431</a></span><span class="t"> <span class="com"># fewer students than school size so everyone else is in one school</span> </span><span class="r"></span></p>
<p id="t432" class="run"><span class="n"><a href="#t432">432</a></span><span class="t"> <span class="key">if</span> <span class="nam">len</span><span class="op">(</span><span class="nam">uids_in_school</span><span class="op">)</span> <span class="op"><</span> <span class="nam">size</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t433" class="run"><span class="n"><a href="#t433">433</a></span><span class="t"> <span class="key">for</span> <span class="nam">uid</span> <span class="key">in</span> <span class="nam">uids_in_school</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t434" class="run"><span class="n"><a href="#t434">434</a></span><span class="t"> <span class="nam">ai</span> <span class="op">=</span> <span class="nam">uids_in_school</span><span class="op">[</span><span class="nam">uid</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t435" class="run"><span class="n"><a href="#t435">435</a></span><span class="t"> <span class="nam">new_school</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">int</span><span class="op">(</span><span class="nam">ai</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t436" class="run"><span class="n"><a href="#t436">436</a></span><span class="t"> <span class="nam">new_school_uids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t437" class="run"><span class="n"><a href="#t437">437</a></span><span class="t"> <span class="nam">uids_in_school_by_age</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">.</span><span class="nam">remove</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t438" class="run"><span class="n"><a href="#t438">438</a></span><span class="t"> <span class="nam">ages_in_school_count</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t439" class="run"><span class="n"><a href="#t439">439</a></span><span class="t"> <span class="nam">left_in_bracket</span><span class="op">[</span><span class="nam">age_by_brackets_dic</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t440" class="run"><span class="n"><a href="#t440">440</a></span><span class="t"> <span class="nam">uids_in_school</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t441" class="run"><span class="n"><a href="#t441">441</a></span><span class="t"> <span class="key">if</span> <span class="nam">verbose</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t442" class="mis show_mis"><span class="n"><a href="#t442">442</a></span><span class="t"> <span class="nam">print</span><span class="op">(</span><span class="str">'last school'</span><span class="op">,</span> <span class="str">'size from distribution'</span><span class="op">,</span> <span class="nam">size</span><span class="op">,</span> <span class="str">'size generated'</span><span class="op">,</span> <span class="nam">len</span><span class="op">(</span><span class="nam">new_school</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t443" class="pln"><span class="n"><a href="#t443">443</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t444" class="pln"><span class="n"><a href="#t444">444</a></span><span class="t"> <span class="key">else</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t445" class="run"><span class="n"><a href="#t445">445</a></span><span class="t"> <span class="nam">bi_min</span> <span class="op">=</span> <span class="nam">max</span><span class="op">(</span><span class="num">0</span><span class="op">,</span> <span class="nam">bindex</span><span class="op">-</span><span class="num">1</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t446" class="run"><span class="n"><a href="#t446">446</a></span><span class="t"> <span class="nam">bi_max</span> <span class="op">=</span> <span class="nam">bindex</span> <span class="op">+</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t447" class="pln"><span class="n"><a href="#t447">447</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t448" class="run"><span class="n"><a href="#t448">448</a></span><span class="t"> <span class="key">for</span> <span class="nam">i</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="nam">size</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t449" class="run"><span class="n"><a href="#t449">449</a></span><span class="t"> <span class="key">if</span> <span class="nam">len</span><span class="op">(</span><span class="nam">uids_in_school</span><span class="op">)</span> <span class="op">==</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t450" class="mis show_mis"><span class="n"><a href="#t450">450</a></span><span class="t"> <span class="key">break</span> </span><span class="r"></span></p>
<p id="t451" class="pln"><span class="n"><a href="#t451">451</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t452" class="pln"><span class="n"><a href="#t452">452</a></span><span class="t"> <span class="com"># no one left to send? should only choose other students from the mixing matrices, not teachers so don't create schools with</span> </span><span class="r"></span></p>
<p id="t453" class="run"><span class="n"><a href="#t453">453</a></span><span class="t"> <span class="key">if</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">[</span><span class="nam">left_in_bracket</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span> <span class="key">for</span> <span class="nam">bi</span> <span class="key">in</span> <span class="nam">np</span><span class="op">.</span><span class="nam">arange</span><span class="op">(</span><span class="nam">bi_min</span><span class="op">,</span> <span class="nam">bi_max</span><span class="op">+</span><span class="num">1</span><span class="op">)</span><span class="op">]</span><span class="op">)</span> <span class="op">==</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t454" class="run"><span class="n"><a href="#t454">454</a></span><span class="t"> <span class="key">break</span> </span><span class="r"></span></p>
<p id="t455" class="pln"><span class="n"><a href="#t455">455</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t456" class="run"><span class="n"><a href="#t456">456</a></span><span class="t"> <span class="nam">bi</span> <span class="op">=</span> <span class="nam">spsamp</span><span class="op">.</span><span class="nam">sample_single_arr</span><span class="op">(</span><span class="nam">b_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t457" class="pln"><span class="n"><a href="#t457">457</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t458" class="run"><span class="n"><a href="#t458">458</a></span><span class="t"> <span class="key">while</span> <span class="nam">left_in_bracket</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span> <span class="op">==</span> <span class="num">0</span> <span class="key">or</span> <span class="nam">np</span><span class="op">.</span><span class="nam">abs</span><span class="op">(</span><span class="nam">bindex</span> <span class="op">-</span> <span class="nam">bi</span><span class="op">)</span> <span class="op">></span> <span class="num">1</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t459" class="run"><span class="n"><a href="#t459">459</a></span><span class="t"> <span class="nam">bi</span> <span class="op">=</span> <span class="nam">spsamp</span><span class="op">.</span><span class="nam">sample_single_arr</span><span class="op">(</span><span class="nam">b_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t460" class="pln"><span class="n"><a href="#t460">460</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t461" class="run"><span class="n"><a href="#t461">461</a></span><span class="t"> <span class="nam">ai</span> <span class="op">=</span> <span class="nam">spsamp</span><span class="op">.</span><span class="nam">sample_from_range</span><span class="op">(</span><span class="nam">ages_in_school_distr</span><span class="op">,</span> <span class="nam">age_brackets</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span><span class="op">,</span> <span class="nam">age_brackets</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t462" class="run"><span class="n"><a href="#t462">462</a></span><span class="t"> <span class="nam">uid</span> <span class="op">=</span> <span class="nam">uids_in_school_by_age</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> <span class="com"># grab the next student in line</span> </span><span class="r"></span></p>
<p id="t463" class="pln"><span class="n"><a href="#t463">463</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t464" class="run"><span class="n"><a href="#t464">464</a></span><span class="t"> <span class="nam">new_school</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">ai</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t465" class="run"><span class="n"><a href="#t465">465</a></span><span class="t"> <span class="nam">new_school_uids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t466" class="pln"><span class="n"><a href="#t466">466</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t467" class="run"><span class="n"><a href="#t467">467</a></span><span class="t"> <span class="nam">uids_in_school_by_age</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">.</span><span class="nam">remove</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t468" class="run"><span class="n"><a href="#t468">468</a></span><span class="t"> <span class="nam">uids_in_school</span><span class="op">.</span><span class="nam">pop</span><span class="op">(</span><span class="nam">uid</span><span class="op">,</span> <span class="key">None</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t469" class="pln"><span class="n"><a href="#t469">469</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t470" class="run"><span class="n"><a href="#t470">470</a></span><span class="t"> <span class="nam">ages_in_school_count</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t471" class="run"><span class="n"><a href="#t471">471</a></span><span class="t"> <span class="nam">ages_in_school_distr</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">ages_in_school_count</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t472" class="run"><span class="n"><a href="#t472">472</a></span><span class="t"> <span class="nam">left_in_bracket</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t473" class="pln"><span class="n"><a href="#t473">473</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t474" class="run"><span class="n"><a href="#t474">474</a></span><span class="t"> <span class="nam">syn_schools</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">new_school</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t475" class="run"><span class="n"><a href="#t475">475</a></span><span class="t"> <span class="nam">syn_school_uids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">new_school_uids</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t476" class="run"><span class="n"><a href="#t476">476</a></span><span class="t"> <span class="nam">new_school</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">array</span><span class="op">(</span><span class="nam">new_school</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t477" class="run"><span class="n"><a href="#t477">477</a></span><span class="t"> <span class="nam">kids</span> <span class="op">=</span> <span class="nam">new_school</span> <span class="op"><=</span> <span class="num">19</span> </span><span class="r"></span></p>
<p id="t478" class="pln"><span class="n"><a href="#t478">478</a></span><span class="t"> <span class="com"># new_school_age_counter = Counter(new_school)</span> </span><span class="r"></span></p>
<p id="t479" class="run"><span class="n"><a href="#t479">479</a></span><span class="t"> <span class="key">if</span> <span class="nam">verbose</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t480" class="mis show_mis"><span class="n"><a href="#t480">480</a></span><span class="t"> <span class="nam">print</span><span class="op">(</span><span class="str">'new school ages'</span><span class="op">,</span> <span class="nam">len</span><span class="op">(</span><span class="nam">new_school</span><span class="op">)</span><span class="op">,</span> <span class="nam">sorted</span><span class="op">(</span><span class="nam">new_school</span><span class="op">)</span><span class="op">,</span> <span class="str">'nkids'</span><span class="op">,</span> <span class="nam">kids</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">)</span><span class="op">,</span> <span class="str">'n20+'</span><span class="op">,</span> <span class="nam">len</span><span class="op">(</span><span class="nam">new_school</span><span class="op">)</span><span class="op">-</span><span class="nam">kids</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">)</span><span class="op">,</span> <span class="str">'kid-adult ratio'</span><span class="op">,</span> <span class="nam">kids</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">)</span><span class="op">/</span><span class="op">(</span><span class="nam">len</span><span class="op">(</span><span class="nam">new_school</span><span class="op">)</span><span class="op">-</span><span class="nam">kids</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t481" class="run"><span class="n"><a href="#t481">481</a></span><span class="t"> <span class="key">if</span> <span class="nam">verbose</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t482" class="mis show_mis"><span class="n"><a href="#t482">482</a></span><span class="t"> <span class="nam">print</span><span class="op">(</span><span class="str">'people in school'</span><span class="op">,</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">[</span><span class="nam">len</span><span class="op">(</span><span class="nam">school</span><span class="op">)</span> <span class="key">for</span> <span class="nam">school</span> <span class="key">in</span> <span class="nam">syn_schools</span><span class="op">]</span><span class="op">)</span><span class="op">,</span> <span class="str">'left to send'</span><span class="op">,</span> <span class="nam">len</span><span class="op">(</span><span class="nam">uids_in_school</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t483" class="run"><span class="n"><a href="#t483">483</a></span><span class="t"> <span class="key">return</span> <span class="nam">syn_schools</span><span class="op">,</span> <span class="nam">syn_school_uids</span> </span><span class="r"></span></p>
<p id="t484" class="pln"><span class="n"><a href="#t484">484</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t485" class="pln"><span class="n"><a href="#t485">485</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t486" class="run"><span class="n"><a href="#t486">486</a></span><span class="t"><span class="key">def</span> <span class="nam">get_uids_potential_workers</span><span class="op">(</span><span class="nam">syn_school_uids</span><span class="op">,</span> <span class="nam">employment_rates</span><span class="op">,</span> <span class="nam">age_by_uid_dic</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t487" class="pln"><span class="n"><a href="#t487">487</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t488" class="pln"><span class="n"><a href="#t488">488</a></span><span class="t"><span class="str"> Get IDs for everyone who could be a worker by removing those who are students and those who can't be employed officially.</span> </span><span class="r"></span></p>
<p id="t489" class="pln"><span class="n"><a href="#t489">489</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t490" class="pln"><span class="n"><a href="#t490">490</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t491" class="pln"><span class="n"><a href="#t491">491</a></span><span class="t"><span class="str"> syn_school_uids (list) : A list of lists where each sublist represents a school with the IDs of students in the school.</span> </span><span class="r"></span></p>
<p id="t492" class="pln"><span class="n"><a href="#t492">492</a></span><span class="t"><span class="str"> employment_rates (dict) : The employment rates by age.</span> </span><span class="r"></span></p>
<p id="t493" class="pln"><span class="n"><a href="#t493">493</a></span><span class="t"><span class="str"> age_by_uid_dic (dict) : A dictionary mapping ID to age for individuals in the population.</span> </span><span class="r"></span></p>
<p id="t494" class="pln"><span class="n"><a href="#t494">494</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t495" class="pln"><span class="n"><a href="#t495">495</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t496" class="pln"><span class="n"><a href="#t496">496</a></span><span class="t"><span class="str"> A dictionary of potential workers mapping their ID to their age, a dictionary mapping age to the list of IDs for potential</span> </span><span class="r"></span></p>
<p id="t497" class="pln"><span class="n"><a href="#t497">497</a></span><span class="t"><span class="str"> workers with that age, and a dictionary mapping age to the count of potential workers left to assign to a workplace for that age.</span> </span><span class="r"></span></p>
<p id="t498" class="pln"><span class="n"><a href="#t498">498</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t499" class="run"><span class="n"><a href="#t499">499</a></span><span class="t"> <span class="nam">potential_worker_uids</span> <span class="op">=</span> <span class="nam">deepcopy</span><span class="op">(</span><span class="nam">age_by_uid_dic</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t500" class="run"><span class="n"><a href="#t500">500</a></span><span class="t"> <span class="nam">potential_worker_uids_by_age</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t501" class="run"><span class="n"><a href="#t501">501</a></span><span class="t"> <span class="nam">potential_worker_ages_left_count</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t502" class="pln"><span class="n"><a href="#t502">502</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t503" class="run"><span class="n"><a href="#t503">503</a></span><span class="t"> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="num">101</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t504" class="run"><span class="n"><a href="#t504">504</a></span><span class="t"> <span class="key">if</span> <span class="nam">a</span> <span class="op">>=</span> <span class="num">15</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t505" class="run"><span class="n"><a href="#t505">505</a></span><span class="t"> <span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t506" class="run"><span class="n"><a href="#t506">506</a></span><span class="t"> <span class="nam">potential_worker_ages_left_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">=</span> <span class="num">0</span> </span><span class="r"></span></p>
<p id="t507" class="pln"><span class="n"><a href="#t507">507</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t508" class="pln"><span class="n"><a href="#t508">508</a></span><span class="t"> <span class="com"># remove students from any potential workers since the model assumes student and worker status are exclusive</span> </span><span class="r"></span></p>
<p id="t509" class="run"><span class="n"><a href="#t509">509</a></span><span class="t"> <span class="key">for</span> <span class="nam">school</span> <span class="key">in</span> <span class="nam">syn_school_uids</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t510" class="run"><span class="n"><a href="#t510">510</a></span><span class="t"> <span class="key">for</span> <span class="nam">uid</span> <span class="key">in</span> <span class="nam">school</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t511" class="run"><span class="n"><a href="#t511">511</a></span><span class="t"> <span class="nam">potential_worker_uids</span><span class="op">.</span><span class="nam">pop</span><span class="op">(</span><span class="nam">uid</span><span class="op">,</span> <span class="key">None</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t512" class="pln"><span class="n"><a href="#t512">512</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t513" class="run"><span class="n"><a href="#t513">513</a></span><span class="t"> <span class="key">for</span> <span class="nam">uid</span> <span class="key">in</span> <span class="nam">age_by_uid_dic</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t514" class="run"><span class="n"><a href="#t514">514</a></span><span class="t"> <span class="key">if</span> <span class="nam">age_by_uid_dic</span><span class="op">[</span><span class="nam">uid</span><span class="op">]</span> <span class="key">not</span> <span class="key">in</span> <span class="nam">employment_rates</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t515" class="run"><span class="n"><a href="#t515">515</a></span><span class="t"> <span class="nam">potential_worker_uids</span><span class="op">.</span><span class="nam">pop</span><span class="op">(</span><span class="nam">uid</span><span class="op">,</span> <span class="key">None</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t516" class="pln"><span class="n"><a href="#t516">516</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t517" class="run"><span class="n"><a href="#t517">517</a></span><span class="t"> <span class="key">for</span> <span class="nam">uid</span> <span class="key">in</span> <span class="nam">potential_worker_uids</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t518" class="run"><span class="n"><a href="#t518">518</a></span><span class="t"> <span class="nam">ai</span> <span class="op">=</span> <span class="nam">potential_worker_uids</span><span class="op">[</span><span class="nam">uid</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t519" class="run"><span class="n"><a href="#t519">519</a></span><span class="t"> <span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t520" class="run"><span class="n"><a href="#t520">520</a></span><span class="t"> <span class="nam">potential_worker_ages_left_count</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span> <span class="op">+=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t521" class="pln"><span class="n"><a href="#t521">521</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t522" class="pln"><span class="n"><a href="#t522">522</a></span><span class="t"> <span class="com"># shuffle workers around!</span> </span><span class="r"></span></p>
<p id="t523" class="run"><span class="n"><a href="#t523">523</a></span><span class="t"> <span class="key">for</span> <span class="nam">ai</span> <span class="key">in</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t524" class="run"><span class="n"><a href="#t524">524</a></span><span class="t"> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">shuffle</span><span class="op">(</span><span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t525" class="pln"><span class="n"><a href="#t525">525</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t526" class="run"><span class="n"><a href="#t526">526</a></span><span class="t"> <span class="key">return</span> <span class="nam">potential_worker_uids</span><span class="op">,</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">,</span> <span class="nam">potential_worker_ages_left_count</span> </span><span class="r"></span></p>
<p id="t527" class="pln"><span class="n"><a href="#t527">527</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t528" class="pln"><span class="n"><a href="#t528">528</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t529" class="run"><span class="n"><a href="#t529">529</a></span><span class="t"><span class="key">def</span> <span class="nam">generate_workplace_sizes</span><span class="op">(</span><span class="nam">workplace_size_distr_by_bracket</span><span class="op">,</span> <span class="nam">workplace_size_brackets</span><span class="op">,</span> <span class="nam">workers_by_age_to_assign_count</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t530" class="pln"><span class="n"><a href="#t530">530</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t531" class="pln"><span class="n"><a href="#t531">531</a></span><span class="t"><span class="str"> Given a number of individuals employed, generate a list of workplace sizes to place everyone in a workplace.</span> </span><span class="r"></span></p>
<p id="t532" class="pln"><span class="n"><a href="#t532">532</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t533" class="pln"><span class="n"><a href="#t533">533</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t534" class="pln"><span class="n"><a href="#t534">534</a></span><span class="t"><span class="str"> workplace_size_distr_by_bracket (dict) : The distribution of binned workplace sizes.</span> </span><span class="r"></span></p>
<p id="t535" class="pln"><span class="n"><a href="#t535">535</a></span><span class="t"><span class="str"> worplace_size_brackets (dict) : A dictionary of workplace size brackets.</span> </span><span class="r"></span></p>
<p id="t536" class="pln"><span class="n"><a href="#t536">536</a></span><span class="t"><span class="str"> workers_by_age_to_assign_count (dict) : A dictionary mapping age to the count of employed individuals of that age.</span> </span><span class="r"></span></p>
<p id="t537" class="pln"><span class="n"><a href="#t537">537</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t538" class="pln"><span class="n"><a href="#t538">538</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t539" class="pln"><span class="n"><a href="#t539">539</a></span><span class="t"><span class="str"> A list of workplace sizes.</span> </span><span class="r"></span></p>
<p id="t540" class="pln"><span class="n"><a href="#t540">540</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t541" class="run"><span class="n"><a href="#t541">541</a></span><span class="t"> <span class="nam">nworkers</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">[</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">workers_by_age_to_assign_count</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t542" class="pln"><span class="n"><a href="#t542">542</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t543" class="pln"><span class="n"><a href="#t543">543</a></span><span class="t"> <span class="com"># normalize workplace_size_distr_by_bracket because it's likely a count rather than distribution</span> </span><span class="r"></span></p>
<p id="t544" class="run"><span class="n"><a href="#t544">544</a></span><span class="t"> <span class="nam">workplace_size_distr_by_bracket</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">workplace_size_distr_by_bracket</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t545" class="pln"><span class="n"><a href="#t545">545</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t546" class="run"><span class="n"><a href="#t546">546</a></span><span class="t"> <span class="nam">sorted_brackets</span> <span class="op">=</span> <span class="nam">sorted</span><span class="op">(</span><span class="nam">workplace_size_brackets</span><span class="op">.</span><span class="nam">keys</span><span class="op">(</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t547" class="run"><span class="n"><a href="#t547">547</a></span><span class="t"> <span class="nam">prob_by_sorted_brackets</span> <span class="op">=</span> <span class="op">[</span><span class="nam">workplace_size_distr_by_bracket</span><span class="op">[</span><span class="nam">b</span><span class="op">]</span> <span class="key">for</span> <span class="nam">b</span> <span class="key">in</span> <span class="nam">sorted_brackets</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t548" class="pln"><span class="n"><a href="#t548">548</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t549" class="run"><span class="n"><a href="#t549">549</a></span><span class="t"> <span class="nam">workplace_sizes</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t550" class="pln"><span class="n"><a href="#t550">550</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t551" class="run"><span class="n"><a href="#t551">551</a></span><span class="t"> <span class="key">while</span> <span class="nam">nworkers</span> <span class="op">></span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t552" class="run"><span class="n"><a href="#t552">552</a></span><span class="t"> <span class="nam">size_bracket</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">choice</span><span class="op">(</span><span class="nam">sorted_brackets</span><span class="op">,</span> <span class="nam">p</span><span class="op">=</span><span class="nam">prob_by_sorted_brackets</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t553" class="run"><span class="n"><a href="#t553">553</a></span><span class="t"> <span class="nam">size</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">choice</span><span class="op">(</span><span class="nam">workplace_size_brackets</span><span class="op">[</span><span class="nam">size_bracket</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t554" class="run"><span class="n"><a href="#t554">554</a></span><span class="t"> <span class="nam">nworkers</span> <span class="op">-=</span> <span class="nam">size</span> </span><span class="r"></span></p>
<p id="t555" class="run"><span class="n"><a href="#t555">555</a></span><span class="t"> <span class="nam">workplace_sizes</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">size</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t556" class="run"><span class="n"><a href="#t556">556</a></span><span class="t"> <span class="key">if</span> <span class="nam">nworkers</span> <span class="op"><</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t557" class="run"><span class="n"><a href="#t557">557</a></span><span class="t"> <span class="nam">workplace_sizes</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span> <span class="op">=</span> <span class="nam">workplace_sizes</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span> <span class="op">+</span> <span class="nam">nworkers</span> </span><span class="r"></span></p>
<p id="t558" class="run"><span class="n"><a href="#t558">558</a></span><span class="t"> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">shuffle</span><span class="op">(</span><span class="nam">workplace_sizes</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t559" class="run"><span class="n"><a href="#t559">559</a></span><span class="t"> <span class="key">return</span> <span class="nam">workplace_sizes</span> </span><span class="r"></span></p>
<p id="t560" class="pln"><span class="n"><a href="#t560">560</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t561" class="pln"><span class="n"><a href="#t561">561</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t562" class="run"><span class="n"><a href="#t562">562</a></span><span class="t"><span class="key">def</span> <span class="nam">generate_usa_workplace_sizes</span><span class="op">(</span><span class="nam">workplace_sizes_by_bracket</span><span class="op">,</span> <span class="nam">workplace_size_brackets</span><span class="op">,</span> <span class="nam">workers_by_age_to_assign_count</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t563" class="pln"><span class="n"><a href="#t563">563</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t564" class="pln"><span class="n"><a href="#t564">564</a></span><span class="t"><span class="str"> Given a number of individuals employed, generate a list of workplace sizes to place everyone in a workplace.</span> </span><span class="r"></span></p>
<p id="t565" class="pln"><span class="n"><a href="#t565">565</a></span><span class="t"><span class="str"> Specific to data from the US.</span> </span><span class="r"></span></p>
<p id="t566" class="pln"><span class="n"><a href="#t566">566</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t567" class="pln"><span class="n"><a href="#t567">567</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t568" class="pln"><span class="n"><a href="#t568">568</a></span><span class="t"><span class="str"> workplace_sizes_by_bracket (dict) : The distribution of binned workplace sizes.</span> </span><span class="r"></span></p>
<p id="t569" class="pln"><span class="n"><a href="#t569">569</a></span><span class="t"><span class="str"> worplace_size_brackets (dict) : A dictionary of workplace size brackets.</span> </span><span class="r"></span></p>
<p id="t570" class="pln"><span class="n"><a href="#t570">570</a></span><span class="t"><span class="str"> workers_by_age_to_assign_count (dict) : A dictionary mapping age to the count of employed individuals of that age.</span> </span><span class="r"></span></p>
<p id="t571" class="pln"><span class="n"><a href="#t571">571</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t572" class="pln"><span class="n"><a href="#t572">572</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t573" class="pln"><span class="n"><a href="#t573">573</a></span><span class="t"><span class="str"> A list of workplace sizes.</span> </span><span class="r"></span></p>
<p id="t574" class="pln"><span class="n"><a href="#t574">574</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t575" class="mis show_mis"><span class="n"><a href="#t575">575</a></span><span class="t"> <span class="nam">nw</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">[</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">workers_by_age_to_assign_count</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t576" class="pln"><span class="n"><a href="#t576">576</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t577" class="mis show_mis"><span class="n"><a href="#t577">577</a></span><span class="t"> <span class="nam">size_distr</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t578" class="mis show_mis"><span class="n"><a href="#t578">578</a></span><span class="t"> <span class="key">for</span> <span class="nam">b</span> <span class="key">in</span> <span class="nam">workplace_size_brackets</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t579" class="mis show_mis"><span class="n"><a href="#t579">579</a></span><span class="t"> <span class="nam">size</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="nam">np</span><span class="op">.</span><span class="nam">mean</span><span class="op">(</span><span class="nam">workplace_size_brackets</span><span class="op">[</span><span class="nam">b</span><span class="op">]</span><span class="op">)</span> <span class="op">+</span> <span class="num">0.5</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t580" class="mis show_mis"><span class="n"><a href="#t580">580</a></span><span class="t"> <span class="nam">size_distr</span><span class="op">[</span><span class="nam">size</span><span class="op">]</span> <span class="op">=</span> <span class="nam">workplace_sizes_by_bracket</span><span class="op">[</span><span class="nam">b</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t581" class="pln"><span class="n"><a href="#t581">581</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t582" class="mis show_mis"><span class="n"><a href="#t582">582</a></span><span class="t"> <span class="nam">size_distr</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">size_distr</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t583" class="mis show_mis"><span class="n"><a href="#t583">583</a></span><span class="t"> <span class="nam">workplace_sizes</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t584" class="pln"><span class="n"><a href="#t584">584</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t585" class="mis show_mis"><span class="n"><a href="#t585">585</a></span><span class="t"> <span class="nam">s_range</span> <span class="op">=</span> <span class="nam">sorted</span><span class="op">(</span><span class="nam">size_distr</span><span class="op">.</span><span class="nam">keys</span><span class="op">(</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t586" class="mis show_mis"><span class="n"><a href="#t586">586</a></span><span class="t"> <span class="nam">p</span> <span class="op">=</span> <span class="op">[</span><span class="nam">size_distr</span><span class="op">[</span><span class="nam">s</span><span class="op">]</span> <span class="key">for</span> <span class="nam">s</span> <span class="key">in</span> <span class="nam">s_range</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t587" class="pln"><span class="n"><a href="#t587">587</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t588" class="mis show_mis"><span class="n"><a href="#t588">588</a></span><span class="t"> <span class="key">while</span> <span class="nam">nw</span> <span class="op">></span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t589" class="mis show_mis"><span class="n"><a href="#t589">589</a></span><span class="t"> <span class="nam">s</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">choice</span><span class="op">(</span><span class="nam">s_range</span><span class="op">,</span> <span class="nam">p</span><span class="op">=</span><span class="nam">p</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t590" class="mis show_mis"><span class="n"><a href="#t590">590</a></span><span class="t"> <span class="nam">nw</span> <span class="op">-=</span> <span class="nam">s</span> </span><span class="r"></span></p>
<p id="t591" class="mis show_mis"><span class="n"><a href="#t591">591</a></span><span class="t"> <span class="nam">workplace_sizes</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">s</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t592" class="pln"><span class="n"><a href="#t592">592</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t593" class="mis show_mis"><span class="n"><a href="#t593">593</a></span><span class="t"> <span class="key">if</span> <span class="nam">nw</span> <span class="op"><</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t594" class="mis show_mis"><span class="n"><a href="#t594">594</a></span><span class="t"> <span class="nam">workplace_sizes</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span> <span class="op">=</span> <span class="nam">workplace_sizes</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span> <span class="op">+</span> <span class="nam">nw</span> </span><span class="r"></span></p>
<p id="t595" class="pln"><span class="n"><a href="#t595">595</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t596" class="mis show_mis"><span class="n"><a href="#t596">596</a></span><span class="t"> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">shuffle</span><span class="op">(</span><span class="nam">workplace_sizes</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t597" class="mis show_mis"><span class="n"><a href="#t597">597</a></span><span class="t"> <span class="key">return</span> <span class="nam">workplace_sizes</span> </span><span class="r"></span></p>
<p id="t598" class="pln"><span class="n"><a href="#t598">598</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t599" class="pln"><span class="n"><a href="#t599">599</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t600" class="run"><span class="n"><a href="#t600">600</a></span><span class="t"><span class="key">def</span> <span class="nam">get_workers_by_age_to_assign</span><span class="op">(</span><span class="nam">employment_rates</span><span class="op">,</span> <span class="nam">potential_worker_ages_left_count</span><span class="op">,</span> <span class="nam">uids_by_age_dic</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t601" class="pln"><span class="n"><a href="#t601">601</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t602" class="pln"><span class="n"><a href="#t602">602</a></span><span class="t"><span class="str"> Get the number of people to assign to a workplace by age using those left who can potentially go to work and employment rates by age.</span> </span><span class="r"></span></p>
<p id="t603" class="pln"><span class="n"><a href="#t603">603</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t604" class="pln"><span class="n"><a href="#t604">604</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t605" class="pln"><span class="n"><a href="#t605">605</a></span><span class="t"><span class="str"> employment_rates (dict) : A dictionary of employment rates by age.</span> </span><span class="r"></span></p>
<p id="t606" class="pln"><span class="n"><a href="#t606">606</a></span><span class="t"><span class="str"> potential_worker_ages_left_count (dict) : A dictionary of the count of workers to assign by age.</span> </span><span class="r"></span></p>
<p id="t607" class="pln"><span class="n"><a href="#t607">607</a></span><span class="t"><span class="str"> uids_by_age_dic (dict) : A dictionary mapping age to the list of ids with that age.</span> </span><span class="r"></span></p>
<p id="t608" class="pln"><span class="n"><a href="#t608">608</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t609" class="pln"><span class="n"><a href="#t609">609</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t610" class="pln"><span class="n"><a href="#t610">610</a></span><span class="t"><span class="str"> A dictionary with a count of workers to assign to a workplace.</span> </span><span class="r"></span></p>
<p id="t611" class="pln"><span class="n"><a href="#t611">611</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t612" class="pln"><span class="n"><a href="#t612">612</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t613" class="run"><span class="n"><a href="#t613">613</a></span><span class="t"> <span class="nam">workers_by_age_to_assign_count</span> <span class="op">=</span> <span class="nam">dict</span><span class="op">.</span><span class="nam">fromkeys</span><span class="op">(</span><span class="nam">np</span><span class="op">.</span><span class="nam">arange</span><span class="op">(</span><span class="num">101</span><span class="op">)</span><span class="op">,</span> <span class="num">0</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t614" class="run"><span class="n"><a href="#t614">614</a></span><span class="t"> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">potential_worker_ages_left_count</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t615" class="run"><span class="n"><a href="#t615">615</a></span><span class="t"> <span class="key">if</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">employment_rates</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t616" class="run"><span class="n"><a href="#t616">616</a></span><span class="t"> <span class="key">try</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t617" class="run"><span class="n"><a href="#t617">617</a></span><span class="t"> <span class="nam">c</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="nam">employment_rates</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">*</span> <span class="nam">len</span><span class="op">(</span><span class="nam">uids_by_age_dic</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t618" class="run"><span class="n"><a href="#t618">618</a></span><span class="t"> <span class="key">except</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t619" class="run"><span class="n"><a href="#t619">619</a></span><span class="t"> <span class="nam">c</span> <span class="op">=</span> <span class="num">0</span> </span><span class="r"></span></p>
<p id="t620" class="run"><span class="n"><a href="#t620">620</a></span><span class="t"> <span class="nam">number_of_people_who_can_be_assigned</span> <span class="op">=</span> <span class="nam">min</span><span class="op">(</span><span class="nam">c</span><span class="op">,</span> <span class="nam">potential_worker_ages_left_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t621" class="run"><span class="n"><a href="#t621">621</a></span><span class="t"> <span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">=</span> <span class="nam">number_of_people_who_can_be_assigned</span> </span><span class="r"></span></p>
<p id="t622" class="pln"><span class="n"><a href="#t622">622</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t623" class="run"><span class="n"><a href="#t623">623</a></span><span class="t"> <span class="key">return</span> <span class="nam">workers_by_age_to_assign_count</span> </span><span class="r"></span></p>
<p id="t624" class="pln"><span class="n"><a href="#t624">624</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t625" class="pln"><span class="n"><a href="#t625">625</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t626" class="run"><span class="n"><a href="#t626">626</a></span><span class="t"><span class="key">def</span> <span class="nam">assign_teachers_to_work</span><span class="op">(</span><span class="nam">syn_schools</span><span class="op">,</span> <span class="nam">syn_school_uids</span><span class="op">,</span> <span class="nam">employment_rates</span><span class="op">,</span> <span class="nam">workers_by_age_to_assign_count</span><span class="op">,</span> <span class="nam">potential_worker_uids</span><span class="op">,</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">,</span> <span class="nam">potential_worker_ages_left_count</span><span class="op">,</span> <span class="nam">student_teacher_ratio</span><span class="op">=</span><span class="num">20</span><span class="op">,</span> <span class="nam">teacher_age_min</span><span class="op">=</span><span class="num">25</span><span class="op">,</span> <span class="nam">teacher_age_max</span><span class="op">=</span><span class="num">75</span><span class="op">,</span> <span class="nam">verbose</span><span class="op">=</span><span class="key">False</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t627" class="pln"><span class="n"><a href="#t627">627</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t628" class="pln"><span class="n"><a href="#t628">628</a></span><span class="t"><span class="str"> Assign teachers to each school according to the average student-teacher ratio.</span> </span><span class="r"></span></p>
<p id="t629" class="pln"><span class="n"><a href="#t629">629</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t630" class="pln"><span class="n"><a href="#t630">630</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t631" class="pln"><span class="n"><a href="#t631">631</a></span><span class="t"><span class="str"> syn_schools (list): list of lists where each sublist is a school with the ages of the students within</span> </span><span class="r"></span></p>
<p id="t632" class="pln"><span class="n"><a href="#t632">632</a></span><span class="t"><span class="str"> syn_school_uids (list): list of lists where each sublist is a school with the ids of the students within</span> </span><span class="r"></span></p>
<p id="t633" class="pln"><span class="n"><a href="#t633">633</a></span><span class="t"><span class="str"> employment_rates (dict): employment rates by age</span> </span><span class="r"></span></p>
<p id="t634" class="pln"><span class="n"><a href="#t634">634</a></span><span class="t"><span class="str"> workers_by_age_to_assign_count (dict): dictionary of the count of workers left to assign by age</span> </span><span class="r"></span></p>
<p id="t635" class="pln"><span class="n"><a href="#t635">635</a></span><span class="t"><span class="str"> potential_worker_uids (dict): dictionary of potential workers mapping their id to their age</span> </span><span class="r"></span></p>
<p id="t636" class="pln"><span class="n"><a href="#t636">636</a></span><span class="t"><span class="str"> potential_worker_uids_by_age (dict): dictionary mapping age to the list of worker ids with that age</span> </span><span class="r"></span></p>
<p id="t637" class="pln"><span class="n"><a href="#t637">637</a></span><span class="t"><span class="str"> potential_worker_ages_left_count (dict): dictionary of the count of potential workers left that can be assigned by age</span> </span><span class="r"></span></p>
<p id="t638" class="pln"><span class="n"><a href="#t638">638</a></span><span class="t"><span class="str"> student_teacher_ratio (int): average student teacher ratio</span> </span><span class="r"></span></p>
<p id="t639" class="pln"><span class="n"><a href="#t639">639</a></span><span class="t"><span class="str"> teacher_age_min (int): minimum age for teachers - should be location specific</span> </span><span class="r"></span></p>
<p id="t640" class="pln"><span class="n"><a href="#t640">640</a></span><span class="t"><span class="str"> teacher_age_max (int): maximum age for teachers - should be location specific</span> </span><span class="r"></span></p>
<p id="t641" class="pln"><span class="n"><a href="#t641">641</a></span><span class="t"><span class="str"> verbose (bool): If True, print statements about the generated schools as teachers are being added to each school.</span> </span><span class="r"></span></p>
<p id="t642" class="pln"><span class="n"><a href="#t642">642</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t643" class="pln"><span class="n"><a href="#t643">643</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t644" class="pln"><span class="n"><a href="#t644">644</a></span><span class="t"><span class="str"> List of lists of schools with the ages of individuals in each, lists of lists of schools with the ids of individuals in each,</span> </span><span class="r"></span></p>
<p id="t645" class="pln"><span class="n"><a href="#t645">645</a></span><span class="t"><span class="str"> dictionary of potential workers mapping id to their age, dictionary mapping age to the list of potential workers of that age,</span> </span><span class="r"></span></p>
<p id="t646" class="pln"><span class="n"><a href="#t646">646</a></span><span class="t"><span class="str"> dictionary with the count of workers left to assign for each age after teachers have been assigned.</span> </span><span class="r"></span></p>
<p id="t647" class="pln"><span class="n"><a href="#t647">647</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t648" class="pln"><span class="n"><a href="#t648">648</a></span><span class="t"> <span class="com"># matrix method will already get some teachers into schools so student_teacher_ratio should be higher</span> </span><span class="r"></span></p>
<p id="t649" class="pln"><span class="n"><a href="#t649">649</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t650" class="run"><span class="n"><a href="#t650">650</a></span><span class="t"> <span class="nam">all_teachers</span> <span class="op">=</span> <span class="nam">dict</span><span class="op">.</span><span class="nam">fromkeys</span><span class="op">(</span><span class="nam">np</span><span class="op">.</span><span class="nam">arange</span><span class="op">(</span><span class="num">101</span><span class="op">)</span><span class="op">,</span> <span class="num">0</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t651" class="pln"><span class="n"><a href="#t651">651</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t652" class="run"><span class="n"><a href="#t652">652</a></span><span class="t"> <span class="nam">syn_teachers</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t653" class="run"><span class="n"><a href="#t653">653</a></span><span class="t"> <span class="nam">syn_teacher_ids</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t654" class="pln"><span class="n"><a href="#t654">654</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t655" class="run"><span class="n"><a href="#t655">655</a></span><span class="t"> <span class="key">for</span> <span class="nam">n</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">len</span><span class="op">(</span><span class="nam">syn_schools</span><span class="op">)</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t656" class="run"><span class="n"><a href="#t656">656</a></span><span class="t"> <span class="nam">school</span> <span class="op">=</span> <span class="nam">syn_schools</span><span class="op">[</span><span class="nam">n</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t657" class="run"><span class="n"><a href="#t657">657</a></span><span class="t"> <span class="nam">school_uids</span> <span class="op">=</span> <span class="nam">syn_school_uids</span><span class="op">[</span><span class="nam">n</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t658" class="pln"><span class="n"><a href="#t658">658</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t659" class="run"><span class="n"><a href="#t659">659</a></span><span class="t"> <span class="nam">size</span> <span class="op">=</span> <span class="nam">len</span><span class="op">(</span><span class="nam">school</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t660" class="run"><span class="n"><a href="#t660">660</a></span><span class="t"> <span class="nam">nteachers</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="nam">size</span> <span class="op">/</span> <span class="nam">float</span><span class="op">(</span><span class="nam">student_teacher_ratio</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t661" class="run"><span class="n"><a href="#t661">661</a></span><span class="t"> <span class="nam">nteachers</span> <span class="op">=</span> <span class="nam">max</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="nam">nteachers</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t662" class="run"><span class="n"><a href="#t662">662</a></span><span class="t"> <span class="key">if</span> <span class="nam">verbose</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t663" class="mis show_mis"><span class="n"><a href="#t663">663</a></span><span class="t"> <span class="nam">print</span><span class="op">(</span><span class="str">'nteachers'</span><span class="op">,</span> <span class="nam">nteachers</span><span class="op">,</span> <span class="str">'student-teacher ratio'</span><span class="op">,</span> <span class="nam">size</span> <span class="op">/</span> <span class="nam">nteachers</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t664" class="run"><span class="n"><a href="#t664">664</a></span><span class="t"> <span class="nam">teachers</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t665" class="run"><span class="n"><a href="#t665">665</a></span><span class="t"> <span class="nam">teacher_uids</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t666" class="pln"><span class="n"><a href="#t666">666</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t667" class="run"><span class="n"><a href="#t667">667</a></span><span class="t"> <span class="key">for</span> <span class="nam">nt</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">nteachers</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t668" class="pln"><span class="n"><a href="#t668">668</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t669" class="run"><span class="n"><a href="#t669">669</a></span><span class="t"> <span class="nam">a</span> <span class="op">=</span> <span class="nam">spsamp</span><span class="op">.</span><span class="nam">sample_from_range</span><span class="op">(</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">,</span> <span class="nam">teacher_age_min</span><span class="op">,</span> <span class="nam">teacher_age_max</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t670" class="run"><span class="n"><a href="#t670">670</a></span><span class="t"> <span class="nam">uid</span> <span class="op">=</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t671" class="run"><span class="n"><a href="#t671">671</a></span><span class="t"> <span class="nam">teachers</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">a</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t672" class="run"><span class="n"><a href="#t672">672</a></span><span class="t"> <span class="nam">all_teachers</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">+=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t673" class="pln"><span class="n"><a href="#t673">673</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t674" class="run"><span class="n"><a href="#t674">674</a></span><span class="t"> <span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span><span class="op">.</span><span class="nam">remove</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t675" class="run"><span class="n"><a href="#t675">675</a></span><span class="t"> <span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t676" class="run"><span class="n"><a href="#t676">676</a></span><span class="t"> <span class="nam">potential_worker_ages_left_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t677" class="run"><span class="n"><a href="#t677">677</a></span><span class="t"> <span class="nam">potential_worker_uids</span><span class="op">.</span><span class="nam">pop</span><span class="op">(</span><span class="nam">uid</span><span class="op">,</span> <span class="key">None</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t678" class="pln"><span class="n"><a href="#t678">678</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t679" class="pln"><span class="n"><a href="#t679">679</a></span><span class="t"> <span class="com"># school.append(a)</span> </span><span class="r"></span></p>
<p id="t680" class="pln"><span class="n"><a href="#t680">680</a></span><span class="t"> <span class="com"># school_uids.append(uid)</span> </span><span class="r"></span></p>
<p id="t681" class="run"><span class="n"><a href="#t681">681</a></span><span class="t"> <span class="nam">teachers</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">a</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t682" class="run"><span class="n"><a href="#t682">682</a></span><span class="t"> <span class="nam">teacher_uids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t683" class="pln"><span class="n"><a href="#t683">683</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t684" class="run"><span class="n"><a href="#t684">684</a></span><span class="t"> <span class="nam">syn_teachers</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">teachers</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t685" class="run"><span class="n"><a href="#t685">685</a></span><span class="t"> <span class="nam">syn_teacher_ids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">teacher_uids</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t686" class="pln"><span class="n"><a href="#t686">686</a></span><span class="t"> <span class="com"># syn_schools[n] = school</span> </span><span class="r"></span></p>
<p id="t687" class="pln"><span class="n"><a href="#t687">687</a></span><span class="t"> <span class="com"># syn_school_uids[n] = school_uids</span> </span><span class="r"></span></p>
<p id="t688" class="run"><span class="n"><a href="#t688">688</a></span><span class="t"> <span class="key">if</span> <span class="nam">verbose</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t689" class="mis show_mis"><span class="n"><a href="#t689">689</a></span><span class="t"> <span class="nam">print</span><span class="op">(</span><span class="str">'school with teachers'</span><span class="op">,</span> <span class="nam">sorted</span><span class="op">(</span><span class="nam">school</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t690" class="mis show_mis"><span class="n"><a href="#t690">690</a></span><span class="t"> <span class="nam">print</span><span class="op">(</span><span class="str">'nkids'</span><span class="op">,</span> <span class="op">(</span><span class="nam">np</span><span class="op">.</span><span class="nam">array</span><span class="op">(</span><span class="nam">school</span><span class="op">)</span> <span class="op"><=</span> <span class="num">19</span><span class="op">)</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">)</span><span class="op">,</span> <span class="str">'n20+'</span><span class="op">,</span> <span class="op">(</span><span class="nam">np</span><span class="op">.</span><span class="nam">array</span><span class="op">(</span><span class="nam">school</span><span class="op">)</span> <span class="op">></span> <span class="num">19</span><span class="op">)</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t691" class="mis show_mis"><span class="n"><a href="#t691">691</a></span><span class="t"> <span class="nam">print</span><span class="op">(</span><span class="str">'kid-adult ratio'</span><span class="op">,</span> <span class="op">(</span><span class="nam">np</span><span class="op">.</span><span class="nam">array</span><span class="op">(</span><span class="nam">school</span><span class="op">)</span> <span class="op"><=</span> <span class="num">19</span><span class="op">)</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">)</span> <span class="op">/</span> <span class="op">(</span><span class="nam">np</span><span class="op">.</span><span class="nam">array</span><span class="op">(</span><span class="nam">school</span><span class="op">)</span> <span class="op">></span> <span class="num">19</span><span class="op">)</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t692" class="pln"><span class="n"><a href="#t692">692</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t693" class="pln"><span class="n"><a href="#t693">693</a></span><span class="t"> <span class="com"># return syn_schools, syn_school_uids, potential_worker_uids, potential_worker_uids_by_age, workers_by_age_to_assign_count</span> </span><span class="r"></span></p>
<p id="t694" class="run"><span class="n"><a href="#t694">694</a></span><span class="t"> <span class="key">return</span> <span class="nam">syn_teachers</span><span class="op">,</span> <span class="nam">syn_teacher_ids</span><span class="op">,</span> <span class="nam">potential_worker_uids</span><span class="op">,</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">,</span> <span class="nam">workers_by_age_to_assign_count</span> </span><span class="r"></span></p>
<p id="t695" class="pln"><span class="n"><a href="#t695">695</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t696" class="pln"><span class="n"><a href="#t696">696</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t697" class="run"><span class="n"><a href="#t697">697</a></span><span class="t"><span class="key">def</span> <span class="nam">assign_rest_of_workers</span><span class="op">(</span><span class="nam">workplace_sizes</span><span class="op">,</span> <span class="nam">potential_worker_uids</span><span class="op">,</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">,</span> <span class="nam">workers_by_age_to_assign_count</span><span class="op">,</span> <span class="nam">age_by_uid_dic</span><span class="op">,</span> <span class="nam">age_brackets</span><span class="op">,</span> <span class="nam">age_by_brackets_dic</span><span class="op">,</span> <span class="nam">contact_matrix_dic</span><span class="op">,</span> <span class="nam">verbose</span><span class="op">=</span><span class="key">False</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t698" class="pln"><span class="n"><a href="#t698">698</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t699" class="pln"><span class="n"><a href="#t699">699</a></span><span class="t"><span class="str"> Assign the rest of the workers to non-school workplaces.</span> </span><span class="r"></span></p>
<p id="t700" class="pln"><span class="n"><a href="#t700">700</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t701" class="pln"><span class="n"><a href="#t701">701</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t702" class="pln"><span class="n"><a href="#t702">702</a></span><span class="t"><span class="str"> workplace_sizes (list) : list of workplace sizes</span> </span><span class="r"></span></p>
<p id="t703" class="pln"><span class="n"><a href="#t703">703</a></span><span class="t"><span class="str"> potential_worker_uids (dict) : dictionary of potential workers mapping their id to their age</span> </span><span class="r"></span></p>
<p id="t704" class="pln"><span class="n"><a href="#t704">704</a></span><span class="t"><span class="str"> potential_worker_uids_by_age (dict) : dictionary mapping age to the list of worker ids with that age</span> </span><span class="r"></span></p>
<p id="t705" class="pln"><span class="n"><a href="#t705">705</a></span><span class="t"><span class="str"> workers_by_age_to_assign_count (dict) : dictionary of the count of workers left to assign by age</span> </span><span class="r"></span></p>
<p id="t706" class="pln"><span class="n"><a href="#t706">706</a></span><span class="t"><span class="str"> age_by_uid_dic (dict) : dictionary mapping id to age for all individuals in the population</span> </span><span class="r"></span></p>
<p id="t707" class="pln"><span class="n"><a href="#t707">707</a></span><span class="t"><span class="str"> age_brackets (dict) : dictionary mapping age bracket keys to age bracket range</span> </span><span class="r"></span></p>
<p id="t708" class="pln"><span class="n"><a href="#t708">708</a></span><span class="t"><span class="str"> age_by_brackets_dic (dict) : dictionary mapping age to the age bracket range it falls in</span> </span><span class="r"></span></p>
<p id="t709" class="pln"><span class="n"><a href="#t709">709</a></span><span class="t"><span class="str"> contact_matrix_dic (dict) : dictionary of age specific contact matrix for different physical contact settings</span> </span><span class="r"></span></p>
<p id="t710" class="pln"><span class="n"><a href="#t710">710</a></span><span class="t"><span class="str"> verbose (bool) : If True, print statements about the generated schools as teachers are being added to each school.</span> </span><span class="r"></span></p>
<p id="t711" class="pln"><span class="n"><a href="#t711">711</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t712" class="pln"><span class="n"><a href="#t712">712</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t713" class="pln"><span class="n"><a href="#t713">713</a></span><span class="t"><span class="str"> List of lists where each sublist is a workplace with the ages of workers, list of lists where each sublist is a workplace with the ids of workers,</span> </span><span class="r"></span></p>
<p id="t714" class="pln"><span class="n"><a href="#t714">714</a></span><span class="t"><span class="str"> dictionary of potential workers left mapping id to age, dictionary mapping age to a list of potential workers left of that age, dictionary</span> </span><span class="r"></span></p>
<p id="t715" class="pln"><span class="n"><a href="#t715">715</a></span><span class="t"><span class="str"> mapping age to the count of workers left to assign.</span> </span><span class="r"></span></p>
<p id="t716" class="pln"><span class="n"><a href="#t716">716</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t717" class="run"><span class="n"><a href="#t717">717</a></span><span class="t"> <span class="nam">syn_workplaces</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t718" class="run"><span class="n"><a href="#t718">718</a></span><span class="t"> <span class="nam">syn_workplace_uids</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t719" class="run"><span class="n"><a href="#t719">719</a></span><span class="t"> <span class="nam">worker_age_keys</span> <span class="op">=</span> <span class="nam">workers_by_age_to_assign_count</span><span class="op">.</span><span class="nam">keys</span><span class="op">(</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t720" class="run"><span class="n"><a href="#t720">720</a></span><span class="t"> <span class="nam">sorted_worker_age_keys</span> <span class="op">=</span> <span class="nam">sorted</span><span class="op">(</span><span class="nam">worker_age_keys</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t721" class="pln"><span class="n"><a href="#t721">721</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t722" class="pln"><span class="n"><a href="#t722">722</a></span><span class="t"> <span class="com"># off turn likelihood to meet those unemployed in the workplace because the matrices are not an exact match for the population under study</span> </span><span class="r"></span></p>
<p id="t723" class="run"><span class="n"><a href="#t723">723</a></span><span class="t"> <span class="key">for</span> <span class="nam">b</span> <span class="key">in</span> <span class="nam">age_brackets</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t724" class="run"><span class="n"><a href="#t724">724</a></span><span class="t"> <span class="nam">workers_left_in_bracket</span> <span class="op">=</span> <span class="op">[</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">age_brackets</span><span class="op">[</span><span class="nam">b</span><span class="op">]</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t725" class="run"><span class="n"><a href="#t725">725</a></span><span class="t"> <span class="nam">number_of_workers_left_in_bracket</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="nam">workers_left_in_bracket</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t726" class="run"><span class="n"><a href="#t726">726</a></span><span class="t"> <span class="key">if</span> <span class="nam">number_of_workers_left_in_bracket</span> <span class="op">==</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t727" class="run"><span class="n"><a href="#t727">727</a></span><span class="t"> <span class="nam">b</span> <span class="op">=</span> <span class="nam">min</span><span class="op">(</span><span class="nam">b</span><span class="op">,</span> <span class="nam">contact_matrix_dic</span><span class="op">[</span><span class="str">'W'</span><span class="op">]</span><span class="op">.</span><span class="nam">shape</span><span class="op">[</span><span class="num">1</span><span class="op">]</span> <span class="op">-</span> <span class="num">1</span><span class="op">)</span> <span class="com"># Ensure it doesn't go past the end of the array</span> </span><span class="r"></span></p>
<p id="t728" class="run"><span class="n"><a href="#t728">728</a></span><span class="t"> <span class="nam">contact_matrix_dic</span><span class="op">[</span><span class="str">'W'</span><span class="op">]</span><span class="op">[</span><span class="op">:</span><span class="op">,</span> <span class="nam">b</span><span class="op">]</span> <span class="op">=</span> <span class="num">0</span> </span><span class="r"></span></p>
<p id="t729" class="pln"><span class="n"><a href="#t729">729</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t730" class="run"><span class="n"><a href="#t730">730</a></span><span class="t"> <span class="key">for</span> <span class="nam">n</span><span class="op">,</span> <span class="nam">size</span> <span class="key">in</span> <span class="nam">enumerate</span><span class="op">(</span><span class="nam">workplace_sizes</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t731" class="run"><span class="n"><a href="#t731">731</a></span><span class="t"> <span class="nam">workers_by_age_to_assign_distr</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t732" class="run"><span class="n"><a href="#t732">732</a></span><span class="t"> <span class="key">if</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">[</span><span class="nam">workers_by_age_to_assign_distr</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">workers_by_age_to_assign_distr</span><span class="op">]</span><span class="op">)</span> <span class="op">==</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t733" class="run"><span class="n"><a href="#t733">733</a></span><span class="t"> <span class="key">break</span> </span><span class="r"></span></p>
<p id="t734" class="run"><span class="n"><a href="#t734">734</a></span><span class="t"> <span class="key">if</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">[</span><span class="nam">len</span><span class="op">(</span><span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span><span class="op">)</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">]</span><span class="op">)</span> <span class="op">==</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t735" class="mis show_mis"><span class="n"><a href="#t735">735</a></span><span class="t"> <span class="key">break</span> </span><span class="r"></span></p>
<p id="t736" class="run"><span class="n"><a href="#t736">736</a></span><span class="t"> <span class="nam">new_work</span><span class="op">,</span> <span class="nam">new_work_uids</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span><span class="op">,</span> <span class="op">[</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t737" class="pln"><span class="n"><a href="#t737">737</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t738" class="run"><span class="n"><a href="#t738">738</a></span><span class="t"> <span class="nam">a_prob</span> <span class="op">=</span> <span class="op">[</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">sorted_worker_age_keys</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t739" class="run"><span class="n"><a href="#t739">739</a></span><span class="t"> <span class="nam">a_prob</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">array</span><span class="op">(</span><span class="nam">a_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t740" class="run"><span class="n"><a href="#t740">740</a></span><span class="t"> <span class="nam">a_prob</span> <span class="op">=</span> <span class="nam">a_prob</span> <span class="op">/</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="nam">a_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t741" class="pln"><span class="n"><a href="#t741">741</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t742" class="run"><span class="n"><a href="#t742">742</a></span><span class="t"> <span class="nam">achoice</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">choice</span><span class="op">(</span><span class="nam">a</span><span class="op">=</span><span class="nam">sorted_worker_age_keys</span><span class="op">,</span> <span class="nam">p</span><span class="op">=</span><span class="nam">a_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t743" class="run"><span class="n"><a href="#t743">743</a></span><span class="t"> <span class="nam">aindex</span> <span class="op">=</span> <span class="nam">achoice</span> </span><span class="r"></span></p>
<p id="t744" class="pln"><span class="n"><a href="#t744">744</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t745" class="run"><span class="n"><a href="#t745">745</a></span><span class="t"> <span class="nam">uid</span> <span class="op">=</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">aindex</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t746" class="run"><span class="n"><a href="#t746">746</a></span><span class="t"> <span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">aindex</span><span class="op">]</span><span class="op">.</span><span class="nam">remove</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t747" class="run"><span class="n"><a href="#t747">747</a></span><span class="t"> <span class="nam">potential_worker_uids</span><span class="op">.</span><span class="nam">pop</span><span class="op">(</span><span class="nam">uid</span><span class="op">,</span> <span class="key">None</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t748" class="run"><span class="n"><a href="#t748">748</a></span><span class="t"> <span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">aindex</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t749" class="run"><span class="n"><a href="#t749">749</a></span><span class="t"> <span class="nam">workers_by_age_to_assign_distr</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t750" class="run"><span class="n"><a href="#t750">750</a></span><span class="t"> <span class="nam">new_work</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">aindex</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t751" class="run"><span class="n"><a href="#t751">751</a></span><span class="t"> <span class="nam">new_work_uids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t752" class="pln"><span class="n"><a href="#t752">752</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t753" class="run"><span class="n"><a href="#t753">753</a></span><span class="t"> <span class="nam">bindex</span> <span class="op">=</span> <span class="nam">age_by_brackets_dic</span><span class="op">[</span><span class="nam">aindex</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t754" class="run"><span class="n"><a href="#t754">754</a></span><span class="t"> <span class="nam">bindex</span> <span class="op">=</span> <span class="nam">min</span><span class="op">(</span><span class="nam">bindex</span><span class="op">,</span> <span class="nam">contact_matrix_dic</span><span class="op">[</span><span class="str">'W'</span><span class="op">]</span><span class="op">.</span><span class="nam">shape</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> <span class="op">-</span> <span class="num">1</span><span class="op">)</span> <span class="com"># Ensure it doesn't go past the end of the array</span> </span><span class="r"></span></p>
<p id="t755" class="run"><span class="n"><a href="#t755">755</a></span><span class="t"> <span class="nam">b_prob</span> <span class="op">=</span> <span class="nam">contact_matrix_dic</span><span class="op">[</span><span class="str">'W'</span><span class="op">]</span><span class="op">[</span><span class="nam">bindex</span><span class="op">,</span> <span class="op">:</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t756" class="run"><span class="n"><a href="#t756">756</a></span><span class="t"> <span class="key">if</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="nam">b_prob</span><span class="op">)</span> <span class="op">></span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t757" class="run"><span class="n"><a href="#t757">757</a></span><span class="t"> <span class="nam">b_prob</span> <span class="op">=</span> <span class="nam">b_prob</span> <span class="op">/</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="nam">b_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t758" class="pln"><span class="n"><a href="#t758">758</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t759" class="run"><span class="n"><a href="#t759">759</a></span><span class="t"> <span class="key">if</span> <span class="nam">size</span> <span class="op">></span> <span class="nam">len</span><span class="op">(</span><span class="nam">potential_worker_uids</span><span class="op">)</span> <span class="op">-</span> <span class="num">1</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t760" class="mis show_mis"><span class="n"><a href="#t760">760</a></span><span class="t"> <span class="nam">size</span> <span class="op">=</span> <span class="nam">len</span><span class="op">(</span><span class="nam">potential_worker_uids</span><span class="op">)</span> <span class="op">-</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t761" class="run"><span class="n"><a href="#t761">761</a></span><span class="t"> <span class="nam">workers_left_count</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="op">[</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">workers_by_age_to_assign_count</span><span class="op">]</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t762" class="run"><span class="n"><a href="#t762">762</a></span><span class="t"> <span class="key">if</span> <span class="nam">size</span> <span class="op">></span> <span class="nam">workers_left_count</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t763" class="run"><span class="n"><a href="#t763">763</a></span><span class="t"> <span class="nam">size</span> <span class="op">=</span> <span class="nam">workers_left_count</span> <span class="op">+</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t764" class="pln"><span class="n"><a href="#t764">764</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t765" class="pln"><span class="n"><a href="#t765">765</a></span><span class="t"> <span class="com"># not enough people left over to try to match age mixing patterns in the last workplace so grab everyone who will get placed in order</span> </span><span class="r"></span></p>
<p id="t766" class="run"><span class="n"><a href="#t766">766</a></span><span class="t"> <span class="key">if</span> <span class="nam">len</span><span class="op">(</span><span class="nam">potential_worker_uids</span><span class="op">)</span> <span class="op"><=</span> <span class="nam">size</span> <span class="key">or</span> <span class="nam">workers_left_count</span> <span class="op"><=</span> <span class="nam">size</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t767" class="run"><span class="n"><a href="#t767">767</a></span><span class="t"> <span class="key">for</span> <span class="nam">ai</span> <span class="key">in</span> <span class="nam">workers_by_age_to_assign_count</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t768" class="run"><span class="n"><a href="#t768">768</a></span><span class="t"> <span class="key">for</span> <span class="nam">i</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">)</span><span class="op">:</span> <span class="com"># do not change this during the loop but afterwards, and if 0 then no one will be placed</span> </span><span class="r"></span></p>
<p id="t769" class="run"><span class="n"><a href="#t769">769</a></span><span class="t"> <span class="nam">uid</span> <span class="op">=</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t770" class="run"><span class="n"><a href="#t770">770</a></span><span class="t"> <span class="nam">new_work</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">ai</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t771" class="run"><span class="n"><a href="#t771">771</a></span><span class="t"> <span class="nam">new_work_uids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t772" class="run"><span class="n"><a href="#t772">772</a></span><span class="t"> <span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">.</span><span class="nam">remove</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t773" class="run"><span class="n"><a href="#t773">773</a></span><span class="t"> <span class="nam">potential_worker_uids</span><span class="op">.</span><span class="nam">pop</span><span class="op">(</span><span class="nam">uid</span><span class="op">,</span> <span class="key">None</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t774" class="run"><span class="n"><a href="#t774">774</a></span><span class="t"> <span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span> <span class="op">=</span> <span class="num">0</span> <span class="com"># set to zero now that everyone will be placed in this last workplace</span> </span><span class="r"></span></p>
<p id="t775" class="run"><span class="n"><a href="#t775">775</a></span><span class="t"> <span class="nam">workers_by_age_to_assign_distr</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t776" class="pln"><span class="n"><a href="#t776">776</a></span><span class="t"> <span class="key">else</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t777" class="run"><span class="n"><a href="#t777">777</a></span><span class="t"> <span class="key">for</span> <span class="nam">i</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="nam">size</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t778" class="pln"><span class="n"><a href="#t778">778</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t779" class="run"><span class="n"><a href="#t779">779</a></span><span class="t"> <span class="nam">bichoice</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">multinomial</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="nam">b_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t780" class="run"><span class="n"><a href="#t780">780</a></span><span class="t"> <span class="nam">bi</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">where</span><span class="op">(</span><span class="nam">bichoice</span><span class="op">)</span><span class="op">[</span><span class="num">0</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t781" class="pln"><span class="n"><a href="#t781">781</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t782" class="run"><span class="n"><a href="#t782">782</a></span><span class="t"> <span class="nam">workers_left_in_bracket</span> <span class="op">=</span> <span class="op">[</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">age_brackets</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span> <span class="key">if</span> <span class="nam">len</span><span class="op">(</span><span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span><span class="op">)</span> <span class="op">></span> <span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t783" class="run"><span class="n"><a href="#t783">783</a></span><span class="t"> <span class="key">if</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="nam">b_prob</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t784" class="run"><span class="n"><a href="#t784">784</a></span><span class="t"> <span class="key">while</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="nam">workers_left_in_bracket</span><span class="op">)</span> <span class="op">==</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t785" class="run"><span class="n"><a href="#t785">785</a></span><span class="t"> <span class="nam">bichoice</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">multinomial</span><span class="op">(</span><span class="num">1</span><span class="op">,</span> <span class="nam">b_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t786" class="run"><span class="n"><a href="#t786">786</a></span><span class="t"> <span class="nam">bi</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">where</span><span class="op">(</span><span class="nam">bichoice</span><span class="op">)</span><span class="op">[</span><span class="num">0</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t787" class="run"><span class="n"><a href="#t787">787</a></span><span class="t"> <span class="nam">workers_left_in_bracket</span> <span class="op">=</span> <span class="op">[</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">age_brackets</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span> <span class="key">if</span> <span class="nam">len</span><span class="op">(</span><span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span><span class="op">)</span> <span class="op">></span> <span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t788" class="run"><span class="n"><a href="#t788">788</a></span><span class="t"> <span class="nam">a_prob</span> <span class="op">=</span> <span class="op">[</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">age_brackets</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t789" class="run"><span class="n"><a href="#t789">789</a></span><span class="t"> <span class="nam">a_prob</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">array</span><span class="op">(</span><span class="nam">a_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t790" class="run"><span class="n"><a href="#t790">790</a></span><span class="t"> <span class="nam">a_prob</span> <span class="op">=</span> <span class="nam">a_prob</span><span class="op">/</span><span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="nam">a_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t791" class="pln"><span class="n"><a href="#t791">791</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t792" class="run"><span class="n"><a href="#t792">792</a></span><span class="t"> <span class="nam">ai</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">random</span><span class="op">.</span><span class="nam">choice</span><span class="op">(</span><span class="nam">a</span><span class="op">=</span><span class="nam">age_brackets</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span><span class="op">,</span> <span class="nam">p</span><span class="op">=</span><span class="nam">a_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t793" class="pln"><span class="n"><a href="#t793">793</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t794" class="run"><span class="n"><a href="#t794">794</a></span><span class="t"> <span class="nam">uid</span> <span class="op">=</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t795" class="run"><span class="n"><a href="#t795">795</a></span><span class="t"> <span class="nam">new_work</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">ai</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t796" class="run"><span class="n"><a href="#t796">796</a></span><span class="t"> <span class="nam">new_work_uids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t797" class="run"><span class="n"><a href="#t797">797</a></span><span class="t"> <span class="nam">potential_worker_uids_by_age</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span><span class="op">.</span><span class="nam">remove</span><span class="op">(</span><span class="nam">uid</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t798" class="run"><span class="n"><a href="#t798">798</a></span><span class="t"> <span class="nam">potential_worker_uids</span><span class="op">.</span><span class="nam">pop</span><span class="op">(</span><span class="nam">uid</span><span class="op">,</span> <span class="key">None</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t799" class="run"><span class="n"><a href="#t799">799</a></span><span class="t"> <span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">ai</span><span class="op">]</span> <span class="op">-=</span> <span class="num">1</span> </span><span class="r"></span></p>
<p id="t800" class="run"><span class="n"><a href="#t800">800</a></span><span class="t"> <span class="nam">workers_by_age_to_assign_distr</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t801" class="pln"><span class="n"><a href="#t801">801</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t802" class="pln"><span class="n"><a href="#t802">802</a></span><span class="t"> <span class="com"># if there's no one left in the bracket, then you should turn this bracket off in the contact matrix</span> </span><span class="r"></span></p>
<p id="t803" class="run"><span class="n"><a href="#t803">803</a></span><span class="t"> <span class="nam">workers_left_in_bracket</span> <span class="op">=</span> <span class="op">[</span><span class="nam">workers_by_age_to_assign_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">age_brackets</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t804" class="run"><span class="n"><a href="#t804">804</a></span><span class="t"> <span class="key">if</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="nam">workers_left_in_bracket</span><span class="op">)</span> <span class="op">==</span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t805" class="run"><span class="n"><a href="#t805">805</a></span><span class="t"> <span class="nam">contact_matrix_dic</span><span class="op">[</span><span class="str">'W'</span><span class="op">]</span><span class="op">[</span><span class="op">:</span><span class="op">,</span> <span class="nam">bi</span><span class="op">]</span> <span class="op">=</span> <span class="num">0.</span> </span><span class="r"></span></p>
<p id="t806" class="pln"><span class="n"><a href="#t806">806</a></span><span class="t"> <span class="com"># since the matrix was modified, calculate the bracket probabilities again</span> </span><span class="r"></span></p>
<p id="t807" class="run"><span class="n"><a href="#t807">807</a></span><span class="t"> <span class="nam">b_prob</span> <span class="op">=</span> <span class="nam">contact_matrix_dic</span><span class="op">[</span><span class="str">'W'</span><span class="op">]</span><span class="op">[</span><span class="nam">bindex</span><span class="op">,</span> <span class="op">:</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t808" class="run"><span class="n"><a href="#t808">808</a></span><span class="t"> <span class="key">if</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="nam">b_prob</span><span class="op">)</span> <span class="op">></span> <span class="num">0</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t809" class="run"><span class="n"><a href="#t809">809</a></span><span class="t"> <span class="nam">b_prob</span> <span class="op">=</span> <span class="nam">b_prob</span> <span class="op">/</span> <span class="nam">np</span><span class="op">.</span><span class="nam">sum</span><span class="op">(</span><span class="nam">b_prob</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t810" class="pln"><span class="n"><a href="#t810">810</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t811" class="run"><span class="n"><a href="#t811">811</a></span><span class="t"> <span class="key">if</span> <span class="nam">verbose</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t812" class="mis show_mis"><span class="n"><a href="#t812">812</a></span><span class="t"> <span class="nam">print</span><span class="op">(</span><span class="nam">n</span><span class="op">,</span> <span class="nam">Counter</span><span class="op">(</span><span class="nam">new_work</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t813" class="pln"><span class="n"><a href="#t813">813</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t814" class="run"><span class="n"><a href="#t814">814</a></span><span class="t"> <span class="nam">syn_workplaces</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">new_work</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t815" class="run"><span class="n"><a href="#t815">815</a></span><span class="t"> <span class="nam">syn_workplace_uids</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">new_work_uids</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t816" class="run"><span class="n"><a href="#t816">816</a></span><span class="t"> <span class="key">return</span> <span class="nam">syn_workplaces</span><span class="op">,</span> <span class="nam">syn_workplace_uids</span><span class="op">,</span> <span class="nam">potential_worker_uids</span><span class="op">,</span> <span class="nam">potential_worker_uids_by_age</span><span class="op">,</span> <span class="nam">workers_by_age_to_assign_count</span> </span><span class="r"></span></p>
<p id="t817" class="pln"><span class="n"><a href="#t817">817</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t818" class="pln"><span class="n"><a href="#t818">818</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t819" class="run"><span class="n"><a href="#t819">819</a></span><span class="t"><span class="key">def</span> <span class="nam">generate_synthetic_population</span><span class="op">(</span><span class="nam">n</span><span class="op">,</span> <span class="nam">datadir</span><span class="op">,</span> <span class="nam">location</span><span class="op">=</span><span class="str">'seattle_metro'</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">=</span><span class="str">'Washington'</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">=</span><span class="str">'usa'</span><span class="op">,</span> <span class="nam">sheet_name</span><span class="op">=</span><span class="str">'United States of America'</span><span class="op">,</span> <span class="nam">school_enrollment_counts_available</span><span class="op">=</span><span class="key">False</span><span class="op">,</span> <span class="nam">verbose</span><span class="op">=</span><span class="key">False</span><span class="op">,</span> <span class="nam">plot</span><span class="op">=</span><span class="key">False</span><span class="op">,</span> <span class="nam">write</span><span class="op">=</span><span class="key">False</span><span class="op">,</span> <span class="nam">return_popdict</span><span class="op">=</span><span class="key">False</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="key">False</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t820" class="pln"><span class="n"><a href="#t820">820</a></span><span class="t"> <span class="str">"""</span> </span><span class="r"></span></p>
<p id="t821" class="pln"><span class="n"><a href="#t821">821</a></span><span class="t"><span class="str"> Wrapper function that calls other functions to generate a full population with their contacts in the household, school, and workplace layers,</span> </span><span class="r"></span></p>
<p id="t822" class="pln"><span class="n"><a href="#t822">822</a></span><span class="t"><span class="str"> and then writes this population to appropriate files.</span> </span><span class="r"></span></p>
<p id="t823" class="pln"><span class="n"><a href="#t823">823</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t824" class="pln"><span class="n"><a href="#t824">824</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t825" class="pln"><span class="n"><a href="#t825">825</a></span><span class="t"><span class="str"> n (int) : The number of people in the population.</span> </span><span class="r"></span></p>
<p id="t826" class="pln"><span class="n"><a href="#t826">826</a></span><span class="t"><span class="str"> datadir (string) : The file path to the data directory.</span> </span><span class="r"></span></p>
<p id="t827" class="pln"><span class="n"><a href="#t827">827</a></span><span class="t"><span class="str"> location (string) : The name of the location.</span> </span><span class="r"></span></p>
<p id="t828" class="pln"><span class="n"><a href="#t828">828</a></span><span class="t"><span class="str"> state_location (string) : The name of the state the location is in.</span> </span><span class="r"></span></p>
<p id="t829" class="pln"><span class="n"><a href="#t829">829</a></span><span class="t"><span class="str"> country_location (string) : The name of the country the location is in.</span> </span><span class="r"></span></p>
<p id="t830" class="pln"><span class="n"><a href="#t830">830</a></span><span class="t"><span class="str"> sheet_name (string) : The name of the sheet in the Excel file with contact patterns.</span> </span><span class="r"></span></p>
<p id="t831" class="pln"><span class="n"><a href="#t831">831</a></span><span class="t"><span class="str"> school_enrollment_counts_available (bool) : If True, a list of school sizes is available and a count of the sizes can be constructed.</span> </span><span class="r"></span></p>
<p id="t832" class="pln"><span class="n"><a href="#t832">832</a></span><span class="t"><span class="str"> verbose (bool) : If True, print statements as contacts are being generated.</span> </span><span class="r"></span></p>
<p id="t833" class="pln"><span class="n"><a href="#t833">833</a></span><span class="t"><span class="str"> plot (bool) : If True, plot and show a comparison of the generated age distribution in households vs. the expected age distribution of the population from census data being sampled.</span> </span><span class="r"></span></p>
<p id="t834" class="pln"><span class="n"><a href="#t834">834</a></span><span class="t"><span class="str"> write (bool) : If True, write population to file.</span> </span><span class="r"></span></p>
<p id="t835" class="pln"><span class="n"><a href="#t835">835</a></span><span class="t"><span class="str"> return_popdict (bool) : If True, returns a dictionary of individuals in the population</span> </span><span class="r"></span></p>
<p id="t836" class="pln"><span class="n"><a href="#t836">836</a></span><span class="t"><span class="str"> use_default (bool) : If True, try to first use the other parameters to find data specific to the location under study; otherwise, return default data drawing from Seattle, Washington.</span> </span><span class="r"></span></p>
<p id="t837" class="pln"><span class="n"><a href="#t837">837</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t838" class="pln"><span class="n"><a href="#t838">838</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t839" class="pln"><span class="n"><a href="#t839">839</a></span><span class="t"><span class="str"> If return_popdict is True, returns popdict, a dictionary of people with attributes. Dictionary keys are the IDs of individuals in the population and the values are a dictionary for each individual with their</span> </span><span class="r"></span></p>
<p id="t840" class="pln"><span class="n"><a href="#t840">840</a></span><span class="t"><span class="str"> attributes, such as age, household ID (hhid), school ID (scid), workplace ID (wpid), workplace industry code (wpindcode) if available, and the IDs of their contacts in different layers. Different layers</span> </span><span class="r"></span></p>
<p id="t841" class="pln"><span class="n"><a href="#t841">841</a></span><span class="t"><span class="str"> available are households ('H'), schools ('S'), and workplaces ('W'). Contacts in these layers are clustered and thus form a network composed of groups of people interacting with each other. For example, all</span> </span><span class="r"></span></p>
<p id="t842" class="pln"><span class="n"><a href="#t842">842</a></span><span class="t"><span class="str"> household members are contacts of each other, and everyone in the same school is a contact of each other. Else, return None.</span> </span><span class="r"></span></p>
<p id="t843" class="pln"><span class="n"><a href="#t843">843</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t844" class="pln"><span class="n"><a href="#t844">844</a></span><span class="t"><span class="str"> Example</span> </span><span class="r"></span></p>
<p id="t845" class="pln"><span class="n"><a href="#t845">845</a></span><span class="t"><span class="str"> =======</span> </span><span class="r"></span></p>
<p id="t846" class="pln"><span class="n"><a href="#t846">846</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t847" class="pln"><span class="n"><a href="#t847">847</a></span><span class="t"><span class="str"> ::</span> </span><span class="r"></span></p>
<p id="t848" class="pln"><span class="n"><a href="#t848">848</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t849" class="pln"><span class="n"><a href="#t849">849</a></span><span class="t"><span class="str"> datadir = sp.datadir # point datadir where your data folder lives</span> </span><span class="r"></span></p>
<p id="t850" class="pln"><span class="n"><a href="#t850">850</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t851" class="pln"><span class="n"><a href="#t851">851</a></span><span class="t"><span class="str"> location = 'seattle_metro'</span> </span><span class="r"></span></p>
<p id="t852" class="pln"><span class="n"><a href="#t852">852</a></span><span class="t"><span class="str"> state_location = 'Washington'</span> </span><span class="r"></span></p>
<p id="t853" class="pln"><span class="n"><a href="#t853">853</a></span><span class="t"><span class="str"> country_location = 'usa'</span> </span><span class="r"></span></p>
<p id="t854" class="pln"><span class="n"><a href="#t854">854</a></span><span class="t"><span class="str"> sheet_name = 'United States of America'</span> </span><span class="r"></span></p>
<p id="t855" class="pln"><span class="n"><a href="#t855">855</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t856" class="pln"><span class="n"><a href="#t856">856</a></span><span class="t"><span class="str"> n = 10000</span> </span><span class="r"></span></p>
<p id="t857" class="pln"><span class="n"><a href="#t857">857</a></span><span class="t"><span class="str"> verbose = False</span> </span><span class="r"></span></p>
<p id="t858" class="pln"><span class="n"><a href="#t858">858</a></span><span class="t"><span class="str"> plot = False</span> </span><span class="r"></span></p>
<p id="t859" class="pln"><span class="n"><a href="#t859">859</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t860" class="pln"><span class="n"><a href="#t860">860</a></span><span class="t"><span class="str"> # this will generate a population with microstructure and age demographics that</span> </span><span class="r"></span></p>
<p id="t861" class="pln"><span class="n"><a href="#t861">861</a></span><span class="t"><span class="str"> # approximate those of the location selected</span> </span><span class="r"></span></p>
<p id="t862" class="pln"><span class="n"><a href="#t862">862</a></span><span class="t"><span class="str"> # also saves to file in:</span> </span><span class="r"></span></p>
<p id="t863" class="pln"><span class="n"><a href="#t863">863</a></span><span class="t"><span class="str"> # datadir/demographics/contact_matrices_152_countries/state_location/</span> </span><span class="r"></span></p>
<p id="t864" class="pln"><span class="n"><a href="#t864">864</a></span><span class="t"><span class="str"> sp.generate_synthetic_population(n,datadir,location=location,</span> </span><span class="r"></span></p>
<p id="t865" class="pln"><span class="n"><a href="#t865">865</a></span><span class="t"><span class="str"> state_location=state_location,</span> </span><span class="r"></span></p>
<p id="t866" class="pln"><span class="n"><a href="#t866">866</a></span><span class="t"><span class="str"> country_location=country_location,</span> </span><span class="r"></span></p>
<p id="t867" class="pln"><span class="n"><a href="#t867">867</a></span><span class="t"><span class="str"> sheet_name=sheet_name,verbose=verbose,plot=plot)</span> </span><span class="r"></span></p>
<p id="t868" class="pln"><span class="n"><a href="#t868">868</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t869" class="run"><span class="n"><a href="#t869">869</a></span><span class="t"> <span class="nam">folder_name</span> <span class="op">=</span> <span class="str">'contact_networks'</span> </span><span class="r"></span></p>
<p id="t870" class="run"><span class="n"><a href="#t870">870</a></span><span class="t"> <span class="nam">age_brackets</span> <span class="op">=</span> <span class="nam">spdata</span><span class="op">.</span><span class="nam">get_census_age_brackets</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">=</span><span class="nam">state_location</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">=</span><span class="nam">country_location</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="nam">use_default</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t871" class="run"><span class="n"><a href="#t871">871</a></span><span class="t"> <span class="nam">age_by_brackets_dic</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">get_age_by_brackets_dic</span><span class="op">(</span><span class="nam">age_brackets</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t872" class="pln"><span class="n"><a href="#t872">872</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t873" class="run"><span class="n"><a href="#t873">873</a></span><span class="t"> <span class="nam">contact_matrix_dic</span> <span class="op">=</span> <span class="nam">spdata</span><span class="op">.</span><span class="nam">get_contact_matrix_dic</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">sheet_name</span><span class="op">=</span><span class="nam">sheet_name</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="nam">use_default</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t874" class="pln"><span class="n"><a href="#t874">874</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t875" class="run"><span class="n"><a href="#t875">875</a></span><span class="t"> <span class="nam">household_size_distr</span> <span class="op">=</span> <span class="nam">spdata</span><span class="op">.</span><span class="nam">get_household_size_distr</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">location</span><span class="op">=</span><span class="nam">location</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">=</span><span class="nam">state_location</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">=</span><span class="nam">country_location</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="nam">use_default</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t876" class="pln"><span class="n"><a href="#t876">876</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t877" class="run"><span class="n"><a href="#t877">877</a></span><span class="t"> <span class="nam">min_pop</span> <span class="op">=</span> <span class="num">100</span> </span><span class="r"></span></p>
<p id="t878" class="run"><span class="n"><a href="#t878">878</a></span><span class="t"> <span class="key">if</span> <span class="nam">n</span> <span class="op"><</span> <span class="nam">min_pop</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t879" class="run"><span class="n"><a href="#t879">879</a></span><span class="t"> <span class="key">raise</span> <span class="nam">NotImplementedError</span><span class="op">(</span><span class="str">f"Population is too small to currently be generated properly. Try a size larger than {min_pop}."</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t880" class="run"><span class="n"><a href="#t880">880</a></span><span class="t"> <span class="nam">n</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="nam">n</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t881" class="pln"><span class="n"><a href="#t881">881</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t882" class="pln"><span class="n"><a href="#t882">882</a></span><span class="t"> <span class="com"># this could be unnecessary if we get the single year age distribution in a different way.</span> </span><span class="r"></span></p>
<p id="t883" class="run"><span class="n"><a href="#t883">883</a></span><span class="t"> <span class="nam">n_to_sample_smoothly</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="num">1e6</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t884" class="run"><span class="n"><a href="#t884">884</a></span><span class="t"> <span class="nam">hh_sizes</span> <span class="op">=</span> <span class="nam">generate_household_sizes</span><span class="op">(</span><span class="nam">n_to_sample_smoothly</span><span class="op">,</span> <span class="nam">household_size_distr</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t885" class="run"><span class="n"><a href="#t885">885</a></span><span class="t"> <span class="nam">totalpop</span> <span class="op">=</span> <span class="nam">get_totalpopsize_from_household_sizes</span><span class="op">(</span><span class="nam">hh_sizes</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t886" class="pln"><span class="n"><a href="#t886">886</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t887" class="pln"><span class="n"><a href="#t887">887</a></span><span class="t"> <span class="com"># create a rough single year age distribution to draw from instead of the distribution by age brackets.</span> </span><span class="r"></span></p>
<p id="t888" class="run"><span class="n"><a href="#t888">888</a></span><span class="t"> <span class="nam">syn_ages</span><span class="op">,</span> <span class="nam">syn_sexes</span> <span class="op">=</span> <span class="nam">spsamp</span><span class="op">.</span><span class="nam">get_usa_age_sex_n</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">location</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">,</span> <span class="nam">totalpop</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t889" class="run"><span class="n"><a href="#t889">889</a></span><span class="t"> <span class="nam">syn_age_count</span> <span class="op">=</span> <span class="nam">Counter</span><span class="op">(</span><span class="nam">syn_ages</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t890" class="run"><span class="n"><a href="#t890">890</a></span><span class="t"> <span class="nam">syn_age_distr_unordered</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">norm_dic</span><span class="op">(</span><span class="nam">syn_age_count</span><span class="op">)</span> <span class="com"># Ensure it's ordered</span> </span><span class="r"></span></p>
<p id="t891" class="run"><span class="n"><a href="#t891">891</a></span><span class="t"> <span class="nam">syn_age_keys</span> <span class="op">=</span> <span class="nam">list</span><span class="op">(</span><span class="nam">syn_age_distr_unordered</span><span class="op">.</span><span class="nam">keys</span><span class="op">(</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t892" class="run"><span class="n"><a href="#t892">892</a></span><span class="t"> <span class="nam">sort_inds</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">argsort</span><span class="op">(</span><span class="nam">syn_age_keys</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t893" class="run"><span class="n"><a href="#t893">893</a></span><span class="t"> <span class="nam">syn_age_distr</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t894" class="run"><span class="n"><a href="#t894">894</a></span><span class="t"> <span class="key">for</span> <span class="nam">i</span> <span class="key">in</span> <span class="nam">sort_inds</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t895" class="run"><span class="n"><a href="#t895">895</a></span><span class="t"> <span class="nam">syn_age_distr</span><span class="op">[</span><span class="nam">syn_age_keys</span><span class="op">[</span><span class="nam">i</span><span class="op">]</span><span class="op">]</span> <span class="op">=</span> <span class="nam">syn_age_distr_unordered</span><span class="op">[</span><span class="nam">syn_age_keys</span><span class="op">[</span><span class="nam">i</span><span class="op">]</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t896" class="pln"><span class="n"><a href="#t896">896</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t897" class="pln"><span class="n"><a href="#t897">897</a></span><span class="t"> <span class="com"># actual household sizes</span> </span><span class="r"></span></p>
<p id="t898" class="run"><span class="n"><a href="#t898">898</a></span><span class="t"> <span class="nam">hh_sizes</span> <span class="op">=</span> <span class="nam">generate_household_sizes_from_fixed_pop_size</span><span class="op">(</span><span class="nam">n</span><span class="op">,</span> <span class="nam">household_size_distr</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t899" class="pln"><span class="n"><a href="#t899">899</a></span><span class="t"> <span class="com"># totalpop = get_totalpopsize_from_household_sizes(hh_sizes)</span> </span><span class="r"></span></p>
<p id="t900" class="pln"><span class="n"><a href="#t900">900</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t901" class="run"><span class="n"><a href="#t901">901</a></span><span class="t"> <span class="nam">hha_brackets</span> <span class="op">=</span> <span class="nam">spdata</span><span class="op">.</span><span class="nam">get_head_age_brackets</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">=</span><span class="nam">country_location</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="nam">use_default</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t902" class="run"><span class="n"><a href="#t902">902</a></span><span class="t"> <span class="nam">hha_by_size</span> <span class="op">=</span> <span class="nam">spdata</span><span class="op">.</span><span class="nam">get_head_age_by_size_distr</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">=</span><span class="nam">country_location</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="nam">use_default</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t903" class="pln"><span class="n"><a href="#t903">903</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t904" class="run"><span class="n"><a href="#t904">904</a></span><span class="t"> <span class="nam">homes_dic</span><span class="op">,</span> <span class="nam">homes</span> <span class="op">=</span> <span class="nam">generate_all_households</span><span class="op">(</span><span class="nam">n</span><span class="op">,</span> <span class="nam">hh_sizes</span><span class="op">,</span> <span class="nam">hha_by_size</span><span class="op">,</span> <span class="nam">hha_brackets</span><span class="op">,</span> <span class="nam">age_brackets</span><span class="op">,</span> <span class="nam">age_by_brackets_dic</span><span class="op">,</span> <span class="nam">contact_matrix_dic</span><span class="op">,</span> <span class="nam">deepcopy</span><span class="op">(</span><span class="nam">syn_age_distr</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t905" class="run"><span class="n"><a href="#t905">905</a></span><span class="t"> <span class="nam">homes_by_uids</span><span class="op">,</span> <span class="nam">age_by_uid_dic</span> <span class="op">=</span> <span class="nam">assign_uids_by_homes</span><span class="op">(</span><span class="nam">homes</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t906" class="run"><span class="n"><a href="#t906">906</a></span><span class="t"> <span class="nam">new_ages_count</span> <span class="op">=</span> <span class="nam">Counter</span><span class="op">(</span><span class="nam">age_by_uid_dic</span><span class="op">.</span><span class="nam">values</span><span class="op">(</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t907" class="pln"><span class="n"><a href="#t907">907</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t908" class="pln"><span class="n"><a href="#t908">908</a></span><span class="t"> <span class="com"># plot synthetic age distribution as a check</span> </span><span class="r"></span></p>
<p id="t909" class="run"><span class="n"><a href="#t909">909</a></span><span class="t"> <span class="key">if</span> <span class="nam">plot</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t910" class="pln"><span class="n"><a href="#t910">910</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t911" class="mis show_mis"><span class="n"><a href="#t911">911</a></span><span class="t"> <span class="nam">cmap</span> <span class="op">=</span> <span class="nam">mplt</span><span class="op">.</span><span class="nam">cm</span><span class="op">.</span><span class="nam">get_cmap</span><span class="op">(</span><span class="nam">cmocean</span><span class="op">.</span><span class="nam">cm</span><span class="op">.</span><span class="nam">deep_r</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t912" class="mis show_mis"><span class="n"><a href="#t912">912</a></span><span class="t"> <span class="nam">cmap3</span> <span class="op">=</span> <span class="nam">mplt</span><span class="op">.</span><span class="nam">cm</span><span class="op">.</span><span class="nam">get_cmap</span><span class="op">(</span><span class="nam">cmocean</span><span class="op">.</span><span class="nam">cm</span><span class="op">.</span><span class="nam">matter</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t913" class="pln"><span class="n"><a href="#t913">913</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t914" class="mis show_mis"><span class="n"><a href="#t914">914</a></span><span class="t"> <span class="nam">fig</span> <span class="op">=</span> <span class="nam">plt</span><span class="op">.</span><span class="nam">figure</span><span class="op">(</span><span class="nam">figsize</span><span class="op">=</span><span class="op">(</span><span class="num">7</span><span class="op">,</span> <span class="num">5</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t915" class="mis show_mis"><span class="n"><a href="#t915">915</a></span><span class="t"> <span class="nam">ax</span> <span class="op">=</span> <span class="nam">fig</span><span class="op">.</span><span class="nam">add_subplot</span><span class="op">(</span><span class="num">111</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t916" class="pln"><span class="n"><a href="#t916">916</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t917" class="mis show_mis"><span class="n"><a href="#t917">917</a></span><span class="t"> <span class="nam">x</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">arange</span><span class="op">(</span><span class="num">101</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t918" class="mis show_mis"><span class="n"><a href="#t918">918</a></span><span class="t"> <span class="nam">y_exp</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">zeros</span><span class="op">(</span><span class="num">101</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t919" class="mis show_mis"><span class="n"><a href="#t919">919</a></span><span class="t"> <span class="nam">y_sim</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">zeros</span><span class="op">(</span><span class="num">101</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t920" class="pln"><span class="n"><a href="#t920">920</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t921" class="mis show_mis"><span class="n"><a href="#t921">921</a></span><span class="t"> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="num">101</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t922" class="mis show_mis"><span class="n"><a href="#t922">922</a></span><span class="t"> <span class="nam">expected</span> <span class="op">=</span> <span class="nam">int</span><span class="op">(</span><span class="nam">syn_age_distr</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">*</span> <span class="nam">totalpop</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t923" class="mis show_mis"><span class="n"><a href="#t923">923</a></span><span class="t"> <span class="nam">y_exp</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">=</span> <span class="nam">expected</span> </span><span class="r"></span></p>
<p id="t924" class="mis show_mis"><span class="n"><a href="#t924">924</a></span><span class="t"> <span class="nam">y_sim</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">=</span> <span class="nam">new_ages_count</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t925" class="pln"><span class="n"><a href="#t925">925</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t926" class="mis show_mis"><span class="n"><a href="#t926">926</a></span><span class="t"> <span class="nam">ax</span><span class="op">.</span><span class="nam">plot</span><span class="op">(</span><span class="nam">x</span><span class="op">,</span> <span class="nam">y_exp</span><span class="op">,</span> <span class="nam">color</span><span class="op">=</span><span class="nam">cmap</span><span class="op">(</span><span class="num">0.2</span><span class="op">)</span><span class="op">,</span> <span class="nam">label</span><span class="op">=</span><span class="str">'Expected'</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t927" class="mis show_mis"><span class="n"><a href="#t927">927</a></span><span class="t"> <span class="nam">ax</span><span class="op">.</span><span class="nam">plot</span><span class="op">(</span><span class="nam">x</span><span class="op">,</span> <span class="nam">y_sim</span><span class="op">,</span> <span class="nam">color</span><span class="op">=</span><span class="nam">cmap3</span><span class="op">(</span><span class="num">0.6</span><span class="op">)</span><span class="op">,</span> <span class="nam">label</span><span class="op">=</span><span class="str">'Simulated'</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t928" class="mis show_mis"><span class="n"><a href="#t928">928</a></span><span class="t"> <span class="nam">leg</span> <span class="op">=</span> <span class="nam">ax</span><span class="op">.</span><span class="nam">legend</span><span class="op">(</span><span class="nam">fontsize</span><span class="op">=</span><span class="num">18</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t929" class="mis show_mis"><span class="n"><a href="#t929">929</a></span><span class="t"> <span class="nam">leg</span><span class="op">.</span><span class="nam">draw_frame</span><span class="op">(</span><span class="key">False</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t930" class="mis show_mis"><span class="n"><a href="#t930">930</a></span><span class="t"> <span class="nam">ax</span><span class="op">.</span><span class="nam">set_xlim</span><span class="op">(</span><span class="nam">left</span><span class="op">=</span><span class="num">0</span><span class="op">,</span> <span class="nam">right</span><span class="op">=</span><span class="num">100</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t931" class="mis show_mis"><span class="n"><a href="#t931">931</a></span><span class="t"> <span class="nam">ax</span><span class="op">.</span><span class="nam">set_xticks</span><span class="op">(</span><span class="nam">np</span><span class="op">.</span><span class="nam">arange</span><span class="op">(</span><span class="num">0</span><span class="op">,</span> <span class="num">101</span><span class="op">,</span> <span class="num">5</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t932" class="pln"><span class="n"><a href="#t932">932</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t933" class="mis show_mis"><span class="n"><a href="#t933">933</a></span><span class="t"> <span class="nam">plt</span><span class="op">.</span><span class="nam">show</span><span class="op">(</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t934" class="pln"><span class="n"><a href="#t934">934</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t935" class="pln"><span class="n"><a href="#t935">935</a></span><span class="t"> <span class="com"># Make a dictionary listing out uids of people by their age</span> </span><span class="r"></span></p>
<p id="t936" class="run"><span class="n"><a href="#t936">936</a></span><span class="t"> <span class="nam">uids_by_age_dic</span> <span class="op">=</span> <span class="nam">spb</span><span class="op">.</span><span class="nam">get_ids_by_age_dic</span><span class="op">(</span><span class="nam">age_by_uid_dic</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t937" class="pln"><span class="n"><a href="#t937">937</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t938" class="pln"><span class="n"><a href="#t938">938</a></span><span class="t"> <span class="com"># Generate school sizes</span> </span><span class="r"></span></p>
<p id="t939" class="run"><span class="n"><a href="#t939">939</a></span><span class="t"> <span class="nam">school_sizes_count_by_brackets</span> <span class="op">=</span> <span class="nam">spdata</span><span class="op">.</span><span class="nam">get_school_size_distr_by_brackets</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">location</span><span class="op">=</span><span class="nam">location</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">=</span><span class="nam">state_location</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">=</span><span class="nam">country_location</span><span class="op">,</span> <span class="nam">counts_available</span><span class="op">=</span><span class="nam">school_enrollment_counts_available</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="nam">use_default</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t940" class="run"><span class="n"><a href="#t940">940</a></span><span class="t"> <span class="nam">school_size_brackets</span> <span class="op">=</span> <span class="nam">spdata</span><span class="op">.</span><span class="nam">get_school_size_brackets</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">location</span><span class="op">=</span><span class="nam">location</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">=</span><span class="nam">state_location</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">=</span><span class="nam">country_location</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="nam">use_default</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t941" class="pln"><span class="n"><a href="#t941">941</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t942" class="pln"><span class="n"><a href="#t942">942</a></span><span class="t"> <span class="com"># Figure out who's going to school as a student with enrollment rates (gets called inside sp.get_uids_in_school)</span> </span><span class="r"></span></p>
<p id="t943" class="run"><span class="n"><a href="#t943">943</a></span><span class="t"> <span class="nam">uids_in_school</span><span class="op">,</span> <span class="nam">uids_in_school_by_age</span><span class="op">,</span> <span class="nam">ages_in_school_count</span> <span class="op">=</span> <span class="nam">get_uids_in_school</span><span class="op">(</span><span class="nam">datadir</span><span class="op">,</span> <span class="nam">n</span><span class="op">,</span> <span class="nam">location</span><span class="op">,</span> <span class="nam">state_location</span><span class="op">,</span> <span class="nam">country_location</span><span class="op">,</span> <span class="nam">age_by_uid_dic</span><span class="op">=</span><span class="nam">age_by_uid_dic</span><span class="op">,</span> <span class="nam">homes_by_uids</span><span class="op">=</span><span class="nam">homes_by_uids</span><span class="op">,</span> <span class="nam">use_default</span><span class="op">=</span><span class="nam">use_default</span><span class="op">)</span> <span class="com"># use_default will call in school enrollment rates from the location if available or use the defaults</span> </span><span class="r"></span></p>
<p id="t944" class="pln"><span class="n"><a href="#t944">944</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t945" class="pln"><span class="n"><a href="#t945">945</a></span><span class="t"> <span class="com"># Get school sizes</span> </span><span class="r"></span></p>
<p id="t946" class="run"><span class="n"><a href="#t946">946</a></span><span class="t"> <span class="nam">gen_school_sizes</span> <span class="op">=</span> <span class="nam">generate_school_sizes</span><span class="op">(</span><span class="nam">school_sizes_count_by_brackets</span><span class="op">,</span> <span class="nam">school_size_brackets</span><span class="op">,</span> <span class="nam">uids_in_school</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t947" class="pln"><span class="n"><a href="#t947">947</a></span><span class="t"> </span><span class="r"></span></p>