-
Notifications
You must be signed in to change notification settings - Fork 1
/
link_set.py
304 lines (228 loc) · 6.5 KB
/
link_set.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from __future__ import absolute_import
import copy
import abc
from .utils import ABC_to_string
# 2/3 Cross Compatibility
ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
class LinkSet(ABC):
"""LinkSet Object API Specification.
A LinkSet can be abstractly be defined as a set of links :code:`(u, v)`.
"""
def __str__(self):
return ABC_to_string(self, ['u', 'v'])
@property
@abc.abstractmethod
def weighted(self):
"""Designate if the LinkSet has weights.
Parameters
----------
None. Property.
Returns
-------
weights : Bool
True if the links are weighted.
None if the linkset is empty.
"""
pass
@property
def size(self):
"""Returns the size of the LinkSet.
Parameters
----------
None. Property.
Returns
-------
size : Real
The number of links.
"""
return sum(1 for _ in iter(self))
@property
def weighted_size(self):
"""Returns the weighted size of the LinkSet.
Parameters
----------
None. Property.
Returns
-------
size : Real
The sum of weights of links.
"""
weighted = self.weighted
if bool(self):
if weighted:
return self._weighted_size
else:
return self.size
else:
return .0
def _weighted_size(self):
return sum(k[-1] for k in iter(self))
@property
@abc.abstractmethod
def nodeset(self):
"""Return the nodeset that can be derived from the LinkSet.
Parameters
----------
None. Property.
Returns
-------
nodeset : NodeSet
Returns all the nodes that appear in links.
"""
pass
@abc.abstractmethod
def __bool__(self):
"""Implementation of the :code:`bool` casting of a LinkSet object.
Parameters
----------
None.
Returns
-------
out : Bool
Return True if an object is **both** initialized and contains information.
"""
pass
# Python2 cross-compatibility
def __nonzero__(self):
return self.__bool__()
@abc.abstractmethod
def neighbors_of(self, u=None, direction='out'):
"""Return the nodeset of a neighbors of a node.
Parameters
----------
u : Node_Id or None
direction : string={'in', 'out', 'both'}, default='both'
Returns
-------
nodestream : NodeSet or NodeCollection(NodeSet)
Return the ('in', 'out' or 'both') nodeset of neighbors of u.
If u is None, returns a NodeCollection of its node with its neighbors.
"""
pass
def degree(self, u=None, direction='out', weights=False):
"""Return the degree of a node.
Parameters
----------
u : Node_Id, default=None
direction : string={'in', 'out', 'both'}, default='both'
weights : bool, default=False
Returns
-------
nodestream : Real or NodeCollection(Real)
Return the ('in', 'out' or 'both') degree of a node of u.
If None returns a NodeCollection with the degree for each Node.
.
"""
if weights and self.weighted:
return self._degree_weighted(u, direction)
else:
return self._degree_unweighted(u, direction)
@abc.abstractmethod
def _degree_weighted(self, u, direction):
pass
@abc.abstractmethod
def _degree_unweighted(self, u, direction):
pass
@abc.abstractmethod
def __contains__(self, l):
"""Implementation of the :code:`in` operator for LinkSet.
Parameters
----------
l : tuple, len(l) == 2
l[0] : Node_Id or None
l[1] : Node_Id or None
Returns
-------
contains : Bool
Returns true if the (u, v) appears in the LinkSet.
If u is None v can match with anything and if v is None the opposite.
If both u and v is None this function should return False.
"""
pass
@abc.abstractmethod
def __and__(self, ls):
"""Implementation of the :code:`&` operator for a LinkSet object.
Parameters
----------
ls : LinkSet
Returns
-------
out : LinkSet
Returns the **intersection** of Links.
"""
pass
@abc.abstractmethod
def __or__(self, ls):
"""Implementation of the :code:`|` operator for a LinkSet object.
Parameters
----------
ls : LinkSet
Returns
-------
out : LinkSet
Returns the **union** of Links.
"""
pass
@abc.abstractmethod
def __sub__(self, ls):
"""Implementation of the :code:`-` operator for a LinkSet object.
Parameters
----------
ls : LinkSet
Returns
-------
out : LinkStream
Returns the **difference** of Links.
"""
pass
@abc.abstractmethod
def __iter__(self):
"""Implementation of the :code:`iter` function for a LinkSet object.
Parameters
----------
None
Returns
-------
out : Iterator of tuple
Each tuple is of the form (u, v)
"""
pass
@abc.abstractmethod
def issuperset(self, ls):
"""Check if a LinkSet contains another LinkSet.
Parameters
----------
ls : LinkSet
Returns
-------
issuperset_f : Bool
True if all links of ls appears in this LinkSet.
"""
pass
def copy(self, deep=True):
"""Returns a deep or shallow copy of the current LinkSet.
Parameters
----------
deep : Bool
Returns
-------
linkset_copy : LinkSet
Returns a deep or shallow copy of the current LinkSet
"""
if deep:
return copy.deepcopy(self)
else:
return copy.copy(self)
@property
def as_graph(self):
"""Generate the graph containing this LinkSet.
Parameters
----------
None. Property.
Returns
-------
graph : Graph
Generates a Graph containing the LinkSet.
"""
from stream_graph import Graph
return Graph(self.nodeset, self)