Пример #1
0
    def test_optimiser_is_membership_fixed_large_labels(self):
        G = ig.Graph.Erdos_Renyi(n=100, p=5. / 100, directed=True, loops=True)

        membership = list(range(G.vcount()))
        partition = leidenalg.RBConfigurationVertexPartition(
            G, initial_membership=membership)

        # large enough to force nonconsecutive labels in the final partition
        fixed_node_idx = 90
        is_membership_fixed = [False] * G.vcount()
        is_membership_fixed[fixed_node_idx] = True

        original_quality = partition.quality()
        diff = self.optimiser.optimise_partition(
            partition, is_membership_fixed=is_membership_fixed)

        self.assertLess(
            len(set(partition.membership)),
            len(partition),
            msg="Optimisation with fixed nodes yielded too many communities")
        self.assertAlmostEqual(
            partition.quality() - original_quality,
            diff,
            places=10,
            msg="Optimisation with fixed nodes returned inconsistent quality")
        self.assertEqual(
            partition.membership[fixed_node_idx],
            fixed_node_idx,
            msg=
            "Optimisation with fixed nodes failed to keep the associated community labels fixed"
        )
Пример #2
0
    def leiden(self, G, interslice, resolution):
        
        layers, interslice_layer, G_full = la.time_slices_to_layers(G, interslice_weight = interslice)
        
        partitions = [la.RBConfigurationVertexPartition(H, 
                                            weights = 'weight', 
                                            resolution_parameter = resolution) for H in layers]
        
        interslice_partition = la.RBConfigurationVertexPartition(interslice_layer, 
                                                                 weights = 'weight',
                                                                 resolution_parameter = 0)
                                                     
        optimiser = la.Optimiser()
        
        diff = optimiser.optimise_partition_multiplex(partitions + [interslice_partition])

        return(partitions, interslice_partition)
Пример #3
0
def run_alg(G, alg, gamma=1.0):
    '''
    run community detection algorithm with resolution parameter. Right now only use RB in Louvain
    :param G: an igraph graph
    :param gamma: resolution parameter
    :return: 
    '''
    if alg == 'louvain':
        partition_type = louvain.RBConfigurationVertexPartition
        partition = louvain.find_partition(G,
                                           partition_type,
                                           resolution_parameter=gamma)
    elif alg == 'leiden':
        partition_type = leidenalg.RBConfigurationVertexPartition
        partition = leidenalg.RBConfigurationVertexPartition(
            G, partition_type, resolution_parameter=gamma)
    # partition = sorted(partition, key=len, reverse=True)
    return partition
    def fit_transform(self, graphs):
        G=[]
        for graph in graphs:
            if type(graph) is ig.Graph:
                G.append(graph)
            elif issparse(graph):
                G.append(self._scipy_to_igraph(graph))
            else:
                G.append(self._other_to_igraph(graph))
                
        if self.verbose:
            for i in range(len(G)):
                print("View Graph {}: num_nodes: {}, num_edges: {}, directed: {}, num_components: {}, num_isolates: {}"
                      .format(i, G[i].vcount(), G[i].ecount(), G[i].is_directed(), 
                              len(G[i].components(mode='WEAK').sizes()), G[i].components(mode='WEAK').sizes().count(1)))
        
        self.weights = []
        self.resolutions =[]
        self.best_modularity =-np.inf
        self.best_clustering = None
        self.best_resolutions = None
        self.best_weights = None
        self.modularities =[]
        self.clusterings =[]
        self.final_iteration = 0
        self.best_iteration = 0
        
        weights = [1]*len(G)
        resolutions =[1]*len(G)
        
        for iterate in range(self.max_clusterings):
            partitions = []
            for i in range(len(G)):
                partitions.append(la.RBConfigurationVertexPartition(G[i], resolution_parameter=resolutions[i]))
                
            optimiser = la.Optimiser()
            diff = optimiser.optimise_partition_multiplex(partitions, layer_weights = weights, n_iterations=self.n_iterations)
            self.clusterings.append(np.array(partitions[0].membership))
            self.modularities.append([part.quality()/(part.graph.ecount() if part.graph.is_directed() else 2*part.graph.ecount()) 
                                      for part in partitions])
            self.weights.append(weights.copy())
            self.resolutions.append(resolutions.copy())
            self.final_iteration +=1
            
            
            if self.verbose:
                print("--------")
                print("Iteration: {} \n Modularities: {} \n Resolutions: {} \n Weights: {}"
                      .format(self.final_iteration, self.modularities[-1], resolutions, weights))
            
            # if np.sum(np.array(self.weights[-1]) * np.array(self.modularities[-1])) > self.best_modularity:
            self.best_clustering = self.clusterings[-1]
            self.best_modularity = np.sum(np.array(self.weights[-1]) * np.array(self.modularities[-1]))
            self.best_resolutions = self.resolutions[-1]
            self.best_weights = self.weights[-1]
            self.best_iteration = self.final_iteration
                
            theta_in, theta_out = self._calculate_edge_probabilities(G)
            for i in range(len(G)):
                resolutions[i] = (theta_in[i] - theta_out[i])/ (np.log(theta_in[i]) - np.log(theta_out[i]))
                weights[i] = (np.log(theta_in[i]) - np.log(theta_out[i]))/(np.mean([np.log(theta_in[j]) - np.log(theta_out[j]) for j in range(len(G))]))

                
            if (np.all(np.abs(np.array(self.resolutions[-1])-np.array(resolutions)) <= self.resolution_tol)
                and np.all(np.abs(np.array(self.weights[-1])-np.array(weights)) <= self.resolution_tol)):
                break
        else:
            best_iteration = np.argmax([np.sum(np.array(self.weights[i]) * np.array(self.modularities[i]))
                                        for i in range(len(self.modularities))])
            self.best_clustering = self.clusterings[best_iteration]
            self.best_modularity = np.sum(np.array(self.weights[best_iteration]) * np.array(self.modularities[best_iteration]))
            self.best_resolutions = self.resolutions[best_iteration]
            self.best_weights = self.weights[best_iteration]
            self.best_iteration = best_iteration
            
            if self.verbose:
                print("MVMC did not converge, best result found: Iteration: {}, Modularity: {}, Resolutions: {}, Weights: {}"
                      .format(self.best_iteration, self.best_modularity, self.best_resolutions, self.best_weights))


        return self.best_clustering
