def __init__(self, parent=None):
        super().__init__(parent=parent)

        # create pfgraph
        with pf.Graph() as graph:
            imageio = pf.import_('imageio')
            ndimage = pf.import_('scipy.ndimage')
            np = pf.import_('numpy')

            x = PFSlider(name="x")
            y = PFSlider(name="y")
            r = (x * y).set_name("r")

            frame = pf.placeholder("frame")
            readop(path=pf.placeholder('filepath'), frame=frame)
            # image = imageio.imread(filename).set_name('imread')

        self.setLayout(QHBoxLayout())

        self.model = PFGraphModel(
            graph, **{
                'x': 90,
                'y': 30,
                'frame': 0,
                'filepath': "C:/Users/andris/Videos/M2U00001.MPG"
            })
        self.graphview = GraphView()
        self.graphview.setModel(self.model)
        self.viewer = Viewer()
        self.layout().addWidget(self.graphview)
        self.layout().addWidget(self.viewer)

        self.renderer = GraphicsRenderer()
        self.viewer.addItem(self.renderer)
        self.viewer.body.centerOn(0, 0)

        def onSelectionChange():
            """show output results in viewer"""
            if len(self.model.nodeSelection()) > 0:
                for node in self.model.nodeSelection():
                    outputPorts = self.model.nodeOutputs(node)
                    port = outputPorts[0]
                    outputValue = self.model.portData((port, "value"),
                                                      Qt.EditRole)
                    self.renderer.setInput(outputValue)
                    self.renderer.update()
            else:
                self.renderer.setInput(None)
                self.renderer.update()

        def onDataChange(index):
            for node in self.model.nodeSelection():
                outputPorts = self.model.nodeOutputs(node)
                port = outputPorts[0]
                outputValue = self.model.portData((port, "value"), Qt.EditRole)
                self.renderer.setInput(outputValue)
                self.renderer.update()

        self.model.nodeSelectionChange.connect(onSelectionChange)
        self.model.portDataChange.connect(onDataChange)
Beispiel #2
0
 def __init__(self, target, graph, **kwargs):
     self.t_exp = target
     self.t_sym = '_' + target
     self.graph_ops = graph.operations
     if self.t_sym not in self.graph_ops:
         pf.placeholder(self.t_sym)
     super(LazySymbol, self).__init__(**kwargs)
Beispiel #3
0
def test_cache():
    calls = []
    cache = {}

    @pf.opmethod
    def _cached_add(x, y):
        calls.append((x, y))
        return x + y

    with pf.Graph() as graph:
        a = pf.placeholder()
        b = pf.placeholder()
        c = pf.cache(_cached_add(a, b), cache.__getitem__, cache.setdefault)

    for _ in range(3):
        assert graph(c, {a: 1, b: 2}) == 3
        assert len(calls) == 1
        assert len(cache) == 1

    # Modify the cache value
    key, = cache.keys()
    assert key == hash((1, 2))
    cache[key] = -1
    assert graph(c, {a: 1, b: 2}) == -1

    # Check another call
    assert graph(c, {a: 1, b: 4}) == 5
    assert len(calls) == 2
    assert len(cache) == 2
Beispiel #4
0
def test_context():
    with pf.Graph() as graph:
        a = pf.placeholder(name='a')
        b = pf.placeholder(name='b')
        c = pf.placeholder(name='c')
        x = a * b + c
    actual = graph(x, {a: 4, 'b': 7}, c=9)
    assert actual == 37
Beispiel #5
0
def test_try_not_caught():
    with pf.Graph() as graph:
        a = pf.placeholder()
        b = pf.placeholder()
        c = pf.try_(a / b, [(ValueError, 'value-error')])

    with pytest.raises(ZeroDivisionError):
        graph(c, {a: 1, b: 0})
Beispiel #6
0
def run_processor():
    with pf.Graph() as graph:
        x = pf.placeholder('x')
        a = pf.placeholder('a')
        y = (a * x).set_name('y')

    with pf.Processor.from_graph('tcp://127.0.0.1:5555',
                                 'tcp://127.0.0.1:5556', graph) as processor:
        processor.run()
