In [ ]:
import networkx as nx
import pandas as pd
from matplotlib import pyplot as plt
import glob
%matplotlib inline
In [ ]:
def get_data(filename):
return pd.read_csv(filename)
In [ ]:
def clean_dataframe(dataframe):
dataframe['agent_name'] = 'A' + dataframe['V2'].astype(str)
dataframe['diff_avg'] = dataframe['avg_pos'] - dataframe['avg_neg']
return dataframe
In [ ]:
def get_val_map(dataframe):
"""Get dictionary of Agent ID and difference of means"""
return dict(zip(dataframe.agent_name, dataframe.diff_avg))
In [ ]:
def create_graph_from_edge_list(filename):
return nx.read_edgelist(filename)
In [ ]:
def get_values(G, val_map):
return [val_map.get(node) for node in G.nodes()]
In [ ]:
def plot_network(G):
nx.draw_networkx(G,
cmap=plt.get_cmap('jet'),
node_color=values,
pos=nx.circular_layout(G))
plt.show()
In [ ]:
def get_time_from_filename(filename):
num = int(filename.split('/')[-1].replace('.csv', '').replace('t_', ''))
num_format_4 = '{0:04d}'.format(num)
return "{}.png".format(num_format_4)
In [ ]:
def plot_graph(filename, save=False):
data = get_data(filename)
data = clean_dataframe(data)
val_map = get_val_map(data)
G = create_graph_from_edge_list(edge_list_file)
values = get_values(G, val_map)
plt.figure(figsize=(11,11))
nx.draw_networkx(G,
cmap=plt.get_cmap('bwr'),
node_color=values,
node_size=700,
vmin=-1,vmax=1,
width=0.5,
pos=nx.circular_layout(G))
plt.axis('off')
print(filename)
if save is True:
plt.savefig(get_time_from_filename(filename))
# plt.show()
plt.close()
return None
In [ ]:
base_dir = '../../results/simulations/02-lens_single_2015-04-07_21:25:50//'
edge_list_file = base_dir + '/output/edge_list.gz'
time_files = glob.glob(base_dir + '/output/t_*.csv')
print(len(time_files))
sorted(time_files)[2:52]
In [ ]:
for filename in sorted(time_files)[2:52]:
plot_graph(filename, True)
In [ ]:
# nx.degree_centrality(G)
In [ ]:
# nx.betweenness_centrality(G)
In [ ]:
# sorted(nx.betweenness_centrality(G), key=nx.betweenness_centrality(G).get)
In [ ]:
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import axes3d
import os, sys
import numpy as np
def make_gif(files,output,delay=100, repeat=True,**kwargs):
"""
Uses imageMagick to produce an animated .gif from a list of
picture files.
"""
loop = -1 if repeat else 0
os.system('convert -delay %d -loop %d %s %s'
%(delay,loop," ".join(files),output))
In [ ]:
import glob
files = sorted(glob.glob('*.png'))
print(files)
make_gif(files, 'network-0-50.gif', delay=50)
In [ ]:
def make_movie(files,output, fps=10,bitrate=1800,**kwargs):
"""
Uses mencoder, produces a .mp4/.ogv/... movie from a list of
picture files.
"""
output_name, output_ext = os.path.splitext(output)
command = { '.mp4' : 'mencoder "mf://%s" -mf fps=%d -o %s.mp4 -ovc lavc\
-lavcopts vcodec=msmpeg4v2:vbitrate=%d'
%(",".join(files),fps,output_name,bitrate)}
command['.ogv'] = command['.mp4'] + '; ffmpeg -i %s.mp4 -r %d %s'%(output_name,fps,output)
print(command[output_ext])
output_ext = os.path.splitext(output)[1]
os.system(command[output_ext])
In [ ]:
import glob
files = sorted(glob.glob('*.png'))
print(files)
In [ ]:
# make_movie(files, 'network-0-50.mp4', delay=50)
In [ ]:
%%bash
rm out.mp4
ffmpeg -framerate 1/0.5 -i %04d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4