def test_interval_set_construction_fusion():
    interval_set = IntervalSet(Interval(0., 1.), Interval(1., 3.))
    assert_equal(interval_set.intervals[0], Interval(0., 3.))
    assert_equal(len(interval_set.intervals), 1)
    assert_is(type(interval_set), FloatIntervalSet)

    interval_set = IntervalSet(Interval(1., 3.), Interval(0., 1.))
    assert_equal(interval_set.intervals[0], Interval(0., 3.))
    assert_equal(len(interval_set.intervals), 1)
    assert_is(type(interval_set), FloatIntervalSet)

    interval_set = IntervalSet(Interval(0., 1., upper_closed=False),
                               Interval(1., 3., lower_closed=False))
    assert_equal(interval_set.intervals[0], Interval(0.,
                                                     1.,
                                                     upper_closed=False))
    assert_equal(interval_set.intervals[1], Interval(1.,
                                                     3.,
                                                     lower_closed=False))
    assert_equal(len(interval_set.intervals), 2)
    assert_is(type(interval_set), FloatIntervalSet)

    interval_set = IntervalSet(Interval(1., 3., lower_closed=False),
                               Interval(1., 1.),
                               Interval(0., 1., upper_closed=False))
    assert_equal(interval_set.intervals[0], Interval(0., 3.),
                 str(interval_set.intervals[0]))
    assert_equal(len(interval_set.intervals), 1)
    assert_is(type(interval_set), FloatIntervalSet)

    interval_set = IntervalSet(Interval(0., 1.000001, upper_closed=False),
                               Interval(1., 3., lower_closed=False))
    assert_equal(interval_set.intervals[0], Interval(0., 3.))
    assert_equal(len(interval_set.intervals), 1)
    assert_is(type(interval_set), FloatIntervalSet)
def test_intersection():
    interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False),
                                Interval(1., 3., lower_closed=False))
    interval_set2 = IntervalSet(Interval(.5, 1.5))
    assert_equal(
        interval_set1.intersection(interval_set2).intervals,
        IntervalSet(Interval(.5, 1., upper_closed=False),
                    Interval(1., 1.5, lower_closed=False)).intervals)
    interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False))
    interval_set2 = IntervalSet(Interval(1., 3., lower_closed=False))
    assert_equal(interval_set1.intersection(interval_set2).intervals, tuple())
def test_iteration():
    interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False),
                                Interval(1., 3., lower_closed=False))
    assert_list_equal(list(interval_set1), [
        Interval(0., 1., upper_closed=False),
        Interval(1., 3., lower_closed=False)
    ])
def test_date_interval_set_construction():
    interval_set = IntervalSet(Interval(date(2012, 1, 1), date(2012, 2, 5)),
                               Interval(date(2014, 12, 3), date(2015, 3, 17)))
    assert_equal(len(interval_set.intervals), 2)
    assert_equal(interval_set.intervals[0],
                 Interval(date(2012, 1, 1), date(2012, 2, 5)))
    assert_equal(interval_set.intervals[1],
                 Interval(date(2014, 12, 3), date(2015, 3, 17)))
    assert_is(type(interval_set), DateIntervalSet)
def test_union():
    interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False),
                                Interval(1., 3., lower_closed=False))
    interval_set2 = IntervalSet(Interval(.5, 1.5))
    assert_equal(
        interval_set1.union(interval_set2).intervals, (Interval(0., 3.), ))
    interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False))
    interval_set2 = IntervalSet(Interval(1., 3., lower_closed=False))
    assert_equal(
        interval_set1.union(interval_set2).intervals, (Interval(
            0., 1., upper_closed=False), Interval(1., 3., lower_closed=False)))
def test_contains():
    interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False),
                                Interval(1., 3., lower_closed=False))
    assert_in(.3, interval_set1)
    assert_not_in(1., interval_set1)
    assert_not_in(-0.00001, interval_set1)
    assert_not_in(3.000001, interval_set1)
    assert_in(2., interval_set1)
    assert_in(2, interval_set1)  # int automatically cast as double
    assert_raises(TypeError, lambda: 'x' in interval_set1)
