# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## An example of a diffusion network

# #### First, import required modules  and load the data

# +
import matplotlib as mpl
import matplotlib.patches
import matplotlib.pyplot as plt
import networkx as nx 
import numpy
import numpy as np
# %matplotlib inline

with open('../data/data2.csv', 'r') as f:
    rt_time = []
    for line in f:
        time= line.strip().split(',')[-1]
        print('\ntime: ')
        print (time)
        
        day = time[0:4] + time[5:7] + time[8:10]
        print('\nday: ')
        print (day)
       
        #hms= time[9:17].replace(':', '')
        hm = time[11:16].replace(':','')
        time = int(day + hm)
        print('\ntime: ')
        print(time)

        rt_time.append(time)

# -

# #### Next, count the time lag and convert into a graph

array = numpy.array(rt_time)
order = array.argsort()
ranks = order.argsort()
G = nx.DiGraph()

# #### Compute a few metrics, like degree, closeness, betweenness, and shortest path length

# +
with open('../data/data2.csv', 'r') as f:
    for position, line in enumerate(f):
        uid, rtuid= line.strip().split(',')[:-1]
        G.add_edge(uid, rtuid, time = ranks[position])
        
edges,colors = zip(*nx.get_edge_attributes(G,'time').items())
G = G.to_directed()

# Edges
print('Edges: ')
print(edges)

print('Number of Edges: ')
print (G.number_of_edges()) 

# Degree
degree = G.degree() 
print('Degree: ')
print(degree)

# SPL
path_length = nx.all_pairs_shortest_path_length(G)

# Closeness
closenesss = nx.closeness_centrality(G)

# Betweenness
betweenness = nx.betweenness_centrality(G)
# -

# ### Plot the graph

# +
pos = nx.spring_layout(G) #set up a network

fig = plt.figure(figsize=(6, 6), facecolor='white')

nx.draw(G, pos, 
        nodelist    = list(dict(G.degree).keys()),
        node_size   = [v*40 for v in dict(G.degree).values()], 
        node_color  = 'blue',         
        node_shape  = 'o', 
        cmap        = plt.cm.gray,
        edgelist    = edges, 
        edge_color  = 'gray', 
        width       = 1.0,
        with_labels = True, 
        arrows      = True, 
        arrowstyle  = '->')

plt.title("A twitter social network")
plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered