Exemplo n.º 1
0
def test_results_graph_bands():
    bundles = [Bundle('a', 'b'), ]

    # Mock flow data
    bundle_flows = {
        bundles[0]: pd.DataFrame.from_records(
            [
                ('a1', 'b1', 'm', 3),
            ],
            columns=('source', 'target', 'material', 'value'))
    }

    view_graph = LayeredGraph()
    view_graph.add_node('a', node=ProcessGroup())
    view_graph.add_node('b', node=ProcessGroup())
    view_graph.add_edges_from([('a', 'b', {'bundles': bundles}), ])

    view_graph.ordering = Ordering([
        [['a'], []],
        [[], ['b']],
    ])

    # Do partition based on flows stored in bundles
    Gr, groups = results_graph(view_graph, bundle_flows)

    assert Gr.ordering == Ordering([
        # rank 1
        [['a^*'], []],
        # rank 2
        [[], ['b^*']],
    ])
Exemplo n.º 2
0
def test_results_graph_with_extra_or_not_enough_groups():
    # Mock flow data
    bundle_flows = {
        0: pd.DataFrame.from_records(
            [
                ('a1', 'b1', 'm', 3),
                ('a2', 'b1', 'm', 1),
            ],
            columns=('source', 'target', 'material', 'value'))
    }

    # Group 'a3' not used. ProcessGroup 'a2' isn't in any group.
    node_a = ProcessGroup(partition=Partition.Simple('process', ['a1', 'a3']))
    node_b = ProcessGroup(partition=Partition.Simple('process', ['b1']))
    view_graph = LayeredGraph()
    view_graph.add_node('a', node=node_a)
    view_graph.add_node('b', node=node_b)
    view_graph.add_edges_from([('a', 'b', {'bundles': [0]}), ])
    view_graph.ordering = Ordering([[['a']], [['b']]])

    # Do partition based on flows stored in bundles
    Gr, groups = results_graph(view_graph, bundle_flows)

    assert set(Gr.nodes()) == {'a^a1', 'a^_', 'b^b1'}
    assert sorted(Gr.edges(keys=True, data=True)) == [
        ('a^_', 'b^b1', ('*', '*'), {'value': 1, 'measures': {},
                                     'bundles': [0]}),
        ('a^a1', 'b^b1', ('*', '*'), {'value': 3, 'measures': {},
                                      'bundles': [0]}),
    ]

    assert Gr.ordering == Ordering([
        [['a^a1', 'a^_']],
        [['b^b1']],
    ])
Exemplo n.º 3
0
def test_remove_node():
    G = LayeredGraph()
    G.add_edges_from([('a', 'b'), ('a', 'c'), ('b', 'd')])
    G.ordering = Ordering([['a'], ['b', 'c'], ['d']])
    assert sorted(G.nodes()) == ['a', 'b', 'c', 'd']

    G.remove_node('c')
    assert sorted(G.nodes()) == ['a', 'b', 'd']
    assert G.ordering == Ordering([['a'], ['b'], ['d']])
Exemplo n.º 4
0
def test_dummy_nodes_left_LR():
    G = _twonodes(2, 'L', 0, 'R')
    assert set(G.nodes()) == {'x', 'y', '__x_y_0', '__x_y_1'}
    assert set(G.edges()) == {('x', '__x_y_1'), ('__x_y_1', '__x_y_0'),
                              ('__x_y_0', 'y')}
    assert G.ordering == Ordering([[['y', '__x_y_0']], [['__x_y_1']], [['x']]])

    G = _twonodes(1, 'L', 0, 'R')
    assert set(G.nodes()) == {'x', 'y', '__x_y_0'}
    assert set(G.edges()) == {('x', '__x_y_0'), ('__x_y_0', 'y')}
    assert G.ordering == Ordering([[['y', '__x_y_0']], [['x']]])
