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 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 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 concepts_into_space(concepts, domains={}, except_for=None, colors=['b','g','r','c','m','brown','rosybrown','darkslategrey','pink','grey']): if except_for: for concept in list(concepts.keys()): if concepts[concept]['supercategory']==except_for: del(concepts[concept]) ordered_dict={} for example in concepts: if concepts[example]['supercategory'] in ordered_dict: ordered_dict[concepts[example]['supercategory']].append(example) else: ordered_dict[concepts[example]['supercategory']]=[example] '''for key in ordered_dict: print(key)''' #ordered_dict={'mammal':ordered_dict['mammal']} #print(ordered_dict) colors=iter(colors) concept_colors={} if(domains=={}): for concept in concepts.values(): domains.update(concept) del(domains['supercategory']) #domains = mapping: domain->dimension indices domain_mapping = {} i=0 for key in domains: domain_mapping[key]=list(range(i,i+len(domains[key]))) i+=len(domains[key]) dimension_names = [] for domain in domains.values(): dimension_names += [dim for dim in domain.keys()] space.init(len(dimension_names),domain_mapping,dimension_names) for category in ordered_dict: print('putting',category,'into space') cuboids=[] domains = {} for example in ordered_dict[category]: if not is_consistent(concepts[example]): print(example,' is inconsistent') else: subdomains={domain:space._domains[domain] for domain in concepts[example] if not domain == 'supercategory'} point=[concepts[example].get(domain,{str(key):float("inf") for key in range(len(space._domains[domain]))}).values() for domain in space._domains] point=[p for dom in point for p in dom] #print(example) #print('point: ',point) cuboids.append(Cuboid([dim if not dim==float('inf') else -dim for dim in point], point, subdomains)) domains.update(subdomains) #print(cuboids[-1]._p_max) #for cuboid in cuboids: #print('cuboid:',cuboid) core=from_cuboids(cuboids,domains) weights=Weights(space._def_dom_weights,space._def_dim_weights) concept=Concept(core, 1.0, 0.5, weights) space.add_concept(category,concept,next(colors)) animal_concept=[] for concept in space._concepts.values(): animal_concept+=concept._core._cuboids #print('animal cubs:\n',animal_concept) core=from_cuboids(animal_concept,space._domains) weights=Weights(space._def_dom_weights,space._def_dim_weights) concept=Concept(core, 1.0, 0.5, weights) space.add_concept('animal',concept,next(colors))
p_max=[value for domain in dimension_values for value in domain[1]] #try: c_example = Cuboid(p_min, p_max, domains) s_example = Core([c_example], domains) w_example = Weights(space._def_dom_weights,space._def_dim_weights) bear = Concept(s_example, 1.0, 0.5, w_example) space.add_concept(example, bear, 'yellow')''' '''for concept in space._concepts:''' #print(concept) p_max=[value for domain in dimension_values for value in domain[1]] print('bear:') print(p_max) space.add_concept(example+' from membership',one_shot(p_max),'forestgreen') space.add_concept(example+' from animal',one_shot(p_max,supercategory='animal'),'lime') example='camel' to_learn={example:mammal[example]} dimension_values=[] concepts=to_learn consistent = True #print(bear) for domain in space._domains.keys(): dimension_values.append([list(concepts[example].get(domain,{str(key):float("-inf") for key in range(len(space._domains[domain]))}).values()) ,list(concepts[example].get(domain,{str(key):float("inf") for key in range(len(space._domains[domain]))}).values())]) if(not(len(dimension_values[-1][0])==len(space._domains[domain]))): print(domain+' in '+example+' is inconsistent') consistent=False
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]}) s_green = Core([c_green], {"color": [0]}) w_green = Weights({"color": 1.0}, {"color": {0: 1.0}}) green = Concept(s_green, 1.0, 40.0, w_green)
def test_add_concept_failure(self): space.init(4, {0: [0, 1], 1: [2, 3]}) with self.assertRaises(Exception): space.add_concept(42, 1337)
import visualization.concept_inspector as ci # define the conceptual space domains = {"color": [0], "shape": [1], "taste": [2]} dimension_names = ["hue", "round", "sweet"] space.init(3, domains, dimension_names) # standard weights for the dimensions within each domain w_dim = {"color": {0: 1}, "shape": {1: 1}, "taste": {2: 1}} # define pear concept c_pear = Cuboid([0.5, 0.4, 0.35], [0.7, 0.6, 0.45], domains) s_pear = Core([c_pear], domains) w_pear = Weights({"color": 0.50, "shape": 1.25, "taste": 1.25}, w_dim) pear = Concept(s_pear, 1.0, 24.0, w_pear) space.add_concept("pear", pear, 'g') # define orange concept c_orange = Cuboid([0.8, 0.9, 0.6], [0.9, 1.0, 0.7], domains) s_orange = Core([c_orange], domains) w_orange = Weights({"color": 1.0, "shape": 1.0, "taste": 1.0}, w_dim) orange = Concept(s_orange, 1.0, 30.0, w_orange) space.add_concept("orange", orange, 'orange') # define lemon concept c_lemon = Cuboid([0.7, 0.45, 0.0], [0.8, 0.55, 0.1], domains) s_lemon = Core([c_lemon], domains) w_lemon = Weights({"color": 0.5, "shape": 0.5, "taste": 2.0}, w_dim) lemon = Concept(s_lemon, 1.0, 40.0, w_lemon) space.add_concept("lemon", lemon, 'y')
def generate(target='bird', dims=[6, 7, 8], n=None): #read concepts from prototypes and exemplars 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) testdata = deserialize_testdata('testdata.pkl') if not n: n = random.choice(list(range(len(testdata[target])))) ex = {'target': target, 'point': testdata[target][n], 'n': n} sibblings = [family for family in concepts if not family == target] print(ex) centers = np.array(concepts[target]).T means = [] for dimension in range(len(centers)): means.append( np.average([ value for value in centers[dimension] if not value == float('inf') ])) means = [mean if not np.isnan(mean) else float('inf') for mean in means] aps = evaluate_example(ex, data, concepts, domain_mapping, dim_names, method=two_cubs_PCA, testdata=testdata, criterion=ap_balanced) aps = aps[list(aps.keys())[0]] space.init(len(dim_names), domain_mapping, dim_names) for name, values in zip(concepts, concepts.values()): family_into_space(name, values, add=True) space.add_concept( 'learnt_' + target, one_shot(ex['point'], method=two_cubs_PCA, sibblings=sibblings, name="learnt_" + target, learn_weights=False)) space.add_concept( 'cheated_' + target, one_shot(means, method=two_cubs_PCA, sibblings=sibblings, name=target + 'cheated_' + target, learn_weights=False)) del (space._concepts['mammal']) ci.init(dims=dims) #ap_learnt, ap_naive, ap_known, ap_cheated, ap_supercheated print('learnt', aps[0]) print('small', aps[1]) print('big', aps[2]) print('known', aps[3]) print('cheated', aps[4])