def test_eq_ne_shallow_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]}) s2 = Core([c], {0: [0, 1, 2]}) self.assertTrue(s == s2) self.assertFalse(s != s2)
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_get_center_two_cuboids_infinity(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"),1,1], [float("inf"),4,4], {1:[1,2]}) s = Core([c1,c2], {1:[1,2]}) c_res = Cuboid([-float("inf"),2,3], [float("inf"),4,4], {1:[1,2]}) self.assertEqual(s.get_center(), c_res)
def test_find_closest_point_candidates_infinity(self): cs.init(3, {0:[0], 1:[1,2]}) c1 = Cuboid([float("-inf"),2,3],[float("inf"),8,9], {1:[1,2]}) c2 = Cuboid([float("-inf"),5,6],[float("inf"),9,7], {1:[1,2]}) s = Core([c1, c2], {1:[1,2]}) p = [12,-2,8] self.assertEqual(s.find_closest_point_candidates(p), [[12,2,8],[12,5,7]])
def test_find_closest_point_candidates_two_cuboids(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]}) s = Core([c1, c2], {0:[0,1,2]}) p = [12,-2,8] self.assertEqual(s.find_closest_point_candidates(p), [[7,2,8],[7,5,7]])
def test_midpoint_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]}) self.assertEqual(s.midpoint(), [3.5, 3.5, 3.5])
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_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_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.union_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_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_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_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 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_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_constructor_nonintersecting(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]}) l = [c1, c2] with self.assertRaises(Exception): Core(l, {0: [0, 1, 2]})
def test_constructor_correct_arg(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]}) l = [c1, c2] s = Core(l, {0: [0, 1, 2]}) self.assertEqual(s._cuboids, l)
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_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_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_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 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 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_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 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_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_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)
import cs.cs as space from cs.weights import Weights from cs.cuboid import Cuboid from cs.core import Core from cs.concept import Concept import visualization.concept_inspector as ci # define the conceptual space domains = {"color": [0], "taste": [1, 2]} dimension_names = ["hue", "sour", "sweet"] space.init(3, domains, dimension_names) # define red property c_red = Cuboid([0.7, float("-inf"), float("-inf")], [1.0, float("inf"), float("inf")], {"color": [0]}) s_red = Core([c_red], {"color": [0]}) w_red = Weights({"color": 1.0}, {"color": {0: 1.0}}) red = Concept(s_red, 1.0, 40.0, w_red) space.add_concept("red", red, 'r') # define yellow property c_yellow = Cuboid([0.4, float("-inf"), float("-inf")], [0.6, float("inf"), float("inf")], {"color": [0]}) s_yellow = Core([c_yellow], {"color": [0]}) w_yellow = Weights({"color": 1.0}, {"color": {0: 1.0}}) yellow = Concept(s_yellow, 1.0, 40.0, w_yellow) space.add_concept("yellow", yellow, 'y') # define green property c_green = Cuboid([0.0, float("-inf"), float("-inf")], [0.3, float("inf"), float("inf")], {"color": [0]})