-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchat_mpl_live.py
More file actions
81 lines (65 loc) · 2.32 KB
/
chat_mpl_live.py
File metadata and controls
81 lines (65 loc) · 2.32 KB
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
import time
import datetime
import signal
from secrets import token_bytes
from bitstring import BitArray
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from threading import Thread
# Global flag to stop the data collection thread
stop_flag = False
# Global lists to store data for the plot
x_data = []
y_data = []
def handle_interrupt(signal, frame):
global stop_flag
stop_flag = True
print('Data collection stopped by user.')
signal.signal(signal.SIGINT, handle_interrupt)
def collect_random_numbers(blocksize: int, interval_value: int, file_name: str) -> None:
"""
A function to collect random numbers, save to a .bin file, count number of ones,
and save timestamp and count to a .csv file.
blocksize: Number of bytes
interval_value: Interval between collections in seconds
file_name: Base name for the output files
"""
global stop_flag, x_data, y_data
while not stop_flag:
start_cap = time.time()
with open(f'{file_name}.bin', "ab") as bin_file:
data = token_bytes(blocksize)
bin_file.write(data)
bin_hex = BitArray(data)
bin_ascii = bin_hex.bin
num_ones = bin_ascii.count('1')
now = datetime.datetime.now().replace(microsecond=0)
with open(f'{file_name}.csv', "a+") as write_file:
write_file.write(f'{now},{num_ones}\n')
x_data.append(now)
y_data.append(num_ones)
end_cap = time.time()
try:
time.sleep(interval_value - (end_cap - start_cap))
except ValueError:
print('Warning: data collection is slower than the specified interval.')
def animate(i: int) -> None:
"""
Function to animate the matplotlib figure.
i: The current frame number (ignored in this function)
"""
plt.cla()
plt.plot(x_data, y_data)
plt.xticks(rotation=45)
plt.subplots_adjust(bottom=0.20)
if __name__ == "__main__":
blocksize = 256 # in bytes
interval_value = 1 # in seconds
file_name = 'output'
data_collection_thread = Thread(target=collect_random_numbers, args=(blocksize, interval_value, file_name))
data_collection_thread.start()
fig = plt.figure()
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
stop_flag = True
print('Data collection stopped.')