예제 #1
0
 def test_threshold_adjacency_matrix(self):
     adj_matrix = self.data_5d[0, 0, 0].squeeze()
     mask, real_cost = util.threshold_adjacency_matrix(adj_matrix, 0)
     npt.assert_equal(mask.sum(), 0)
     npt.assert_equal(real_cost, 0)
     mask, real_cost = util.threshold_adjacency_matrix(adj_matrix, 0.9)
     npt.assert_equal(mask.sum(), 1800)
     npt.assert_equal(real_cost, 0.9)
예제 #2
0
 def test_threshold_adjacency_matrix(self):
     adj_matrix = self.data_5d[0, 0, 0].squeeze()
     mask, real_cost = util.threshold_adjacency_matrix(adj_matrix, 0)
     npt.assert_equal(mask.sum(), 0)
     npt.assert_equal(real_cost, 0)
     mask, real_cost = util.threshold_adjacency_matrix(adj_matrix, .9)
     npt.assert_equal(mask.sum(), 1800)
     npt.assert_equal(real_cost, 0.9)
예제 #3
0
 def test_find_true_cost(self):
     adj_matrix = self.data_5d[0, 0, 0].squeeze()
     mask, real_cost = util.threshold_adjacency_matrix(adj_matrix, 0.2)
     true_cost = util.find_true_cost(mask)
     npt.assert_equal(real_cost, true_cost)
     ## test on rounded array
     adj_matrix = self.data_5d[0, 0, 0].squeeze().round(decimals=1)
     mask, expected_cost = util.threshold_adjacency_matrix(adj_matrix, 0.2)
     true_cost = util.find_true_cost(mask)
     ## the cost of the thresholded matrix will be less than expected
     npt.assert_equal(real_cost > true_cost, True)
예제 #4
0
 def test_find_true_cost(self):
     adj_matrix = self.data_5d[0, 0, 0].squeeze()
     mask, real_cost = util.threshold_adjacency_matrix(adj_matrix, 0.2)
     true_cost = util.find_true_cost(mask)
     npt.assert_equal(real_cost, true_cost)
     ## test on rounded array
     adj_matrix = self.data_5d[0, 0, 0].squeeze().round(decimals=1)
     mask, expected_cost = util.threshold_adjacency_matrix(adj_matrix, 0.2)
     true_cost = util.find_true_cost(mask)
     ## the cost of the thresholded matrix will be less than expected
     npt.assert_equal(real_cost > true_cost, True)
예제 #5
0
def calc_sa_modularity(mat, cost):
    """ Use brainx to 
    generate binary matrix theshold based on cost
    use simulated annealing to find communities
    
    Parameters
    ----------
    mat : numpy matrix
        symmetric adjacency matrix, should not contain nan
    cost: float
        cost used to binarize adjacency matrix
        
    Returns
    -------
    part : GraphPartition
        part.index provides access to found communities
    true_cost : float
        the actual cost associated with thresholded adj matrix
    """

    mask, real_cost = util.threshold_adjacency_matrix(mat, cost)
    true_cost = util.find_true_cost(mask)
    graph = nx.from_numpy_matrix(mask)
    part, mod = modularity.simulated_annealing(graph)
    return part, true_cost
def make_graph(matrix,cost=0.15):
    mask, real_cost = util.threshold_adjacency_matrix(matrix, cost)
    # true_cost = util.find_true_cost(mask)
    graph = nx.from_numpy_matrix(mask)
    return graph