Argparse : How to setup argument (for BrainFlow and OpenBCI)

398 Views Asked by At

Hello guys (or girls)!

I recently purchased an eeg headset and in order to be able to read data from python I need to be able to receive information from a doogle. To do this I need to use BrainFlow, which seems to be the most suitable centralized multi-language solution. However I'm not used to using Argparse, whose role is to receive arguments (from a yml? js file? directly in the code?)

Anyway, can someone tell me how to provide arguments to argparse?

BTW here is the code :

import argparse
import time

from brainflow.board_shim import BoardShim, BrainFlowInputParams


def main():
    BoardShim.enable_dev_board_logger()

    parser = argparse.ArgumentParser()
    # use docs to check which parameters are required for specific board, e.g. for Cyton - set serial port
    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('--streamer-params', type=str, help='streamer params', required=False, default='')
    parser.add_argument('--serial-number', type=str, help='serial number', required=False, default='0')
    parser.add_argument('--board-id', type=int, help='board id, check docs to get a list of supported boards',
                        required=True)
    parser.add_argument('--file', type=str, help='file', required=False, default='')
    args = parser.parse_args()

    params = BrainFlowInputParams()
    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

    board = BoardShim(args.board_id, params)
    board.prepare_session()
    # board.start_stream () # use this for default options
    board.start_stream(45000, args.streamer_params)
    time.sleep(10)
    # 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)


if __name__ == "__main__":
    main()

Whenever I run the code in the cmd like this : python test.py, it says that board-id argument is required. Same when I do python test.py 0 or python test.py "0".

So my question is : How do I setup argument for argparse ?

Thank you in advance :) , Best, KL

1

There are 1 best solutions below

0
On

I think it's a little bit late to answer that, but as mentioned before, when you run your code, run it from the terminal with the arguments needed:

e.p. python script.py --board-id 0 --serial-port COM5

this will consider you're using an OpenBCI Cyton board, which has the id 0, using the port COM5. You can check what id your device has from the brainflow documentation. To know what port you're using, your device's documentation should show how. I'm familiar with OpenBCI. One easy way, to know what port you're using, is from the OpenBCI's GUI.

If you're using the same setup, you can set these in the default argument, so you don't have to specify them each time you run your code.

parser.add_argument('--serial-port', type=str, help='serial port', required=False, default='COM5')

To test the code, you can use a synthetic board, which has the id -1:

python script.py --board-id -1

(no need for a serial port to be specified here)

Hope that helps.

Best of luck!