Exemplo n.º 7
0
def compare_intersection():
    cyinterval_set1 = CyIntervalSet(CyInterval(0., 1., upper_closed=False),
                                    CyInterval(1., 3., lower_closed=False))
    cyinterval_set2 = CyIntervalSet(CyInterval(.5, 1.5))
    orinterval_set1 = OrigIntervalSet([
        CyInterval(0., 1., upper_closed=False),
        OrigInterval(1., 3., lower_closed=False)
    ])
    orinterval_set2 = OrigIntervalSet([OrigInterval(.5, 1.5)])
    n = 100000
    t0 = time.time()
    for _ in range(n):
        cyinterval_set1.intersection(cyinterval_set2)
    t1 = time.time()
    print('cyinterval took %f seconds for %d iterations' % (t1 - t0, n))
    d_cy = t1 - t0
    t0 = time.time()
    for _ in range(n):
        orinterval_set1.intersection(orinterval_set2)
    t1 = time.time()
    d_orig = t1 - t0
    print('interval took %f seconds for %d iterations' % (t1 - t0, n))
    print('cyinterval was %f times as fast as interval' % (d_orig / d_cy))
def test_minus():
    interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False),
                                Interval(1., 3., lower_closed=False))
    interval_set2 = IntervalSet(Interval(.5, 1.5))
    assert_equal(
        interval_set1.minus(interval_set2).intervals,
        (Interval(0., .5,
                  upper_closed=False), Interval(1.5, 3., lower_closed=False)))
    assert_equal(
        interval_set2.minus(interval_set1).intervals, (Interval(1., 1.), ))
def test_complement():
    interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False),
                                Interval(1., 3., lower_closed=False))
    assert_equal(interval_set1.complement().intervals,
                 (Interval(unbounded, 0., upper_closed=False), Interval(
                     1., 1.), Interval(3., unbounded, lower_closed=False)))
    assert_equal(
        IntervalSet(interval_type=float).complement().intervals,
        IntervalSet(Interval(unbounded, unbounded,
                             interval_type=float)).intervals)
    assert_equal(interval_set1.complement().complement(), interval_set1)
def test_operators():
    interval_set1 = IntervalSet(Interval(0., 1., upper_closed=False),
                                Interval(1., 3., lower_closed=False))
    interval_set2 = IntervalSet(Interval(.5, 1.5))
    assert_not_equal(interval_set1, interval_set2)
    assert_less_equal(interval_set1, interval_set1)
    assert_greater_equal(interval_set1, interval_set1)
    assert_equal(interval_set2, IntervalSet(Interval(.5, 1.5)))
    assert_less(interval_set1 & interval_set2, interval_set2)
    assert_less(interval_set1 & interval_set2, interval_set1)
    assert_less(interval_set1, interval_set1 | interval_set2)
    assert_less(interval_set2, interval_set1 | interval_set2)
    assert_equal(~~interval_set2, interval_set2)
    assert_equal((~interval_set2) & interval_set2,
                 IntervalSet(interval_type=float))
    assert_equal(
        (~interval_set2) | interval_set2,
        IntervalSet(Interval(unbounded, unbounded, interval_type=float)))
    assert_equal(
        interval_set1 - interval_set2,
        IntervalSet(Interval(0., .5, upper_closed=False),
                    Interval(1.5, 3., lower_closed=False)))
def test_float_interval_set_construction():
    interval_set = IntervalSet(Interval(0., 1.), Interval(2., 3.))
    assert_equal(len(interval_set.intervals), 2)
    assert_equal(interval_set.intervals[0], Interval(0., 1.))
    assert_equal(interval_set.intervals[1], Interval(2., 3.))
    assert_is(type(interval_set), FloatIntervalSet)
def test_interval_set_construction_single_interval():
    interval_set = IntervalSet(Interval(0., 1.))
    assert_equal(len(interval_set.intervals), 1)
    assert_equal(interval_set.intervals[0], Interval(0., 1.))
    assert_is(type(interval_set), FloatIntervalSet)
def test_default_type():
    interval_set = IntervalSet(Interval(unbounded, unbounded))
    assert_is(type(interval_set), ObjectIntervalSet)
    interval_set = IntervalSet()
    assert_is(type(interval_set), ObjectIntervalSet)
Exemplo n.º 14
0
from cyinterval.cyinterval import Interval, IntervalSet
from collections import defaultdict

positive = IntervalSet(Interval(lower_bound = 0, lower_closed=False))
nonnegative = IntervalSet(Interval(lower_bound = 0, lower_closed=True))
negative = IntervalSet(Interval(upper_bound = 0, upper_closed=False))
nonpositive = IntervalSet(Interval(upper_bound = 0, upper_closed=True))

def all_or_none(collection, attribute, ignore={}):
    if not collection:
        return None
    first = True
    result = None
    for item in collection:
        try:
            value = getattr(item,attribute)
        except AttributeError:
            continue
        if value in ignore:
            continue
        if first:
            result = value
            first = False
        elif value != result:
            return None
    return result
        
def all_or_raise(collection, attribute, ignore={}):
    if not collection:
        return None
    first = True