Exemple #1
0
def test_pipeline_multi_input():
    @pipeline(ancestor_count=2)
    def sum_offset(p1, p2, o):
        return p1 + p2 + o

    p1 = Slicerator(list(range(10)))
    p2 = Slicerator(list(range(10, 20)))
    o = 100

    res = sum_offset(p1, p2, o)
    assert (isinstance(res, Pipeline))
    assert_array_equal(res, list(range(110, 129, 2)))
    assert (len(res) == len(p1))

    resi = sum_offset(1, 2, 3)
    assert (isinstance(resi, int))
    assert (resi == 6)

    p3 = Slicerator(list(range(20)))
    try:
        sum_offset(p1, p3)
    except ValueError:
        pass
    else:
        raise AssertionError("Should be unable to create pipeline with "
                             "ancestors having different lengths.")
Exemple #2
0
def test_from_class():
    class Dummy(object):
        """DocString"""
        def __init__(self):
            self.frame = list('abcdefghij')

        def __len__(self):
            return len(self.frame)

        def __getitem__(self, i):
            """Other Docstring"""
            return self.frame[i]  # actual code of get_frame

        def __repr__(self):
            return 'Repr'

    DummySli = Slicerator.from_class(Dummy)
    assert Dummy()[:2] == ['a', 'b']  # Dummy is unaffected

    # class slots propagate
    assert DummySli.__name__ == Dummy.__name__
    assert DummySli.__doc__ == Dummy.__doc__
    assert DummySli.__module__ == Dummy.__module__

    dummy = DummySli()
    assert isinstance(dummy, Dummy)  # still instance of Dummy
    assert repr(dummy) == 'Repr'  # repr propagates

    compare_slice_to_list(dummy, 'abcdefghij')
    compare_slice_to_list(dummy[1:], 'bcdefghij')
    compare_slice_to_list(dummy[1:][2:], 'defghij')

    capitalize = pipeline(_capitalize_if_equal)
    cap_b = capitalize(dummy, 'b')
    assert_letters_equal(cap_b, 'aBcdefghij')
Exemple #3
0
def test_from_class():
    class Dummy(object):
        """DocString"""
        def __init__(self):
            self.frame = list('abcdefghij')

        def __len__(self):
            return len(self.frame)

        def __getitem__(self, i):
            """Other Docstring"""
            return self.frame[i]  # actual code of get_frame

        def __repr__(self):
            return 'Repr'

    DummySli = Slicerator.from_class(Dummy)
    assert Dummy()[:2] == ['a', 'b']  # Dummy is unaffected

    # class slots propagate
    assert DummySli.__name__ == Dummy.__name__
    assert DummySli.__doc__ == Dummy.__doc__
    assert DummySli.__module__ == Dummy.__module__

    dummy = DummySli()
    assert isinstance(dummy, Dummy)  # still instance of Dummy
    assert repr(dummy) == 'Repr'  # repr propagates

    compare_slice_to_list(dummy, 'abcdefghij')
    compare_slice_to_list(dummy[1:], 'bcdefghij')
    compare_slice_to_list(dummy[1:][2:], 'defghij')

    capitalize = pipeline(_capitalize_if_equal)
    cap_b = capitalize(dummy, 'b')
    assert_letters_equal(cap_b, 'aBcdefghij')
Exemple #4
0
def test_lazy_hasattr():
    # this ensures that the Slicerator init does not evaluate all properties
    class Dummy(object):
        """DocString"""
        def __init__(self):
            self.frame = list('abcdefghij')

        def __len__(self):
            return len(self.frame)

        def __getitem__(self, i):
            """Other Docstring"""
            return self.frame[i]  # actual code of get_frame

        @property
        def forbidden_property(self):
            raise RuntimeError()

    DummySli = Slicerator.from_class(Dummy)
Exemple #5
0
def test_lazy_hasattr():
    # this ensures that the Slicerator init does not evaluate all properties
    class Dummy(object):
        """DocString"""
        def __init__(self):
            self.frame = list('abcdefghij')

        def __len__(self):
            return len(self.frame)

        def __getitem__(self, i):
            """Other Docstring"""
            return self.frame[i]  # actual code of get_frame

        @property
        def forbidden_property(self):
            raise RuntimeError()

    DummySli = Slicerator.from_class(Dummy)
