-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-Tabs.py
145 lines (106 loc) · 4.38 KB
/
7-Tabs.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
import pandas as pd
import numpy as np
import datetime
import time
import os
import bokeh.plotting as bkp
import bokeh.models as bkm
import bokeh.layouts as bkl
from demo import read_data
def monitor_file():
global temp_data
try:
line = temperature_file.readline().strip()
if line == '':
raise EOFError
line = line.split(',')
date, temp = line
date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
temp = float(temp)
new_line = {'time': [date], 'temp': [temp]}
temp_data.stream(new_line, rollover=100)
print("At {}, the temperature is {}".format(date, temp))
except EOFError:
pass
# If the temperature data file is not there, create it as an empty file
if not os.path.isfile('data/TemperatureData.txt'):
with open('data/TemperatureData.txt', 'w') as f:
f.write('')
# I need to open the file, g r a c e f u l l y
temperature_file = open('data/TemperatureData.txt', 'r', os.O_NONBLOCK)
# Initialise the data storage as empty
temp_data = bkm.ColumnDataSource({'time':[], 'temp':[]})
# Fill with the data so far.
for line in temperature_file:
line = line.strip().split(',')
date, temp = line
date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
temp = float(temp)
new_line = {'time': [date], 'temp': [temp]}
temp_data.stream(new_line, rollover=100)
# Here, we make the figure and add the line glyph
p = bkp.figure(title='Streaming Data Demo', plot_width=1600, plot_height=800, x_axis_type='datetime')
p.line(x='time', y='temp', source=temp_data, line_width=3)
# Make pretty axes
p.xaxis.formatter = bkm.DatetimeTickFormatter(
hours=["%d %B %Y"],
days=["%d %B %Y"],
months=["%d %B %Y"],
years=["%d %B %Y"],
)
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Temperature'
########################################################################################
########################################################################################
########################################################################################
heart_data = pd.read_csv('data/HeartDisease.csv', index_col=0)
heart_data = bkm.ColumnDataSource(heart_data)
column_names = ['ID','Age','Sex','cp','trestbps','chol','fbs','restecg','thalach','exang','oldpeak','num','Place']
cols = []
for column in column_names:
col = bkm.widgets.TableColumn(field=column, title=column)
cols.append(col)
table = bkm.widgets.DataTable(source=heart_data, columns=cols, width=1600, height=800, fit_columns=True, index_position=None,
reorderable = True,
selectable = True,
sortable = True,
)
########################################################################################
########################################################################################
########################################################################################
def alter_data(attr, old, new):
'''Take a ColumnDataSource, and modify it in place. Adds [offset] to the flux column's original value.'''
offset = float(new)
# Grab the stuff for tweaking
data = tab3_data.data
offset = np.exp(offset * data['phase'])
data['flux'] = data['o_flux'] + offset
data['upper_err'] = data['flux'] + data['err']
data['lower_err'] = data['flux'] - data['err']
tab3_data.data = data
fname = 'data/SDSSJ0748_0_2017-02-15_KG5.calib'
title = 'Slide my Slider ;)'
color = 'black'
# Grab the data from the file, into a pandas array
tab3_data = read_data(fname)
# Create a bokeh figure
tab3_plot = bkp.figure(title=title, plot_width=1600, plot_height=700)
# Format some stuff
tab3_plot.xaxis.axis_label = 'Phase'
tab3_plot.yaxis.axis_label = 'Flux'
# Add data to it
tab3_plot.scatter(x='phase', y='flux', source=tab3_data, color=color)
#### Lets add a slider
slider = bkm.Slider(title='Flux offset', start=-1, end=1, value=0.0, width=600, step=0.01)
#TODO:
slider.on_change('value', alter_data)
# Make layouts, and add to document
tab1_layout = bkl.row(p)
tab2_layout = bkl.row(table)
tab3_layout = bkl.column([tab3_plot, slider])
tab1 = bkm.Panel(child=tab1_layout, title='Temperature Plot')
tab2 = bkm.Panel(child=tab2_layout, title='Heart Disease Data')
tab3 = bkm.Panel(child=tab3_layout, title='Slider demo')
tabs = bkm.widgets.Tabs(tabs=[tab1, tab2, tab3])
bkp.curdoc().add_root(tabs)
bkp.curdoc().add_periodic_callback(monitor_file, 10)