[Bug Report] Allowing None type in config class changes observation ordering
When creating an observation config class like below, if I add a type hint for a term to be ObsTerm | None, the order of the observations is changed. Instead of being created in the order defined, the None typed term is moved to the top.
`@configclass class ObservationsCfg: """Observation specifications for the MDP."""
@configclass
class PolicyCfg(ObsGroup):
"""Observations for policy group."""
# observation terms (order preserved)
joint_pos = ObsTerm(
func=mdp.joint_pos_rel, noise=Unoise(n_min=-0.01, n_max=0.01)
)
joint_vel = ObsTerm(
func=mdp.joint_vel_rel, noise=Unoise(n_min=-0.01, n_max=0.01)
)
ee_pose = ObsTerm(
func=mdp.ee_pose,
noise=Unoise(n_min=-0.01, n_max=0.01),
params={"asset_cfg": SceneEntityCfg("robot", body_names="panda_hand")},
)
pose_command: ObsTerm | None = ObsTerm(
func=mdp.generated_commands, params={"command_name": "ee_pose"}
)
actions = ObsTerm(func=mdp.last_action)
def __post_init__(self):
self.enable_corruption = True
self.concatenate_terms = True
# observation groups
policy: PolicyCfg = PolicyCfg()
[INFO] Observation Manager:
Thank you for posting this. The team will take a look. Thanks.
hey @reeceomahoney,
The None term hint makes pose_command a default field.
For this configclass, it looks like python moves default fields before non-default fields.
if you want to explicitly define an ordering, here's a messy workaround with field and default_factory
from dataclasses import field
@configclass
class ObservationsCfg:
"""Observation specifications for the MDP."""
@configclass
class PolicyCfg(ObsGroup):
"""Observations for policy group."""
# observation terms (order preserved)
joint_pos = field(
default_factory=lambda: ObsTerm(func=mdp.joint_pos_rel, noise=Unoise(n_min=-0.01, n_max=0.01)
)
joint_vel = field(
default_factory=lambda: ObsTerm(func=mdp.joint_vel_rel, noise=Unoise(n_min=-0.01, n_max=0.01)
)
ee_pose = field(
default_factory=lambda: ObsTerm(
func=mdp.ee_pose,
noise=Unoise(n_min=-0.01, n_max=0.01),
params={"asset_cfg": SceneEntityCfg("robot", body_names="panda_hand")},
)
)
pose_command: ObsTerm | None = field(
default_factory=lambda: ObsTerm(
func=mdp.generated_commands, params={"command_name": "ee_pose"}
)
)
actions = field(
default_factory=lambda: ObsTerm(func=mdp.last_action)
)
def __post_init__(self):
self.enable_corruption = True
self.concatenate_terms = True
# observation groups
policy: PolicyCfg = PolicyCfg()
[INFO] Observation Manager: contains 1 groups. +-------------------------------------------------------+ | Active Observation Terms in Group: 'policy' (shape: (41,)) | +-------------+---------------------------+-------------+ | Index | Name | Shape | +-------------+---------------------------+-------------+ | 0 | joint_pos | (9,) | | 1 | joint_vel | (9,) | | 2 | ee_pose | (9,) | | 3 | pose_command | (7,) | | 4 | actions | (7,) | +-------------+---------------------------+-------------+
Let me know if this works for you.
Alternatively,
- since ordering is maintained within default fields, a term hint (like
None) can be given to all fields - fields can be defined at runtime in
__post_init__to avoid reordering, but that doesn't support static type checking and the class instance will always need to be created
Hi Matthew, thanks for getting back. This should be a sufficient workaround for now, I'm happy to call this closed.