forked from brainflow-dev/brainflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.py
40 lines (32 loc) · 1.28 KB
/
serialization.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
import argparse
import time
import numpy as np
import pandas as pd
import brainflow
from brainflow.board_shim import BoardShim, BrainFlowInputParams, LogLevels, BoardIds
from brainflow.data_filter import DataFilter, FilterTypes, AggOperations
def main():
BoardShim.enable_dev_board_logger()
# use synthetic board for demo
params = BrainFlowInputParams()
board = BoardShim(BoardIds.SYNTHETIC_BOARD.value, params)
board.prepare_session()
board.start_stream()
BoardShim.log_message(LogLevels.LEVEL_INFO.value, 'start sleeping in the main thread')
time.sleep(10)
data = board.get_board_data()
board.stop_stream()
board.release_session()
# demo how to convert it to pandas DF and plot data
eeg_channels = BoardShim.get_eeg_channels(BoardIds.SYNTHETIC_BOARD.value)
df = pd.DataFrame(np.transpose(data))
print('Data From the Board')
print(df.head(10))
# demo for data serialization using brainflow API, we recommend to use it instead pandas.to_csv()
DataFilter.write_file(data, 'test.csv', 'w') # use 'a' for append mode
restored_data = DataFilter.read_file('test.csv')
restored_df = pd.DataFrame(np.transpose(restored_data))
print('Data From the File')
print(restored_df.head(10))
if __name__ == "__main__":
main()