-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplex2hasse.py
227 lines (181 loc) · 8.21 KB
/
simplex2hasse.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import networkx as nx
import numpy as np
import warnings
import itertools
from tqdm import tqdm_notebook
from scipy.special import perm, factorial
import os
def simplex2hasse_uniform(data, max_order=None):
'''Returns the Hasse diagram with "unweighted" weighting scheme as a networkX graph (undirected, simple).
Input: list of frozensets
Output: networkx Graph object
'''
def _build_simplices(simplex, l):
#recursive function that calculates all possible simplices
for s in itertools.combinations(simplex, len(simplex)-1):
if len(s) > 0:
l.add((frozenset(simplex),frozenset(s)))
if len(s)>1:
_build_simplices(s,l)
return
# execute cleaning of the dataset - remove duplicate simplices
#data = list(set(data))
# initialize the Hasse graph (diagram)
g = nx.Graph()
# go through the simplices, create nodes and edges
for u in tqdm_notebook(data, 'Creating Hasse diagram'):
if None == max_order or (len(u)<max_order+1 and len(u) >= 1):
buff = set({})
_build_simplices(u, buff)
g.add_edges_from(buff)
else:
for v in itertools.combinations(u,max_order+1):
buff = set({})
_build_simplices(v, buff)
g.add_edges_from(buff)
weight = 1.
nx.set_node_attributes(g, weight,'weight')
return g
def simplex2hasse_counts(data, max_order=None):
'''Returns the Hasse diagram with "counts" weighting scheme as a networkX graph (undirected, weighted). Simplices appearing in the dataset receive the non-trivial weight that equals to the number of their appearance.
Input: list of frozensets
Output: networkx Graph object
'''
epsilon = 1.
def _build_simplices(simplex, l):
#recursive function that calculates all possible son simplices
for s in itertools.combinations(simplex, len(simplex)-1):
fs = frozenset(s)
if len(s)>0:
if fs not in weights_dict:
weights_dict[fs] = epsilon
# else:
# weights_dict[fs] += epsilon
l.add((frozenset(simplex), fs))
if len(s)>1:
_build_simplices(s,l)
return
# execute cleaning of the dataset - remove duplicate simplices
#data = list(set(data))
# initialize the Hasse graph (diagram)
g = nx.Graph()
weights_dict = {}
# go through the simplices, create nodes
for u in tqdm_notebook(data, 'Creating Hasse diagram'):
if None == max_order or (len(u) < max_order+1 and len(u) >= 1):
if u not in weights_dict:
weights_dict[u] = 1.
else:
weights_dict[u] += 1.
buff = set({})
_build_simplices(u, buff)
g.add_edges_from(buff)
else:
for v in itertools.combinations(u, max_order+1):
if frozenset(v) not in weights_dict:
weights_dict[frozenset(v)] = 1.
else:
weights_dict[frozenset(v)] += 1.
buff = set({})
_build_simplices(v, buff)
g.add_edges_from(buff)
nx.set_node_attributes(g, weights_dict, 'weight')
return g
def simplex2hasse_LOexponential(data, max_order=None):
'''Returns the Hasse diagram with "lobias" weighting scheme as a networkX graph (undirected, weighted) with cumulative appearance counts on nodes adjusted by the diagram level. Adjustment coefficient for n-simplex on level k (level of k-simplices) is (n+1)*n*..*(n-k+1)
Example: 3-simplex (tetrahedron) appearing in the data receives weight 1, adjacent 2-simplices (triangles) receive weigth 4, 1-simplices (edges) receive (4*3), 0-simplices (nodes) receive (4*3*2).
Input: list of frozensets
Output: networkx Graph object
'''
def _build_simplices(simplex, l):
#recursive function that calculates all possible son simplices
for s in itertools.combinations(simplex, len(simplex)-1):
fs = frozenset(s)
#top_simplex_order = max_order
level = top_simplex_order - len(simplex) + 1
if len(s) > 0:
if fs in weights_dict:
weights_dict[fs] += factorial(top_simplex_order)/factorial(top_simplex_order-level)
else:
weights_dict[fs] = factorial(top_simplex_order)/factorial(top_simplex_order-level)
l.add((frozenset(simplex), fs))
if len(s) > 1:
_build_simplices(s,l)
return
# execute cleaning of the dataset - remove duplicate simplices
#data = list(set(data))
# initialize the Hasse graph (diagram)
g = nx.Graph()
weights_dict = {}
# go through the simplices, create nodes
for u in tqdm_notebook(data, 'Creating Hasse diagram'):
if None == max_order or (len(u) < max_order+1 and len(u) >= 1):
if u not in weights_dict:
weights_dict[u] = 1.
else:
weights_dict[u] += 1.
buff = set({})
top_simplex_order = len(u)
_build_simplices(u, buff)
g.add_edges_from(buff)
else:
for v in itertools.combinations(u, max_order+1):
if frozenset(v) not in weights_dict:
weights_dict[frozenset(v)] = 1.
else:
weights_dict[frozenset(v)] += 1.
buff = set({})
top_simplex_order = len(v)
_build_simplices(v, buff)
g.add_edges_from(buff)
nx.set_node_attributes(g, weights_dict, 'weight')
return g
def simplex2hasse_HOexponential(data, max_order=None):
'''Returns the Hasse diagram with "hobias" weighting scheme as as a networkX graph (undirected, weighted) with cumulative appearance counts on nodes adjusted by the diagram level. Adjustment coefficient for n-simplex on level k (level of k-simplices) is 1/((n+1)*n*..*(n-k+1))
Example: 3-simplex (tetrahedron) appearing in the data receives weight 1, adjacent 2-simplices (triangles) receive weigth 1/4, 1-simplices (edges) receive 1/(4*3), 0-simplices (nodes) receive 1/(4*3*2).
Input: list of frozensets
Output: networkx Graph object
'''
def _build_simplices(simplex, l):
#recursive function that calculates all possible son simplices
for s in itertools.combinations(simplex, len(simplex)-1):
fs = frozenset(s)
#top_simplex_order = max_order
level = top_simplex_order - len(simplex) + 1
if len(s) > 0:
if fs in weights_dict:
weights_dict[fs] += factorial(top_simplex_order-level)/factorial(top_simplex_order)
else:
weights_dict[fs] = factorial(top_simplex_order-level)/factorial(top_simplex_order)
l.add((frozenset(simplex), fs))
if len(s) > 1:
_build_simplices(s,l)
return
# execute cleaning of the dataset - remove duplicate simplices
#data = list(set(data))
# initialize the Hasse graph (diagram)
g = nx.Graph()
weights_dict = {}
# go through the simplices, create nodes
for u in tqdm_notebook(data, 'Creating Hasse diagram'):
if None == max_order or (len(u) < max_order+1 and len(u) >= 1):
if u not in weights_dict:
weights_dict[u] = 1.
else:
weights_dict[u] += 1.
buff = set({})
top_simplex_order = len(u)
_build_simplices(u, buff)
g.add_edges_from(buff)
else:
for v in itertools.combinations(u, max_order+1):
if frozenset(v) not in weights_dict:
weights_dict[frozenset(v)] = 1.
else:
weights_dict[frozenset(v)] += 1.
buff = set({})
top_simplex_order = len(v)
_build_simplices(frozenset(v), buff)
g.add_edges_from(buff)
nx.set_node_attributes(g, weights_dict, 'weight')
return g