-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
52 lines (40 loc) · 1.59 KB
/
tests.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
import time
import pandas as pd
from OHA.WHO import WHO
from OHA.param_builders.who_param_builder import WhoParamsBuilder as WPB
def calculate_hearts_risk(row):
# column names: country,age,gender,bp,total_chol,smoker,diabetes,cvd_risk
is_smoker = False
has_diabetes = False
if row['smoker'] == 1:
is_smoker = True
if row['diabetes'] == 1:
has_diabetes = True
params = WPB() \
.gender(row['gender']) \
.age(row['age']) \
.sbp1(row['bp']) \
.sbp2(row['bp']) \
.chol(row['total_chol'], 'mmol/l') \
.region(row['country']) \
.smoker(is_smoker) \
.diabetic(has_diabetes) \
.build()
result = WHO().calculate(params)
# print('--> WHO:', params, ' => ', result, '---\n')
return result['risk_range']
print('---> running tests ---')
filename = 'tests/data/who_tests.csv'
tests_df = pd.read_csv(filename)
tests_df['calculated'] = tests_df.apply(calculate_hearts_risk, axis=1)
tests_df['result'] = tests_df.apply(lambda x: True if x['cvd_risk'] == x['calculated'] else False, axis=1)
print(tests_df)
df_filtered = tests_df.query('result == True')
total_tests = tests_df['result'].count()
successful_tests = df_filtered['result'].count()
success_percentage = round(((int(successful_tests) / int(total_tests)) * 100), 2)
# add the date-time to the filname
timestr = time.strftime("%Y%m%d-%H%M%S")
filename = 'tmp/test_results/who_tests_processed_' + timestr + '.csv'
print('---> completed tests ---, %s' % success_percentage, '% correct --- outputting to file', filename)
tests_df.to_csv(filename, mode='w+')