Pyntacle¶
This Jupyter notebook provides an example of using the Python packages gravis and Pyntacle. The .ipynb file can be found here.
References¶
-
Tutorials (Case studies)
Note: Pyntacle builds on top of igraph. It uses igraph Graph objects and the usual igraph methods are available for them.
Installation¶
Import¶
[1]:
import os
import warnings
warnings.simplefilter('ignore') # ignore various CUDA warnings
[2]:
import igraph # for graph generation
import pyntacle
import pyntacle.algorithms.local_topology
import pyntacle.algorithms.global_topology
import pyntacle.io_stream.generator
import pyntacle.io_stream.importer
import gravis as gv
Quick start¶
Example 1¶
[3]:
def assign_properties(g):
# Pyntacle builds on top of igraph. The following calculations are provided by igraph!
# Centrality calculation
node_centralities = g.betweenness()
edge_centralities = g.edge_betweenness()
# Community detection
communities = g.community_fastgreedy().as_clustering().membership
# Node properties: Size by centrality, color by community
colors = ['red', 'blue', 'green', 'orange', 'pink', 'brown', 'yellow', 'cyan', 'magenta', 'violet']
g.vs['size'] = [5.0 + val / 10.0 for val in node_centralities]
g.vs['color'] = [colors[community_index % len(colors)] for community_index in communities]
# Edge properties: Size by centrality, color by community (within=community color, between=black)
g.es['size'] = [0.5 + val / 50.0 for val in edge_centralities]
g.es['color'] = [colors[communities[i] % len(colors)] if communities[i] == communities[j] else 'black'
for i, j in g.get_edgelist()]
# Create a graph
filepath = os.path.join('data', 'pyntacle_graph.adjm')
g = pyntacle.io_stream.importer.PyntacleImporter.AdjacencyMatrix(
file=filepath, header = True, sep = "\t")
# Assign properties
assign_properties(g)
# Plot it
gv.d3(g, zoom_factor=0.2)
Adjacency matrix from pyntacle_graph.adjm imported
[3]:
Details for selected element
Graph construction¶
API reference: pyntacle.io_stream
1) Manual graph construction¶
[4]:
# ~ Not supported by Pyntacle, possible via igraph ~
2) Algorithmic graph construction¶
API Reference: pyntacle.io_stream.generator
[5]:
generator = pyntacle.io_stream.generator.PyntacleGenerator
num_nodes = 50
# Erdos-Renyi model - arg1: number of nodes, arg2: number of edges (or rewiring probability)
g = generator.Random([num_nodes, 80])
# Barabasi-Albert model - arg1: number of nodes, arg2: average number of edges per node
g = generator.ScaleFree([num_nodes, 2])
# Watts-Strogatz model - arg1: dimension of the lattice, arg2: size of the lattice among all dimensions
# arg3: distance k between nodes, arg4: node rewiring probability p
g = generator.SmallWorld([2, 10, 1, 0.1])
# Tree topology as in Wolfram alpha docs - arg1: number of nodes, arg2: number of children per node
g = generator.Tree([num_nodes, 3])
gv.d3(g)
WARNING: the following isolates will be removed from the input graph:
6,13,18,33,35
Leaving 45 nodes out of 50
The node names will be stored in the 'isolates' graph attribute
[5]:
Details for selected element
3) Graph loading from an internal collection¶
[6]:
# ~ Not supported by Pyntacle, possible via igraph ~
4) Graph import and export¶
API reference: pyntacle.io_stream
Import¶
API reference: pyntacle.io_stream.importer
[7]:
importer = pyntacle.io_stream.importer.PyntacleImporter
filepath = os.path.join('data', 'pyntacle_graph.adjm')
g = importer.AdjacencyMatrix(file=filepath, header=True, sep='\t')
Adjacency matrix from pyntacle_graph.adjm imported
Export¶
API reference: pyntacle.io_stream.exporter
[8]:
# TODO
Basic graph inspection¶
1) Graph and its properties¶
[9]:
print(type(g)) # pyntacle uses igraph as basis
<class 'igraph.Graph'>
2) Nodes and their properties¶
[10]:
# TODO
3) Edges and their properties¶
[11]:
# TODO
Calculating graph measures and metrics¶
1) Quantitative measures¶
API reference: pyntacle.algorithms
1.a) Graph properties¶
API reference: Global topology
[12]:
gt = pyntacle.algorithms.global_topology.GlobalTopology
value = gt.average_closeness(g)
value = gt.average_clustering_coefficient(g)
value = gt.average_degree(g)
value = gt.average_eccentricity(g)
value = gt.average_radiality(g)
value = gt.average_radiality_reach(g)
value = gt.components(g)
value = gt.density(g)
value = gt.diameter(g)
value = gt.pi(g)
value = gt.radius(g)
value = gt.weighted_clustering_coefficient(g)
1.b) Node properties¶
API reference: Local topology
[13]:
lt = pyntacle.algorithms.local_topology.LocalTopology
values = lt.betweenness(g)
values = lt.closeness(g)
values = lt.clustering_coefficient(g)
values = lt.degree(g)
values = lt.eccentricity(g)
# values = lt.eigenvector_centrality(g) # fails
values = lt.pagerank(g)
values = lt.radiality(g)
values = lt.radiality_reach(g)
1.c) Group properties¶
API reference: Local topology
[14]:
node_names = g.vs['name']
used_node_names = node_names[:5]
value = lt.group_betweenness(g, used_node_names)
value = lt.group_closeness(g, used_node_names)
value = lt.group_degree(g, used_node_names)
2) Structure inference¶
[15]:
# TODO
Graph visualization¶
[16]:
# TODO