generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_rate_graphs.py
72 lines (65 loc) · 2.54 KB
/
draw_rate_graphs.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
color_cycle = ["#ff7f00", "#3579b1", "#3b86c4", "#4e92ca", "#629fd0", "#76abd6"]
old_names = ["0 days", "30 days", "60 days", "90 days", "120 days"]
new_names = ["0-29 days", "30-59 days", "60-89 days", "90-119 days", "120+ days"]
groups = ["covid", "pneumonia"]
titles = [
"a) Patients discharged after hospitalisation with COVID-19",
"b) Patients discharged after hospitalisation with pneumonia",
"c) Patients positive for SARS-CoV-2, not hospitalised",
]
df = pd.DataFrame()
for f in groups:
df = df.append(pd.read_csv(f"released_outputs/output/tabfig/rates_summary_{f}.csv"))
df = df.replace(to_replace=dict(zip(old_names, new_names)))
df = df.set_index(["group", "outcome", "time"])
df = df.loc[df["variable"] == "Overall"]
print(df)
def plot_rates(outcome_group, rows):
fig, axes = plt.subplots(ncols=2, nrows=1, sharey=True, figsize=[12, 6])
for i, ax in enumerate(axes.flat):
df_to_plot = df.loc[df.index.isin(rows, level=1)]
df_to_plot = df_to_plot.loc[groups[i]]
df_to_plot = df_to_plot * 120
df_to_plot = df_to_plot.unstack(level=-1)
errlo = df_to_plot["rate_ppm"] - df_to_plot["lc_ppm"]
errhi = df_to_plot["uc_ppm"] - df_to_plot["rate_ppm"]
errlo_arr = errlo.values[:, np.newaxis, :]
errhi_arr = errhi.values[:, np.newaxis, :]
yerr = np.append(errlo_arr, errhi_arr, axis=1).T
data = df_to_plot["rate_ppm"]
data = data[["Full period"] + new_names]
data.plot(kind="bar", ax=ax, width=0.8, yerr=yerr, color=color_cycle)
ax.set_title(titles[i], loc="left")
ax.legend(loc=2).set_title("Time since hospital discharge")
ax.set_xticklabels(labels)
ax.set_ylabel("Rate of each outcome (per 1,000 person years)")
ax.grid(b=True, axis="y", color="#666666", linestyle="-", alpha=0.1)
plt.tight_layout()
plt.savefig(f"output/rate_graphs{outcome_group}.svg")
outcome_groups = {
"_cens_gp": [
"stroke_cens_gp",
"dvt_cens_gp",
"pe_cens_gp",
"heart_failure_cens_gp",
"mi_cens_gp",
"aki",
"t2dm",
],
"_all": ["stroke", "dvt", "pe", "heart_failure", "mi", "aki", "t2dm"],
"_no_gp": [
"stroke_no_gp",
"dvt_no_gp",
"pe_no_gp",
"heart_failure_no_gp",
"mi_no_gp",
"aki",
"t2dm",
],
}
labels = ["Stroke", "DVT", "PE", "Heart failure", "MI", "AKI", "T2DM"]
for group, rows in outcome_groups.items():
plot_rates(group, rows)