Exemplo n.º 1
0
    def _build_tree_static(self, proc_mat, fraction, parent_id, idx_map):
        """ Recursive top-down (start at root) construction 
            of a tree. This construction is static, i.e. it
            is built from the same processed pair-wise matrix,
            the relationships do not change based on the level
            of the tree.
        """
    
        if fraction < 0:
            return

        c = Components(proc_mat)
        components = c.get_components(fraction, proc_mat)[0]

        for component in components:
            i_map = idx_map[component]
            b_mat = self.base_affinity_matrix[i_map,:][:,i_map]
            p_mat = self.proc_affinity_matrix[i_map,:][:,i_map]
            keys = self.key_list[i_map]
            n_id = len(self.nodes)
            
            n = Node(i_map, keys, b_mat, p_mat, n_id)
            n._parent = parent_id

            if parent_id is not None:
                self.nodes[parent_id]._children.append(n_id)
            
            self.nodes[n_id] = n
            
            fraction = fraction - 1/float(self.fixed_k)
            self._build_tree_static(p_mat, fraction, n_id, i_map)
Exemplo n.º 2
0
    def _build_tree_dynamic(self):
        """ Non-recursive bottom-up construction of the tree;
            at each level the pair-wise relationships are updated
            between components - where the instances inside components
            influence each others relationship to instances in other
            components
        """
        node_id = 0
        fractions = dice_fractions(self.fixed_k)

        c = Components(self.proc_affinity_matrix)
        #Build the bottom level
        components, comp_mat = c.get_components(fractions.next(), 
                                                self.proc_affinity_matrix,
                                                strongly_connected=True)
        for component in components:
            base_mat = self.base_affinity_matrix[component,:][:,component]
            proc_mat = self.proc_affinity_matrix[component,:][:,component]
            keys = self.key_list[component]
            n = Node(component, keys, base_mat, proc_mat, node_id)
            self.nodes[node_id] = n
            node_id += 1

        node_offset = temp_offset = 0
        for fraction in fractions:
            temp_offset += len(components) 
            c = Components(comp_mat)
            components, comp_mat = c.get_components(fraction, comp_mat, True)
            for component in components:
                instances = []
                for instance in component:
                    idx = instance + node_offset
                    self.nodes[idx]._parent = node_id
                    instances += self.nodes[idx].list_of_instances
                base_mat = self.base_affinity_matrix[instances,:][:,instances]
                proc_mat = self.proc_affinity_matrix[instances,:][:,instances]
                
                keys = self.key_list[instances]
                n = Node(instances, keys, base_mat, proc_mat, node_id)
                n._children = list(asanyarray(component) + node_offset) 
                self.nodes[node_id] = n
                node_id += 1
  
            node_offset = temp_offset
        self.root_id = node_id - 1
Exemplo n.º 3
0
#cost_mat = genfromtxt(data_path + '/cost-matrices/' + sys.argv[1])

""" Compute the bullseye score.
    Assuming MPEG-7 data is loaded
"""
e = Evaluation(cost_mat, 20, 70)
print "Top 40 bullseye score: ", e.bullseye(40)

""" Compute a new similarity matrix using dice coefficient
    as a population cue
"""
#Geometric mean, to ensure symmetry
cost_mat = sqrt(cost_mat * cost_mat.transpose())
p = Population(cost_mat, 20, 70, verbose=True)
#Not setting k will attempt to automatically find it!
processed_matrix = p.generate_diff(k=13) 

e = Evaluation(processed_matrix, 20, 70)
print "Top 40 bullseye score using dice: ", e.bullseye(40)

""" Update the similarity matrix further using the previous
    matrix to build components 
"""
c = Components(processed_matrix)
c.get_components(0.3, strongly_connected=False)
comp_mat = c._expand_component_difference()

e = Evaluation(comp_mat, 20, 70)
print "Top 40 bullseye score after components: ", e.bullseye(40)