示例#1
0
mg = mx.MultilayerGraph(list_of_layers=[g1, g2],
                        inter_adjacency_matrix=adj_block)
mg.set_edges_weights(inter_layer_edges_weight=2)

mg.set_intra_edges_weights(layer=0, weight=1)
mg.set_intra_edges_weights(layer=1, weight=2)

fig = plt.figure(figsize=(10, 10))

ax1 = fig.add_subplot(122)
ax1.axis('off')
ax1.set_title('regular interconnected network')
pos = mx.get_position(mg,
                      mx.fruchterman_reingold_layout(mg.get_layer(0)),
                      layer_vertical_shift=1.4,
                      layer_horizontal_shift=0.0,
                      proj_angle=7)

mx.draw_networkx(mg,
                 pos=pos,
                 ax=ax1,
                 node_size=30,
                 with_labels=False,
                 edge_color=[mg[a][b]['weight'] for a, b in mg.edges()],
                 edge_cmap=plt.cm.jet_r)

plt.show()

# print(numpy.__file__)
示例#2
0
import numpy as np
import matplotlib.pyplot as plt

N = 50
g1 = mx.erdos_renyi_graph(N, 0.07, seed=218)
g2 = mx.erdos_renyi_graph(N, 0.07, seed=211)
g3 = mx.erdos_renyi_graph(N, 0.07, seed=208)

mg = mx.MultilayerGraph(list_of_layers=[g1, g2, g3])
mg.set_intra_edges_weights(layer=0, weight=1)
mg.set_intra_edges_weights(layer=1, weight=2)
mg.set_intra_edges_weights(layer=2, weight=3)

fig = plt.figure(figsize=(15, 5))

ax2 = fig.add_subplot(122)
ax2.axis('off')
ax2.set_title('edge colored network')
pos = mx.get_position(mg,
                      mx.fruchterman_reingold_layout(g1),
                      layer_vertical_shift=0.2,
                      layer_horizontal_shift=0.0,
                      proj_angle=47)
mx.draw_networkx(mg,
                 pos=pos,
                 ax=ax2,
                 node_size=50,
                 with_labels=False,
                 edge_color=[mg[a][b]['weight'] for a, b in mg.edges()],
                 edge_cmap=plt.cm.jet_r)
