-
Notifications
You must be signed in to change notification settings - Fork 3
/
causetevent.py
533 lines (478 loc) · 17.7 KB
/
causetevent.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
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/env python
'''
Created on 15 Jul 2020
@author: Christoph Minz
@license: BSD 3-Clause
'''
from __future__ import annotations
from typing import Set, List, Iterable, Any
import math
class CausetEvent(object):
'''
Handles a single event (point) and its causal relations in a causal set.
The attribute 'Label' can be used to assign a label, but does not need to
be unique (default: None).
Instances of CausetEvent are comparable:
a == b is True if a is the same instance as b
a < b is True if a precedes b
a <= b is True if a precedes b or a is the same as b
a > b is True if a succeeds b
a >= b is True if a succeeds b or a is the same as b
a.isSpacelikeTo(b) is True if a is spacelike separated to b
'''
Label: Any
_prec: Set['CausetEvent']
_succ: Set['CausetEvent']
_coordinates: List[float]
_position: List[float]
def __init__(self, **kwargs) -> None:
'''
Initialise a CausetEvent.
Keyword parameters:
label: str
Label for the event (does not need to be unique in a causet)
past: Iterable[CausetEvent]
Set of past events (that may or may not be linked). This instance
will automatically be added to their future.
future: Iterable[CausetEvent]
Set of future events (that may or may not be linked). This instance
will automatically be added to their past.
coordinates: Iterable[float]
Coordinates if the event shall be considered as embedded in a
spacetime region.
position: Iterable[float]
Coordinate pair of the event in a Hasse diagram if the Hasse
diagram is manually defined.
'''
self.Label = kwargs.get('label')
self._prec = set()
for e in kwargs.get('past', []):
self._prec.update(e.PresentOrPast)
self._succ = set()
for e in kwargs.get('future', []):
self._succ.update(e.PresentOrFuture)
self._coordinates = list(kwargs.get('coordinates', []))
self._position = list(kwargs.get('position', []))
# Add this instance to its causal relatives:
for e in self._prec:
e._addToFuture(self)
for e in self._succ:
e._addToPast(self)
def hasBeenLinked(self) -> bool:
'''
Tests if the method link() has been called (and unlink() has not been
called after).
'''
return hasattr(self, '_lprec') and hasattr(self, '_lsucc')
def _addToPast(self, other: 'CausetEvent') -> bool:
'''
Adds an event to the past of this event.
It returns False if the event is already in the past, otherwise it adds
the event and returns True.
'''
if other in self._prec:
return False
else:
if self.hasBeenLinked() and CausetEvent.isLink(other, self):
self._lprec -= other._prec
self._lprec.add(other)
self._prec.add(other)
return True
def _addToFuture(self, other: 'CausetEvent') -> bool:
'''
Adds an event to the future of this event.
It returns False if the event is already in the future, otherwise it
adds the event and returns True.
'''
if other in self._succ:
return False
else:
if self.hasBeenLinked() and CausetEvent.isLink(self, other):
self._lsucc -= other._succ
self._lsucc.add(other)
self._succ.add(other)
return True
def _discard(self, other: 'CausetEvent') -> bool:
'''
Removes an event from the past and future of this event.
It returns True if the event was in the past or future, otherwise False.
'''
if other in self._prec:
if self.hasBeenLinked() and (other in self._lprec):
self._lprec |= {
e for e in other._prec if CausetEvent.isLink(e, self)}
self._lprec.remove(other)
self._prec.discard(other)
return True
elif other in self._succ:
if self.hasBeenLinked() and (other in self._lsucc):
self._lsucc |= {
e for e in other._succ if CausetEvent.isLink(self, e)}
self._lsucc.remove(other)
self._succ.discard(other)
return True
else:
return False
def disjoin(self) -> None:
'''
Disjoins this event from the causal set (the set of events to the past
and future).
'''
self.unlink()
for e in self.Cone:
e._discard(self)
self._prec = set()
for e in self.Cone:
e._discard(self)
self._succ = set()
def __str__(self):
if self.Label:
return f'#{self.Label}' if isinstance(self.Label, int) else \
f'#\'{self.Label}\''
else:
return '#Event'
def __repr__(self):
P: str = ', '.join([str(e) for e in self.LinkPast])
l: str = None
if self.Label:
l = str(self.Label) if isinstance(self.Label, int) else \
f'\'{self.Label}\''
try:
if P:
if l:
return f'{self.__class__.__name__}' + \
'(past={' + f'{P}' + '}, ' + \
f'label={l}, ' + \
f'coordinates={self._coordinates})'
else:
return f'{self.__class__.__name__}' + \
'(past={' + f'{P}' + '}, ' + \
f'coordinates={self._coordinates})'
elif l:
return f'{self.__class__.__name__}' + \
f'(label={l}, ' + \
f'coordinates={self._coordinates})'
else:
return f'{self.__class__.__name__}' + \
f'(coordinates={self._coordinates})'
except AttributeError:
if P:
if l:
return f'{self.__class__.__name__}' + \
'(past={' + f'{P}' + '}, ' + \
f'label={l})'
else:
return f'{self.__class__.__name__}' + \
'(past={' + f'{P}' + '})'
elif l:
return f'{self.__class__.__name__}' + \
f'(label={l})'
else:
return f'{self.__class__.__name__}()'
def __lt__(self, other):
return self in other._prec
def __le__(self, other):
return (self is other) or (self in other._prec)
def __gt__(self, other):
return other in self._prec
def __ge__(self, other):
return (self is other) or (other in self._prec)
def link(self) -> None:
'''
Computes the causal links between this event and all related events.
Only call this method if it is necessary to increase performance of
this instance, when link requests with isPastLink(...) and
isFutureLink(...) are necessary. The first call of any of these two
methods will call link() if it was not called before.
'''
self._lprec: Set[CausetEvent] = {
e for e in self._prec if CausetEvent.isLink(e, self)}
self._lsucc: Set[CausetEvent] = {
e for e in self._succ if CausetEvent.isLink(self, e)}
def unlink(self) -> None:
'''
Force the CausetEvent instance to reset its link memory.
'''
delattr(self, '_lprec')
delattr(self, '_lsucc')
@classmethod
def isLink(cls, a: 'CausetEvent', b: 'CausetEvent') -> bool:
'''
Tests if event a is linked to event b by intersecting a.Future() with
b.Past().
This method is slow, but saves memory. Instead of calling this
method many times, faster access is achieved with the call of
a.isFutureLink(b).
'''
return not (a._succ & b._prec)
def isPastLink(self, other: 'CausetEvent') -> bool:
'''
Tests if another event is linked in the past of this event.
'''
try:
return other in self._lprec
except AttributeError:
self.link()
return other in self._lprec
def isFutureLink(self, other: 'CausetEvent') -> bool:
'''
Tests if another event is linked in the future of this event.
'''
try:
return other in self._lsucc
except AttributeError:
self.link()
return other in self._lsucc
def isCausalTo(self, other: 'CausetEvent') -> bool:
'''
Tests if another event is causally related to this event.
'''
return (self <= other) or (self > other)
def isLinkedTo(self, other: 'CausetEvent') -> bool:
'''
Tests if another event is linked in the past or future of this event.
'''
return self.isPastLink(other) or self.isFutureLink(other)
def isSpacelikeTo(self, other: 'CausetEvent') -> bool:
'''
Tests if another event is spacelike separated to this event.
'''
return (other is not self) and (other not in self._prec) and \
(other not in self._succ)
@staticmethod
def LinkCountOf(eventSet: Set['CausetEvent']) -> int:
return sum([len(e.LinkPast & eventSet) for e in eventSet])
@property
def Past(self) -> Set['CausetEvent']:
'''
Returns a set of events (instances of CausetEvent) that are in the past
of this event.
'''
return self._prec
@property
def Future(self) -> Set['CausetEvent']:
'''
Returns a set of events (instances of CausetEvent) that are in the
future of this event.
'''
return self._succ
@property
def Cone(self) -> Set['CausetEvent']:
'''
Returns a set of events (instances of CausetEvent) that are in the
past, present or future of this event.
'''
return self._prec | {self} | self._succ
@property
def PresentOrPast(self) -> Set['CausetEvent']:
'''
Returns a set of events (instances of CausetEvent) that are in the past
of this event, including this event.
'''
return self._prec | {self}
@property
def PresentOrFuture(self) -> Set['CausetEvent']:
'''
Returns a set of events (instances of CausetEvent) that are in the
future of this event, including this event.
'''
return self._succ | {self}
def Spacelike(self, eventSet: Set['CausetEvent']) -> Set['CausetEvent']:
'''
Returns the subset of events (instances of CausetEvent) of `eventSet`
that are spacelike separated to this event.
'''
return eventSet - self.Cone
@property
def PastCard(self) -> int:
'''
Returns the number of past events (set cardinality).
'''
return len(self._prec)
@property
def FutureCard(self) -> int:
'''
Returns the number of future events (set cardinality).
'''
return len(self._succ)
@property
def ConeCard(self) -> int:
'''
Returns the number of past and future events (set cardinality),
including this event (present).
'''
return len(self._prec) + len(self._succ) + 1
def SpacelikeCard(self, eventSet: Set['CausetEvent']) -> int:
'''
Returns the number of events (instances of CausetEvent) of `eventSet`
that are spacelike separated to this event.
'''
if self in eventSet:
return len(eventSet - self._prec - self._succ) - 1
else:
return len(eventSet - self._prec - self._succ)
@property
def LinkPast(self) -> Set['CausetEvent']:
'''
Returns a set of events (instances of CausetEvent) that are linked and
in the past of this event.
'''
try:
return self._lprec
except AttributeError:
self.link()
return self._lprec
@property
def LinkFuture(self) -> Set['CausetEvent']:
'''
Returns a set of events (instances of CausetEvent) that are linked and
in the future of this event.
'''
try:
return self._lsucc
except AttributeError:
self.link()
return self._lsucc
@property
def LinkCone(self) -> Set['CausetEvent']:
'''
Returns a set of events (instances of CausetEvent) that are linked and
in the past or future of this event.
'''
try:
return self._lprec | self._lsucc
except AttributeError:
self.link()
return self._lprec | self._lsucc
@property
def LinkPastCard(self) -> int:
'''
Returns the number of linked past events (set cardinality).
'''
try:
return len(self._lprec)
except AttributeError:
self.link()
return len(self._lprec)
@property
def LinkFutureCard(self) -> int:
'''
Returns the number of linked future events (set cardinality).
'''
try:
return len(self._lsucc)
except AttributeError:
self.link()
return len(self._lsucc)
@property
def LinkConeCard(self) -> int:
'''
Returns the number of linked past and linked future events (set
cardinality).
'''
try:
return len(self._lprec) & len(self._lsucc)
except AttributeError:
self.link()
return len(self._lprec) & len(self._lsucc)
def copy(self) -> 'CausetEvent':
'''
Returns a shallow copy of the event without embedding.
'''
if self.Label is None:
e = CausetEvent()
elif isinstance(self.Label, int):
e = CausetEvent(label=self.Label)
else:
e = CausetEvent(label=f'{self.Label}')
e._prec = self._prec.copy()
e._succ = self._succ.copy()
if self.hasBeenLinked():
e._lprec = self._lprec.copy()
e._lsucc = self._lsucc.copy()
return e
def Rank(self, other: 'CausetEvent') -> float:
'''
Returns the rank of event other in the future of this event. If `other`
is not in the future of this event, math.inf is returned.
'''
if not self <= other:
return math.inf
elif self == other:
return 0.0
elif self.isFutureLink(other):
return 1.0
else:
a_linked = self.LinkFuture
b_linked = other.LinkPast
if a_linked & b_linked:
return 2.0
r = math.inf
for a_succ in a_linked:
for b_prec in b_linked:
r = min(r, a_succ.Rank(b_prec) + 2.0)
if r == 3.0:
return 3.0
return r
@property
def Position(self) -> List[float]:
'''
Returns the coordinates for the position in a Hasse diagram.
'''
try:
return self._position
except AttributeError:
return [0.0, 0.0]
@Position.setter
def Position(self, value: Iterable[float]) -> None:
'''
Sets the coordinates for the position in a Hasse diagram.
'''
value = list(value)
if len(value) != 2:
raise ValueError(
'The position has to be an iterable of exactly two values.')
self._position = value
@property
def isEmbedded(self) -> bool:
'''
Returns True if the event has a coordinate tuple assigned to it.
'''
return hasattr(self, '_coordinates')
def embed(self, coordinates: Iterable[float], reembed: bool = False) -> \
None:
'''
Assigns coordinates to the event. If the event already has coordinates,
an `AttributeError` is raised. To avoid this error and overwrite the
value, use reembed.
'''
if not reembed and self.isEmbedded:
raise AttributeError(f'The event {self} is already embedded. ' +
'Use the `disembed` method first or use ' +
'the `reembed` flag.')
self._coordinates = list(coordinates)
def disembed(self) -> None:
'''
Removes the embedding of the event.
'''
delattr(self, '_coordinates')
@property
def Coordinates(self) -> List[float]:
'''
Returns the embedding coordinates.
Raises an AttributeError if the event is not embedded.
'''
try:
return self._coordinates
except AttributeError:
raise AttributeError(f'The event {self} is not embedded. ' +
'Use the `embed` method first.')
@property
def CoordinatesDim(self) -> int:
'''
Returns the dimension of its coordinates if any, otherwise 0.
'''
try:
return len(self._coordinates)
except AttributeError:
return 0