Skip to content

Commit

Permalink
[vizualizer] for LeRobodDataset V2 (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
mishig25 authored Dec 20, 2024
1 parent 66f8736 commit 73b64c3
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 135 deletions.
57 changes: 57 additions & 0 deletions lerobot/common/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import json
import logging
import textwrap
from collections.abc import Iterator
from itertools import accumulate
from pathlib import Path
from pprint import pformat
from types import SimpleNamespace
from typing import Any

import datasets
Expand Down Expand Up @@ -502,3 +504,58 @@ def create_lerobot_dataset_card(
template_path=str(card_template_path),
**kwargs,
)


class IterableNamespace(SimpleNamespace):
"""
A namespace object that supports both dictionary-like iteration and dot notation access.
Automatically converts nested dictionaries into IterableNamespaces.
This class extends SimpleNamespace to provide:
- Dictionary-style iteration over keys
- Access to items via both dot notation (obj.key) and brackets (obj["key"])
- Dictionary-like methods: items(), keys(), values()
- Recursive conversion of nested dictionaries
Args:
dictionary: Optional dictionary to initialize the namespace
**kwargs: Additional keyword arguments passed to SimpleNamespace
Examples:
>>> data = {"name": "Alice", "details": {"age": 25}}
>>> ns = IterableNamespace(data)
>>> ns.name
'Alice'
>>> ns.details.age
25
>>> list(ns.keys())
['name', 'details']
>>> for key, value in ns.items():
... print(f"{key}: {value}")
name: Alice
details: IterableNamespace(age=25)
"""

def __init__(self, dictionary: dict[str, Any] = None, **kwargs):
super().__init__(**kwargs)
if dictionary is not None:
for key, value in dictionary.items():
if isinstance(value, dict):
setattr(self, key, IterableNamespace(value))
else:
setattr(self, key, value)

def __iter__(self) -> Iterator[str]:
return iter(vars(self))

def __getitem__(self, key: str) -> Any:
return vars(self)[key]

def items(self):
return vars(self).items()

def values(self):
return vars(self).values()

def keys(self):
return vars(self).keys()
Loading

0 comments on commit 73b64c3

Please sign in to comment.