plt.show()
示例#3
0
def main():
    data = collect_data('results/t6/')
    comm = collect_community('results/t6/')

    def find_best(data, method):
        key = max(data[method].iteritems(), key=operator.itemgetter(1))[0]
        y = data[method][key]
        return y, key, method

    print 'LART', data['lart']['9.0_1.0_1.0']
    print 'PMM', data['pmm']['30.0_140.0']
    print 'Glouvain', data['glouvain']['1.0_1.0']

    gl = find_best(data, 'glouvain')
    ll = find_best(data, 'lart')
    pl = find_best(data, 'pmm')

    print 'LART', ll
    print 'PMM', pl
    print 'Glouvain', gl

    best = max([gl, ll, pl], key=operator.itemgetter(0))
    best_comm = {}
    for b in comm[best[2]][best[1]].split('\n'):
        if b:
            a, l, c = b.split(',')
            best_comm['%s-%s' % (a, l)] = int(c)

    layers = {'RT': nx.Graph(), 'ff': nx.Graph(), 'Re': nx.Graph()}
    ids = {}

    counter = 0
    groups = []
    with open('data/t6/dk', 'r') as fd:
        for l in fd.readlines():
            a1, a2, layer = l.replace('\n', '').split(",")

            if a1 not in ids:
                ids[a1] = counter
                counter = counter + 1

            if a2 not in ids:
                ids[a2] = counter
                counter = counter + 1

            groups.append(best_comm['%s-%s' % (a1, layer)])
            groups.append(best_comm['%s-%s' % (a2, layer)])

            for k, v in layers.iteritems():
                v.add_node(ids[a1], label=best_comm['%s-%s' % (a1, layer)])
                v.add_node(ids[a2], label=best_comm['%s-%s' % (a2, layer)])

            layers[layer].add_edge(ids[a1], ids[a2])

    truth = {}
    with open('data/t6/dk_truth', 'r') as fd:
        for l in fd.readlines():
            actor, party = l.replace('\n', '').split(',')
            truth[actor] = party

    mapping = dict(zip(sorted(groups), count()))

    N, L = len(layers['ff'].nodes()), len(layers.keys())
    adj_block = mx.lil_matrix(np.zeros((N * L, N * L)))

    for i in xrange(L):
        for j in xrange(L):
            if i < j:
                adj_block[N * i:N * (i + 1),
                          N * j:(j + 1) * N] = np.identity(N)

    adj_block += adj_block.T

    mg = mx.MultilayerGraph(list_of_layers=[v for k, v in layers.items()])
    #inter_adjacency_matrix = adj_block)

    mg.set_edges_weights(intra_layer_edges_weight=1)
    #inter_layer_edges_weight=2)

    fig = plt.figure(figsize=(16, 16))
    plt.title('Twitter data of the Danish 2015 election')

    stats = collections.defaultdict(list)

    for k, v in best_comm.items():
        stats[v].append(k)

    stats2 = collections.defaultdict(dict)
    for k, v in stats.items():
        for e in v:
            actor, _ = e.split('-')

            if truth[actor] in stats2[k]:
                stats2[k][truth[actor]] += 1
            else:
                stats2[k][truth[actor]] = 1

    left = [
        'Dansk Folkeparti',
        'Venstre',
        'Liberal Alliance',
        'Det Konservative Folkeparti',
        'KristenDemokraterne',
    ]

    right = [
        'Socialistisk Folkeparti', 'Radikale Venstre', 'Socialdemokratiet',
        'Alternativet', 'Enhedslisten'
    ]
    out = 'Udenfor partierne'

    for k, v in stats2.items():
        total = 0
        for k1, v1 in v.items():
            total += v1

        pscore = 0
        for k1, v1 in v.items():
            if k1 in left:
                pscore += (stats2[k][k1] * 1)
            if k1 in right:
                pscore -= (stats2[k][k1] * 1)

            stats2[k][k1] = round(float(v1) / float(total), 2)

        stats2[k]['nodes'] = filter(
            lambda x, i=mg, k=k: i.node[x]['label'] == k, mg.node)
        stats2[k]['pscore'] = pscore / float(total)

    stats2 = dict(stats2)

    if len(sys.argv) > 1 and sys.argv[1] == 'heat':
        cmap = plt.get_cmap('RdBu_r')
        colors = [stats2[mg.node[n]['label']]['pscore'] for n in mg.nodes()]

        pos = mx.get_position(mg,
                              nx.spring_layout(layers[layers.keys()[2]],
                                               weight='pscore'),
                              layer_vertical_shift=0.2,
                              layer_horizontal_shift=0.0,
                              proj_angle=47)

        for key, val in stats2.items():
            set_trace()
            mx.draw_networkx_nodes(mg,
                                   pos=pos,
                                   node_size=100,
                                   with_labels=False,
                                   nodelist=val['nodes'],
                                   label=key,
                                   node_color=[colors[n] for n in val['']],
                                   cmap=cmap)
    else:
        val_map = {
            0: 'k',
            1: 'r',
            2: 'g',
            3: 'b',
            4: 'c',
            5: 'm',
            6: 'y',
            7: '0.75',
            8: 'w',
        }
        colors = [val_map[mg.node[n]['label']] for n in mg.nodes()]
        pos = mx.get_position(mg,
                              nx.spring_layout(layers[layers.keys()[2]]),
                              layer_vertical_shift=0.2,
                              layer_horizontal_shift=0.0,
                              proj_angle=47)

        for k, v in stats2.items():
            mx.draw_networkx_nodes(mg,
                                   pos=pos,
                                   node_size=100,
                                   with_labels=False,
                                   nodelist=v['nodes'],
                                   label=k,
                                   node_color=[colors[n] for n in v['nodes']],
                                   cmap=plt.get_cmap('Set2'))

    mx.draw_networkx_edges(mg, pos=pos, edge_color='b')

    fig.tight_layout()
    plt.legend(numpoints=1, loc=1)
    plt.xticks([])
    plt.yticks([])
    plt.show()
