diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..11a7d15 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,12 @@ +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.3.2 + hooks: + # Run the linter. + - id: ruff + types_or: [ python, pyi, jupyter ] + args: [ "--fix"] + # Run the formatter. + - id: ruff-format + types_or: [ python, pyi, jupyter ] diff --git a/pyaer/__about__.py b/pyaer/__about__.py index 8790941..bd96a42 100644 --- a/pyaer/__about__.py +++ b/pyaer/__about__.py @@ -1,4 +1,5 @@ """About page.""" + __all__ = ["__version__", "__author__", "__author_email__", "__url__"] __version__ = "0.2.7a0" diff --git a/pyaer/__init__.py b/pyaer/__init__.py index 009f1ca..38ae5ce 100644 --- a/pyaer/__init__.py +++ b/pyaer/__init__.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import absolute_import from __future__ import print_function @@ -12,6 +13,7 @@ from pyaer.__about__ import __author__ # noqa from pyaer.__about__ import __version__ # noqa + FILE_PATH = os.path.realpath(__file__) CURR_PATH = os.path.dirname(os.path.realpath(__file__)) PKG_PATH = os.path.dirname(CURR_PATH) diff --git a/pyaer/comm.py b/pyaer/comm.py index 93b5dfa..be7cc08 100644 --- a/pyaer/comm.py +++ b/pyaer/comm.py @@ -13,6 +13,7 @@ Author: Yuhuang Hu Email : yuhuang.hu@ini.uzh.ch """ + import json import signal import subprocess @@ -25,6 +26,7 @@ import numpy as np import zmq + try: import zarr except Exception: @@ -181,7 +183,6 @@ def __init__( name : str the name of the publisher """ - self.__dict__.update(kwargs) self.url = url @@ -271,7 +272,7 @@ def __init__( port=5100, master_topic="", name="", - **kwargs + **kwargs, ): """AERPublisher. @@ -605,7 +606,7 @@ def __init__( sub_port=5099, sub_topic="", sub_name="", - **kwargs + **kwargs, ): """Publisher-Subscriber. @@ -760,7 +761,6 @@ def get_keys(self): def get_frame(self, device_name, group_name): """Get frame events at this packet.""" - try: frame_events = self.aer_file[device_name][group_name]["frame_events"][()] diff --git a/pyaer/davis.py b/pyaer/davis.py index 9ef0b17..5180592 100644 --- a/pyaer/davis.py +++ b/pyaer/davis.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + import numpy as np from pyaer import libcaer @@ -855,7 +856,6 @@ def get_bias(self): bias_obj: `dict`
dictionary that contains DAVIS current bias settings. """ - bias_obj = {} # output sources bias_obj["aps_enabled"] = self.get_config( @@ -1310,7 +1310,6 @@ def get_event_container(self): Instead of returning different variables, return an event container. """ - data = self.get_event() if data is None: @@ -1447,7 +1446,7 @@ def get_event(self, mode="events"): num_imu_event, ) else: - return None,0,None,0,None,0,None,0 + return None, 0, None, 0, None, 0, None, 0 class DAVISFX2(DAVIS): diff --git a/pyaer/device.py b/pyaer/device.py index 548d2ff..9d8e6a7 100644 --- a/pyaer/device.py +++ b/pyaer/device.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + import abc from pyaer import libcaer diff --git a/pyaer/dvs128.py b/pyaer/dvs128.py index c6e2040..1be6f0a 100644 --- a/pyaer/dvs128.py +++ b/pyaer/dvs128.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + import numpy as np from pyaer import libcaer diff --git a/pyaer/dvxplorer.py b/pyaer/dvxplorer.py index 5f51f1d..8268158 100644 --- a/pyaer/dvxplorer.py +++ b/pyaer/dvxplorer.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + import numpy as np from pyaer import libcaer diff --git a/pyaer/dynapse.py b/pyaer/dynapse.py index 238522f..2d058ce 100644 --- a/pyaer/dynapse.py +++ b/pyaer/dynapse.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + import time import numpy as np @@ -186,7 +187,7 @@ def set_bias_from_json( 1: [0, 1, 2, 3], 2: [0, 1, 2, 3], 3: [0, 1, 2, 3], - } + } ``` """ bias_obj = utils.load_dynapse_bias(file_path, verbose) @@ -369,7 +370,7 @@ def set_bias( 1: [0, 1, 2, 3], 2: [0, 1, 2, 3], 3: [0, 1, 2, 3], - } + } ``` # Returns @@ -408,7 +409,7 @@ def set_bias( assert isinstance(scope, dict) # Set biases for some activity - for (chip_id, core_ids) in scope.items(): + for chip_id, core_ids in scope.items(): self.set_activity_bias( bias_obj, self.chip_config[chip_id], core_ids=core_ids ) diff --git a/pyaer/edvs.py b/pyaer/edvs.py index f4b3d6b..a7174bb 100644 --- a/pyaer/edvs.py +++ b/pyaer/edvs.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + import numpy as np from pyaer import libcaer diff --git a/pyaer/evk.py b/pyaer/evk.py index 3be174b..8ee1c2b 100644 --- a/pyaer/evk.py +++ b/pyaer/evk.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + import numpy as np from pyaer import libcaer diff --git a/pyaer/filters.py b/pyaer/filters.py index 77b49b6..22f2da1 100644 --- a/pyaer/filters.py +++ b/pyaer/filters.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from typing import Any from typing import Dict diff --git a/pyaer/log.py b/pyaer/log.py index 8aa9302..b88c71c 100644 --- a/pyaer/log.py +++ b/pyaer/log.py @@ -5,11 +5,13 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + import logging from logging import Logger from typing import Optional from typing import TextIO + # Remaps logging levels for easy access. NOTSET = logging.NOTSET DEBUG = logging.DEBUG diff --git a/pyaer/utils.py b/pyaer/utils.py index 8a97e6a..eeb0d31 100644 --- a/pyaer/utils.py +++ b/pyaer/utils.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + import importlib.util as imutil import json import os @@ -16,6 +17,7 @@ from pyaer import libcaer from pyaer import log + logger = log.get_logger("utils", pyaer.LOG_LEVEL) @@ -42,7 +44,6 @@ def import_custom_module(custom_file, custom_class): def parse_type(custom_str): """Parse custom string to its corresponding type.""" - # check integer try: return int(custom_str) diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..42da16c --- /dev/null +++ b/ruff.toml @@ -0,0 +1,77 @@ +line-length = 88 + +exclude = [ + ".git/", + "__pycache__/", + "build/", + "dist/", + "venv*/", + "docs/", + "docs/*", + "res/", + "res/*", +] + +[lint] + +extend-select = [ + # Enable the isort rules. + "I", + # Enable all `pydocstyle` rules, limiting to those that adhere to the + # Google convention via `convention = "google"`, below. + "D", + # Doc line length. + "W505" +] + +ignore = [ + "E203", + "E501", + # On top of the Google convention, disable `D417`, which requires + # documentation for every function parameter. + "D417", + # Missing docstring in public module + "D100", + # Missing docstring in public class + "D101", + # Missing docstring in public method + "D102", + # Missing docstring in public function + "D103", + # Missing docstring in public package + "D104", + # Missing docstring in magic method + "D105", + # Missing docstring in `__init__` + "D107", + # 1 blank line required between summary line and description. + # This is needed because sometimes our summaries span multiple lines. + "D205", + # Multi-line docstring closing quotes should be on a separate line + # This is a result of summary lines spanning more than one line. + "D209", + # Multi-line docstring summary should start at the first line. + "D212", + # Use `r"""` if any backslashes in a docstring. + "D301", + # Doc line too long + # This is needed because we have ~400 errors otherwise. + "W505", +] + + +[lint.isort] +order-by-type = false +case-sensitive = false +split-on-trailing-comma = true +force-single-line = true +lines-after-imports = 2 + +[lint.pydocstyle] +convention = "google" + +[lint.pycodestyle] +max-doc-length = 88 + +[format] +docstring-code-format = true diff --git a/scripts/aer_comm/aer_hub b/scripts/aer_comm/aer_hub index 557db9d..9539b93 100755 --- a/scripts/aer_comm/aer_hub +++ b/scripts/aer_comm/aer_hub @@ -6,54 +6,61 @@ Author: Yuhuang Hu Email : yuhuang.hu@ini.uzh.ch """ -from __future__ import print_function, absolute_import +from __future__ import absolute_import +from __future__ import print_function -import json import argparse +import json from pyaer.comm import AERHub + parser = argparse.ArgumentParser("AER Hub") -parser.add_argument("--url", type=str, - default="tcp://127.0.0.1", - help="AERHub URL") +parser.add_argument("--url", type=str, default="tcp://127.0.0.1", help="AERHub URL") # Note that the publisher port and subscriber port are # hub_sub_port and hub_pub_port respectively. # This reversed order is intentional. # User doesn't need to know. -parser.add_argument("--publisher_port", type=int, - default=5100, - help="the port that connects all publishers") -parser.add_argument("--subscriber_port", type=int, - default=5099, - help="the port that connects all subscribers") +parser.add_argument( + "--publisher_port", + type=int, + default=5100, + help="the port that connects all publishers", +) +parser.add_argument( + "--subscriber_port", + type=int, + default=5099, + help="the port that connects all subscribers", +) -parser.add_argument("--aer_hub_name", type=str, - default="PyAER Message Hub") +parser.add_argument("--aer_hub_name", type=str, default="PyAER Message Hub") args = parser.parse_args() # print all options -print("="*50) +print("=" * 50) print(json.dumps(args.__dict__, indent=4, sort_keys=True)) -print("="*50) +print("=" * 50) -aer_hub = AERHub(url=args.url, - hub_pub_port=args.subscriber_port, - hub_sub_port=args.publisher_port, - aer_hub_name=args.aer_hub_name) +aer_hub = AERHub( + url=args.url, + hub_pub_port=args.subscriber_port, + hub_sub_port=args.publisher_port, + aer_hub_name=args.aer_hub_name, +) -aer_hub.logger.info("="*50) +aer_hub.logger.info("=" * 50) aer_hub.logger.info("Tools") aer_hub.logger.info("aer_hub: launch a central message relay hub") aer_hub.logger.info("aer_lstopic: display all published topics") aer_hub.logger.info("aer_publisher: add a custom publisher") aer_hub.logger.info("aer_subscriber: add a custom subscriber") aer_hub.logger.info("aer_saver: add an AER Saver") -aer_hub.logger.info("="*50) +aer_hub.logger.info("=" * 50) # run the hub aer_hub.run() diff --git a/scripts/aer_comm/aer_launch b/scripts/aer_comm/aer_launch index 9bd5b8c..8710dde 100755 --- a/scripts/aer_comm/aer_launch +++ b/scripts/aer_comm/aer_launch @@ -6,17 +6,19 @@ Author: Yuhuang Hu Email : yuhuang.hu@ini.uzh.ch """ -from __future__ import print_function, absolute_import +from __future__ import absolute_import +from __future__ import print_function +import argparse import os import sys -import argparse import time +from pyaer import log +from pyaer.comm import AERProcess from pyaer.utils import expandpath from pyaer.utils import ordered_yml_load -from pyaer.comm import AERProcess -from pyaer import log + HUB = "Hub" PUB = "Publisher" @@ -40,9 +42,7 @@ DEFAULT_LIBVER_VERSION = "earliest" PROGRAM_KEY = "program" -launch_logger = log.get_logger( - "AER Launcher", - log.INFO, stream=sys.stdout) +launch_logger = log.get_logger("AER Launcher", log.INFO, stream=sys.stdout) def parse_hub(hub_desc): @@ -75,11 +75,12 @@ def parse_publisher(pub_desc): parsed_cmd += [ "--url", - DEFAULT_URL if "url" not in pub_desc else pub_desc.pop("url")] + DEFAULT_URL if "url" not in pub_desc else pub_desc.pop("url"), + ] parsed_cmd += [ "--port", - DEFAULT_PUBLISHER_PORT if "port" not in pub_desc else - str(pub_desc.pop("port"))] + DEFAULT_PUBLISHER_PORT if "port" not in pub_desc else str(pub_desc.pop("port")), + ] parsed_cmd += ["--name", pub_desc.pop("name")] parsed_cmd += ["--master_topic", pub_desc.pop("master_topic")] @@ -101,13 +102,12 @@ def parse_publisher(pub_desc): if pub_desc.pop("noise_filter", default=False): parsed_cmd += ["--noise_filter"] if "bias_file" in pub_desc: - parsed_cmd += [ - "--bias_file", expandpath(pub_desc.pop("bias_file"))] + parsed_cmd += ["--bias_file", expandpath(pub_desc.pop("bias_file"))] except Exception: pass try: - for (option, value) in pub_desc.items(): + for option, value in pub_desc.items(): parsed_cmd += ["--{}".format(option), str(value)] except Exception: pass @@ -126,11 +126,14 @@ def parse_subscriber(sub_desc): parsed_cmd += [ "--url", - DEFAULT_URL if "url" not in sub_desc else sub_desc.pop("url")] + DEFAULT_URL if "url" not in sub_desc else sub_desc.pop("url"), + ] parsed_cmd += [ "--port", - DEFAULT_SUBSCRIBER_PORT if "port" not in sub_desc else - str(sub_desc.pop("port"))] + DEFAULT_SUBSCRIBER_PORT + if "port" not in sub_desc + else str(sub_desc.pop("port")), + ] parsed_cmd += ["--name", sub_desc.pop("name")] parsed_cmd += ["--topic", sub_desc.pop("topic")] @@ -147,7 +150,7 @@ def parse_subscriber(sub_desc): sub_desc.pop(USE_DEFAULT_SUB) try: - for (option, value) in sub_desc.items(): + for option, value in sub_desc.items(): parsed_cmd += ["--{}".format(option), str(value)] except Exception: pass @@ -166,24 +169,27 @@ def parse_pubsuber(pubsuber_desc): parsed_cmd += [ "--url", - DEFAULT_URL if "url" not in pubsuber_desc else - pubsuber_desc.pop("url")] + DEFAULT_URL if "url" not in pubsuber_desc else pubsuber_desc.pop("url"), + ] parsed_cmd += [ "--pub_port", - DEFAULT_PUBLISHER_PORT if "pub_port" not in pubsuber_desc else - str(pubsuber_desc.pop("pub_port"))] + DEFAULT_PUBLISHER_PORT + if "pub_port" not in pubsuber_desc + else str(pubsuber_desc.pop("pub_port")), + ] parsed_cmd += ["--pub_name", pubsuber_desc.pop("pub_name")] parsed_cmd += ["--pub_topic", pubsuber_desc.pop("pub_topic")] parsed_cmd += [ "--sub_port", - DEFAULT_SUBSCRIBER_PORT if "sub_port" not in pubsuber_desc else - str(pubsuber_desc.pop("sub_port"))] + DEFAULT_SUBSCRIBER_PORT + if "sub_port" not in pubsuber_desc + else str(pubsuber_desc.pop("sub_port")), + ] parsed_cmd += ["--sub_name", pubsuber_desc.pop("sub_name")] parsed_cmd += ["--sub_topic", pubsuber_desc.pop("sub_topic")] - custom_pubsuber, custom_class = os.path.split( - pubsuber_desc.pop("custom_pubsuber")) + custom_pubsuber, custom_class = os.path.split(pubsuber_desc.pop("custom_pubsuber")) parsed_cmd += ["--custom_pubsuber", expandpath(custom_pubsuber)] parsed_cmd += ["--custom_class", custom_class] @@ -193,13 +199,12 @@ def parse_pubsuber(pubsuber_desc): if pubsuber_desc.pop("noise_filter", default=False): parsed_cmd += ["--noise_filter"] if "bias_file" in pubsuber_desc: - parsed_cmd += ["--bias_file", expandpath( - pubsuber_desc.pop("bias_file"))] + parsed_cmd += ["--bias_file", expandpath(pubsuber_desc.pop("bias_file"))] except Exception: pass try: - for (option, value) in pubsuber_desc.items(): + for option, value in pubsuber_desc.items(): parsed_cmd += ["--{}".format(option), str(value)] except Exception: pass @@ -212,11 +217,14 @@ def parse_saver(saver_desc): parsed_cmd += [ "--url", - DEFAULT_URL if "url" not in saver_desc else saver_desc["url"]] + DEFAULT_URL if "url" not in saver_desc else saver_desc["url"], + ] parsed_cmd += [ "--port", - DEFAULT_SUBSCRIBER_PORT if "port" not in saver_desc else - str(saver_desc["port"])] + DEFAULT_SUBSCRIBER_PORT + if "port" not in saver_desc + else str(saver_desc["port"]), + ] parsed_cmd += ["--name", saver_desc["name"]] parsed_cmd += ["--topic", saver_desc["topic"]] @@ -224,12 +232,12 @@ def parse_saver(saver_desc): parsed_cmd += [ "--mode", - DEFAULT_HDF5_MODE if "mode" not in saver_desc else - saver_desc["mode"]] + DEFAULT_HDF5_MODE if "mode" not in saver_desc else saver_desc["mode"], + ] parsed_cmd += [ "--libver", - DEFAULT_LIBVER_VERSION if "libver" not in saver_desc else - saver_desc["libver"]] + DEFAULT_LIBVER_VERSION if "libver" not in saver_desc else saver_desc["libver"], + ] # Use HDF5 as the default saver try: @@ -242,8 +250,7 @@ def parse_saver(saver_desc): parser = argparse.ArgumentParser("AER Launch") -parser.add_argument("--launch_file", type=expandpath, - help="AER Launch File") +parser.add_argument("--launch_file", type=expandpath, help="AER Launch File") args = parser.parse_args() @@ -259,29 +266,23 @@ try: # if the first one is not a hub type, then start the default hub if pg_i == 0 and pg_type != HUB: parsed_hub_cmd = parse_hub({"use_default": True}) - process_collector.append( - AERProcess(parsed_hub_cmd, daemon=True)) + process_collector.append(AERProcess(parsed_hub_cmd, daemon=True)) if pg_type == HUB: parsed_hub_cmd = parse_hub(pg_desc) - process_collector.append( - AERProcess(parsed_hub_cmd)) + process_collector.append(AERProcess(parsed_hub_cmd)) elif PUB in pg_type: parsed_pub_cmd = parse_publisher(pg_desc) - process_collector.append( - AERProcess(parsed_pub_cmd)) + process_collector.append(AERProcess(parsed_pub_cmd)) elif SUB in pg_type: parsed_sub_cmd = parse_subscriber(pg_desc) - process_collector.append( - AERProcess(parsed_sub_cmd)) + process_collector.append(AERProcess(parsed_sub_cmd)) elif PUBSUB in pg_type: parsed_pubsuber_cmd = parse_pubsuber(pg_desc) - process_collector.append( - AERProcess(parsed_pubsuber_cmd)) + process_collector.append(AERProcess(parsed_pubsuber_cmd)) elif SAVER in pg_type: parsed_saver_cmd = parse_saver(pg_desc) - process_collector.append( - AERProcess(parsed_saver_cmd)) + process_collector.append(AERProcess(parsed_saver_cmd)) else: launch_logger.error("Unsupported Type {}".format(pg_type)) diff --git a/scripts/aer_comm/aer_lstopic b/scripts/aer_comm/aer_lstopic index a93fff5..2476bd6 100755 --- a/scripts/aer_comm/aer_lstopic +++ b/scripts/aer_comm/aer_lstopic @@ -6,16 +6,17 @@ Author: Yuhuang Hu Email : yuhuang.hu@ini.uzh.ch """ -from __future__ import print_function, absolute_import +from __future__ import absolute_import +from __future__ import print_function -import os import argparse +import os + from pyaer.comm import AERSubscriber class ListSubscriber(AERSubscriber): - def __init__(self, url="tcp://127.0.0.1", - port=5099, topic='', name="Topic List"): + def __init__(self, url="tcp://127.0.0.1", port=5099, topic="", name="Topic List"): """ListSubscriber. Used for list all the topics. @@ -27,36 +28,35 @@ class ListSubscriber(AERSubscriber): while True: data = self.socket.recv_multipart() - topic_name = self.unpack_data_name( - data[:2], topic_name_only=True) + topic_name = self.unpack_data_name(data[:2], topic_name_only=True) if topic_name not in topic_list: topic_list.append(topic_name) topic_list.sort() # Clear screen and write - os.system('cls' if os.name == 'nt' else 'clear') - print("="*50) + os.system("cls" if os.name == "nt" else "clear") + print("=" * 50) print("List of topic names") - print("="*50) + print("=" * 50) for tn in topic_list: print(tn) - print("="*50) + print("=" * 50) parser = argparse.ArgumentParser("AER List Topics") -parser.add_argument("--url", type=str, - default="tcp://127.0.0.1", - help="AER list topic URL") +parser.add_argument( + "--url", type=str, default="tcp://127.0.0.1", help="AER list topic URL" +) # Note that the publisher port and subscriber port are # hub_sub_port and hub_pub_port respectively. # This reversed order is intentional. # User doesn't need to know. -parser.add_argument("--port", type=int, - default=5099, - help="the port that connects all subscribers") +parser.add_argument( + "--port", type=int, default=5099, help="the port that connects all subscribers" +) args = parser.parse_args() diff --git a/scripts/aer_comm/aer_publisher b/scripts/aer_comm/aer_publisher index dd79553..29cc844 100755 --- a/scripts/aer_comm/aer_publisher +++ b/scripts/aer_comm/aer_publisher @@ -6,60 +6,69 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ -from __future__ import print_function, absolute_import +from __future__ import absolute_import +from __future__ import print_function import argparse import json +from pyaer.comm import AERPublisher from pyaer.davis import DAVIS from pyaer.dvs128 import DVS128 from pyaer.dvxplorer import DVXPLORER -from pyaer.utils import expandpath, import_custom_module +from pyaer.utils import expandpath +from pyaer.utils import import_custom_module from pyaer.utils import parse_custom_args -from pyaer.comm import AERPublisher + parser = argparse.ArgumentParser() -parser.add_argument("--url", type=str, - default="tcp://127.0.0.1", - help="AER Publisher URL") -parser.add_argument("--port", type=int, - default=5100, - help="the port that connects this publisher") -parser.add_argument("--master_topic", type=str, - default="device", - help="Master topic name for the publisher") -parser.add_argument("--name", type=str, - default="", - help="Name of the publisher") - -parser.add_argument("--device", type=str, - default="DAVIS", - help="Currently supported options: DAVIS, DVS, DVXPLORER") -parser.add_argument("--noise_filter", action="store_true", - help="Add option to enable noise filter.") -parser.add_argument("--bias_file", type=expandpath, - default=None, - help="Optional bias file") +parser.add_argument( + "--url", type=str, default="tcp://127.0.0.1", help="AER Publisher URL" +) +parser.add_argument( + "--port", type=int, default=5100, help="the port that connects this publisher" +) +parser.add_argument( + "--master_topic", + type=str, + default="device", + help="Master topic name for the publisher", +) +parser.add_argument("--name", type=str, default="", help="Name of the publisher") + +parser.add_argument( + "--device", + type=str, + default="DAVIS", + help="Currently supported options: DAVIS, DVS, DVXPLORER", +) +parser.add_argument( + "--noise_filter", action="store_true", help="Add option to enable noise filter." +) +parser.add_argument( + "--bias_file", type=expandpath, default=None, help="Optional bias file" +) parser.add_argument("--use_default_pub", action="store_true") -parser.add_argument("--custom_pub", type=expandpath, - default="", - help="path to the custom publisher class") -parser.add_argument("--custom_class", type=str, - default="", - help="custom publisher class name") +parser.add_argument( + "--custom_pub", + type=expandpath, + default="", + help="path to the custom publisher class", +) +parser.add_argument( + "--custom_class", type=str, default="", help="custom publisher class name" +) args, custom_args = parser.parse_known_args() custom_args_dict = parse_custom_args(custom_args) # print all options -print("="*50) -print(json.dumps( - {**args.__dict__, **custom_args_dict}, - indent=4, sort_keys=True)) -print("="*50) +print("=" * 50) +print(json.dumps({**args.__dict__, **custom_args_dict}, indent=4, sort_keys=True)) +print("=" * 50) # open the device if args.device == "None": @@ -79,20 +88,25 @@ else: # define publisher if args.use_default_pub: # fall back to the default publisher - publisher = AERPublisher(device=device, - url=args.url, - port=args.port, - master_topic=args.master_topic, - name=args.name) + publisher = AERPublisher( + device=device, + url=args.url, + port=args.port, + master_topic=args.master_topic, + name=args.name, + ) publisher.logger.info("Use default publisher") else: # use custom publisher CustomPublisher = import_custom_module(args.custom_pub, args.custom_class) publisher = CustomPublisher( device=device, - url=args.url, port=args.port, master_topic=args.master_topic, + url=args.url, + port=args.port, + master_topic=args.master_topic, name=args.name, - **custom_args_dict) + **custom_args_dict, + ) publisher.logger.info("Use custom publisher {}".format(args.custom_class)) # Start sending data diff --git a/scripts/aer_comm/aer_pubsuber b/scripts/aer_comm/aer_pubsuber index 2852aaa..05ea7f6 100755 --- a/scripts/aer_comm/aer_pubsuber +++ b/scripts/aer_comm/aer_pubsuber @@ -9,56 +9,59 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ -from __future__ import print_function, absolute_import +from __future__ import absolute_import +from __future__ import print_function import argparse import json -from pyaer.utils import expandpath, import_custom_module +from pyaer.utils import expandpath +from pyaer.utils import import_custom_module from pyaer.utils import parse_custom_args + parser = argparse.ArgumentParser() -parser.add_argument("--url", type=str, - default="tcp://127.0.0.1", - help="AER Publisher URL") - -parser.add_argument("--pub_port", type=int, - default=5100, - help="the port that connects this publisher") -parser.add_argument("--pub_topic", type=str, - default="device", - help="publish topic name for the publisher") -parser.add_argument("--pub_name", type=str, - default="", - help="Name of the publisher") - -parser.add_argument("--sub_port", type=int, - default=5099, - help="the port that connects this subscriber") -parser.add_argument("--sub_topic", type=str, - default="", - help="subscriber topic name for the subscriber") -parser.add_argument("--sub_name", type=str, - default="", - help="Name of the subscriber") - -parser.add_argument("--custom_pubsuber", type=expandpath, - default="", - help="path to the custom PubSuber class") -parser.add_argument("--custom_class", type=str, - default="", - help="custom publisher class name") +parser.add_argument( + "--url", type=str, default="tcp://127.0.0.1", help="AER Publisher URL" +) + +parser.add_argument( + "--pub_port", type=int, default=5100, help="the port that connects this publisher" +) +parser.add_argument( + "--pub_topic", + type=str, + default="device", + help="publish topic name for the publisher", +) +parser.add_argument("--pub_name", type=str, default="", help="Name of the publisher") + +parser.add_argument( + "--sub_port", type=int, default=5099, help="the port that connects this subscriber" +) +parser.add_argument( + "--sub_topic", type=str, default="", help="subscriber topic name for the subscriber" +) +parser.add_argument("--sub_name", type=str, default="", help="Name of the subscriber") + +parser.add_argument( + "--custom_pubsuber", + type=expandpath, + default="", + help="path to the custom PubSuber class", +) +parser.add_argument( + "--custom_class", type=str, default="", help="custom publisher class name" +) args, custom_args = parser.parse_known_args() custom_args_dict = parse_custom_args(custom_args) # print all options -print("="*50) -print(json.dumps( - {**args.__dict__, **custom_args_dict}, - indent=4, sort_keys=True)) -print("="*50) +print("=" * 50) +print(json.dumps({**args.__dict__, **custom_args_dict}, indent=4, sort_keys=True)) +print("=" * 50) # define publisher @@ -66,9 +69,14 @@ print("="*50) CustomPubSuber = import_custom_module(args.custom_pubsuber, args.custom_class) pubsuber = CustomPubSuber( url=args.url, - pub_port=args.pub_port, pub_topic=args.pub_topic, pub_name=args.pub_name, - sub_port=args.sub_port, sub_topic=args.sub_topic, sub_name=args.sub_name, - **custom_args_dict) + pub_port=args.pub_port, + pub_topic=args.pub_topic, + pub_name=args.pub_name, + sub_port=args.sub_port, + sub_topic=args.sub_topic, + sub_name=args.sub_name, + **custom_args_dict, +) pubsuber.logger.info("Use custom PubSuber {}".format(args.custom_class)) # Start sending data diff --git a/scripts/aer_comm/aer_saver b/scripts/aer_comm/aer_saver index cb2de02..478040f 100755 --- a/scripts/aer_comm/aer_saver +++ b/scripts/aer_comm/aer_saver @@ -6,14 +6,16 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ -from __future__ import print_function, absolute_import +from __future__ import absolute_import +from __future__ import print_function -import json import argparse +import json -from pyaer.utils import expandpath +from pyaer.comm import AERHDF5Saver from pyaer.comm import AERSubscriber -from pyaer.comm import AERHDF5Saver, AERZarrSaver +from pyaer.comm import AERZarrSaver +from pyaer.utils import expandpath class AERSaverSubscriber(AERSubscriber): @@ -28,13 +30,11 @@ class AERSaverSubscriber(AERSubscriber): try: data = self.socket.recv_multipart() - topic_name = self.unpack_data_name( - data[:2], topic_name_only=True) + topic_name = self.unpack_data_name(data[:2], topic_name_only=True) # you can select some of these functions to use if "polarity" in topic_name: - data_id, polarity_events = \ - self.unpack_polarity_events(data) + data_id, polarity_events = self.unpack_polarity_events(data) if polarity_events is not None: self.saver.save(data_id, polarity_events) elif "special" in topic_name: @@ -42,8 +42,7 @@ class AERSaverSubscriber(AERSubscriber): if special_events is not None: self.saver.save(data_id, special_events) elif "frame" in topic_name: - data_id, frame_events, frame_ts = \ - self.unpack_frame_events(data) + data_id, frame_events, frame_ts = self.unpack_frame_events(data) if frame_events is not None: self.saver.save(data_id, frame_events) elif "imu" in topic_name: @@ -57,34 +56,27 @@ class AERSaverSubscriber(AERSubscriber): parser = argparse.ArgumentParser() -parser.add_argument("--url", type=str, - default="tcp://127.0.0.1", - help="AER Subscriber URL") -parser.add_argument("--port", type=int, - default=5099, - help="the port that connects this subscriber") -parser.add_argument("--topic", type=str, - default="", - help="Topic to subscribe") -parser.add_argument("--name", type=str, - default="") - -parser.add_argument("--filename", type=expandpath, - default="record.hdf5", - help="Path to save record") -parser.add_argument("--mode", type=str, - default="w-", - help="opening mode") - -parser.add_argument("--hdf5", action="store_true", - help="use HDF5 as saver") -parser.add_argument("--zarr", action="store_true", - help="use Zarr as saver") +parser.add_argument( + "--url", type=str, default="tcp://127.0.0.1", help="AER Subscriber URL" +) +parser.add_argument( + "--port", type=int, default=5099, help="the port that connects this subscriber" +) +parser.add_argument("--topic", type=str, default="", help="Topic to subscribe") +parser.add_argument("--name", type=str, default="") + +parser.add_argument( + "--filename", type=expandpath, default="record.hdf5", help="Path to save record" +) +parser.add_argument("--mode", type=str, default="w-", help="opening mode") + +parser.add_argument("--hdf5", action="store_true", help="use HDF5 as saver") +parser.add_argument("--zarr", action="store_true", help="use Zarr as saver") # HDF5 specific arguments -parser.add_argument("--libver", type=str, - default="latest", - help="HDF5 library version.") +parser.add_argument( + "--libver", type=str, default="latest", help="HDF5 library version." +) # Zarr specific arguments @@ -92,16 +84,14 @@ parser.add_argument("--libver", type=str, args = parser.parse_args() # print all options -print("="*50) +print("=" * 50) print(json.dumps(args.__dict__, indent=4, sort_keys=True)) -print("="*50) +print("=" * 50) if args.hdf5: # use HDF5 as saver - saver = AERHDF5Saver(filename=args.filename, - mode=args.mode, - libver=args.libver) + saver = AERHDF5Saver(filename=args.filename, mode=args.mode, libver=args.libver) elif args.zarr: # use Zarr as saver saver = AERZarrSaver(filename=args.filename, mode=args.mode) @@ -110,8 +100,8 @@ else: raise ValueError("No saver selected, use --hdf5 or --zarr") saver_sub = AERSaverSubscriber( - url=args.url, port=args.port, topic=args.topic, - name=args.name) + url=args.url, port=args.port, topic=args.topic, name=args.name +) # set saver saver_sub.set_saver(saver) diff --git a/scripts/aer_comm/aer_subscriber b/scripts/aer_comm/aer_subscriber index d3af01d..69a758b 100755 --- a/scripts/aer_comm/aer_subscriber +++ b/scripts/aer_comm/aer_subscriber @@ -1,42 +1,44 @@ #!/usr/bin/env python -"""AER Subscriber +"""AER Subscriber. Author: Yuhuang Hu Email : duguyue100@gmail.com """ -from __future__ import print_function, absolute_import +from __future__ import absolute_import +from __future__ import print_function -import json import argparse +import json -from pyaer.utils import expandpath, import_custom_module -from pyaer.utils import parse_custom_args from pyaer.comm import AERSubscriber +from pyaer.utils import expandpath +from pyaer.utils import import_custom_module +from pyaer.utils import parse_custom_args + parser = argparse.ArgumentParser() -parser.add_argument("--url", type=str, - default="tcp://127.0.0.1", - help="AER Subscriber URL") -parser.add_argument("--port", type=int, - default=5099, - help="the port that connects this subscriber") -parser.add_argument("--topic", type=str, - default="", - help="Topic to subscribe") -parser.add_argument("--name", type=str, - default="", - help="Name of the subscriber") +parser.add_argument( + "--url", type=str, default="tcp://127.0.0.1", help="AER Subscriber URL" +) +parser.add_argument( + "--port", type=int, default=5099, help="the port that connects this subscriber" +) +parser.add_argument("--topic", type=str, default="", help="Topic to subscribe") +parser.add_argument("--name", type=str, default="", help="Name of the subscriber") parser.add_argument("--use_default_sub", action="store_true") -parser.add_argument("--custom_sub", type=expandpath, - default="", - help="path to the custom publisher class") -parser.add_argument("--custom_class", type=str, - default="", - help="custom publisher class name") +parser.add_argument( + "--custom_sub", + type=expandpath, + default="", + help="path to the custom publisher class", +) +parser.add_argument( + "--custom_class", type=str, default="", help="custom publisher class name" +) args, custom_args = parser.parse_known_args() @@ -44,27 +46,28 @@ args, custom_args = parser.parse_known_args() custom_args_dict = parse_custom_args(custom_args) # print all options -print("="*50) -print(json.dumps( - {**args.__dict__, **custom_args_dict}, - indent=4, sort_keys=True)) -print("="*50) +print("=" * 50) +print(json.dumps({**args.__dict__, **custom_args_dict}, indent=4, sort_keys=True)) +print("=" * 50) # define subscriber if args.use_default_sub: # fall back to the default publisher subscriber = AERSubscriber( - url=args.url, port=args.port, topic=args.topic, - name=args.name) + url=args.url, port=args.port, topic=args.topic, name=args.name + ) subscriber.logger.info("Use default subscriber") else: # use custom publisher CustomSubscriber = import_custom_module(args.custom_sub, args.custom_class) subscriber = CustomSubscriber( - url=args.url, port=args.port, topic=args.topic, name=args.name, - **custom_args_dict) - subscriber.logger.info( - "Use custom subscriber {}".format(args.custom_class)) + url=args.url, + port=args.port, + topic=args.topic, + name=args.name, + **custom_args_dict, + ) + subscriber.logger.info("Use custom subscriber {}".format(args.custom_class)) # Start sending data subscriber.run() diff --git a/scripts/aer_comm/custom_comm.py b/scripts/aer_comm/custom_comm.py index 2c2257f..5f75670 100644 --- a/scripts/aer_comm/custom_comm.py +++ b/scripts/aer_comm/custom_comm.py @@ -4,20 +4,22 @@ Email : yuhuang.hu@ini.uzh.ch """ -from __future__ import print_function, absolute_import +from __future__ import absolute_import +from __future__ import print_function import time + import cv2 -from pyaer.comm import AERPublisher, AERSubscriber +from pyaer.comm import AERPublisher +from pyaer.comm import AERSubscriber class CustomPublisher(AERPublisher): - def __init__(self, device, url, port, master_topic, name, **kwargs): super().__init__( - device=device, url=url, port=port, master_topic=master_topic, - **kwargs) + device=device, url=url, port=port, master_topic=master_topic, **kwargs + ) def run_once(self, verbose=False): data = self.device.get_event() @@ -35,7 +37,6 @@ def run_once(self, verbose=False): class CustomSubscriber(AERSubscriber): - def __init__(self, url, port, topic, name, **kwargs): super().__init__(url, port, topic, name, **kwargs) @@ -45,28 +46,23 @@ def __init__(self, url, port, topic, name, **kwargs): def run_once(self, verbose=False): data = self.socket.recv_multipart() - topic_name = self.unpack_data_name( - data[:2], topic_name_only=True) + topic_name = self.unpack_data_name(data[:2], topic_name_only=True) if "frame" in topic_name: - data_id, frame_events, frame_ts = \ - self.unpack_frame_events(data) + data_id, frame_events, frame_ts = self.unpack_frame_events(data) if frame_events is not None: try: - frame = cv2.cvtColor( - frame_events[0], - cv2.COLOR_BGR2RGB) + frame = cv2.cvtColor(frame_events[0], cv2.COLOR_BGR2RGB) frame = cv2.resize(frame, (1384, 1040)) cv2.imshow("frame", frame) - if cv2.waitKey(1) & 0xFF == ord('q'): + if cv2.waitKey(1) & 0xFF == ord("q"): return except Exception: pass class DVViewerSubscriber(AERSubscriber): - def __init__(self, url, port, topic, name, **kwargs): super().__init__(url, port, topic, name, **kwargs) @@ -76,21 +72,17 @@ def __init__(self, url, port, topic, name, **kwargs): def run_once(self, verbose=False): data = self.socket.recv_multipart() - topic_name = self.unpack_data_name( - data[:2], topic_name_only=True) + topic_name = self.unpack_data_name(data[:2], topic_name_only=True) if "frame" in topic_name: - data_id, frame_events, frame_ts = \ - self.unpack_frame_events(data) + data_id, frame_events, frame_ts = self.unpack_frame_events(data) if frame_events is not None: try: cv2.imshow( - "frame", - cv2.cvtColor( - frame_events[0], - cv2.COLOR_BGR2RGB)) - if cv2.waitKey(1) & 0xFF == ord('q'): + "frame", cv2.cvtColor(frame_events[0], cv2.COLOR_BGR2RGB) + ) + if cv2.waitKey(1) & 0xFF == ord("q"): return except Exception: pass diff --git a/scripts/aer_comm/test_hdf5_reader.py b/scripts/aer_comm/test_hdf5_reader.py index 8293f2b..b70af04 100644 --- a/scripts/aer_comm/test_hdf5_reader.py +++ b/scripts/aer_comm/test_hdf5_reader.py @@ -4,27 +4,27 @@ Email : yuhuang.hu@ini.uzh.ch """ -from __future__ import print_function, absolute_import +from __future__ import absolute_import +from __future__ import print_function import os from contextlib import suppress + from pyaer.comm import AERHDF5Reader -data_path = os.path.join( - os.environ["HOME"], "data", "pyaer_test.hdf5") +data_path = os.path.join(os.environ["HOME"], "data", "pyaer_test.hdf5") reader = AERHDF5Reader(data_path) for device, groups in reader.get_keys().items(): for group_name in groups: - frame = reader.get_frame(device, group_name) events = reader.get_polarity_events(device, group_name) imu = reader.get_imu_events(device, group_name) special = reader.get_special_events(device, group_name) - print("-"*50) + print("-" * 50) with suppress(Exception): print("Frame:", frame.shape) diff --git a/scripts/davis240_test.py b/scripts/davis240_test.py index 7c88ea0..6250919 100644 --- a/scripts/davis240_test.py +++ b/scripts/davis240_test.py @@ -3,30 +3,31 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function -import numpy as np import cv2 +import numpy as np from pyaer import libcaer from pyaer.davis import DAVIS + device = DAVIS(noise_filter=True) -print ("Device ID:", device.device_id) +print("Device ID:", device.device_id) if device.device_is_master: - print ("Device is master.") + print("Device is master.") else: - print ("Device is slave.") -print ("Device Serial Number:", device.device_serial_number) -print ("Device String:", device.device_string) -print ("Device USB bus Number:", device.device_usb_bus_number) -print ("Device USB device address:", device.device_usb_device_address) -print ("Device size X:", device.dvs_size_X) -print ("Device size Y:", device.dvs_size_Y) -print ("Logic Version:", device.logic_version) -print ("Background Activity Filter:", - device.dvs_has_background_activity_filter) + print("Device is slave.") +print("Device Serial Number:", device.device_serial_number) +print("Device String:", device.device_string) +print("Device USB bus Number:", device.device_usb_bus_number) +print("Device USB device address:", device.device_usb_device_address) +print("Device size X:", device.dvs_size_X) +print("Device size Y:", device.dvs_size_Y) +print("Logic Version:", device.logic_version) +print("Background Activity Filter:", device.dvs_has_background_activity_filter) device.start_data_stream() @@ -49,27 +50,38 @@ def get_event(device): try: data = get_event(device) if data is not None: - (pol_events, num_pol_event, - special_events, num_special_event, - frames_ts, frames, imu_events, - num_imu_event) = data + ( + pol_events, + num_pol_event, + special_events, + num_special_event, + frames_ts, + frames, + imu_events, + num_imu_event, + ) = data if frames.shape[0] != 0: cv2.imshow("frame", frames[0]) - print ("Number of events:", num_pol_event, "Number of Frames:", - frames.shape, "Exposure:", - device.get_config( - libcaer.DAVIS_CONFIG_APS, - libcaer.DAVIS_CONFIG_APS_EXPOSURE)) + print( + "Number of events:", + num_pol_event, + "Number of Frames:", + frames.shape, + "Exposure:", + device.get_config( + libcaer.DAVIS_CONFIG_APS, libcaer.DAVIS_CONFIG_APS_EXPOSURE + ), + ) if num_pol_event != 0: - img = pol_events[..., 1]-pol_events[..., 0] + img = pol_events[..., 1] - pol_events[..., 0] img = np.clip(img, -clip_value, clip_value) - img = img+clip_value + img = img + clip_value - cv2.imshow("image", img/float(clip_value*2)) + cv2.imshow("image", img / float(clip_value * 2)) - if cv2.waitKey(1) & 0xFF == ord('q'): + if cv2.waitKey(1) & 0xFF == ord("q"): break else: diff --git a/scripts/davis346_color_events.py b/scripts/davis346_color_events.py index 06b3406..2c49118 100644 --- a/scripts/davis346_color_events.py +++ b/scripts/davis346_color_events.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function import cv2 @@ -11,6 +12,7 @@ from pyaer import libcaer from pyaer.davis import DAVIS + device = DAVIS(noise_filter=False, color_filter=True) print("Device ID:", device.device_id) @@ -25,8 +27,7 @@ print("Device size X:", device.dvs_size_X) print("Device size Y:", device.dvs_size_Y) print("Logic Version:", device.logic_version) -print("Background Activity Filter:", - device.dvs_has_background_activity_filter) +print("Background Activity Filter:", device.dvs_has_background_activity_filter) print("Color Filter", device.aps_color_filter, type(device.aps_color_filter)) print(device.aps_color_filter == 1) @@ -51,52 +52,77 @@ def get_event(device): try: data = get_event(device) if data is not None: - (pol_events, num_pol_event, - special_events, num_special_event, - frames_ts, frames, imu_events, - num_imu_event) = data + ( + pol_events, + num_pol_event, + special_events, + num_special_event, + frames_ts, + frames, + imu_events, + num_imu_event, + ) = data if frames.shape[0] != 0: frame = cv2.cvtColor(frames[0], cv2.COLOR_BGR2RGB) - frame = cv2.resize(frame, dsize=(692, 520), - interpolation=cv2.INTER_LINEAR) + frame = cv2.resize( + frame, dsize=(692, 520), interpolation=cv2.INTER_LINEAR + ) cv2.imshow("frame", frame) if pol_events is not None: - print("Number of events:", pol_events.shape, - "Number of Frames:", - frames.shape, "Exposure:", - device.get_config( - libcaer.DAVIS_CONFIG_APS, - libcaer.DAVIS_CONFIG_APS_EXPOSURE), - "Autoexposure:", device.get_config( - libcaer.DAVIS_CONFIG_APS, - libcaer.DAVIS_CONFIG_APS_AUTOEXPOSURE), - "Color:", pol_events[0, 4]) + print( + "Number of events:", + pol_events.shape, + "Number of Frames:", + frames.shape, + "Exposure:", + device.get_config( + libcaer.DAVIS_CONFIG_APS, libcaer.DAVIS_CONFIG_APS_EXPOSURE + ), + "Autoexposure:", + device.get_config( + libcaer.DAVIS_CONFIG_APS, libcaer.DAVIS_CONFIG_APS_AUTOEXPOSURE + ), + "Color:", + pol_events[0, 4], + ) if num_pol_event != 0: # extract color events - pol_g_1 = (pol_events[:, 4] == 1) # lower left green - pol_b = (pol_events[:, 4] == 2) # lower right blue - pol_r = (pol_events[:, 4] == 3) # upper left red - pol_g_2 = (pol_events[:, 4] == 4) # upper right green + pol_g_1 = pol_events[:, 4] == 1 # lower left green + pol_b = pol_events[:, 4] == 2 # lower right blue + pol_r = pol_events[:, 4] == 3 # upper left red + pol_g_2 = pol_events[:, 4] == 4 # upper right green g_1, _, _ = np.histogram2d( - pol_events[pol_g_1, 2], pol_events[pol_g_1, 1], - bins=(260, 346), range=histrange) + pol_events[pol_g_1, 2], + pol_events[pol_g_1, 1], + bins=(260, 346), + range=histrange, + ) b, _, _ = np.histogram2d( - pol_events[pol_b, 2], pol_events[pol_b, 1], - bins=(260, 346), range=histrange) + pol_events[pol_b, 2], + pol_events[pol_b, 1], + bins=(260, 346), + range=histrange, + ) r, _, _ = np.histogram2d( - pol_events[pol_r, 2], pol_events[pol_r, 1], - bins=(260, 346), range=histrange) + pol_events[pol_r, 2], + pol_events[pol_r, 1], + bins=(260, 346), + range=histrange, + ) g_2, _, _ = np.histogram2d( - pol_events[pol_g_2, 2], pol_events[pol_g_2, 1], - bins=(260, 346), range=histrange) + pol_events[pol_g_2, 2], + pol_events[pol_g_2, 1], + bins=(260, 346), + range=histrange, + ) - g_1 = np.clip(g_1, None, clip_value)/float(clip_value) - b = np.clip(b, None, clip_value)/float(clip_value) - r = np.clip(r, None, clip_value)/float(clip_value) - g_2 = np.clip(g_2, None, clip_value)/float(clip_value) + g_1 = np.clip(g_1, None, clip_value) / float(clip_value) + b = np.clip(b, None, clip_value) / float(clip_value) + r = np.clip(r, None, clip_value) / float(clip_value) + g_2 = np.clip(g_2, None, clip_value) / float(clip_value) ig_1 = np.zeros((260, 346, 3), dtype=np.float) ib = np.zeros((260, 346, 3), dtype=np.float) @@ -113,13 +139,11 @@ def get_event(device): ir[..., 2] = r ig_2[..., 1] = g_2 - img = np.vstack(( - np.hstack((ir, ig_2)), - np.hstack((ig_1, ib)))) + img = np.vstack((np.hstack((ir, ig_2)), np.hstack((ig_1, ib)))) cv2.imshow("image", img) - if cv2.waitKey(1) & 0xFF == ord('q'): + if cv2.waitKey(1) & 0xFF == ord("q"): break else: pass diff --git a/scripts/davis346_color_test.py b/scripts/davis346_color_test.py index c6defdc..2008e50 100644 --- a/scripts/davis346_color_test.py +++ b/scripts/davis346_color_test.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function import cv2 @@ -11,6 +12,7 @@ from pyaer import libcaer from pyaer.davis import DAVIS + device = DAVIS(noise_filter=True, color_filter=True) print("Device ID:", device.device_id) @@ -25,8 +27,7 @@ print("Device size X:", device.dvs_size_X) print("Device size Y:", device.dvs_size_Y) print("Logic Version:", device.logic_version) -print("Background Activity Filter:", - device.dvs_has_background_activity_filter) +print("Background Activity Filter:", device.dvs_has_background_activity_filter) print("Color Filter", device.aps_color_filter, type(device.aps_color_filter)) print(device.aps_color_filter == 1) @@ -50,25 +51,37 @@ def get_event(device): try: data = get_event(device) if data is not None: - (pol_events, num_pol_event, - special_events, num_special_event, - frames_ts, frames, imu_events, - num_imu_event) = data + ( + pol_events, + num_pol_event, + special_events, + num_special_event, + frames_ts, + frames, + imu_events, + num_imu_event, + ) = data if frames.shape[0] != 0: frame = cv2.cvtColor(frames[0], cv2.COLOR_BGR2RGB) cv2.imshow("frame", frame) if pol_events is not None: - print("Number of events:", pol_events.shape, - "Number of Frames:", - frames.shape, "Exposure:", - device.get_config( - libcaer.DAVIS_CONFIG_APS, - libcaer.DAVIS_CONFIG_APS_EXPOSURE), - "Autoexposure:", device.get_config( - libcaer.DAVIS_CONFIG_APS, - libcaer.DAVIS_CONFIG_APS_AUTOEXPOSURE), - "Color:", pol_events[0, 5]) + print( + "Number of events:", + pol_events.shape, + "Number of Frames:", + frames.shape, + "Exposure:", + device.get_config( + libcaer.DAVIS_CONFIG_APS, libcaer.DAVIS_CONFIG_APS_EXPOSURE + ), + "Autoexposure:", + device.get_config( + libcaer.DAVIS_CONFIG_APS, libcaer.DAVIS_CONFIG_APS_AUTOEXPOSURE + ), + "Color:", + pol_events[0, 5], + ) if num_pol_event != 0: if num_packet_before_disable > 0: @@ -78,24 +91,31 @@ def get_event(device): else: device.disable_noise_filter() print("Noise filter disabled") - pol_on = (pol_events[:, 3] == 1) + pol_on = pol_events[:, 3] == 1 pol_off = np.logical_not(pol_on) img_on, _, _ = np.histogram2d( - pol_events[pol_on, 2], pol_events[pol_on, 1], - bins=(260, 346), range=histrange) + pol_events[pol_on, 2], + pol_events[pol_on, 1], + bins=(260, 346), + range=histrange, + ) img_off, _, _ = np.histogram2d( - pol_events[pol_off, 2], pol_events[pol_off, 1], - bins=(260, 346), range=histrange) + pol_events[pol_off, 2], + pol_events[pol_off, 1], + bins=(260, 346), + range=histrange, + ) if clip_value is not None: integrated_img = np.clip( - (img_on-img_off), -clip_value, clip_value) + (img_on - img_off), -clip_value, clip_value + ) else: - integrated_img = (img_on-img_off) - img = integrated_img+clip_value + integrated_img = img_on - img_off + img = integrated_img + clip_value - cv2.imshow("image", img/float(clip_value*2)) + cv2.imshow("image", img / float(clip_value * 2)) - if cv2.waitKey(1) & 0xFF == ord('q'): + if cv2.waitKey(1) & 0xFF == ord("q"): break else: pass diff --git a/scripts/davis346_test.py b/scripts/davis346_test.py index 956218b..31890f2 100644 --- a/scripts/davis346_test.py +++ b/scripts/davis346_test.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function import cv2 @@ -11,22 +12,22 @@ from pyaer import libcaer from pyaer.davis import DAVIS + device = DAVIS(noise_filter=True) -print ("Device ID:", device.device_id) +print("Device ID:", device.device_id) if device.device_is_master: - print ("Device is master.") + print("Device is master.") else: - print ("Device is slave.") -print ("Device Serial Number:", device.device_serial_number) -print ("Device String:", device.device_string) -print ("Device USB bus Number:", device.device_usb_bus_number) -print ("Device USB device address:", device.device_usb_device_address) -print ("Device size X:", device.dvs_size_X) -print ("Device size Y:", device.dvs_size_Y) -print ("Logic Version:", device.logic_version) -print ("Background Activity Filter:", - device.dvs_has_background_activity_filter) + print("Device is slave.") +print("Device Serial Number:", device.device_serial_number) +print("Device String:", device.device_string) +print("Device USB bus Number:", device.device_usb_bus_number) +print("Device USB device address:", device.device_usb_device_address) +print("Device size X:", device.dvs_size_X) +print("Device size Y:", device.dvs_size_Y) +print("Logic Version:", device.logic_version) +print("Background Activity Filter:", device.dvs_has_background_activity_filter) device.start_data_stream() # setting bias after data stream started @@ -48,21 +49,33 @@ def get_event(device): try: data = get_event(device) if data is not None: - (pol_events, num_pol_event, - special_events, num_special_event, - frames_ts, frames, imu_events, - num_imu_event) = data + ( + pol_events, + num_pol_event, + special_events, + num_special_event, + frames_ts, + frames, + imu_events, + num_imu_event, + ) = data if frames.shape[0] != 0: cv2.imshow("frame", frames[0]) - print("Number of events:", num_pol_event, "Number of Frames:", - frames.shape, "Exposure:", - device.get_config( - libcaer.DAVIS_CONFIG_APS, - libcaer.DAVIS_CONFIG_APS_EXPOSURE), - "Autoexposure:", device.get_config( - libcaer.DAVIS_CONFIG_APS, - libcaer.DAVIS_CONFIG_APS_AUTOEXPOSURE)) + print( + "Number of events:", + num_pol_event, + "Number of Frames:", + frames.shape, + "Exposure:", + device.get_config( + libcaer.DAVIS_CONFIG_APS, libcaer.DAVIS_CONFIG_APS_EXPOSURE + ), + "Autoexposure:", + device.get_config( + libcaer.DAVIS_CONFIG_APS, libcaer.DAVIS_CONFIG_APS_AUTOEXPOSURE + ), + ) if num_pol_event != 0: if num_packet_before_disable > 0: @@ -72,24 +85,31 @@ def get_event(device): else: device.disable_noise_filter() print("Noise filter disabled") - pol_on = (pol_events[:, 3] == 1) + pol_on = pol_events[:, 3] == 1 pol_off = np.logical_not(pol_on) img_on, _, _ = np.histogram2d( - pol_events[pol_on, 2], pol_events[pol_on, 1], - bins=(260, 346), range=histrange) + pol_events[pol_on, 2], + pol_events[pol_on, 1], + bins=(260, 346), + range=histrange, + ) img_off, _, _ = np.histogram2d( - pol_events[pol_off, 2], pol_events[pol_off, 1], - bins=(260, 346), range=histrange) + pol_events[pol_off, 2], + pol_events[pol_off, 1], + bins=(260, 346), + range=histrange, + ) if clip_value is not None: integrated_img = np.clip( - (img_on-img_off), -clip_value, clip_value) + (img_on - img_off), -clip_value, clip_value + ) else: - integrated_img = (img_on-img_off) - img = integrated_img+clip_value + integrated_img = img_on - img_off + img = integrated_img + clip_value - cv2.imshow("image", img/float(clip_value*2)) + cv2.imshow("image", img / float(clip_value * 2)) - if cv2.waitKey(1) & 0xFF == ord('q'): + if cv2.waitKey(1) & 0xFF == ord("q"): break else: pass diff --git a/scripts/dvs128_glumpy.py b/scripts/dvs128_glumpy.py index 41b09b1..317c973 100644 --- a/scripts/dvs128_glumpy.py +++ b/scripts/dvs128_glumpy.py @@ -3,30 +3,33 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function import threading import numpy as np -from glumpy import app, gloo, gl +from glumpy import app +from glumpy import gl +from glumpy import gloo from pyaer.dvs128 import DVS128 device = DVS128() -print ("Device ID:", device.device_id) +print("Device ID:", device.device_id) if device.device_is_master: - print ("Device is master.") + print("Device is master.") else: - print ("Device is slave.") -print ("Device Serial Number:", device.device_serial_number) -print ("Device String:", device.device_string) -print ("Device USB bus Number:", device.device_usb_bus_number) -print ("Device USB device address:", device.device_usb_device_address) -print ("Device size X:", device.dvs_size_X) -print ("Device size Y:", device.dvs_size_Y) -print ("Logic Version:", device.logic_version) + print("Device is slave.") +print("Device Serial Number:", device.device_serial_number) +print("Device String:", device.device_string) +print("Device USB bus Number:", device.device_usb_bus_number) +print("Device USB device address:", device.device_usb_device_address) +print("Device size X:", device.dvs_size_X) +print("Device size Y:", device.dvs_size_Y) +print("Logic Version:", device.logic_version) data_stream = False @@ -57,15 +60,14 @@ window = app.Window(width=1024, height=1024, aspect=1) -img_array = (np.random.uniform( - 0, 1, (128, 128, 3))*250).astype(np.uint8) +img_array = (np.random.uniform(0, 1, (128, 128, 3)) * 250).astype(np.uint8) @window.event def on_close(): global device - print ("Shutting down the device") + print("Shutting down the device") device.shutdown() del device @@ -82,31 +84,34 @@ def on_draw(dt): data_stream = True lock.acquire() - (pol_events, num_pol_event, - special_events, num_special_event) = \ - device.get_event() + (pol_events, num_pol_event, special_events, num_special_event) = device.get_event() if num_pol_event != 0: - pol_on = (pol_events[:, 3] == 1) + pol_on = pol_events[:, 3] == 1 pol_off = np.logical_not(pol_on) img_on, _, _ = np.histogram2d( - pol_events[pol_on, 2], pol_events[pol_on, 1], - bins=(128, 128), range=histrange) + pol_events[pol_on, 2], + pol_events[pol_on, 1], + bins=(128, 128), + range=histrange, + ) img_off, _, _ = np.histogram2d( - pol_events[pol_off, 2], pol_events[pol_off, 1], - bins=(128, 128), range=histrange) + pol_events[pol_off, 2], + pol_events[pol_off, 1], + bins=(128, 128), + range=histrange, + ) if clip_value is not None: - integrated_img = np.clip( - (img_on-img_off), -clip_value, clip_value) + integrated_img = np.clip((img_on - img_off), -clip_value, clip_value) else: - integrated_img = (img_on-img_off) + integrated_img = img_on - img_off - img_array = ((integrated_img+clip_value)/float( - clip_value*2)*255).astype(np.uint8) + img_array = ( + (integrated_img + clip_value) / float(clip_value * 2) * 255 + ).astype(np.uint8) img_array = img_array[..., np.newaxis].repeat(3, axis=2) else: - img_array = (np.random.uniform( - 0, 1, (128, 128, 3))*250).astype(np.uint8) + img_array = (np.random.uniform(0, 1, (128, 128, 3)) * 250).astype(np.uint8) quad["texture"] = img_array quad.draw(gl.GL_TRIANGLE_STRIP) @@ -114,7 +119,7 @@ def on_draw(dt): quad = gloo.Program(vertex, fragment, count=4) -quad['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] -quad['texcoord'] = [(0, 1), (0, 0), (1, 1), (1, 0)] -quad['texture'] = img_array +quad["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] +quad["texcoord"] = [(0, 1), (0, 0), (1, 1), (1, 0)] +quad["texture"] = img_array app.run(framerate=150) diff --git a/scripts/dvs128_test.py b/scripts/dvs128_test.py index e2044aa..e1b94a6 100644 --- a/scripts/dvs128_test.py +++ b/scripts/dvs128_test.py @@ -3,51 +3,57 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function -import numpy as np import cv2 +import numpy as np from pyaer.dvs128 import DVS128 + device = DVS128() -print ("Device ID:", device.device_id) +print("Device ID:", device.device_id) if device.device_is_master: - print ("Device is master.") + print("Device is master.") else: - print ("Device is slave.") -print ("Device Serial Number:", device.device_serial_number) -print ("Device String:", device.device_string) -print ("Device USB bus Number:", device.device_usb_bus_number) -print ("Device USB device address:", device.device_usb_device_address) -print ("Device size X:", device.dvs_size_X) -print ("Device size Y:", device.dvs_size_Y) -print ("Logic Version:", device.logic_version) + print("Device is slave.") +print("Device Serial Number:", device.device_serial_number) +print("Device String:", device.device_string) +print("Device USB bus Number:", device.device_usb_bus_number) +print("Device USB device address:", device.device_usb_device_address) +print("Device size X:", device.dvs_size_X) +print("Device size Y:", device.dvs_size_Y) +print("Logic Version:", device.logic_version) device.start_data_stream() # load new config device.set_bias_from_json("./scripts/configs/dvs128_config.json") -print (device.get_bias()) +print(device.get_bias()) clip_value = 3 histrange = [(0, v) for v in (128, 128)] while True: try: - (pol_events, num_pol_event, - special_events, num_special_event) = \ + (pol_events, num_pol_event, special_events, num_special_event) = ( device.get_event("events_hist") + ) if num_pol_event != 0: - img = pol_events[..., 1]-pol_events[..., 0] + img = pol_events[..., 1] - pol_events[..., 0] img = np.clip(img, -clip_value, clip_value) - img = img+clip_value + img = img + clip_value - cv2.imshow("image", img/float(clip_value*2)) - print ("Number of events:", num_pol_event, "Number of special events:", - num_special_event) + cv2.imshow("image", img / float(clip_value * 2)) + print( + "Number of events:", + num_pol_event, + "Number of special events:", + num_special_event, + ) - if cv2.waitKey(1) & 0xFF == ord('q'): + if cv2.waitKey(1) & 0xFF == ord("q"): break except KeyboardInterrupt: diff --git a/scripts/dvs128_thread_test.py b/scripts/dvs128_thread_test.py index 0c37644..4853c18 100644 --- a/scripts/dvs128_thread_test.py +++ b/scripts/dvs128_thread_test.py @@ -3,33 +3,36 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function -from queue import Queue import threading -import numpy as np +from queue import Queue + import cv2 +import numpy as np from pyaer.dvs128 import DVS128 + device = DVS128() -print ("Device ID:", device.device_id) +print("Device ID:", device.device_id) if device.device_is_master: - print ("Device is master.") + print("Device is master.") else: - print ("Device is slave.") -print ("Device Serial Number:", device.device_serial_number) -print ("Device String:", device.device_string) -print ("Device USB bus Number:", device.device_usb_bus_number) -print ("Device USB device address:", device.device_usb_device_address) -print ("Device size X:", device.dvs_size_X) -print ("Device size Y:", device.dvs_size_Y) -print ("Logic Version:", device.logic_version) + print("Device is slave.") +print("Device Serial Number:", device.device_serial_number) +print("Device String:", device.device_string) +print("Device USB bus Number:", device.device_usb_bus_number) +print("Device USB device address:", device.device_usb_device_address) +print("Device size X:", device.dvs_size_X) +print("Device size Y:", device.dvs_size_Y) +print("Logic Version:", device.logic_version) # load new config device.set_bias_from_json("./scripts/configs/dvs128_config.json") -print (device.get_bias()) +print(device.get_bias()) device.start_data_stream() @@ -41,24 +44,30 @@ def drawing_func(in_q): while threading.currentThread().isAlive(): try: - (pol_events, num_pol_event, - special_events, num_special_event) = in_q.get() + (pol_events, num_pol_event, special_events, num_special_event) = in_q.get() if num_pol_event != 0: - pol_on = (pol_events[:, 3] == 1) + pol_on = pol_events[:, 3] == 1 pol_off = np.logical_not(pol_on) img_on, _, _ = np.histogram2d( - pol_events[pol_on, 2], pol_events[pol_on, 1], - bins=(128, 128), range=histrange) + pol_events[pol_on, 2], + pol_events[pol_on, 1], + bins=(128, 128), + range=histrange, + ) img_off, _, _ = np.histogram2d( - pol_events[pol_off, 1], pol_events[pol_off, 0], - bins=(128, 128), range=histrange) + pol_events[pol_off, 1], + pol_events[pol_off, 0], + bins=(128, 128), + range=histrange, + ) if clip_value is not None: integrated_img = np.clip( - (img_on-img_off), -clip_value, clip_value) + (img_on - img_off), -clip_value, clip_value + ) else: - integrated_img = (img_on-img_off) - img = integrated_img+clip_value - cv2.imshow("image", img/float(clip_value*2)) + integrated_img = img_on - img_off + img = integrated_img + clip_value + cv2.imshow("image", img / float(clip_value * 2)) cv2.waitKey(1) except KeyboardInterrupt: device.shutdown() @@ -69,9 +78,12 @@ def fetching_func(out_q): while threading.currentThread().isAlive(): try: event_packet = device.get_event() - print ("Number of events:", event_packet[1], - "Number of special events:", - event_packet[3]) + print( + "Number of events:", + event_packet[1], + "Number of special events:", + event_packet[3], + ) out_q.put(event_packet) except KeyboardInterrupt: device.shutdown() @@ -81,12 +93,10 @@ def fetching_func(out_q): if __name__ == "__main__": # define thread q = Queue(maxsize=1) - drawer = threading.Thread( - name="drawer", target=drawing_func, args=(q, )) - fetcher = threading.Thread( - name="fetcher", target=fetching_func, args=(q, )) + drawer = threading.Thread(name="drawer", target=drawing_func, args=(q,)) + fetcher = threading.Thread(name="fetcher", target=fetching_func, args=(q,)) fetcher.start() - print ("fetcher started") + print("fetcher started") drawer.start() - print ("drawer started") + print("drawer started") diff --git a/scripts/dvs128_vispy.py b/scripts/dvs128_vispy.py index b11f845..618e671 100644 --- a/scripts/dvs128_vispy.py +++ b/scripts/dvs128_vispy.py @@ -3,11 +3,13 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function import numpy as np import vispy -from vispy import app, scene, visuals, gloo +from vispy import app +from vispy import gloo from vispy.util.transforms import ortho from pyaer.dvs128 import DVS128 @@ -15,22 +17,22 @@ device = DVS128() -print ("Device ID:", device.device_id) +print("Device ID:", device.device_id) if device.device_is_master: - print ("Device is master.") + print("Device is master.") else: - print ("Device is slave.") -print ("Device Serial Number:", device.device_serial_number) -print ("Device String:", device.device_string) -print ("Device USB bus Number:", device.device_usb_bus_number) -print ("Device USB device address:", device.device_usb_device_address) -print ("Device size X:", device.dvs_size_X) -print ("Device size Y:", device.dvs_size_Y) -print ("Logic Version:", device.logic_version) + print("Device is slave.") +print("Device Serial Number:", device.device_serial_number) +print("Device String:", device.device_string) +print("Device USB bus Number:", device.device_usb_bus_number) +print("Device USB device address:", device.device_usb_device_address) +print("Device size X:", device.dvs_size_X) +print("Device size Y:", device.dvs_size_Y) +print("Logic Version:", device.logic_version) # load new config device.set_bias_from_json("./scripts/configs/dvs128_config.json") -print (device.get_bias()) +print(device.get_bias()) device.start_data_stream() @@ -42,10 +44,9 @@ W, H = 128, 128 img_array = np.random.uniform(0, 1, (W, H)).astype(np.float32) -data = np.zeros(4, dtype=[('a_position', np.float32, 2), - ('a_texcoord', np.float32, 2)]) -data['a_position'] = np.array([[0, 0], [W, 0], [0, H], [W, H]]) -data['a_texcoord'] = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) +data = np.zeros(4, dtype=[("a_position", np.float32, 2), ("a_texcoord", np.float32, 2)]) +data["a_position"] = np.array([[0, 0], [W, 0], [0, H], [W, H]]) +data["a_texcoord"] = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) VERT_SHADER = """ // Uniforms @@ -79,66 +80,70 @@ class Canvas(vispy.app.Canvas): def __init__(self): - vispy.app.Canvas.__init__(self, keys='interactive', size=(300, 300)) + vispy.app.Canvas.__init__(self, keys="interactive", size=(300, 300)) self.program = gloo.Program(VERT_SHADER, FRAG_SHADER) - self.texture = gloo.Texture2D( - img_array, interpolation="linear") + self.texture = gloo.Texture2D(img_array, interpolation="linear") - self.program['u_texture'] = self.texture + self.program["u_texture"] = self.texture self.program.bind(gloo.VertexBuffer(data)) self.view = np.eye(4, dtype=np.float32) self.model = np.eye(4, dtype=np.float32) self.projection = np.eye(4, dtype=np.float32) - self.program['u_model'] = self.model - self.program['u_view'] = self.view + self.program["u_model"] = self.model + self.program["u_view"] = self.view self.projection = ortho(0, W, 0, H, -1, 1) - self.program['u_projection'] = self.projection + self.program["u_projection"] = self.projection - gloo.set_clear_color('white') + gloo.set_clear_color("white") - self._timer = app.Timer('auto', connect=self.update, start=True) + self._timer = app.Timer("auto", connect=self.update, start=True) self.show() # @profile def on_draw(self, ev): gloo.clear(color=True, depth=True) - (pol_events, num_pol_event, - special_events, num_special_event) = \ + (pol_events, num_pol_event, special_events, num_special_event) = ( device.get_event() + ) if num_pol_event != 0: - pol_on = (pol_events[:, 3] == 1) + pol_on = pol_events[:, 3] == 1 pol_off = np.logical_not(pol_on) img_on, _, _ = np.histogram2d( - pol_events[pol_on, 1], 127-pol_events[pol_on, 2], - bins=(128, 128), range=histrange) + pol_events[pol_on, 1], + 127 - pol_events[pol_on, 2], + bins=(128, 128), + range=histrange, + ) img_off, _, _ = np.histogram2d( - pol_events[pol_off, 1], 127-pol_events[pol_off, 2], - bins=(128, 128), range=histrange) + pol_events[pol_off, 1], + 127 - pol_events[pol_off, 2], + bins=(128, 128), + range=histrange, + ) if clip_value is not None: - integrated_img = np.clip( - (img_on-img_off), -clip_value, clip_value) + integrated_img = np.clip((img_on - img_off), -clip_value, clip_value) else: - integrated_img = (img_on-img_off) + integrated_img = img_on - img_off - img_array = ((integrated_img+clip_value)/float( - clip_value*2)).astype(np.float32) + img_array = ((integrated_img + clip_value) / float(clip_value * 2)).astype( + np.float32 + ) else: - img_array[...] = np.zeros( - (128, 128), dtype=np.uint8).astype(np.float32) + img_array[...] = np.zeros((128, 128), dtype=np.uint8).astype(np.float32) self.texture.set_data(img_array) - self.program.draw('triangle_strip') + self.program.draw("triangle_strip") # @profile def on_resize(self, event): width, height = event.physical_size gloo.set_viewport(0, 0, width, height) self.projection = ortho(0, width, 0, height, -100, 100) - self.program['u_projection'] = self.projection + self.program["u_projection"] = self.projection # Compute thje new size of the quad r = width / float(height) @@ -149,13 +154,13 @@ def on_resize(self, event): else: w, h = height * R, height x, y = int((width - w) / 2), 0 - data['a_position'] = np.array( - [[x, y], [x + w, y], [x, y + h], [x + w, y + h]]) + data["a_position"] = np.array([[x, y], [x + w, y], [x, y + h], [x + w, y + h]]) self.program.bind(gloo.VertexBuffer(data)) + # @profile def run(): - win = Canvas() + win = Canvas() # noqa app.run() diff --git a/scripts/dvs_noise_filter_test.py b/scripts/dvs_noise_filter_test.py index 0d0acfe..11996cd 100644 --- a/scripts/dvs_noise_filter_test.py +++ b/scripts/dvs_noise_filter_test.py @@ -3,32 +3,35 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ -from __future__ import print_function, absolute_import + +from __future__ import absolute_import +from __future__ import print_function import cv2 import numpy as np from pyaer.dvs128 import DVS128 + device = DVS128(noise_filter=True) -print ("Device ID:", device.device_id) +print("Device ID:", device.device_id) if device.device_is_master: - print ("Device is master.") + print("Device is master.") else: - print ("Device is slave.") -print ("Device Serial Number:", device.device_serial_number) -print ("Device String:", device.device_string) -print ("Device USB bus Number:", device.device_usb_bus_number) -print ("Device USB device address:", device.device_usb_device_address) -print ("Device size X:", device.dvs_size_X) -print ("Device size Y:", device.dvs_size_Y) -print ("Logic Version:", device.logic_version) + print("Device is slave.") +print("Device Serial Number:", device.device_serial_number) +print("Device String:", device.device_string) +print("Device USB bus Number:", device.device_usb_bus_number) +print("Device USB device address:", device.device_usb_device_address) +print("Device size X:", device.dvs_size_X) +print("Device size Y:", device.dvs_size_Y) +print("Logic Version:", device.logic_version) device.start_data_stream() # load new config device.set_bias_from_json("./scripts/configs/dvs128_config.json") -print (device.get_bias()) +print(device.get_bias()) clip_value = 1 histrange = [(0, v) for v in (128, 128)] @@ -39,38 +42,45 @@ def get_event(device): global num_packet_before_disable - (pol_events, num_pol_event, - special_events, num_special_event) = \ - device.get_event() + (pol_events, num_pol_event, special_events, num_special_event) = device.get_event() if num_pol_event != 0: if num_packet_before_disable > 0: pol_events = pol_events[pol_events[:, 4] == 1] num_packet_before_disable -= 1 else: device.disable_noise_filter() - print ("Noise filter disabled") + print("Noise filter disabled") - pol_on = (pol_events[:, 3] == 1) + pol_on = pol_events[:, 3] == 1 pol_off = np.logical_not(pol_on) img_on, _, _ = np.histogram2d( - pol_events[pol_on, 2], pol_events[pol_on, 1], - bins=(128, 128), range=histrange) + pol_events[pol_on, 2], + pol_events[pol_on, 1], + bins=(128, 128), + range=histrange, + ) img_off, _, _ = np.histogram2d( - pol_events[pol_off, 2], pol_events[pol_off, 1], - bins=(128, 128), range=histrange) + pol_events[pol_off, 2], + pol_events[pol_off, 1], + bins=(128, 128), + range=histrange, + ) if clip_value is not None: - integrated_img = np.clip( - (img_on-img_off), -clip_value, clip_value) + integrated_img = np.clip((img_on - img_off), -clip_value, clip_value) else: - integrated_img = (img_on-img_off) - img = integrated_img+clip_value - - cv2.imshow("image", img/float(clip_value*2)) - print ("Number of events:", num_pol_event, "Number of special events:", - num_special_event) + integrated_img = img_on - img_off + img = integrated_img + clip_value + + cv2.imshow("image", img / float(clip_value * 2)) + print( + "Number of events:", + num_pol_event, + "Number of special events:", + num_special_event, + ) del pol_events, num_pol_event, special_events, num_special_event - if cv2.waitKey(1) & 0xFF == ord('q'): + if cv2.waitKey(1) & 0xFF == ord("q"): return @@ -80,7 +90,7 @@ def get_event(device): except KeyboardInterrupt: hot_pixels = device.noise_filter.get_hot_pixels() - print (hot_pixels) - print (device.noise_filter.get_bias()) + print(hot_pixels) + print(device.noise_filter.get_bias()) device.shutdown() break diff --git a/scripts/dvxplorer_test.py b/scripts/dvxplorer_test.py index bc61df7..cc81552 100644 --- a/scripts/dvxplorer_test.py +++ b/scripts/dvxplorer_test.py @@ -3,13 +3,16 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ -from __future__ import print_function, absolute_import -import numpy as np +from __future__ import absolute_import +from __future__ import print_function + import cv2 +import numpy as np from pyaer.dvxplorer import DVXPLORER + device = DVXPLORER() print("Device ID:", device.device_id) @@ -42,17 +45,21 @@ while True: try: - (pol_events, num_pol_event, - special_events, num_special_event, - imu_events, num_imu_event) = \ - device.get_event("events_hist") + ( + pol_events, + num_pol_event, + special_events, + num_special_event, + imu_events, + num_imu_event, + ) = device.get_event("events_hist") print("Number of events:", num_pol_event) if num_pol_event != 0: - img = pol_events[..., 1]-pol_events[..., 0] + img = pol_events[..., 1] - pol_events[..., 0] img = np.clip(img, -clip_value, clip_value) - img = (img+clip_value)/float(clip_value*2) + img = (img + clip_value) / float(clip_value * 2) cv2.imshow("image", img) diff --git a/scripts/dynapse_test.py b/scripts/dynapse_test.py index 9ae8f81..27a390f 100644 --- a/scripts/dynapse_test.py +++ b/scripts/dynapse_test.py @@ -3,27 +3,29 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function -from pyaer.dynapse import DYNAPSE from pyaer import utils +from pyaer.dynapse import DYNAPSE + device = DYNAPSE() -print ("Device ID:", device.device_id) +print("Device ID:", device.device_id) if device.device_is_master: - print ("Device is master.") + print("Device is master.") else: - print ("Device is slave.") -print ("Device Serial Number:", device.device_serial_number) -print ("Device String:", device.device_string) -print ("Device USB bus Number:", device.device_usb_bus_number) -print ("Device USB device address:", device.device_usb_device_address) -print ("Logic Version:", device.logic_version) -print ("Logic Clock:", device.logic_clock) -print ("Chip ID:", device.chip_id) -print ("AER has statistics:", device.aer_has_statistics) -print ("MUX has statistics:", device.mux_has_statistics) + print("Device is slave.") +print("Device Serial Number:", device.device_serial_number) +print("Device String:", device.device_string) +print("Device USB bus Number:", device.device_usb_bus_number) +print("Device USB device address:", device.device_usb_device_address) +print("Logic Version:", device.logic_version) +print("Logic Clock:", device.logic_clock) +print("Chip ID:", device.chip_id) +print("AER has statistics:", device.aer_has_statistics) +print("MUX has statistics:", device.mux_has_statistics) device.start_data_stream() @@ -32,13 +34,17 @@ # set bias from json file scope = { 0: [0, 1, 2, 3], - } - -device.set_bias_from_json("./scripts/configs/dynapse_config.json", - clear_sram=False, setup_sram=False, - fpga_bias=True, scope=scope) +} + +device.set_bias_from_json( + "./scripts/configs/dynapse_config.json", + clear_sram=False, + setup_sram=False, + fpga_bias=True, + scope=scope, +) # print FPGA biases -print (device.get_fpga_bias()) +print(device.get_fpga_bias()) # set biases for a single chip device.set_chip_bias(bias_obj, chip_id=1) @@ -48,9 +54,8 @@ events = device.get_event() if events is not None: - print ("Number of events from DYNAPSE : %d" % - (events[1])) + print("Number of events from DYNAPSE : %d" % (events[1])) except KeyboardInterrupt: - print ("Device shutting down...") + print("Device shutting down...") device.shutdown() break diff --git a/scripts/edvs_test.py b/scripts/edvs_test.py index 3ca2ab7..cef8fc1 100644 --- a/scripts/edvs_test.py +++ b/scripts/edvs_test.py @@ -3,28 +3,30 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function -import numpy as np import cv2 +import numpy as np from pyaer.edvs import eDVS + device = eDVS() -print ("Device ID:", device.device_id) +print("Device ID:", device.device_id) if device.device_is_master: - print ("Device is master.") + print("Device is master.") else: - print ("Device is slave.") -print ("Device String:", device.device_string) -print ("Device size X:", device.dvs_size_X) -print ("Device size Y:", device.dvs_size_Y) + print("Device is slave.") +print("Device String:", device.device_string) +print("Device size X:", device.dvs_size_X) +print("Device size Y:", device.dvs_size_Y) device.start_data_stream() # load new config device.set_bias_from_json("./scripts/configs/edvs_config.json") -print (device.get_bias()) +print(device.get_bias()) device.close() @@ -34,31 +36,38 @@ # @profile def get_event(device): - (pol_events, num_pol_event, - special_events, num_special_event) = \ - device.get_event() + (pol_events, num_pol_event, special_events, num_special_event) = device.get_event() if num_pol_event != 0: - pol_on = (pol_events[:, 3] == 1) + pol_on = pol_events[:, 3] == 1 pol_off = np.logical_not(pol_on) img_on, _, _ = np.histogram2d( - pol_events[pol_on, 2], pol_events[pol_on, 1], - bins=(128, 128), range=histrange) + pol_events[pol_on, 2], + pol_events[pol_on, 1], + bins=(128, 128), + range=histrange, + ) img_off, _, _ = np.histogram2d( - pol_events[pol_off, 2], pol_events[pol_off, 1], - bins=(128, 128), range=histrange) + pol_events[pol_off, 2], + pol_events[pol_off, 1], + bins=(128, 128), + range=histrange, + ) if clip_value is not None: - integrated_img = np.clip( - (img_on-img_off), -clip_value, clip_value) + integrated_img = np.clip((img_on - img_off), -clip_value, clip_value) else: - integrated_img = (img_on-img_off) - img = integrated_img+clip_value + integrated_img = img_on - img_off + img = integrated_img + clip_value - cv2.imshow("image", img/float(clip_value*2)) - print ("Number of events:", num_pol_event, "Number of special events:", - num_special_event) + cv2.imshow("image", img / float(clip_value * 2)) + print( + "Number of events:", + num_pol_event, + "Number of special events:", + num_special_event, + ) del pol_events, num_pol_event, special_events, num_special_event - if cv2.waitKey(1) & 0xFF == ord('q'): + if cv2.waitKey(1) & 0xFF == ord("q"): return diff --git a/scripts/event_container_test.py b/scripts/event_container_test.py index 5026770..16d1cc5 100644 --- a/scripts/event_container_test.py +++ b/scripts/event_container_test.py @@ -3,15 +3,13 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import print_function -import cv2 -import numpy as np +from timer import Timer -from pyaer import libcaer from pyaer.davis import DAVIS -from timer import Timer device = DAVIS(noise_filter=True) @@ -27,8 +25,7 @@ print("Device size X:", device.dvs_size_X) print("Device size Y:", device.dvs_size_Y) print("Logic Version:", device.logic_version) -print("Background Activity Filter:", - device.dvs_has_background_activity_filter) +print("Background Activity Filter:", device.dvs_has_background_activity_filter) print("Color Filter", device.aps_color_filter, type(device.aps_color_filter)) print(device.aps_color_filter == 1) @@ -54,15 +51,19 @@ def get_event(device): event_container = device.get_event_container() if event_container is not None: - print("Duration (s): {}, Num Events: {}, Event Rate (ev/s): {}" - .format( + print( + "Duration (s): {}, Num Events: {}, Event Rate (ev/s): {}".format( event_container.pol_event_duration, event_container.num_pol_events, - event_container.pol_event_rate)) - print("Signal Rate (ev/s): {}, Noise Rate (ev/s): {}" - .format( + event_container.pol_event_rate, + ) + ) + print( + "Signal Rate (ev/s): {}, Noise Rate (ev/s): {}".format( event_container.valid_pol_events_rate, - event_container.invalid_pol_events_rate)) + event_container.invalid_pol_events_rate, + ) + ) # print("Number of events:", num_pol_event, "Number of Frames:", # frames.shape, "Exposure:", diff --git a/scripts/timer.py b/scripts/timer.py index d6a65c2..5dacd9a 100644 --- a/scripts/timer.py +++ b/scripts/timer.py @@ -1,30 +1,33 @@ +import atexit import logging import time -import numpy as np + import matplotlib.pyplot as plt +import numpy as np from engineering_notation import EngNumber as eng # only from pip -import atexit LOGGING_LEVEL = logging.INFO class CustomFormatter(logging.Formatter): - """Logging Formatter to add colors and count warning / errors""" + """Logging Formatter to add colors and count warning / errors.""" grey = "\x1b[38;21m" yellow = "\x1b[33;21m" red = "\x1b[31;21m" bold_red = "\x1b[31;1m" reset = "\x1b[0m" - format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)" + format = ( + "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)" + ) FORMATS = { logging.DEBUG: grey + format + reset, logging.INFO: grey + format + reset, logging.WARNING: yellow + format + reset, logging.ERROR: red + format + reset, - logging.CRITICAL: bold_red + format + reset + logging.CRITICAL: bold_red + format + reset, } def format(self, record): @@ -51,11 +54,12 @@ def my_logger(name): class Timer: - def __init__(self, timer_name='', delay=None, - show_hist=False, numpy_file=None): - """ Make a Timer() in a _with_ statement for a block of code. - The timer is started when the block is entered and stopped when exited. - The Timer _must_ be used in a with statement. + def __init__(self, timer_name="", delay=None, show_hist=False, numpy_file=None): + """Make a Timer() in a _with_ statement for a block of code. + + The timer is started when the block is entered and stopped when exited. The Timer _must_ be + used in a with statement. + :param timer_name: the str by which this timer is repeatedly called and which it is named when summary is printed on exit :param delay: set this to a value to simply accumulate @@ -87,23 +91,31 @@ def __exit__(self, *args): times[self.timer_name].append(self.interval) def print_timing_info(self, logger=None): - """ Prints the timing information accumulated for this Timer + """Prints the timing information accumulated for this Timer. + :param logger: write to the supplied logger, otherwise use the built-in logger """ if len(times) == 0: - log.error(f'Timer {self.timer_name} has no statistics; was it used without a "with" statement?') + log.error( + f'Timer {self.timer_name} has no statistics; was it used without a "with" statement?' + ) return a = np.array(times[self.timer_name]) - timing_mean = np.mean(a) # todo use built in print method for timer + timing_mean = np.mean(a) # todo use built in print method for timer timing_std = np.std(a) timing_median = np.median(a) timing_min = np.min(a) timing_max = np.max(a) - s='{} n={}: {}s +/- {}s (median {}s, min {}s max {}s)'.format(self.timer_name, len(a), - eng(timing_mean), eng(timing_std), - eng(timing_median), eng(timing_min), - eng(timing_max)) + s = "{} n={}: {}s +/- {}s (median {}s, min {}s max {}s)".format( + self.timer_name, + len(a), + eng(timing_mean), + eng(timing_std), + eng(timing_median), + eng(timing_min), + eng(timing_max), + ) if logger is not None: logger.info(s) @@ -119,39 +131,54 @@ def print_timing_info(): timing_median = np.median(a) timing_min = np.min(a) timing_max = np.max(a) - log.info('== Timing statistics from all Timer ==\n{} n={}: {}s +/- {}s (median {}s, min {}s max {}s)'.format(k, len(a), - eng(timing_mean), eng(timing_std), - eng(timing_median), eng(timing_min), - eng(timing_max))) + log.info( + "== Timing statistics from all Timer ==\n{} n={}: {}s +/- {}s (median {}s, min {}s max {}s)".format( + k, + len(a), + eng(timing_mean), + eng(timing_std), + eng(timing_median), + eng(timing_min), + eng(timing_max), + ) + ) if timers[k].numpy_file is not None: try: - log.info(f'saving timing data for {k} in numpy file {timers[k].numpy_file}') - log.info('there are {} times'.format(len(a))) + log.info( + f"saving timing data for {k} in numpy file {timers[k].numpy_file}" + ) + log.info("there are {} times".format(len(a))) np.save(timers[k].numpy_file, a) except Exception as e: - log.error(f'could not save numpy file {timers[k].numpy_file}; caught {e}') + log.error( + f"could not save numpy file {timers[k].numpy_file}; caught {e}" + ) if timers[k].show_hist: def plot_loghist(x, bins): - hist, bins = np.histogram(x, bins=bins) # histogram x linearly - if len(bins)<2 or bins[0]<=0: - log.error(f'cannot plot histogram since bins={bins}') + hist, bins = np.histogram(x, bins=bins) # histogram x linearly + if len(bins) < 2 or bins[0] <= 0: + log.error(f"cannot plot histogram since bins={bins}") return - logbins = np.logspace(np.log10(bins[0]), np.log10(bins[-1]), len(bins)) # use resulting bin ends to get log bins - plt.hist(x, bins=logbins) # now again histogram x, but with the log-spaced bins, and plot this histogram - plt.xscale('log') - - dt = np.clip(a,1e-6, None) + logbins = np.logspace( + np.log10(bins[0]), np.log10(bins[-1]), len(bins) + ) # use resulting bin ends to get log bins + plt.hist( + x, bins=logbins + ) # now again histogram x, but with the log-spaced bins, and plot this histogram + plt.xscale("log") + + dt = np.clip(a, 1e-6, None) # logbins = np.logspace(np.log10(bins[0]), np.log10(bins[-1]), len(bins)) try: - plot_loghist(dt,bins=100) - plt.xlabel('interval[ms]') - plt.ylabel('frequency') + plot_loghist(dt, bins=100) + plt.xlabel("interval[ms]") + plt.ylabel("frequency") plt.title(k) plt.show() except Exception as e: - log.error(f'could not plot histogram: got {e}') + log.error(f"could not plot histogram: got {e}") atexit.register(print_timing_info) diff --git a/setup.py b/setup.py index c7ccea6..6c6b63a 100644 --- a/setup.py +++ b/setup.py @@ -3,6 +3,7 @@ Author: Yuhuang Hu Email : duguyue100@gmail.com """ + from __future__ import absolute_import from __future__ import print_function @@ -15,6 +16,7 @@ from setuptools import setup from setuptools.extension import Extension + classifiers = """ Development Status :: 4 - Beta Intended Audience :: Science/Research