def show_sample(processes,
                possible_inputs,
                trace,
                isamp,
                sdd,
                widget=None,
                burn=500):
    if isamp is None:
        I = trace['inputs', burn:].mean(axis=0)
        F = trace['F', burn:].mean(axis=0)
        dataset = Dataset(
            inputs_flows_as_dataframe(processes, possible_inputs, I, F))
    else:
        I = trace['inputs'][isamp]
        F = trace['F'][isamp]
        dataset = Dataset(
            inputs_flows_as_dataframe(processes, possible_inputs, I, F))
    new_widget = weave(sdd, dataset).to_widget(width=600,
                                               height=300,
                                               margins=dict(left=50,
                                                            right=100,
                                                            top=10,
                                                            bottom=10))
    if widget is None:
        return new_widget
    else:
        widget.value = new_widget.value
Example #2
0
 def get_sdd(self):
     tmp_flows, nodes, bundles, orderings, df_piv = get_node_order_bundle(
         self.df, self.index_widget.value, self.column_widget.value,
         self.value_widget.value)
     flow_partition = nodes[self.column_level_widget.value].partition
     y_list = np.sort(
         df_piv[self.column_level_widget.value].unique()).tolist()
     palette = get_palette(y_list, self.value_level_widget.value,
                           self.target_color, self.base_color)
     sdd = SankeyDefinition(nodes,
                            bundles,
                            orderings,
                            flow_partition=flow_partition)
     self.flows = tmp_flows
     self.palette = palette
     self.size = dict(width=self.width, height=self.height)
     if self.multiple_display_widget.value == "No" and self.display_flag is True:
         display(self.multiple_display_widget)
         display(self.path_widget)
         display(self.index_widget)
         display(self.column_widget)
         display(self.value_widget)
         display(self.column_level_widget)
         display(self.value_level_widget)
     print("file: {}, index: {}, date: {} is {}, target: {} is {}".format(
         self.path_widget.value, self.index_widget.value,
         self.column_widget.value, self.column_level_widget.value,
         self.value_widget.value, self.value_level_widget.value))
     display(
         weave(sdd, self.flows,
               palette=self.palette).to_widget(**self.size))
     if self.multiple_display_widget.value == "No":
         clear_output(wait=True)
     self.display_flag = True
     return sdd
def weave_variance(flows, sdd, normed=False, vlim=None, palette=None):
    if palette is None:
        palette = sequential.Reds_9.mpl_colormap

    # Aggregate
    def measures(group):
        agg = group.groupby('sample').agg({'value': 'sum'})
        return {'value': agg['value'].values}

    def link_width(data):
        return data['value'].mean()

    scale = (NormalisedHPDRangeScale('value', palette=palette)
             if normed else AbsoluteHPDRangeScale('value', palette=palette))

    if vlim is not None:
        scale.set_domain(vlim)

    result = weave(sdd,
                   flows,
                   measures=measures,
                   link_width=link_width,
                   link_color=scale)

    return result, scale.get_domain()
Example #4
0
def plot_sankey_sector(data, com_coord, save=False):
    
    paths       = prep.read_yaml('conf','paths')
    sector_list = prep.read_yaml('conf','subsets_criteria')
    for sector in sector_list.keys():
        dic = sector_list[sector]
        
        data_sel = filter_sector_subset(data, dic['on_fields'], dic['off_fields'])

        #Selecting only the desired subset of producers
        if data_sel.shape[0] == 0:
            print('There are no producers in the requested subset: ', subset)
        else:
            #print('Dimension of the subset: ', data_sel.shape)
            flows = create_df_for_sankey(data_sel)
            ###https://github.com/psychemedia/parlihacks/blob/master/notebooks/MigrantFlow.ipynb)

            sdd = plot_sankey(flows, com_coord)
            ## New Sankey!
            size = dict(width=870, height=1000)
            
            name = paths['output'] + "sankeydiag_"+sector+".svg"

            wid = weave(sdd, flows, link_color=QuantitativeScale('value'), \
            measures='value').to_widget(**size)
            wid.auto_save_svg(name)
            display(wid)
def main(flows_datapackage, year, output_file):
    flows = load_dataframe(flows_datapackage, 'flows')
    data = weave(sdd, flows.query('year == @year'), palette=palette).to_json()

    data['metadata']['title'] = str(year)

    with open(output_file, 'wt') as f:
        json.dump(data, f)
 def update(isamp):
     new_widget = weave(sdd,
                        dataset(isamp)).to_widget(width=600,
                                                  height=300,
                                                  margins=dict(left=50,
                                                               right=100,
                                                               top=10,
                                                               bottom=10))
     widget.links = new_widget.links
