<jupyter_start><jupyter_text>igraph basics<jupyter_code>import igraph as ig
# Gnerating a random network
g_random = ig.Graph.Erdos_Renyi(n=15, p=0.2, directed=False, loops=False)
print(g_random)
# Nodes
[node.index for node in g_random.vs]
# Edges
[(edge.source, edge.target) for edge in g_random.es]
# or simply
g_random.get_edgelist()
# Calculate degree of nodes
g_random.degree()
# Setting node attributes
g_random.vs["double degree"] = [2*d for d in g_random.degree()]
print(g_random)
# Works with numpy arrays as well
import numpy as np
g_random.vs["random number"] = np.random.random(g_random.vcount())
# Setting an random weight attribute to edges
g_random.es["weight"] = 10*np.random.random(g_random.ecount())
print(g_random)
# Saving as gml
g_random.write_gml("random_network.gml")
# Loading as gml
g_random_loaded = ig.Graph.Read_GML("random_network.gml")
print(g_random_loaded)
# Saving xnet (useful for the Helios-Web demo)
import xnetwork as xn
xn.igraph2xnet(g_random,"random_network.xnet")
# Loading xnet (useful for the Helios-Web demo)
import xnetwork as xn
g_random_from_xnet = xn.xnet2igraph("random_network.xnet")
print(g_random_from_xnet)<jupyter_output>IGRAPH U-W- 15 15 --
+ attr: double degree (v), random number (v), weight (e)
+ edges:
1--5 4--6 2--7 5--9 3--10 4--10 5--11 0--12 8--12 9--12 11--12 2--14 4--14
5--14 13--14
{'fraction_non_alphanumeric': 0.1334321719792439, 'fraction_numerical': 0.04151223128243143, 'mean_word_length': 5.136363636363637, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '151307', 'license': 'MIT', 'n_tokens_mistral': 558, 'n_tokens_neox': 515, 'n_words': 134, 'path': 'Activities/01-iGraph-Basics/igraph-basics.ipynb', 'repo_name': 'filipinascimento/workshop-netviz', 'size': '1349'}