Beispiel #7
0
    def __call__(self, fetches, **placeholders):
        vs = {'_' + k: v for k, v in placeholders.items()}

        with self.graph:
            for key in vs:
                if key not in self.graph.operations:
                    pf.placeholder(key)

        return self.graph(fetches, **vs)
Beispiel #8
0
def make_simple_graph(node):
    """
    Defines environment logic by building pf.Graph with provided nodes.

    Args:
        node:     dictionary of Node instances

    Returns:
        pf.Graph instance, dictionaries of input of and output handles.s
    """
    with pf.Graph() as graph:

        # Input placeholders to graph:
        is_reset = pf.placeholder(name='reset_input_flag')
        episode_duration = pf.placeholder(name='episode_duration_input')
        dataset = pf.placeholder(name='entire_dataset_input')
        action = pf.placeholder(name='incoming_mdp_action')

        # Connect nodes, define runtime logic:
        episode = node['episode'](input_state=dataset,
                                  reset=is_reset,
                                  sample_length=episode_duration)

        market_state = node['market'](input_state=episode, reset=is_reset)

        orders = node['order'](input_state=action, reset=is_reset)

        portfolio_state = node['manager'](input_state=market_state,
                                          reset=is_reset,
                                          orders=orders)

        reward = node['reward'](input_state=portfolio_state, reset=is_reset)

        done = node['done'](input_state=market_state)

        # Filter out all irrelevant fields to get observation tensor(s):
        observation_state = {
            'market_features': market_state['features'],
            'value': portfolio_state['portfolio_value'],
            'reward': reward,
        }
        # Service node, processes dictionaries, dataframes
        # and emits numpy arrays ready to feed to estimator:
        observation_state = node['observation'](input_state=observation_state)

    # Group graph input and output handles:
    graph_input = dict(reset=is_reset,
                       dataset=dataset,
                       episode_duration=episode_duration,
                       action=action)
    graph_output = dict(
        observation=observation_state,
        reward=reward,
        done=done,
    )
    return graph, graph_input, graph_output
Beispiel #9
0
def test_stack_trace():
    with pf.Graph() as graph:
        a = pf.placeholder()
        b = pf.placeholder()
        c = a / b

    try:
        graph(c, {a: 1, b: 0})
        raise RuntimeError("did not raise ZeroDivisionError")
    except ZeroDivisionError as ex:
        assert isinstance(ex.__cause__, pf.EvaluationError)
Beispiel #10
0
def test_try(context, expected):
    finally_reached = []

    with pf.Graph() as graph:
        a = pf.placeholder('a')
        b = pf.placeholder('b')
        c = pf.try_(a / b, [(ZeroDivisionError, 'zero-division')],
                    pf.func_op(lambda: finally_reached.append('done')))

    assert graph(c, context) == expected
    assert finally_reached
Beispiel #11
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)
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)
Beispiel #13
0
def test_cache_file():
    with tempfile.TemporaryDirectory() as tmpdir:
        with pf.Graph() as graph:
            a = pf.placeholder()
            b = pf.placeholder()
            c = pf.cache_file(a + b, f"{tmpdir}/%s.pkl")

        assert graph(c, {a: 5, b: 9}) == 14
        filename = f"{tmpdir}/{hash((5, 9))}.pkl"
        assert os.path.isfile(filename)
        # Move the file to a different location and ensure the value is loaded
        os.rename(filename, f"{tmpdir}/{hash((8, 1))}.pkl")
        assert graph(c, {a: 8, b: 1}) == 14
def test_cache_file():
    make_tmpdir = Py2TemporaryDirectory if six.PY2 else tempfile.TemporaryDirectory
    with make_tmpdir() as tmpdir:
        with pf.Graph() as graph:
            a = pf.placeholder()
            b = pf.placeholder()
            c = pf.cache_file(a + b, "{}/%s.pkl".format(tmpdir))

        assert graph(c, {a: 5, b: 9}) == 14
        filename = "{}/{}.pkl".format(tmpdir, hash((5, 9)))
        assert os.path.isfile(filename)
        # Move the file to a different location and ensure the value is loaded
        os.rename(filename, "{}/{}.pkl".format(tmpdir, hash((8, 1))))
        assert graph(c, {a: 8, b: 1}) == 14