Exemplo n.º 5
0
def test_dummy_nodes_merge_bundles():
    G = LayeredGraph()
    G.add_node('a', node=ProcessGroup())
    G.add_node('b', node=ProcessGroup())
    G.ordering = Ordering([[['a']], [['b']]])

    G = add_dummy_nodes(G, 'a', 'b', bundle_key=1)
    assert G['a']['b']['bundles'] == [1]

    G = add_dummy_nodes(G, 'a', 'b', bundle_key=2)
    assert G['a']['b']['bundles'] == [1, 2]

    assert set(G.nodes()) == {'a', 'b'}
    assert set(G.edges()) == {('a', 'b')}
    assert G.ordering == Ordering([[['a']], [['b']]])
Exemplo n.º 6
0
def test_dummy_nodes_right_LL():
    G = _twonodes(0, 'L', 2, 'L')
    assert set(G.nodes()) == {'x', 'y', '__x_y_0', '__x_y_1', '__x_y_2'}
    assert set(G.edges()) == {('x', '__x_y_0'), ('__x_y_0', '__x_y_1'),
                              ('__x_y_1', '__x_y_2'), ('__x_y_2', 'y')}
    assert G.ordering == Ordering([[['__x_y_0', 'x']], [['__x_y_1']],
                                   [['__x_y_2', 'y']]])
Exemplo n.º 7
0
def _twonodes(xrank, xdir, yrank, ydir, **kwargs):
    G = LayeredGraph()
    G.add_node('x', node=ProcessGroup(direction=xdir))
    G.add_node('y', node=ProcessGroup(direction=ydir))
    layers = [[[]] for i in range(max(xrank, yrank) + 1)]
    layers[xrank][0].append('x')
    layers[yrank][0].append('y')
    G.ordering = Ordering(layers)
    kwargs.setdefault('bundle_key', None)
    return add_dummy_nodes(G, 'x', 'y', **kwargs)
Exemplo n.º 8
0
def _twonode_viewgraph():
    view_graph = LayeredGraph()
    view_graph.add_node('a', node=ProcessGroup())
    view_graph.add_node('b', node=ProcessGroup())
    view_graph.add_edge('a', 'b', {'bundles': [0]})
    view_graph.ordering = Ordering([
        [['a']],
        [['b']],
    ])
    return view_graph
Exemplo n.º 9
0
def test_dummy_nodes_order_dependence():
    #
    #  a   b
    #  c   d
    #
    # bundles a-b, c-d, b-a

    G = LayeredGraph()
    G.add_nodes_from('abcd', node=ProcessGroup())
    G.ordering = Ordering([[['a', 'c']], [['b', 'd']]])

    # Correct G.order: a-b, c-d, b-a
    G1 = _apply_bundles(G, ('ab', 'cd', 'ba'))
    assert G1.ordering == Ordering([[['a', '__b_a_0', 'c']],
                                    [['b', '__b_a_1', 'd']]])

    # Incorrect G.order: b-a first
    G2 = _apply_bundles(G, ('ba', 'ab', 'cd'))
    assert G2.ordering == Ordering([[['a', 'c', '__b_a_0']],
                                    [['b', '__b_a_1', 'd']]])
