-
Notifications
You must be signed in to change notification settings - Fork 0
/
_mnt_d_Github_synthpops_synthpops_base_py.html
334 lines (334 loc) · 64.5 KB
/
_mnt_d_Github_synthpops_synthpops_base_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
<!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 /mnt/d/Github/synthpops/synthpops/base.py: 79%</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>/mnt/d/Github/synthpops/synthpops/base.py</b> :
<span class="pc_cov">79%</span>
</h1>
<img id="keyboard_icon" src="keybd_closed.png" alt="Show keyboard shortcuts" />
<h2 class="stats">
82 statements
<button type="button" class="run shortkey_r button_toggle_run" title="Toggle lines run">65 run</button>
<button type="button" class="mis show_mis shortkey_m button_toggle_mis" title="Toggle lines missing">17 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">The module contains frequently-used functions that do not neatly fit into other areas of the code base.</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 sciris as sc</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">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="t7" class="run"><span class="n"><a href="#t7">7</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="t8" class="run"><span class="n"><a href="#t8">8</a></span><span class="t"><span class="key">from</span> <span class="op">.</span> <span class="key">import</span> <span class="nam">config</span> <span class="key">as</span> <span class="nam">cfg</span> </span><span class="r"></span></p>
<p id="t9" class="pln"><span class="n"><a href="#t9">9</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t10" class="pln"><span class="n"><a href="#t10">10</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t11" class="run"><span class="n"><a href="#t11">11</a></span><span class="t"><span class="key">def</span> <span class="nam">norm_dic</span><span class="op">(</span><span class="nam">dic</span><span class="op">)</span><span class="op">:</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 class="str">"""</span> </span><span class="r"></span></p>
<p id="t13" class="pln"><span class="n"><a href="#t13">13</a></span><span class="t"><span class="str"> Normalize the dictionary ``dic``.</span> </span><span class="r"></span></p>
<p id="t14" class="pln"><span class="n"><a href="#t14">14</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t15" class="pln"><span class="n"><a href="#t15">15</a></span><span class="t"><span class="str"> Args:</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 class="str"> dic (dict): A dictionary with numerical values.</span> </span><span class="r"></span></p>
<p id="t17" class="pln"><span class="n"><a href="#t17">17</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t18" class="pln"><span class="n"><a href="#t18">18</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t19" class="pln"><span class="n"><a href="#t19">19</a></span><span class="t"><span class="str"> A normalized dictionary.</span> </span><span class="r"></span></p>
<p id="t20" class="pln"><span class="n"><a href="#t20">20</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t21" class="pln"><span class="n"><a href="#t21">21</a></span><span class="t"> <span class="com"># total = np.sum([dic[i] for i in dic], dtype=float)</span> </span><span class="r"></span></p>
<p id="t22" class="run"><span class="n"><a href="#t22">22</a></span><span class="t"> <span class="nam">total</span> <span class="op">=</span> <span class="nam">float</span><span class="op">(</span><span class="nam">sum</span><span class="op">(</span><span class="nam">dic</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><span class="r"></span></p>
<p id="t23" class="run"><span class="n"><a href="#t23">23</a></span><span class="t"> <span class="key">if</span> <span class="nam">total</span> <span class="op">==</span> <span class="num">0.0</span><span class="op">:</span> </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">return</span> <span class="nam">dic</span> </span><span class="r"></span></p>
<p id="t25" class="run"><span class="n"><a href="#t25">25</a></span><span class="t"> <span class="nam">new_dic</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t26" class="run"><span class="n"><a href="#t26">26</a></span><span class="t"> <span class="key">for</span> <span class="nam">i</span> <span class="key">in</span> <span class="nam">dic</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t27" class="run"><span class="n"><a href="#t27">27</a></span><span class="t"> <span class="nam">new_dic</span><span class="op">[</span><span class="nam">i</span><span class="op">]</span> <span class="op">=</span> <span class="nam">float</span><span class="op">(</span><span class="nam">dic</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">total</span> </span><span class="r"></span></p>
<p id="t28" class="run"><span class="n"><a href="#t28">28</a></span><span class="t"> <span class="key">return</span> <span class="nam">new_dic</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><span class="r"></span></p>
<p id="t30" class="pln"><span class="n"><a href="#t30">30</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t31" class="run"><span class="n"><a href="#t31">31</a></span><span class="t"><span class="key">def</span> <span class="nam">norm_age_group</span><span class="op">(</span><span class="nam">age_dic</span><span class="op">,</span> <span class="nam">age_min</span><span class="op">,</span> <span class="nam">age_max</span><span class="op">)</span><span class="op">:</span> </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">"""</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"> Create a normalized dictionary for the range ``age_min`` to ``age_max``, inclusive.</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><span class="r"></span></p>
<p id="t35" class="pln"><span class="n"><a href="#t35">35</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t36" class="pln"><span class="n"><a href="#t36">36</a></span><span class="t"><span class="str"> age_dic (dict) : A dictionary with numerical values.</span> </span><span class="r"></span></p>
<p id="t37" class="pln"><span class="n"><a href="#t37">37</a></span><span class="t"><span class="str"> age_min (int) : The minimum value of the range for the dictionary.</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 class="str"> age_max (int) : The maximum value of the range for the dictionary.</span> </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="pln"><span class="n"><a href="#t40">40</a></span><span class="t"><span class="str"> Returns:</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"> A normalized dictionary for keys in the range ``age_min`` to ``age_max``, inclusive.</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"> """</span> </span><span class="r"></span></p>
<p id="t43" class="run"><span class="n"><a href="#t43">43</a></span><span class="t"> <span class="nam">dic</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t44" class="run"><span class="n"><a href="#t44">44</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="nam">age_min</span><span class="op">,</span> <span class="nam">age_max</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="t45" class="run"><span class="n"><a href="#t45">45</a></span><span class="t"> <span class="nam">dic</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">=</span> <span class="nam">age_dic</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t46" class="run"><span class="n"><a href="#t46">46</a></span><span class="t"> <span class="key">return</span> <span class="nam">norm_dic</span><span class="op">(</span><span class="nam">dic</span><span class="op">)</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><span class="r"></span></p>
<p id="t49" class="pln"><span class="n"><a href="#t49">49</a></span><span class="t"><span class="com"># Functions related to age distributions</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><span class="r"></span></p>
<p id="t51" class="run"><span class="n"><a href="#t51">51</a></span><span class="t"><span class="key">def</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 class="op">:</span> </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="str">"""</span> </span><span class="r"></span></p>
<p id="t53" class="pln"><span class="n"><a href="#t53">53</a></span><span class="t"><span class="str"> Create a dictionary mapping age to the age bracket it falls in.</span> </span><span class="r"></span></p>
<p id="t54" class="pln"><span class="n"><a href="#t54">54</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t55" class="pln"><span class="n"><a href="#t55">55</a></span><span class="t"><span class="str"> Args:</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 class="str"> age_brackets (dict): A dictionary mapping age bracket keys to age bracket range.</span> </span><span class="r"></span></p>
<p id="t57" class="pln"><span class="n"><a href="#t57">57</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t58" class="pln"><span class="n"><a href="#t58">58</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t59" class="pln"><span class="n"><a href="#t59">59</a></span><span class="t"><span class="str"> A dictionary of age bracket by age.</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="str"> Example</span> </span><span class="r"></span></p>
<p id="t62" class="pln"><span class="n"><a href="#t62">62</a></span><span class="t"><span class="str"> =======</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="str"> ::</span> </span><span class="r"></span></p>
<p id="t65" class="pln"><span class="n"><a href="#t65">65</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t66" class="pln"><span class="n"><a href="#t66">66</a></span><span class="t"><span class="str"> age_brackets = sp.get_census_age_brackets(sp.datadir,state_location='Washington',country_location='usa')</span> </span><span class="r"></span></p>
<p id="t67" class="pln"><span class="n"><a href="#t67">67</a></span><span class="t"><span class="str"> age_by_brackets_dic = sp.get_age_by_brackets_dic(age_brackets)</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 class="str"> """</span> </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">age_by_brackets_dic</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</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">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="t71" class="run"><span class="n"><a href="#t71">71</a></span><span class="t"> <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="t72" class="run"><span class="n"><a href="#t72">72</a></span><span class="t"> <span class="nam">age_by_brackets_dic</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> <span class="op">=</span> <span class="nam">b</span> </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">return</span> <span class="nam">age_by_brackets_dic</span> </span><span class="r"></span></p>
<p id="t74" class="pln"><span class="n"><a href="#t74">74</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t75" class="pln"><span class="n"><a href="#t75">75</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t76" class="run"><span class="n"><a href="#t76">76</a></span><span class="t"><span class="key">def</span> <span class="nam">get_aggregate_ages</span><span class="op">(</span><span class="nam">ages</span><span class="op">,</span> <span class="nam">age_by_brackets_dic</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t77" class="pln"><span class="n"><a href="#t77">77</a></span><span class="t"> <span class="str">"""</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 class="str"> Create a dictionary of the count of ages by age brackets.</span> </span><span class="r"></span></p>
<p id="t79" class="pln"><span class="n"><a href="#t79">79</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t80" class="pln"><span class="n"><a href="#t80">80</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t81" class="pln"><span class="n"><a href="#t81">81</a></span><span class="t"><span class="str"> ages (dict) : A dictionary of age count by single year.</span> </span><span class="r"></span></p>
<p id="t82" class="pln"><span class="n"><a href="#t82">82</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="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="pln"><span class="n"><a href="#t84">84</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t85" class="pln"><span class="n"><a href="#t85">85</a></span><span class="t"><span class="str"> A dictionary of aggregated age count for specified age brackets.</span> </span><span class="r"></span></p>
<p id="t86" class="pln"><span class="n"><a href="#t86">86</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t87" class="pln"><span class="n"><a href="#t87">87</a></span><span class="t"><span class="str"> Example</span> </span><span class="r"></span></p>
<p id="t88" class="pln"><span class="n"><a href="#t88">88</a></span><span class="t"><span class="str"> =======</span> </span><span class="r"></span></p>
<p id="t89" class="pln"><span class="n"><a href="#t89">89</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t90" class="pln"><span class="n"><a href="#t90">90</a></span><span class="t"><span class="str"> ::</span> </span><span class="r"></span></p>
<p id="t91" class="pln"><span class="n"><a href="#t91">91</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t92" class="pln"><span class="n"><a href="#t92">92</a></span><span class="t"><span class="str"> aggregate_age_count = sp.get_aggregate_ages(age_count, age_by_brackets_dic)</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 class="str"> aggregate_matrix = symmetric_matrix.copy()</span> </span><span class="r"></span></p>
<p id="t94" class="pln"><span class="n"><a href="#t94">94</a></span><span class="t"><span class="str"> aggregate_matrix = sp.get_aggregate_matrix(aggregate_matrix, age_by_brackets_dic)</span> </span><span class="r"></span></p>
<p id="t95" class="pln"><span class="n"><a href="#t95">95</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t96" class="run"><span class="n"><a href="#t96">96</a></span><span class="t"> <span class="nam">bracket_keys</span> <span class="op">=</span> <span class="nam">set</span><span class="op">(</span><span class="nam">age_by_brackets_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="t97" class="run"><span class="n"><a href="#t97">97</a></span><span class="t"> <span class="nam">aggregate_ages</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">bracket_keys</span><span class="op">,</span> <span class="num">0</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t98" class="run"><span class="n"><a href="#t98">98</a></span><span class="t"> <span class="key">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">ages</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t99" class="run"><span class="n"><a href="#t99">99</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">a</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t100" class="run"><span class="n"><a href="#t100">100</a></span><span class="t"> <span class="nam">aggregate_ages</span><span class="op">[</span><span class="nam">b</span><span class="op">]</span> <span class="op">+=</span> <span class="nam">ages</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t101" class="run"><span class="n"><a href="#t101">101</a></span><span class="t"> <span class="key">return</span> <span class="nam">aggregate_ages</span> </span><span class="r"></span></p>
<p id="t102" class="pln"><span class="n"><a href="#t102">102</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t103" class="pln"><span class="n"><a href="#t103">103</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t104" class="run"><span class="n"><a href="#t104">104</a></span><span class="t"><span class="key">def</span> <span class="nam">get_aggregate_age_dict_conversion</span><span class="op">(</span><span class="nam">larger_aggregate_ages</span><span class="op">,</span> <span class="nam">larger_age_brackets</span><span class="op">,</span> <span class="nam">smaller_age_brackets</span><span class="op">,</span> <span class="nam">age_by_brackets_dic_larger</span><span class="op">,</span> <span class="nam">age_by_brackets_dic_smaller</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t105" class="pln"><span class="n"><a href="#t105">105</a></span><span class="t"> <span class="str">"""</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 class="str"> Convert the aggregate age count in ``larger_aggregate_ages`` from a larger number of age brackets to a smaller number of age brackets.</span> </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="pln"><span class="n"><a href="#t108">108</a></span><span class="t"><span class="str"> Args:</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"> larger_aggregate_ages (dict) : A dictionary of aggregated age count.</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"> larger_age_brackets (dict) : A dictionary of age brackets.</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"> smaller_age_brackets (dict) : A dictionary of fewer age brackets.</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 class="str"> age_by_brackets_dic_larger (dict) : A dictionary mapping age to the larger number of age brackets.</span> </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"> age_by_brackets_dic_smaller (dict) : A dictionary mapping age to the smaller number of age brackets.</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><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"> Returns:</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"> A dictionary of the aggregated age count for the smaller number of age brackets.</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><span class="r"></span></p>
<p id="t118" class="pln"><span class="n"><a href="#t118">118</a></span><span class="t"><span class="str"> """</span> </span><span class="r"></span></p>
<p id="t119" class="mis show_mis"><span class="n"><a href="#t119">119</a></span><span class="t"> <span class="key">if</span> <span class="nam">len</span><span class="op">(</span><span class="nam">larger_age_brackets</span><span class="op">)</span> <span class="op"><</span> <span class="nam">len</span><span class="op">(</span><span class="nam">smaller_age_brackets</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t120" class="mis show_mis"><span class="n"><a href="#t120">120</a></span><span class="t"> <span class="key">raise</span> <span class="nam">NotImplementedError</span><span class="op">(</span><span class="str">'Cannot reduce aggregate ages any further.'</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t121" class="mis show_mis"><span class="n"><a href="#t121">121</a></span><span class="t"> <span class="nam">smaller_aggregate_ages</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">smaller_age_brackets</span><span class="op">.</span><span class="nam">keys</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="t122" class="mis show_mis"><span class="n"><a href="#t122">122</a></span><span class="t"> <span class="key">for</span> <span class="nam">lb</span> <span class="key">in</span> <span class="nam">larger_age_brackets</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t123" class="mis show_mis"><span class="n"><a href="#t123">123</a></span><span class="t"> <span class="nam">a</span> <span class="op">=</span> <span class="nam">larger_age_brackets</span><span class="op">[</span><span class="nam">lb</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="t124" class="mis show_mis"><span class="n"><a href="#t124">124</a></span><span class="t"> <span class="nam">sb</span> <span class="op">=</span> <span class="nam">age_by_brackets_dic_smaller</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t125" class="mis show_mis"><span class="n"><a href="#t125">125</a></span><span class="t"> <span class="nam">smaller_aggregate_ages</span><span class="op">[</span><span class="nam">sb</span><span class="op">]</span> <span class="op">+=</span> <span class="nam">larger_aggregate_ages</span><span class="op">[</span><span class="nam">lb</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t126" class="mis show_mis"><span class="n"><a href="#t126">126</a></span><span class="t"> <span class="key">return</span> <span class="nam">smaller_aggregate_ages</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="pln"><span class="n"><a href="#t129">129</a></span><span class="t"><span class="com"># Functions related to contact matrices</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><span class="r"></span></p>
<p id="t131" class="run"><span class="n"><a href="#t131">131</a></span><span class="t"><span class="key">def</span> <span class="nam">get_aggregate_matrix</span><span class="op">(</span><span class="nam">M</span><span class="op">,</span> <span class="nam">age_by_brackets_dic</span><span class="op">)</span><span class="op">:</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 class="str">"""</span> </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"> Aggregate a symmetric matrix to fewer age brackets. Do not use for homogeneous mixing matrix.</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><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"> Args:</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"> M (np.ndarray) : A symmetric age contact matrix.</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"> 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="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"> A symmetric contact matrix (``np.ndarray``) aggregated to age brackets.</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><span class="r"></span></p>
<p id="t142" class="pln"><span class="n"><a href="#t142">142</a></span><span class="t"><span class="str"> Example</span> </span><span class="r"></span></p>
<p id="t143" class="pln"><span class="n"><a href="#t143">143</a></span><span class="t"><span class="str"> =======</span> </span><span class="r"></span></p>
<p id="t144" class="pln"><span class="n"><a href="#t144">144</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t145" class="pln"><span class="n"><a href="#t145">145</a></span><span class="t"><span class="str"> ::</span> </span><span class="r"></span></p>
<p id="t146" class="pln"><span class="n"><a href="#t146">146</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t147" class="pln"><span class="n"><a href="#t147">147</a></span><span class="t"><span class="str"> age_brackets = sp.get_census_age_brackets(sp.datadir,state_location='Washington',country_location='usa')</span> </span><span class="r"></span></p>
<p id="t148" class="pln"><span class="n"><a href="#t148">148</a></span><span class="t"><span class="str"> age_by_brackets_dic = sp.get_age_by_brackets_dic(age_brackets)</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="pln"><span class="n"><a href="#t150">150</a></span><span class="t"><span class="str"> aggregate_age_count = sp.get_aggregate_ages(age_count, age_by_brackets_dic)</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 class="str"> aggregate_matrix = symmetric_matrix.copy()</span> </span><span class="r"></span></p>
<p id="t152" class="pln"><span class="n"><a href="#t152">152</a></span><span class="t"><span class="str"> aggregate_matrix = sp.get_aggregate_matrix(aggregate_matrix, age_by_brackets_dic)</span> </span><span class="r"></span></p>
<p id="t153" class="pln"><span class="n"><a href="#t153">153</a></span><span class="t"> </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"> asymmetric_matrix = sp.get_asymmetric_matrix(aggregate_matrix, aggregate_age_count)</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><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"> """</span> </span><span class="r"></span></p>
<p id="t157" class="run"><span class="n"><a href="#t157">157</a></span><span class="t"> <span class="nam">N</span> <span class="op">=</span> <span class="nam">len</span><span class="op">(</span><span class="nam">M</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t158" class="run"><span class="n"><a href="#t158">158</a></span><span class="t"> <span class="nam">num_agebrackets</span> <span class="op">=</span> <span class="nam">len</span><span class="op">(</span><span class="nam">set</span><span class="op">(</span><span class="nam">age_by_brackets_dic</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><span class="r"></span></p>
<p id="t159" class="run"><span class="n"><a href="#t159">159</a></span><span class="t"> <span class="nam">M_agg</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">num_agebrackets</span><span class="op">,</span> <span class="nam">num_agebrackets</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t160" class="run"><span class="n"><a href="#t160">160</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">N</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t161" class="run"><span class="n"><a href="#t161">161</a></span><span class="t"> <span class="nam">bi</span> <span class="op">=</span> <span class="nam">age_by_brackets_dic</span><span class="op">[</span><span class="nam">i</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t162" class="run"><span class="n"><a href="#t162">162</a></span><span class="t"> <span class="key">for</span> <span class="nam">j</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">N</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t163" class="run"><span class="n"><a href="#t163">163</a></span><span class="t"> <span class="nam">bj</span> <span class="op">=</span> <span class="nam">age_by_brackets_dic</span><span class="op">[</span><span class="nam">j</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t164" class="run"><span class="n"><a href="#t164">164</a></span><span class="t"> <span class="nam">M_agg</span><span class="op">[</span><span class="nam">bi</span><span class="op">]</span><span class="op">[</span><span class="nam">bj</span><span class="op">]</span> <span class="op">+=</span> <span class="nam">M</span><span class="op">[</span><span class="nam">i</span><span class="op">]</span><span class="op">[</span><span class="nam">j</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t165" class="run"><span class="n"><a href="#t165">165</a></span><span class="t"> <span class="key">return</span> <span class="nam">M_agg</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><span class="r"></span></p>
<p id="t167" class="pln"><span class="n"><a href="#t167">167</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t168" class="run"><span class="n"><a href="#t168">168</a></span><span class="t"><span class="key">def</span> <span class="nam">get_asymmetric_matrix</span><span class="op">(</span><span class="nam">symmetric_matrix</span><span class="op">,</span> <span class="nam">aggregate_ages</span><span class="op">)</span><span class="op">:</span> </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">"""</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"> Get the contact matrix for the average individual in each age bracket.</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><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"> Args:</span> </span><span class="r"></span></p>
<p id="t173" class="pln"><span class="n"><a href="#t173">173</a></span><span class="t"><span class="str"> symmetric_matrix (np.ndarray) : A symmetric age contact matrix.</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 class="str"> aggregate_ages (dict) : A dictionary mapping single year ages to age brackets.</span> </span><span class="r"></span></p>
<p id="t175" class="pln"><span class="n"><a href="#t175">175</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t176" class="pln"><span class="n"><a href="#t176">176</a></span><span class="t"><span class="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t177" class="pln"><span class="n"><a href="#t177">177</a></span><span class="t"><span class="str"> A contact matrix (``np.ndarray``) whose elements ``M_ij`` describe the contact frequency for the average individual in age bracket ``i`` with all possible contacts in age bracket ``j``.</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="pln"><span class="n"><a href="#t179">179</a></span><span class="t"><span class="str"> Example</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 class="str"> =======</span> </span><span class="r"></span></p>
<p id="t181" class="pln"><span class="n"><a href="#t181">181</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t182" class="pln"><span class="n"><a href="#t182">182</a></span><span class="t"><span class="str"> ::</span> </span><span class="r"></span></p>
<p id="t183" class="pln"><span class="n"><a href="#t183">183</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t184" class="pln"><span class="n"><a href="#t184">184</a></span><span class="t"><span class="str"> age_brackets = sp.get_census_age_brackets(sp.datadir,state_location='Washington',country_location='usa')</span> </span><span class="r"></span></p>
<p id="t185" class="pln"><span class="n"><a href="#t185">185</a></span><span class="t"><span class="str"> age_by_brackets_dic = sp.get_age_by_brackets_dic(age_brackets)</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="pln"><span class="n"><a href="#t187">187</a></span><span class="t"><span class="str"> aggregate_age_count = sp.get_aggregate_ages(age_count, age_by_brackets_dic)</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 class="str"> aggregate_matrix = symmetric_matrix.copy()</span> </span><span class="r"></span></p>
<p id="t189" class="pln"><span class="n"><a href="#t189">189</a></span><span class="t"><span class="str"> aggregate_matrix = sp.get_aggregate_matrix(aggregate_matrix, age_by_brackets_dic)</span> </span><span class="r"></span></p>
<p id="t190" class="pln"><span class="n"><a href="#t190">190</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t191" class="pln"><span class="n"><a href="#t191">191</a></span><span class="t"><span class="str"> asymmetric_matrix = sp.get_asymmetric_matrix(aggregate_matrix, aggregate_age_count)</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 class="str"> """</span> </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="nam">M</span> <span class="op">=</span> <span class="nam">deepcopy</span><span class="op">(</span><span class="nam">symmetric_matrix</span><span class="op">)</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">for</span> <span class="nam">a</span> <span class="key">in</span> <span class="nam">aggregate_ages</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">M</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 class="nam">M</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 class="nam">float</span><span class="op">(</span><span class="nam">aggregate_ages</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="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="run"><span class="n"><a href="#t197">197</a></span><span class="t"> <span class="key">return</span> <span class="nam">M</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="pln"><span class="n"><a href="#t199">199</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t200" class="run"><span class="n"><a href="#t200">200</a></span><span class="t"><span class="key">def</span> <span class="nam">get_symmetric_community_matrix</span><span class="op">(</span><span class="nam">ages</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t201" class="pln"><span class="n"><a href="#t201">201</a></span><span class="t"> <span class="str">"""</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 class="str"> Get a symmetric homogeneous matrix.</span> </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="pln"><span class="n"><a href="#t204">204</a></span><span class="t"><span class="str"> Args:</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"> ages (dict): A dictionary with the count of each single year age.</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"> Returns:</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"> A symmetric homogeneous matrix for age count in ages.</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"> """</span> </span><span class="r"></span></p>
<p id="t209" class="mis show_mis"><span class="n"><a href="#t209">209</a></span><span class="t"> <span class="nam">N</span> <span class="op">=</span> <span class="nam">len</span><span class="op">(</span><span class="nam">ages</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t210" class="mis show_mis"><span class="n"><a href="#t210">210</a></span><span class="t"> <span class="nam">M</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">ones</span><span class="op">(</span><span class="op">(</span><span class="nam">N</span><span class="op">,</span> <span class="nam">N</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t211" class="mis show_mis"><span class="n"><a href="#t211">211</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="nam">N</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t212" class="mis show_mis"><span class="n"><a href="#t212">212</a></span><span class="t"> <span class="nam">M</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 class="nam">M</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 class="nam">ages</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t213" class="mis show_mis"><span class="n"><a href="#t213">213</a></span><span class="t"> <span class="nam">M</span><span class="op">[</span><span class="op">:</span><span class="op">,</span> <span class="nam">a</span><span class="op">]</span> <span class="op">=</span> <span class="nam">M</span><span class="op">[</span><span class="op">:</span><span class="op">,</span> <span class="nam">a</span><span class="op">]</span> <span class="op">*</span> <span class="nam">ages</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t214" class="mis show_mis"><span class="n"><a href="#t214">214</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="nam">N</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t215" class="mis show_mis"><span class="n"><a href="#t215">215</a></span><span class="t"> <span class="nam">M</span><span class="op">[</span><span class="nam">a</span><span class="op">,</span> <span class="nam">a</span><span class="op">]</span> <span class="op">-=</span> <span class="nam">ages</span><span class="op">[</span><span class="nam">a</span><span class="op">]</span> </span><span class="r"></span></p>
<p id="t216" class="mis show_mis"><span class="n"><a href="#t216">216</a></span><span class="t"> <span class="nam">M</span> <span class="op">=</span> <span class="nam">M</span> <span class="op">/</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">ages</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</span><span class="op">]</span><span class="op">,</span> <span class="nam">dtype</span><span class="op">=</span><span class="nam">float</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="t217" class="mis show_mis"><span class="n"><a href="#t217">217</a></span><span class="t"> <span class="key">return</span> <span class="nam">M</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><span class="r"></span></p>
<p id="t219" class="pln"><span class="n"><a href="#t219">219</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t220" class="run"><span class="n"><a href="#t220">220</a></span><span class="t"><span class="key">def</span> <span class="nam">combine_matrices</span><span class="op">(</span><span class="nam">matrix_dic</span><span class="op">,</span> <span class="nam">weights_dic</span><span class="op">,</span> <span class="nam">num_agebrackets</span><span class="op">)</span><span class="op">:</span> </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">"""</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"> Combine different contact matrices into a single contact matrix.</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><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"> Args:</span> </span><span class="r"></span></p>
<p id="t225" class="pln"><span class="n"><a href="#t225">225</a></span><span class="t"><span class="str"> matrix_dic (dict) : A dictionary of different contact matrices by setting.</span> </span><span class="r"></span></p>
<p id="t226" class="pln"><span class="n"><a href="#t226">226</a></span><span class="t"><span class="str"> weights_dic (dict) : A dictionary of weights for each setting.</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="str"> num_agebrackets (int) : The number of age brackets for the different matrices.</span> </span><span class="r"></span></p>
<p id="t228" class="pln"><span class="n"><a href="#t228">228</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t229" class="pln"><span class="n"><a href="#t229">229</a></span><span class="t"><span class="str"> Returns:</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 class="str"> A contact matrix (``np.ndarray``) that is a linear combination of setting specific matrices given weights for each setting.</span> </span><span class="r"></span></p>
<p id="t231" class="pln"><span class="n"><a href="#t231">231</a></span><span class="t"><span class="str"> """</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">M</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">cfg</span><span class="op">.</span><span class="nam">matrix_size</span><span class="op">,</span> <span class="nam">cfg</span><span class="op">.</span><span class="nam">matrix_size</span><span class="op">)</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t233" class="run"><span class="n"><a href="#t233">233</a></span><span class="t"> <span class="key">for</span> <span class="nam">setting_code</span> <span class="key">in</span> <span class="nam">weights_dic</span><span class="op">:</span> </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">M</span> <span class="op">+=</span> <span class="nam">matrix_dic</span><span class="op">[</span><span class="nam">setting_code</span><span class="op">]</span> <span class="op">*</span> <span class="nam">weights_dic</span><span class="op">[</span><span class="nam">setting_code</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">return</span> <span class="nam">M</span> </span><span class="r"></span></p>
<p id="t236" class="pln"><span class="n"><a href="#t236">236</a></span><span class="t"> </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="key">def</span> <span class="nam">get_ids_by_age_dic</span><span class="op">(</span><span class="nam">age_by_id_dic</span><span class="op">)</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t239" class="pln"><span class="n"><a href="#t239">239</a></span><span class="t"> <span class="str">"""</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 class="str"> Get lists of IDs that map to each age.</span> </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="pln"><span class="n"><a href="#t242">242</a></span><span class="t"><span class="str"> Args:</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"> age_by_id_dic (dict): A dictionary with the age of each individual by their ID.</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><span class="r"></span></p>
<p id="t245" class="pln"><span class="n"><a href="#t245">245</a></span><span class="t"><span class="str"> Returns:</span> </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"> A dictionary listing IDs for each age from a dictionary that maps ID to age.</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"> """</span> </span><span class="r"></span></p>
<p id="t248" class="run"><span class="n"><a href="#t248">248</a></span><span class="t"> <span class="nam">max_val</span> <span class="op">=</span> <span class="nam">max</span><span class="op">(</span><span class="op">[</span><span class="nam">v</span> <span class="key">for</span> <span class="nam">v</span> <span class="key">in</span> <span class="nam">age_by_id_dic</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><span class="r"></span></p>
<p id="t249" class="run"><span class="n"><a href="#t249">249</a></span><span class="t"> <span class="nam">ids_by_age_dic</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="nam">max_val</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="t250" class="run"><span class="n"><a href="#t250">250</a></span><span class="t"> <span class="key">for</span> <span class="nam">i</span> <span class="key">in</span> <span class="nam">ids_by_age_dic</span><span class="op">:</span> </span><span class="r"></span></p>
<p id="t251" class="run"><span class="n"><a href="#t251">251</a></span><span class="t"> <span class="nam">ids_by_age_dic</span><span class="op">[</span><span class="nam">i</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="t252" class="run"><span class="n"><a href="#t252">252</a></span><span class="t"> <span class="key">for</span> <span class="nam">i</span> <span class="key">in</span> <span class="nam">age_by_id_dic</span><span class="op">:</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">ids_by_age_dic</span><span class="op">[</span><span class="nam">age_by_id_dic</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">append</span><span class="op">(</span><span class="nam">i</span><span class="op">)</span> </span><span class="r"></span></p>
<p id="t254" class="run"><span class="n"><a href="#t254">254</a></span><span class="t"> <span class="key">return</span> <span class="nam">ids_by_age_dic</span> </span><span class="r"></span></p>
<p id="t255" class="pln"><span class="n"><a href="#t255">255</a></span><span class="t"> </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">def</span> <span class="nam">get_uids_by_age_dic</span><span class="op">(</span><span class="nam">popdict</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 class="str">"""</span> </span><span class="r"></span></p>
<p id="t259" class="pln"><span class="n"><a href="#t259">259</a></span><span class="t"><span class="str"> Get lists of UIDs that map to each age.</span> </span><span class="r"></span></p>
<p id="t260" class="pln"><span class="n"><a href="#t260">260</a></span><span class="t"> </span><span class="r"></span></p>
<p id="t261" class="pln"><span class="n"><a href="#t261">261</a></span><span class="t"><span class="str"> Args:</span> </span><span class="r"></span></p>
<p id="t262" class="pln"><span class="n"><a href="#t262">262</a></span><span class="t"><span class="str"> popdict (sc.dict): A dictionary mapping an individual's ID to a dictionary with their age and other attributes.</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="str"> Returns:</span> </span><span class="r"></span></p>
<p id="t264" class="pln"><span class="n"><a href="#t264">264</a></span><span class="t"><span class="str"> A dictionary listing UIDs for each age.</span> </span><span class="r"></span></p>
<p id="t265" class="pln"><span class="n"><a href="#t265">265</a></span><span class="t"><span class="str"> """</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">uids_by_age_dic</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span> </span><span class="r"></span></p>
<p id="t267" class="run"><span class="n"><a href="#t267">267</a></span><span class="t"> <span class="key">for</span> <span class="nam">uid</span> <span class="key">in</span> <span class="nam">popdict</span><span class="op">:</span> </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">uids_by_age_dic</span><span class="op">.</span><span class="nam">setdefault</span><span class="op">(</span><span class="nam">popdict</span><span class="op">[</span><span class="nam">uid</span><span class="op">]</span><span class="op">[</span><span class="str">'age'</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="t269" class="run"><span class="n"><a href="#t269">269</a></span><span class="t"> <span class="nam">uids_by_age_dic</span><span class="op">[</span><span class="nam">popdict</span><span class="op">[</span><span class="nam">uid</span><span class="op">]</span><span class="op">[</span><span class="str">'age'</span><span class="op">]</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="t270" class="run"><span class="n"><a href="#t270">270</a></span><span class="t"> <span class="key">return</span> <span class="nam">uids_by_age_dic</span> </span><span class="r"></span></p>
</div>
<div id="footer">
<div class="content">
<p>
<a class="nav" href="index.html">« index</a> <a class="nav" href="https://coverage.readthedocs.io">coverage.py v5.3</a>,
created at 2020-10-05 17:01 -0700
</p>
</div>
</div>
</body>
</html>