def testMJCFFeature(self): mjcf_root = mjcf.from_xml_string(_MJCF) physics = mjcf.Physics.from_mjcf_model(mjcf_root) my_hinge = mjcf_root.find('joint', 'my_hinge') hinge_observable = mjcf_observable.MJCFFeature(kind='qpos', mjcf_element=my_hinge) hinge_observation = hinge_observable.observation_callable(physics)() np.testing.assert_array_equal( hinge_observation, physics.named.data.qpos[my_hinge.full_identifier]) small_sphere = mjcf_root.find('geom', 'small_sphere') box_observable = mjcf_observable.MJCFFeature(kind='xpos', mjcf_element=small_sphere, update_interval=5) box_observation = box_observable.observation_callable(physics)() self.assertEqual(box_observable.update_interval, 5) np.testing.assert_array_equal( box_observation, physics.named.data.geom_xpos[small_sphere.full_identifier]) my_box = mjcf_root.find('geom', 'my_box') list_observable = mjcf_observable.MJCFFeature( kind='xpos', mjcf_element=[my_box, small_sphere]) list_observation = (list_observable.observation_callable(physics)()) np.testing.assert_array_equal( list_observation, physics.named.data.geom_xpos[[ my_box.full_identifier, small_sphere.full_identifier ]]) with self.assertRaisesRegexp(ValueError, 'expected an `mjcf.Element`'): mjcf_observable.MJCFFeature('qpos', 'my_hinge') with self.assertRaisesRegexp(ValueError, 'expected an `mjcf.Element`'): mjcf_observable.MJCFFeature('geom_xpos', [my_box, 'small_sphere'])
def testMJCFFeatureIndex(self): mjcf_root = mjcf.from_xml_string(_MJCF) physics = mjcf.Physics.from_mjcf_model(mjcf_root) small_sphere = mjcf_root.find('geom', 'small_sphere') sphere_xmat = np.array( physics.named.data.geom_xmat[small_sphere.full_identifier]) observable_xrow = mjcf_observable.MJCFFeature( 'xmat', small_sphere, index=[1, 3, 5, 7]) np.testing.assert_array_equal( observable_xrow.observation_callable(physics)(), sphere_xmat[[1, 3, 5, 7]]) observable_yyzz = mjcf_observable.MJCFFeature('xmat', small_sphere)[2:6] np.testing.assert_array_equal( observable_yyzz.observation_callable(physics)(), sphere_xmat[2:6])