Exemplo n.º 1
0
def test_sub_graph_with_arg_input():
    graph0 = Graph()
    graph0_0 = Graph()

    with pytest.raises(PyungoError) as err:
        graph0.add_node(graph0_0,
                        args=['inp1_1', 'inp1_2'],
                        outputs=['inp2_1'])

    assert "Node with Graph can only accept kwargs input. However, get args = ['inp1_1', 'inp1_2']" in str(
        err.value)
Exemplo n.º 2
0
def test_simple_parallel():
    """ TODO: We could mock and make sure things are called correctly """

    graph = Graph(pool_size=2)

    graph.add_node(par_f_my_function, inputs=['a', 'b'], outputs=['c'])
    graph.add_node(par_f_my_function3, inputs=['d', 'a'], outputs=['e'])
    graph.add_node(par_f_my_function2, inputs=['c'], outputs=['d'])
    graph.add_node(par_f_my_function2, inputs=['c'], outputs=['f'])
    graph.add_node(par_f_my_function2, inputs=['c'], outputs=['g'])

    res = graph.calculate(data={'a': 2, 'b': 3})

    assert res == -1.5
Exemplo n.º 3
0
def test_simple_without_decorator():
    graph = Graph()

    def f_my_function(a, b):
        return a + b

    def f_my_function3(d, a):
        return d - a

    def f_my_function2(c):
        return c / 10.

    graph.add_node(f_my_function, inputs=['a', 'b'], outputs=['c'])
    graph.add_node(f_my_function3, inputs=['d', 'a'], outputs=['e'])
    graph.add_node(f_my_function2, inputs=['c'], outputs=['d'])

    res = graph.calculate(data={'a': 2, 'b': 3})

    assert res == -1.5
    assert graph.data['e'] == -1.5
Exemplo n.º 4
0

def unpack_dict(f):  # (ordered)
    def wrap(*args, **kwargs):
        res = f(*args, **kwargs)
        return [res[k] for k in res]

    return wrap


# parallelism not needed for this example
graph = Graph(parallel=False)

graph.add_node(unpack_df(pvlib.solarposition.get_solarposition),
               inputs=['index', 'latitude', 'longitude'],
               outputs=[
                   'apparent_elevation', 'apparent_zenith', 'azimuth',
                   'elevation', 'equation_of_time', 'zenith'
               ])
graph.add_node(pvlib.irradiance.extraradiation,
               inputs=['index'],
               outputs=['dni_extra'])
graph.add_node(pvlib.atmosphere.relativeairmass,
               inputs=['apparent_zenith'],
               outputs=['airmass'])
graph.add_node(pvlib.irradiance.haydavies,
               inputs=[
                   'surface_tilt', 'surface_azimuth', 'DHI', 'DNI',
                   'dni_extra', 'apparent_zenith', 'azimuth'
               ],
               outputs=['poa_sky_diffuse'])
