SocratesClub / css

《计算社会科学》课程
https://SocratesClub.github.io/css/
16 stars 4 forks source link

A minor error in the false news study #10

Open chengjun opened 4 years ago

chengjun commented 4 years ago

Figure 1

The spread of true and false news online Soroush Vosoughi, Deb Roy, Sinan Aral,*

Science 09 Mar 2018: Vol. 359, Issue 6380, pp. 1146-1151 DOI: 10.1126/science.aap9559

https://science.sciencemag.org/content/359/6380/1146.full

Do you find there is an error in Figure 1A max-breadth?

import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')

alist = [['a', 0], ['a', 'b', 1], ['a', 'c', 3],['b', 'd', 4], 
         ['c', 'e', 6], ['c', 'f', 7], ['d', 'g', 10], ['c', 'h', 11]]

g = nx.Graph()
for i in alist:
    if len(i) == 2:
        n1, t = i
        g.add_node(n1)
    else:
        n1, n2, t = i
        g.add_edge(n1, n2)

pos={'a': np.array([ 0 , 0.5]),
     'b': np.array([ 1,  0.55]),
     'c': np.array([ 3,  0.45]),
     'd': np.array([ 4,  0.55]),
     'e': np.array([ 6,  0.5]),
     'f': np.array([ 7,  0.4]),
     'g': np.array([ 10,  0.55 ]),
     'h': np.array([ 11,  0.45])}

temporal_network = {}
for k in range(1, len(alist)+1):
    elist = alist[:k]
    t = elist[-1][-1]
    temporal_network[t] = nx.Graph()
    for i in elist:
        if len(i) == 2:
            n1 = i[:-1][0]
            temporal_network[t].add_node(n1)
        else:
            n1, n2 = i[:-1]
            temporal_network[t].add_edge(n1, n2)

def calc_size(G):
    num_nodes=G.number_of_nodes()
    return num_nodes

def calc_depth(G,G_root):
    depth=nx.eccentricity(G,v=G_root)
    return depth

def calc_structural_viralty(G):
    size = calc_size(G)
    if size==1:
        return 0 ##virality is not defined for cascades of size 1,
    sv=nx.average_shortest_path_length(G)  #Note: this is very time-consuming for larger cascades
    return sv

def calc_max_breadth(g, root):
    depth = calc_depth(g,root)
    if depth ==0:
        return 0
    else:
        g_in_radius = [len(nx.ego_graph(g, root, radius=i).nodes())-1 for i in range(1, depth+1)]
        breadth_list = [i-g_in_radius[k-1] if k > 0 else i for k, i in enumerate(g_in_radius)]
        return np.max(breadth_list)

t = list(range(np.max(ts)+1))
ts = list(temporal_network.keys())
sizes = [len(temporal_network[i].nodes()) if i in ts else None for i in t]
depths = [calc_depth(temporal_network[i], 'a') if i in ts else None for i in t]
viralty = [calc_structural_viralty(temporal_network[i]) if i in ts else None for i in t]
breadths = [calc_max_breadth(temporal_network[i], 'a') if i in ts else None for i in t]

df  = pd.DataFrame(np.array([t, sizes, depths, breadths, viralty]).T, 
                   columns = ['t', 'size', 'depth', 'breadth', 'viralty'] )
df = df.fillna(method = 'ffill')

plt.figure(figsize = [8, 12])
plt.subplot(511)
nx.draw(g, with_labels = True, pos = pos)

plt.subplot(512)
plt.plot(df['t'], df['size'], 'r-')
plt.ylabel('Size', fontsize = 16)
plt.ylim([0, 8])

plt.subplot(513)
plt.plot(df['t'], df['depth'], 'r-')
plt.ylabel('Depth', fontsize = 16)

plt.subplot(514)
plt.plot(df['t'], df['breadth'], 'r-')
plt.ylabel('Breadth', fontsize = 16)

plt.subplot(515)
plt.plot(df['t'], df['viralty'], 'r-')
plt.ylabel('Virality', fontsize = 16)
plt.xlabel('Time (minutes)', fontsize = 16)
plt.show()

image