def test_chain(): periodic = True # periodic = False graph = gt_gen.lattice([1, 200], periodic=periodic) if periodic: figure_title = 'line_periodic' else: figure_title = 'line_non-periodic' n = graph.num_vertices() location1 = 0.2 * n location2 = n - location1 jump = 1e-3 weight = graph.new_edge_property('double', vals=1) e1 = graph.edge(location1, location1 + 1) weight[e1] = jump e2 = graph.edge(location2 - 1, location2) weight[e2] = jump pos_x = np.arange(n) pos_y = np.zeros((n,)) v_pos = graph.new_vertex_property('vector<double>', vals=np.vstack((pos_x, pos_y)).T) v_text = graph.new_vertex_property('string') for v in graph.vertices(): v_text[v] = pos_x[graph.vertex_index[v]] palette = sns.color_palette('Set1', n_colors=2) cmap = colors.ListedColormap(palette) gt_draw.graph_draw(graph, pos=v_pos, edge_color=weight, ecmap=cmap, edge_pen_width=.5, vertex_fill_color='w', vertex_size=2, vertex_text=v_text, vertex_font_size=1, output=figure_title + '_graph.pdf') x_signal1 = np.cos(np.linspace(0, 4 * np.pi, location1)) x_signal2 = np.cos(np.linspace(0, 50 * np.pi, n - 2 * location1)) x_signal3 = np.cos(np.linspace(0, 8 * np.pi, location1)) x_signal = np.hstack([x_signal1, x_signal2, x_signal3]) palette = sns.color_palette('Set1', n_colors=3) plt.figure() markerline, stemlines, baseline = plt.stem(x_signal, markerfmt=' ') plt.setp(stemlines, color=palette[1], linewidth=1.5) plt.setp(baseline, color='k') plt.savefig(figure_title + '_signal.pdf', dpi=300) n_eigs = graph.num_vertices() - 1 tau = 200 alpha = -1e-4 factories = [spec.ConvolutionSGFT(graph, n_eigs, tau, weight=weight), spec.PageRankSGFT(graph, n_eigs, alpha, weight=weight), spec.ConvolutionSGFT(graph, n_eigs, tau, weight=None), spec.PageRankSGFT(graph, n_eigs, alpha, weight=None)] sgft.comparison.compare_spectrograms(factories, x_signal, graph, v_pos, file_name=figure_title) sgft.comparison.compare_localization(factories, location1, graph, v_pos, file_name=figure_title)
def test_main(): g = lattice((5, 5)) for n, c in [(0, 1), (10, 5), (g.num_vertices(), 1)]: for _ in range(c): obs = np.random.choice(np.arange(g.num_vertices()), g.num_vertices(), replace=False) t = min_steiner_tree(g, obs) assert is_tree(t) assert set(obs).issubset(set(map(int, t.vertices())))
def test_unweighted(): g = lattice((5, 5)) for n, c in [(10, 5), (g.num_vertices(), 1)]: # repeat `c` rounds, using `n` terminals for _ in range(c): obs = np.random.choice(np.arange(g.num_vertices()), n, replace=False) t = min_steiner_tree(g, obs) assert is_tree(t) assert set(obs).issubset(set(map(int, t.vertices())))
def g(): graph = remove_filters(lattice((10, 10))) graph.set_directed(True) edges_iter = list(graph.edges()) for e in edges_iter: graph.add_edge(e.target(), e.source()) ew = graph.new_edge_property('float') ew.a = np.random.random(graph.num_edges()) * 0.2 + 0.8 graph.edge_properties['weights'] = ew return preprocess(graph)
def load_graph_by_name(name, weighted=False, suffix=''): suffix = suffix.strip() if name == 'lattice': shape = (10, 10) g = lattice(shape) else: if weighted: path = 'data/{}/graph_weighted{}.gt'.format(name, suffix) else: path = 'data/{}/graph{}.gt'.format(name, suffix) print('load graph from {}'.format(path)) g = load_graph(path) # assert not g.is_directed() return remove_filters(g) # add shell
def test_chain(): graph = gt_gen.lattice([1, 1000], periodic=False) # gt_draw.graph_draw(graph) jump = 1e-3 weight = graph.new_edge_property('double', vals=1) e1 = graph.edge(300, 301) weight[e1] = jump e1 = graph.edge(699, 700) weight[e1] = jump# + 0.01 for e in graph.edges(): # weight[e] += min(abs(0.3 * np.random.randn()), 1) if e.source() == 300 or e.source() == 699: print e, weight[e] n_eigs = 100 alpha = -1e-10 factory_weighted = ppr.PersonalizedPageRank(graph, n_eigs, weight=weight) factory_unweighted = ppr.PersonalizedPageRank(graph, n_eigs, weight=None) spectrogram_weighted = np.zeros((graph.num_vertices(), n_eigs)) spectrogram_unweighted = np.zeros((graph.num_vertices(), n_eigs)) t = timeit.default_timer() for v in range(graph.num_vertices()): _, loadings_weighted = factory_weighted.vector([graph.vertex(v)], alpha) _, loadings_unweighted = factory_unweighted.vector([graph.vertex(v)], alpha) spectrogram_weighted[v, :] = loadings_weighted spectrogram_unweighted[v, :] = loadings_unweighted print timeit.default_timer() - t spectrogram_weighted = np.abs(spectrogram_weighted) spectrogram_unweighted = np.abs(spectrogram_unweighted) spectrogram_weighted /= np.atleast_2d(np.sum(spectrogram_weighted, axis=1)).T spectrogram_unweighted /= np.atleast_2d(np.sum(spectrogram_unweighted, axis=1)).T palette = sns.color_palette('RdBu_r', n_colors=256) cmap = colors.ListedColormap(palette, N=256) plt.figure() plt.subplot(121) plt.imshow(spectrogram_weighted, interpolation='none', cmap=cmap) plt.title('Weighted') plt.axis('tight') plt.subplot(122) plt.imshow(spectrogram_unweighted, interpolation='none', cmap=cmap) plt.title('Unweighted') plt.axis('tight')
def batonGraph(drawGraph=False): ''' A graph with NO symmetries. ''' g = generation.lattice((9, 1)) v = g.add_vertex() g.add_edge(v, g.vertex(6)) if drawGraph == True: draw.graph_draw(g, vertex_text=g.vertex_index, edge_color="black", output="batton.pdf") return g
def lattice(n1: int, n2: int): """ Build 2D lattice graph Parameters ---------- n1: int Number of nodes in dimension 1 n2: int Number of nodes in dimension 2 Returns ------- Graph Lattice graph """ return generation.lattice((n1, n2))
def test_weighted(): g = lattice((5, 5)) # assign some random weight p = g.new_edge_property('float') weights = np.random.random(g.num_edges()) p.a = (-np.log(weights)) for n, c in [(10, 5), (g.num_vertices(), 1)]: # repeat `c` rounds, using `n` terminals for _ in range(c): obs = np.random.choice(np.arange(g.num_vertices()), n, replace=False) t = min_steiner_tree(g, obs, p) # print(t) assert is_tree(t) assert set(obs).issubset(set(map(int, t.vertices())))
def test_contract_graph_by_nodes(): def get_weight_by_edges(g, weights, edges): return [weights[g.edge(u, v)] for u, v in edges] # weighted graph g = lattice((2, 2)) weights = g.new_edge_property('float') for e in g.edges(): weights[e] = int(e.target()) # contract 0 and 1 cg, new_weights = contract_graph_by_nodes(g, [0, 1], weights) assert set(extract_nodes(cg)) == set(range(3)) edges = [(0, 0), (0, 1), (0, 2), (1, 2)] assert set(extract_edges(cg)) == set(edges) assert_almost_equal(get_weight_by_edges(cg, new_weights, edges), [1, 2, 3, 3]) # contract 0, 1 and 2 cg, new_weights = contract_graph_by_nodes(g, [0, 1, 2], weights) assert set(extract_nodes(cg)) == set(range(2)) edges = [(0, 0), (0, 1)] assert set(extract_edges(cg)) == set(edges) assert_almost_equal(get_weight_by_edges(cg, new_weights, edges), [3, 6]) # contract all nodes cg, new_weights = contract_graph_by_nodes(g, [0, 1, 2, 3], weights) assert set(extract_nodes(cg)) == {0} assert set(extract_edges(cg)) == {(0, 0)} assert_almost_equal(new_weights.a, [9]) # contract just 1 cg, new_weights = contract_graph_by_nodes(g, [0], weights) assert set(extract_nodes(cg)) == set(extract_nodes(g)) assert set(extract_edges(cg)) == set(extract_edges(g)) assert_almost_equal(new_weights.a, weights.a)
def main(filename, f_seeds, b_seeds): # Load image print("Reading", filename, flush=True, end="...") img = cv3.imread(filename, cv3.IMREAD_COLOR)[:, :, ::-1] rows, cols, _ = img.shape print("Done") # Init graph print("Initializing graph", flush=True, end="...") g = gt.lattice((rows, cols)) v_r = g.new_vertex_property('int') v_g = g.new_vertex_property('int') v_b = g.new_vertex_property('int') e_w = g.new_edge_property('float') print("Done") # Make a row,col map of the indices for easy access dex = np.zeros((rows, cols), dtype='int') np.ravel(dex)[:] = range(rows * cols) # Load vertex property arrays with pixel values print("Loading pixel values", flush=True, end="...") v_r.a = np.ravel(img[:, :, 0]) v_g.a = np.ravel(img[:, :, 1]) v_b.a = np.ravel(img[:, :, 2]) print("Done") # Weight n-link edges print("Setting n-link weights", flush=True, end="...") def n_weight(s_r, s_g, s_b, t_r, t_g, t_b): return 1.0 - 3.0**-0.5 * ((v_r[s] - v_r[t])**2 + (v_g[s] - v_g[t])**2 + (v_b[s] - v_b[t])**2) / 65536.0 # Scale to 1 for e in g.edges(): s, t = e.source(), e.target() e_w[e] = 0.13 * n_weight(v_r[s], v_g[s], v_b[s], v_r[t], v_g[t], v_b[t]) print("Done") # Double up and make directed print("Converting to directed graph", flush=True, end="...") g.set_directed(True) g.add_edge_list([(e.target(), e.source(), e_w[e]) for e in g.edges()], eprops=(e_w, )) print("Done") # Make t-link edges print("Making t-link edges", flush=True, end="...") f, b = g.add_vertex(2) for n in range(rows * cols): g.add_edge(f, n) g.add_edge(n, b) print("Done") # Seeds kde = KernalDensityEstimator([img[c] for c in f_seeds], [img[c] for c in b_seeds]) # Weight t-link edges print("Setting t-link weights", flush=True, end="...") for row, col in product(range(rows), range(cols)): if (row, col) in f_seeds: e_w[g.edge(f, g.vertex(dex[row, col]))] = 1e9 e_w[g.edge(g.vertex(dex[row, col]), b)] = 0.0 elif (row, col) in b_seeds: e_w[g.edge(f, g.vertex(dex[row, col]))] = 0.0 e_w[g.edge(g.vertex(dex[row, col]), b)] = 1e9 else: (e_w[g.edge(f, g.vertex(dex[row, col]))], e_w[g.edge(g.vertex(dex[row, col]), b)]) = kde.probs(img[row, col]) print("Done") # Perform the cut print("Making the cut", flush=True, end="...") res = gt.flow.boykov_kolmogorov_max_flow(g, f, b, e_w) cut = gt.flow.min_st_cut(g, f, e_w, res) print("Done") # Display the result print("Highlighting", flush=True, end="...") highlight = np.array((0.2, 0.8, 0.8)) for row, col in product(range(rows), range(cols)): if not cut[g.vertex(dex[row, col])]: img[row, col] = 255 - highlight * (255 - img[row, col]) # img[row, col] = (0, 0, 128) print("Done") display_image(img) return g, e_w, v_r, cut
sample_steiner_trees(g, obs, method, n_samples=1, gi=gi, return_tree_nodes=return_tree_nodes) te = time.time() return te - ts def run(g, N, method, return_tree_nodes): t_sum = 0 gi = util.from_gt(g, None) for i in tqdm(range(N), total=N): obs = np.random.choice(np.arange(g.num_vertices()), 10, replace=False) t_sum += wrapper_for_sampling_procedure(g, obs, method, gi, return_tree_nodes) print('{} took {:.2f} sec on average'.format(method, t_sum / N)) # return_tree_nodes plays a big difference here # construting the GraphView is very time consuming g_dim = 100 return_tree_nodes = True g = lattice((g_dim, g_dim)) run(g, N, 'cut_naive', return_tree_nodes) run(g, N, 'cut', return_tree_nodes) run(g, N, 'loop_erased', return_tree_nodes)
import line_profiler import numpy as np from sample_pool import TreeSamplePool from random_steiner_tree.util import from_gt from graph_helpers import remove_filters from core1 import query_score, prediction_error, matching_trees_cython from core1_python import query_score as query_score_py, prediction_error as prediction_error_py, matching_trees from graph_tool.generation import lattice g = lattice((10, 10)) gv = remove_filters(g) gi = from_gt(g) pool = TreeSamplePool(gv, n_samples=20, method='cut', gi=gi, return_tree_nodes=True # using tree nodes ) n_obs = 10 obs = np.random.permutation(g.num_vertices())[:n_obs] print(obs) pool.fill(obs) q = 0 hidden_nodes = set(map(int, g.vertices())) - {q}- set(obs)
def test_circle(): size = 50 periodic = True # periodic = False graph = gt_gen.lattice([size, size], periodic=periodic) if periodic: figure_title = 'grid_periodic' else: figure_title = 'grid_non-periodic' indices = np.arange(size * size) rows = np.mod(indices, size) cols = np.floor(indices / size) v_pos = graph.new_vertex_property('vector<double>', vals=np.vstack((rows, cols)).T) radius = 10 center = (size + 1) * (size / 2) vertex_w = set([]) for i in range(-radius, radius): for j in range(-radius, radius): if i ** 2 + j ** 2 < radius ** 2: idx = j * size + i + center vertex_w.add(idx) jump = 1e-5 weight = graph.new_edge_property('double', vals=1) for idx in vertex_w: u = graph.vertex(idx) for e in u.out_edges(): if e.target() not in vertex_w: weight[e] = jump v_color = graph.new_vertex_property('vector<double>') for u in graph.vertices(): if graph.vertex_index[u] in vertex_w: v_color[u] = [1, 0, 0, 1] else: v_color[u] = [0, 0, 1, 1] vertex_w = list(vertex_w) x = np.linspace(-np.pi, np.pi, size) y = np.linspace(-np.pi, np.pi, size) xx, yy = np.meshgrid(x, y) z1 = np.sin(np.pi * xx).flatten() z2 = np.sin(2 * np.pi * yy).flatten() x_signal = z1 x_signal[vertex_w] = z2[vertex_w] palette = sns.color_palette('RdBu', n_colors=256, desat=.7) cmap = colors.ListedColormap(palette, N=256) plt.figure() plt.imshow(np.reshape(x_signal, (size, size)), interpolation='nearest', cmap=cmap) plt.savefig(figure_title + '_signal.pdf', dpi=300) n_eigs = 500 # graph.num_vertices() - 1 alpha = -1e-4 factories = [spec.ConvolutionSGFT(graph, n_eigs, tau=200, weight=weight), spec.PageRankSGFT(graph, n_eigs, alpha, weight=weight), spec.ConvolutionSGFT(graph, n_eigs, tau=5, weight=None), spec.PageRankSGFT(graph, n_eigs, alpha, weight=None)] sgft.comparison.compare_spectrograms(factories, x_signal, graph, v_pos, file_name=figure_title, show_ncomps=300) spec.show_window(factories[0], center, weight=weight, pos=v_pos, vertex_size=20, file_name=figure_title + '_w_window.png') spec.show_window(factories[1], center, weight=weight, pos=v_pos, vertex_size=20, file_name=figure_title + '_w_window_ppr.png') spec.show_window(factories[2], center, weight=None, pos=v_pos, vertex_size=20, file_name=figure_title + '_u_window.png') spec.show_window(factories[3], center, weight=None, pos=v_pos, vertex_size=20, file_name=figure_title + '_u_window_ppr.png')
def test_circle(): size = 100 graph = gt_gen.lattice([size, size], periodic=False) indices = np.arange(size * size) rows = np.mod(indices, size) cols = np.floor(indices / size) v_pos = graph.new_vertex_property('vector<double>', vals=np.vstack((rows, cols)).T) center = size * (size / 2) + size / 2 - 20 radius = 20 vertex_w = set([]) for i in range(-radius, radius): for j in range(-radius, radius): if i ** 2 + j ** 2 < radius ** 2: idx = j * size + i + center vertex_w.add(idx) idx = j * size + i + center + 35 vertex_w.add(idx) print len(vertex_w) jump = 1e-5 weight = graph.new_edge_property('double', vals=1) for idx in vertex_w: u = graph.vertex(idx) for e in u.out_edges(): if e.target() not in vertex_w: weight[e] = jump # for e in graph.edges(): # weight[e] += min(abs(0.01 * np.random.randn()), 1) v_color = graph.new_vertex_property('vector<double>') for u in graph.vertices(): if graph.vertex_index[u] in vertex_w: v_color[u] = [1, 0, 0, 1] else: v_color[u] = [0, 0, 1, 1] # gt_draw.graph_draw(graph, pos=v_pos, vertex_fill_color=v_color, # vertex_size=3) alpha = -1e-5 n_eigs = 50 factory_weighted = ppr.PersonalizedPageRank(graph, n_eigs, weight=weight) factory_unweighted = ppr.PersonalizedPageRank(graph, n_eigs, weight=None) spectrogram_weighted = np.zeros((graph.num_vertices(), n_eigs - 1)) spectrogram_unweighted = np.zeros((graph.num_vertices(), n_eigs - 1)) t = timeit.default_timer() for v in range(graph.num_vertices()): _, loadings_weighted = factory_weighted.vector([graph.vertex(v)], alpha) _, loadings_unweighted = factory_unweighted.vector([graph.vertex(v)], alpha) spectrogram_weighted[v, :] = loadings_weighted spectrogram_unweighted[v, :] = loadings_unweighted print timeit.default_timer() - t spectrogram_weighted = np.abs(spectrogram_weighted) spectrogram_unweighted = np.abs(spectrogram_unweighted) spectrogram_weighted /= np.atleast_2d(np.sum(spectrogram_weighted, axis=1)).T spectrogram_unweighted /= np.atleast_2d(np.sum(spectrogram_unweighted, axis=1)).T plt.figure() plt.subplot(121) plt.imshow(spectrogram_weighted, interpolation='none') plt.title('Weighted') plt.axis('tight') plt.subplot(122) plt.imshow(spectrogram_unweighted, interpolation='none') plt.title('Unweighted') plt.axis('tight')