Exemple #6
0
def test_pipeline_class():
    sli = Slicerator(np.empty((10, 32, 64)))

    @pipeline
    class crop(Pipeline):
        def __init__(self, reader, bbox):
            self.bbox = bbox
            Pipeline.__init__(self, None, reader)

        def _get(self, key):
            bbox = self.bbox
            return self._ancestors[0][key][bbox[0]:bbox[2], bbox[1]:bbox[3]]

        @property
        def frame_shape(self):
            bbox = self.bbox
            return (bbox[2] - bbox[0], bbox[3] - bbox[1])

    cropped = crop(sli, (5, 5, 10, 20))
    assert_array_equal(cropped[0], sli[0][5:10, 5:20])
    assert_array_equal(cropped.frame_shape, (5, 15))
Exemple #7
0
def test_getattr():
    class MyList(list):
        attr1 = 'hello'
        attr2 = 'hello again'

        @index_attr
        def s(self, i):
            return list('ABCDEFGHIJ')[i]

        def close(self):
            pass

    a = Slicerator(MyList('abcdefghij'), propagate_attrs=['attr1', 's'])
    assert_letters_equal(a, list('abcdefghij'))
    assert_true(hasattr(a, 'attr1'))
    assert_false(hasattr(a, 'attr2'))
    assert_true(hasattr(a, 's'))
    assert_false(hasattr(a, 'close'))
    assert_equal(a.attr1, 'hello')
    with assert_raises(AttributeError):
        a[:5].nonexistent_attr

    compare_slice_to_list(list(a.s), list('ABCDEFGHIJ'))
    compare_slice_to_list(list(a[::2].s), list('ACEGI'))
    compare_slice_to_list(list(a[::2][1:].s), list('CEGI'))

    capitalize = pipeline(_capitalize)
    b = capitalize(a)
    assert_letters_equal(b, list('ABCDEFGHIJ'))
    assert_true(hasattr(b, 'attr1'))
    assert_false(hasattr(b, 'attr2'))
    assert_true(hasattr(b, 's'))
    assert_false(hasattr(b, 'close'))
    assert_equal(b.attr1, 'hello')
    with assert_raises(AttributeError):
        b[:5].nonexistent_attr

    compare_slice_to_list(list(b.s), list('ABCDEFGHIJ'))
    compare_slice_to_list(list(b[::2].s), list('ACEGI'))
    compare_slice_to_list(list(b[::2][1:].s), list('CEGI'))
Exemple #8
0
def test_from_func():
    v = Slicerator.from_func(lambda x: "abcdefghij"[x], length=10)
    compare_slice_to_list(v, list("abcdefghij"))
    compare_slice_to_list(v[1:], list("bcdefghij"))
    compare_slice_to_list(v[1:][:4], list("bcde"))
Exemple #9
0
def test_pipeline_propagate_attrs():
    a1 = Slicerator(list(range(10)))
    a1.attr1 = 10
    a2 = Slicerator(list(range(10, 20)))
    a2.attr1 = 20
    a2.attr2 = 30

    p1 = Pipeline(lambda x, y: x + y,
                  a1,
                  a2,
                  propagate_attrs={"attr1", "attr2"},
                  propagate_how=0)
    assert (p1.attr1 == 10)
    try:
        p1.attr2
    except AttributeError:
        pass
    else:
        raise AssertionError("attr2 should not exist")

    p2 = Pipeline(lambda x, y: x + y,
                  a1,
                  a2,
                  propagate_attrs={"attr1", "attr2"},
                  propagate_how=1)
    assert (p2.attr1 == 20)
    assert (p2.attr2 == 30)

    p3 = Pipeline(lambda x, y: x + y,
                  a1,
                  a2,
                  propagate_attrs={"attr1", "attr2"},
                  propagate_how="first")
    assert (p3.attr1 == 10)
    assert (p3.attr2 == 30)

    p4 = Pipeline(lambda x, y: x + y,
                  a1,
                  a2,
                  propagate_attrs={"attr1", "attr2"},
                  propagate_how="last")
    assert (p4.attr1 == 20)
    assert (p4.attr2 == 30)

    a1.attr3 = 40
    a1.attr4 = 50
    a1._propagate_attrs = {"attr3"}
    a1.propagate_attrs = {"attr4"}
    p5 = Pipeline(lambda x, y: x + y, a1, a2, propagate_how="first")
    assert (p5.attr3 == 40)
    assert (p5.attr4 == 50)
    try:
        p5.attr1
    except AttributeError:
        pass
    else:
        raise AssertionError("attr1 should not exist")
    try:
        p5.attr2
    except AttributeError:
        pass
    else:
        raise AssertionError("attr2 should not exist")
