Pyntacle

This Jupyter notebook provides an example of using the Python packages gravis and Pyntacle. The .ipynb file can be found here.

References

Note: Pyntacle builds on top of igraph. It uses igraph Graph objects and the usual igraph methods are available for them.

Installation

  • With pip: pip install pyntacle

  • With conda: conda install -c conda-forge -c bfxcss pyntacle

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
General
App state
Display mode
Export
Data selection
Graph
Node label text
Edge label text
Node size
Minimum
Maximum
Edge size
Minimum
Maximum
Nodes
Visibility
Size
Scaling factor
Position
Drag behavior
Hover behavior
Node images
Visibility
Size
Scaling factor
Node labels
Visibility
Size
Scaling factor
Rotation
Angle
Edges
Visibility
Size
Scaling factor
Form
Curvature
Hover behavior
Edge labels
Visibility
Size
Scaling factor
Rotation
Angle
Layout algorithm
Simulation
Many-body force
Strength
Theta
Min
Max
Links force
Collision force
Radius
Strength
x-positioning force
Strength
y-positioning force
Strength
Centering force

Graph construction

1) Manual graph construction

[4]:
# ~ Not supported by Pyntacle, possible via igraph ~

2) Algorithmic graph construction

[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
General
App state
Display mode
Export
Data selection
Graph
Node label text
Edge label text
Node size
Minimum
Maximum
Edge size
Minimum
Maximum
Nodes
Visibility
Size
Scaling factor
Position
Drag behavior
Hover behavior
Node images
Visibility
Size
Scaling factor
Node labels
Visibility
Size
Scaling factor
Rotation
Angle
Edges
Visibility
Size
Scaling factor
Form
Curvature
Hover behavior
Edge labels
Visibility
Size
Scaling factor
Rotation
Angle
Layout algorithm
Simulation
Many-body force
Strength
Theta
Min
Max
Links force
Collision force
Radius
Strength
x-positioning force
Strength
y-positioning force
Strength
Centering force

3) Graph loading from an internal collection

[6]:
# ~ Not supported by Pyntacle, possible via igraph ~

4) Graph import and export

Import

[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

[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

1.a) Graph properties

[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

[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

[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