def plot_df(df,
            source='source',
            target='target',
            weight='weight',
            layout_opts=None,
            plot_opts=None,
            fig_opts=None):

    #print([ x.key for x in network_layout.layout_setups])

    g = networkx_utility.df_to_nx(df,
                                  source=source,
                                  target=target,
                                  bipartite=False,
                                  weight=weight)

    layout, _ = network_layout.layout_network(g, layout_opts['algorithm'],
                                              **layout_opts['args'])

    edges = networkx_utility.get_positioned_edges2(g, layout)
    nodes = networkx_utility.get_positioned_nodes(g, layout)

    plot_data = plot_network(nodes=nodes,
                             edges=edges,
                             plot_opts=plot_opts,
                             fig_opts=fig_opts)

    return plot_data
Esempio n. 2
0
def display_party_network(parties=None,
                          period_group_index=0,
                          treaty_filter='',
                          plot_data=None,
                          topic_group=None,
                          recode_is_cultural=False,
                          layout_algorithm='',
                          C=1.0,
                          K=0.10,
                          p1=0.10,
                          output='network',
                          party_name='party',
                          node_size_range=[40, 60],
                          palette_name=None,
                          width=900,
                          height=900,
                          node_size=None,
                          node_partition=None,
                          wti_index=None,
                          year_limit=None,
                          progress=utility.noop,
                          done_callback=None):
    try:

        if output == 'print_args':
            args = utility.filter_dict(locals(), ['progress', 'done_callback'],
                                       filter_out=True)
            args['wti_index'] = None
            args['plot_data'] = None
            args['output'] = 'network'
            pp(args)
            return

        plot_data = plot_data or utility.SimpleStruct(handle=None,
                                                      nodes=None,
                                                      edges=None,
                                                      slice_range_type=2,
                                                      slice_range=year_limit)
        weight_threshold = 0.0

        palette = get_palette(palette_name)

        progress(1)
        period_group = config.DEFAULT_PERIOD_GROUPS[period_group_index]
        kwargs = dict(period_group=period_group,
                      treaty_filter=treaty_filter,
                      recode_is_cultural=recode_is_cultural,
                      year_limit=year_limit)
        parties = list(parties)
        party_data = wti_index.get_party_network(party_name, topic_group,
                                                 parties, **kwargs)

        if party_data is None or party_data.shape[0] == 0:
            print('No data for selection')
            return

        if topic_group is not None:

            party_data = party_data.loc[(party_data.topic.isin(
                topic_group.keys()))]

            group_keys = topic_group.keys()
            line_palette = color_utility.DEFAULT_LINE_PALETTE
            line_palette_map = {
                k: i % len(line_palette)
                for i, k in enumerate(group_keys)
            }
            party_data['line_color'] = party_data.category.apply(
                lambda x: line_palette[line_palette_map[x]])

        else:
            party_data['category'] = party_data.topic

        party_data['edge_label'] = party_data.signed.apply(
            lambda x: x.year).astype(str) + '/' + party_data.category

        progress(2)

        #if not multigraph:
        #    data = data.groupby(['party', 'party_other']).size().reset_index().rename(columns={0: 'weight'})

        if party_data is None or party_data.shape[0] == 0:
            print('No data for selection')
            return

        G = create_party_network(party_data, K, node_partition,
                                 palette)  #, multigraph)

        progress(3)

        if output == 'network':

            if weight_threshold > 0:
                G = get_sub_network(G, weight_threshold)

            layout, _ = layout_network(G, layout_algorithm,
                                       **dict(scale=1.0, K=K, C=C, p=p1))

            progress(4)

            edges = get_positioned_edges2(G, layout, sort_attr='signed')
            nodes = get_positioned_nodes(G, layout)

            edges = {k: list(edges[k]) for k in edges}
            nodes = {k: list(nodes[k]) for k in nodes}

            node_size = setup_node_size(nodes, node_size, node_size_range)

            x_offset, y_offset = adjust_node_label_offset(nodes, node_size)

            plot_opts = utility.extend(NETWORK_PLOT_OPTS,
                                       figsize=(width, height),
                                       node_size=node_size,
                                       node_label_opts=dict(y_offset=y_offset,
                                                            x_offset=x_offset),
                                       edge_label_opts={})

            progress(5)

            data = plot_network(nodes=nodes,
                                edges=edges,
                                node_label='name',
                                edge_label='edge_label',
                                **plot_opts)

            plot_data.update(**data)

            progress(6)

            #bp.show(p)

            if done_callback is not None:
                done_callback(None)

        elif output == 'table':
            party_data.columns = [
                dict(party='source', party_other='target').get(x, x)
                for x in party_data.columns
            ]
            display(party_data)

    except Exception as ex:
        logger.error(ex)
        raise
    finally:
        progress(0)