CSV files, ROS bags and arrays: the readers¶
iter_csv, iter_rosbag and iter_array read a file, a ROS bag or an array and give a stream of Signal and, when there is one, of a target y. The overview of the section is on Streams.
1. A robot log file becomes a stream¶
A CSV file with the columns of the CASPER recording as it is distributed on Kaggle (Kayan et al. 2023), one row per sample, with a timestamp column and a column holding the six joint velocities as a list; here three joints and three rows.
from pathlib import Path
from dense_armor.core.signals.stream import iter_csv
p = Path("arm.csv")
p.write_text(
"Timestamp,Actual Joint Velocities\n"
'0.00,"[0.1, -0.2, 0.0]"\n'
'0.05,"[0.2, -0.1, 0.0]"\n'
'0.10,"[0.3, 0.0, 0.1]"\n'
)
for sig, y in iter_csv(p, time_col="Timestamp", array_cols=["Actual Joint Velocities"]):
print(sig.t, {k: round(float(v), 2) for k, v in sig.items()})
0.0 {'Actual_Joint_Velocities_0': 0.1, 'Actual_Joint_Velocities_1': -0.2, 'Actual_Joint_Velocities_2': 0.0}
0.05 {'Actual_Joint_Velocities_0': 0.2, 'Actual_Joint_Velocities_1': -0.1, 'Actual_Joint_Velocities_2': 0.0}
0.1 {'Actual_Joint_Velocities_0': 0.3, 'Actual_Joint_Velocities_1': 0.0, 'Actual_Joint_Velocities_2': 0.1}
Each row becomes one Signal with its timestamp; the list column is split into one
channel per joint. The file is read one row at a time, so a 24-hour log uses the same memory as a
three-row one; start and stop read only a time window and stop at the end of it. An empty or
unreadable cell becomes a missing channel, not an error.
API reference¶
csv ¶
Read a CSV file as a stream of :class:Signal samples.
Examples:
>>> import tempfile
>>> from pathlib import Path
>>> from dense_armor.core.signals.stream import iter_csv
>>> d = tempfile.mkdtemp()
>>> p = Path(d) / "t.csv"
>>> _ = p.write_text("t,a,b\n0,1,2\n1,3,4\n")
>>> for sig, y in iter_csv(p, time_col="t"):
... print(sig["a"], sig["b"], sig.t)
1.0 2.0 0.0
3.0 4.0 1.0
iter_csv ¶
iter_csv(
path: str | Path,
target: str | None = None,
time_col: str | None = None,
columns: list[str] | None = None,
array_cols: list[str] | None = None,
start: float | None = None,
stop: float | None = None,
) -> Iterator[tuple[Signal, Any]]
Iterate rows of a CSV file as (Signal, y) pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
path to the CSV file. |
required |
target
|
str | None
|
label column, or |
None
|
time_col
|
str | None
|
timestamp column in seconds, or |
None
|
columns
|
list[str] | None
|
scalar feature columns. |
None
|
array_cols
|
list[str] | None
|
columns holding a list literal |
None
|
start
|
float | None
|
skip rows before this time; needs |
None
|
stop
|
float | None
|
stop at the first row past this time; needs |
None
|
Yields:
| Type | Description |
|---|---|
tuple[Signal, Any]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
on a requested column that is not in the file, or on
|
Source code in dense_armor/core/signals/stream/csv.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
rosbag ¶
Read ROS 1 and ROS 2 bags as a stream of :class:Signal samples.
The pure Python package rosbags reads both formats; it is an
optional dependency, installed with the extra [ros], and imported
inside :func:iter_rosbag. Every sensor_msgs/msg/JointState message
becomes one :class:Signal with channels q_<joint>,
qd_<joint>, tau_<joint>.
On Windows the rosbags package cannot generate ROS 1 message
definitions (it builds them with PosixPath), so ROS 1 bags are
tested on Linux and macOS.
Examples:
>>> from dense_armor.core.signals.stream import iter_rosbag
>>> stream = iter_rosbag("/path/to/bag")
iter_rosbag ¶
Iterate a ROS 1 or ROS 2 bag as (Signal, y) pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
ROS 1 |
required |
topics
|
list[str] | None
|
topic names to read. |
None
|
Yields:
| Type | Description |
|---|---|
tuple[Signal, Any]
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
if |
Source code in dense_armor/core/signals/stream/rosbag.py
array ¶
Read a numpy or JAX array as a stream of :class:Signal samples.
Examples:
>>> import numpy as np
>>> from dense_armor.core.signals.stream import iter_array
>>> X = np.array([[1.0, 2.0], [3.0, 4.0]])
>>> for sig, y in iter_array(X, names=["a", "b"], units=["m", "s"]):
... print(sig["a"], sig["b"], sig.t, y)
1.0 2.0 0.0 None
3.0 4.0 1.0 None
iter_array ¶
iter_array(
X: Any,
y: Any = None,
t: Any = None,
names: Iterable[str] | None = None,
units: Iterable[str] | None = None,
) -> Iterator[tuple[Signal, Any]]
Iterate rows of an array as (Signal, y) pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Any
|
array of shape |
required |
y
|
Any
|
optional labels of shape |
None
|
t
|
Any
|
optional timestamps in seconds. If |
None
|
names
|
Iterable[str] | None
|
channel names in column order. |
None
|
units
|
Iterable[str] | None
|
channel units. |
None
|
Yields:
| Type | Description |
|---|---|
tuple[Signal, Any]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if a length does not match. |
Source code in dense_armor/core/signals/stream/array.py
Details¶
iter_csvuses Python'scsvmodule, one row at a time; a list cell"[a, b, c]"becomes channels<name>_0, <name>_1, ...(spaces in the name become underscores);start/stopselect a window ontime_coland stop reading afterstop.iter_rosbag(path, topics=None)reads ROS 1 and ROS 2 bags with the pure Python packagerosbags(extra[ros]);sensor_msgs/msg/JointStatebecomes channelsq_<joint>,qd_<joint>,tau_<joint>with the time of the message header.