-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.py
249 lines (200 loc) · 6.38 KB
/
graph.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from __future__ import absolute_import
import copy
from stream_graph import ABC
from .node_set_s import NodeSetS
from .link_set_df import LinkSetDF
from stream_graph.collections import NodeCollection
class Graph(object):
"""Graph abstract-object implementation.
A Graph :math:`S=(V, E)` is a collection of two elements:
- :math:`V`, a node-set
- :math:`E`, a link-set
"""
def __init__(self, nodeset=None, linkset=None, weighted=False):
if nodeset is not None and linkset is not None:
if isinstance(nodeset, ABC.NodeSet):
self.nodeset_ = nodeset
else:
# If the nodeset is not an instance of the ABC, cast it.
self.nodeset_ = NodeSetS(nodeset)
if isinstance(linkset, ABC.LinkSet):
self.linkset_ = linkset
else:
# If the linkset is not an instance of the ABC, cast it.
# `weighted` is only applied here.
self.linkset_ = LinkSetDF(linkset, weighted=weighted)
def __bool__(self):
return hasattr(self, 'nodeset_') and hasattr(self, 'linkset_') and bool(self.nodeset_) and bool(self.linkset_)
# Python2 cross-compatibility
__nonzero__ = __bool__
def __str__(self):
if bool(self):
out = [('Node-Set', str(self.nodeset_))]
out += [('Link-Set', str(self.linkset_))]
header = ['Graph']
header += [len(header[0]) * '=']
return '\n\n'.join(['\n'.join(header)] + ['\n'.join([a, len(a) * '-', b]) for a, b in out])
else:
out = ["Empty Graph"]
out = [out[0] + "\n" + len(out[0]) * '-']
if not hasattr(self, 'nodeset_'):
out += ['- Node-Set: None']
elif not bool(self.nodeset_):
out += ['- Node-Set: Empty']
if not hasattr(self, 'linkset_'):
out += ['- Link-Set: None']
elif not bool(self.linkset_):
out += ['- Link-Set: Empty']
return '\n\n '.join(out)
@property
def weighted(self):
return self.linkset_.weighted
@property
def nodeset(self):
"""Extract the nodeset.
Parameters
----------
None. Property
Returns
-------
nodeset: ABC.NodeSet
Returns a copy of the nodeset defining this graph.
"""
if hasattr(self, 'nodeset_'):
return self.nodeset_.copy()
else:
return NodeSetS()
@property
def linkset(self):
"""Extract the linkset.
Parameters
----------
None. Property
Returns
-------
linkset: ABC.LinkSet
Returns a copy of the linkset defining this graph.
"""
if hasattr(self, 'linkset_'):
return self.linkset_.copy()
else:
return LinkSetDF()
@property
def n(self):
"""Extract the number of nodes.
Parameters
----------
None. Property.
Returns
-------
n: Int
Returns the size of the nodeset defining this graph.
"""
return self.nodeset_.size
@property
def m(self):
"""Extract the number of links.
Parameters
----------
None. Property
Returns
-------
m: Int
Returns the size of the linkset defining this graph.
"""
return self.linkset_.size
@property
def wm(self):
"""Extract the weighted number of links.
Parameters
----------
None. Property
Returns
-------
m: Int
Returns the size of the linkset defining this graph.
"""
return self.linkset_.weighted_size
@property
def total_coverage(self):
"""Extract the total coverage of the graph.
Parameters
----------
None. Property.
Returns
-------
total_coverage: Real
Returns :math:`\\frac{m}{n^{2}}`.
"""
if bool(self):
return self.m / float(self.n ** 2)
else:
return 0.
@property
def weighted_total_coverage(self):
"""Extract the weighted total coverage of the graph.
Parameters
----------
None. Property.
Returns
-------
total_coverage: Real
Returns :math:`\\frac{m_{w}}{n^{2}}`.
"""
if bool(self):
return self.wm / float(self.n ** 2)
else:
return 0.
def to_networkx(self, create_using=None):
"""Convert Graph to a networkx graph.
Parameters
----------
create_using : (NetworkX graph constructor, optional (default=nx.Graph))
Graph type to create. If graph instance, then cleared before populated.
Returns
-------
graph : nx.Graph
"""
import networkx as nx
if create_using is None:
G = nx.Graph()
else:
assert isinstance(create_using, nx.Graph)
G = create_using()
G.add_nodes_from(self.nodeset_)
if self.linkset_.weighted:
G.add_weighted_edges_from(self.linkset_)
else:
G.add_edges_from(self.linkset_)
return G
def neighbor_coverage_of(self, u=None, direction='out', weights=False):
"""Extract the neighbor coverage of the graph.
Parameters
----------
u: NodeId or None
direction: 'in', 'out' or 'both', default='out'
weights: Bool
Returns
-------
total_coverage: Real or NodeCollection
If u is Real, returns :math:`\\frac{d_{direction}(u)}{n^{2}}`.
Otherwise returns the coverage of each node.
"""
if bool(self):
denom = float(self.n ** 2)
if u is None:
def fun(x, y):
return y / denom
return self.linkset_.degree(direction=direction, weights=weights).map(fun)
else:
return self.linkset_.degree(u, direction=direction, weights=weights) / denom
else:
if u is None:
return NodeCollection()
else:
return 0.
def copy(self, deep=True):
if deep:
return copy.deepcopy(self)
else:
return copy.copy(self)