Exemple #10
0
def test_inplace_pipeline():
    n_mutable = Slicerator([list([i]) for i in range(10)])
    appended = append_zero_inplace(n_mutable)

    assert_equal(appended[5], [5, 0])  # execute the function
    assert_equal(n_mutable[5], [5])  # check the original
Exemple #11
0
def test_from_func():
    v = Slicerator.from_func(lambda x: 'abcdefghij'[x], length=10)
    compare_slice_to_list(v, list('abcdefghij'))
    compare_slice_to_list(v[1:], list('bcdefghij'))
    compare_slice_to_list(v[1:][:4], list('bcde'))
Exemple #12
0
def test_no_len_raises():
    with assert_raises(ValueError):
        Slicerator((i for i in range(5)), (i for i in range(5)))
Exemple #13
0
    # mixing positive and negative indices
    some_indices = [
        r.choice(indices + [-i - 1 for i in indices]) for _ in range(2)
    ]
    assert_letters_equal([actual[i] for i in some_indices],
                         [expected[i] for i in some_indices])
    # test slices
    assert_letters_equal(actual[::2], expected[::2])
    assert_letters_equal(actual[1::2], expected[1::2])
    assert_letters_equal(actual[::3], expected[::3])
    assert_letters_equal(actual[1:], expected[1:])
    assert_letters_equal(actual[:], expected[:])
    assert_letters_equal(actual[:-1], expected[:-1])


v = Slicerator(list('abcdefghij'))
n = Slicerator(list(range(10)))


def test_bool_mask():
    mask = np.array([True, False] * 5)
    s = v[mask]
    assert_letters_equal(s, list('acegi'))


def test_slice_of_slice():
    slice1 = v[4:]
    compare_slice_to_list(slice1, list('efghij'))
    slice2 = slice1[-3:]
    compare_slice_to_list(slice2, list('hij'))
    slice1a = v[[3, 4, 5, 6, 7, 8, 9]]
Exemple #14
0
    # mixing positive and negative indices
    some_indices = [
        r.choice(indices + [-i - 1 for i in indices]) for _ in range(2)
    ]
    assert_letters_equal([actual[i] for i in some_indices],
                         [expected[i] for i in some_indices])
    # test slices
    assert_letters_equal(actual[::2], expected[::2])
    assert_letters_equal(actual[1::2], expected[1::2])
    assert_letters_equal(actual[::3], expected[::3])
    assert_letters_equal(actual[1:], expected[1:])
    assert_letters_equal(actual[:], expected[:])
    assert_letters_equal(actual[:-1], expected[:-1])


v = Slicerator(list('abcdefghij'))


def test_bool_mask():
    mask = np.array([True, False] * 5)
    s = v[mask]
    assert_letters_equal(s, list('acegi'))


def test_slice_of_slice():
    slice1 = v[4:]
    compare_slice_to_list(slice1, list('efghij'))
    slice2 = slice1[-3:]
    compare_slice_to_list(slice2, list('hij'))
    slice1a = v[[3, 4, 5, 6, 7, 8, 9]]
    compare_slice_to_list(slice1a, list('defghij'))
Exemple #15
0
def test_from_func():
    v = Slicerator.from_func(lambda x: 'abcdefghij'[x], length=10)
    compare_slice_to_list(v, list('abcdefghij'))
    compare_slice_to_list(v[1:], list('bcdefghij'))
    compare_slice_to_list(v[1:][:4], list('bcde'))