示例#4
0
    def multilayer(self, filename='multilayer_network'):
        import numpy as np
        import matplotlib.pyplot as plt
        import multinetx as mx
        import networkx as nx
        from mpl_toolkits.basemap import Basemap

        # %% Define the type of interconnection between the layers
        N = len(self.poi_nodes) + len(self.user_nodes)
        N1 = len(self.poi_nodes)
        adj_block = mx.lil_matrix(np.zeros((N, N)))

        for row in self.user_poi_s.iterrows():
            user_ind = self.user_nodes[self.user_nodes['userId'] == row[1]
                                       ['userId']].index[0]
            pos_ind = self.poi_nodes[self.poi_nodes['venueId'] == row[1]
                                     ['venueId']].index[0]

            adj_block[pos_ind, user_ind] = row[1]['weight']

        adj_block += adj_block.T

        # %% multilayer network
        poi_layer = nx.Graph(layer='POI')
        poi_layer.add_nodes_from(self.poi_nodes.index.values)
        poi_layer.add_weighted_edges_from(self.poi_edges)

        user_layer = nx.DiGraph(layer='User')
        user_layer.add_nodes_from(self.user_nodes.index.values)
        user_layer.add_weighted_edges_from(self.user_edges)

        mg = mx.MultilayerGraph(list_of_layers=[poi_layer, user_layer],
                                inter_adjacency_matrix=adj_block)

        # venue position in decimal lat/lon
        lats_v = list(self.poi_nodes['latitude'])
        lons_v = list(self.poi_nodes['longitude'])

        min_lat = min(lats_v)
        max_lat = max(lats_v)
        min_lon = min(lons_v)
        max_lon = max(lons_v)

        # user position in decimal lat/lon
        lats_u = list(self.user_nodes['latitude'])
        lons_u = list(self.user_nodes['longitude'])

        min_lat = min(min_lat, min(lats_u))
        max_lat = max(max_lat, max(lats_u))
        min_lon = min(min_lon, min(lons_u))
        max_lon = max(max_lon, max(lons_u))

        m = Basemap(projection='merc',
                    llcrnrlon=min_lon,
                    llcrnrlat=min_lat,
                    urcrnrlon=max_lon,
                    urcrnrlat=max_lat,
                    lat_ts=0,
                    resolution='i',
                    suppress_ticks=True)

        # convert lat and lon to map projection
        x, y = m(lons_v, lats_v)

        ppos = {}
        n = 0
        for v in self.poi_nodes.index:
            ppos[v] = np.asarray([x[n], y[n]])
            n += 1

        # convert lat and lon to map projection
        x, y = m(lons_u, lats_u)
        n = 0
        for v in self.user_nodes.index:
            ppos[v] = np.asarray([x[n], y[n]])
            n += 1

        target_userID = list(self.similar_users['userId1'])[0]
        node_of_target_user = self.user_nodes[self.user_nodes['userId'] ==
                                              target_userID].index[0]

        for i in range(2):
            pos = mx.get_position(mg,
                                  ppos,
                                  layer_vertical_shift=1000.0,
                                  layer_horizontal_shift=400.0,
                                  proj_angle=0.2)

            fig, ax2 = plt.subplots(figsize=(10, 5))
            # ax2.axis('off')
            ax2.set_title(
                'Multilayer Network of User and PoI (Target User ID: {0})'.
                format(target_userID))

            mg.set_edges_weights(inter_layer_edges_weight=3)
            mg.set_intra_edges_weights(layer=0, weight=5)
            # mg.set_intra_edges_weights(layer=1,weight=2)

            mx.draw_networkx(
                mg,
                pos=pos,
                ax=ax2,
                node_size=50,
                with_labels=False,
                node_color=[
                    'y' if a < N1 else
                    ('brown' if a == node_of_target_user else 'g')
                    for a in mg.nodes()
                ],
                edge_color=[mg[a][b]['weight'] for a, b in mg.edges()],
                edge_cmap=plt.cm.Blues)

        # plt.show()
        plt.savefig('output/{0}.png'.format(filename),
                    dpi=500,
                    bbox_inches='tight')
