【Brainflow】get_data_from_board代码解读

对Brainflow这一python提供给脑机接口设备的库中的代码进行解读。这一篇是关于get_data_from_board的解读。

使用设备:openBCI
使用软件:openBCI Hub + openBCI GUI v5.1.0, Pycharm
使用文件:利用SYNTHETIC_BOARD生成的数据文件
使用语言:Python

因为看官方文档时看的都是英文,所以注释也使用英文写了,如有疑问可以交流。

代码原型参考这个网址。

在接下来的代码中,只有一个“#”的注释是官方原有的注释,有两个“#”的注释是我个人添加的注释。

## author: Mocode
## create time: 2023-01-12
## update time: 2023-01-13
## purpose:
##   Get data from an existing file which recorded data from openBCI (SYNTHETIC_BOARD).
##   Use "PLAYBACK_FILE_BOARD" to complete this task.

import argparse
import time
# from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds, BrainFlowPresets
## UPDATE, "BrainFlowPresets" is not needed in this program
## UPDATE, "BoardIds" is not needed either, since we can use an Integer to represent a borad
## In this program, according to DOCS(https://brainflow.readthedocs.io/en/stable/UserAPI.html#brainflow-board-shim), we use "-3" to represent "BoardIds.PLAYBACK_FILE_BOARD", use "-1" to represent "BoardIds.SYNTHETIC_BOARD"
from brainflow.board_shim import BoardShim, BrainFlowInputParams

def main():
    BoardShim.enable_dev_board_logger() ## You can comment this line to ignore the output of log.

    ## Set parameters
    parser = argparse.ArgumentParser()
    # use docs to check which parameters are required for specific board, e.g. for Cyton - set serial port
    ## The lines I comment are useless argument for "PLAYBACK_FILE_BOARD", according to DOCS.
    # parser.add_argument('--timeout', type=int, help='timeout for device discovery or connection', required=False,
    #                     default=0)
    # parser.add_argument('--ip-port', type=int, help='ip port', required=False, default=0)
    # parser.add_argument('--ip-protocol', type=int, help='ip protocol, check IpProtocolType enum', required=False,
    #                     default=0)
    # parser.add_argument('--ip-address', type=str, help='ip address', required=False, default='')
    # parser.add_argument('--serial-port', type=str, help='serial port', required=False, default='')
    # parser.add_argument('--mac-address', type=str, help='mac address', required=False, default='')
    # parser.add_argument('--other-info', type=str, help='other info', required=False, default='')
    # parser.add_argument('--serial-number', type=str, help='serial number', required=False, default='')
    parser.add_argument('--board-id', type=int, help='board id, check docs to get a list of supported boards',
                        required=True)
    ## For "--board-id", since "required" is set to "True", this parameter cannot use "default" to set.
    ## Instead, Add it in "Configuration" of Pycharm. See this blog for guidance: https://blog.csdn.net/counte_rking/article/details/78837028?spm=1001.2014.3001.5506
    parser.add_argument('--file', type=str, help='file', required=False, default='.../Recordings/OpenBCISession_.../BrainFlow-RAW_....csv') ## Change the "default" content to the route of your file.
    ## For "--file", the route of ".csv" file is needed, not ".txt" file, though they are saved in the same folder.
    # parser.add_argument('--master-board', type=int, help='master board id for streaming and playback boards',
    #                     required=False, default=-1)
    ## When board id is STREAMING_BOARD or PLAYBACK_FILE_BOARD, master board id needs to be recorded in the field of "other info" of parser.
    parser.add_argument('--other-info', type=str, help='master board id needs to be recorded in this parameter when board id is STREAMING_BOARD or PLAYBACK_FILE_BOARD',
                        required=False, default='-1')
    args = parser.parse_args()

    ## Initialize
    params = BrainFlowInputParams()
    ## The lines I comment are useless argument for "PLAYBACK_FILE_BOARD", according to DOCS.
    # params.ip_port = args.ip_port
    # params.serial_port = args.serial_port
    # params.mac_address = args.mac_address
    # params.other_info = args.other_info
    # params.serial_number = args.serial_number
    # params.ip_address = args.ip_address
    # params.ip_protocol = args.ip_protocol
    # params.timeout = args.timeout
    params.file = args.file
    # params.master_board = args.master_board
    ## "master_board" needs to be set in "other_info", explained before (line 41).
    params.other_info = args.other_info

    board = BoardShim(args.board_id, params)
    board.prepare_session()
    board.start_stream()
    time.sleep(10) ## That means waiting 10 seconds, and you will get the data that have received in this period.
    data = board.get_current_board_data(256) # get latest 256 packages or less, doesnt remove them from internal buffer
    # data = board.get_board_data()  # get all data and remove it from internal buffer
    board.stop_stream()
    board.release_session()

    print(data)
    ## The size of "data":
    ## When "data = board.get_current_board_data(256)"
    ## For example (In "sleep(x)", x >= 2, here x = 10), data.size = 8192, data.shape = (32, 256), len(data) = 32
    ## Another example (In "sleep(x)", x < 2, here x = 1, the 2nd dimension of "data.shape" may less than 256), data.size = 6240, data.shape = (32, 195), len(data) = 32
    ## When "data = board.get_board_data()"
    ## For example (In "sleep(x)", x >= 2, here x = 10), data.size = 64672, data.shape = (32, 2021), len(data) = 32
    ## The 2nd dimension of "data.shape" (noted as "y") may change when another "run" of this program happens or another "x" in "sleep(x)" is set, so as the "data.size", which equals "32 * y"
    ## In "data.shape", the 1st dimension: 32 --> 32 columns, which equals to the number of the columns in the .csv file.
    ## In "data.shape", the 2nd dimension: 256 --> 256 packages, which also means 256 data points.

if __name__ == "__main__":
    main()

你可能感兴趣的:(python,pycharm,开发语言)