Intervals Class

class tnetwork.utils.Intervals(initial=None)[source]

Class used to represent complex intervals

This class is used to represent periods of existence of nodes and edges. Nodes and edges can exist during not continuous periods (e.g., from time 2 to 5, and from time 7 to 8). Those intervals are represent as closed on the left and open on the right, i.e., [2,5[ and [2,8[. If we were to use closed intervals on the right, we would be confronted to ponctual overlaps (without duration), which cause troubles. Furthermore, intervals are often used to represent discrete time events. If we want to express that an edge exist during one hour, from 8a.m. to 9a.m, representing it as [8,9[ gives the following results:

  • Does the edge exist at 8a.m? -> answer YES
  • Does the edge exist at 9a.m? -> answer NO
  • Duration -> 1h

When intervals are added, overlapping ones are merged, i.e. if the current Intervals contains [0,3[ and [4,5[ and we add the interval [2,4[, The resulting Interval will be [0,5[

This class uses a sorted dictionary to maintain efficiently a proper complex interval, key=start date, value=pair(start,end)

The attribute “interv” contains the interval (a SortedDict) and can be safely manipulated

Adding and removing intervals

Intervals.__init__([initial]) Instantiate intervals
Intervals.add_interval(interval) Add the provided interval to the current interval object.
Intervals.__add__(o) Add two Intervals using + operator
Intervals.__sub__(o) Substract an interval from other using - operator

Accessing Intervals properties

Intervals.contains_t(t) Return True if the provided t is in the current Intervals
Intervals.contains(period) Is the period contained in this Interval
Intervals.__contains__(time) Defines the in operator
Intervals.periods() Return the periods as a list of pairs (start, end)
Intervals.duration() Duration of the interval
Intervals.start() First date of the Intervals
Intervals.end() Last date of the interval

Operations

Intervals.intersection(other_Intervals) Intersection with another Intervals
Intervals.union(other_Intervals) Union with another Intervals
Intervals.__eq__(other) Defines the = operator