Пример #1
0
def test_slice():
    with pf.Graph() as graph:
        a = pf.constant(range(100))
        b = pf.constant(1)
        c = a[b:]

    assert len(graph(c)) == 99
Пример #2
0
def example_client():
    with pf.Graph() as graph:
        a = pf.constant(2)
        b = pf.constant(4)
        c = b - a
        d = pf.constant(4)
        e = d - c
        e.name = "end"
        ib_server(graph, {})
Пример #3
0
def test_name_change():
    with pf.Graph() as graph:
        operation = pf.constant(None, name='operation1')
        pf.constant(None, name='operation3')

    assert 'operation1' in graph.operations
    operation.name = 'operation2'
    assert 'operation2' in graph.operations
    assert graph['operation2'] is operation
    # We cannot rename to an existing operation
    with pytest.raises(ValueError):
        operation.name = 'operation3'
Пример #4
0
def test_conditional_callback():
    with pf.Graph() as graph:
        a = pf.constant(1)
        b = pf.constant(2)
        c = pf.placeholder()
        d = pf.conditional(c, a, b + 1)

    # Check that we have "traced" the correct number of operation evaluations
    tracer = pf.Profiler()
    assert graph(d, {c: True}, callback=tracer) == 1
    assert len(tracer.times) == 2
    tracer = pf.Profiler()
    assert graph(d, {c: False}, callback=tracer) == 3
    assert len(tracer.times) == 3
Пример #5
0
def test_binary_operators_left(binary_operators):
    operator, a, b, expected = binary_operators
    print(operator, a, b, expected)
    print(operator, a, b, expected)
    print(operator, a, b, expected)
    print(operator, a, b, expected)
    print(operator, a, b, expected)
    with pf.Graph() as graph:
        _a = pf.constant(a)
        operation = eval('_a %s b' % operator)

    actual = graph(operation)
    assert actual == expected, "expected %s %s %s == %s but got %s" % \
        (a, operator, b, expected, actual)
    # ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ #
    # TODO: this is hacky, there should be some way around this!
    #       this might be a common pattern!
    expr = 'a %s b' % operator

    @slipform(debug=True, add_scope={'expr': expr})
    def graph(a, b):
        operation = eval(expr)

    actual = graph('operation', a=a, b=b)
    assert actual == expected, "expected %s %s %s == %s but got %s" % \
        (a, operator, b, expected, actual)
Пример #6
0
def test_list():
    expected = 13
    with pf.Graph() as graph:
        a = pf.constant(expected)
        b = pf.identity([a, a])
    actual, _ = graph(b)
    assert actual is expected, "expected %s but got %s" % (expected, actual)
Пример #7
0
def test_dict():
    expected = 13
    with pf.Graph() as graph:
        a = pf.constant(expected)
        b = pf.identity({'foo': a})
    actual = graph(b)['foo']
    assert actual is expected, "expected %s but got %s" % (expected, actual)
Пример #8
0
def test_contains():
    with pf.Graph() as graph:
        test = pf.placeholder()
        alphabet = pf.constant('abc')
        contains = pf.contains(alphabet, test)

    assert graph(contains, {test: 'a'})
    assert not graph(contains, {test: 'x'})
Пример #9
0
def test_binary_operators_left(binary_operators):
    operator, a, b, expected = binary_operators
    with pf.Graph() as graph:
        _a = pf.constant(a)
        operation = eval('_a %s b' % operator)

    actual = graph(operation)
    assert actual == expected, "expected %s %s %s == %s but got %s" % \
        (a, operator, b, expected, actual)
Пример #10
0
def test_getattr():
    with pf.Graph() as graph:
        imag = pf.constant(1 + 4j).imag
    assert graph(imag) == 4
    # ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ #
    @slipform()
    def graph():
        imag = (1 + 4j).imag

    assert graph('imag') == 4
Пример #11
0
def test_abs():
    with pf.Graph() as graph:
        absolute = abs(pf.constant(-5))

    assert graph(absolute) == 5
    # ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ #
    @slipform()
    def graph():
        absolute = abs(-5)

    assert graph('absolute') == 5
Пример #12
0
def test_reversed():
    with pf.Graph() as graph:
        rev = reversed(pf.constant('abc'))

    assert list(graph(rev)) == list('cba')
    # ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ #
    @slipform()
    def graph():
        rev = reversed('abc')

    assert list(graph('rev')) == list('cba')
Пример #13
0
def test_conditional():
    with pf.Graph() as graph:
        x = pf.constant(4)
        y = pf.placeholder(name='y')
        condition = pf.placeholder(name='condition')
        z = pf.conditional(condition, x, y)

    assert graph(z, condition=True) == 4
    assert graph(z, condition=False, y=5) == 5
    # We expect a value error if we evaluate the other branch without a placeholder
    with pytest.raises(ValueError):
        graph(z, condition=False)
Пример #14
0
def workers(broker):
    with pf.Graph() as graph:
        x = pf.placeholder('x')
        y = pf.placeholder('y')
        sleep = pf.func_op(time.sleep, pf.func_op(random.uniform, 0, .1))
        with pf.control_dependencies([sleep]):
            (x / y).set_name('z')
        # Can't pickle entire modules
        pf.constant(pf).set_name('not_pickleable')

    # Create multiple workers
    _workers = []
    while len(_workers) < 10:
        worker = pfmq.Worker.from_graph(graph, broker.backend_address)
        worker.run_async()
        _workers.append(worker)

    yield _workers

    # Shut down all the workers
    for worker in _workers:
        worker.cancel()
