intervals

class Interval(start_offset: float = -inf, stop_offset: float = inf)

Bases: object

An interval (typically of time).

>>> from supriya.utils import Interval
>>> Interval(0, 10)
Interval(start_offset=0.0, stop_offset=10.0)
__and__(interval: Interval)

Logical AND of two intervals:

>>> from supriya.utils import Interval
>>> interval_1 = Interval(0, 10)
>>> interval_2 = Interval(5, 12)
>>> interval_3 = Interval(-2, 2)
>>> interval_4 = Interval(10, 20)
>>> interval_1 & interval_2
IntervalTree(
    intervals=[
        Interval(start_offset=5.0, stop_offset=10.0),
    ],
)
>>> interval_1 & interval_3
IntervalTree(
    intervals=[
        Interval(start_offset=0.0, stop_offset=2.0),
    ],
)
>>> interval_1 & interval_4
IntervalTree(intervals=[])
>>> interval_2 & interval_3
IntervalTree(intervals=[])
>>> interval_2 & interval_4
IntervalTree(
    intervals=[
        Interval(start_offset=10.0, stop_offset=12.0),
    ],
)
>>> interval_3 & interval_4
IntervalTree(intervals=[])
__ge__(expr: Interval | float)

Return self>=value.

__gt__(expr: Interval | float)

Return self>value.

__le__(expr: Interval | float)

Return self<=value.

__lt__(expr: Interval | float)

Return self<value.

__or__(interval: Interval)

Logical OR of two intervals:

>>> from supriya.utils import Interval
>>> interval_1 = Interval(0, 10)
>>> interval_2 = Interval(5, 12)
>>> interval_3 = Interval(-2, 2)
>>> interval_4 = Interval(10, 20)
>>> interval_1 | interval_2
IntervalTree(
    intervals=[
        Interval(start_offset=0.0, stop_offset=12.0),
    ],
)
>>> interval_1 | interval_3
IntervalTree(
    intervals=[
        Interval(start_offset=-2.0, stop_offset=10.0),
    ],
)
>>> interval_1 | interval_4
IntervalTree(
    intervals=[
        Interval(start_offset=0.0, stop_offset=20.0),
    ],
)
>>> interval_2 | interval_3
IntervalTree(
    intervals=[
        Interval(start_offset=-2.0, stop_offset=2.0),
        Interval(start_offset=5.0, stop_offset=12.0),
    ],
)
>>> interval_2 | interval_4
IntervalTree(
    intervals=[
        Interval(start_offset=5.0, stop_offset=20.0),
    ],
)
>>> interval_3 | interval_4
IntervalTree(
    intervals=[
        Interval(start_offset=-2.0, stop_offset=2.0),
        Interval(start_offset=10.0, stop_offset=20.0),
    ],
)
__post_init__()
__sub__(interval: Interval)

Subtract interval from interval:

>>> from supriya.utils import Interval
>>> interval_1 = Interval(0, 10)
>>> interval_2 = Interval(5, 12)
>>> interval_3 = Interval(8, 11)
>>> interval_4 = Interval(10, 20)
>>> interval_1 - interval_1
IntervalTree(intervals=[])
>>> interval_1 - interval_2
IntervalTree(
    intervals=[
        Interval(start_offset=0.0, stop_offset=5.0),
    ],
)
>>> interval_1 - interval_3
IntervalTree(
    intervals=[
        Interval(start_offset=0.0, stop_offset=8.0),
    ],
)
>>> interval_1 - interval_4
IntervalTree(
    intervals=[
        Interval(start_offset=0.0, stop_offset=10.0),
    ],
)
>>> interval_2 - interval_1
IntervalTree(
    intervals=[
        Interval(start_offset=10.0, stop_offset=12.0),
    ],
)
>>> interval_2 - interval_2
IntervalTree(intervals=[])
>>> interval_2 - interval_3
IntervalTree(
    intervals=[
        Interval(start_offset=5.0, stop_offset=8.0),
        Interval(start_offset=11.0, stop_offset=12.0),
    ],
)
>>> interval_2 - interval_4
IntervalTree(
    intervals=[
        Interval(start_offset=5.0, stop_offset=10.0),
    ],
)
>>> interval_3 - interval_1
IntervalTree(
    intervals=[
        Interval(start_offset=10.0, stop_offset=11.0),
    ],
)
>>> interval_3 - interval_2
IntervalTree(intervals=[])
>>> interval_3 - interval_3
IntervalTree(intervals=[])
>>> interval_3 - interval_4
IntervalTree(
    intervals=[
        Interval(start_offset=8.0, stop_offset=10.0),
    ],
)
>>> interval_4 - interval_1
IntervalTree(
    intervals=[
        Interval(start_offset=10.0, stop_offset=20.0),
    ],
)
>>> interval_4 - interval_2
IntervalTree(
    intervals=[
        Interval(start_offset=12.0, stop_offset=20.0),
    ],
)
>>> interval_4 - interval_3
IntervalTree(
    intervals=[
        Interval(start_offset=11.0, stop_offset=20.0),
    ],
)
>>> interval_4 - interval_4
IntervalTree(intervals=[])
__xor__(interval: Interval)