示例#5
0
def plot_all_struct_func(mG_path, namer_dir, name, modality_paths, metadata):
    """
    Plot adjacency matrix and glass brain for structural-functional multiplex connectome.

    Parameters
    ----------
    mG_path : str
        A gpickle file containing a a MultilayerGraph object (See https://github.com/nkoub/multinetx).
    namer_dir : str
        Path to output directory for multiplex data.
    name : str
        Concatenation of multimodal graph filenames.
    modality_paths : tuple
       A tuple of filepath strings to the raw structural and raw functional connectome graph files (.npy).
    metadata : dict
        Dictionary coontaining coords and labels shared by each layer of the multilayer graph.
    """
    import numpy as np
    import multinetx as mx
    import matplotlib
    matplotlib.use('agg')
    import pkg_resources
    import networkx as nx
    import yaml
    import sys
    from matplotlib import pyplot as plt
    from nilearn import plotting as niplot
    from pynets.core import thresholding
    from pynets.plotting.plot_gen import create_gb_palette

    coords = metadata['coords']
    labels = metadata['labels']

    ch2better_loc = pkg_resources.resource_filename(
        "pynets", "templates/ch2better.nii.gz")

    with open(pkg_resources.resource_filename("pynets", "runconfig.yaml"),
              'r') as stream:
        hardcoded_params = yaml.load(stream)
        try:
            color_theme_func = hardcoded_params['plotting']['functional'][
                'glassbrain']['color_theme'][0]
            color_theme_struct = hardcoded_params['plotting']['structural'][
                'glassbrain']['color_theme'][0]
            glassbrain = hardcoded_params['plotting']['glassbrain'][0]
            adjacency = hardcoded_params['plotting']['adjacency'][0]
            dpi_resolution = hardcoded_params['plotting']['dpi'][0]
        except KeyError:
            print(
                'ERROR: Plotting configuration not successfully extracted from runconfig.yaml'
            )
            sys.exit(0)
    stream.close()

    [struct_mat_path, func_mat_path] = modality_paths
    struct_mat, func_mat = [np.load(struct_mat_path), np.load(func_mat_path)]

    if adjacency is True:
        # Multiplex adjacency
        mG = nx.read_gpickle(mG_path)

        fig = plt.figure(figsize=(15, 5))
        ax1 = fig.add_subplot(121)
        adj = thresholding.standardize(
            mx.adjacency_matrix(mG, weight='weight').todense())
        [z_min, z_max] = np.abs(adj).min(), np.abs(adj).max()

        adj[adj == 0] = np.nan

        ax1.imshow(adj,
                   origin='lower',
                   interpolation='nearest',
                   cmap=plt.cm.RdBu,
                   vmin=0.01,
                   vmax=z_max)
        ax1.set_title('Supra-Adjacency Matrix')

        ax2 = fig.add_subplot(122)
        ax2.axis('off')
        ax2.set_title(f"Functional-Structural Multiplex Connectome")

        pos = mx.get_position(mG,
                              mx.fruchterman_reingold_layout(mG.get_layer(0)),
                              layer_vertical_shift=1.0,
                              layer_horizontal_shift=0.0,
                              proj_angle=7)
        edge_intensities = []
        for a, b, w in mG.edges(data=True):
            if w != {}:
                edge_intensities.append(w['weight'])
            else:
                edge_intensities.append(0)

        node_centralities = list(
            nx.algorithms.eigenvector_centrality(mG, weight='weight').values())
        mx.draw_networkx(mG,
                         pos=pos,
                         ax=ax2,
                         node_size=100,
                         with_labels=False,
                         edge_color=edge_intensities,
                         node_color=node_centralities,
                         edge_vmin=z_min,
                         edge_vmax=z_max,
                         dim=3,
                         font_size=6,
                         widths=3,
                         alpha=0.7,
                         cmap=plt.cm.RdBu)
        plt.savefig(f"{namer_dir}/{name[:200]}supra_adj.png",
                    dpi=dpi_resolution)

    if glassbrain is True:
        # Multiplex glass brain
        views = ['x', 'y', 'z']
        connectome = niplot.plot_connectome(np.zeros(shape=(1, 1)),
                                            [(0, 0, 0)],
                                            node_size=0.0001,
                                            black_bg=True)
        connectome.add_overlay(ch2better_loc, alpha=0.50, cmap=plt.cm.gray)

        [struct_mat, _, _, _, edge_sizes_struct, _, _, coords,
         labels] = create_gb_palette(struct_mat,
                                     color_theme_struct,
                                     coords,
                                     labels,
                                     prune=False)

        connectome.add_graph(struct_mat,
                             coords,
                             edge_threshold='50%',
                             edge_cmap=plt.cm.binary,
                             node_size=1,
                             edge_kwargs={
                                 'alpha': 0.50,
                                 "lineStyle": 'dashed'
                             },
                             node_kwargs={'alpha': 0.95},
                             edge_vmax=float(1),
                             edge_vmin=float(1))

        for view in views:
            mod_lines = []
            for line, edge_size in list(
                    zip(connectome.axes[view].ax.lines, edge_sizes_struct)):
                line.set_lw(edge_size)
                mod_lines.append(line)
            connectome.axes[view].ax.lines = mod_lines

        [
            func_mat, clust_pal_edges, clust_pal_nodes, node_sizes,
            edge_sizes_func, z_min, z_max, coords, labels
        ] = create_gb_palette(func_mat,
                              color_theme_func,
                              coords,
                              labels,
                              prune=False)
        connectome.add_graph(func_mat,
                             coords,
                             edge_threshold='50%',
                             edge_cmap=clust_pal_edges,
                             edge_kwargs={'alpha': 0.75},
                             edge_vmax=float(z_max),
                             edge_vmin=float(z_min),
                             node_size=node_sizes,
                             node_color=clust_pal_nodes)

        for view in views:
            mod_lines = []
            for line, edge_size in list(
                    zip(
                        connectome.axes[view].ax.
                        lines[len(edge_sizes_struct):], edge_sizes_func)):
                line.set_lw(edge_size)
                mod_lines.append(line)
            connectome.axes[view].ax.lines[len(edge_sizes_struct):] = mod_lines

        connectome.savefig(f"{namer_dir}/{name[:200]}glassbrain_mplx.png",
                           dpi=dpi_resolution)

    return
示例#6
0
lons=list(user_nodes['longitude'])
# convert lat and lon to map projection
x,y=m(lons,lats)
n=0
for v in user_nodes.index:
    ppos[v]=np.asarray([x[n],y[n]])
    n+=1

node_of_target_user = user_nodes[user_nodes['userId'] == userId].index[0]
    
# upos = mx.kamada_kawai_layout(user_layer)
# ppos.update(upos)
# %%
pos = mx.get_position(
                mg,
                ppos,
                layer_vertical_shift=1000.0,
                layer_horizontal_shift=400.0,
                proj_angle=0.2)


fig, ax2 = plt.subplots(figsize=(10,5))
# ax2.axis('off')
ax2.set_title('Multilayer Network of User and PoI')

mg.set_edges_weights(inter_layer_edges_weight=3)
mg.set_intra_edges_weights(layer=0,weight=5)
# mg.set_intra_edges_weights(layer=1,weight=2)

mx.draw_networkx(mg,pos=pos,ax=ax2,node_size=50,with_labels=False,
                node_color=['y' if a < N1 else ('brown' if a == node_of_target_user else 'g') for a in mg.nodes()],
                edge_color=[mg[a][b]['weight'] for a,b in mg.edges()], edge_cmap=plt.cm.Blues)