Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.G=igraph.load("karate.gml")
membership=[
[0,1,2,3,7,11,12,13,17,19,21],
[4,5,6,10,16],
[8,9,14,15,18,20,22,23,24,25,26,27,28,29,30,31,32,33]]
cover=igraph.VertexCover(self.G, membership)
metrics=VertexCoverMetric.run_analysis(cover, weights=None)
metrics.report()
self.comm_metrics=metrics.comm_metrics
def test_from_igraph_weighted():
n = 100
m = 500
K = np.zeros((n, n))
for _ in range(m):
e = np.random.choice(n, 2, replace=False)
K[e[0], e[1]] = K[e[1], e[0]] = np.random.uniform(0, 1)
g = igraph.Graph.Weighted_Adjacency(K.tolist())
G = graphtools.from_igraph(g)
G2 = graphtools.Graph(K, precomputed="adjacency")
assert np.all(G.K == G2.K)
'Zachary'),
###########################################################################
# ER Networks
# Undirected no loop
name_object(ig.Graph.Erdos_Renyi(100, p=1./100, directed=False, loops=False),
'ER_k1_undirected_no_loops'),
name_object(ig.Graph.Erdos_Renyi(100, p=5./100, directed=False, loops=False),
'ER_k5_undirected_no_loops'),
# Directed no loop
name_object(ig.Graph.Erdos_Renyi(100, p=1./100, directed=True, loops=False),
'ER_k1_directed_no_loops'),
name_object(ig.Graph.Erdos_Renyi(100, p=5./100, directed=True, loops=False),
'ER_k5_directed_no_loops'),
# Undirected loops
name_object(ig.Graph.Erdos_Renyi(100, p=1./100, directed=False, loops=True),
'ER_k1_undirected_loops'),
name_object(ig.Graph.Erdos_Renyi(100, p=5./100, directed=False, loops=True),
'ER_k5_undirected_loops'),
# Directed loops
name_object(ig.Graph.Erdos_Renyi(100, p=1./100, directed=True, loops=True),
'ER_k1_directed_loops'),
name_object(ig.Graph.Erdos_Renyi(100, p=5./100, directed=True, loops=True),
'ER_k5_directed_loops'),
###########################################################################
# Tree
name_object(ig.Graph.Tree(100, 3, type=ig.TREE_UNDIRECTED),
'Tree_undirected'),
name_object(ig.Graph.Tree(100, 3, type=ig.TREE_OUT),
'Tree_directed_out'),
name_object(ig.Graph.Tree(100, 3, type=ig.TREE_IN),
name_object(ig.Graph.Erdos_Renyi(100, p=1./100, directed=False, loops=False),
'ER_k1_undirected_no_loops'),
name_object(ig.Graph.Erdos_Renyi(100, p=5./100, directed=False, loops=False),
'ER_k5_undirected_no_loops'),
# Directed no loop
name_object(ig.Graph.Erdos_Renyi(100, p=1./100, directed=True, loops=False),
'ER_k1_directed_no_loops'),
name_object(ig.Graph.Erdos_Renyi(100, p=5./100, directed=True, loops=False),
'ER_k5_directed_no_loops'),
# Undirected loops
name_object(ig.Graph.Erdos_Renyi(100, p=1./100, directed=False, loops=True),
'ER_k1_undirected_loops'),
name_object(ig.Graph.Erdos_Renyi(100, p=5./100, directed=False, loops=True),
'ER_k5_undirected_loops'),
# Directed loops
name_object(ig.Graph.Erdos_Renyi(100, p=1./100, directed=True, loops=True),
'ER_k1_directed_loops'),
name_object(ig.Graph.Erdos_Renyi(100, p=5./100, directed=True, loops=True),
'ER_k5_directed_loops'),
###########################################################################
# Tree
name_object(ig.Graph.Tree(100, 3, type=ig.TREE_UNDIRECTED),
'Tree_undirected'),
name_object(ig.Graph.Tree(100, 3, type=ig.TREE_OUT),
'Tree_directed_out'),
name_object(ig.Graph.Tree(100, 3, type=ig.TREE_IN),
'Tree_directed_in'),
###########################################################################
# Lattice
name_object(ig.Graph.Lattice([100], nei=3, directed=False, mutual=True, circular=True),
def test_diff_move_node_optimality(self):
G = ig.Graph.Erdos_Renyi(100, p=5./100, directed=False, loops=False);
partition = leidenalg.CPMVertexPartition(G, resolution_parameter=0.1);
while 0 < self.optimiser.move_nodes(partition, consider_comms=leidenalg.ALL_NEIGH_COMMS):
pass;
for v in G.vs:
neigh_comms = set(partition.membership[u.index] for u in v.neighbors());
for c in neigh_comms:
self.assertLessEqual(
partition.diff_move(v.index, c), 1e-10, # Allow for a small difference up to rounding error.
msg="Was able to move a node to a better community, violating node optimality.");
def test_from_igraph_invalid_attribute():
with assert_warns_message(
UserWarning, "Edge attribute invalid not found. Returning unweighted graph"
):
n = 100
m = 500
K = np.zeros((n, n))
for _ in range(m):
e = np.random.choice(n, 2, replace=False)
K[e[0], e[1]] = K[e[1], e[0]] = 1
g = igraph.Graph.Adjacency(K.tolist())
G = graphtools.from_igraph(g, attribute="invalid")
def test_from_igraph_invalid_precomputed():
with assert_warns_message(
UserWarning,
"Cannot build graph from igraph with precomputed=affinity. Use 'adjacency' instead.",
):
n = 100
m = 500
K = np.zeros((n, n))
for _ in range(m):
e = np.random.choice(n, 2, replace=False)
K[e[0], e[1]] = K[e[1], e[0]] = 1
g = igraph.Graph.Adjacency(K.tolist())
G = graphtools.from_igraph(g, attribute=None, precomputed="affinity")
def from_edges(edges, source_key='source', target_key='target', weight_key='weight', directed=True):
"""
Given a List of Dictionaries with source, target, and weight attributes, return a weighted, directed graph.
"""
raw = list(map(lambda x: [x[source_key], x[target_key], int(x[weight_key])], edges))
g = IGraph.TupleList(raw, weights=True, directed=directed)
g.vs['indegree'] = g.degree(mode="in")
g.vs['outdegree'] = g.degree(mode="out")
g.vs['label'] = g.vs['name']
if 'group' not in g.vs.attributes():
g.vs['group'] = labels_to_groups(g.vs['label'])
return g
import igraph
import sys
import scipy.sparse as sp
import numpy as np
import json
import time
sf_graph_file = '../data/network_graph.pkl'
g = igraph.Graph.Read_Pickle(sf_graph_file)
print(g.summary())
t0 = time.time()
path_collection = g.get_shortest_paths(1, weights='sec_length', output='epath')
t1 = time.time()
print(t1-t0)
sys.exit(0)
# IGRAPH D--- 83335 149318 --
# + attr: n_x (v), n_y (v), node_index (v), node_osmid (v), edge_index (e), edge_osmid (e), end_node (e), sec_length (e), speed_limit (e), start_node (e)
# node_osmid_list = g.vs['node_osmid']
# node_osmid2graphid = {node_osmid_list[i]: i for i in range(len(node_osmid_list))}
# with open('../data/node_osmid2graphid.json', 'w') as outfile:
# json.dump(node_osmid2graphid, outfile, indent=2)
# sys.exit(0)
g.g.vs['size'] = [math.sqrt(v['weighted_indegree']+1)*10 for v
in g.g.vs]
logging.debug('plot: begin')
ig.plot(g.g, target=lang+"_general.png", bbox=(0, 0, 8000, 8000),
edge_color='grey', layout='drl')
logging.debug('plot: end')
weights = g.g.es['weight']
max_weight = max(weights)
g.g.es['color'] = [(255.*e['weight']/max_weight, 0., 0.) for e
in g.g.es]
g.g.es['width'] = weights
ig.plot(g.g, target=lang+"_weighted_edges.png", bbox=(0, 0, 4000,
2400),
layout='fr', vertex_label=' ')
if options.as_table:
tablr.stop()
#tablr.printHeader()
#tablr.printData()
tablr.saveInDjangoModel()
if options.adjacency:
giant = g.g.clusters().giant()
#destAdj = "%s/%swiki-%s-adj.csv" % (os.path.split(fn)[0], lang, date)
destAdj = "%swiki-%s-adj.csv" % (lang, date)