def test_view_graph_does_non_dummy_bundles_first(): """It's important to do bundles that don't require adding dummy nodes first, so when it comes to return loops, they are better placed.""" nodes = { 'a': ProcessGroup(selection=('a', )), 'b': ProcessGroup(selection=('b', )), 'c': ProcessGroup(selection=('c', )), 'd': ProcessGroup(selection=('d', )), } order = [[['a', 'c']], [['b', 'd']]] bundles = [ Bundle('a', 'b'), Bundle('c', 'd'), Bundle('b', 'a'), ] G = view_graph(SankeyDefinition(nodes, bundles, order)) assert G.ordering == Ordering([ [['a', '__b_a_0', 'c']], [['b', '__b_a_1', 'd']], ]) # order of bundles doesn't affect it G2 = view_graph(SankeyDefinition(nodes, bundles[::-1], order)) assert G2.ordering == G.ordering
def test_view_graph_does_short_bundles_last(): """Return loops are inserted immediately below the source node, so work from the outside in.""" # # ,a -- b -- c-, # | `----`| # `-----------' # nodes = { 'a': ProcessGroup(selection=('a', )), 'b': ProcessGroup(selection=('b', )), 'c': ProcessGroup(selection=('c', )), } order = [[['a']], [['b']], [['c']]] bundles = [ Bundle('a', 'b'), Bundle('b', 'c'), Bundle('c', 'b'), Bundle('c', 'a'), ] G = view_graph(SankeyDefinition(nodes, bundles, order)) assert G.ordering == Ordering([ [['a', '__c_a_0']], [['b', '__c_b_1', '__c_a_1']], [['c', '__c_b_2', '__c_a_2']], ]) # order of bundles doesn't affect it G2 = view_graph(SankeyDefinition(nodes, bundles[::-1], order)) assert G.ordering == G2.ordering
def test_augment_waypoint_alignment(): # j -- a -- x # b # k -- c -- y # # should insert "from b" betwen x and y # and "to b" between j and k G = LayeredGraph() G.add_nodes_from([ ('a', {'node': ProcessGroup()}), ('b', {'node': ProcessGroup(selection=['b1'])}), ('c', {'node': ProcessGroup()}), ('x', {'node': ProcessGroup()}), ('y', {'node': ProcessGroup()}), ('j', {'node': ProcessGroup()}), ('k', {'node': ProcessGroup()}), ]) G.add_edges_from([ ('a', 'x', {'bundles': [2]}), ('k', 'c', {'bundles': [1]}), ('j', 'a', {'bundles': [0]}), ('c', 'y', {'bundles': [3]}), ]) G.ordering = Ordering([[['j', 'k']], [['a', 'b', 'c']], [['x', 'y']]]) new_waypoints = { 'from b': Waypoint(), 'to b': Waypoint(), } new_bundles = { 'b>': Bundle('b', Elsewhere, waypoints=['from b']), '>b': Bundle(Elsewhere, 'b', waypoints=['to b']), } G2 = augment(G, new_waypoints, new_bundles) assert set(G2.nodes()).difference(G.nodes()) == {'from b', 'to b'} assert G2.ordering == Ordering([ [['j', 'to b', 'k']], [['a', 'b', 'c']], [['x', 'from b', 'y']] ])
def test_view_graph_adds_waypoints(): nodes = { 'n1': ProcessGroup(selection=['n1']), 'n2': ProcessGroup(selection=['n2']), 'w1': Waypoint(), } bundles = [ Bundle('n1', 'n2', waypoints=['w1']), ] order0 = [['n1'], [], ['w1'], [], [], ['n2']] G = view_graph(SankeyDefinition(nodes, bundles, order0)) assert sorted(nodes_ignoring_elsewhere(G, data=True)) == [ ('__n1_w1_1', { 'node': Waypoint(title='') }), ('__w1_n2_3', { 'node': Waypoint(title='') }), ('__w1_n2_4', { 'node': Waypoint(title='') }), ('n1', { 'node': ProcessGroup(selection=['n1']) }), ('n2', { 'node': ProcessGroup(selection=['n2']) }), ('w1', { 'node': Waypoint() }), ] assert sorted(edges_ignoring_elsewhere(G, data=True)) == [ ('__n1_w1_1', 'w1', { 'bundles': [0] }), ('__w1_n2_3', '__w1_n2_4', { 'bundles': [0] }), ('__w1_n2_4', 'n2', { 'bundles': [0] }), ('n1', '__n1_w1_1', { 'bundles': [0] }), ('w1', '__w1_n2_3', { 'bundles': [0] }), ] assert G.ordering == Ordering([[['n1']], [['__n1_w1_1']], [['w1']], [['__w1_n2_3']], [['__w1_n2_4']], [['n2']]])
def test_view_graph_does_not_mutate_definition(): nodes = { 'n1': ProcessGroup(selection=['n1']), 'n2': ProcessGroup(selection=['n2']), } bundles = [ Bundle('n1', 'n2'), ] order0 = [['n1'], [], ['n2']] vd = SankeyDefinition(nodes, bundles, order0) G = view_graph(vd) assert vd.nodes == { 'n1': ProcessGroup(selection=['n1']), 'n2': ProcessGroup(selection=['n2']), } assert vd.bundles == { 0: Bundle('n1', 'n2'), } assert vd.ordering == Ordering([[['n1']], [[]], [['n2']]])
def test_weave_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']]] sdd = 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) result = weave(sdd, dataset) def link(src, tgt, original_flows, value, link_type='*', color='#FBB4AE'): return SankeyLink(source=src, target=tgt, type=link_type, time='*', data={'value': value}, title=link_type, color=color, original_flows=original_flows) assert set(n.id for n in result.nodes) == { 'a^*', 'b^*', 'via^m', 'via^n', 'c^c1', 'c^c2' } assert sorted(result.links) == [ link('a^*', 'via^m', [0], 3), link('a^*', 'via^n', [1], 1), link('b^*', 'via^m', [2, 3], 3), link('b^*', 'via^n', [4], 1), link('via^m', 'c^c1', [0, 2], 4), link('via^m', 'c^c2', [3], 2), link('via^n', 'c^c1', [1], 1), link('via^n', 'c^c2', [4], 1), ] assert result.ordering == Ordering([ [['a^*', 'b^*']], [['via^m', 'via^n']], [['c^c1', 'c^c2']], ]) assert result.groups == [ { '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 sdd2 = SankeyDefinition(nodes, bundles, ordering, flow_partition=Partition.Simple( 'material', ['m', 'n'])) scale = CategoricalScale('type', palette=['red', 'blue']) scale.set_domain(['m', 'n']) result = weave(sdd2, dataset, link_color=scale) assert sorted(result.links) == [ link('a^*', 'via^m', [0], 3, 'm', 'red'), link('a^*', 'via^n', [1], 1, 'n', 'blue'), link('b^*', 'via^m', [2, 3], 3, 'm', 'red'), link('b^*', 'via^n', [4], 1, 'n', 'blue'), link('via^m', 'c^c1', [0, 2], 4, 'm', 'red'), link('via^m', 'c^c2', [3], 2, 'm', 'red'), link('via^n', 'c^c1', [1], 1, 'n', 'blue'), link('via^n', 'c^c2', [4], 1, 'n', 'blue'), ]