interval module

class mavis.interval.Interval(start, end=None, freq=1, number_type=None)[source]

Bases: object

Parameters:
  • start (int) – the start of the interval (inclusive)
  • end (int) – the end of the interval (inclusive)
  • freq (int) – the frequency or weight of the interval
__and__(other)[source]

the intersection of two intervals

Example

>>> Interval(1, 10) & Interval(5, 50)
Interval(5, 10)
>>> Interval(1, 2) & Interval(10, 11)
None
__len__()[source]

the length of the interval

Example

>>> len(Interval(1, 11))
12

Warning

only works for integer intervals

__or__(other)[source]

the union of two intervals

Example

>>> Interval(1, 10) | Interval(5, 50)
Interval(1, 50)
>>> Interval(1, 2) | Interval(10, 11)
Interval(1, 11)
__sub__(other)[source]

the difference of two intervals

Example

>>> Interval(1, 10) - Interval(5, 50)
[Interval(1, 4)]
>>> Interval(1, 2) - Interval(10, 11)
[Interval(1, 2)]
>>> Interval(1, 2) - Interval(-1, 10)
[]
__xor__(other)[source]
center

the middle of the interval

Example

>>> Interval(1, 10).center
5.5
>>> Interval(1, 11).center
6
classmethod convert_pos(mapping, pos, forward_to_reverse=None)[source]
classmethod convert_ratioed_pos(mapping, pos, forward_to_reverse=None)[source]

convert any given position given a mapping of intervals to another range

Parameters:
  • mapping (dict of Interval and Interval) – a mapping of a set of continuous intervals
  • pos (int) – a position in the first coordinate system
Returns:

the position in the alternate coordinate system given the input mapping

Return type:

int

Raises:
  • AttributeError – if the input position is outside the set of input segments
  • IndexError – if the input position cannot be converted to the output system

Example

>>> mapping = {(1, 10): (101, 110), (11, 20): (555, 564)}
>>> Interval.convert_pos(mapping, 5)
5
>>> Interval.convert_pos(mapping, 15)
559
classmethod dist(first, other)[source]

returns the minimum distance between intervals

Example

>>> Interval.dist((1, 4), (5, 7))
-1
>>> Interval.dist((5, 7), (1, 4))
1
>>> Interval.dist((5, 8), (7, 9))
0
classmethod intersection(*intervals)[source]

returns None if there is no intersection

Example

>>> Interval.intersection((1, 10), (2, 8), (7, 15))
Interval(7, 8)
>>> Interval.intersection((1, 2), (5, 9))
None
length()[source]
classmethod min_nonoverlapping(*intervals)[source]

for a list of intervals, orders them and merges any overlap to return a list of non-overlapping intervals O(nlogn)

Example

>>> Interval.min_nonoverlapping((1, 10), (7, 8), (6, 14), (17, 20))
[Interval(1, 14), Interval(17, 20)]
classmethod overlaps(first, other)[source]

checks if two intervals have any portion of their given ranges in common

Parameters:
  • first (Interval) – an interval to be compared
  • other (Interval) – an interval to be compared

Example

>>> Interval.overlaps(Interval(1, 4), Interval(5, 7))
False
>>> Interval.overlaps(Interval(1, 10), Interval(10, 11))
True
>>> Interval.overlaps((1, 10), (10, 11))
True
classmethod position_in_range(segments, pos)[source]
classmethod union(*intervals)[source]

returns the union of the set of input intervals

Example

>>> Interval.union((1, 2), (4, 6), (4, 9), (20, 21))
Interval(1, 21)