Utilities¶
Some useful utilities abstracting or enriching functionality of the main source.
types
¶
Classes:
| Name | Description |
|---|---|
AudioCodec |
Object specifying audio codec options for FFmpeg. |
AudioFormatEnum |
Audio format enumeration for supported FFmpeg video formats. |
LoggingSpec |
Object specifying data storage options. |
VideoCodec |
Object specifying video codec options for FFmpeg. |
VideoFormatEnum |
Video format enumeration for supported FFmpeg video formats. |
AudioCodec
dataclass
¶
Object specifying audio codec options for FFmpeg.
AudioFormatEnum
¶
Bases: Enum
flowchart TD
hermes.utils.types.AudioFormatEnum[AudioFormatEnum]
click hermes.utils.types.AudioFormatEnum href "" "hermes.utils.types.AudioFormatEnum"
Audio format enumeration for supported FFmpeg video formats.
TODO:
Must be a tuple of (ffmpeg -formats
LoggingSpec
dataclass
¶
Object specifying data storage options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Path to the directory on disk to flush data to. |
required |
|
dict[str, str]
|
Nested setup definition of Nodes across distributed hosts. |
required |
|
float
|
Start time of saving data. |
required |
|
float
|
Reference time of the Broker to align all Nodes to. |
required |
|
float
|
Duration of periods over which to flush streamed accumulated data from memory to disk. Defaults to |
30.0
|
|
bool
|
Whether to print FFmpeg stats to the terminal. Defaults to |
False
|
|
bool
|
Whether to stream data into HDF5 files. Defaults to |
False
|
|
bool
|
Whether to stream video data into MP4/MKV files. Defaults to |
False
|
|
bool
|
Whether to stream data into CSV files. Defaults to |
False
|
|
bool
|
Whether to stream audio data into MP3/WAV files. Defaults to |
False
|
|
bool
|
Weather to dump in-memory recorded data in CSV files. Defaults to |
False
|
|
bool
|
Weather to dump in-memory recorded data in HDF5 files. Defaults to |
False
|
|
bool
|
Weather to dump in-memory recorded video data in MP4/MKV files. Defaults to |
False
|
|
bool
|
Weather to dump in-memory recorded audio data in MP3/WAV files. Defaults to |
False
|
|
VideoCodec
|
Definition of the video codec to use for FFmpeg. Defaults to |
None
|
|
AudioCodec
|
Definition of the audio codec to use for FFmpeg. Defaults to |
None
|
VideoCodec
dataclass
¶
Object specifying video codec options for FFmpeg.
VideoFormatEnum
¶
Bases: Enum
flowchart TD
hermes.utils.types.VideoFormatEnum[VideoFormatEnum]
click hermes.utils.types.VideoFormatEnum href "" "hermes.utils.types.VideoFormatEnum"
Video format enumeration for supported FFmpeg video formats.
Must be a tuple of (ffmpeg -formats
pixel color is one of: ffmpeg -pix_fmts
time_utils
¶
Classes:
| Name | Description |
|---|---|
SystemTime |
Highly accurate system time Singleton that uses performance counters of the host device. |
Functions:
| Name | Description |
|---|---|
get_ref_time |
Gets the current process's |
get_time |
Gets the highly accurate current system time. |
get_time_s_from_local_str |
Gets seconds since epoch from provided local time string. |
get_time_s_from_utc_time_no_date_str |
Gets local time in seconds since epoch from provided UTC time strings. |
get_time_str |
Gets a date string from seconds since epoch. |
init_time |
Initialize the current process's |
SystemTime
¶
Highly accurate system time Singleton that uses performance counters of the host device.
Performance counters of the host devices are not Unix float numbers since epoch,
but a float starting from 0 on the boot-up of the device. This Singleton uses less accurate system time
with the time.time() call and combines with the performance counters, to get highly accurate time of the host device.
This Singleton's methods should not be called directly. Use the global methods instead.
Each subprocess of the application must be initialized via init_time(...)
with a reference time obtained in the parent process via get_ref_time().
get_ref_time
¶
Gets the current process's SystemTime Singleton reference time.
get_time_s_from_local_str
¶
get_time_s_from_local_str(
time_local_str: str,
input_time_format: str = "%H:%M:%S.%f",
date_local_str: str | None = None,
input_date_format: str = "%Y-%m-%d",
) -> float
Gets seconds since epoch from provided local time string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
local time string to convert to seconds. |
required |
|
str
|
Time string format of the provided time. Defaults to |
'%H:%M:%S.%f'
|
|
str
|
local date string to convert to seconds. Defaults to |
None
|
|
str
|
Date string format of the provided date. Defaults to |
'%Y-%m-%d'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Seconds count since epoch, corresponding to the provided local time strings. |
get_time_s_from_utc_time_no_date_str
¶
get_time_s_from_utc_time_no_date_str(
time_utc_str: str,
input_time_format: str = "%H:%M:%S.%f",
date_utc_str: str | None = None,
input_date_format: str = "%Y-%m-%d",
) -> float
Gets local time in seconds since epoch from provided UTC time strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
UTC time string to convert to seconds. |
required |
|
str
|
Time string format of the provided time. Defaults to |
'%H:%M:%S.%f'
|
|
str
|
UTC date to convert to seconds. Defaults to |
None
|
|
str
|
Date string format of the provided date. Defaults to |
'%Y-%m-%d'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Seconds count since epoch, corresponding to the provided UTC time strings. |
get_time_str
¶
Gets a date string from seconds since epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
float
|
Time since epoch to convert to the human-readable string. Defaults to the current system time. |
get_time()
|
|
str
|
Format to construct the date string. Default to |
'%Y-%m-%d_%H-%M-%S'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted date string corresponding to the provided time since epoch. |
init_time
¶
init_time(ref_time: float) -> None
Initialize the current process's SystemTime Singleton with a common reference time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
float
|
Time obtained via |
required |
node_utils
¶
Functions:
| Name | Description |
|---|---|
launch_node |
Launches callable |
search_node_class |
Queries the current Python environment to match the requested |
launch_node
¶
launch_node(spec: dict, input_queue: Queue[tuple[float, str]])
Launches callable Node objects using the user-provided specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
Specification containing at least package and |
required |
|
Queue[tuple[float, str]]
|
Multiprocessing queue to fan-in user keyboard inputs if the |
required |
search_node_class
¶
search_node_class(module_name: str, class_name: str) -> type[NodeInterface]
Queries the current Python environment to match the requested hermes.<module>.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Name of the Python module containing the requested HERMES type |
required |
|
str
|
Name of the |
required |
mp_utils
¶
Functions:
| Name | Description |
|---|---|
launch_broker |
Builds the |
launch_callable |
Launches a callable object with the user-provided inputs. |
launch_broker
¶
launch_broker(
args: Namespace,
node_specs: list[dict],
input_queue: Queue[tuple[float, str]],
is_ready_event: Event,
is_quit_event: Event,
is_done_event: Event,
ref_time_s: float,
) -> None
Builds the Broker using provided configurations and manage all the components of the experiment.
Meant to be used as a target for a spawned Process or Thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Namespace
|
HERMES top-level user input arguments for the experiment definition. |
required |
|
list[dict]
|
Configuration dictionaries of the Broker's locally managed |
required |
|
Queue[tuple[float, str]]
|
Multiprocessing queue to fan-out user keyboard inputs to all |
required |
|
Event
|
Multiprocessing synchronization primitive indicating completion of |
required |
|
Event
|
Multiprocessing synchronization primitive triggering to the |
required |
|
Event
|
Multiprocessing synchronization primitive indicating that the |
required |
|
float
|
Main process reference system time obtained with |
required |
launch_callable
¶
msgpack_utils
¶
Functions:
| Name | Description |
|---|---|
convert_bytes_keys_to_strings |
Recursively decodes keys of the key-value mappings into strings. |
decode_ndarray |
Decodes received bytes and reconstructs detected NumPy arrays into original objects. |
deserialize |
Deserializes the received message to construct the original Python object. |
encode_ndarray |
Encodes NumPy contents of the provided object into bytes. |
serialize |
Serializes a Python dict-like object for ZeroMQ transmission. |
convert_bytes_keys_to_strings
¶
convert_bytes_keys_to_strings(obj: object) -> object
Recursively decodes keys of the key-value mappings into strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
object
|
Object whose keys to decode. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
Processed object with keys converted into proper dictionary string fields. |
decode_ndarray
¶
decode_ndarray(obj: object) -> object
Decodes received bytes and reconstructs detected NumPy arrays into original objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
object
|
Raw bytes to convert into NumPy arrays if any detected. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
Decoded key-value mapping with NumPy arrays reconstructed. |
deserialize
¶
deserialize(msg: bytes) -> dict
Deserializes the received message to construct the original Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
bytes
|
Raw binary data containing the original Python object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Python object with the key-value pairs preserved and any NumPy arrays reconstructed. |
encode_ndarray
¶
encode_ndarray(obj: object) -> object
Encodes NumPy contents of the provided object into bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
object
|
NumPy array to encode as serializeable key-value pair. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
Encoded key-value mapping with NumPy arrays serialized to bytes. |
serialize
¶
Serializes a Python dict-like object for ZeroMQ transmission.
Preserves named arguments as key-value pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
Inputs to serialize using a custom encoding hook for NumPy arrays. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
Serialized binary data safe to transmit. |
argparse_utils
¶
Classes:
| Name | Description |
|---|---|
ParseExperimentKwargs |
Parsing object for experiment details specification. |
ParseNodeKwargs |
Parsing object for |
ParseStorageKwargs |
Parsing object for |
Functions:
| Name | Description |
|---|---|
parse_type |
Parsing utility to convert data types to native Python objects. |
validate_ip4 |
Validates whether parsed value is a valid IPv4 address. |
validate_path |
Validates whether parsed file path exists. |
ParseExperimentKwargs
¶
Bases: Action
flowchart TD
hermes.utils.argparse_utils.ParseExperimentKwargs[ParseExperimentKwargs]
click hermes.utils.argparse_utils.ParseExperimentKwargs href "" "hermes.utils.argparse_utils.ParseExperimentKwargs"
Parsing object for experiment details specification.
ParseNodeKwargs
¶
Bases: Action
flowchart TD
hermes.utils.argparse_utils.ParseNodeKwargs[ParseNodeKwargs]
click hermes.utils.argparse_utils.ParseNodeKwargs href "" "hermes.utils.argparse_utils.ParseNodeKwargs"
Parsing object for Node specifications.
ParseStorageKwargs
¶
Bases: Action
flowchart TD
hermes.utils.argparse_utils.ParseStorageKwargs[ParseStorageKwargs]
click hermes.utils.argparse_utils.ParseStorageKwargs href "" "hermes.utils.argparse_utils.ParseStorageKwargs"
Parsing object for Storage specification.
parse_type
¶
parse_type(s: str) -> int | float | bool | str
Parsing utility to convert data types to native Python objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Text to convert to the corresponding data type. |
required |
Returns:
| Type | Description |
|---|---|
int | float | bool | str
|
int | float | bool | str: Parsed primitive data type. |
validate_ip4
¶
validate_ip4(s: str) -> str
Validates whether parsed value is a valid IPv4 address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Parsed text value to test for validity as IPv4 address. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Unchanged text value to use as IP address. |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
If the parsed value is not a valid IPv4 address. |
validate_path
¶
validate_path(s: str) -> str
Validates whether parsed file path exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
File path to parse. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Unchanged path if it exists. |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
If the provided path does not exist. |
sensor_utils
¶
Functions:
| Name | Description |
|---|---|
estimate_transmission_delay |
Estimates the mean transmission delay of the provided "ping" function. |
estimate_transmission_delay
¶
estimate_transmission_delay(ping_fn: Callable, num_samples: int = 100) -> float
Estimates the mean transmission delay of the provided "ping" function.
Used with the custom provided ping_fn to measure the one-way delay to use for
offsetting sensor data streams from the toa_s to obtain true sample time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Callable
|
User's function that wraps "ping" like functionality to the specific device. |
required |
|
int
|
Number of round-trip transmission to perform to average over. Defaults to |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Estimated mean delay for samples from the device. |
numpy_utils
¶
dict_utils
¶
angle_utils
¶
print_utils
¶
Classes:
| Name | Description |
|---|---|
SystemTime |
Highly accurate system time Singleton that uses performance counters of the host device. |
Functions:
| Name | Description |
|---|---|
get_ref_time |
Gets the current process's |
get_time |
Gets the highly accurate current system time. |
get_time_s_from_local_str |
Gets seconds since epoch from provided local time string. |
get_time_s_from_utc_time_no_date_str |
Gets local time in seconds since epoch from provided UTC time strings. |
get_time_str |
Gets a date string from seconds since epoch. |
init_time |
Initialize the current process's |
SystemTime
¶
Highly accurate system time Singleton that uses performance counters of the host device.
Performance counters of the host devices are not Unix float numbers since epoch,
but a float starting from 0 on the boot-up of the device. This Singleton uses less accurate system time
with the time.time() call and combines with the performance counters, to get highly accurate time of the host device.
This Singleton's methods should not be called directly. Use the global methods instead.
Each subprocess of the application must be initialized via init_time(...)
with a reference time obtained in the parent process via get_ref_time().
get_ref_time
¶
Gets the current process's SystemTime Singleton reference time.
get_time_s_from_local_str
¶
get_time_s_from_local_str(
time_local_str: str,
input_time_format: str = "%H:%M:%S.%f",
date_local_str: str | None = None,
input_date_format: str = "%Y-%m-%d",
) -> float
Gets seconds since epoch from provided local time string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
local time string to convert to seconds. |
required |
|
str
|
Time string format of the provided time. Defaults to |
'%H:%M:%S.%f'
|
|
str
|
local date string to convert to seconds. Defaults to |
None
|
|
str
|
Date string format of the provided date. Defaults to |
'%Y-%m-%d'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Seconds count since epoch, corresponding to the provided local time strings. |
get_time_s_from_utc_time_no_date_str
¶
get_time_s_from_utc_time_no_date_str(
time_utc_str: str,
input_time_format: str = "%H:%M:%S.%f",
date_utc_str: str | None = None,
input_date_format: str = "%Y-%m-%d",
) -> float
Gets local time in seconds since epoch from provided UTC time strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
UTC time string to convert to seconds. |
required |
|
str
|
Time string format of the provided time. Defaults to |
'%H:%M:%S.%f'
|
|
str
|
UTC date to convert to seconds. Defaults to |
None
|
|
str
|
Date string format of the provided date. Defaults to |
'%Y-%m-%d'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Seconds count since epoch, corresponding to the provided UTC time strings. |
get_time_str
¶
Gets a date string from seconds since epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
float
|
Time since epoch to convert to the human-readable string. Defaults to the current system time. |
get_time()
|
|
str
|
Format to construct the date string. Default to |
'%Y-%m-%d_%H-%M-%S'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted date string corresponding to the provided time since epoch. |
init_time
¶
init_time(ref_time: float) -> None
Initialize the current process's SystemTime Singleton with a common reference time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
float
|
Time obtained via |
required |