Skip to content

Commit fd6a7f5

Browse files
committed
Addressing reviews
1 parent 26dbf24 commit fd6a7f5

3 files changed

Lines changed: 50 additions & 27 deletions

File tree

source/isaaclab/isaaclab/sim/simulation_context.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pxr import Gf, PhysxSchema, Sdf, Usd, UsdPhysics, UsdUtils
3131

3232
import isaaclab.sim as sim_utils
33+
from isaaclab.utils.dict import to_flat_dict
3334
from isaaclab.utils.logger import configure_logging
3435
from isaaclab.utils.version import get_isaac_sim_version
3536

@@ -41,33 +42,6 @@
4142
logger = logging.getLogger(__name__)
4243

4344

44-
def to_flat_dict(input_dict: dict[str, Any], delimiter: str = ".") -> dict[str, Any]:
45-
"""A simple method to transform a nested dict with key strings into a flat dict
46-
where keys are separated with a given delimiter. For example, if the input dictionary
47-
is {"foo": "bar", "spam": {"egg": "ham"}}, and the delimiter is ".", then the output
48-
would be {"foo": "bar", "spam.egg": "ham"}
49-
50-
Args:
51-
input_dict (dict[str, Any]): Input dictionary with string keys, potentially nested.
52-
delimiter (str, optional): Delimiter for concatenating keys. Defaults to ".".
53-
54-
Returns:
55-
dict[str, Any]: Output flattened dictionary with nested keys.
56-
"""
57-
out_dict = {}
58-
for key, value in input_dict.items():
59-
# If we have a dict inside the current value, we need to flatten it.
60-
if isinstance(value, dict):
61-
inner_flattened_dict = to_flat_dict(value, delimiter)
62-
# Recursively combine parent key with inner flattened directory.
63-
for inner_key, inner_value in inner_flattened_dict.items():
64-
out_dict[f"{key}{delimiter}{inner_key}"] = inner_value
65-
# If we are already flat, keep as is.
66-
else:
67-
out_dict[key] = value
68-
return out_dict
69-
70-
7145
class SimulationContext(_SimulationContext):
7246
"""A class to control simulation-related events such as physics stepping and rendering.
7347

source/isaaclab/isaaclab/utils/dict.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,30 @@ def print_dict(val, nesting: int = -4, start: bool = True):
342342
print(callable_to_string(val))
343343
else:
344344
print(val)
345+
346+
347+
def to_flat_dict(input_dict: dict[str, Any], delimiter: str = ".") -> dict[str, Any]:
348+
"""A simple method to transform a nested dict with key strings into a flat dict
349+
where keys are separated with a given delimiter. For example, if the input dictionary
350+
is {"foo": "bar", "spam": {"egg": "ham"}}, and the delimiter is ".", then the output
351+
would be {"foo": "bar", "spam.egg": "ham"}
352+
353+
Args:
354+
input_dict (dict[str, Any]): Input dictionary with string keys, potentially nested.
355+
delimiter (str, optional): Delimiter for concatenating keys. Defaults to ".".
356+
357+
Returns:
358+
dict[str, Any]: Output flattened dictionary with nested keys.
359+
"""
360+
out_dict = {}
361+
for key, value in input_dict.items():
362+
# If we have a dict inside the current value, we need to flatten it.
363+
if isinstance(value, dict):
364+
inner_flattened_dict = to_flat_dict(value, delimiter)
365+
# Recursively combine parent key with inner flattened directory.
366+
for inner_key, inner_value in inner_flattened_dict.items():
367+
out_dict[f"{key}{delimiter}{inner_key}"] = inner_value
368+
# If we are already flat, keep as is.
369+
else:
370+
out_dict[key] = value
371+
return out_dict

source/isaaclab/test/utils/test_dict.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,25 @@ def test_dict_to_md5():
9797
for _ in range(200):
9898
md5_hash_2 = dict_utils.dict_to_md5_hash(test_dict)
9999
assert md5_hash_1 == md5_hash_2
100+
101+
102+
def test_to_flat_dict():
103+
"""Test dictionary flattening."""
104+
105+
# create a complex nested dictionary
106+
test_dict = {
107+
"a": 1,
108+
"b": 2,
109+
"c": {"d": 3, "e": 4, "f": {"g": 5, "h": 6}},
110+
"i": random.random(),
111+
"k": dict_utils.callable_to_string(dict_utils.class_to_dict),
112+
}
113+
flattened_dict = dict_utils.to_flat_dict(test_dict)
114+
115+
# Test that we match the nesting
116+
expected_keys = ["a", "b", "c.d", "c.e", "c.f.g", "c.f.h"]
117+
expected_values = [1, 2, 3, 4, 5, 6]
118+
assert len(flattened_dict) == len(expected_keys)
119+
for key, value in flattened_dict:
120+
assert key in expected_keys
121+
assert value in expected_values

0 commit comments

Comments
 (0)