-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRL_plots.py
173 lines (109 loc) · 4.74 KB
/
RL_plots.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
import torch
import numpy as np
import pandas as pd
import datetime as dt
import random
import time
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset,DataLoader
import torch.nn.functional as F
import os
import glob
import copy
import plotly.express as px
import plotly.graph_objects as go
import matplotlib.pyplot as plt
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device='cuda' if torch.cuda.is_available() else 'cpu'
import plotly.express as px
import plotly.graph_objects as go
def plot_polar_plots(pat,save=False):
pat_df=df[df.Pat==pat]
state=torch.Tensor(pat_df.iloc[:,:41].values)
exps=model.get_exp_vals(state).squeeze(-1).cpu().detach().numpy()
actions = ['Vaso :' +str(i)+' Fluids :'+ str(j) for i in range(3) for j in range(3)]
fig = go.Figure()
fig.add_trace(go.Scatterpolar(
r=(exps[0,:]+18)/36,
theta=actions,
# fill='toself',
name='Admission'
))
fig.add_trace(go.Scatterpolar(
r=(exps[-12,:]+18)/36,
theta=actions,
# fill='toself',
name='12 hours from death/Discharge'
))
fig.add_trace(go.Scatterpolar(
r=(exps[-5,:]+18)/36,
theta=actions,
# fill='toself',
name='5 hours from actual Death/Discharge'
))
fig.update_layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0, 1],
showline=True
)),
showlegend=True
)
fig.show()
if save==True:
# fig.write_html("'radarplot_{}.html".format(pat))
fig.write_image("radarplot_{}.jpg".format(pat))
actions = ['Vaso :' +str(i)+' Fluids :'+ str(j) for i in range(3) for j in range(3)]
def plot_feature_uncertainty(pat,df,feat=None,size=(20,20)):
pat_df=df[df.Pat==pat]
state=torch.FloatTensor(df[df.Pat==pat].iloc[:,:41].values).to(device)
exps=model.get_exp_vals(state).squeeze(-1).detach().cpu().numpy() #T*9
cols=['Uncertainty_act_{}'.format(i) for i in range(9)]
uncertains=pat_df[cols].values
fig = go.Figure()
for k in range(9):
label='Expected Values for '+ actions[k]
# label="Expected Values for Action : "+actions[k]
fig.add_trace(go.Scatter(x=np.arange(pat_df.shape[0]), y=exps[:,k],
mode='lines+markers', marker=dict(size=uncertains[:,k]*10),
name=label))
cols=['coral','green','darkgrey']
if feat:
for i,feature in enumerate(feat):
fig.add_trace(go.Scatter(x=np.arange(pat_df.shape[0]),y=pat_df[feature].values,
mode='lines',
name=feature,marker=dict(color=cols[i])))
fig.show()
def plot_feature_ensm_treat(pat,df,feature='SBP',size=(10,10),lam=False):
legends = ['Fluids for the Patient', 'Standardized {}'.format(feature),'Mean {}'.format(feature),'Vaso for the patient']
acts=[(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)]
plt.figure(figsize=size)
pat_df=df[df.Pat==pat]
state=torch.FloatTensor(df[df.Pat==pat].iloc[:,:41].values).to(device)
action=ensm.get_ensemble_action(state,lam=lam)
fluids_pat=[acts[action[i]][1] for i in range(action.shape[0])]
vaso_pat=[acts[action[i]][0] for i in range(action.shape[0])]
plt.plot(np.arange(pat_df.shape[0]),fluids_pat,'royalblue',linestyle='dotted',linewidth=0.8,label=legends[0],marker=".",markersize=5)
plt.plot(np.arange(pat_df.shape[0]),vaso_pat,'orange',linestyle='dotted',linewidth=0.8,label=legends[3],marker=".",markersize=5)
plt.plot(np.arange(pat_df.shape[0]),pat_df[feature].values,'g',linestyle=':',linewidth=1,marker='*',label=legends[1])
title='Lambda :'+ str(lam)
plt.title(title)
plt.hlines(df[feature].mean(),0,(pat_df.shape[0]),'k',label=legends[2])
plt.legend()
import matplotlib.pyplot as plt
def plot_feature_ensemble_exp_values(pat,feat=None,size=(20,20)):
legends=['Vaso :' +str(i)+' Fluids :'+ str(j) for i in range(3) for j in range(3)]
colors=['cornflowerblue','royalblue','midnightblue','lightcoral','indianred','darkred','orange','chocolate','grey']
plt.figure(figsize=size)
pat_df=df[df.Pat==pat]
state=torch.FloatTensor(df[df.Pat==pat].iloc[:,:41].values).to(device)
exps=ensm.get_ensemble_exp_values(state) #T*9
for k in range(9):
plt.plot(np.arange(state.shape[0]),exps[:,k].cpu().detach().numpy(),color=colors[k],linestyle='dotted',linewidth=1,label=legends[k],marker=".",markersize=4)
if feat:
plt.plot(np.arange(state.shape[0]),pat_df[feat].values,'g',linestyle=':',linewidth=1,marker='*',label='Standardized {}'.format(feat))
plt.hlines(df[feat].mean(),0,(pat_df.shape[0]),'k',label='Mean of {}'.format(feat))
plt.legend()
plt.show()