Logical XOR of two intervals:

>>> from supriya.utils import Interval
>>> interval_1 = Interval(0, 10)
>>> interval_2 = Interval(5, 12)
>>> interval_3 = Interval(-2, 2)
>>> interval_4 = Interval(10, 20)
>>> interval_1 ^ interval_2
IntervalTree(
    intervals=[
        Interval(start_offset=0.0, stop_offset=5.0),
        Interval(start_offset=10.0, stop_offset=12.0),
    ],
)
>>> interval_1 ^ interval_3
IntervalTree(
    intervals=[
        Interval(start_offset=-2.0, stop_offset=0.0),
        Interval(start_offset=2.0, stop_offset=10.0),
    ],
)
>>> interval_1 ^ interval_4
IntervalTree(
    intervals=[
        Interval(start_offset=0.0, stop_offset=10.0),
        Interval(start_offset=10.0, stop_offset=20.0),
    ],
)
>>> interval_2 ^ interval_3
IntervalTree(
    intervals=[
        Interval(start_offset=-2.0, stop_offset=2.0),
        Interval(start_offset=5.0, stop_offset=12.0),
    ],
)
>>> interval_2 ^ interval_4
IntervalTree(
    intervals=[
        Interval(start_offset=5.0, stop_offset=10.0),
        Interval(start_offset=12.0, stop_offset=20.0),
    ],
)
>>> interval_3 ^ interval_4
IntervalTree(
    intervals=[
        Interval(start_offset=-2.0, stop_offset=2.0),
        Interval(start_offset=10.0, stop_offset=20.0),
    ],
)
intersects(expr: Interval | float) bool

True when interval intersects expr, another interval or offset:

>>> from supriya.utils import Interval
>>> interval_1 = Interval(0, 10)
>>> interval_2 = Interval(5, 15)
>>> interval_3 = Interval(10, 15)
>>> interval_4 = Interval(15, 25)
>>> interval_1.intersects(interval_1)
True
>>> interval_1.intersects(interval_2)
True
>>> interval_1.intersects(interval_3)
False
>>> interval_1.intersects(interval_4)
False
>>> for offset in [-5, 0, 5, 10, 15]:
...     print(offset, interval_1.intersects(offset))
... 
-5 False
0 True
5 True
10 False
15 False
is_tangent_to(expr: Interval | float) bool

True when interval is tangent to expr, another interval or offset:

>>> from supriya.utils import Interval
>>> interval_1 = Interval(0, 10)
>>> interval_2 = Interval(5, 15)
>>> interval_3 = Interval(10, 15)
>>> interval_4 = Interval(15, 25)
>>> interval_1.is_tangent_to(interval_1)
False
>>> interval_1.is_tangent_to(interval_2)
False
>>> interval_1.is_tangent_to(interval_3)
True
>>> interval_1.is_tangent_to(interval_4)
False
>>> for offset in [-5, 0, 5, 10, 15]:
...     print(offset, interval_1.is_tangent_to(offset))
... 
-5 False
0 True
5 False
10 True
15 False
new(start_offset=None, stop_offset=None, **kwargs)

Template a new interval:

>>> from supriya.utils import Interval
>>> interval_one = Interval(0, 10)
>>> interval_two = interval_one.new()
>>> interval_two
Interval(start_offset=0.0, stop_offset=10.0)
>>> interval_two is not interval_one
True
>>> interval_two.new(start_offset=5.0)
Interval(start_offset=5.0, stop_offset=10.0)
split(*offsets)

