-
Notifications
You must be signed in to change notification settings - Fork 0
/
livestream.py
132 lines (115 loc) · 2.85 KB
/
livestream.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
from oracle import *
import argparse
from collections import deque
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import seaborn as sns
from time import time
# Check for arguments.
parser = argparse.ArgumentParser(description="Visualize data.")
parser.add_argument(
"-w",
"--width",
nargs="?",
default=15,
type=float,
help="Width of the plot, in seconds.",
)
parser.add_argument(
"-m",
"--min",
nargs="?",
default=800,
type=float,
help="Minimum y-scale of plot (Ohms).",
)
parser.add_argument(
"-x",
"--max",
nargs="?",
default=1100,
type=float,
help="Maximum y-scale of plot (Ohms).",
)
parser.add_argument(
"-f",
"--filter",
nargs="?",
default=3,
type=float,
help="Number of filter taps to use; 0 for no filtering.",
)
parser.add_argument(
"-c",
"--channel",
nargs="?",
default=0,
type=float,
help="Which channel should we plot?",
)
parser.add_argument(
"-a",
"--autoscale",
nargs="?",
default=1,
type=float,
help="Autoscale on (1) or off (0)",
)
parser.add_argument(
"-s",
"--save",
nargs="?",
default=0,
type=float,
help="Save the data to disk (1) or not (0)",
)
args = parser.parse_args()
# Are we autoscaling?
autoscale = args.autoscale
# Should we save the data?
save_data = args.save
# What channel are we plotting?
channels = [int(args.channel)]
print(f"> Plotting data from channel {channels[0]}.")
# Create an oracle object that streams data from the board.
filter_taps = args.filter
# channels = [1]
oracle = Oracle(channels=channels, nb_taps=filter_taps, do_save_data=save_data)
# Define width of plot (in seconds).
width_in_seconds = args.width
dt = 1 / 98 # estimate! May vary with machine/biomonitor.
min_ohms = args.min
max_ohms = args.max
# Plot all the things.
plt.close("all")
sns.set_context("poster")
fig, ax = plt.subplots(figsize=(15, 6))
plt.ylim([min_ohms, max_ohms])
# Set up the line plots.
x = np.arange(-width_in_seconds, 0, dt)
y = deque(np.zeros_like(x), maxlen=len(x))
line, = ax.plot(x, y)
plt.xlabel("Time Relative to Now (seconds)")
plt.ylabel("Bioimpedance (Ohms)")
def init():
"""Only required for blitting to give a clean slate."""
line.set_ydata([np.nan] * len(x))
return (line,)
def animate(i):
t_ = True
while t_:
t_, y_ = next(oracle.buffer[channels[0]].sample)
if y_:
y.append(y_)
line.set_ydata(y) # update the data.
mu = np.mean(y)
std = np.std(y)
if autoscale:
plt.ylim([mu - 5 * std, mu + 5 * std])
return (line,)
# Launch the animation; as long as there is more data available, the plot shifts left.
ani = animation.FuncAnimation(
fig, animate, init_func=init, interval=1, blit=False, save_count=100
)
plt.show()