IsaacLab icon indicating copy to clipboard operation
IsaacLab copied to clipboard

[Bug Report] Allowing None type in config class changes observation ordering

Open reeceomahoney opened this issue 1 year ago • 3 comments

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: contains 1 groups. +-------------------------------------------------------+ | Active Observation Terms in Group: 'policy' (shape: (41,)) | +-------------+---------------------------+-------------+ | Index | Name | Shape | +-------------+---------------------------+-------------+ | 0 | pose_command | (7,) | | 1 | joint_pos | (9,) | | 2 | joint_vel | (9,) | | 3 | ee_pose | (9,) | | 4 | actions | (7,) | +-------------+---------------------------+-------------+

reeceomahoney avatar Jan 28 '25 15:01 reeceomahoney

Thank you for posting this. The team will take a look. Thanks.

RandomOakForest avatar Jan 30 '25 18:01 RandomOakForest

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,

  1. since ordering is maintained within default fields, a term hint (like None) can be given to all fields
  2. 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

matthewtrepte avatar Feb 21 '25 22:02 matthewtrepte

Hi Matthew, thanks for getting back. This should be a sufficient workaround for now, I'm happy to call this closed.

reeceomahoney avatar Feb 27 '25 11:02 reeceomahoney