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)
def test_unary_operators(value, operator): expected = eval('%s value' % operator) with pf.Graph() as graph: operation = eval('%s pf.constant(value)' % operator) actual = graph(operation) assert actual == expected, "expected %s %s = %s but got %s" % \ (operator, value, expected, actual)
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
def build(): with pf.Graph() as graph: a = rand() b = rand() c = rand() d = rand() mode = rand() if (mode == 0): e0 = a + b e1 = b - a e2 = a + d e3 = e1 + c e3.name = "end" elif (mode == 1): e0 = a - b e1 = c + b e2 = d + d e3 = e1 + e2 e4 = e3 + c e4.name = "end" elif (mode == 2): e0 = d - b e1 = d + a e2 = b + c e3 = e1 - e2 e4 = e3 + a e4.name = "end" graph("end")
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)
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)
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)
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
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)
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})
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'})
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)
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
def generate_graph(filename): # Generate AST with open(filename, 'r') as fp: tree = ast.parse(fp.read()) # AST -> Graph with pf.Graph() as graph: return a2r(tree, graph)
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, {})
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()
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)
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})
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
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
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')
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
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
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)
def test_logger(level): with pf.Graph() as graph: logger = pf.Logger(uuid.uuid4().hex) log1 = logger.log(level, "this is a %s message", "test") log2 = getattr(logger, level)("this is another %s message", "test") # Add a handler to the logger stream = six.StringIO() logger.logger.setLevel(logging.DEBUG) logger.logger.addHandler(logging.StreamHandler(stream)) graph([log1, log2]) assert stream.getvalue() == "this is a test message\nthis is another test message\n"
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
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_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'
def test_thread_compatibility(): def work(event): with pf.Graph() as graph: event.wait() event = threading.Event() thread = threading.Thread(target=work, args=(event,)) thread.start() try: with pf.Graph() as graph: event.set() except AssertionError as e: event.set() raise e
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