generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation_script.py
147 lines (132 loc) · 4.32 KB
/
validation_script.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
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
from lib_phenotype_validation import *
############################ CONFIGURE OPTIONS HERE ################################
# Import file
input_path = 'output/data/input_processed.feather'
# Definitions
definitions = ['backend_computed_bmi', 'computed_bmi', 'derived_bmi', 'recorded_bmi']
# Code dictionary
code_dict = {
"ethnicity": {
1: "White",
2: "Mixed",
3: "Asian",
4: "Black",
5: "Other",
np.nan: "Unknown",
0: "Unknown",
},
"imd": {
0: "Unknown",
1: "1 Most deprived",
2: "2",
3: "3",
4: "4",
5: "5 Least deprived",
},
}
# Other variables to include
other_vars = [
"height_backend",
"weight_backend",
]
# Dates
dates = True
date_min = '2015-03-01'
date_max = '2022-03-01'
time_delta = 'M'
# Min/max range
min_range = 4
max_range = 200
# Null value – could be multiple values in a list [0,'0',NA]
null = [0]
# Covariates
demographic_covariates = ['age_band', 'sex', 'ethnicity', 'region', 'imd']
clinical_covariates = ['dementia', 'diabetes', 'hypertension', 'learning_disability']
# Output path
output_path = 'phenotype_validation_bmi'
########################## SPECIFY ANALYSES TO RUN HERE ##############################
def main():
df_clean = import_clean(
input_path, definitions, other_vars, demographic_covariates,
clinical_covariates, null, date_min, date_max,
time_delta, output_path, code_dict, dates
)
# Count patients with records
patient_counts(
df_clean, definitions, demographic_covariates,
clinical_covariates, output_path
)
# Count patients without records
patient_counts(
df_clean, definitions, demographic_covariates,
clinical_covariates, output_path, missing=True
)
# Count number of measurements
num_measurements(
df_clean, definitions, demographic_covariates,
clinical_covariates, output_path
)
# Report distributions
report_distribution(df_clean, definitions, output_path, group='')
for group in demographic_covariates + clinical_covariates:
report_distribution(df_clean, definitions, output_path, group)
# Count values out of range
report_out_of_range(
df_clean, definitions, min_range,
null, output_path, group='', less_than=True
)
report_out_of_range(
df_clean, definitions, max_range,
null, output_path, group='', less_than=False
)
for group in demographic_covariates + clinical_covariates:
report_out_of_range(
df_clean, definitions, min_range,
null, output_path, group, less_than=True
)
report_out_of_range(
df_clean, definitions, max_range,
null, output_path, group, less_than=False
)
# Report new records over time
records_over_time(
df_clean, definitions, demographic_covariates,
clinical_covariates, output_path,''
)
# Report time between measurement and now
recent_to_now(df_clean, definitions, output_path)
# Report means over time
means_over_time(
df_clean, definitions, demographic_covariates,
clinical_covariates, output_path,''
)
# Report number of records and means over time of high computed BMI
df_high_computed = df_clean.loc[df_clean['backend_computed_bmi'] > max_range]
records_over_time(
df_high_computed, ['backend_computed_bmi'], demographic_covariates,
clinical_covariates, output_path,'_greater_than_max'
)
means_over_time(
df_high_computed, ['backend_computed_bmi'], demographic_covariates,
clinical_covariates, output_path,'_greater_than_max'
)
# Report distribution of height and weight for high computed BMI
count_table(
df_high_computed, 'height_backend',
output_path, 'height_high_computed_bmi'
)
cdf(
df_high_computed, 'height_backend',
output_path, 'height_high_computed_bmi'
)
count_table(
df_high_computed, 'weight_backend',
output_path, 'weight_high_computed_bmi'
)
cdf(
df_high_computed, 'weight_backend',
output_path, 'weight_high_computed_bmi'
)
########################## DO NOT EDIT – RUNS SCRIPT ##############################
if __name__ == "__main__":
main()