-
Notifications
You must be signed in to change notification settings - Fork 1
/
temporal_node_set.py
522 lines (401 loc) · 13.3 KB
/
temporal_node_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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
from __future__ import absolute_import
import abc
import copy
from itertools import combinations
from warnings import warn
from collections import Iterable
from six import string_types
from .utils import ABC_to_string
# 2/3 Cross Compatibility
ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
class TemporalNodeSet(ABC):
"""TemporalNodeSet Object API Specification.
A TemporalNodeSet can be abstractly be defined as a set of nodes :code:`u` associated with a set of time intervals :code:`(ts, tf)`.
"""
def __str__(self):
return ABC_to_string(self, ['u'])
@property
def instantaneous(self):
"""Defines if the Time Set is instantaneous."""
return False
@property
@abc.abstractmethod
def discrete(self):
"""Designate if the TemporalNodeSet is on discrete Time.
Parameters
----------
None. Property.
Returns
-------
discrete : Bool or None
True if the time is discrete.
Returns None if empty.
"""
pass
@property
@abc.abstractmethod
def nodeset(self):
"""Return the nodeset that can be derived from this TemporalNodeSet.
Parameters
----------
None. Property.
Returns
-------
nodeset : NodeSet
Returns all the distinct nodes that appear inside the TemporalNodeSet.
"""
pass
@property
@abc.abstractmethod
def timeset(self):
"""Return the timeset that can be derived from this TemporalNodeSet.
Parameters
----------
None. Property.
Returns
-------
timeset : TimeSet
Returns all the times that nodes appear inside the TemporalNodeSet.
"""
pass
@property
@abc.abstractmethod
def size(self):
"""Returns the size of the TemporalNodeSet.
Parameters
----------
None. Property.
Returns
-------
size : Real
The size :math:`\sum_{u} |T_{u}|` of the stream_graph.
"""
pass
@property
@abc.abstractmethod
def total_common_time(self):
"""Returns the total time that each node shares with all the other nodes of the TemporalNodeSet.
Parameters
----------
None. Property.
Returns
-------
size : Real
The size :math:`\sum_{uv\in V\otimes V} |T_{u} \cap T_{v}|` of the stream_graph.
"""
pass
@property
def n(self):
"""Returns number of nodes of the TemporalNodeSet.
Parameters
----------
None. Property.
Returns
-------
n : Int
Returns the number of distinct nodes in the TemporalNodeSet.
"""
return self.nodeset.size
@property
def total_time(self):
"""Returns the size of the derived TimeSet.
Parameters
----------
None. Property.
Returns
-------
total_time : Real
The total amount of time from the union of intervals that nodes exist.
"""
return self.timeset.size
@abc.abstractmethod
def __contains__(self, u):
"""Implementation of the :code:`in` operator for LinkStream.
Parameters
----------
l : tuple, len(l) == 2
l[0] : :code:`u` or None
l[1] : :code:`t` or :code:`(ts, tf)` or None
Returns
-------
contains : Bool
Returns True if a node :code:`u` exists at :code:`t` or through **all** :code:`(ts, tf)`.
If an element is :code:`None`, :code:`in` will return True if the other elements matches.
If both elements are :code:`None` output will be None.
"""
pass
def duration_of(self, u=None):
"""Returns the duration of a node.
Parameters
----------
u : Node_Id or None
Returns
-------
duration : Real or NodeCollection(Real)
The total amount of time that a node exist.
If None, returns a NodeCollection with the durations for all nodes in the temporal_nodeset.
"""
if u is None:
from stream_graph.collections import NodeCollection
return NodeCollection({u: self.times_of(u).size for u in self.nodeset})
else:
return self.times_of(u).size
@abc.abstractmethod
def common_time(self, u=None):
"""Returns the common_time between a node and one other or all.
Parameters
----------
u : Node_Id or set(Node_Id) or None
Returns
-------
total_time : Real or NodeCollection(Real)
If :code:`u` is a Node_Id, returns :math:`\sum_{v\in V,\; v\\neq u} |T_{u} \cap T_{v}|`.
If None it returns a NodeCollection of :math:`\sum_{v\in V,\; v\\neq u} |T_{u} \cap T_{v}|` for each u.
If u is a set of Node_Id the above is restricted onto the provided Nodes.
"""
pass
@abc.abstractmethod
def common_time_pair(self, l=None):
"""Returns the common_time between a pair of nodes.
Parameters
----------
l : (Node_Id, Node_Id) or set((Node_Id, Node_Id)) or None
Returns
-------
total_time : Real or LinkCollection(Real)
If l is (Node_Id, Node_Id), return :math:`|T_{u} \cap T_{v}|`.
If None, return a LinkCollection for all (u, v) with :math:`|T_{u} \cap T_{v}|`.
If u is a set of Node_Id the above is restricted onto the provided Node airs.
"""
pass
@abc.abstractmethod
def nodes_at(self, t=None):
"""Return the nodes at a certain time.
Parameters
----------
t : Real or None
Returns
-------
nodes : NodeSet or TimeGenerator(NodeSet)
Active nodes at time t.
If None returns an TimeGenerator of tuples containing a timestamp and a NodeSet.
"""
pass
@abc.abstractmethod
def times_of(self, u=None):
"""Returns TimeSet that a nodes appears in the TemporalNodeSet.
Parameters
----------
u : Node_Id or None
Returns
-------
timeset : TimeSet or NodeCollection(TimeSet)
Returns the times that node :code:`u` exists.
Return a NodeCollection of the TimeSet for each node u.
"""
pass
def n_at(self, t=None):
"""Returns number of nodes of the TemporalNodeSet at a certain time.
Parameters
----------
t : Real
Returns
-------
n : Int or TimeCollection(Int)
Returns the number of active nodes at time :code:`t`.
If None returns an iterator of tuples containing a timestamp and an Int.
"""
if t is None:
from stream_graph.collections import TimeCollection
time_nodes = self.nodes_at()
return TimeCollection([(ts, ns.size) for (ts, ns) in time_nodes], time_nodes.instants)
else:
return self.nodes_at(t).size
def _common_time_pair__list_input(self, l):
return (isinstance(l, Iterable) and
not (isinstance(l, tuple) and
len(l) == 2 and
any(not isinstance(a, Iterable) or isinstance(a, string_types) for a in l)))
def _common_time__list_input(self, u):
return isinstance(u, Iterable) and not isinstance(u, string_types)
@abc.abstractmethod
def issuperset(self, ns):
"""Check if a temporal_nodeset contains another temporal_nodeset.
Parameters
----------
ns : TemporalNodeSet
Returns
-------
issuperset_f : Bool
True if all nodes of ns appear in same or lesser intervals.
"""
pass
@abc.abstractmethod
def __iter__(self):
"""Implementation of the :code:`iter` function for a TemporalNodeSet object.
Parameters
----------
None
Returns
-------
out : Iterator of tuple
Each tuple is of the form :code:`(u, ts, tf)`.
"""
pass
@abc.abstractmethod
def __bool__(self):
"""Implementation of the :code:`bool` casting of a TemporalNodeSet 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 __and__(self, ns):
"""Implementation of the :code:`&` operator for a TemporalNodeSet object.
Parameters
----------
ns : TemporalNodeSet
Returns
-------
out : TemporalNodeSet
Returns the **intersection** of Nodes at Intervals.
"""
pass
@abc.abstractmethod
def __sub__(self, ns):
"""Implementation of the :code:`-` operator for a TemporalNodeSet object.
Parameters
----------
ns : TemporalNodeSet
Returns
-------
out : TemporalNodeSet
Returns the **difference** of Nodes at Intervals.
"""
pass
@abc.abstractmethod
def __or__(self, ns):
"""Implementation of the :code:`|` operator for a TemporalNodeSet object.
Parameters
----------
ns : TemporalNodeSet
Returns
-------
out : TemporalNodeSet
Returns the **union** of Nodes at Intervals.
"""
pass
def copy(self, deep=True):
"""Returns a deep or shallow copy of the current TemporalNodeSet.
Parameters
----------
deep : Bool
Returns
-------
temporal_nodeset_copy : TemporalNodeSet
Returns a deep or shallow copy of the current TemporalNodeSet.
"""
if deep:
return copy.deepcopy(self)
else:
return copy.copy(self)
def discretize(self, bins=None, bin_size=None):
"""Returns a discrete version of the current TemporalNodeSet.
Parameters
----------
bins : Iterable or None.
If None, step should be provided.
If Iterable it should contain n+1 elements that declare the start and the end of all (continuous) bins.
bin_size : Int or datetime
If bins is provided this argument is ommited.
Else declare the size of each bin.
Returns
-------
timeset_discrete : TimeSet
Returns a discrete version of the TimeSet.
bins : list
A list of the created bins.
"""
if not self.discrete:
return self._to_discrete(bins, bin_size)
else:
warn(str(self.__class__) + ' is already discrete')
return self, bins
@abc.abstractmethod
def _to_discrete(self, bins, bin_size):
pass
class ITemporalNodeSet(TemporalNodeSet):
"""ITemporalNodeSet Object API Specification.
A Instantaneous Temporal NodeSet can be abstractly be defined as a set of nodes :code:`u` associated with a time-stamp :code:`ts`.
"""
@property
def instantaneous(self):
"""Defines if the Time Set is instantaneous."""
return True
@property
def total_common_time(self):
if self.discrete:
return self._total_common_time_discrete
else:
return 0
@property
@abc.abstractmethod
def _total_common_time_discrete(self):
pass
@property
def size(self):
if self.discrete:
return self.number_of_instants
else:
return 0
def duration_of(self, u=None):
if self.discrete:
return self._duration_of_discrete(u)
else:
if u is None:
from stream_graph.collections import NodeCollection
return NodeCollection({u: 0 for u in self.nodeset})
return 0
@abc.abstractmethod
def _duration_of_discrete(self, u=None):
pass
def common_time(self, u=None):
if self.discrete:
return self._common_time_discrete(u)
else:
if u is None:
from stream_graph.collections import NodeCollection
return NodeCollection({u: 0 for u in self.nodeset})
elif self._common_time__list_input(u):
from stream_graph.collections import NodeCollection
return NodeCollection({v: 0 for v in u})
return 0
@abc.abstractmethod
def _common_time_discrete(self, u=None):
pass
def common_time_pair(self, l=None):
if self.discrete:
return self._common_time_pair_discrete(l)
else:
if l is None:
from stream_graph.collections import NodeCollection
return NodeCollection({l: 0 for l in combinations(self.nodeset, 2)})
if self._common_time_pair__list_input(l):
from stream_graph.collections import NodeCollection
return NodeCollection({l: 0 for u in l})
return 0
@abc.abstractmethod
def _common_time_pair_discrete(self, l=None):
pass
@property
@abc.abstractmethod
def number_of_instants(self):
pass