def smoke_test_random_graph(self): seed = 42 G=gnp_random_graph(100,0.25,seed) G=binomial_graph(100,0.25,seed) G=erdos_renyi_graph(100,0.25,seed) G=fast_gnp_random_graph(100,0.25,seed) G=gnm_random_graph(100,20,seed) G=dense_gnm_random_graph(100,20,seed) G=watts_strogatz_graph(10,2,0.25,seed) assert_equal(len(G), 10) assert_equal(G.number_of_edges(), 10) G=connected_watts_strogatz_graph(10,2,0.1,seed) assert_equal(len(G), 10) assert_equal(G.number_of_edges(), 10) G=watts_strogatz_graph(10,4,0.25,seed) assert_equal(len(G), 10) assert_equal(G.number_of_edges(), 20) G=newman_watts_strogatz_graph(10,2,0.0,seed) assert_equal(len(G), 10) assert_equal(G.number_of_edges(), 10) G=newman_watts_strogatz_graph(10,4,0.25,seed) assert_equal(len(G), 10) assert_true(G.number_of_edges() >= 20) G=barabasi_albert_graph(100,1,seed) G=barabasi_albert_graph(100,3,seed) assert_equal(G.number_of_edges(),(97*3)) G = extended_barabasi_albert_graph(100, 1, 0, 0, seed) assert_equal(G.number_of_edges(), 99) G = extended_barabasi_albert_graph(100, 3, 0, 0, seed) assert_equal(G.number_of_edges(), 97 * 3) G = extended_barabasi_albert_graph(100, 1, 0, 0.5, seed) assert_equal(G.number_of_edges(), 99) G = extended_barabasi_albert_graph(100, 2, 0.5, 0, seed) assert_greater(G.number_of_edges(), 100 * 3) assert_less(G.number_of_edges(), 100 * 4) G=extended_barabasi_albert_graph(100, 2, 0.3, 0.3, seed) assert_greater(G.number_of_edges(), 100 * 2) assert_less(G.number_of_edges(), 100 * 4) G=powerlaw_cluster_graph(100,1,1.0,seed) G=powerlaw_cluster_graph(100,3,0.0,seed) assert_equal(G.number_of_edges(),(97*3)) G=random_regular_graph(10,20,seed) assert_raises(NetworkXError, random_regular_graph, 3, 21) constructor=[(10,20,0.8),(20,40,0.8)] G=random_shell_graph(constructor,seed) G=random_lobster(10,0.1,0.5,seed)
def smoke_test_random_graph(self): seed = 42 G=gnp_random_graph(100,0.25,seed) G=binomial_graph(100,0.25,seed) G=erdos_renyi_graph(100,0.25,seed) G=fast_gnp_random_graph(100,0.25,seed) G=gnm_random_graph(100,20,seed) G=dense_gnm_random_graph(100,20,seed) G=watts_strogatz_graph(10,2,0.25,seed) assert_equal(len(G), 10) assert_equal(G.number_of_edges(), 10) G=connected_watts_strogatz_graph(10,2,0.1,seed) assert_equal(len(G), 10) assert_equal(G.number_of_edges(), 10) G=watts_strogatz_graph(10,4,0.25,seed) assert_equal(len(G), 10) assert_equal(G.number_of_edges(), 20) G=newman_watts_strogatz_graph(10,2,0.0,seed) assert_equal(len(G), 10) assert_equal(G.number_of_edges(), 10) G=newman_watts_strogatz_graph(10,4,0.25,seed) assert_equal(len(G), 10) assert_true(G.number_of_edges() >= 20) G=barabasi_albert_graph(100,1,seed) G=barabasi_albert_graph(100,3,seed) assert_equal(G.number_of_edges(),(97*3)) G = extended_barabasi_albert_graph(100, 1, 0, 0, seed) assert_equal(G.number_of_edges(), 99) G = extended_barabasi_albert_graph(100, 3, 0, 0, seed) assert_equal(G.number_of_edges(), 97 * 3) G = extended_barabasi_albert_graph(100, 1, 0, 0.5, seed) assert_equal(G.number_of_edges(), 99) G = extended_barabasi_albert_graph(100, 2, 0.5, 0, seed) assert_greater(G.number_of_edges(), 100 * 3) assert_less(G.number_of_edges(), 100 * 4) G=extended_barabasi_albert_graph(100, 2, 0.3, 0.3, seed) assert_greater(G.number_of_edges(), 100 * 2) assert_less(G.number_of_edges(), 100 * 4) G=powerlaw_cluster_graph(100,1,1.0,seed) G=powerlaw_cluster_graph(100,3,0.0,seed) assert_equal(G.number_of_edges(),(97*3)) G=random_regular_graph(10,20,seed) assert_raises(NetworkXError, random_regular_graph, 3, 21) constructor=[(10,20,0.8),(20,40,0.8)] G=random_shell_graph(constructor,seed) G=random_lobster(10,0.1,0.5,seed)
def test_extended_barabasi_albert(self, m=2): """ Tests that the extended BA random graph generated behaves consistently. Tests the exceptions are raised as expected. The graphs generation are repeated several times to prevent lucky-shots """ seed = 42 repeats = 2 BA_model = barabasi_albert_graph(100, m, seed) BA_model_edges = BA_model.number_of_edges() while repeats: repeats -= 1 # This behaves just like BA, the number of edges must be the same G1 = extended_barabasi_albert_graph(100, m, 0, 0, seed) assert_equal(G1.size(), BA_model_edges) # More than twice more edges should have been added G1 = extended_barabasi_albert_graph(100, m, 0.8, 0, seed) assert_greater(G1.size(), BA_model_edges * 2) # Only edge rewiring, so the number of edges less than original G2 = extended_barabasi_albert_graph(100, m, 0, 0.8, seed) assert_equal(G2.size(), BA_model_edges) # Mixed scenario: less edges than G1 and more edges than G2 G3 = extended_barabasi_albert_graph(100, m, 0.3, 0.3, seed) assert_greater(G3.size(), G2.size()) assert_less(G3.size(), G1.size()) # Testing exceptions ebag = extended_barabasi_albert_graph assert_raises(NetworkXError, ebag, m, m, 0, 0) assert_raises(NetworkXError, ebag, 1, 0.5, 0, 0) assert_raises(NetworkXError, ebag, 100, 2, 0.5, 0.5)
def test_extended_barabasi_albert(self, m=2): """ Tests that the extended BA random graph generated behaves consistently. Tests the exceptions are raised as expected. The graphs generation are repeated several times to prevent lucky-shots """ seed = 42 repeats = 2 BA_model = barabasi_albert_graph(100, m, seed) BA_model_edges = BA_model.number_of_edges() while repeats: repeats -= 1 # This behaves just like BA, the number of edges must be the same G1 = extended_barabasi_albert_graph(100, m, 0, 0, seed) assert_equal(G1.size(), BA_model_edges) # More than twice more edges should have been added G1 = extended_barabasi_albert_graph(100, m, 0.8, 0, seed) assert_greater(G1.size(), BA_model_edges * 2) # Only edge rewiring, so the number of edges less than original G2 = extended_barabasi_albert_graph(100, m, 0, 0.8, seed) assert_equal(G2.size(), BA_model_edges) # Mixed scenario: less edges than G1 and more edges than G2 G3 = extended_barabasi_albert_graph(100, m, 0.3, 0.3, seed) assert_greater(G3.size(), G2.size()) assert_less(G3.size(), G1.size()) # Testing exceptions ebag = extended_barabasi_albert_graph assert_raises(NetworkXError, ebag, m, m, 0, 0) assert_raises(NetworkXError, ebag, 1, 0.5, 0, 0) assert_raises(NetworkXError, ebag, 100, 2, 0.5, 0.5)
def generate_subgraph(self, n_nodes_in_subgraph, **kwargs): """ Generate a subgraph with specified properties. Args - n_nodes_in_subgraph (int): number of nodes in each subgraph Return - G (networkx object): subgraph """ subgraph_generator = kwargs.pop('subgraph_generator', 'path') if subgraph_generator == 'cycle': G = nx.cycle_graph(n_nodes_in_subgraph) elif subgraph_generator == 'path': G = nx.path_graph(n_nodes_in_subgraph) elif subgraph_generator == 'house': G = nx.house_graph() elif subgraph_generator == 'complete': G = nx.complete_graph(n_nodes_in_subgraph) elif subgraph_generator == 'star': G = nx.star_graph(n_nodes_in_subgraph) elif subgraph_generator == 'barabasi_albert': m = kwargs.get('m', 5) G = barabasi_albert_graph(n_nodes_in_subgraph, m, seed=config.RANDOM_SEED) elif subgraph_generator == 'extended_barabasi_albert': m = kwargs.get('m', 5) p = kwargs.get('p', 0.5) q = kwargs.get('q', 0) G = extended_barabasi_albert_graph(n_nodes_in_subgraph, m, p, q, seed=config.RANDOM_SEED) elif subgraph_generator == 'duplication_divergence_graph': p = kwargs.get('p', 0.5) G = duplication_divergence_graph(n_nodes_in_subgraph, p) else: raise Exception( 'The subgraph generator you specified is not implemented.') return G
def smoke_test_random_graph(self): seed = 42 G = gnp_random_graph(100, 0.25, seed) G = gnp_random_graph(100, 0.25, seed, directed=True) G = binomial_graph(100, 0.25, seed) G = erdos_renyi_graph(100, 0.25, seed) G = fast_gnp_random_graph(100, 0.25, seed) G = fast_gnp_random_graph(100, 0.25, seed, directed=True) G = gnm_random_graph(100, 20, seed) G = gnm_random_graph(100, 20, seed, directed=True) G = dense_gnm_random_graph(100, 20, seed) G = watts_strogatz_graph(10, 2, 0.25, seed) assert len(G) == 10 assert G.number_of_edges() == 10 G = connected_watts_strogatz_graph(10, 2, 0.1, tries=10, seed=seed) assert len(G) == 10 assert G.number_of_edges() == 10 pytest.raises(NetworkXError, connected_watts_strogatz_graph, \ 10, 2, 0.1, tries=0) G = watts_strogatz_graph(10, 4, 0.25, seed) assert len(G) == 10 assert G.number_of_edges() == 20 G = newman_watts_strogatz_graph(10, 2, 0.0, seed) assert len(G) == 10 assert G.number_of_edges() == 10 G = newman_watts_strogatz_graph(10, 4, 0.25, seed) assert len(G) == 10 assert G.number_of_edges() >= 20 G = barabasi_albert_graph(100, 1, seed) G = barabasi_albert_graph(100, 3, seed) assert G.number_of_edges() == (97 * 3) G = extended_barabasi_albert_graph(100, 1, 0, 0, seed) assert G.number_of_edges() == 99 G = extended_barabasi_albert_graph(100, 3, 0, 0, seed) assert G.number_of_edges() == 97 * 3 G = extended_barabasi_albert_graph(100, 1, 0, 0.5, seed) assert G.number_of_edges() == 99 G = extended_barabasi_albert_graph(100, 2, 0.5, 0, seed) assert G.number_of_edges() > 100 * 3 assert G.number_of_edges() < 100 * 4 G = extended_barabasi_albert_graph(100, 2, 0.3, 0.3, seed) assert G.number_of_edges() > 100 * 2 assert G.number_of_edges() < 100 * 4 G = powerlaw_cluster_graph(100, 1, 1.0, seed) G = powerlaw_cluster_graph(100, 3, 0.0, seed) assert G.number_of_edges() == (97 * 3) G = random_regular_graph(10, 20, seed) pytest.raises(NetworkXError, random_regular_graph, 3, 21) pytest.raises(NetworkXError, random_regular_graph, 33, 21) constructor = [(10, 20, 0.8), (20, 40, 0.8)] G = random_shell_graph(constructor, seed) G = random_lobster(10, 0.1, 0.5, seed) # difficult to find seed that requires few tries seq = random_powerlaw_tree_sequence(10, 3, seed=14, tries=1) G = random_powerlaw_tree(10, 3, seed=14, tries=1)
def test_random_graph(self): seed = 42 G = gnp_random_graph(100, 0.25, seed) G = gnp_random_graph(100, 0.25, seed, directed=True) G = binomial_graph(100, 0.25, seed) G = erdos_renyi_graph(100, 0.25, seed) G = fast_gnp_random_graph(100, 0.25, seed) G = fast_gnp_random_graph(100, 0.25, seed, directed=True) G = gnm_random_graph(100, 20, seed) G = gnm_random_graph(100, 20, seed, directed=True) G = dense_gnm_random_graph(100, 20, seed) G = watts_strogatz_graph(10, 2, 0.25, seed) assert len(G) == 10 assert G.number_of_edges() == 10 G = connected_watts_strogatz_graph(10, 2, 0.1, tries=10, seed=seed) assert len(G) == 10 assert G.number_of_edges() == 10 pytest.raises(NetworkXError, connected_watts_strogatz_graph, 10, 2, 0.1, tries=0) G = watts_strogatz_graph(10, 4, 0.25, seed) assert len(G) == 10 assert G.number_of_edges() == 20 G = newman_watts_strogatz_graph(10, 2, 0.0, seed) assert len(G) == 10 assert G.number_of_edges() == 10 G = newman_watts_strogatz_graph(10, 4, 0.25, seed) assert len(G) == 10 assert G.number_of_edges() >= 20 G = barabasi_albert_graph(100, 1, seed) G = barabasi_albert_graph(100, 3, seed) assert G.number_of_edges() == (97 * 3) G = extended_barabasi_albert_graph(100, 1, 0, 0, seed) assert G.number_of_edges() == 99 G = extended_barabasi_albert_graph(100, 3, 0, 0, seed) assert G.number_of_edges() == 97 * 3 G = extended_barabasi_albert_graph(100, 1, 0, 0.5, seed) assert G.number_of_edges() == 99 G = extended_barabasi_albert_graph(100, 2, 0.5, 0, seed) assert G.number_of_edges() > 100 * 3 assert G.number_of_edges() < 100 * 4 G = extended_barabasi_albert_graph(100, 2, 0.3, 0.3, seed) assert G.number_of_edges() > 100 * 2 assert G.number_of_edges() < 100 * 4 G = powerlaw_cluster_graph(100, 1, 1.0, seed) G = powerlaw_cluster_graph(100, 3, 0.0, seed) assert G.number_of_edges() == (97 * 3) G = random_regular_graph(10, 20, seed) pytest.raises(NetworkXError, random_regular_graph, 3, 21) pytest.raises(NetworkXError, random_regular_graph, 33, 21) constructor = [(10, 20, 0.8), (20, 40, 0.8)] G = random_shell_graph(constructor, seed) def is_caterpillar(g): """ A tree is a caterpillar iff all nodes of degree >=3 are surrounded by at most two nodes of degree two or greater. ref: http://mathworld.wolfram.com/CaterpillarGraph.html """ deg_over_3 = [n for n in g if g.degree(n) >= 3] for n in deg_over_3: nbh_deg_over_2 = [ nbh for nbh in g.neighbors(n) if g.degree(nbh) >= 2 ] if not len(nbh_deg_over_2) <= 2: return False return True def is_lobster(g): """ A tree is a lobster if it has the property that the removal of leaf nodes leaves a caterpillar graph (Gallian 2007) ref: http://mathworld.wolfram.com/LobsterGraph.html """ non_leafs = [n for n in g if g.degree(n) > 1] return is_caterpillar(g.subgraph(non_leafs)) G = random_lobster(10, 0.1, 0.5, seed) assert max([G.degree(n) for n in G.nodes()]) > 3 assert is_lobster(G) pytest.raises(NetworkXError, random_lobster, 10, 0.1, 1, seed) pytest.raises(NetworkXError, random_lobster, 10, 1, 1, seed) pytest.raises(NetworkXError, random_lobster, 10, 1, 0.5, seed) # docstring says this should be a caterpillar G = random_lobster(10, 0.1, 0.0, seed) assert is_caterpillar(G) # difficult to find seed that requires few tries seq = random_powerlaw_tree_sequence(10, 3, seed=14, tries=1) G = random_powerlaw_tree(10, 3, seed=14, tries=1)