Split at offsets:

>>> from supriya.utils import Interval
>>> interval = Interval(0, 10)
>>> for split_interval in interval.split(1, 3, 7):
...     split_interval
... 
Interval(start_offset=0.0, stop_offset=1.0)
Interval(start_offset=1.0, stop_offset=3.0)
Interval(start_offset=3.0, stop_offset=7.0)
Interval(start_offset=7.0, stop_offset=10.0)
translate(translation: float, stop_translation: float | None = None)

Translate offsets by translation, and optionally translate independently when specifying stop_translation:

>>> from supriya.utils import Interval
>>> interval = Interval(0, 10)
>>> interval.translate(5)
Interval(start_offset=5.0, stop_offset=15.0)
>>> interval.translate(5, 7.5)
Interval(start_offset=5.0, stop_offset=17.5)
property duration
property size : float
property wellformed : bool
class IntervalTree(intervals=None, accelerated=True)

Bases: object

A mutable always-sorted collection of intervals.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
__and__(interval)
__contains__(interval)

Is true if this interval tree contains interval. Otherwise false.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> intervals[0] in interval_tree
True
>>> Interval(-1, 100) in interval_tree
False

Returns boolean.

__getitem__(item)

Gets interval at index item.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> interval_tree[-1]
Interval(start_offset=6.0, stop_offset=9.0)
>>> for interval in interval_tree[:3]:
...     interval
... 
Interval(start_offset=0.0, stop_offset=3.0)
Interval(start_offset=1.0, stop_offset=2.0)
Interval(start_offset=1.0, stop_offset=3.0)

Returns interval or intervals.

__iter__()

Iterates intervals in this interval tree.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> for interval in interval_tree:
...     interval
... 
Interval(start_offset=0.0, stop_offset=3.0)
Interval(start_offset=1.0, stop_offset=2.0)
Interval(start_offset=1.0, stop_offset=3.0)
Interval(start_offset=2.0, stop_offset=5.0)
Interval(start_offset=6.0, stop_offset=9.0)

Returns generator.

__len__()

Gets length of this interval tree.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> len(interval_tree)
5

Returns integer.

__setitem__(i, new)

Sets intervals at index i to new.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> interval_tree[:3] = [Interval(100, 200)]

Returns none.

__sub__(interval)

Delete material that intersects interval:

>>> from supriya.utils import Interval, IntervalTree
>>> interval_tree = IntervalTree(
...     [
...         Interval(0, 16),
...         Interval(5, 12),
...         Interval(-2, 8),
...     ]
... )
>>> interval = Interval(5, 10)
>>> result = interval_tree - interval
>>> for interval in interval_tree:
...     interval
... 
Interval(start_offset=-2.0, stop_offset=5.0)
Interval(start_offset=0.0, stop_offset=5.0)
Interval(start_offset=10.0, stop_offset=12.0)
Interval(start_offset=10.0, stop_offset=16.0)

Operates in place and returns interval tree.

add(interval)
find_intersection(interval_or_offset)

Find intervals intersecting a interval or offset.

Finds intervals overlapping offset.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> for x in interval_tree.find_intersection(1.5):
...     x
... 
Interval(start_offset=0.0, stop_offset=3.0)
Interval(start_offset=1.0, stop_offset=2.0)
Interval(start_offset=1.0, stop_offset=3.0)

Finds intervals overlapping interval.

>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> interval = Interval(2, 4)
>>> for x in interval_tree.find_intersection(interval):
...     x
... 
Interval(start_offset=0.0, stop_offset=3.0)
Interval(start_offset=1.0, stop_offset=3.0)
Interval(start_offset=2.0, stop_offset=5.0)
find_intervals_starting_at(offset)
find_intervals_stopping_at(offset)
get_moment_at(offset)

Gets moment at offset.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> interval_tree.get_moment_at(1)
<Moment(1 <<3>>)>
>>> interval_tree.get_moment_at(6.5)
<Moment(6.5 <<1>>)>
get_offset_after(offset)

