ricklupton / floweaver

View flow data as Sankey diagrams
https://floweaver.readthedocs.io
MIT License
449 stars 88 forks source link

Flow Groupings #5

Open charlieselway opened 6 years ago

charlieselway commented 6 years ago

Hi, I'm having trouble with my flows when I want them to miss a stage in the diagram. In the image below, for example, I'd like Polyethylene to join directly to Thermoplastics, instead of grouping together with the Urea and other flows in the stage in between. I bundle each stage with each other stage so wondered if this might be the issue, but can't see a way to create the diagram without this. I've copied excerpts of my code below too.

sankey grouping

nodes = { 'Source': ProcessGroup(['Oil', 'Natural Gas', 'Refinery Sourced', 'Secondary Reactants']), 'Primaries': ProcessGroup(['Ammonia', 'Methyl Alcohol', 'Ethylene', 'Propylene', 'C4 Stream', 'BTX aromatics']), 'First-Tier': ProcessGroup(['Urea', 'Ammonium Phosphate', 'Ammonium Sulphate', 'Nitric Acid', 'Polyethylene', 'Vinyl Chloride', 'Polypropylene', 'Styrene', 'Ethylene Glycol', 'Terephthalic Acid']), 'Second-Tier': ProcessGroup(['Ammonium Nitrate', 'Polyvinyl Chloride', 'Polystyrene', 'Polystyrene', 'Polyethylene Terephthalate']), 'End Products': ProcessGroup(['N-fertilisers', 'Thermoplastics', 'Thermosets, Fibre & Elastomers', 'Solvents, Additives & Explosives', 'Other', 'Secondary Products']), }

ordering = [ ['Source'], ['Primaries'], ['First-Tier'], ['Second-Tier'], ['End Products'], ]

bundles = [ Bundle('Source', 'Primaries'), Bundle('Primaries', 'First-Tier'), Bundle('First-Tier', 'Second-Tier'), Bundle('Source', 'First-Tier'), Bundle('Source', 'Second-Tier'), Bundle('Source', 'End Products'), Bundle('Primaries', 'End Products'), Bundle('First-Tier', 'End Products'), Bundle('Second-Tier', 'End Products'), ]

nodes['Source'].partition = sources_by_name nodes['Primaries'].partition = primaries_by_name nodes['First-Tier'].partition = firsttier_by_name nodes['Second-Tier'].partition = secondtier_by_name nodes['End Products'].partition = endproducts_by_name

Cheers

ricklupton commented 6 years ago

Thanks for reporting this complete with picture & code! This is a gap in the documentation we should fill.

The issue is the Bundle('First-Tier', 'End Products'), which has an automatic waypoint added above Amonium Nitrate in order to pass through the Second-Tier layer. The default for automatic waypoints is to have no partition set -- so everything is grouped together as you see.

You've got a couple of options:

  1. Change the default partition of the Bundle like this:

    Bundle('First-Tier', 'End Products', default_partition=partition_you_want)
  2. Add a Waypoint that you can control directly -- including setting the partition -- and route the Bundle via it:

    Bundle('First-Tier', 'End Products', waypoints=['your_waypoint'])
    
    ordering = [
       ['Source'],
       ['Primaries'],
       ['First-Tier'],
       ['your_waypoint', 'Second-Tier'],
       ['End Products'],
    ]
    # also add 'your_waypoint' to nodes

Hope that helps. I'll leave the issue open as a reminder to add something to the documentation about this (if you figure it out, feel free to write it up as a short notebook to add to the tutorials or cookbook section!)