def community_detection(eID,
                        probe="both",
                        bin=0.02,
                        sensitivity=1,
                        visual=False,
                        feedbackType=None,
                        user_start="trial_start",
                        user_end="trial_end",
                        region_list=[],
                        difficulty=[-1, 1],
                        percentage=1,
                        data=None):
    """
    Function:
    Takes an experiment ID and makes community detection analysis 


    Parameters:
    eID: experiment ID 
    probe: name of the probe wanted or both for both probes
    bin: the size of the bin
    sensitivity: the sensibility parameter for the leiden algorithm
    visual: a boolean on whether visualization is wanted
    feedbackType: value for feedback wanted
    starts: the name of the type of start intervals
    ends: the name of the type of end intervals



    Return:
    partition: ig graph vertex partition object
    partition_dictionary: a dictionary with keys for each community and sets as values with the indices of the clusters that belong to that community, and the key
    region_dict: dictionary keyed by community number and value of a dictionary with the names of the brain regions of that community and their frequency
    locations: a list of the locations for each cluster
    
    
    Example:
    without a know path:
    >>>community_detection(
    >>community_detection(
            exp_ID,
            visual=True,
            probe="probe00",
            start="stimOn_times",
            end="response_times",
        )s
    with a known path "\\directory\\": 
        community_detection(
            exp_ID,
            visual=True,
            path="\\directory\\"
            probe="probe00",
            start="stimOn_times",
            end="response_times",
        )


    """
    if not bool(data):
        spikes, clusters, trials, locations = djl.loading(
            eID, probe, region_list)
    else:
        spikes, clusters, trials, locations = data

    starts, ends = section_trial(user_start, user_end, trials, feedbackType,
                                 difficulty, percentage)

    spikes_interval, clusters_interval = spp.interval_selection(
        spikes, clusters, starts, ends)

    spikes_matrix = bb.processing.bincount2D(
        spikes_interval,
        clusters_interval,
        xbin=bin,  # xlim=[0, nclusters]
    )[0]
    spikes_matrix_fixed = spp.addition_of_empty_neurons(
        spikes_matrix, clusters, clusters_interval)
    correlation_matrix_original = np.corrcoef(spikes_matrix_fixed)
    correlation_matrix = correlation_matrix_original[:, :]
    correlation_matrix[correlation_matrix < 0] = 0
    np.fill_diagonal(correlation_matrix, 0)
    neuron_graph = ig.Graph.Weighted_Adjacency(correlation_matrix.tolist(),
                                               mode="UNDIRECTED")
    neuron_graph.vs["label"] = [f"{i}" for i in range(np.max(clusters))]

    if sensitivity != 1:

        partition = la.RBConfigurationVertexPartition(
            neuron_graph, resolution_parameter=sensitivity)
        optimiser = la.Optimiser()
        optimiser.optimise_partition(partition)
    else:
        partition = la.find_partition(neuron_graph,
                                      la.ModularityVertexPartition)

    visualization(neuron_graph, partition) if visual else None
    partition_dictionary = dictionary_from_communities(partition)
    region_dict = location_dictionary(partition_dictionary, locations)
    return partition, partition_dictionary, region_dict, locations