Beispiel #15
0
def test_context():
    with pf.Graph() as graph:
        a = pf.placeholder(name='a')
        b = pf.placeholder(name='b')
        c = pf.placeholder(name='c')
        x = a * b + c
    actual = graph(x, {a: 4, 'b': 7}, c=9)
    assert actual == 37
    # ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ #
    @slipform()
    def graph(a, b, c):
        x = a * b + c

    actual = graph('x', a=4, b=7, c=9)
    assert actual == 37
Beispiel #16
0
def _(x, lhs=None, graph=None):
    name = resolve_name(x)
    if name.startswith('_'):
        if name in graph.operations:
            return graph.operations[name]
        else:
            return pf.placeholder(name)
    elif lhs == name:
        name = '_' + name
        if name in graph.operations:
            return graph.operations[name]
        else:
            return pf.placeholder(name)
    else:
        return LazySymbol(name, graph=graph)
Beispiel #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'})
Beispiel #18
0
def test_assert_with_value():
    with pf.Graph() as graph:
        x = pf.placeholder(name='x')
        assertion = pf.assert_(x < 10, value=2 * x)

    assert graph(assertion, x=9) == 18
    with pytest.raises(AssertionError):
        graph(assertion, x=11)
Beispiel #19
0
def test_duplicate_value():
    with pf.Graph() as graph:
        a = pf.placeholder('a')

    with pytest.raises(ValueError):
        graph([], {a: 1}, a=1)

    with pytest.raises(ValueError):
        graph([], {a: 1, 'a': 1})
Beispiel #20
0
def test_graph_pickle():
    with pf.Graph() as graph:
        x = pf.placeholder('x')
        y = pf.pow_(x, 3, name='y')

    _x = random.uniform(0, 1)
    desired = graph('y', x=_x)

    pickled = pickle.dumps(graph)
    graph = pickle.loads(pickled)
    actual = graph('y', x=_x)
    assert desired == actual
Beispiel #21
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()
Beispiel #22
0
def test_invalid_fetches():
    with pf.Graph():
        a = pf.placeholder()
    graph = pf.Graph()

    with pytest.raises(RuntimeError):
        graph(a)

    with pytest.raises(KeyError):
        graph('a')

    with pytest.raises(ValueError):
        graph(123)
Beispiel #23
0
def _(ass, graph=None):
    lhs, rhs = ass.target, ass.value

    lhs_name = resolve_name(lhs)

    phname = '_' + lhs_name
    if phname not in graph.operations:
        old = pf.placeholder(phname)
    else:
        old = graph.operations[phname]

    (old + unwind(rhs, lhs=lhs_name, graph=graph)).set_name(lhs_name)

    return lhs_name
Beispiel #24
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
Beispiel #25
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
Beispiel #26
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'})
Beispiel #27
0
def test_profiling():
    with pf.Graph() as graph:
        duration = pf.placeholder('duration')
        # Use a conditional to make sure we also test the evaluation of conditionals with callbacks
        positive_duration = pf.conditional(duration < 0, 0, duration)
        sleep = pf.import_('time').sleep(positive_duration)

    callback = pf.Profiler()
    graph(sleep, callback=callback, duration=0.5)
    assert callback.times[sleep] > 0.5
    # We never evaluate duration because it is already provided in the context
    assert duration not in callback.times
    # Check that the slowest operation comes first
    callback_str = str(callback)
    assert callback_str.startswith(str(sleep))
    assert len(callback_str.split('\n')) == 5
Beispiel #28
0
def test_assert_with_dependencies(message):
    with pf.Graph() as graph:
        x = pf.placeholder(name='x')
        if message:
            assertion = pf.assert_(x < 10, message, 10, x)
        else:
            assertion = pf.assert_(x < 10)
        with pf.control_dependencies([assertion]):
            y = 2 * x

    assert len(y.dependencies) == 1
    assert graph(y, x=9) == 18
    with pytest.raises(AssertionError) as exc_info:
        graph(y, x=11)

    if message:
        exc_info.match(message % (10, 11))
Beispiel #29
0
def test_unpack_without_length():
    with pytest.raises(TypeError):
        with pf.Graph():
            _1, _2 = pf.placeholder()
Beispiel #30
0
def test_bool():
    with pf.Graph() as graph:
        a = pf.placeholder()

    assert a