Example #7
0
 def on_clear_output(self, change):
     flag = change["new"]
     if self.sdd is not None:
         if flag == "Yes":
             display(self.multiple_display_widget)
             display(self.path_widget)
             display(self.index_widget)
             display(self.column_widget)
             display(self.value_widget)
             display(self.column_level_widget)
             display(self.value_level_widget)
             print("file: {}, index: {}, date: {} is {}, target: {} is {}".
                   format(self.path_widget.value, self.index_widget.value,
                          self.column_widget.value,
                          self.column_level_widget.value,
                          self.value_widget.value,
                          self.value_level_widget.value))
             display(
                 weave(self.sdd, self.flows,
                       palette=self.palette).to_widget(**self.size))
         else:
             clear_output(wait=False)
             display(self.multiple_display_widget)
             display(self.path_widget)
             display(self.index_widget)
             display(self.column_widget)
             display(self.value_widget)
             display(self.column_level_widget)
             display(self.value_level_widget)
             print("file: {}, index: {}, date: {} is {}, target: {} is {}".
                   format(self.path_widget.value, self.index_widget.value,
                          self.column_widget.value,
                          self.column_level_widget.value,
                          self.value_widget.value,
                          self.value_level_widget.value))
             display(
                 weave(self.sdd, self.flows,
                       palette=self.palette).to_widget(**self.size))
             clear_output(wait=True)
Example #8
0
 def show_point(self, point, sdd):
     """Show Sankey diagram for trace point."""
     all_inputs = self.model.deterministics[0].eval({
         v: point[str(v)]
         for v in self.model.vars if str(v) == 'inputs_interval_'
     })
     flows = self.model.flows.eval({
         self.model.inputs_interval_:
         point['inputs_interval_'],
         self.model.params:
         point['params']
     })
     dataset = Dataset(
         inputs_flows_as_dataframe(self.processes, self.possible_inputs,
                                   all_inputs, flows))
     return weave(sdd, dataset).to_widget(width=900,
                                          height=400,
                                          margins=dict(left=bg50,
                                                       right=100,
                                                       top=10,
                                                       bottom=10))
def animate_samples(processes, possible_inputs, trace, sdd, rescale=False):
    def dataset(isamp):
        I = trace['inputs'][isamp]
        F = trace['F'][isamp]
        return Dataset(
            inputs_flows_as_dataframe(processes, possible_inputs, I, F))

    widget = weave(sdd, dataset(0)).to_widget(width=800,
                                              height=500,
                                              margins=dict(left=50,
                                                           right=100,
                                                           top=10,
                                                           bottom=10))
    button = ipywidgets.Button(description='Go')
    box = ipywidgets.VBox([button, widget])

    def update(isamp):
        new_widget = weave(sdd,
                           dataset(isamp)).to_widget(width=600,
                                                     height=300,
                                                     margins=dict(left=50,
                                                                  right=100,
                                                                  top=10,
                                                                  bottom=10))
        widget.links = new_widget.links

    def play(_):
        try:
            for i in range(0, len(trace), 10):
                update(i)
                if rescale:
                    widget.set_scale()
                time.sleep(0.5)
        except KeyboardInterrupt:
            return

    button.on_click(play)

    return box
Example #10
0
def plot_reclassification(
    path_nml: str,
    path_save="LC-WRF-SUEWS.png",
    width=800,
    height=360,
    top=10,
    bottom=10,
    left=260,
    right=60,
):
    """Produce Sankey Diagram to visualise the reclassification specified in `path_nml`

    Parameters
    ----------
    path_nml : str
        Path to `namelist.suews`
    path_save : str, optional
        Path to save Sankey diagram, by default 'LC-WRF-SUEWS.png'
    width : int, optional
        Width of diagram, by default 800
    height : int, optional
        Height of diagram, by default 360
    top : int, optional
        Top margin of diagram, by default 10
    bottom : int, optional
        Bottom margin of diagram, by default 10
    left : int, optional
        Left margin of diagram, by default 260
    right : int, optional
        Right margin of diagram, by default 60

    Returns
    -------
    Sankey Diagram
        Sankey Diagram showing the reclassification.
    """
    from floweaver import (
        Dataset,
        Partition,
        ProcessGroup,
        Waypoint,
        SankeyDefinition,
        weave,
    )

    # derive DataFrames required by Sankey
    df_flow, df_process = gen_df_sankey(path_nml)

    # set the default size to fit the documentation better.
    size = dict(width=width, height=height)
    margins = dict(top=top, bottom=bottom, left=left, right=right)

    # create Sankey data
    dataset = Dataset(df_flow, dim_process=df_process)
    # SUEWS LCs
    list_suews = df_flow.target.unique().tolist()
    # WRF LCs
    list_wrf = df_flow.source.unique().tolist()
    list_type = df_flow.type.unique().tolist()
    # LC types
    lc_by_type = Partition.Simple("type", list_type)

    nodes = {
        "SUEWS": ProcessGroup(list_suews),
        "WRF": ProcessGroup(list_wrf),
    }
    nodes["SUEWS"].partition = Partition.Simple("process", list_cat_suews)
    nodes["WRF"].partition = Partition.Simple("process", list_wrf)
    nodes["type"] = Waypoint(lc_by_type)
    ordering = [
        ["WRF"],  # put "WRF" on the left...
        ["type"],  # put "type" on the left...
        ["SUEWS"],  # ... and "SUEWS" on the right.
    ]
    bundles = [
        Bundle("WRF", "SUEWS", waypoints=["type"]),
    ]

    # Set the colours for the labels in the partition.
    palette = {
        "Built-up": "slategrey",
        "Bsoil": "tan",
        "Vegetated": "forestgreen",
        "Water": "royalblue",
    }

    sdd = SankeyDefinition(nodes, bundles, ordering, flow_partition=lc_by_type)

    data_sankey = weave(sdd, dataset, palette=palette)
    sankey = data_sankey.to_widget(**size, margins=margins)
    if in_ipynb():
        path_save = Path(path_save)
        suffix = path_save.suffix
        if suffix == "png":
            print("Saving figure in png:")
            sankey.auto_save_png(path_save)
        elif suffix == "svg":
            print("Saving figure in svg:")
            sankey.auto_save_svg(path_save)
        else:
            print("Saving figure in png: ")
            sankey.auto_save_png(path_save)
        print(path_save.resolve())
    else:
        print(
            "Please run this function in Jupyter notebook for visualisation.")

    return sankey
Example #11
0
def sankey_fw(frame):
    # FLOWS FOR USING FLOWEAVER
    df = frame.sum(1)
    flows = pd.DataFrame([['N', 'P', 'fossil', df[0]],
                          ['N', 'P', 'biomass', df[1]],
                          ['N', 'P', 'metal', df[2]],
                          ['N', 'P', 'nonmetal', df[3]],
                          ['P', 'E', 'fossil', df[4]],
                          ['P', 'E', 'biomass', df[5]],
                          ['P', 'E', 'metal', df[6]],
                          ['P', 'E', 'nonmetal', df[7]],
                          ['P', 'W', 'fossil', df[8]],
                          ['P', 'W', 'biomass', df[9]],
                          ['P', 'W', 'metal', df[10]],
                          ['P', 'W', 'nonmetal', df[11]],
                          ['P', 'S', 'fossil', df[12]],
                          ['P', 'S', 'biomass', df[13]],
                          ['P', 'S', 'metal', df[14]],
                          ['P', 'S', 'nonmetal', df[15]],
                          ['S', 'W', 'fossil', df[16]],
                          ['S', 'W', 'biomass', df[17]],
                          ['S', 'W', 'metal', df[18]],
                          ['S', 'W', 'nonmetal', df[19]],
                          ['W', 'G', 'fossil', df[20]],
                          ['W', 'G', 'biomass', df[21]],
                          ['W', 'G', 'metal', df[22]],
                          ['W', 'G', 'nonmetal', df[23]],
                          ['W', 'P', 'fossil', df[24]],
                          ['W', 'P', 'biomass', df[25]],
                          ['W', 'P', 'metal', df[26]],
                          ['W', 'P', 'nonmetal', df[27]]])
    flows.columns = ['source', 'target', 'type', 'value']
    # SETTINS
    size = dict(width=750, height=300)
    nodes = {
        'N': fw.ProcessGroup(['N']),
        'I&C': fw.ProcessGroup(['C']),
        'S': fw.ProcessGroup(['S']),
        'W': fw.ProcessGroup(['W']),
        'DPO': fw.ProcessGroup(['E', 'G']),
        'rec': fw.Waypoint(direction='L'),
    }
    ordering = [[['N'], []], [['I&C'], ['rec']], [['S'], []], [['W'], []],
                [['DPO'], []]]
    bundles = [
        fw.Bundle('N', 'I&C'),
        fw.Bundle('I&C', 'W'),
        fw.Bundle('I&C', 'S'),
        fw.Bundle('I&C', 'DPO'),
        fw.Bundle('S', 'W'),
        fw.Bundle('W', 'DPO'),
        fw.Bundle('W', 'I&C'),
    ]
    sdd = fw.SankeyDefinition(nodes, bundles, ordering)
    out_by_name = fw.Partition.Simple('process', ['E', 'G'])
    nodes['DPO'].partition = out_by_name
    flow_by_type = fw.Partition.Simple(
        'type', ['fossil', 'biomass', 'metal', 'nonmetal'])
    palette = {
        'fossil': 'yellow',
        'biomass': 'green',
        'metal': 'blue',
        'nonmetal': 'orange'
    }
    sdd = fw.SankeyDefinition(nodes,
                              bundles,
                              ordering,
                              flow_partition=flow_by_type)
    fw.weave(sdd, flows, palette=palette).to_widget(**size)
    return flows