Exemplo n.º 10
0
def test_results_graph_time_partition():
    time_partition = Partition.Simple('time', [1, 2])

    view_graph = LayeredGraph()
    view_graph.add_node('a', node=ProcessGroup())
    view_graph.add_node('b', node=ProcessGroup())
    view_graph.add_edges_from([('a', 'b', {'bundles': [0]}), ])
    view_graph.ordering = Ordering([[['a']], [['b']]])

    # Mock flow data
    bundle_flows = {
        0: pd.DataFrame.from_records(
            [
                ('a1', 'b1', 'm', 1, 3),
                ('a2', 'b1', 'n', 1, 1),
                ('a2', 'b2', 'n', 1, 2),
                ('a1', 'b1', 'm', 2, 1),
                ('a1', 'b1', 'n', 2, 3),
            ],
            columns=('source', 'target', 'material', 'time', 'value')),
    }

    # Do partition based on flows stored in bundles
    Gr, groups = results_graph(view_graph,
                               bundle_flows,
                               time_partition=time_partition)
    assert sorted(Gr.edges(keys=True, data=True)) == [
        ('a^*', 'b^*', ('*', '1'), {'value': 6, 'measures': {},
                                    'bundles': [0]}),
        ('a^*', 'b^*', ('*', '2'), {'value': 4, 'measures': {},
                                    'bundles': [0]}),
    ]

    # Now add a material partition too
    material_partition = Partition.Simple('material', ['m', 'n'])
    Gr, groups = results_graph(view_graph, bundle_flows, material_partition,
                               time_partition)
    assert sorted(Gr.edges(keys=True, data=True)) == [
        ('a^*', 'b^*', ('m', '1'), {'value': 3, 'measures': {},
                                    'bundles': [0]}),
        ('a^*', 'b^*', ('m', '2'), {'value': 1, 'measures': {},
                                    'bundles': [0]}),
        ('a^*', 'b^*', ('n', '1'), {'value': 3, 'measures': {},
                                    'bundles': [0]}),
        ('a^*', 'b^*', ('n', '2'), {'value': 3, 'measures': {},
                                    'bundles': [0]}),
    ]
Exemplo n.º 11
0
def test_results_graph_material_key():
    # Mock flow data
    flows = pd.DataFrame.from_records(
        [
            ('a1', 'c1', 'm', 'long', 3),
            ('a1', 'c1', 'n', 'long', 1),
        ],
        columns=('source', 'target', 'material_type', 'shape', 'value'))

    view_graph = LayeredGraph()
    view_graph.add_node('a', node=ProcessGroup())
    view_graph.add_node('c', node=ProcessGroup())
    view_graph.add_edge('a', 'c', bundles=[0])
    view_graph.ordering = Ordering([[['a']], [['c']]])
    bundle_flows = {0: flows}

    material_partition = Partition.Simple('material_type', ['m', 'n'])
    shape_partition = Partition.Simple('shape', ['long', 'thin'])

    # Partition based on material_type
    view_graph.edge['a']['c']['flow_partition'] = material_partition
    Gr, groups = results_graph(view_graph, bundle_flows)
    assert sorted(Gr.edges(keys=True, data=True)) == [
        ('a^*', 'c^*', ('m', '*'), {'value': 3, 'measures': {},
                                    'bundles': [0]}),
        ('a^*', 'c^*', ('n', '*'), {'value': 1, 'measures': {},
                                    'bundles': [0]}),
    ]

    # Partition based on shape
    view_graph.edge['a']['c']['flow_partition'] = shape_partition
    Gr, groups = results_graph(view_graph, bundle_flows)
    assert sorted(Gr.edges(keys=True, data=True)) == [
        ('a^*', 'c^*', ('long', '*'), {'value': 4, 'measures': {},
                                       'bundles': [0]}),
    ]
Exemplo n.º 12
0
def test_dummy_nodes_left_RL():
    G = _twonodes(2, 'R', 0, 'L')
    assert set(G.nodes()) == {'x', 'y', '__x_y_1', '__x_y_2'}
    assert set(G.edges()) == {('x', '__x_y_2'), ('__x_y_2', '__x_y_1'),
                              ('__x_y_1', 'y')}
    assert G.ordering == Ordering([[['y']], [['__x_y_1']], [['x', '__x_y_2']]])
Exemplo n.º 13
0
def test_dummy_nodes_simple():
    G = _twonodes(0, 'R', 1, 'R', bundle_key=27)
    assert set(G.nodes()) == {'x', 'y'}
    assert set(G.edges()) == {('x', 'y')}
    assert G.ordering == Ordering([[['x']], [['y']]])
    assert G['x']['y']['bundles'] == [27]
