Esempio n. 1
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']],
    ])
Esempio n. 2
0
def test_view_graph_bundle_flow_partitions_must_be_equal():
    material_partition_mn = Partition.Simple('material', ['m', 'n'])
    material_partition_XY = Partition.Simple('material', ['X', 'Y'])
    nodes = {
        'a': ProcessGroup(selection=['a1']),
        'b': ProcessGroup(selection=['b1']),
        'c': ProcessGroup(selection=['c1']),
        'via': Waypoint(),
    }
    order = [['a', 'b'], ['via'], ['c']]
    bundles = [
        Bundle('a',
               'c',
               waypoints=['via'],
               flow_partition=material_partition_mn),
        Bundle('b',
               'c',
               waypoints=['via'],
               flow_partition=material_partition_XY),
    ]

    # Do partition based on flows stored in bundles
    with pytest.raises(ValueError):
        G = view_graph(SankeyDefinition(nodes, bundles, order))

    bundles[1] = Bundle('b',
                        'c',
                        waypoints=['via'],
                        flow_partition=material_partition_mn)
    assert view_graph(SankeyDefinition(nodes, bundles, order))
Esempio n. 3
0
def test_simple_partition_groups():
    G = Partition.Simple('dim1', ['x', ('group', ['y', 'z'])])
    assert G.labels == ['x', 'group']
    assert G.groups == (
        Group('x', [('dim1', ('x', ))]),
        Group('group', [('dim1', ('y', 'z'))]),
    )
Esempio n. 4
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]}),
    ]
Esempio n. 5
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]}),
    ]
Esempio n. 6
0
def test_sankey_view_results_time_partition():
    nodes = {
        'a': ProcessGroup(selection=['a1']),
        'b': ProcessGroup(selection=['b1']),
    }
    bundles = [Bundle('a', 'b')]
    ordering = [[['a']], [['b']]]
    time_partition = Partition.Simple('time', [1, 2])
    vd = SankeyDefinition(nodes,
                          bundles,
                          ordering,
                          time_partition=time_partition)

    # Dataset
    flows = pd.DataFrame.from_records([
        ('a1', 'b1', 'm', 1, 3),
        ('a1', 'b1', 'm', 2, 2),
    ],
                                      columns=('source', 'target', 'material',
                                               'time', 'value'))
    dim_process = pd.DataFrame({'id': ['a1', 'b1']}).set_index('id')
    dataset = Dataset(flows, dim_process)

    GR, groups = sankey_view(vd, dataset)
    assert set(GR.nodes()) == {'a^*', 'b^*'}
    assert sorted(GR.edges(keys=True, data=True)) == [
        ('a^*', 'b^*', ('*', '1'), {
            'value': 3,
            'measures': {},
            'bundles': [0]
        }),
        ('a^*', 'b^*', ('*', '2'), {
            'value': 2,
            'measures': {},
            'bundles': [0]
        }),
    ]
    assert GR.ordering == Ordering([[['a^*']], [['b^*']]])
Esempio n. 7
0
def test_view_graph_adds_waypoints_partition():
    nodes = {
        'n1': ProcessGroup(selection=['n1']),
        'n2': ProcessGroup(selection=['n2']),
    }
    g = Partition.Simple('test', ['x'])
    bundles = [
        Bundle('n1', 'n2', default_partition=g),
    ]
    order0 = [['n1'], [], ['n2']]
    G = view_graph(SankeyDefinition(nodes, bundles, order0))

    assert sorted(nodes_ignoring_elsewhere(G, data=True)) == [
        ('__n1_n2_1', {
            'node': Waypoint(title='', partition=g)
        }),
        ('n1', {
            'node': ProcessGroup(selection=['n1'])
        }),
        ('n2', {
            'node': ProcessGroup(selection=['n2'])
        }),
    ]
Esempio n. 8
0
def test_partition_simple_checks_for_duplicates():
    with pytest.raises(ValueError):
        Partition.Simple('dim1', ['a', 'a'])

    with pytest.raises(ValueError):
        Partition.Simple('dim1', [('label1', ['a', 'b']), 'b'])
Esempio n. 9
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']},
    ]
Esempio n. 10
0
def test_sankey_view_results():
    nodes = {
        'a':
        ProcessGroup(selection=['a1', 'a2']),
        'b':
        ProcessGroup(selection=['b1']),
        'c':
        ProcessGroup(selection=['c1', 'c2'],
                     partition=Partition.Simple('process', ['c1', 'c2'])),
        'via':
        Waypoint(partition=Partition.Simple('material', ['m', 'n'])),
    }
    bundles = [
        Bundle('a', 'c', waypoints=['via']),
        Bundle('b', 'c', waypoints=['via']),
    ]
    ordering = [[['a', 'b']], [['via']], [['c']]]
    vd = SankeyDefinition(nodes, bundles, ordering)

    # Dataset
    flows = pd.DataFrame.from_records([
        ('a1', 'c1', 'm', 3),
        ('a2', 'c1', 'n', 1),
        ('b1', 'c1', 'm', 1),
        ('b1', 'c2', 'm', 2),
        ('b1', 'c2', 'n', 1),
    ],
                                      columns=('source', 'target', 'material',
                                               'value'))
    dim_process = pd.DataFrame({
        'id':
        list(flows.source.unique()) + list(flows.target.unique())
    }).set_index('id')
    dataset = Dataset(flows, dim_process)

    GR, groups = sankey_view(vd, dataset)

    assert set(GR.nodes()) == {'a^*', 'b^*', 'via^m', 'via^n', 'c^c1', 'c^c2'}
    assert sorted(GR.edges(keys=True, data=True)) == [
        ('a^*', 'via^m', ('*', '*'), {
            'value': 3,
            'measures': {},
            'bundles': [0]
        }),
        ('a^*', 'via^n', ('*', '*'), {
            'value': 1,
            'measures': {},
            'bundles': [0]
        }),
        ('b^*', 'via^m', ('*', '*'), {
            'value': 3,
            'measures': {},
            'bundles': [1]
        }),
        ('b^*', 'via^n', ('*', '*'), {
            'value': 1,
            'measures': {},
            'bundles': [1]
        }),
        ('via^m', 'c^c1', ('*', '*'), {
            'value': 4,
            'measures': {},
            'bundles': [0, 1]
        }),
        ('via^m', 'c^c2', ('*', '*'), {
            'value': 2,
            'measures': {},
            'bundles': [0, 1]
        }),
        ('via^n', 'c^c1', ('*', '*'), {
            'value': 1,
            'measures': {},
            'bundles': [0, 1]
        }),
        ('via^n', 'c^c2', ('*', '*'), {
            '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': '',
            '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']
        },
    ]

    # Can also set flow_partition for all bundles at once
    vd2 = SankeyDefinition(nodes,
                           bundles,
                           ordering,
                           flow_partition=Partition.Simple(
                               'material', ['m', 'n']))
    GR, groups = sankey_view(vd2, dataset)
    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]
        }),
    ]