This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply_exceptions.py
75 lines (70 loc) · 3.35 KB
/
apply_exceptions.py
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
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 08:53:02 2019
@author: LaurencT
"""
import numpy as np
import pandas as pd
def clear_exceptions(estimates_output, param_dict):
"""Clears exceptions column of estimates_output df for all models that are
going to be run (this is needed because the str defining exceptions get
concatenated)
"""
estimates_output = estimates_output.copy()
for code in param_dict.keys():
estimates_output.loc[code, 'exception_count'] = 0
estimates_output.loc[code, 'exception_comment'] = '.'
return estimates_output
def update_exceptions(estimates_output, code, new_comment):
"""Updates exception_count and exception comment columns of param_user for
appropriate codes
Inputs:
estimates_output - a df of parameters and estimates
code - a str - id_code in the form "dddddd000d[A-Z]"
new_comment - a str - explaining the nature of the exception
Returns:
param_user_all - a df of parameters and estimates (with updated
exception details)
"""
estimates_output.loc[code, 'exception_count'] += 1
estimates_output.loc[code, 'exception_comment'] += (new_comment+' ')
return(estimates_output)
def apply_geography_exceptions(cov_pop_burden_dict, estimates_output):
"""Applying exceptions where target_pop is non-zero only in certain geographies
Inputs:
cov_pop_burden_dict - a nested dict where keys are id_codes and then
scenarios and the values are dfs
estimates_output - a df of parameters and estimates
Returns:
cov_pop_burden_dict - updated with geographical expections applied
"""
for code in cov_pop_burden_dict.keys():
cov_pop_burden_trials = cov_pop_burden_dict[code]
if code == '2123460002B':
# Regional exception based on assumed product profile
super_regions = ['South Asia']
for index in cov_pop_burden_trials.keys():
cov_pop_burden_df = cov_pop_burden_trials[index]
cov_pop_burden_df['target_pop'] = (
np.where(np.isin(cov_pop_burden_df['super_region'], super_regions),
cov_pop_burden_df['target_pop'],
0)
)
new_comment = 'Only allowed for coverage in South Asia as in the TPP'
estimates_output= update_exceptions(estimates_output, code, new_comment)
elif code == '2123460002C':
# Regional exception based on assumed product profile
super_regions = ["Sub-Saharan Africa"]
for index in cov_pop_burden_trials.keys():
cov_pop_burden_df = cov_pop_burden_trials[index]
cov_pop_burden_df['target_pop'] = (
np.where(np.isin(cov_pop_burden_df['super_region'], super_regions),
cov_pop_burden_df['target_pop'],
0)
)
new_comment = 'Only allowed for coverage in Africa as in the TPP.'
estimates_output = update_exceptions(estimates_output, code, new_comment)
else:
# As there is no expceptions relevant here
pass
return(estimates_output, cov_pop_burden_dict)