def pred_disagreement(self, entity, predication): if self.predtype in ('attribute', 'type'): return self.value.pred_disagreement(entity=entity, predication=predication) ref_entities = self.reference.filter_agreement( entities=predication.not_disagreeing, predication=predication) if len(ref_entities) == 0: return True if self.predtype == 'x-rel': return util.all_and_any( (entity.center.x - reference.center.x) * self.value < 0.0 for reference in ref_entities if reference != entity) elif self.predtype == 'y-rel': return util.all_and_any( (entity.center.y - reference.center.y) * self.value < 0.0 for reference in ref_entities if reference != entity) elif self.predtype == 'z-rel': for reference in ref_entities: collision = entity.collides(reference, ratio=True, symmetric=True) if collision > 0.0 and ( collision <= Settings.min_overlap or entity.id * self.value > reference.id * self.value): return False return True elif self.predtype == 'size-rel': return util.all_and_any( (reference.shape.area - entity.shape.area) * self.value > Settings.min_area for reference in ref_entities if reference != entity) elif self.predtype == 'shade-rel': return util.all_and_any( (reference.color.shade - entity.color.shade) * self.value > Settings.min_shade for reference in ref_entities if reference != entity and reference.color == entity.color) comp_entities = self.comparison.filter_agreement( entities=predication.not_disagreeing, predication=predication) if self.predtype == 'proximity-rel': for reference in ref_entities: for comparison in comp_entities: if reference.id == comparison.id or entity.id == reference.id or entity.id == comparison.id: continue if ((comparison.center - reference.center).length - (entity.center - reference.center).length ) * self.value < Settings.min_distance: return False return True
def pred_agreement(self, entity, predication): if self.predtype == 'relation': return self.value.pred_agreement(entity=entity, predication=predication) elif self.predtype == 'shape': return entity.shape.name == self.value elif self.predtype == 'color': return entity.color.name == self.value elif self.predtype == 'texture': return entity.texture.name == self.value elif self.predtype == 'combination': return (entity.shape.name, entity.color.name, entity.texture.name) == self.value elif self.predtype == 'shapes': return entity.shape.name in self.value elif self.predtype == 'colors': return entity.color.name in self.value elif self.predtype == 'textures': return entity.texture.name in self.value elif self.predtype == 'combinations': return (entity.shape.name, entity.color.name, entity.texture.name) in self.value elif self.predtype == 'x-max': return util.all_and_any((entity.center.x - other.center.x) * self.value > Settings.min_axis_distance for other in predication.agreeing if other != entity) elif self.predtype == 'y-max': return util.all_and_any((entity.center.y - other.center.y) * self.value > Settings.min_axis_distance for other in predication.agreeing if other != entity) elif self.predtype == 'size-max': return util.all_and_any((entity.shape.area - other.shape.area) * self.value > Settings.min_area for other in predication.agreeing if other != entity) elif self.predtype == 'shade-max': return util.all_and_any( (entity.color.shade - other.color.shade) * self.value > Settings.min_shade for other in predication.agreeing if other != entity and other.color == entity.color)
def implies(self, predicate, **kwargs): assert isinstance(predicate, Predicate) return util.all_and_any( predicate.pred_agreement(entity=entity, **kwargs) for entity in self.agreeing) and all( not predicate.pred_disagreement(entity=entity, **kwargs) for entity in self.ambiguous)
def pred_agreement(self, entity, scope_predication, ref_predication=None): scope_entities = scope_predication.not_disagreeing if all(other != entity for other in scope_entities): return False elif self.predtype == 'unique': return len(scope_entities) == 1 and len(scope_predication.agreeing) == 1 elif len(scope_predication.agreeing) < 2: return False elif self.predtype[-4:] == '-two' and (len(scope_entities) != 2 or len(scope_predication.agreeing) != 2): # print('c', len(scope_entities), len(scope_predication.agreeing)) return False elif self.predtype == 'x-two' or self.predtype == 'x-max': return util.all_and_any((entity.center.x - other.center.x) * self.value > Settings.min_axis_distance for other in scope_entities if other != entity) elif self.predtype == 'y-two' or self.predtype == 'y-max': return util.all_and_any((entity.center.y - other.center.y) * self.value > Settings.min_axis_distance for other in scope_entities if other != entity) elif self.predtype == 'size-two' or self.predtype == 'size-max': return util.all_and_any((entity.shape.area - other.shape.area) * self.value > Settings.min_area for other in scope_entities if other != entity and other.shape == entity.shape) elif self.predtype == 'shade-two' or self.predtype == 'shade-max': return util.all_and_any((entity.color.shade - other.color.shade) * self.value > Settings.min_shade for other in scope_entities if other != entity and other.color == entity.color) ref_entities = ref_predication.agreeing if self.predtype == 'proximity-two' or self.predtype == 'proximity-max': for other in scope_entities: for reference in ref_entities: if other == reference or entity == other or entity == reference: continue if ((entity.center - other.center).length() - (reference.center - other.center).length()) * self.value > Settings.min_distance: return True return False else: assert False
def __init__(self, world_size=64, world_color='black', shapes=('square', 'rectangle', 'triangle', 'pentagon', 'cross', 'circle', 'semicircle', 'ellipse'), colors=('red', 'green', 'blue', 'yellow', 'magenta', 'cyan', 'gray'), textures=('solid', ), rotation=True, size_range=(0.1, 0.25), distortion_range=(2.0, 3.0), shade_range=0.4, collision_tolerance=0.25, collision_shade_difference=0.5, boundary_tolerance=0.25, entity_counts=(1, ), train_entity_counts=None, validation_entity_counts=None, validation_count_rate=0.5, test_entity_counts=None, test_count_rate=0.5, validation_combinations=None, validation_space_rate_range=(0.0, 1.0), validation_combination_rate=0.5, test_combinations=None, test_space_rate_range=(0.0, 1.0), test_combination_rate=0.5): super(GenericGenerator, self).__init__( world_size=world_size, world_color=world_color, shapes=shapes, colors=colors, textures=textures, rotation=rotation, size_range=size_range, distortion_range=distortion_range, shade_range=shade_range, collision_tolerance=collision_tolerance, collision_shade_difference=collision_shade_difference, boundary_tolerance=boundary_tolerance) assert util.all_and_any( isinstance(n, int) and n >= 0 for n in entity_counts) assert train_entity_counts is None or util.all_and_any( isinstance(n, int) and n >= 0 for n in train_entity_counts) assert validation_entity_counts is None or util.all_and_any( isinstance(n, int) and n >= 0 for n in validation_entity_counts) assert test_entity_counts is None or util.all_and_any( isinstance(n, int) and n >= 0 for n in test_entity_counts) self.entity_counts = entity_counts self.train_entity_counts = util.value_or_default( train_entity_counts, entity_counts) self.validation_entity_counts = util.value_or_default( validation_entity_counts, entity_counts) self.validation_count_rate = validation_count_rate self.test_entity_counts = util.value_or_default( test_entity_counts, entity_counts) self.test_count_rate = test_count_rate assert (validation_combinations is None) == (test_combinations is None) self.validation_combinations = validation_combinations self.validation_space_rate_range = validation_space_rate_range self.validation_combination_rate = validation_combination_rate self.test_combinations = test_combinations self.test_space_rate_range = test_space_rate_range self.test_combination_rate = test_combination_rate self.invalid_combinations = set() self.invalid_validation_combinations = set() if self.validation_combinations is not None: assert len(self.validation_combinations) > 0 validation_shapes = set( shape for shape, _, _ in self.validation_combinations) validation_colors = set( color for _, color, _ in self.validation_combinations) validation_textures = set( texture for _, _, texture in self.validation_combinations) self.validation_space = [(shape, color, texture) for shape in validation_shapes for color in validation_colors for texture in validation_textures] self.invalid_combinations.update(self.validation_combinations) if self.test_combinations is not None: assert len(self.test_combinations) > 0 test_shapes = set(shape for shape, _, _ in self.test_combinations) test_colors = set(color for _, color, _ in self.test_combinations) test_textures = set(texture for _, _, texture in self.test_combinations) self.test_space = [(shape, color, texture) for shape in test_shapes for color in test_colors for texture in test_textures] self.invalid_combinations.update(self.test_combinations) self.invalid_validation_combinations.update(self.test_combinations)
def contradictory(self, predicate, **kwargs): assert isinstance(predicate, Predicate) return util.all_and_any(predicate.pred_disagreement(entity=entity, **kwargs) for entity in self.agreeing)
def tautological(self, predicate, **kwargs): assert isinstance(predicate, Predicate) return util.all_and_any(predicate.pred_agreement(entity=entity, **kwargs) for entity in self.agreeing) and \ all(predicate.pred_disagreement(entity=entity, predication=self, **kwargs) for entity in self.disagreeing)
def redundant(self, predicate, **kwargs): assert isinstance(predicate, Predicate) return util.all_and_any(predicate.pred_agreement(entity=entity, **kwargs) for entity in self.agreeing)
def redundant(self, predicate, predication=None): assert isinstance(predicate, Predicate) return util.all_and_any( predicate.pred_agreement(entity=entity, predication=predication) for entity in self.agreeing)
def __init__(self, entity_counts, world_size=None, world_color=None, shapes=None, colors=None, textures=None, rotation=None, size_range=None, distortion_range=None, shade_range=None, collision_tolerance=None, boundary_tolerance=None, train_entity_counts=None, validation_entity_counts=None, test_entity_counts=None, validation_combinations=None, test_combinations=None): super(GenericGenerator, self).__init__(world_size=world_size, world_color=world_color, shapes=shapes, colors=colors, textures=textures, rotation=rotation, size_range=size_range, distortion_range=distortion_range, shade_range=shade_range, collision_tolerance=collision_tolerance, boundary_tolerance=boundary_tolerance) assert util.all_and_any( isinstance(n, int) and n >= 0 for n in entity_counts) assert train_entity_counts is None or util.all_and_any( isinstance(n, int) and n >= 0 for n in train_entity_counts) assert validation_entity_counts is None or util.all_and_any( isinstance(n, int) and n >= 0 for n in validation_entity_counts) assert test_entity_counts is None or util.all_and_any( isinstance(n, int) and n >= 0 for n in test_entity_counts) self.entity_counts = entity_counts self.train_entity_counts = util.value_or_default( train_entity_counts, entity_counts) self.validation_entity_counts = util.value_or_default( validation_entity_counts, entity_counts) self.test_entity_counts = util.value_or_default( test_entity_counts, entity_counts) assert (validation_combinations is None) == (test_combinations is None) self.validation_combinations = util.value_or_default( validation_combinations, list()) self.test_combinations = util.value_or_default(test_combinations, list()) if validation_combinations is None: self.validation_shapes = list(self.shapes) self.validation_colors = list(self.colors) self.validation_textures = list(self.textures) else: assert len(validation_combinations) > 0 self.validation_shapes = [ shape for shape, _, _ in self.validation_combinations ] self.validation_colors = [ color for _, color, _ in self.validation_combinations ] self.validation_textures = [ texture for _, _, texture in self.validation_combinations ] if test_combinations is None: self.test_shapes = list(self.shapes) self.test_colors = list(self.colors) self.test_textures = list(self.textures) else: assert len(test_combinations) > 0 self.test_shapes = [ shape for shape, _, _ in self.test_combinations ] self.test_colors = [ color for _, color, _ in self.test_combinations ] self.test_textures = [ texture for _, _, texture in self.test_combinations ]