def test_magic_ponies_jars(self): def mock_center_of_mass(**kwargs): return (0, kwargs['diameter']) with unittest.mock.patch.object( phyre.creator.shapes.Jar, 'center_of_mass', side_effect=mock_center_of_mass) as mock_method: steps = 1 _, _, _, objects = simulator.magic_ponies( self._task_jar_test, self._ball_user_input, steps=steps, stride=1, need_images=False, need_featurized_objects=True) diameter = phyre.creator.shapes.Jar._diameter( **phyre.creator.shapes.Jar.default_sizes(0.3)) ideal_vector = np.array([[ 50 / 256., 30 / 256. + diameter / 256., 0.0, diameter / 256., 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 ], [70 / 256., 200 / 256., 0.0, 0.2, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0], [ 100 / 256., 100 / 256., 0, 3.9062500e-02, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 ]]) np.testing.assert_allclose(ideal_vector, objects[0], atol=1e-3)
def test_complicated_object_vec_to_scene(self): steps = 50 _, _, images, objects = simulator.magic_ponies( self._task_complicated_object_test, self._ball_user_input_big, steps=steps, stride=1, need_images=True, need_featurized_objects=True) objects = phyre.simulation.Simulation( featurized_objects=objects).featurized_objects.features for i in range(len(images)): image = images[i] original_object_vec = objects[i] object_vec = original_object_vec for j in range(5): # Test no loss of information in conversion self.assertTrue( np.allclose(original_object_vec, simulator.scene_to_featurized_objects( phyre.objects_util. featurized_objects_vector_to_scene( object_vec)).features, atol=1e-4)) object_vec = simulator.scene_to_featurized_objects( phyre.objects_util.featurized_objects_vector_to_scene( object_vec)).features[0] self.assertTrue((image == simulator.scene_to_raster( phyre.objects_util.featurized_objects_vector_to_scene( object_vec))).all())
def test_is_solution_valid(self): steps = 200 assert steps >= simulator.STEPS_FOR_SOLUTION # Empty solution should be valid. self.assertTrue( simulator.magic_ponies(self._task, self._box_compressed_user_input, steps=steps)[0])
def test_add_input_and_ponies(self): steps = 10 task_simulation = simulator.simulate_task_with_input( self._task, self._ball_user_input, steps=steps, stride=1) is_solved, had_occlusions, images, scenes = simulator.magic_ponies( self._task, self._ball_user_input, steps=steps, stride=1, need_images=True, need_featurized_objects=True) self.assertEqual(is_solved, task_simulation.isSolution) self.assertEqual(len(images), steps) self.assertEqual(len(task_simulation.sceneList), steps) self.assertEqual( had_occlusions, task_simulation.sceneList[0].user_input_status == scene_if.UserInputStatus.HAD_OCCLUSIONS) # Check images match target scenes self.assertFalse( np.array_equal( images[0], simulator.scene_to_raster(task_simulation.sceneList[-1]))) self.assertTrue((images[-1] == simulator.scene_to_raster( task_simulation.sceneList[-1])).all()) # Test just images works _, _, only_images, _ = simulator.magic_ponies( self._task, self._ball_user_input, steps=steps, stride=1, need_images=True, need_featurized_objects=False) self.assertTrue(np.array_equal(images, only_images)) # Test just scenes works _, _, _, only_scenes = simulator.magic_ponies( self._task, self._ball_user_input, steps=steps, stride=1, need_images=False, need_featurized_objects=True) self.assertTrue(np.array_equal(scenes, only_scenes))
def test_magic_ponies_objects(self): steps = 1 _, _, _, objects = simulator.magic_ponies(self._task_object_test, self._ball_user_input, steps=steps, stride=1, need_images=False, need_featurized_objects=True) ideal_vector = np.array([[ 50 / 256., 30 / 256., 350. / 360., 0.3, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0 ], [70 / 256., 200 / 256., 0.0, 0.2, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0], [ 100 / 256., 100 / 256., 0, 3.9062500e-02, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 ]]) np.testing.assert_allclose(ideal_vector, objects[0], atol=1e-3)
def test_add_input_and_ponies(self): raise unittest.SkipTest steps = 10 task_simulation = simulator.simulate_task_with_input( self._task, self._box_user_input, steps=steps, stride=1) is_solved, had_occlusions, images = simulator.magic_ponies( self._task, self._box_compressed_user_input, steps=steps, stride=1) self.assertEqual(is_solved, task_simulation.isSolution) self.assertEqual(len(images), steps) self.assertEqual(len(task_simulation.sceneList), steps) self.assertEqual( had_occlusions, task_simulation.sceneList[0].user_input_status == scene_if.UserInputStatus.HAD_OCCLUSIONS) self.assertFalse((images[0] == simulator.scene_to_raster( task_simulation.sceneList[-1])).all()) self.assertTrue((images[-1] == simulator.scene_to_raster( task_simulation.sceneList[-1])).all())
def test_object_vec_to_scene_simple(self): _, _, images, featurized_objects = simulator.magic_ponies( self._task_complicated_object_test, self._ball_user_input_big, steps=1, stride=1, need_images=True, need_featurized_objects=True) objects = phyre.simulation.Simulation( featurized_objects=featurized_objects).featurized_objects.features image = images[0] object_vec = objects[0] self.assertTrue( (image == phyre.objects_util.featurized_objects_vector_to_raster( object_vec)).all()) self.assertTrue( np.array_equal( object_vec, simulator.scene_to_featurized_objects( phyre.objects_util.featurized_objects_vector_to_scene( object_vec)).features[0]))
def test_object_vec_to_scene(self): steps = 50 _, _, images, objects = simulator.magic_ponies( self._task_object_test, self._ball_user_input_small, steps=steps, stride=1, need_images=True, need_featurized_objects=True) objects = phyre.simulation.Simulation( featurized_objects=objects).featurized_objects.features for i in range(len(images)): image = images[i] object_vec = objects[i] self.assertTrue( np.allclose( object_vec, simulator.scene_to_featurized_objects( phyre.objects_util.featurized_objects_vector_to_scene( object_vec)).features, atol=1e-4)) self.assertTrue((image == simulator.scene_to_raster( phyre.objects_util.featurized_objects_vector_to_scene( object_vec))).all())