Пример #15
0
def test_conditional_with_length():
    def f(a):
        return a, a

    with pf.Graph() as graph:
        x = pf.constant(4)
        y = pf.placeholder(name='y')
        condition = pf.placeholder(name='condition')

        z1, z2 = pf.conditional(condition, pf.func_op(f, x), pf.func_op(f, y), length=2)

    assert graph([z1, z2], condition=True) == (4, 4)
    assert graph([z1, z2], condition=False, y=5) == (5, 5)
Пример #16
0
def test_try_callback():
    with pf.Graph() as graph:
        a = pf.placeholder('a')
        b = pf.assert_((a > 0).set_name('condition'), value=a, name='b')
        c = pf.try_(
            b, [(AssertionError,
                 (pf.constant(41, name='41') + 1).set_name('alternative'))])

    tracer = pf.Profiler()
    graph(c, {a: 3}, callback=tracer) == 3
    assert len(tracer.times) == 3

    graph(c, {a: -2}, callback=tracer) == 42
    assert len(tracer.times) == 5
Пример #17
0
def test_contains():
    with pf.Graph() as graph:
        test = pf.placeholder()
        alphabet = pf.constant('abc')
        contains = pf.contains(alphabet, test)

    assert graph(contains, {test: 'a'})
    assert not graph(contains, {test: 'x'})
    # ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ #
    @slipform()
    def graph(test):
        alphabet = 'abc'
        contains = test in alphabet

    assert graph('contains', {'test': 'a'})
    assert not graph('contains', {'test': 'x'})
Пример #18
0
def test_call():
    class Adder:
        def __init__(self, a, b):
            self.a = a
            self.b = b

        def compute(self):
            return self.a + self.b

        def __call__(self):
            return self.compute()

    with pf.Graph() as graph:
        adder = pf.constant(Adder(3, 7))
        op1 = adder()
        op2 = adder.compute()

    assert graph([op1, op2]) == (10, 10)
Пример #19
0
import pythonflow as pf
import math

with pf.Graph() as graph:
    pi = pf.constant(math.pi)
    length = pf.constant(1.0)
    radius = pf.constant(0.25)
    density = pf.constant(450)
    volume = length * pi * radius**2
    mass = volume * density

print(f'Volume: {graph(volume)}')
print(f'Mass: {graph(mass)}')
print("\nNow recalculate with twice the length")
print(f'Volume: {graph(volume, {length: graph(length)*2})}')
print(f'Mass: {graph(mass, {length: graph(length)*2})}')
Пример #20
0
def test_parametrized_decorator():
    with pf.Graph() as graph:
        a, b = _split_in_two(pf.constant('abcd'))

    assert graph(a) == 'ab'
    assert graph(b) == 'cd'
Пример #21
0
def create_benchmark_graph():
    with pf.Graph() as graph:
        pf.constant(None, name='fetch')
        seconds = pf.placeholder('seconds')
        pf.func_op(time.sleep, seconds, name='sleep')
    return graph
Пример #22
0
def rand():
    return pf.constant(random.randint(0, 50))
Пример #23
0
def test_reversed():
    with pf.Graph() as graph:
        rev = reversed(pf.constant('abc'))

    assert list(graph(rev)) == list('cba')
Пример #24
0
def test_abs():
    with pf.Graph() as graph:
        absolute = abs(pf.constant(-5))

    assert graph(absolute) == 5
Пример #25
0
 def func():
     a = pf.constant(1)
     b, c = pf.constant(2), pf.constant(3)
     d, e = [4, 5]
Пример #26
0
def test_getattr():
    with pf.Graph() as graph:
        imag = pf.constant(1 + 4j).imag
    assert graph(imag) == 4
Пример #27
0
def test_iter():
    with pf.Graph() as graph:
        pf.constant('abc', name='alphabet', length=3)
    a, b, c = graph['alphabet']
    assert graph([a, b, c]) == tuple('abc')
Пример #28
0
def _(num, lhs=None, graph=None):
    return pf.constant(num.n)
Пример #29
0
def _(st, lhs=None, graph=None):
    return pf.constant(st.s)
Пример #30
0
import pythonflow as pf

with pf.Graph() as graph:
    # Only load the libraries when necessary
    imageio = pf.import_('imageio')
    ndimage = pf.import_('scipy.ndimage')
    np = pf.import_('numpy')

    filename = pf.placeholder('filename')
    image = (imageio.imread(filename).set_name('imread')[..., :3] /
             255.0).set_name('image')
    noise_scale = pf.constant(.25, name='noise_scale')
    noise = (1 -
             np.random.uniform(0, noise_scale, image.shape)).set_name('noise')
    noisy_image = (image * noise).set_name('noisy_image')
    angle = np.random.uniform(-45, 45)
    rotated_image = ndimage.rotate(noisy_image, angle,
                                   reshape=False).set_name('rotated_image')