def get_frac_nodes_in_gcc_from_dates(start, end): g_slice = graphgen.get_graph_slice(start, end) nodes_in_gcc = len(nx.weakly_connected_components(g_slice)[0]) return float(nodes_in_gcc) / g_slice.number_of_nodes()
def get_out_degrees_from_dates(start, end): g_slice = graphgen.get_graph_slice(start, end) return g_slice.out_degree()
def get_num_nodes_edges_from_dates(start, end): g_slice = graphgen.get_graph_slice(start, end) return g_slice.number_of_nodes(), g_slice.number_of_edges()
def analyze_public_trading(): """ Analyzes the snippet of the graph around the time when bitcoin opened for public trading with USD. """ g = graphgen.get_graph_slice(_START_PUBLIC_TRADING, _END_PUBLIC_TRADING)
import networkx as nx import graphgen import graphtools import tags_over_time from networkx.algorithms import * _HMS = 1000000 slices = graphgen.generate_time_slices(slice_time=5, num_intervals=50, num_in_slice=10) i = 0 deg_connectivity = [] dates = [] avg_clust = [] lccs = [] largest_scc = [] for start, end in slices: g = graphgen.get_graph_slice(start * _HMS, end * _HMS) if len(g) == 0: continue # stamp = str(start) + '_' + str(end) #tags_over_time.user_transaction_frequency(g, stamp) # tags_over_time.user_transaction_amount(g, stamp) # tags_over_time.user_buy_frequency(g, stamp) # tags_over_time.user_sell_frequency(g, stamp) # d = assortativity.average_degree_connectivity(g) # utils.save_node_map(d, stamp) deg_connectivity.append(assortativity.average_degree_connectivity(g)) undir_g = nx.Graph(g.copy()) avg_clust.append(nx.average_clustering(undir_g)) lccs.append(len(graphtools.get_lcc_from_graph(g))) largest_scc.append(len(graphtools.get_sccs_from_graph(g)[0]))
def analyze_silk_road(): """ Analyzes the snippet of the bitcoin network around the time that the silk road launched. """ g = graphgen.get_graph_slice(_START_SILK_ROAD, _END_SILK_ROAD)
def analyze_satoshi_dice(): """ Analyzes the snippet of the bitcoin network around the time that SatoshiDice was announced. There is a hard date for its launch. """ g_before = graphgen.get_graph_slice(_START_SATOSHI_DICE, _LAUNCH_SATOSHI_DICE) g_after = graphgen.get_graph_slice(_START_SATOSHI_DICE, _END_SATOSHI_DICE) before_lcc = graphtools.get_lcc_from_graph(g_before) after_lcc = graphtools.get_lcc_from_graph(g_after) print len(before_lcc), len(after_lcc) ''' before_in = g_before.in_degree() before_in_values = sorted(set(before_in.values())) before_in_hist = [before_in.values().count(x)/float(len(g_before.nodes())) for x in before_in_values] after_in = g_after.in_degree() after_in_values = sorted(set(after_in.values())) after_in_hist = [after_in.values().count(x)/float(len(g_after.nodes())) for x in after_in_values] plt.loglog(before_in_values, before_in_hist, 'bo', label='In-degree before SatoshiDICE announced') plt.loglog(after_in_values, after_in_hist, 'ro', label='In-degree after SatoshiDICE announced') plt.legend(loc=1) plt.xlabel('Degree') plt.ylabel('Fraction of nodes') plt.title('In-degree 1 week before and after SatoshiDICE announced') plt.show() before_out = g_before.out_degree() before_out_values = sorted(set(before_out.values())) before_out_hist = [before_out.values().count(x)/float(len(g_before.nodes())) for x in before_out_values] after_out = g_after.out_degree() after_out_values = sorted(set(after_out.values())) after_out_hist = [after_out.values().count(x)/float(len(g_after.nodes())) for x in after_out_values] plt.loglog(before_out_values, before_out_hist, 'bo', label='Out-degree before SatoshiDICE announced') plt.loglog(after_out_values, after_out_hist, 'ro', label='Out-degree after SatoshiDICE announced') plt.legend(loc=1) plt.xlabel('Degree') plt.ylabel('Fraction of nodes') plt.title('Out-degree 1 week before and after SatoshiDICE announced') plt.show() ''' #Can't use k-core with MultiDiGraph ''' g_before.remove_edges_from(g_before.selfloop_edges()) g_after.remove_edges_from(g_after.selfloop_edges()) print len(graphtools.get_k_core_from_graph(g_before).nodes()) print len(graphtools.get_k_core_from_graph(g_after).nodes()) ''' #before_node_max_in = graphtools.get_node_max_in_degree(g_before) #after_node_max_in = graphtools.get_node_max_in_degree(g_after) #print before_node_max_in, after_node_max_in #print sorted(g_before.in_degree().iteritems(), key=operator.itemgetter(1)) #print sorted(g_after.in_degree().iteritems(), key=operator.itemgetter(1)) #print len(g_after.edges('25', data=True)) #print nx.get_edge_attributes(g_after, 'transaction_key') #print g_after.in_degree(after_node_max_in) #new_nodes = set(g_after.in_degree())-set(g_before.in_degree()) #print len(new_nodes) #new_nodes_degrees = {k: g_after.in_degree()[k] for k in new_nodes} #print sorted(new_nodes_degrees.iteritems(), key=operator.itemgetter(1)) time_period = graphgen._days[graphgen._days.index(_START_SATOSHI_DICE/graphgen._HMS):graphgen._days.index(_END_SATOSHI_DICE/graphgen._HMS)] transactions_per_day = [] for day in time_period: daystart = graphtools.string_to_datetime(str(day) + '000000') dayend = graphtools.string_to_datetime(str(day) + '235959') transactions = [] for n, nbrs in g_after.adjacency_iter(): for nbr, edict in nbrs.items(): for eattr in edict.values(): timestamp = graphtools.string_to_datetime(eattr['date']) if (timestamp >= daystart and timestamp <= dayend): transactions.append(1) transactions_per_day.append(len(transactions)) print time_period print transactions_per_day time_period = [graphtools.string_to_datetime(str(time)) for time in time_period] dates = matplotlib.dates.date2num(time_period) plt.plot(time_period, transactions_per_day, 'b-') plt.xlabel('Date') plt.ylabel('Number of transactions') plt.title('Number of transactions 1 week before and after SatoshiDICE announced') plt.show()
def get_effective_diameter_from_dates(start, end): g_slice = graphgen.get_graph_slice(start, end) return effective_diameter(g_slice)
def get_avg_out_degree_from_dates(start, end): g_slice = graphgen.get_graph_slice(start, end) return get_avg_out_degree_from_graph(g_slice)
def get_sccs_from_dates(start, end): g_slice = graphgen.get_graph_slice(start, end) return nx.strongly_connected_components(graph)
def get_wccs_from_dates(start, end): g_slice = graphgen.get_graph_slice(start, end) return nx.weakly_connected_components(g_slice)
import graphtools import tags_over_time from networkx.algorithms import * _HMS = 1000000 slices = graphgen.generate_time_slices(slice_time=5, num_intervals=50, num_in_slice=10) i = 0 deg_connectivity = [] dates = [] avg_clust = [] lccs = [] largest_scc = [] for start, end in slices: g = graphgen.get_graph_slice(start * _HMS, end * _HMS) if len(g) == 0: continue # stamp = str(start) + '_' + str(end) #tags_over_time.user_transaction_frequency(g, stamp) # tags_over_time.user_transaction_amount(g, stamp) # tags_over_time.user_buy_frequency(g, stamp) # tags_over_time.user_sell_frequency(g, stamp) # d = assortativity.average_degree_connectivity(g) # utils.save_node_map(d, stamp) deg_connectivity.append(assortativity.average_degree_connectivity(g)) undir_g = nx.Graph(g.copy()) avg_clust.append(nx.average_clustering(undir_g)) lccs.append(len(graphtools.get_lcc_from_graph(g))) largest_scc.append(len(graphtools.get_sccs_from_graph(g)[0]))
def _node_and_edges(start, end): g = graphgen.get_graph_slice(start * 1000000, end * 1000000) if len(g) == 0: return 0, 0 return g.number_of_nodes(), g.number_of_edges()
def analyze_satoshi_dice(): """ Analyzes the snippet of the bitcoin network around the time that SatoshiDice was announced. There is a hard date for its launch. """ g_before = graphgen.get_graph_slice(_START_SATOSHI_DICE, _LAUNCH_SATOSHI_DICE) g_after = graphgen.get_graph_slice(_START_SATOSHI_DICE, _END_SATOSHI_DICE) before_lcc = graphtools.get_lcc_from_graph(g_before) after_lcc = graphtools.get_lcc_from_graph(g_after) print len(before_lcc), len(after_lcc) ''' before_in = g_before.in_degree() before_in_values = sorted(set(before_in.values())) before_in_hist = [before_in.values().count(x)/float(len(g_before.nodes())) for x in before_in_values] after_in = g_after.in_degree() after_in_values = sorted(set(after_in.values())) after_in_hist = [after_in.values().count(x)/float(len(g_after.nodes())) for x in after_in_values] plt.loglog(before_in_values, before_in_hist, 'bo', label='In-degree before SatoshiDICE announced') plt.loglog(after_in_values, after_in_hist, 'ro', label='In-degree after SatoshiDICE announced') plt.legend(loc=1) plt.xlabel('Degree') plt.ylabel('Fraction of nodes') plt.title('In-degree 1 week before and after SatoshiDICE announced') plt.show() before_out = g_before.out_degree() before_out_values = sorted(set(before_out.values())) before_out_hist = [before_out.values().count(x)/float(len(g_before.nodes())) for x in before_out_values] after_out = g_after.out_degree() after_out_values = sorted(set(after_out.values())) after_out_hist = [after_out.values().count(x)/float(len(g_after.nodes())) for x in after_out_values] plt.loglog(before_out_values, before_out_hist, 'bo', label='Out-degree before SatoshiDICE announced') plt.loglog(after_out_values, after_out_hist, 'ro', label='Out-degree after SatoshiDICE announced') plt.legend(loc=1) plt.xlabel('Degree') plt.ylabel('Fraction of nodes') plt.title('Out-degree 1 week before and after SatoshiDICE announced') plt.show() ''' #Can't use k-core with MultiDiGraph ''' g_before.remove_edges_from(g_before.selfloop_edges()) g_after.remove_edges_from(g_after.selfloop_edges()) print len(graphtools.get_k_core_from_graph(g_before).nodes()) print len(graphtools.get_k_core_from_graph(g_after).nodes()) ''' #before_node_max_in = graphtools.get_node_max_in_degree(g_before) #after_node_max_in = graphtools.get_node_max_in_degree(g_after) #print before_node_max_in, after_node_max_in #print sorted(g_before.in_degree().iteritems(), key=operator.itemgetter(1)) #print sorted(g_after.in_degree().iteritems(), key=operator.itemgetter(1)) #print len(g_after.edges('25', data=True)) #print nx.get_edge_attributes(g_after, 'transaction_key') #print g_after.in_degree(after_node_max_in) #new_nodes = set(g_after.in_degree())-set(g_before.in_degree()) #print len(new_nodes) #new_nodes_degrees = {k: g_after.in_degree()[k] for k in new_nodes} #print sorted(new_nodes_degrees.iteritems(), key=operator.itemgetter(1)) time_period = graphgen._days[graphgen._days.index(_START_SATOSHI_DICE / graphgen._HMS):graphgen. _days.index(_END_SATOSHI_DICE / graphgen._HMS)] transactions_per_day = [] for day in time_period: daystart = graphtools.string_to_datetime(str(day) + '000000') dayend = graphtools.string_to_datetime(str(day) + '235959') transactions = [] for n, nbrs in g_after.adjacency_iter(): for nbr, edict in nbrs.items(): for eattr in edict.values(): timestamp = graphtools.string_to_datetime(eattr['date']) if (timestamp >= daystart and timestamp <= dayend): transactions.append(1) transactions_per_day.append(len(transactions)) print time_period print transactions_per_day time_period = [ graphtools.string_to_datetime(str(time)) for time in time_period ] dates = matplotlib.dates.date2num(time_period) plt.plot(time_period, transactions_per_day, 'b-') plt.xlabel('Date') plt.ylabel('Number of transactions') plt.title( 'Number of transactions 1 week before and after SatoshiDICE announced') plt.show()