def step(self, action=None, **action_args): if type(action) is dict: action = copy.deepcopy(action) # prevent changes from leaking else: action = dict(action=action) raise_for_failure = action_args.pop('raise_for_failure', False) action.update(action_args) if self.headless: action["renderImage"] = False action['sequenceId'] = self.sequence_id action['agentId'] = self.agent_id self.last_action = action rotation = action.get('rotation') if rotation is not None and type(rotation) != dict: action['rotation'] = {} action['rotation']['y'] = rotation payload = self._post_event('step', action) events = [] for i, agent_metadata in enumerate(payload['metadata']['agents']): event = Event(agent_metadata) image_mapping = dict( image=lambda x: event.add_image( x, flip_y=False, flip_rb_colors=False), image_depth=lambda x: event.add_image_depth_robot( x, self.depth_format, camera_near_plane=self.camera_near_plane, camera_far_plane=self.camera_far_plane, flip_y=False, dtype=np.float64)) for key in image_mapping.keys(): if key in payload and len(payload[key]) > i: image_mapping[key](payload[key][i]) events.append(event) if len(events) > 1: self.last_event = MultiAgentEvent(self.agent_id, events) else: self.last_event = events[0] if not self.last_event.metadata[ 'lastActionSuccess'] and self.last_event.metadata[ 'errorCode'] == 'InvalidAction': raise ValueError(self.last_event.metadata['errorMessage']) if raise_for_failure: assert self.last_event.metadata['lastActionSuccess'] # pprint("Display event:") # Controller._display_step_event(self.last_event) return self.last_event
def test_failure(): fake_event = Event(dict(screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=False, errorCode='NotOpen')) c = ai2thor.controller.Controller() c.last_event = fake_event action1 = dict(action='MoveAhead') c.request_queue = FakeQueue() c.request_queue.put_nowait(fake_event) e = c.step(action1) assert c.last_action == action1 assert not e.metadata['lastActionSuccess']
def test_raise_for_failure(): fake_event = Event(dict(screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=False, errorCode='NotOpen')) c = ai2thor.controller.Controller() c.last_event = fake_event action1 = dict(action='MoveAhead') c.request_queue = FakeQueue() c.request_queue.put_nowait(fake_event) with pytest.raises(AssertionError): c.step(action1, raise_for_failure=True)
def test_invalid_action(): fake_event = Event(dict(screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=False, errorCode='InvalidAction', errorMessage='Invalid method: moveaheadbadmethod')) c = ai2thor.controller.Controller() c.last_event = fake_event action1 = dict(action='MoveaheadbadMethod') c.request_queue = FakeQueue() c.request_queue.put_nowait(fake_event) with pytest.raises(ValueError) as excinfo: c.step(action1, raise_for_failure=True) assert excinfo.value.args == ('Invalid method: moveaheadbadmethod',)
def test_step_put_object_in_wrong_receptacle(): fake_event = Event( dict(screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=True)) c = ai2thor.controller.Controller() c.last_event = fake_event e = c.step( dict(action='PutObject', objectId='Mug|1.2|3.4|5.6', receptacleObjectId="Box|5.6|4.3|2.1")) assert not e.metadata['lastActionSuccess']
def test_fix_visibility_distance_env(): os.environ['AI2THOR_VISIBILITY_DISTANCE'] = '2.0' fake_event = Event(dict(screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=True)) c = ai2thor.controller.Controller() c.last_event = fake_event action1 = dict(action='Initialize', gridSize=0.25) c.request_queue = FakeQueue() c.request_queue.put_nowait(fake_event) c.step(action1) filtered_action = c.response_queue.get() print(filtered_action) assert filtered_action == {'action': 'Initialize', 'gridSize': 0.25, 'visibilityDistance':2.0} del(os.environ['AI2THOR_VISIBILITY_DISTANCE'])
def test_cast_numpy_generic(): fake_event = Event( dict(screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=True)) c = ai2thor.controller.Controller() c.last_event = fake_event action1 = dict(action='MoveAhead', moveMagnitude=np.ones(1)[0]) c.request_queue = FakeQueue() c.request_queue.put_nowait(fake_event) c.step(action1) filtered_action = c.response_queue.get() print(filtered_action) assert filtered_action == {'action': 'MoveAhead', 'moveMagnitude': 1.0} assert type(filtered_action['moveMagnitude']) is float
def test_failure(): fake_event = Event( dict( screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=False, errorCode="NotOpen", )) c = controller() c.last_event = fake_event action1 = dict(action="MoveAhead") c.server.request_queue.put_nowait(fake_event) e = c.step(action1) assert c.last_action == action1 assert not e.metadata["lastActionSuccess"]
def test_raise_for_failure(): fake_event = Event( dict( screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=False, errorCode="NotOpen", )) c = controller() c.last_event = fake_event action1 = dict(action="MoveAhead") c.server.request_queue.put_nowait(fake_event) with pytest.raises(RuntimeError): c.step(action1, raise_for_failure=True)
def test_last_action(): fake_event = Event(dict(screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=True)) c = ai2thor.controller.Controller() c.last_event = fake_event action1 = dict(action='RotateRight') c.request_queue = FakeQueue() c.request_queue.put_nowait(fake_event) e = c.step(action1) assert c.last_action == action1 assert e.metadata['lastActionSuccess'] c = ai2thor.controller.Controller() c.last_event = fake_event action2 = dict(action='RotateLeft') c.request_queue = FakeQueue() c.request_queue.put_nowait(fake_event) e = c.step(action2) assert c.last_action == action2 assert e.metadata['lastActionSuccess']
def test_fix_visibility_distance_env(): try: os.environ["AI2THOR_VISIBILITY_DISTANCE"] = "2.0" fake_event = Event( dict(screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=True)) c = controller() c.last_event = fake_event action1 = dict(action="Initialize", gridSize=0.25) c.server.request_queue.put_nowait(fake_event) c.step(action1) filtered_action = c.server.response_queue.get() assert filtered_action == { "action": "Initialize", "gridSize": 0.25, "visibilityDistance": 2.0, } finally: del os.environ["AI2THOR_VISIBILITY_DISTANCE"]
def test_last_action(): fake_event = Event( dict(screenWidth=300, screenHeight=300, colors=[], lastActionSuccess=True)) c = controller() c.last_event = fake_event action1 = dict(action="RotateRight") c.server.request_queue.put_nowait(fake_event) e = c.step(action1) assert c.last_action == action1 assert e.metadata["lastActionSuccess"] c = controller() c.last_event = fake_event action2 = dict(action="RotateLeft") c.server.request_queue.put_nowait(fake_event) e = c.step(action2) assert c.last_action == action2 assert e.metadata["lastActionSuccess"]