def _createGraph(self): dlg = PropertyViewer(self.name, self.icon, Nodes=Integer(5, 1, 100, 1), Gamma=Float(3,0,10,0.1)) nodes = [] edges = [] if dlg.exec_(): values = dlg.values() n = values['Nodes'] g = values['Gamma'] G = rnd.random_powerlaw_tree(n, g) nodes = layout.circularNodes(n, 25) for i in G.edges_iter(): edges.append(i) return nodes, edges
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_equal(len(G), 10) assert_equal(G.number_of_edges(), 10) G = connected_watts_strogatz_graph(10, 2, 0.1, tries=10, seed=seed) assert_equal(len(G), 10) assert_equal(G.number_of_edges(), 10) assert_raises(NetworkXError, connected_watts_strogatz_graph, \ 10, 2, 0.1, tries=0) 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) assert_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 main(): import os p = 0.7 delta = 1 parser = argparse.ArgumentParser() parser.add_argument('-t', '--type', choices=all_graph_types, help='graph type') parser.add_argument('-s', '--size', type=int, default=0, help="size of graph") parser.add_argument('-e', '--size_exponent', type=int, default=1, help="exponent of the size") parser.add_argument('-b', '--exponent_base', type=int, default=10, help="base of the size exponent") parser.add_argument('-n', '--n_rounds', type=int, default=100, help="number of simulated cascades") args = parser.parse_args() gtype = args.type if args.size: size = args.size output_dir = 'data/{}/{}'.format(gtype, size) else: size = args.exponent_base**args.size_exponent output_dir = 'data/{}/{}-{}'.format(gtype, args.exponent_base, args.size_exponent) if gtype == KRONECKER_HIER: g = gen_kronecker(P=P_hier, k=args.size_exponent, n_edges=2**args.size_exponent * 3) elif gtype == KRONECKER_PERI: g = gen_kronecker(P=P_peri, k=args.size_exponent, n_edges=2**args.size_exponent * 3) elif gtype == KRONECKER_RAND: g = gen_kronecker(P=P_rand, k=args.size_exponent, n_edges=2**args.size_exponent * 3) elif gtype == PL_TREE: p = 0.88 g = random_powerlaw_tree(size, tries=999999) elif gtype == B_TREE: g = nx.balanced_tree(args.exponent_base, args.size_exponent - 1) elif gtype == ER: g = extract_larges_CC(nx.fast_gnp_random_graph(size, 0.1)) elif gtype == BARABASI: g = extract_larges_CC(nx.barabasi_albert_graph(size, 5)) elif gtype == GRID: g = grid_2d(int(np.sqrt(size))) elif gtype == CLIQUE: g = nx.complete_graph(size) elif gtype == LINE: g = nx.path_graph(size) else: raise ValueError('unsupported graph type {}'.format(gtype)) g.remove_edges_from(g.selfloop_edges()) print('|V|={}, |E|={}'.format(g.number_of_nodes(), g.number_of_edges())) if gtype == GRID: mapping = {(i, j): int(np.sqrt(size)) * i + j for i, j in g.nodes_iter()} g = nx.relabel_nodes(g, mapping) else: g = nx.convert_node_labels_to_integers(g) if not os.path.exists(output_dir): os.makedirs(output_dir) print('graph type: {}'.format(gtype)) # g = add_p_and_delta(g, p, delta) output_path = '{}/graph.graphml'.format(output_dir, gtype) print('saving to {}'.format(output_path)) nx.write_graphml(g, output_path) nx.write_gpickle(g, '{}/graph.gpkl'.format(output_dir, gtype)) if False: pkl.dump( time_probas, open('{}/{}.pkl'.format(output_dir, INF_TIME_PROBA_FILE), 'wb')) pkl.dump(node2id, open('{}/{}.pkl'.format(output_dir, NODE2ID_FILE), 'wb')) pkl.dump(id2node, open('{}/{}.pkl'.format(output_dir, ID2NODE_FILE), 'wb'))
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)
def main(): import os p = 0.7 delta = 1 parser = argparse.ArgumentParser() parser.add_argument('-t', '--type', choices=all_graph_types, help='graph type') parser.add_argument('-s', '--size', type=int, default=0, help="size of graph") parser.add_argument('-e', '--size_exponent', type=int, default=1, help="exponent of the size") parser.add_argument('-b', '--exponent_base', type=int, default=10, help="base of the size exponent") parser.add_argument('-n', '--n_rounds', type=int, default=100, help="number of simulated cascades") args = parser.parse_args() gtype = args.type if args.size: size = args.size output_dir = 'data/{}/{}'.format(gtype, size) else: size = args.exponent_base ** args.size_exponent output_dir = 'data/{}/{}-{}'.format(gtype, args.exponent_base, args.size_exponent) if gtype == KRONECKER_HIER: g = gen_kronecker(P=P_hier, k=args.size_exponent, n_edges=2**args.size_exponent * 3) elif gtype == KRONECKER_PERI: g = gen_kronecker(P=P_peri, k=args.size_exponent, n_edges=2**args.size_exponent * 3) elif gtype == KRONECKER_RAND: g = gen_kronecker(P=P_rand, k=args.size_exponent, n_edges=2**args.size_exponent * 3) elif gtype == PL_TREE: p = 0.88 g = random_powerlaw_tree(size, tries=999999) elif gtype == B_TREE: g = nx.balanced_tree(args.exponent_base, args.size_exponent-1) elif gtype == ER: g = extract_larges_CC(nx.fast_gnp_random_graph(size, 0.1)) elif gtype == BARABASI: g = extract_larges_CC(nx.barabasi_albert_graph(size, 5)) elif gtype == GRID: g = grid_2d(int(np.sqrt(size))) elif gtype == CLIQUE: g = nx.complete_graph(size) elif gtype == LINE: g = nx.path_graph(size) else: raise ValueError('unsupported graph type {}'.format(gtype)) g.remove_edges_from(g.selfloop_edges()) print('|V|={}, |E|={}'.format(g.number_of_nodes(), g.number_of_edges())) if gtype == GRID: mapping = {(i, j): int(np.sqrt(size)) * i + j for i, j in g.nodes_iter()} g = nx.relabel_nodes(g, mapping) else: g = nx.convert_node_labels_to_integers(g) if not os.path.exists(output_dir): os.makedirs(output_dir) print('graph type: {}'.format(gtype)) # g = add_p_and_delta(g, p, delta) output_path = '{}/graph.graphml'.format(output_dir, gtype) print('saving to {}'.format(output_path)) nx.write_graphml(g, output_path) nx.write_gpickle(g, '{}/graph.gpkl'.format(output_dir, gtype)) if False: pkl.dump(time_probas, open('{}/{}.pkl'.format(output_dir, INF_TIME_PROBA_FILE), 'wb')) pkl.dump(node2id, open('{}/{}.pkl'.format(output_dir, NODE2ID_FILE), 'wb')) pkl.dump(id2node, open('{}/{}.pkl'.format(output_dir, ID2NODE_FILE), 'wb'))