示例#1
0
def pyquest_spin_bintrees(data,row_alpha=0.5,col_alpha=0.5,beta=1.0,bal_constant=1.0,n_iters=3,n_spin=10):
    """
    runs what is momentarily the standard questionnaire algorithm:
    initial affinity = mutual cosine similarity
    initial tree based on median of successive eigenvectors
    dual affinities based on earth mover distance.
    dual trees based on eigen_cut method
    """
    #Generate initial affinity
    init_row_aff = affinity.mutual_cosine_similarity(data.T,False,0,threshold=0.1)
    
    #Compute diffusion embedding of initial affinities
    init_row_vecs,init_row_vals = markov.markov_eigs(init_row_aff, 12)
    #Generate median trees
    init_row_tree = bintree_construct.median_tree(init_row_vecs,init_row_vals,max_levels=12)

    row_trees, col_trees = [],[]

    for _ in xrange(n_spin):
        dual_col_trees = []
        dual_row_trees = [init_row_tree]
        
        for _ in xrange(n_iters):
            dual_col_trees.append(bintree_construct.eigen_tree(data,dual_row_trees[-1],alpha=col_alpha,beta=beta,bal_constant=bal_constant))
            dual_row_trees.append(bintree_construct.eigen_tree(data.T,dual_col_trees[-1],alpha=row_alpha,beta=beta,bal_constant=bal_constant))

        row_trees.append(dual_row_trees[-1])
        col_trees.append(dual_col_trees[-1])
            
    return row_trees,col_trees
示例#2
0
def pyquest(data,params):
    #params should be a PyQuestParams object

    Publisher.sendMessage("status.bar", "Calculating initial affinity...")
    if params.init_aff_type == INIT_AFF_COS_SIM:
        init_row_aff = affinity.mutual_cosine_similarity(
                            data.T,False,0,threshold=params.init_aff_threshold)
    elif params.init_aff_type == INIT_AFF_GAUSSIAN:
        #add KNN to the page
        init_row_aff = affinity.gaussian_euclidean(
                            data.T, 5, params.init_aff_epsilon)
    
    #Compute diffusion embedding of initial affinities
    init_row_vecs,init_row_vals = markov.markov_eigs(init_row_aff, 12)
    init_row_vals[np.isnan(init_row_vals)] = 0.0
    row_embedding = init_row_vecs.dot(np.diag(init_row_vals))
    row_distances = spsp.distance.squareform(spsp.distance.pdist(row_embedding))
    row_affinity = np.max(row_distances) - row_distances
    
    #Generate initial tree
    #print "call1 tree_constant:{}".format(tree_constant)
    Publisher.sendMessage("status.bar", "Calculating initial row tree...")

    if params.tree_type == TREE_TYPE_BINARY:
        init_row_tree = bintree_construct.median_tree(
                                init_row_vecs,init_row_vals,max_levels=12)
    elif params.tree_type == TREE_TYPE_FLEXIBLE:
#        init_row_tree = tree_building.make_tree_embedding(
#                                row_affinity,params.tree_constant)
        init_row_tree = tree_building.make_tree_embedding(
                                row_affinity,params.tree_constant)    
    dual_col_trees = []
    dual_row_trees = [init_row_tree]
    
    row_tree_descs = ["Initial tree"]
    col_tree_descs = []
    
    for i in xrange(params.n_iters):
        message = "Iteration {}: calculating column affinity...".format(i)
        Publisher.sendMessage("status.bar", message)

        #print "Beginning iteration {}".format(i)
        if params.col_affinity_type == DUAL_EMD:
            col_emd = dual_affinity.calc_emd(data,dual_row_trees[-1],
                     params.col_alpha,params.col_beta)
            col_aff = dual_affinity.emd_dual_aff(col_emd)
        elif params.col_affinity_type == DUAL_GAUSSIAN:
            print "Gaussian dual affinity not supported at the moment."
            return None
        
        message = "Iteration {}: calculating column tree...".format(i)
        Publisher.sendMessage("status.bar", message)

        if params.tree_type == TREE_TYPE_BINARY:
            col_tree = bintree_construct.eigen_tree(data,dual_row_trees[-1],
                    params.col_alpha,params.col_beta,params.tree_bal_constant)
        elif params.tree_type == TREE_TYPE_FLEXIBLE:
            col_tree = tree_building.make_tree_embedding(col_aff,
                                     params.tree_constant)
        dual_col_trees.append(col_tree)
        col_tree_descs.append("Iteration {}".format(i))

        message = "Iteration {}: calculating row affinity...".format(i)
        Publisher.sendMessage("status.bar", message)

        if params.row_affinity_type == DUAL_EMD:
            row_emd = dual_affinity.calc_emd(data.T,dual_col_trees[-1],
                     params.row_alpha,params.row_beta)
            row_aff = dual_affinity.emd_dual_aff(row_emd)
        elif params.row_affinity_type == DUAL_GAUSSIAN:
            print "Gaussian dual affinity not supported at the moment."
            return None
 
        message = "Iteration {}: calculating row tree...".format(i)
        Publisher.sendMessage("status.bar", message)
       
        if params.tree_type == TREE_TYPE_BINARY:
            row_tree = bintree_construct.eigen_tree(data.T,dual_col_trees[-1],
                    params.row_alpha,params.row_beta,params.tree_bal_constant)
        elif params.tree_type == TREE_TYPE_FLEXIBLE:
            row_tree = tree_building.make_tree_embedding(row_aff,
                                     params.tree_constant)
        dual_row_trees.append(row_tree)
        row_tree_descs.append("Iteration {}".format(i))
        quest_run_desc = "{}".format(datetime.datetime.now())

    return PyQuestRun(quest_run_desc,dual_row_trees,dual_col_trees,
                      row_tree_descs,col_tree_descs,params)