def test_eq_ne_deep_copy(self): cs.init(3, {0:[0,1,2]}) c = Cuboid([1,2,3],[7,8,9], {0:[0,1,2]}) s = Core([c], {0:[0,1,2]}) c2 = Cuboid([1,2,3],[7,8,9], {0:[0,1,2]}) s2 = Core([c2], {0:[0,1,2]}) self.assertTrue(s == s2) self.assertFalse(s != s2)
def test_check_false(self): cs.init(3, {0:[0,1,2]}) c1 = Cuboid([1,2,3],[4,5,6], {0:[0,1,2]}) c2 = Cuboid([0,0,0],[1,1,1], {0:[0,1,2]}) c3 = Cuboid([1,1,1],[2,3,4], {0:[0,1,2]}) l = [c1, c2, c3] s = Core([c1], {0:[0,1,2]}) self.assertFalse(check(l, s._domains))
def test_add_cuboid_different_relevant_dimensions(self): cs.init(3, {0:[0], 1:[1], 2:[2]}) c1 = Cuboid([float("-inf"),2,3],[float("inf"),5,6], {1:[1], 2:[2]}) c2 = Cuboid([2,float("-inf"),4],[5,float("inf"),7], {0:[0], 2:[2]}) s1 = Core([c1], {1:[1], 2:[2]}) s2 = Core([c2], {0:[0], 2:[2]}) self.assertFalse(s1.add_cuboid(c2)) self.assertFalse(s2.add_cuboid(c1))
def test_check_true(self): cs.init(3, {0:[0,1,2]}) c1 = Cuboid([1,2,3],[4,5,6], {0:[0,1,2]}) c2 = Cuboid([2,3,4],[5,6,7], {0:[0,1,2]}) c3 = Cuboid([2,2,2],[12.4,12.5,12.6], {0:[0,1,2]}) l = [c1, c2, c3] s = Core(l, {0:[0,1,2]}) self.assertTrue(check(s._cuboids, s._domains))
def test_get_center_three_cuboids(self): cs.init(3, {0:[0], 1:[1,2]}) c1 = Cuboid([1,2,3], [4,5,6], {0:[0], 1:[1,2]}) c2 = Cuboid([3,2,1], [6,5,4], {0:[0], 1:[1,2]}) c3 = Cuboid([1,3,2], [5,4,6], {0:[0], 1:[1,2]}) s = Core([c1,c2,c3], {0:[0], 1:[1,2]}) c_res = Cuboid([3,3,3], [4,4,4], {0:[0], 1:[1,2]}) self.assertEqual(s.get_center(), c_res)
def test_unify_not_full_dims_different_dims(self): cs.init(3, {0:[0,1], 1:[2]}) c1 = Cuboid([1,2,3],[7,8,9], {0:[0,1], 1:[2]}) c2 = Cuboid([4,5,float("-inf")],[7,7,float("inf")], {0:[0,1]}) s1 = Core([c1], {0:[0,1], 1:[2]}) s2 = Core([c2], {0:[0,1]}) with self.assertRaises(Exception): s1.unify_with(s2)
def test_eq_ne_different_cores(self): cs.init(3, {0:[0,1,2]}) c = Cuboid([1,2,3],[7,8,9], {0:[0,1,2]}) c2 = Cuboid([6,5,4],[9,8,7], {0:[0,1,2]}) s = Core([c], {0:[0,1,2]}) s2 = Core([c2], {0:[0,1,2]}) self.assertTrue(s != s2) self.assertFalse(s == s2)
def test_eq_ne_reversed_cuboid_order(self): cs.init(3, {0:[0,1,2]}) c = Cuboid([1,2,3],[7,8,9], {0:[0,1,2]}) c2 = Cuboid([6,5,4],[9,8,7], {0:[0,1,2]}) s = Core([c, c2], {0:[0,1,2]}) s2 = Core([c2, c], {0:[0,1,2]}) self.assertTrue(s == s2) self.assertFalse(s != s2)
def test_unify_not_full_dims_same_dims(self): cs.init(3, {0: [0, 1], 1: [2]}) c1 = Cuboid([1, 2, float("-inf")], [7, 8, float("inf")], {0: [0, 1]}) c2 = Cuboid([4, 5, float("-inf")], [8, 7, float("inf")], {0: [0, 1]}) s1 = Core([c1], {0: [0, 1]}) s2 = Core([c2], {0: [0, 1]}) s_result = Core([c1, c2], {0: [0, 1]}) self.assertEqual(s1.union_with(s2), s_result) self.assertEqual(s1.union_with(s2), s2.union_with(s1))
def test_unify_no_repair(self): cs.init(3, {0: [0, 1, 2]}) c1 = Cuboid([1, 2, 3], [7, 8, 9], {0: [0, 1, 2]}) c2 = Cuboid([4, 5, 6], [7, 9, 7], {0: [0, 1, 2]}) s1 = Core([c1], {0: [0, 1, 2]}) s2 = Core([c2], {0: [0, 1, 2]}) s_result = Core([c1, c2], {0: [0, 1, 2]}) self.assertEqual(s1.union_with(s2), s_result) self.assertEqual(s1.union_with(s2), s2.union_with(s1))
def test_add_cuboid_same_relevant_dimensions(self): cs.init(3, {0: [0], 1: [1, 2]}) c1 = Cuboid([float("-inf"), 2, 3], [float("inf"), 5, 6], {1: [1, 2]}) c2 = Cuboid([float("-inf"), 3, 4], [float("inf"), 6, 7], {1: [1, 2]}) s1 = Core([c1], {1: [1, 2]}) s2 = Core([c2], {1: [1, 2]}) self.assertTrue(s1.add_cuboid(c2)) self.assertTrue(s2.add_cuboid(c1)) self.assertEqual(s1, s2)
def test_unify_repair(self): cs.init(3, {0: [0, 1, 2]}) c1 = Cuboid([1, 2, 3], [2, 3, 4], {0: [0, 1, 2]}) c2 = Cuboid([3, 4, 5], [7, 7, 7], {0: [0, 1, 2]}) s1 = Core([c1], {0: [0, 1, 2]}) s2 = Core([c2], {0: [0, 1, 2]}) c1_result = Cuboid([1, 2, 3], [3.25, 4, 4.75], {0: [0, 1, 2]}) c2_result = Cuboid([3, 4, 4.75], [7, 7, 7], {0: [0, 1, 2]}) s_result = Core([c1_result, c2_result], {0: [0, 1, 2]}) self.assertEqual(s1.union_with(s2), s_result) self.assertEqual(s1.union_with(s2), s2.union_with(s1))
def test_add_cuboid_false(self): cs.init(3, {0: [0, 1, 2]}) c1 = Cuboid([1, 2, 3], [4, 5, 6], {0: [0, 1, 2]}) c2 = Cuboid([0, 0, 0], [1, 1, 1], {0: [0, 1, 2]}) c3 = Cuboid([1, 1, 1], [2, 3, 4], {0: [0, 1, 2]}) l = [c1] s = Core(l, {0: [0, 1, 2]}) self.assertFalse(s.add_cuboid(c2)) self.assertEqual(s._cuboids, [c1]) self.assertTrue(s.add_cuboid(c3)) self.assertEqual(s._cuboids, [c1, c3])
def test_add_cuboid_true(self): cs.init(3, {0: [0, 1, 2]}) c1 = Cuboid([1, 2, 3], [4, 5, 6], {0: [0, 1, 2]}) c2 = Cuboid([2, 3, 4], [5, 6, 7], {0: [0, 1, 2]}) c3 = Cuboid([2, 2, 2], [12.4, 12.5, 12.6], {0: [0, 1, 2]}) l = [c1] s = Core(l, {0: [0, 1, 2]}) self.assertTrue(s.add_cuboid(c2)) self.assertEqual(s._cuboids, [c1, c2]) self.assertTrue(s.add_cuboid(c3)) self.assertEqual(s._cuboids, [c1, c2, c3])
def test_constructor_different_relevant_dimensions(self): cs.init(3, {0: [0], 1: [1], 2: [2]}) c1 = Cuboid([float("-inf"), 2, 3], [float("inf"), 5, 6], { 1: [1], 2: [2] }) c2 = Cuboid([2, float("-inf"), 4], [5, float("inf"), 7], { 0: [0], 2: [2] }) with self.assertRaises(Exception): Core([c1, c2], {0: [0], 1: [1], 2: [2]})
def test_cut_through_one_cuboid(self): cs.init(3, {0: [0, 1, 2]}) c1 = Cuboid([1, 2, 3], [7, 8, 9], {0: [0, 1, 2]}) c2 = Cuboid([4, 5, 6], [7, 7, 7], {0: [0, 1, 2]}) s1 = Core([c1, c2], {0: [0, 1, 2]}) low_c1 = Cuboid([1, 2, 3], [7, 8, 5], {0: [0, 1, 2]}) low_s = Core([low_c1], {0: [0, 1, 2]}) up_c1 = Cuboid([1, 2, 5], [7, 8, 9], {0: [0, 1, 2]}) up_c2 = Cuboid([4, 5, 6], [7, 7, 7], {0: [0, 1, 2]}) up_s = Core([up_c1, up_c2], {0: [0, 1, 2]}) self.assertEqual(s1.cut_at(2, 5), (low_s, up_s))
def test_cut_infinity(self): cs.init(3, {0:[0], 1:[1], 2:[2]}) c1 = Cuboid([1,float("-inf"),3],[7,float("inf"),9], {0:[0], 2:[2]}) c2 = Cuboid([4,float("-inf"),6],[7,float("inf"),7], {0:[0], 2:[2]}) s1 = Core([c1, c2], {0:[0], 2:[2]}) low_c1 = Cuboid([1,float("-inf"),3],[7,float("inf"),5], {0:[0], 2:[2]}) low_s = Core([low_c1], {0:[0], 2:[2]}) up_c1 = Cuboid([1,float("-inf"),5],[7,float("inf"),9], {0:[0], 2:[2]}) up_c2 = Cuboid([4,float("-inf"),6],[7,float("inf"),7], {0:[0], 2:[2]}) up_s = Core([up_c1, up_c2], {0:[0], 2:[2]}) self.assertEqual(s1.cut_at(2, 5), (low_s, up_s))
def one_shot(point, supercategory='none'): #todo find lowest concept '''print(point)''' for concept in space._concepts: '''print(concept) print('membership:') print(space._concepts[concept].membership_of(point)) print()''' if supercategory=='none': superkey = max(space._concepts,key= lambda candidate:space._concepts[candidate].membership_of(point)) else: superkey = supercategory supercategory = space._concepts[superkey] avg = lambda values: sum(values)/len(values) if len(values) else float('inf') #print([[cuboid._p_max[dim]-cuboid._p_min[dim] for cuboid in supercategory._core._cuboids if not cuboid._p_max[dim]==float('inf')]for dim in range(space._n_dim)]) avg_sizes=[avg([cuboid._p_max[dim]-cuboid._p_min[dim] for cuboid in supercategory._core._cuboids if not cuboid._p_max[dim]==float('inf')]) for dim in range(space._n_dim)] print('supercategory is:') print(superkey) print(avg_sizes) print() p_min = [point[i]-1/2*avg_sizes[i] for i in range(space._n_dim)] p_max = [point[i]+1/2*avg_sizes[i] for i in range(space._n_dim)] cuboid=Cuboid(p_min, p_max, space._domains)#not working in bigger examples core=Core([cuboid],space._domains) weights=Weights(space._def_dom_weights,space._def_dim_weights) concept=Concept(core, 1.0, 0.5, weights) return concept
def test_init_incorrect_domain_inf(self): cs.init(6, {0: [0, 1, 3, 4], 1: [2], 2: [5]}) with self.assertRaises(Exception): Cuboid([1, 2, float("-inf"), 4, 5, float("-inf")], [6, 7, float("inf"), 8, 9, float("inf")], {0: [0, 1, 3, 5]})
def test_add_concept_correct(self): space.init(4, {0: [0, 1], 1: [2, 3]}) s = Core([Cuboid([1, 2, 3, 4], [3, 4, 5, 6], { 0: [0, 1], 1: [2, 3] })], { 0: [0, 1], 1: [2, 3] }) dom = {0: 2, 1: 1} dim = {0: {0: 1, 1: 1}, 1: {2: 3, 3: 2.0}} w = Weights(dom, dim) f = Concept(s, 0.5, 2.0, w) space.add_concept(42, f) self.assertTrue(42 in space._concepts) self.assertEqual(space._concepts[42], f) self.assertFalse(42 in space._concept_colors) space.add_concept(43, f, 'r') self.assertTrue(43 in space._concepts) self.assertEqual(space._concepts[43], f) self.assertTrue(43 in space._concept_colors) self.assertEqual(space._concept_colors[43], 'r')
def test_init_correct(self): cs.init(6, {0: [0, 1, 3, 4], 1: [2], 2: [5]}) c = Cuboid( [1, 2, float("-inf"), 4, 5, float("-inf")], [6, 7, float("inf"), 8, 9, float("inf")], {0: [0, 1, 3, 4]}) self.assertEqual(c._domains, {0: [0, 1, 3, 4]})
def test_add_cuboid_no_cuboid(self): cs.init(3, {0: [0, 1, 2]}) c1 = Cuboid([1, 2, 3], [4, 5, 6], {0: [0, 1, 2]}) l = [c1] s = Core(l, {0: [0, 1, 2]}) with self.assertRaises(Exception): s.add_cuboid(42) self.assertEqual(s._cuboids, [c1])
def test_intersect_two_infinity_same(self): cs.init(3, {0: [0, 1], 1: [2]}) c1 = Cuboid([0, 0, float("-inf")], [2, 2, float("inf")], {0: [0, 1]}) c2 = Cuboid([2, 1, float("-inf")], [3, 3, float("inf")], {0: [0, 1]}) c3 = Cuboid([2, 1, float("-inf")], [2, 2, float("inf")], {0: [0, 1]}) self.assertEqual(c1.intersect_with(c2), c3) self.assertEqual(c1.intersect_with(c2), c2.intersect_with(c1))
def point_to_concept2(point, name, size=100000, weights=[]): domains = domains_from_point(point) p_min = [-size for value in point] p_max = [size for value in point] c_example = Cuboid(p_min, p_max, domains) s_example = Core([c_example], domains) if not weights: weights = space._def_dom_weights w_example = Weights(weights, space._def_dim_weights) concept = Concept(s_example, 1.0, C, w_example) space.add_concept(name, concept) return concept
def point_to_concept(point, name, weights=[]): domains = domains_from_point(point) p_min = [ value if not value == float('inf') else float('-inf') for value in point ] c_example = Cuboid(p_min, point, domains) s_example = Core([c_example], domains) if not weights: weights = space._def_dom_weights w_example = Weights(weights, space._def_dim_weights) concept = Concept(s_example, 1.0, C, w_example) space.add_concept(name, concept) return concept
def test_project_correct(self): cs.init(3, {0: [0, 1], 1: [2]}) c1 = Cuboid([0, 1, 2], [3, 4, 5], {0: [0, 1], 1: [2]}) c_res1 = Cuboid([0, 1, float("-inf")], [3, 4, float("inf")], {0: [0, 1]}) c_res2 = Cuboid([float("-inf"), float("-inf"), 2], [float("inf"), float("inf"), 5], {1: [2]}) self.assertEqual(c1.project_onto({0: [0, 1]}), c_res1) self.assertEqual(c1.project_onto({1: [2]}), c_res2)
def test_intersect_two_infinity_different(self): cs.init(3, {0: [0], 1: [1], 2: [2]}) c1 = Cuboid([0, 0, float("-inf")], [2, 2, float("inf")], { 0: [0], 1: [1] }) c2 = Cuboid([2, float("-inf"), 1], [3, float("inf"), 3], { 0: [0], 2: [2] }) c3 = Cuboid([2, 0, 1], [2, 2, 3], {0: [0], 1: [1], 2: [2]}) self.assertEqual(c1.intersect_with(c2), c3) self.assertEqual(c1.intersect_with(c2), c2.intersect_with(c1))
def family_into_space(name, values, add=True): cuboids = [] domains = {} for point in values: subdomains = point_to_domains(point) cuboids.append( Cuboid([dim if not dim == float('inf') else -dim for dim in point], point, subdomains)) domains.update(subdomains) core = from_cuboids(cuboids, domains) weights = Weights(space._def_dom_weights, space._def_dim_weights) concept = Concept(core, 1.0, C, weights) if add: space.add_concept(name, concept) return concept
def test_get_most_distant_points_same_domains(self): self._create_cs() c1 = Cuboid([0, 4, 1], [2, 5, 6], {0: [0, 1, 2]}) c2 = Cuboid([1, 1, 2], [3, 3, 3], {0: [0, 1, 2]}) a_res = [0, 5, 6] b_res = [3, 1, 2] a, b = c1.get_most_distant_points(c2) b2, a2 = c2.get_most_distant_points(c1) self.assertEqual(a, a_res) self.assertEqual(b, b_res) self.assertEqual(a, a2) self.assertEqual(b, b2)
def test_get_closest_points_subdomains(self): cs.init(3, {0: [0, 1], 1: [2]}) c1 = Cuboid([0, 1, float("-inf")], [1, 2, float("inf")], {0: [0, 1]}) c2 = Cuboid([2, 1, 4], [3, 4, 5], {0: [0, 1], 1: [2]}) a_res = [[1, 1], [1, 2], [4, 5]] b_res = [[2, 2], [1, 2], [4, 5]] a, b = c1.get_closest_points(c2) b2, a2 = c2.get_closest_points(c1) self.assertEqual(a, a_res) self.assertEqual(b, b_res) self.assertEqual(a, a2) self.assertEqual(b, b2)