def test_minimum_spanning_tree(): adjacency_matrix = np.array([[0, 11, 13, 12], [11, 0, 0, 14], [13, 0, 0, 10], [12, 14, 10, 0]]) g = UndirectedGraph(adjacency_matrix) t = g.minimum_spanning_tree(root_vertex=0) assert t.n_edges == 3 assert_allclose( t.adjacency_matrix.todense(), csr_matrix(([11., 12., 10.], ([0, 0, 3], [1, 3, 2])), shape=(4, 4)).todense()) assert t.get_adjacency_list() == [[1, 3], [], [], [2]] assert t.predecessors_list == [None, 0, 3, 0]
def test_minimum_spanning_tree(): adjacency_matrix = np.array([[0, 11, 13, 12], [11, 0, 0, 14], [13, 0, 0, 10], [12, 14, 10, 0]]) g = UndirectedGraph(adjacency_matrix) t = g.minimum_spanning_tree(root_vertex=0) assert t.n_edges == 3 assert_allclose(t.adjacency_matrix.todense(), csr_matrix(([11., 12., 10.], ([0, 0, 3], [1, 3, 2])), shape=(4, 4)).todense()) assert t.get_adjacency_list() == [[1, 3], [], [], [2]] assert t.predecessors_list == [None, 0, 3, 0]
def _compute_minimum_spanning_tree(shapes, root_vertex, level_str, verbose): # initialize edges and weights matrix n_vertices = shapes[0].n_points n_edges = nchoosek(n_vertices, 2) weights = np.zeros((n_vertices, n_vertices)) edges = np.empty((n_edges, 2), dtype=np.int32) # fill edges and weights e = -1 for i in range(n_vertices - 1): for j in range(i + 1, n_vertices, 1): # edge counter e += 1 # print progress if verbose: print_dynamic( '{}Computing complete graph`s weights - {}'.format( level_str, progress_bar_str(float(e + 1) / n_edges, show_bar=False))) # fill in edges edges[e, 0] = i edges[e, 1] = j # create data matrix of edge diffs_x = [s.points[i, 0] - s.points[j, 0] for s in shapes] diffs_y = [s.points[i, 1] - s.points[j, 1] for s in shapes] coords = np.array([diffs_x, diffs_y]) # compute mean m = np.mean(coords, axis=1) # compute covariance c = np.cov(coords) # get weight for im in range(len(shapes)): weights[i, j] += -np.log( multivariate_normal.pdf(coords[:, im], mean=m, cov=c)) weights[j, i] = weights[i, j] # create undirected graph complete_graph = UndirectedGraph(edges) if verbose: print_dynamic('{}Minimum spanning graph computed.\n'.format(level_str)) # compute minimum spanning graph return complete_graph.minimum_spanning_tree(weights, root_vertex)
def _compute_minimum_spanning_tree(shapes, root_vertex, level_str, verbose): # initialize edges and weights matrix n_vertices = shapes[0].n_points n_edges = nchoosek(n_vertices, 2) weights = np.zeros((n_vertices, n_vertices)) edges = np.empty((n_edges, 2), dtype=np.int32) # fill edges and weights e = -1 for i in range(n_vertices-1): for j in range(i+1, n_vertices, 1): # edge counter e += 1 # print progress if verbose: print_dynamic('{}Computing complete graph`s weights - {}'.format( level_str, progress_bar_str(float(e + 1) / n_edges, show_bar=False))) # fill in edges edges[e, 0] = i edges[e, 1] = j # create data matrix of edge diffs_x = [s.points[i, 0] - s.points[j, 0] for s in shapes] diffs_y = [s.points[i, 1] - s.points[j, 1] for s in shapes] coords = np.array([diffs_x, diffs_y]) # compute mean m = np.mean(coords, axis=1) # compute covariance c = np.cov(coords) # get weight for im in range(len(shapes)): weights[i, j] += -np.log(multivariate_normal.pdf(coords[:, im], mean=m, cov=c)) weights[j, i] = weights[i, j] # create undirected graph complete_graph = UndirectedGraph(edges) if verbose: print_dynamic('{}Minimum spanning graph computed.\n'.format(level_str)) # compute minimum spanning graph return complete_graph.minimum_spanning_tree(weights, root_vertex)
def test_minimum_spanning_tree(): adjacency_array = np.array([[3, 1], [2, 3], [0, 3], [2, 0], [0, 1]]) weights = np.array([[0, 11, 13, 12], [11, 0, 0, 14], [13, 0, 0, 10], [12, 14, 10, 0]]) g = UndirectedGraph(adjacency_array) t = g.minimum_spanning_tree(weights, root_vertex=0) assert t.n_edges == 3 assert_allclose(t.adjacency_array, np.array([[0, 1], [0, 3], [3, 2]])) assert t.adjacency_list == [[1, 3], [], [], [2]] assert_allclose( t.get_adjacency_matrix(), np.array([[False, True, False, True], [False, False, False, False], [False, False, False, False], [False, False, True, False]])) assert t.predecessors_list == [None, 0, 3, 0]
def _compute_minimum_spanning_tree(shapes, root_vertex=0, prefix='', verbose=False): # initialize weights matrix n_vertices = shapes[0].n_points weights = np.zeros((n_vertices, n_vertices)) # print progress if requested range1 = range(n_vertices - 1) if verbose: range1 = print_progress( range1, end_with_newline=False, prefix='{}Deformation graph - Computing complete graph`s ' 'weights'.format(prefix)) # compute weights for i in range1: for j in range(i + 1, n_vertices, 1): # create data matrix of edge diffs_x = [s.points[i, 0] - s.points[j, 0] for s in shapes] diffs_y = [s.points[i, 1] - s.points[j, 1] for s in shapes] coords = np.array([diffs_x, diffs_y]) # compute mean and covariance m = np.mean(coords, axis=1) c = np.cov(coords) # get weight for im in range(len(shapes)): weights[i, j] += -np.log( multivariate_normal.pdf(coords[:, im], mean=m, cov=c)) weights[j, i] = weights[i, j] # create undirected graph complete_graph = UndirectedGraph(weights) if verbose: print_dynamic('{}Deformation graph - Minimum spanning graph ' 'computed.\n'.format(prefix)) # compute minimum spanning graph return complete_graph.minimum_spanning_tree(root_vertex)
def _compute_minimum_spanning_tree(shapes, root_vertex=0, prefix='', verbose=False): # initialize weights matrix n_vertices = shapes[0].n_points weights = np.zeros((n_vertices, n_vertices)) # print progress if requested range1 = range(n_vertices-1) if verbose: range1 = print_progress( range1, end_with_newline=False, prefix='{}Deformation graph - Computing complete graph`s ' 'weights'.format(prefix)) # compute weights for i in range1: for j in range(i+1, n_vertices, 1): # create data matrix of edge diffs_x = [s.points[i, 0] - s.points[j, 0] for s in shapes] diffs_y = [s.points[i, 1] - s.points[j, 1] for s in shapes] coords = np.array([diffs_x, diffs_y]) # compute mean and covariance m = np.mean(coords, axis=1) c = np.cov(coords) # get weight for im in range(len(shapes)): weights[i, j] += -np.log(multivariate_normal.pdf(coords[:, im], mean=m, cov=c)) weights[j, i] = weights[i, j] # create undirected graph complete_graph = UndirectedGraph(weights) if verbose: print_dynamic('{}Deformation graph - Minimum spanning graph ' 'computed.\n'.format(prefix)) # compute minimum spanning graph return complete_graph.minimum_spanning_tree(root_vertex)
def test_minimum_spanning_tree(): adjacency_array = np.array([[3, 1], [2, 3], [0, 3], [2, 0], [0, 1]]) weights = np.array([[0, 11, 13, 12], [11, 0, 0, 14], [13, 0, 0, 10], [12, 14, 10, 0]]) g = UndirectedGraph(adjacency_array) t = g.minimum_spanning_tree(weights, root_vertex=0) assert t.n_edges == 3 assert_allclose(t.adjacency_array, np.array([[0, 1], [0, 3], [3, 2]])) assert t.adjacency_list == [[1, 3], [], [], [2]] assert_allclose(t.get_adjacency_matrix(), np.array([[False, True, False, True], [False, False, False, False], [False, False, False, False], [False, False, True, False]])) assert t.predecessors_list == [None, 0, 3, 0]