Fix add_features for multi-dimensional per-frame features

This commit is contained in:
felixmin
2026-03-21 12:25:07 +01:00
committed by CarolinePascal
parent 4c302572c0
commit e0e03887e0
2 changed files with 38 additions and 2 deletions
+4 -2
View File
@@ -1045,10 +1045,12 @@ def _copy_data_with_feature_changes(
df[feature_name] = feature_values df[feature_name] = feature_values
else: else:
feature_slice = values[frame_idx:end_idx] feature_slice = values[frame_idx:end_idx]
if len(feature_slice.shape) > 1 and feature_slice.shape[1] == 1: if feature_slice.ndim == 1:
df[feature_name] = feature_slice
elif feature_slice.ndim == 2 and feature_slice.shape[1] == 1:
df[feature_name] = feature_slice.flatten() df[feature_name] = feature_slice.flatten()
else: else:
df[feature_name] = feature_slice df[feature_name] = list(feature_slice)
frame_idx = end_idx frame_idx = end_idx
# Write using the same chunk/file structure as source # Write using the same chunk/file structure as source
+34
View File
@@ -378,6 +378,40 @@ def test_add_features_with_callable(sample_dataset, tmp_path):
assert float(first_frame["reward"]) == 0.0 assert float(first_frame["reward"]) == 0.0
def test_add_features_with_multidimensional_values(sample_dataset, tmp_path):
"""Test adding a multi-dimensional per-frame feature."""
num_frames = sample_dataset.meta.total_frames
latent_values = np.random.randn(num_frames, 1, 4).astype(np.float32)
feature_info = {
"dtype": "float32",
"shape": (1, 4),
"names": None,
}
features = {
"latent_vectors": (latent_values, feature_info),
}
with (
patch("lerobot.datasets.dataset_metadata.get_safe_version") as mock_get_safe_version,
patch("lerobot.datasets.dataset_metadata.snapshot_download") as mock_snapshot_download,
):
mock_get_safe_version.return_value = "v3.0"
mock_snapshot_download.return_value = str(tmp_path / "with_latents")
new_dataset = add_features(
dataset=sample_dataset,
features=features,
output_dir=tmp_path / "with_latents",
)
assert "latent_vectors" in new_dataset.meta.features
sample_item = new_dataset[0]
assert "latent_vectors" in sample_item
assert isinstance(sample_item["latent_vectors"], torch.Tensor)
assert tuple(sample_item["latent_vectors"].shape) == (1, 4)
def test_add_existing_feature(sample_dataset, tmp_path): def test_add_existing_feature(sample_dataset, tmp_path):
"""Test error when adding an existing feature.""" """Test error when adding an existing feature."""
feature_info = {"dtype": "float32", "shape": (1,)} feature_info = {"dtype": "float32", "shape": (1,)}