-
Notifications
You must be signed in to change notification settings - Fork 1
/
1-AGE-PS.py
245 lines (121 loc) · 3.88 KB
/
1-AGE-PS.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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
#!pip install -U holoviews
#!pip install -U bokeh
#!pip install -U plotly
# In[3]:
import bokeh
#bokeh.sampledata.download()
print("done")
from bokeh.plotting import figure, output_notebook, show
output_notebook()
# In[4]:
import bokeh
bokeh.__version__
# In[5]:
import pandas as pd
import numpy as np
import holoviews as hv
from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import Slider, Button
from bokeh.sampledata import gapminder
from holoviews import dim
from holoviews import opts
from holoviews.plotting.bokeh import BokehRenderer
# In[109]:
l = ["#40fe10","#fffe10","#f25000","#ff0f00"]
# ### Gapminder test
# In[7]:
renderer = hv.renderer('bokeh')
hv.extension('plotly')
hv.extension('bokeh')
# In[85]:
df = pd.read_csv("/home/atez/Scrivania/web valley/milano_cleaned.csv")
# In[86]:
df['visit:visit'].unique()
# In[104]:
#print(df.columns.values)
# In[87]:
df_medical = df[['cod_pz','visit:visit','ana:age','lab:total_cholesterol','esa_obi:sbp','ScoreClass']]
# ### Traiettorie
# In[88]:
df_Trajectories = pd.read_csv("/home/atez/Scrivania/web valley/traj.csv", sep = ' ')
# In[89]:
df_medical.index = df_medical['cod_pz']
df_Trajectories.index = df_Trajectories['ID']
# In[90]:
df_Trajectories.head()
# In[91]:
gapminder_df = df_medical.join(df_Trajectories)
# ### Gruoped
# In[93]:
agr_1 = gapminder_df.groupby(['visit:visit','code'])[['ana:age','esa_obi:sbp','ScoreClass','lab:total_cholesterol']].median()
agr_2 = gapminder_df.groupby(['visit:visit','code'])['cod_pz'].count()
# In[94]:
df_group = agr_1.join(agr_2)
# In[96]:
df_group.reset_index(inplace=True)
# ### HV
# In[69]:
ds = hv.Dataset(df_group)
# In[70]:
# Apply dimension labels and ranges
kdims = ['ana:age', 'esa_obi:sbp']
#vdims = ['Country', 'Population', 'Group']
vdims = ['code', 'cod_pz', 'ScoreClass']
dimensions = {
'ana:age' : dict(label='Age patient', range=(30, 100)),
'esa_obi:sbp' : dict(label='systolic blood pressure', range=(90, 200)),
'cod_pz': ('Numero Pazineti', 'cod_pz')
}
# In[71]:
# Create Points plotting age vs sbp indexed by visit
gapminder_ds = ds.redim(**dimensions).to(hv.Points, kdims, vdims, 'visit:visit')
# Define annotations
text = gapminder_ds.clone({yr: hv.Text(1.2, 25, str(int(yr)), fontsize=30)
for yr in gapminder_ds.keys()})
# In[99]:
hvgapminder = (gapminder_ds * text).opts(
opts.Points(alpha=0.5, color='ScoreClass', cmap = l, line_color='black',
size=np.sqrt((dim('cod_pz'))*20), width=1200, height=800,
tools=['hover'], title='Graph of the correlation between age and blod pressure, grouped by degeneration of the disease.'),
opts.Text(text_font_size='52pt', text_color='lightgray'))
# In[73]:
# Define custom widgets
def animate_update():
year = slider.value + 1
if year > end:
year = start
slider.value = year
# In[74]:
# Update the holoviews plot by calling update with the new year.
def slider_update(attrname, old, new):
hvplot.update((new,))
callback_id = None
def animate():
global callback_id
if button.label == '► Play':
button.label = '❚❚ Pause'
callback_id = doc.add_periodic_callback(animate_update, 200)
else:
button.label = '► Play'
doc.remove_periodic_callback(callback_id)
start = 0;
end = 3;
slider = Slider(start=start, end=end, value=start, step=1, title='Visit')
slider.on_change('value', slider_update)
button = Button(label='► Play', width=60)
button.on_click(animate)
# In[75]:
# Get HoloViews plot and attach document
doc = curdoc()
hvplot = renderer.get_plot(hvgapminder, doc)
hvplot.update((1964,))
# Make a bokeh layout and add it as the Document root
plot = layout([[hvplot.state], [slider, button]], sizing_mode='fixed')
doc.add_root(plot)
# In[ ]:
# In[ ]:
# In[ ]: