<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Colour-intensity scales
In this tutorial we will look at how to use colours in the Sankey diagram. We have already seen how to use a palette, but in this tutorial we will also create a Sankey where the intensity of the colour is proportional to a numerical value.
First step is to import all the required packages and data
Step1:  Now take a look at the dataset we are using. This is a very insightful [made-up] dataset about how different types of people lose weight while on holiday enjoying themselves.
Step2:  We now define the partitions of the data. Rather than listing the categories by hand, we use np.unique to pick out a list of the unique values that occur in the dataset.
Step3:  In fact, this is pretty common so there is a built-in function to do this
Step4:  We then go on to define the structure of our sankey. We define nodes, bundles and the order. In this case its pretty straightforward
Step5:  Now we will plot a Sankey that shows the share of time dedicated to each activity by each type of person.
Step6:  We can start using colour by specifying that we want to partition the flows according to type of person. Notice that this time we are using a pre-determined palette. 
You can find all sorts of palettes listed here.
Step7:  Now, if we want to make the colour of the flow to be proportional to a numerical value. Use the hue parameter to set the name of the variable that you want to display in colour. To start off, let's use "value", which is the width of the lines
Step8:  It's more interesting to use colour to show a different attribute from the flow table. But because a line in the Sankey diagram is an aggregation of multiple flows in the original data, we need to specify how the new dimension will be aggregated. For example, we'll use the mean of the flows within each Sankey link to set the colour. In this case we will use the colour to show how much each type of person emjoys each activity. We can be interested in either the cumulative enjoyment, or the mean enjoyment
Step9:  You can change the colour palette using the palette attribute. The palette names are different from before, because those were categorical (or qualitative) scales, and this is now a sequential scale. The palette names are listed here.
Step10:  It is possible to create a colorbar / scale to show the range of intensity values, but it's not currently as easy as it should be. This should be improved in future.
More customisation
You can subclass the QuantitativeScale class to get more control over the colour scale.
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
import numpy as np
from floweaver import *
df1 = pd.read_csv('holiday_data.csv')
Explanation: Colour-intensity scales
In this tutorial we will look at how to use colours in the Sankey diagram. We have already seen how to use a palette, but in this tutorial we will also create a Sankey where the intensity of the colour is proportional to a numerical value.
First step is to import all the required packages and data:
End of explanation
dataset = Dataset(df1)
df1
Explanation: Now take a look at the dataset we are using. This is a very insightful [made-up] dataset about how different types of people lose weight while on holiday enjoying themselves.
End of explanation
partition_job = Partition.Simple('Employment Job', np.unique(df1['Employment Job']))
partition_activity = Partition.Simple('Activity', np.unique(df1['Activity']))
Explanation: We now define the partitions of the data. Rather than listing the categories by hand, we use np.unique to pick out a list of the unique values that occur in the dataset.
End of explanation
# these statements or the ones above do the same thing
partition_job = dataset.partition('Employment Job')
partition_activity = dataset.partition('Activity')
Explanation: In fact, this is pretty common so there is a built-in function to do this:
End of explanation
nodes = {
    'Activity': ProcessGroup(['Activity'], partition_activity),
    'Job': ProcessGroup(['Employment Job'], partition_job),
}
bundles = [
    Bundle('Activity', 'Job'),
]
ordering = [
    ['Activity'],
    ['Job'],
]
Explanation: We then go on to define the structure of our sankey. We define nodes, bundles and the order. In this case its pretty straightforward:
End of explanation
# These are the same each time, so just write them here once
size_options = dict(width=500, height=400,
                    margins=dict(left=100, right=100))
sdd = SankeyDefinition(nodes, bundles, ordering)
weave(sdd, dataset, measures='Calories Burnt').to_widget(**size_options)
Explanation: Now we will plot a Sankey that shows the share of time dedicated to each activity by each type of person.
End of explanation
sdd = SankeyDefinition(nodes, bundles, ordering, flow_partition=partition_job)
weave(sdd, dataset, palette='Set2_8', measures='Calories Burnt').to_widget(**size_options)
Explanation: We can start using colour by specifying that we want to partition the flows according to type of person. Notice that this time we are using a pre-determined palette. 
You can find all sorts of palettes listed here.
End of explanation
weave(sdd, dataset, link_color=QuantitativeScale('Calories Burnt'), measures='Calories Burnt').to_widget(**size_options)
Explanation: Now, if we want to make the colour of the flow to be proportional to a numerical value. Use the hue parameter to set the name of the variable that you want to display in colour. To start off, let's use "value", which is the width of the lines: wider lines will be shown in a darker colour.
End of explanation
weave(sdd, dataset, measures={'Calories Burnt': 'sum', 'Enjoyment': 'mean'}, link_width='Calories Burnt',
      link_color=QuantitativeScale('Enjoyment')).to_widget(**size_options)
weave(sdd, dataset, measures={'Calories Burnt': 'sum', 'Enjoyment': 'mean'}, link_width='Calories Burnt',
      link_color=QuantitativeScale('Enjoyment', intensity='Calories Burnt')).to_widget(**size_options)
Explanation: It's more interesting to use colour to show a different attribute from the flow table. But because a line in the Sankey diagram is an aggregation of multiple flows in the original data, we need to specify how the new dimension will be aggregated. For example, we'll use the mean of the flows within each Sankey link to set the colour. In this case we will use the colour to show how much each type of person emjoys each activity. We can be interested in either the cumulative enjoyment, or the mean enjoyment: try both!
Aggregation is specified with the measures parameter, which should be set to a dictionary mapping dimension names to aggregation functions ('mean', 'sum' etc).
End of explanation
scale = QuantitativeScale('Enjoyment', palette='Blues_9')
weave(sdd, dataset, 
      measures={'Calories Burnt': 'sum', 'Enjoyment': 'mean'}, 
      link_width='Calories Burnt',
      link_color=scale) \
    .to_widget(**size_options)
scale.domain
Explanation: You can change the colour palette using the palette attribute. The palette names are different from before, because those were categorical (or qualitative) scales, and this is now a sequential scale. The palette names are listed here.
End of explanation
class MyScale(QuantitativeScale):
    def get_palette(self, link):
        # Choose colour scheme based on link type (here, Employment Job)
        name = 'Greens_9' if link.type == 'Student' else 'Blues_9'
        return self.lookup_palette_name(name)
        
    def get_color(self, link, value):
        palette = self.get_palette(link)
        return palette(0.2 + 0.8*value)
my_scale = MyScale('Enjoyment', palette='Blues_9')
weave(sdd, dataset, 
      measures={'Calories Burnt': 'sum', 'Enjoyment': 'mean'}, 
      link_width='Calories Burnt',
      link_color=my_scale) \
    .to_widget(**size_options)
Explanation: It is possible to create a colorbar / scale to show the range of intensity values, but it's not currently as easy as it should be. This should be improved in future.
More customisation
You can subclass the QuantitativeScale class to get more control over the colour scale.
End of explanation
<END_TASK>