Gets first start or stop offset after offset, otherwise None.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> for i in range(-1, 11):
...     print(i, interval_tree.get_offset_after(i))
... 
-1 0.0
0 1.0
1 2.0
2 3.0
3 5.0
4 5.0
5 6.0
6 9.0
7 9.0
8 9.0
9 None
10 None
get_start_offset_after(offset)

Gets start offst in this interval tree after offset.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> interval_tree.get_start_offset_after(-1)
0.0
>>> interval_tree.get_start_offset_after(0)
1.0
>>> interval_tree.get_start_offset_after(1)
2.0
>>> interval_tree.get_start_offset_after(2)
6.0
>>> interval_tree.get_start_offset_after(6) is None
True
get_start_offset_before(offset)

Gets start offst in this interval tree before offset.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> interval_tree.get_start_offset_before(7)
6.0
>>> interval_tree.get_start_offset_before(6)
2.0
>>> interval_tree.get_start_offset_before(2)
1.0
>>> interval_tree.get_start_offset_before(1)
0.0
>>> interval_tree.get_start_offset_before(0) is None
True
index(interval)
iterate_moments(reverse=False)

Iterates moments in this interval tree.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> for x in interval_tree.iterate_moments():
...     x
... 
<Moment(0.0 <<1>>)>
<Moment(1.0 <<3>>)>
<Moment(2.0 <<3>>)>
<Moment(6.0 <<1>>)>
>>> for x in interval_tree.iterate_moments(reverse=True):
...     x
... 
<Moment(6.0 <<1>>)>
<Moment(2.0 <<3>>)>
<Moment(1.0 <<3>>)>
<Moment(0.0 <<1>>)>

Returns generator.

iterate_moments_nwise(n=3, reverse=False)

Iterates moments in this interval tree in groups of n.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> for x in interval_tree.iterate_moments_nwise(n=2):
...     x
... 
[<Moment(0.0 <<1>>)>, <Moment(1.0 <<3>>)>]
[<Moment(1.0 <<3>>)>, <Moment(2.0 <<3>>)>]
[<Moment(2.0 <<3>>)>, <Moment(6.0 <<1>>)>]
>>> for x in interval_tree.iterate_moments_nwise(n=2, reverse=True):
...     x
... 
[<Moment(2.0 <<3>>)>, <Moment(6.0 <<1>>)>]
[<Moment(1.0 <<3>>)>, <Moment(2.0 <<3>>)>]
[<Moment(0.0 <<1>>)>, <Moment(1.0 <<3>>)>]

Returns generator.

remove(interval)

Removes interval from this interval tree.

>>> from supriya.utils import Interval, IntervalTree
>>> intervals = (
...     Interval(0, 3),
...     Interval(1, 3),
...     Interval(1, 2),
...     Interval(2, 5),
...     Interval(6, 9),
... )
>>> interval_tree = IntervalTree(intervals)
>>> for interval in intervals[1:-1]:
...     interval_tree.remove(interval)
... 
>>> for interval in interval_tree:
...     interval
... 
Interval(start_offset=0.0, stop_offset=3.0)
Interval(start_offset=6.0, stop_offset=9.0)
update(intervals)
property all_offsets
property all_start_offsets
property all_stop_offsets
property earliest_start_offset
property earliest_stop_offset
property intervals
property latest_start_offset
property latest_stop_offset
property start_offset
property stop_offset
class IntervalTreeDriver(intervals=None)

Bases: object

__contains__(interval)
__getitem__(item)
__iter__()
__len__()
add(interval)
find_intervals_intersecting_interval(interval)
find_intervals_intersecting_offset(offset)
find_intervals_starting_at(offset)
find_intervals_stopping_at(offset)
get_start_offset_after(offset)
get_start_offset_before(offset)
index(interval)
remove(interval)
update(intervals)
class Moment(interval_tree: IntervalTree, start_offset: float, start_intervals: Sequence[Interval], stop_intervals: Sequence[Interval], overlap_intervals: Sequence[Interval])

Bases: object

A moment of intervals in a interval tree.

property next_moment

Gets the next moment in this moment’s interval collection.

property next_start_offset

Gets the next moment start offset in this moment’s interval tree.

property previous_moment

Gets the previous moment in this moment’s interval collection.

property previous_start_offset

Gets the previous moment start offset in this moment’s interval tree.