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_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_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_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_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_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_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_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_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_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_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_init_correct_two_domains(self): cs.init(6, {0: [0, 1], 1: [3, 4], 2: [2, 5]}) c = Cuboid( [1, 2, float("-inf"), 4, 5, float("-inf")], [6, 7, float("inf"), 8, 9, float("inf")], { 0: [0, 1], 1: [3, 4] }) self.assertEqual(c._domains, {0: [0, 1], 1: [3, 4]})
def test_init_incorrect_domain(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: [3, 4] })
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_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_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_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)
def test_project_correct(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,6],[7,7,7], {0:[0,1], 1:[2]}) s = Core([c1, c2],{0:[0,1], 1:[2]}) c1_res1 = Cuboid([1,2,float("-inf")],[7,8,float("inf")],{0:[0,1]}) c2_res1 = Cuboid([4,5,float("-inf")],[7,7,float("inf")],{0:[0,1]}) s_res1 = Core([c1_res1, c2_res1], {0:[0,1]}) c1_res2 = Cuboid([float("-inf"),float("-inf"),3],[float("inf"),float("inf"),9],{1:[2]}) c2_res2 = Cuboid([float("-inf"),float("-inf"),6],[float("inf"),float("inf"),7],{1:[2]}) s_res2 = Core([c1_res2, c2_res2], {1:[2]}) self.assertEqual(s.project_onto({0:[0,1]}), s_res1) self.assertEqual(s.project_onto({1:[2]}), s_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 test_distance_unit_diff_identically_weighted(self): n = 4 domains = {0: [0, 1], 1: [2, 3]} space.init(n, domains) dom = {0: 1, 1: 1} dim = {0: {0: 0.5, 1: 0.5}, 1: {2: 0.5, 3: 0.5}} w = Weights(dom, dim) x = [1, 2, 3, 4] y = [2, 3, 2, 3] # distance of 1 wrt each coordinate self.assertEqual(space.distance(x, y, w), 2.0) self.assertEqual(space.distance(x, y, w), space.distance(y, x, w))
def test_distance_illegal_point(self): n = 4 domains = {0: [0, 1], 1: [2, 3]} space.init(n, domains) dom = {0: 1, 1: 1} dim = {0: {0: 0.5, 1: 0.5}, 1: {2: 0.5, 3: 0.5}} w = Weights(dom, dim) x = [1, 2, 3, 4] y = [5, 6, 7] with self.assertRaises(Exception): space.distance(x, y, w)
def test_distance_other_diff_identically_weighted(self): n = 4 domains = {0: [0, 1], 1: [2, 3]} space.init(n, domains) dom = {0: 1, 1: 1} dim = {0: {0: 0.5, 1: 0.5}, 1: {2: 0.5, 3: 0.5}} w = Weights(dom, dim) x = [1, 2, 3, 4] y = [2, 0, 2, 2] # difference: 1 2 1 2 self.assertEqual(space.distance(x, y, w), sqrt(0.5 * 1 + 0.5 * 4) + sqrt(0.5 * 1 + 0.5 * 4)) self.assertEqual(space.distance(x, y, w), space.distance(y, x, w))
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 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_distance_other_diff_differently_weighted(self): n = 4 domains = {0: [0, 1], 1: [2, 3]} space.init(n, domains) dom = {0: 2, 1: 1} dim = {0: {0: 1, 1: 1}, 1: {2: 3, 3: 2.0}} w = Weights(dom, dim) x = [1, 2, 3, 4] y = [2, 0, 2, 2] # difference: 1 2 1 2 self.assertEqual(space.distance(x, y, w), (4.0 / 3) * sqrt(0.5 * 1 + 0.5 * 4) + (2.0 / 3) * sqrt(0.6 * 1 + 0.4 * 4)) self.assertEqual(space.distance(x, y, w), space.distance(y, x, w))
def test_delete_concept(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, 'r') self.assertTrue(42 in space._concepts) self.assertEqual(space._concepts[42], f) self.assertTrue(42 in space._concept_colors) self.assertTrue(space._concept_colors[42], 'r') space.delete_concept(43) self.assertTrue(42 in space._concepts) self.assertEqual(space._concepts[42], f) self.assertTrue(42 in space._concept_colors) self.assertTrue(space._concept_colors[42], 'r') space.delete_concept(42) self.assertFalse(42 in space._concepts) self.assertEqual(len(space._concepts), 0) self.assertFalse(42 in space._concept_colors) self.assertEqual(len(space._concept_colors), 0) space.delete_concept(1337) self.assertEqual(len(space._concepts), 0) self.assertEqual(len(space._concept_colors), 0) 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.delete_concept(42) self.assertFalse(42 in space._concepts) self.assertEqual(len(space._concepts), 0) self.assertFalse(42 in space._concept_colors) self.assertEqual(len(space._concept_colors), 0)
def display_space(): domain_mapping, data, dim_names = xml_to_dict("Dataset\\prototypes.xml") domain_mapping, data, dim_names = xml_to_dict( "Dataset\\exemplars.xml", domain_mapping=domain_mapping, data=data, dimension_names=dim_names) domain_mapping, data, dim_names = less_dimensions( domain_mapping=domain_mapping, data=data, dim_names=dim_names) space.init(len(dim_names), domain_mapping, dim_names) concepts = form_supercategories(data) for name, values in zip(concepts, concepts.values()): if not name == 'mammal': family_into_space(name, values, add=True) ci.init(dims=[6, 7, 8]) ''