-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelsa_4_make_figures.py
115 lines (88 loc) · 3.76 KB
/
elsa_4_make_figures.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
from pathlib import Path
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme()
from utils.elsa_dataset import ELSA_dataset
class Figure_cumulative_hazard(ELSA_dataset):
def __init__(self):
super().__init__()
self.savepath = 'outputs/plots/figure_ELSA_cumulative_incidence/'
def _get_cumulated(self, event):
zero = pd.Series([0])
event = pd.concat([event, zero]).sort_index()
return event.cumsum()
def make_plot(self):
df = self.preprocessed_df
self.N = len(df)
events = df.groupby(['J', 'X']).pid.count()
censored = events.loc[0]
event_1 = events.loc[1]
event_2 = events.loc[2]
event_3 = events.loc[3]
event_123 = event_1 + event_2 + event_3
cumulated_censored = self._get_cumulated(censored)
cumulated_events = self._get_cumulated(event_123)
incidence = cumulated_events/self.N
incidence_1 = self._get_cumulated(event_1)/self.N
incidence_2 = self._get_cumulated(event_2)/self.N
incidence_3 = self._get_cumulated(event_3)/self.N
cumulated_removed = cumulated_censored.add(cumulated_events,
fill_value=0)
survival = self.N - cumulated_removed
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax2.grid(False)
ax1.grid(False)
ax1.bar(survival.index,
survival,
alpha=0.5,
color='tab:gray',
label='Number at risk')
ax2.plot(incidence, label='Cumulative incidence')
ax2.plot(incidence_1, label=self.printed_events[1])
ax2.plot(incidence_2, label=self.printed_events[2])
ax2.plot(incidence_3, label=self.printed_events[3])
ax1.annotate(xy=(survival.index[0]+0.4, survival.iloc[0]),
xycoords='data',
text='Number at risk',
ha="left", va="top",
color='tab:gray')
ax2.annotate(xy=(incidence.index[-1], incidence.iloc[-1]-0.065),
xycoords='data',
text='Cumulative incidence',
ha="right", va="bottom",
color='tab:blue',
rotation=20)
ax2.annotate(xy=(incidence_1.index[-1], incidence_1.iloc[-1]-0.029),
xycoords='data',
text=self.printed_events[1],
ha="right", va="bottom",
color='tab:orange',
rotation=8)
ax2.annotate(xy=(incidence_2.index[-1], incidence_2.iloc[-1]+0.017),
xycoords='data',
text=self.printed_events[2],
ha="right", va="top",
color='tab:green',
rotation=9)
ax2.annotate(xy=(incidence_3.index[-1], incidence_3.iloc[-1]-0.008),
xycoords='data',
text=self.printed_events[3],
ha="right", va="bottom",
color='tab:red',
rotation=6)
ax1.set_ylabel('Number at risk', fontsize=16)
ax2.set_ylabel('Cumulative incidence', fontsize=16)
ax1.set_xlabel('Waves', fontsize=16)
ax1.tick_params(labelsize=16)
ax2.tick_params(labelsize=16)
ax2.set_ylim((0, None))
fig.tight_layout()
Path(self.savepath).mkdir(exist_ok=True, parents=True)
savepath = self.savepath+'/ELSA_cumulative_incidence'
fig.savefig(savepath+'.png')
fig.savefig(savepath+'.pdf')
print(f'Saved {savepath}')
self = Figure_cumulative_hazard()
self.make_plot()