This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzigzag.py
75 lines (58 loc) · 2.02 KB
/
zigzag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
import numpy as np
import igraph as ig
import dionysus as d
def sliding_windows(g, res=0.1, overlap=0):
"""Compute subnetworks of a temporal network based on temporal
partitioning of the time range.
:param g: igraph Graph
:param res: resolution
:param overlap: overlap
:return: a list of temporal networks.
"""
times = np.array(g.es["time"])
duration = res * (times.max() - times.min())
windows = []
for i in range(int(1/res)):
edges = g.es.select(time_gt=times.min() + duration*i,
time_lt=times.min() + duration*(i+1))
windows.append(g.subgraph_edges(edges))
return windows
def max_simplicial_complex(g):
"""Return the maximal simplicial complex of a network g.
"""
return d.Filtration(g.maximal_cliques())
def find_transitions(a):
"""Find the transition times in an array of presence times.
"""
res = []
prev = False
for i, cur in enumerate(a):
if cur != prev:
res.append(i)
prev = cur
return res
def presence_times(g):
"""Compute the data required to compute zigzag persistence:
simplicial complex and transition times.
:param g: igraph Graph
:return: a tuple with the maximum simplicial complex and the
transition times of each simplex.
"""
max_simplicial_complex = d.Filtration(g.cliques())
filts = []
for t in np.sort(np.unique(g.es["time"])):
edges = g.es.select(time_eq=t)
cliques = g.subgraph_edges(edges).cliques()
filts.append(d.Filtration(cliques))
presences = [[s in filt for filt in filts] for s in max_simplicial_complex]
presences = [find_transitions(p) for p in presences]
return (max_simplicial_complex, presences)
def zigzag_network(g):
"""Compute zigzag persistence on a temporal network.
:param g: igraph Graph
:return: a list of persistence diagrams.
"""
(f, t) = presence_times(g)
_, dgms, _ = d.zigzag_homology_persistence(f, t)
return dgms