graph.add_node(pvlib.irradiance.grounddiffuse,
Exemplo n.º 5
0
def test_deep_prefix_id_ordered_nodes():
    def f_my_function1(inp1_1, inp1_2):
        return inp1_1 + inp1_2

    def f_my_function2(inp2_1):
        return inp2_1 / 10.

    def f_my_function3(inp3_1, inp3_2):
        return inp3_1 - inp3_2

    graph0 = Graph()
    graph0_0 = Graph()
    graph0_1 = Graph()
    graph0_1_0 = Graph()

    # inp2_1 = 1.5, inp3_2 = 0.15, out=0.1785
    graph0.add_node(graph0_0, kwargs=['inp1_1', 'inp1_2'], outputs=['inp2_1'])
    graph0.add_node(f_my_function2, inputs=['inp2_1'], outputs=['inp3_2'])
    graph0.add_node(graph0_1,
                    kwargs=[('inp1', 'inp1_1'), ('inp2', 'inp3_2')],
                    outputs=['out'])

    graph0_0.add_node(f_my_function1,
                      inputs=['inp1_1', 'inp1_2'],
                      outputs=['inp2_1'])
    graph0_0.add_node(f_my_function2, inputs=['inp2_1'], outputs=['inp3_2'])
    graph0_0.add_node(f_my_function3,
                      inputs=[('inp3_1', 'inp1_1'), 'inp3_2'],
                      outputs=['out'])

    # inp2_1 = 0.215, inp3_2 = 0.0215, out = 0.1785
    graph0_1_0.add_node(f_my_function1,
                        inputs=['inp1_1', 'inp1_2'],
                        outputs=['inp2_1'])
    graph0_1_0.add_node(f_my_function2, inputs=['inp2_1'], outputs=['inp3_2'])
    graph0_1_0.add_node(f_my_function3,
                        inputs=[('inp3_1', 'inp1_1'), 'inp3_2'],
                        outputs=['out'])

    # out1 = 0.2, out2 = 0.015, out=0.1785
    graph0_1.add_node(f_my_function2, inputs=['inp1'], outputs=['out1'])
    graph0_1.add_node(f_my_function2, inputs=['inp2'], outputs=['out2'])
    graph0_1.add_node(graph0_1_0,
                      kwargs=[('inp1_1', 'out1'), ('inp1_2', 'out2')],
                      outputs=['out3'])

    prefix_ids, deep_ordered_nodes = zip(
        *graph0.deep_prefix_id_ordered_nodes())

    graph0_nodes = graph0.ordered_nodes
    graph0_0_nodes = graph0_0.ordered_nodes
    graph0_1_nodes = graph0_1.ordered_nodes
    graph0_1_0_nodes = graph0_1_0.ordered_nodes

    graph0_prefix_ids = [n.id for n in graph0.ordered_nodes]
    graph0_0_prefix_ids = [
        graph0_nodes[0].id + '.' + n.id for n in graph0_0.ordered_nodes
    ]
    graph0_1_prefix_ids = [
        graph0_nodes[2].id + '.' + n.id for n in graph0_1.ordered_nodes
    ]
    graph0_1_0_prefix_ids = [
        graph0_nodes[2].id + '.' + graph0_1_nodes[2].id + '.' + n.id
        for n in graph0_1_0.ordered_nodes
    ]

    assert deep_ordered_nodes[0] is graph0_nodes[0]
    for n1, n2 in zip(deep_ordered_nodes[1:4], graph0_0_nodes):
        assert n1 is n2
    assert deep_ordered_nodes[4] is graph0_nodes[1]
    assert deep_ordered_nodes[5] is graph0_nodes[2]
    for n1, n2 in zip(deep_ordered_nodes[6:9], graph0_1_nodes):
        assert n1 is n2
    for n1, n2 in zip(deep_ordered_nodes[9:], graph0_1_0_nodes):
        assert n1 is n2

    assert prefix_ids[0] == graph0_prefix_ids[0]
    for pi1, pi2 in zip(prefix_ids[1:4], graph0_0_prefix_ids):
        assert pi1 == pi2
    assert prefix_ids[4] == graph0_prefix_ids[1]
    assert prefix_ids[5] == graph0_prefix_ids[2]
    for pi1, pi2 in zip(prefix_ids[6:9], graph0_1_prefix_ids):
        assert pi1 == pi2
    for pi1, pi2 in zip(prefix_ids[9:], graph0_1_0_prefix_ids):
        assert pi1 == pi2
Exemplo n.º 6
0
def test_deep_ordered_nodes():
    def f_my_function1(inp1_1, inp1_2):
        return inp1_1 + inp1_2

    def f_my_function2(inp2_1):
        return inp2_1 / 10.

    def f_my_function3(inp3_1, inp3_2):
        return inp3_1 - inp3_2

    graph0 = Graph()
    graph0_0 = Graph()
    graph0_1 = Graph()
    graph0_1_0 = Graph()

    # inp2_1 = 1.5, inp3_2 = 0.15, out=0.1785
    graph0.add_node(graph0_0, kwargs=['inp1_1', 'inp1_2'], outputs=['inp2_1'])
    graph0.add_node(f_my_function2, inputs=['inp2_1'], outputs=['inp3_2'])
    graph0.add_node(graph0_1,
                    kwargs=[('inp1', 'inp1_1'), ('inp2', 'inp3_2')],
                    outputs=['out'])

    graph0_0.add_node(f_my_function1,
                      inputs=['inp1_1', 'inp1_2'],
                      outputs=['inp2_1'])
    graph0_0.add_node(f_my_function2, inputs=['inp2_1'], outputs=['inp3_2'])
    graph0_0.add_node(f_my_function3,
                      inputs=[('inp3_1', 'inp1_1'), 'inp3_2'],
                      outputs=['out'])

    # inp2_1 = 0.215, inp3_2 = 0.0215, out = 0.1785
    graph0_1_0.add_node(f_my_function1,
                        inputs=['inp1_1', 'inp1_2'],
                        outputs=['inp2_1'])
    graph0_1_0.add_node(f_my_function2, inputs=['inp2_1'], outputs=['inp3_2'])
    graph0_1_0.add_node(f_my_function3,
                        inputs=[('inp3_1', 'inp1_1'), 'inp3_2'],
                        outputs=['out'])

    # out1 = 0.2, out2 = 0.015, out=0.1785
    graph0_1.add_node(f_my_function2, inputs=['inp1'], outputs=['out1'])
    graph0_1.add_node(f_my_function2, inputs=['inp2'], outputs=['out2'])
    graph0_1.add_node(graph0_1_0,
                      kwargs=[('inp1_1', 'out1'), ('inp1_2', 'out2')],
                      outputs=['out3'])

    deep_ordered_nodes = graph0.deep_ordered_nodes
    graph0_nodes = graph0.ordered_nodes
    graph0_0_nodes = graph0_0.ordered_nodes
    graph0_1_nodes = graph0_1.ordered_nodes
    graph0_1_0_nodes = graph0_1_0.ordered_nodes

    assert deep_ordered_nodes[0] is graph0_nodes[0]
    for n1, n2 in zip(deep_ordered_nodes[1:4], graph0_0_nodes):
        assert n1 is n2
    assert deep_ordered_nodes[4] is graph0_nodes[1]
    assert deep_ordered_nodes[5] is graph0_nodes[2]
    for n1, n2 in zip(deep_ordered_nodes[6:9], graph0_1_nodes):
        assert n1 is n2
    for n1, n2 in zip(deep_ordered_nodes[9:], graph0_1_0_nodes):
        assert n1 is n2
Exemplo n.º 7
0
def test_sub_graph():
    def f_my_function1(inp1_1, inp1_2):
        return inp1_1 + inp1_2

    def f_my_function2(inp2_1):
        return inp2_1 / 10.

    def f_my_function3(inp3_1, inp3_2):
        return inp3_1 - inp3_2

    graph0 = Graph()
    graph0_0 = Graph()
    graph0_1 = Graph()
    graph0_1_0 = Graph()

    # inp2_1 = 1.5, inp3_2 = 0.15, out=0.1785
    graph0.add_node(graph0_0, kwargs=['inp1_1', 'inp1_2'], outputs=['inp2_1'])
    graph0.add_node(f_my_function2, inputs=['inp2_1'], outputs=['inp3_2'])
    graph0.add_node(graph0_1,
                    kwargs=[('inp1', 'inp1_1'), ('inp2', 'inp3_2')],
                    outputs=['out'])

    graph0_0.add_node(f_my_function1,
                      inputs=['inp1_1', 'inp1_2'],
                      outputs=['inp2_1'])
    graph0_0.add_node(f_my_function2, inputs=['inp2_1'], outputs=['inp3_2'])
    graph0_0.add_node(f_my_function3,
                      inputs=[('inp3_1', 'inp1_1'), 'inp3_2'],
                      outputs=['out'])

    # inp2_1 = 0.215, inp3_2 = 0.0215, out = 0.1785
    graph0_1_0.add_node(f_my_function1,
                        inputs=['inp1_1', 'inp1_2'],
                        outputs=['inp2_1'])
    graph0_1_0.add_node(f_my_function2, inputs=['inp2_1'], outputs=['inp3_2'])
    graph0_1_0.add_node(f_my_function3,
                        inputs=[('inp3_1', 'inp1_1'), 'inp3_2'],
                        outputs=['out'])

    # out1 = 0.2, out2 = 0.015, out=0.1785
    graph0_1.add_node(f_my_function2, inputs=['inp1'], outputs=['out1'])
    graph0_1.add_node(f_my_function2, inputs=['inp2'], outputs=['out2'])
    graph0_1.add_node(graph0_1_0,
                      kwargs=[('inp1_1', 'out1'), ('inp1_2', 'out2')],
                      outputs=['out3'])

    for _ in range(2):
        res = graph0(inp1_1=2, inp1_2=3)
        assert 0.1785 == pytest.approx(res)
        assert graph0.data['out'] == res
        assert graph0_0.data['out'] == 1.5
        assert graph0_1.data['out3'] == pytest.approx(0.1785)
        assert graph0_1_0.data['out'] == pytest.approx(0.1785)