ArgParse:如何设置参数(用于脑集和openBCI)
大家好(或女孩)!
我最近购买了一款EEG耳机,为了能够从Python读取数据,我需要能够从Doogle那里接收信息。为此,我需要使用BrainFlow,这似乎是最合适的集中式多语言解决方案。但是,我不习惯使用ArgParse,谁的角色是接收参数(从YML?JS文件?直接在代码中?)
,有人可以告诉我如何向Argparse提供参数吗?
顺便说一句,这是代码:
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()
每当我在cmd中运行代码时:python test.py, 它说需要董事会ID论点。 当我进行python test.py 0或python test.py“ 0”时。
所以我的问题是:如何为argparse设置论点?
预先感谢您:),最好,kl
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为回答有点晚了,但是如前所述,当您运行代码时,请从终端运行它所需的参数:
ep python script.py-board-id-id-id-serial-serial-端口COM5
这将考虑您使用的是使用端口COM5的OpenBCI Cyton板,该板具有ID 0。
您可以检查设备从BrainFlow文档中具有的ID。
要知道您正在使用的端口,设备的文档应该显示如何。我熟悉OpenBci。知道您正在使用的端口的一种简单方法是来自OpenBCI的GUI。
如果您使用的是同一设置,则可以在默认参数中设置这些设置,因此每次运行代码时都不必指定它们。
parser.add_argument(' - serial-port',type = str,help ='serial port',quirtiral = false,default =' com5 ')')
测试代码,您可以使用具有ID -1:
python script.py
的合成板。
祝你好运!
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!