def test_absorbing_vs_pagerank(): graph = next(pg.load_datasets_graph(["graph9"])) personalization = {"A": 1, "B": 1} for _ in supported_backends(): pagerank_result = pg.PageRank(normalization='col').rank(graph, personalization) absorbing_result = pg.AbsorbingWalks(0.85, normalization='col', max_iters=1000).rank(graph, personalization) assert pg.Mabs(pagerank_result)(absorbing_result) < pg.epsilon()
def test_convergence_string_conversion(): # TODO: make convergence trackable from wrapping objects graph = next(pg.load_datasets_graph(["graph5"])) ranker = pg.PageRank() >> pg.Normalize() ranker(graph) assert str(ranker.convergence.iteration) + " iterations" in str( ranker.convergence)
def test_separate_normalization(): graph = next(pg.load_datasets_graph(["graph5"])) for _ in supported_backends(): algorithm = pg.PageRank( preserve_norm=False) + pg.SeparateNormalization(["A", "B"]) ranks = algorithm(graph, {"A": 2}) assert abs(ranks["A"] + ranks["B"] - 1) < pg.epsilon()
def test_pagerank_vs_networkx(): graph = next(pg.load_datasets_graph(["graph9"])) for _ in supported_backends(): ranker = pg.Normalize("sum", pg.PageRank(normalization='col', tol=1.E-9)) test_result = ranker(graph) test_result2 = nx.pagerank(graph, tol=1.E-9) # TODO: assert that 2.5*epsilon is indeed a valid limit assert pg.Mabs(test_result)(test_result2) < 2.5*pg.epsilon()
def test_ordinals(): graph = next(pg.load_datasets_graph(["graph5"])) for _ in supported_backends(): test_result = pg.Ordinals( pg.Ordinals(pg.Ordinals(pg.PageRank(normalization='col')))).rank( graph, {"A": 1}) # three ordinal transformations are the same as one assert test_result["A"] == 1
def test_norm_maintain(): # TODO: investigate that 2.5*epsilon is truly something to be expected graph = next(pg.load_datasets_graph(["graph5"])) for _ in supported_backends(): prior = pg.to_signal(graph, {"A": 2}) posterior = pg.MabsMaintain(pg.Normalize(pg.PageRank(), "range")).rank(prior) assert abs(pg.sum(pg.abs(posterior.np)) - 2) < 2.5 * pg.epsilon()
def test_completion(): graph = next(pg.load_datasets_graph(["graph9"])) for _ in supported_backends(): pg.PageRank().rank(graph) pg.HeatKernel().rank(graph) pg.AbsorbingWalks().rank(graph) pg.HeatKernel().rank(graph) assert True
def test_kernel_locality(): graph = next(pg.load_datasets_graph(["graph9"])) personalization = {"A": 1, "B": 1} for _ in supported_backends(): for kernel_algorithm in [pg.HeatKernel, pg.BiasedKernel]: pagerank_result = pg.Normalize("sum", pg.PageRank(max_iters=1000)).rank(graph, personalization) kernel_result = pg.Normalize("sum", kernel_algorithm(max_iters=1000)).rank(graph, personalization) assert pagerank_result['A'] < kernel_result['A'] assert pagerank_result['I'] > kernel_result['I']
def test_signal_init(): for backend in supported_backends(): with pytest.raises(Exception): pg.GraphSignal([1, 2, 3], [1, 2]) signal = pg.GraphSignal(next(pg.load_datasets_graph(["graph9"])), {"A": 1, "B": 2}) if backend != "tensorflow": del signal["A"] assert signal["A"] == 0 assert signal["B"] == 2
def test_subgraph(): graph = next(pg.load_datasets_graph(["graph9"])) signal1 = pg.to_signal(graph, {"A": 1, "B": 1, "C": 1, "D": 1, "E": 0.5}) assert "L" in signal1 signal2 = pg.Subgraph().rank(signal1) assert signal2["E"] == 0.5 assert "L" not in signal2 signal3 = signal2 >> pg.Supergraph() assert "L" in signal3
def test_correlation_compliance(): graph = next(pg.load_datasets_graph(["graph5"])) # TODO: Make spearman and pearson correlation support tensorflow alg1 = pg.PageRank(alpha=0.5) alg2 = pg.PageRank(alpha=0.99) pearson_ordinals = pg.PearsonCorrelation(pg.Ordinals(alg1)(graph))( pg.Ordinals(alg2)(graph)) spearman = pg.SpearmanCorrelation(alg1(graph))(alg2(graph)) assert pearson_ordinals == spearman
def test_stream_diff(): graph = next(pg.load_datasets_graph(["graph9"])) for _ in supported_backends(): ranks1 = pg.GenericGraphFilter( [0, 0, 1], max_iters=4, error_type="iters") | pg.to_signal( graph, {"A": 1}) ranks2 = pg.GenericGraphFilter( [1, 1, 1], tol=None) & ~pg.GenericGraphFilter( [1, 1], tol=None) | pg.to_signal(graph, {"A": 1}) assert pg.Mabs(ranks1)(ranks2) < pg.epsilon()
def test_completion(): graph = next(pg.load_datasets_graph(["graph9"])) for _ in supported_backends(): pg.PageRank().rank(graph) pg.PageRank(normalization="both").rank(graph) pg.HeatKernel().rank(graph) pg.AbsorbingWalks().rank(graph) pg.SymmetricAbsorbingRandomWalks().rank(graph) pg.HeatKernel().rank(graph) assert True
def test_transform(): import math graph = next(pg.load_datasets_graph(["graph5"])) for _ in supported_backends(): r1 = pg.Normalize(pg.PageRank(), "sum").rank(graph) r2 = pg.Transformer(pg.PageRank(), lambda x: x / pg.sum(x)).rank(graph) assert pg.Mabs(r1)(r2) < pg.epsilon() r1 = pg.Transformer(math.exp).transform(pg.PageRank()(graph)) r2 = pg.Transformer(pg.PageRank(), pg.exp).rank(graph) assert pg.Mabs(r1)(r2) < pg.epsilon()
def test_quotient(): graph = next(pg.load_datasets_graph(["graph9"])) for _ in supported_backends(): test_result = pg.PageRank(normalization='symmetric', tol=max(1.E-9, pg.epsilon()), use_quotient=True).rank(graph) norm_result = pg.PageRank(normalization='symmetric', tol=max(1.E-9, pg.epsilon()), use_quotient=pg.Normalize("sum")).rank(graph) assert pg.Mabs(test_result)(norm_result) < pg.epsilon()
def test_abstract_filter_types(): graph = next(pg.load_datasets_graph(["graph5"])) with pytest.raises(Exception): pg.GraphFilter().rank(graph) with pytest.raises(Exception): pg.RecursiveGraphFilter().rank(graph) with pytest.raises(Exception): pg.ClosedFormGraphFilter().rank(graph) with pytest.raises(Exception): pg.Tuner().rank(graph)
def test_lanczos_speedup(): graph = next(pg.load_datasets_graph(["bigraph"])) for _ in supported_backends(): for algorithm in [pg.HeatKernel]: result = pg.Normalize(algorithm(normalization='symmetric')).rank( graph, {"0": 1}) result_lanczos = pg.Normalize( algorithm(normalization='symmetric', krylov_dims=5)).rank(graph, {"0": 1}) assert pg.Mabs(result)(result_lanczos) < 0.01
def test_tautology(): graph = next(pg.load_datasets_graph(["bigraph"])) r = pg.PageRank().rank(graph) tr = pg.Tautology(pg.PageRank()).rank(graph) rt = pg.Tautology().transform(r) for u in graph: assert r[u] == rt[u] assert r[u] == tr[u] u = pg.Tautology().rank(graph) assert float(sum(u.np)) == len(graph)
def test_remove_edges(): graph = next(pg.load_datasets_graph(["graph5"])) assert graph.has_edge("A", "B") assert graph.has_edge("C", "D") pg.remove_intra_edges(graph, { "community1": ["A", "B"], "community2": ["D", "C"] }) assert graph.has_edge("B", "C") assert not graph.has_edge("A", "B") assert not graph.has_edge("C", "D")
def test_automatic_graph_casting(): graph = next(pg.load_datasets_graph(["graph9"])) for _ in supported_backends(): signal = pg.to_signal(graph, {"A": 1}) test_result1 = pg.PageRank(normalization='col').rank(signal, signal) test_result2 = pg.PageRank(normalization='col').rank(personalization=signal) assert pg.Mabs(test_result1)(test_result2) < pg.epsilon() with pytest.raises(Exception): pg.PageRank(normalization='col').rank(personalization={"A": 1}) with pytest.raises(Exception): pg.PageRank(normalization='col').rank(graph.copy(), signal)
def test_stream(): graph = next(pg.load_datasets_graph(["graph9"])) for _ in supported_backends(): ranks1 = pg.Normalize( pg.PageRank(0.85, tol=pg.epsilon(), max_iters=1000, use_quotient=False)).rank(graph, {"A": 1}) ranks2 = pg.to_signal(graph, {"A": 1}) >> pg.PageRank( 0.85, tol=pg.epsilon(), max_iters=1000) + pg.Tautology() >> pg.Normalize() assert pg.Mabs(ranks1)(ranks2) < pg.epsilon()
def test_pagerank_vs_networkx(): graph = next(pg.load_datasets_graph(["graph9"], graph_api=nx)) graph = graph.to_directed() # graph_api needed so that nx.pagerank can perform internal computations for _ in supported_backends(): ranker = pg.Normalize("sum", pg.PageRank(normalization='col')) test_result2 = nx.pagerank(graph) test_result = ranker(graph) print(test_result) print(test_result2) # TODO: assert that 2.5*epsilon is indeed a valid limit assert pg.Mabs(test_result)(test_result2) < 2.5 * pg.epsilon()
def test_sequential(): graph = next(pg.load_datasets_graph(["graph5"])) for _ in supported_backends(): prior = pg.to_signal(graph, {"A": 2}) posterior1 = pg.Normalize(pg.PageRank(), "range").rank(prior) posterior2 = pg.Normalize("range")(pg.PageRank()(prior)) posterior3 = pg.Sequential(pg.PageRank(), pg.Normalize("range")).rank(prior) assert pg.sum(pg.abs(posterior1 - posterior2)) < pg.epsilon( ) # TODO: investigate when not exactly zero assert pg.sum(pg.abs(posterior1 - posterior3)) < pg.epsilon( ) # TODO: investigate when not exactly zero
def test_filter_stream(): graph = next(pg.load_datasets_graph(["graph9"])) for _ in supported_backends(): test_result = pg.Normalize( pg.PageRank(normalization='symmetric', tol=max(1.E-9, pg.epsilon()), use_quotient=True)).rank(graph) norm_result = pg.PageRank(tol=max(1.E-9, pg.epsilon())) \ + pg.preprocessor(normalization='symmetric') \ + pg.Normalize("sum") \ >> pg.Normalize() \ | pg.to_signal(graph, {v: 1 for v in graph}) assert pg.Mabs(test_result)(norm_result) < pg.epsilon()
def test_remove_edges(): import networkx as nx graph = next(pg.load_datasets_graph(["graph5"], graph_api=nx)) # TODO: make removing edges possible for fastgraph assert graph.has_edge("A", "B") assert graph.has_edge("C", "D") pg.remove_intra_edges(graph, { "community1": ["A", "B"], "community2": ["D", "C"] }) assert graph.has_edge("B", "C") assert not graph.has_edge("A", "B") assert not graph.has_edge("C", "D")
def test_venuerank(): graph = next(pg.load_datasets_graph(["bigraph"])) for _ in supported_backends(): venuerank = pg.PageRank(alpha=0.85, max_iters=10000, converge_to_eigenvectors=True, tol=1.E-12) venuerank_result = venuerank.rank(graph) small_restart = pg.PageRank(alpha=0.99, max_iters=10000, tol=1.E-12) small_restart_result = small_restart.rank(graph) #assert venuerank.convergence.iteration < small_restart.convergence.iteration / 2 corr = pg.SpearmanCorrelation(pg.Ordinals()(venuerank_result))( pg.Ordinals()(small_restart_result)) assert corr > 0.99
def test_normalize(): import networkx as nx graph = next(pg.load_datasets_graph(["graph5"])) for _ in supported_backends(): assert float( pg.sum( pg.Normalize("range").transform( pg.to_signal(nx.Graph([("A", "B")]), [2, 2])).np)) == 4 r = pg.Normalize(pg.PageRank(), "range").rank(graph) assert pg.min(r.np) == 0 assert pg.max(r.np) == 1 r = pg.Normalize(pg.PageRank(), "sum").rank(graph) assert abs(pg.sum(r.np) - 1) < pg.epsilon() with pytest.raises(Exception): pg.Normalize(pg.PageRank(), "unknown").rank(graph)
def test_custom_runs(): graph = next(pg.load_datasets_graph(["graph9"])) for _ in supported_backends(): ranks1 = pg.Normalize( pg.PageRank(0.85, tol=pg.epsilon(), max_iters=1000, use_quotient=False)).rank(graph, {"A": 1}) ranks2 = pg.Normalize( pg.GenericGraphFilter([0.85**i * len(graph) for i in range(80)], tol=pg.epsilon())).rank(graph, {"A": 1}) ranks3 = pg.Normalize( pg.LowPassRecursiveGraphFilter([0.85 for _ in range(80)], tol=pg.epsilon())).rank( graph, {"A": 1}) assert pg.Mabs(ranks1)(ranks2) < 1.E-6 assert pg.Mabs(ranks1)(ranks3) < 1.E-6
def test_optimization_dict(): pg.load_backend("numpy") from timeit import default_timer as time graph = next(pg.load_datasets_graph(["bigraph"])) personalization = {str(i): 1 for i in range(200)} preprocessor = pg.preprocessor(assume_immutability=True) preprocessor(graph) tic = time() for _ in range(10): pg.ParameterTuner(preprocessor=preprocessor, tol=1.E-9).rank(graph, personalization) unoptimized = time()-tic optimization = dict() tic = time() for _ in range(10): pg.ParameterTuner(optimization_dict=optimization, preprocessor=preprocessor, tol=1.E-9).rank(graph, personalization) optimized = time() - tic assert len(optimization) == 20 assert unoptimized > optimized
def test_fastgraph(): for graph_api in [pg.fastgraph, nx]: graph = next(pg.load_datasets_graph(["graph5"], graph_api=graph_api)) assert graph.has_edge("A", "B") assert not graph.has_edge("A", "E") graph.add_edge("A", "E") assert graph.has_edge("A", "E") assert graph.has_edge( "E", "A") # checks that undirected is the default mode prev_count = graph.number_of_edges() graph.remove_edge("A", "E") assert graph.number_of_edges() == prev_count - 1 sparse = graph.to_scipy_sparse_array() if isinstance( graph, pg.Graph) else nx.to_scipy_sparse_matrix( graph, weight="weight", dtype=float) assert pg.sum(sparse) == 14 assert not graph.has_edge("A", "E") assert not graph.has_edge("E", "A") graph.add_edge("A", "E") assert graph.has_edge("A", "E") graph.add_edge("Y", "Z") assert graph.has_edge("Y", "Z")