def _in_front_template( figure: TemplateObjectVariable, ground: TemplateObjectVariable, background: Iterable[TemplateObjectVariable], *, is_training: bool, is_near: bool, speaker_root_node: OntologyNode = PERSON, ) -> Phase1SituationTemplate: handle = "training" if is_training else "testing" direction = Direction(positive=True, relative_to_axis=FacingAddresseeAxis(ground)) speaker = standard_object("speaker", speaker_root_node, added_properties=[IS_SPEAKER]) addressee = standard_object("addressee", LEARNER, added_properties=[IS_ADDRESSEE]) computed_background = [speaker, addressee] computed_background.extend(background) return Phase1SituationTemplate( f"preposition-{handle}-{figure.handle}-behind-{ground.handle}", salient_object_variables=[figure, ground], background_object_variables=computed_background, asserted_always_relations=[ near(figure, ground, direction=direction) if is_near else far(figure, ground, direction=direction) ], gazed_objects=[figure], )
def _beside_template( figure: TemplateObjectVariable, ground: TemplateObjectVariable, background: Iterable[TemplateObjectVariable], *, is_right: bool, is_training: bool, ) -> Phase1SituationTemplate: direction_str = "right" if is_right else "left" handle = "training" if is_training else "testing" return Phase1SituationTemplate( f"preposition-{handle}-{figure.handle}-beside-{ground.handle}-{direction_str}", salient_object_variables=[figure, ground], background_object_variables=background, asserted_always_relations=[ near( figure, ground, direction=Direction( positive=is_right, relative_to_axis=HorizontalAxisOfObject(ground, index=0), ), ) ], gazed_objects=[figure], )
def test_objects_in_something_not_implcitly_grounded(): box = situation_object(ontology_node=BOX) ball = situation_object(ontology_node=BALL) situation = HighLevelSemanticsSituation( ontology=GAILA_PHASE_1_ONTOLOGY, salient_objects=[box, ball], always_relations=[ Relation( IN_REGION, ball, Region( box, distance=PROXIMAL, direction=Direction( positive=True, relative_to_axis=HorizontalAxisOfObject(box, index=0), ), ), ) ], ) perception = _PERCEPTION_GENERATOR.generate_perception( situation, chooser=RandomChooser.for_seed(0) ) first_frame = perception.frames[0] ball_perception = perception_with_handle(first_frame, "**ball_0") ground_perception = perception_with_handle(first_frame, "the ground") box_perception = perception_with_handle(first_frame, "**box_0") first_frame_relations = first_frame.relations assert on(ball_perception, ground_perception)[0] in first_frame_relations assert on(box_perception, ground_perception)[0] in first_frame_relations
def make_bird_flies_over_a_house(): bird = situation_object(BIRD) house = situation_object(HOUSE) situation = HighLevelSemanticsSituation( ontology=GAILA_PHASE_1_ONTOLOGY, salient_objects=[bird, house], actions=[ Action( FLY, argument_roles_to_fillers=[(AGENT, bird)], during=DuringAction( at_some_point=[ Relation( IN_REGION, bird, Region( reference_object=house, distance=DISTAL, direction=Direction( positive=True, relative_to_axis=GRAVITATIONAL_AXIS_FUNCTION, ), ), ) ] ), ) ], ) return situation
def _in_front_template( figure: TemplateObjectVariable, ground: TemplateObjectVariable, background: Iterable[TemplateObjectVariable], *, is_training: bool, is_near: bool, background_relations: Iterable[Relation[Any]] = immutableset(), ) -> Phase1SituationTemplate: handle = "training" if is_training else "testing" direction = Direction(positive=True, relative_to_axis=FacingAddresseeAxis(ground)) relations = [ near(figure, ground, direction=direction) if is_near else far(figure, ground, direction=direction) ] relations.extend(background_relations) # type: ignore return Phase1SituationTemplate( f"preposition-{handle}-{figure.handle}-behind-{ground.handle}", salient_object_variables=[figure, ground], background_object_variables=background, asserted_always_relations=flatten_relations(relations), gazed_objects=[figure], )
def test_in_region_constraint() -> None: ball = ObjectPerception( "ball", axes=Axes( primary_axis=symmetric_vertical("ball-generating"), orienting_axes=immutableset( [symmetric("side-to-side0"), symmetric("side-to-side1")] ), ), ) box = ObjectPerception( "box", axes=Axes( primary_axis=straight_up("top_to_bottom"), orienting_axes=immutableset( [symmetric("side-to-side0"), symmetric("side-to-side1")] ), ), ) aabb_ball = AxisAlignedBoundingBox.create_at_center_point(center=np.array([0, 0, 1])) aabb_box = AxisAlignedBoundingBox.create_at_center_point(center=np.array([0, -2, 1])) # specifying that the box should be to the right of the ball direction = Direction( positive=True, relative_to_axis=HorizontalAxisOfObject(ball, index=0) ) region = Region(ball, PROXIMAL, direction) obj_percept_to_aabb = {ball: aabb_ball, box: aabb_box} in_region_relations = {box: [region]} in_region_penalty = InRegionPenalty(obj_percept_to_aabb, {}, {}, in_region_relations) box_penalty = in_region_penalty(box, immutableset([region])) assert box_penalty > 0 # now with a box that *is* to the right of the ball box2 = ObjectPerception( "box2", axes=Axes( primary_axis=straight_up("top_to_bottom"), orienting_axes=immutableset( [symmetric("side-to-side0"), symmetric("side-to-side1")] ), ), ) aabb_box2 = AxisAlignedBoundingBox.create_at_center_point(center=np.array([2, 0, 1])) obj_percept_to_aabb = {ball: aabb_ball, box2: aabb_box2} in_region_relations = {box2: [region]} in_region_penalty = InRegionPenalty(obj_percept_to_aabb, {}, {}, in_region_relations) box_penalty = in_region_penalty(box2, immutableset([region])) assert box_penalty == 0
def background_relations_builder( background_objects: Iterable[TemplateObjectVariable], num_relations: int, *, target: Optional[TemplateObjectVariable] = None, target_2: Optional[TemplateObjectVariable] = None, add_noise: bool = True, include_targets_in_noise: bool = False, chooser: RandomChooser = RandomChooser.for_seed(0), ) -> Iterable[Relation[Any]]: if add_noise: potential_objects = list(background_objects) if target and include_targets_in_noise: potential_objects.append(target) if target_2 and include_targets_in_noise: potential_objects.append(target_2) if len(potential_objects) < 2: return immutableset() relations = [] for _ in range(num_relations): choice = chooser.choice(NOISE_RELATION_DSL_OPTIONS) if choice == "on": relations.append( on( chooser.choice(potential_objects), chooser.choice(potential_objects), )) elif choice == "beside": obj_choice_2 = chooser.choice(potential_objects) relations.append( near( chooser.choice(potential_objects), obj_choice_2, direction=Direction( positive=chooser.choice(BOOL_SET), relative_to_axis=HorizontalAxisOfObject( obj_choice_2, index=0), ), )) elif choice == "under": relations.append( strictly_under( chooser.choice(potential_objects), chooser.choice(potential_objects), dist=DISTAL if chooser.choice(BOOL_SET) else PROXIMAL, )) elif choice == "in_front": obj_choice_2 = chooser.choice(potential_objects) direction = Direction( positive=chooser.choice(BOOL_SET), relative_to_axis=FacingAddresseeAxis(obj_choice_2), ) relations.append( near( chooser.choice(potential_objects), obj_choice_2, direction=direction, ) if chooser.choice(BOOL_SET) else far( chooser.choice(potential_objects), obj_choice_2, direction=direction, )) else: raise RuntimeError( "Invalid relation type in background relations") return flatten_relations(relations) else: return immutableset()