generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilities.py
1002 lines (842 loc) · 30.9 KB
/
utilities.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
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
import re
import json
import os
from pathlib import Path
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
from collections import Counter
from datetime import timedelta as td
backend = os.getenv("OPENSAFELY_BACKEND", "expectations")
BASE_DIR = Path(__file__).parents[1]
OUTPUT_DIR = BASE_DIR / "output"
ANALYSIS_DIR = BASE_DIR / "analysis"
BEST = 0
UPPER_RIGHT = 1
UPPER_LEFT = 2
LOWER_LEFT = 3
LOWER_RIGHT = 4
RIGHT = 5
CENTER_LEFT = 6
CENTER_RIGHT = 7
LOWER_CENTER = 8
UPPER_CENTER = 9
CENTER = 10
def match_input_files(file: str) -> bool:
"""Checks if file name has format outputted by cohort extractor"""
pattern = r"^input_20\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.feather"
return True if re.match(pattern, file) else False
def match_input_files_filtered(file: str) -> bool:
pattern = (
r"^input_filtered_20\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.feather"
)
return True if re.match(pattern, file) else False
def match_egfr_files(file: str) -> bool:
"""Checks if file name has format outputted by cohort extractor"""
pattern = r"^input_egfr.*\.feather"
return True if re.match(pattern, file) else False
def match_measure_files(file: str) -> bool:
"""Checks if file name has format outputted by cohort extractor (generate_measures action)"""
pattern = r"^measure_.*_rate\.csv"
return True if re.match(pattern, file) else False
def get_date_input_file(file: str) -> str:
"""Gets the date in format YYYY-MM-DD from input file name string"""
# check format
if not match_input_files(file):
raise Exception("Not valid input file format")
else:
date = re.search(r"input_(.*)\.feather", file)
return date.group(1)
def get_date_input_file_filtered(file: str) -> str:
"""Gets the date in format YYYY-MM-DD from input file name string"""
# check format
if not match_input_files_filtered(file):
raise Exception("Not valid input file format")
else:
date = re.search(r"input_filtered_(.*)\.feather", file)
return date.group(1)
def get_date_egfr_file(file: str) -> str:
"""Gets the date in format YYYY-MM-DD from input file name string"""
# check format
if not match_egfr_files(file):
raise Exception("Not valid input file format")
else:
date = re.search(r"input_egfr(.*)\.feather", file)
return date.group(1)
def validate_directory(dirpath):
if not dirpath.is_dir():
raise ValueError(f"Not a directory")
def join_ethnicity_region(directory: str) -> None:
"""Finds 'input_ethnicity.feather' in directory and combines with each input file."""
dirpath = Path(directory)
validate_directory(dirpath)
filelist = dirpath.iterdir()
# get ethnicity input file
ethnicity_df = pd.read_feather(dirpath / "input_ethnicity.feather")
# ONS MSOA to region map from here:
# https://geoportal.statistics.gov.uk/datasets/fe6c55f0924b4734adf1cf7104a0173e_0/data
msoa_to_region = pd.read_csv(
ANALYSIS_DIR / "ONS_MSOA_to_region_map.csv",
usecols=["MSOA11CD", "RGN11NM"],
dtype={"MSOA11CD": "category", "RGN11NM": "category"},
)
ethnicity_dict = dict(zip(ethnicity_df["patient_id"], ethnicity_df["ethnicity"]))
msoa_dict = dict(zip(msoa_to_region["MSOA11CD"], msoa_to_region["RGN11NM"]))
for file in filelist:
if match_input_files(file.name):
df = pd.read_feather(dirpath / file.name)
df["ethnicity"] = df["patient_id"].map(ethnicity_dict)
df["region"] = df["msoa"].map(msoa_dict)
df.to_feather(dirpath / file.name)
def count_comparator_value_pairs(directory: str) -> None:
"""Finds 'input_XX-XX-XX.feather' in directory extracts counts of the egfr value/comparator data."""
dirpath = Path(directory)
validate_directory(dirpath)
filelist = dirpath.iterdir()
comparator_value_list = []
flagged_comparator_value_list = []
for file in filelist:
if match_input_files(file.name):
print(f"Reading file [{dirpath}/{file.name}]")
df = pd.read_feather(dirpath / file.name)
match_counts = (
df.groupby(["egfr_less_than_45", "egfr_between_1_and_45"])
.size()
.reset_index(name="count")
)
print(match_counts)
denominator_counts = (
df.groupby(["indicator_k_denominator", "egfr_between_1_and_45"])
.size()
.reset_index(name="count")
)
print(denominator_counts)
df["egfr_comparator"] = df["egfr_comparator"].astype("string")
df.fillna({"egfr_comparator": "", "egfr": -1}, inplace=True)
cv_pairs = [
"".join(i)
for i in zip(df["egfr_comparator"], df["egfr"].map(round).map(str))
]
comparator_value_list = comparator_value_list + cv_pairs
df_flagged = df.query("egfr_between_1_and_45 == '1'")
cv_pairs_flagged = [
"".join(i)
for i in zip(
df_flagged["egfr_comparator"],
df_flagged["egfr"].map(round).map(str),
)
]
flagged_comparator_value_list = (
flagged_comparator_value_list + cv_pairs_flagged
)
comparator_value_df = pd.DataFrame.from_dict(
Counter(comparator_value_list), orient="index"
).reset_index()
comparator_value_df.columns = ["cv_pair", "count"]
comparator_value_df_filtered = comparator_value_df.query("count > 5")
comparator_value_df_filtered.to_csv(
dirpath / "EGFR_comparator-value_counts_new-method.csv", index=False
)
flagged_comparator_value_df = pd.DataFrame(
sorted(set(flagged_comparator_value_list)), columns=["colummn"]
)
flagged_comparator_value_df.to_csv(
dirpath / "EGFR_flagged_comparator-value_counts_new-method.csv", index=False
)
def calculate_rate(df, value_col: str, population_col: str, rate_per: int):
"""Calculates the rate of events for given number of the population.
Args:
df: A measure table.
value_col: The name of the numerator column in the measure table.
population_col: The name of the denominator column in the measure table.
rate_per: Population size to calculate rate by.
Returns:
A pandas series with rate values
"""
rate = df[value_col] / (df[population_col] / rate_per)
return rate
def redact_small_numbers(df, n, numerator, denominator, rate_column, date_column):
"""
Takes counts df as input and suppresses low numbers. Sequentially redacts
low numbers from numerator and denominator until count of redcted values >=n.
Rates corresponding to redacted values are also redacted.
df: input df
n: threshold for low number suppression
numerator: numerator column to be redacted
denominator: denominator column to be redacted
"""
def suppress_column(column):
suppressed_count = column[column <= n].sum()
# if 0 dont need to suppress anything
if suppressed_count == 0:
pass
else:
column[column <= n] = np.nan
while suppressed_count <= n:
suppressed_count += column.min()
column[column.idxmin()] = np.nan
return column
df_list = []
dates = df[date_column].unique()
for d in dates:
df_subset = df.loc[df[date_column] == d, :]
for column in [numerator, denominator]:
df_subset[column] = suppress_column(df_subset[column])
df_subset.loc[
(df_subset[numerator].isna()) | (df_subset[denominator].isna()), rate_column
] = np.nan
df_list.append(df_subset)
return pd.concat(df_list, axis=0)
def plot_measures(
df,
filename: str,
title: str,
column_to_plot: str,
y_label: str,
as_bar: bool = False,
category: str = None,
):
"""Produce time series plot from measures table. One line is plotted for each sub
category within the category column. Saves output in 'output' dir as jpeg file.
Args:
df: A measure table
title: Plot title
column_to_plot: Column name for y-axis values
y_label: Label to use for y-axis
as_bar: Boolean indicating if bar chart should be plotted instead of line chart. Only valid if no categories.
category: Name of column indicating different categories
"""
plt.figure(figsize=(15, 8))
if category:
for unique_category in sorted(df[category].unique()):
# subset on category column and sort by date
df_subset = df[df[category] == unique_category].sort_values("date")
plt.plot(df_subset["date"], df_subset[column_to_plot])
else:
if bar:
df.plot.bar("date", column_to_plot, legend=False)
else:
plt.plot(df["date"], df[column_to_plot])
x_labels = sorted(df["date"].unique())
plt.ylabel(y_label)
plt.xlabel("Date")
plt.xticks(x_labels, rotation="vertical")
plt.title(title)
plt.ylim(
bottom=0,
top=100
if df[column_to_plot].isnull().values.all()
else df[column_to_plot].max() * 1.05,
)
if category:
plt.legend(
sorted(df[category].unique()), bbox_to_anchor=(1.04, 1), loc="upper left"
)
plt.tight_layout()
plt.savefig(f"output/figures/{filename}.jpeg")
plt.clf()
# https://github.com/ebmdatalab/datalab-pandas/blob/master/ebmdatalab/charts.py
def deciles_chart_ebm(
df,
period_column=None,
column=None,
title="",
ylabel="",
show_outer_percentiles=True,
show_legend=True,
ax=None,
):
"""period_column must be dates / datetimes"""
sns.set_style("whitegrid", {"grid.color": ".9"})
if not ax:
fig, ax = plt.subplots(1, 1, figsize=(15, 8))
df = compute_deciles(df, period_column, count_column, column)
linestyles = {
"decile": {
"line": "b--",
"linewidth": 1,
"label": "decile",
},
"median": {
"line": "b-",
"linewidth": 1.5,
"label": "median",
},
"percentile": {
"line": "b:",
"linewidth": 0.8,
"label": "1st-9th, 91st-99th percentile",
},
}
label_seen = []
for percentile in range(1, 100): # plot each decile line
data = df[df["percentile"] == percentile]
add_label = False
if percentile == 50:
style = linestyles["median"]
add_label = True
elif show_outer_percentiles and (percentile < 10 or percentile > 90):
style = linestyles["percentile"]
if "percentile" not in label_seen:
label_seen.append("percentile")
add_label = True
else:
style = linestyles["decile"]
if "decile" not in label_seen:
label_seen.append("decile")
add_label = True
if add_label:
label = style["label"]
else:
label = "_nolegend_"
ax.plot(
data[period_column],
data[column],
style["line"],
linewidth=style["linewidth"],
label=label,
)
ax.set_ylabel(ylabel, size=15, alpha=0.6)
if title:
ax.set_title(title, size=14, wrap=True)
# set ymax across all subplots as largest value across dataset
ax.set_ylim(
[0, 100 if df[column].isnull().values.all() else df[column].max() * 1.05]
)
ax.tick_params(labelsize=12)
ax.set_xlim(
[df[period_column].min(), df[period_column].max()]
) # set x axis range as full date range
plt.setp(ax.xaxis.get_majorticklabels(), rotation=90)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%B %Y"))
if show_legend:
ax.legend(
bbox_to_anchor=(1.1, 0.8), # arbitrary location in axes
# specified as (x0, y0, w, h)
loc=CENTER_LEFT, # which part of the bounding box should
# be placed at bbox_to_anchor
ncol=1, # number of columns in the legend
fontsize=20,
borderaxespad=0.0,
) # padding between the axes and legend
# specified in font-size units
plt.xticks(sorted(df[period_column].unique()), rotation=90)
return plt
def compute_deciles(measure_table, groupby_col, values_col, has_outer_percentiles=True):
"""Computes deciles.
Args:
measure_table: A measure table.
groupby_col: The name of the column to group by.
values_col: The name of the column for which deciles are computed.
has_outer_percentiles: Whether to compute the nine largest and nine smallest
percentiles as well as the deciles.
Returns:
A data frame with `groupby_col`, `values_col`, and `percentile` columns.
"""
quantiles = np.arange(0.1, 1, 0.1)
if has_outer_percentiles:
quantiles = np.concatenate(
[quantiles, np.arange(0.01, 0.1, 0.01), np.arange(0.91, 1, 0.01)]
)
percentiles = (
measure_table.groupby(groupby_col)[values_col]
.quantile(pd.Series(quantiles))
.reset_index()
)
percentiles["percentile"] = percentiles["level_1"].apply(lambda x: int(x * 100))
percentiles = percentiles.drop(columns=["level_1"])
return percentiles
def get_practice_deciles(measure_table, value_column):
measure_table["percentile"] = measure_table.groupby(["date"])[
value_column
].transform(lambda x: pd.cut(x, 100, labels=range(1, 101)))
return measure_table
def deciles_chart(
df,
filename,
period_column=None,
column=None,
title="",
ylabel="",
time_window="",
):
"""period_column must be dates / datetimes"""
df = compute_deciles(df, period_column, column, has_outer_percentiles=False)
sns.set_style("whitegrid", {"grid.color": ".9"})
fig, ax = plt.subplots(1, 1, figsize=(15, 8))
linestyles = {
"decile": {
"line": "b--",
"linewidth": 1,
"label": "Decile",
},
"median": {
"line": "b-",
"linewidth": 1.5,
"label": "Median",
},
"percentile": {
"line": "b:",
"linewidth": 0.8,
"label": "1st-9th, 91st-99th percentile",
},
}
label_seen = []
for percentile in range(1, 100): # plot each decile line
data = df[df["percentile"] == percentile]
add_label = False
if percentile == 50:
style = linestyles["median"]
add_label = True
else:
style = linestyles["decile"]
if "decile" not in label_seen:
label_seen.append("decile")
add_label = True
if add_label:
label = style["label"]
else:
label = "_nolegend_"
ax.plot(
data[period_column],
data[column],
style["line"],
linewidth=style["linewidth"],
label=label,
)
ax.set_ylabel(ylabel, size=15, alpha=0.6)
if title:
ax.set_title(title, size=14, wrap=True)
# set ymax across all subplots as largest value across dataset
ax.set_ylim(
[0, 100 if df[column].isnull().values.all() else df[column].max() * 1.05]
)
ax.tick_params(labelsize=12)
ax.set_xlim(
[df[period_column].min(), df[period_column].max()]
) # set x axis range as full date range
plt.setp(ax.xaxis.get_majorticklabels(), rotation=90)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%B %Y"))
plt.xticks(sorted(df[period_column].unique()), rotation=90)
plt.vlines(
x=[pd.to_datetime("2020-03-01")],
ymin=0,
ymax=100,
colors="orange",
ls="--",
label="National Lockdown",
)
if not time_window == "":
plt.vlines(
x=[pd.to_datetime(time_window)],
ymin=0,
ymax=100,
colors="green",
ls="--",
label="Date of expected impact",
)
ax.legend(
bbox_to_anchor=(1.1, 0.8), # arbitrary location in axes
# specified as (x0, y0, w, h)
loc=CENTER_LEFT, # which part of the bounding box should
# be placed at bbox_to_anchor
ncol=1, # number of columns in the legend
fontsize=20,
borderaxespad=0.0,
) # padding between the axes and legend
# specified in font-size units
plt.tight_layout()
plt.savefig(filename)
plt.clf()
def get_composite_indicator_counts(df, numerators, denominator: str, date: str):
"""
Takes a df and list of numerators that form a composite indicator and returns a
dataframe with the counts of individuals who have varying numbers of the indicators
within each composite.
"""
indicator_num = df.loc[:, numerators].sum(axis=1)
count_df = indicator_num.value_counts().reset_index()
count_df = count_df.rename(
columns={"index": "num_indicators", 0: "count"}
).sort_values(by="num_indicators")
# drop count of individuals with no indicators within composite
count_df = count_df[count_df["num_indicators"] != 0]
count_df["date"] = date
count_df["denominator"] = df[denominator].sum()
return count_df
def co_prescription(df, medications_x: str, medications_y: str) -> None:
"""
Takes in an input.csv file containing necessary co-prescribing vars
and generates a new column indicating co-prescribing of medications_x
and medications_y.
"""
columns = [
medications_x,
medications_y,
f"earliest_{medications_x}_month_3",
f"earliest_{medications_x}_month_2",
f"earliest_{medications_x}_month_1",
f"earliest_{medications_y}_month_3",
f"earliest_{medications_y}_month_2",
f"earliest_{medications_y}_month_1",
f"latest_{medications_x}_month_3",
f"latest_{medications_x}_month_2",
f"latest_{medications_x}_month_1",
f"latest_{medications_y}_month_3",
f"latest_{medications_y}_month_2",
f"latest_{medications_y}_month_1",
]
# check df contains all necessary co-prescribing vars and convert to datetime
for column in columns:
assert column in df.columns
if column not in [medications_x, medications_y]:
df[column] = pd.to_datetime(df[column])
df[f"co_prescribed_{medications_x}_{medications_y}"] = (
(df[f"{medications_x}"] == 1) & (df[f"{medications_y}"] == 1)
) & (
(
(
df[f"earliest_{medications_x}_month_3"]
< (df[f"earliest_{medications_y}_month_3"] + td(days=28))
)
& (
df[f"earliest_{medications_x}_month_3"]
> (df[f"earliest_{medications_y}_month_3"] - td(days=28))
)
)
| (
(
df[f"earliest_{medications_x}_month_3"]
< (df[f"latest_{medications_y}_month_3"] + td(days=28))
)
& (
df[f"earliest_{medications_x}_month_3"]
> (df[f"latest_{medications_y}_month_3"] - td(days=28))
)
)
| (
(
df[f"latest_{medications_x}_month_3"]
< (df[f"earliest_{medications_y}_month_3"] + td(days=28))
)
& (
df[f"latest_{medications_x}_month_3"]
> (df[f"earliest_{medications_y}_month_3"] - td(days=28))
)
)
| (
(
df[f"latest_{medications_x}_month_3"]
< (df[f"latest_{medications_y}_month_3"] + td(days=28))
)
& (
df[f"latest_{medications_x}_month_3"]
> (df[f"latest_{medications_y}_month_3"] - td(days=28))
)
)
| (
df[f"latest_{medications_x}_month_3"]
> (df[f"earliest_{medications_y}_month_2"] - td(days=28))
)
| (
df[f"earliest_{medications_x}_month_2"]
< (df[f"latest_{medications_y}_month_3"] + td(days=28))
)
| (
(
df[f"earliest_{medications_x}_month_2"]
< (df[f"earliest_{medications_y}_month_2"] + td(days=28))
)
& (
df[f"earliest_{medications_x}_month_2"]
> (df[f"earliest_{medications_y}_month_2"] - td(days=28))
)
)
| (
(
df[f"earliest_{medications_x}_month_2"]
< (df[f"latest_{medications_y}_month_2"] + td(days=28))
)
& (
df[f"earliest_{medications_x}_month_2"]
> (df[f"latest_{medications_y}_month_2"] - td(days=28))
)
)
| (
(
df[f"latest_{medications_x}_month_2"]
< (df[f"earliest_{medications_y}_month_2"] + td(days=28))
)
& (
df[f"latest_{medications_x}_month_2"]
> (df[f"earliest_{medications_y}_month_2"] - td(days=28))
)
)
| (
(
df[f"latest_{medications_x}_month_2"]
< (df[f"latest_{medications_y}_month_2"] + td(days=28))
)
& (
df[f"latest_{medications_x}_month_2"]
> (df[f"latest_{medications_y}_month_2"] - td(days=28))
)
)
| (
df[f"latest_{medications_x}_month_2"]
> (df[f"earliest_{medications_y}_month_1"] - td(days=28))
)
| (
df[f"earliest_{medications_x}_month_1"]
< (df[f"latest_{medications_y}_month_2"] + td(days=28))
)
| (
(
df[f"earliest_{medications_x}_month_1"]
< (df[f"earliest_{medications_y}_month_1"] + td(days=28))
)
& (
df[f"earliest_{medications_x}_month_1"]
> (df[f"earliest_{medications_y}_month_1"] - td(days=28))
)
)
| (
(
df[f"earliest_{medications_x}_month_1"]
< (df[f"latest_{medications_y}_month_1"] + td(days=28))
)
& (
df[f"earliest_{medications_x}_month_1"]
> (df[f"latest_{medications_y}_month_1"] - td(days=28))
)
)
| (
(
df[f"latest_{medications_x}_month_1"]
< (df[f"earliest_{medications_y}_month_1"] + td(days=28))
)
& (
df[f"latest_{medications_x}_month_1"]
> (df[f"earliest_{medications_y}_month_1"] - td(days=28))
)
)
| (
(
df[f"latest_{medications_x}_month_1"]
< (df[f"latest_{medications_y}_month_1"] + td(days=28))
)
& (
df[f"latest_{medications_x}_month_1"]
> (df[f"latest_{medications_y}_month_1"] - td(days=28))
)
)
)
df[f"co_prescribed_{medications_x}_{medications_y}"] = df[
f"co_prescribed_{medications_x}_{medications_y}"
].map({False: 0, True: 1})
def drop_irrelevant_practices(df):
"""Drops irrelevant practices from the given measure table.
An irrelevant practice has zero events during the study period.
Args:
df: A measure table.
Returns:
A copy of the given measure table with irrelevant practices dropped.
"""
is_relevant = df.groupby("practice").value.any()
return df[df.practice.isin(is_relevant[is_relevant == True].index)]
def suppress_practice_measures(df, n, numerator, denominator, rate_column):
df_grouped = df.groupby(by=["date"])[[numerator, denominator]].sum().reset_index()
df_grouped["rate"] = (df_grouped[numerator] / df_grouped[denominator]) * 1000
df_grouped = redact_small_numbers(df_grouped, 10, numerator, denominator, "rate")
dates_to_drop = df_grouped.loc[
(df_grouped[numerator].isnull()) | (df_grouped[denominator].isnull(), "date")
]
df["drop"] = df["date"].map(dates_to_drop)
df.loc[df["drop"] == True, [numerator, denominator, rate_column]] = np.nan
return df
def group_low_values(df, value_col, population_col, term_col):
def suppress(df):
suppressed_count = df.loc[df[value_col] <= 5, value_col].sum()
# population_suppressed_count = df.loc[df[value_col]<=5, population_col].sum()
population = df[population_col].mean()
if suppressed_count == 0:
pass
else:
df.loc[df[value_col] <= 5, value_col] = np.nan
while suppressed_count <= 5:
suppressed_count += df[value_col].min()
df.loc[df[value_col].idxmin(), value_col] = np.nan
# population_suppressed_count += df.loc[df[value_col].idxmin(), population_col]
df = df[df[value_col].notnull()]
other_row = {
term_col: "Other",
value_col: suppressed_count,
"date": df["date"].unique()[0],
population_col: population,
}
df = df.append(other_row, ignore_index=True)
return df
return df.groupby(by=["date"]).apply(suppress).reset_index(drop=True)
def get_number_practices(df):
"""Gets the number of practices in the given measure table.
Args:
df: A measure table.
"""
indicator = ""
for column in df.columns:
if re.match(r"indicator_(.*)_numerator", column):
indicator = re.search(r"indicator_(.*)_numerator", column).group(1)
practices_with_value = df.loc[
df[f"indicator_{indicator}_numerator"] > 0, "practice"
]
return len(practices_with_value.unique())
def get_percentage_practices(measure_table):
"""Gets the percentage of practices in the given measure table.
Args:
measure_table: A measure table.
"""
with open(OUTPUT_DIR / f"practice_count_{backend}.json") as f:
num_practices = json.load(f)["num_practices"]
num_practices_in_study = get_number_practices(measure_table)
return num_practices_in_study, np.round(
(num_practices_in_study / num_practices) * 100, 2
)
def get_number_events(measure_table, measure_id):
"""Gets the number of events.
Args:
measure_table: A measure table.
measure_id: The measure ID.
"""
return measure_table[f"indicator_{measure_id}_numerator"].sum()
def get_number_patients(measure_id, denominator_or_numerator):
"""Gets the number of patients.
Args:
measure_id: The measure ID.
denominator_or_numerator: String dictating if number of patients in numerator or denominator is returned
"""
with open(OUTPUT_DIR / f"patient_count_{backend}.json") as f:
d = json.load(f)
return d["num_patients"][denominator_or_numerator][measure_id]
def deciles_chart_subplots(
df,
period_column=None,
column=None,
title="",
ylabel="",
show_outer_percentiles=True,
show_legend=True,
ax=None,
time_window="",
):
"""period_column must be dates / datetimes"""
sns.set_style("whitegrid", {"grid.color": ".9"})
df = compute_deciles(df, period_column, column, has_outer_percentiles=False)
linestyles = {
"decile": {
"line": "b--",
"linewidth": 1,
"label": "Decile",
},
"median": {
"line": "b-",
"linewidth": 1.5,
"label": "Median",
},
"percentile": {
"line": "b:",
"linewidth": 0.8,
"label": "1st-9th, 91st-99th percentile",
},
}
label_seen = []
for percentile in range(1, 100): # plot each decile line
data = df[df["percentile"] == percentile]
add_label = False
if percentile == 50:
style = linestyles["median"]
add_label = True
elif show_outer_percentiles and (percentile < 10 or percentile > 90):
style = linestyles["percentile"]
if "percentile" not in label_seen:
label_seen.append("percentile")
add_label = True
else:
style = linestyles["decile"]
if "decile" not in label_seen:
label_seen.append("decile")
add_label = True
if add_label:
label = style["label"]
else:
label = "_nolegend_"
ax.plot(
data[period_column],
data[column],
style["line"],
linewidth=style["linewidth"],
label=label,
)
ax.vlines(
x=[pd.to_datetime("2020-03-01")],
ymin=0,
ymax=100,
colors="orange",
ls="--",
label="National Lockdown",
)
if not time_window == "":
ax.vlines(
x=[pd.to_datetime(time_window)],
ymin=0,
ymax=100,
colors="green",
ls="--",
label="Date of expected maximum impact",
)
ax.set_ylabel(ylabel, size=24)
ax.set_title(title, size=30, wrap=False, pad=10)
# set ymax across all subplots as largest value across dataset
ax.set_ylim(
[0, df[column].max() * 1.05 if (df[column].max() * 1.05) < 100 else 100]
)
ax.tick_params(labelsize=18)
ax.set_xlim(
[df[period_column].min(), df[period_column].max()]
) # set x axis range as full date range
ax.tick_params(axis="x", labelrotation=90, size=15, labelsize=22)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%B %Y"))
ax.set_xticks(sorted(df[period_column].unique()))
if show_legend:
ax.legend(
bbox_to_anchor=(1.5, 1), # arbitrary location in axes
# specified as (x0, y0, w, h)
loc=UPPER_RIGHT, # which part of the bounding box should
# be placed at bbox_to_anchor
ncol=1, # number of columns in the legend
fontsize=28,
borderaxespad=0.0,
) # padding between the axes and legend
# specified in font-size units
return plt
def update_demographics(demographics_df, df):
"""Updates demographics_df with values from df."""
demographics_df = demographics_df.append(
df[demographics_df.columns]
).drop_duplicates(subset="patient_id", keep="last")
return demographics_df
def produce_stripped_measures(df):
"""Takes in a practice level measures file, calculates rate and strips
persistent id,including only a rate and date column. Rates are rounded
and the df is randomly shuffled to remove any potentially predictive ordering.
Returns stripped df
"""
# drop irrelevant practices
df = drop_irrelevant_practices(df)
# calculate rounded rate
df["rate"] = round(df["value"] * 100, 2)
# select only rate and date column
df = df.loc[:, ["rate", "date"]]