Exemplo n.º 14
0
def test_results_graph_overall():
    material_partition = Partition.Simple('material', ['m', 'n'])
    c_partition = Partition.Simple('process', ['c1', 'c2'])

    view_graph = LayeredGraph()
    view_graph.add_node('a', node=ProcessGroup(title='Node a'))
    view_graph.add_node('b', node=ProcessGroup())
    view_graph.add_node('c', node=ProcessGroup(partition=c_partition))
    view_graph.add_node('via', node=Waypoint(partition=material_partition))
    view_graph.add_edges_from([
        ('a', 'via', {'bundles': [0],
                      'flow_partition': material_partition}),
        ('b', 'via', {'bundles': [1],
                      'flow_partition': material_partition}),
        ('via', 'c', {'bundles': [0, 1],
                      'flow_partition': material_partition}),
    ])
    view_graph.ordering = Ordering([[['a', 'b']], [['via']], [['c']]])

    # Mock flow data
    bundle_flows = {
        0: pd.DataFrame.from_records(
            [
                ('a1', 'c1', 'm', 3),
                ('a2', 'c1', 'n', 1),
            ],
            columns=('source', 'target', 'material', 'value')),
        1: pd.DataFrame.from_records(
            [
                ('b1', 'c1', 'm', 1),
                ('b1', 'c2', 'm', 2),
                ('b1', 'c2', 'n', 1),
            ],
            columns=('source', 'target', 'material', 'value'))
    }

    # Do partition based on flows stored in bundles
    Gr, groups = results_graph(view_graph, bundle_flows)

    assert sorted(Gr.nodes(data=True)) == [
        ('a^*', {'direction': 'R',
                 'type': 'process',
                 'title': 'Node a'}),
        ('b^*', {'direction': 'R',
                 'type': 'process',
                 'title': 'b'}),
        ('c^c1', {'direction': 'R',
                  'type': 'process',
                  'title': 'c1'}),
        ('c^c2', {'direction': 'R',
                  'type': 'process',
                  'title': 'c2'}),
        ('via^m', {'direction': 'R',
                   'type': 'group',
                   'title': 'm'}),
        ('via^n', {'direction': 'R',
                   'type': 'group',
                   'title': 'n'}),
    ]
    assert sorted(Gr.edges(keys=True, data=True)) == [
        ('a^*', 'via^m', ('m', '*'), {'value': 3, 'measures': {},
                                      'bundles': [0]}),
        ('a^*', 'via^n', ('n', '*'), {'value': 1, 'measures': {},
                                      'bundles': [0]}),
        ('b^*', 'via^m', ('m', '*'), {'value': 3, 'measures': {},
                                      'bundles': [1]}),
        ('b^*', 'via^n', ('n', '*'), {'value': 1, 'measures': {},
                                      'bundles': [1]}),
        ('via^m', 'c^c1', ('m', '*'), {'value': 4, 'measures': {},
                                       'bundles': [0, 1]}),
        ('via^m', 'c^c2', ('m', '*'), {'value': 2, 'measures': {},
                                       'bundles': [0, 1]}),
        ('via^n', 'c^c1', ('n', '*'), {'value': 1, 'measures': {},
                                       'bundles': [0, 1]}),
        ('via^n', 'c^c2', ('n', '*'), {'value': 1, 'measures': {},
                                       'bundles': [0, 1]}),
    ]

    assert Gr.ordering == Ordering([
        [['a^*', 'b^*']],
        [['via^m', 'via^n']],
        [['c^c1', 'c^c2']],
    ])

    assert groups == [
        {'id': 'a',
         'title': 'Node a',
         'type': 'process',
         'nodes': ['a^*']},
        {'id': 'b',
         'title': '',
         'type': 'process',
         'nodes': ['b^*']},
        {'id': 'via',
         'title': '',
         'type': 'group',
         'nodes': ['via^m', 'via^n']},
        {'id': 'c',
         'title': '',
         'type': 'process',
         'nodes': ['c^c1', 'c^c2']},
    ]