Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #58: Expose state vector element names #111

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ def obs_dict(self):
@property
def obs_ndarray(self):
"""Numpy vector observation format."""
return vectorize_nested_dict(self.obs_dict)
_, obs = vectorize_nested_dict(self.obs_dict)
return obs

@property
def obs_array_keys(self):
"""Utility to get the keys of the obs_ndarray."""
keys, _ = vectorize_nested_dict(self.obs_dict)
return keys

@property
def obs_list(self):
Expand Down
15 changes: 11 additions & 4 deletions src/bsk_rl/envs/general_satellite_tasking/utils/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,25 @@ def collect_default_args(object: object) -> dict[str, Any]:
return defaults


def vectorize_nested_dict(dictionary: dict) -> np.ndarray:
"""Flattens a dictionary of dicts, arrays, and scalars into a single vector."""
def vectorize_nested_dict(dictionary: dict) -> tuple[list[str], np.ndarray]:
"""Flattens a dictionary of dictionaries, arrays, and scalars into a vector."""
keys = list(dictionary.keys())
values = list(dictionary.values())
for i, value in enumerate(values):
if isinstance(value, np.ndarray):
values[i] = value.flatten()
keys[i] = [keys[i] + f"[{j}]" for j in range(len(value.flatten()))]
elif isinstance(value, list):
keys[i] = [keys[i] + f"[{j}]" for j in range(len(value))]
elif isinstance(value, (float, int)):
values[i] = [value]
keys[i] = [keys[i]]
elif isinstance(value, dict):
values[i] = vectorize_nested_dict(value)
prepend = keys[i]
keys[i], values[i] = vectorize_nested_dict(value)
keys[i] = [prepend + "." + key for key in keys[i]]

return np.concatenate(values)
return list(np.concatenate(keys)), np.concatenate(values)


def aliveness_checker(func: Callable[..., bool]) -> Callable[..., bool]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,24 @@ class C13(self.C1, self.C3):


@pytest.mark.parametrize(
"input,output",
"input,outkeys,outvec",
[
({"a": np.array([1]), "b": 2, "c": [3]}, np.array([1, 2, 3])),
({"a": {"b": 1, "c": 2}, "d": 3}, np.array([1, 2, 3])),
(
{"alpha": np.array([1]), "b": 2, "c": [3]},
["alpha[0]", "b", "c[0]"],
np.array([1, 2, 3]),
),
(
{"a": {"b": 1, "charlie": 2}, "d": 3},
["a.b", "a.charlie", "d"],
np.array([1, 2, 3]),
),
],
)
def test_vectorize_nested_dict(input, output):
assert np.equal(output, functional.vectorize_nested_dict(input)).all()
def test_vectorize_nested_dict(input, outkeys, outvec):
keys, vec = functional.vectorize_nested_dict(input)
assert np.equal(outvec, vec).all()
assert outkeys == keys


class TestAlivenessChecker:
Expand Down
Loading