<jupyter_start><jupyter_text>Draw Sankey of UK Energy System The first step involves importing all the necessary packages to crete the Sankey<jupyter_code>import pandas as pd
from floweaver import *
from floweaver.jupyter import show_sankey<jupyter_output><empty_output><jupyter_text>Then we proceed to load the data that we want to draw. In one worksheet we have all the flows, while in the other worksheet we have information about each process in the diagram.<jupyter_code>FILENAME = './UK_Energy_Sankey_data.xlsx'
processes = pd.read_excel(FILENAME, sheetname='Processes', index_col=0)
flows = pd.read_excel(FILENAME, sheetname='Flows')<jupyter_output><empty_output><jupyter_text>We need to make sure that there is no repetition of flows in the data. Therefore we group and sum all flows.<jupyter_code>flows = flows.groupby(['source', 'target', 'type'], as_index=False).sum()<jupyter_output><empty_output><jupyter_text>We now create the variable which will be used to plot the diagram<jupyter_code>dataset = Dataset(flows, dim_process=processes)
source_grouping = Partition.Simple(
    'process',
    ['Gas',
     'Liquid fuel',
     'Coal',
     'Nuclear',
     'Solid biomass & Waste',
     'Renewable',
     'Primary Electricity',
     'Heat',
     'Electricity'
])

sector_grouping = Partition.Simple(
    'process',
    ['Road','Rail','Navigation','Aviation',
     'Industry',
     'Commercial and public services',
     'Residential',
     'Agriculture/forestry'
])

EndUse_grouping =Partition.Simple(
    'process',
    ['Mechanical',
     'Process Heat-Direct',
     'Process Heat-Boiler',
     'Space Heat',
     'Hot Water', 
     'Space Cooling',
     'Process Cooling',
     'Light',
     'Information'
])

device_grouping1 = Partition.Simple(
    'process',
    ['Boiler','Burner','Electric Heater','Spark Ignition Engine',
     'Diesel Engine',
     'Gas Turbine',
     'Electric Motor',    
     'Cooler',
     'Light Device',
     'Electronics'
    ])

useful_grouping = Partition.Simple(
    'process', ['Heating',
                'Motion',
                'Cool',
                'Illumination',
                'Information',
                'Loss'
    ])

trans_grouping = Partition.Simple(
    'process', ['Transformation','Heat Plant','Power Plant'])<jupyter_output><empty_output><jupyter_text>TODO:- make a two-band diagram with the losses shown underneath- adjust to make layout neater<jupyter_code>default_grouping =  Partition.Simple('process', processes.index.values)

nodes = {
    'inputs':       ProcessGroup(selection="Level == 1", partition=source_grouping),
    'Power Plants': ProcessGroup(selection="Level == 11",partition=trans_grouping),
    'Devices':      ProcessGroup(selection='Level == 2', partition=device_grouping1),
    'Useful':       ProcessGroup(selection='Level == 3', partition=useful_grouping),
}

order = [
    ['inputs'],
    ['Power Plants'],
    ['Devices'],
    ['Useful']
]

bundles = [
    Bundle('inputs', 'Devices'),
    Bundle('inputs', 'Power Plants'),
    Bundle('Power Plants', 'Devices'),
    Bundle('Power Plants', 'Useful'),
    Bundle('Devices','Useful'),
]

vd = SankeyDefinition(nodes, bundles, order, flow_partition=dataset.partition('type'))
#from palettable import qualitative
#palette = qualitative.Pastel1_8.hex_colors
from collections import defaultdict
palette = defaultdict(lambda: '#999')
palette['Coal'] = 'black'
palette['Liquid fuel'] = 'red'
palette['Gas'] = 'blue'
palette['Solid biomass & Waste'] = 'Green'
palette['Primary Electricity'] = 'Green'
palette['Biogas'] = 'Green'
palette['Renewable'] = 'Green'
palette['Nuclear'] = 'darkviolet'
palette['Wind'] = 'aquamarine'
palette['Hydro'] = 'aquamarine'
palette['Solar PV'] = 'aquamarine'
palette['Waste'] = 'black'
palette['Electricity'] = 'yellow'
palette['Heat'] = 'orange'
palette['Loss'] = 'gray'
palette['Renewable Heat'] = 'orange'
palette['Fuel'] = 'black'
show_sankey(vd, dataset, width=900, height=600,palette=palette).auto_save_svg('Uk_Sankey_Template.svg')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14268020807530343, 'fraction_numerical': 0.004706465196928412, 'mean_word_length': 4.56198347107438, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '317618', 'license': 'MIT', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1281, 'n_words': 300, 'path': 'examples/UK_Energy_Sankey_Solved.ipynb', 'repo_name': 'konstantinstadler/floweaver', 'size': '4037'}