示例#1
0
def random_slices(slice_time=1, num_slices=100):
  slices = []
  for i in range(num_slices):
    start = random.choice(_days)
    end = np.datetime64(graphtools.string_to_datetime(str(start))) + np.timedelta64(slice_time, 'D')
    slices.append((start, str(end)[:10].replace('-','')))
  return slices
示例#2
0
def random_slices(slice_time=1, num_slices=100):
    slices = []
    for i in range(num_slices):
        start = random.choice(_days)
        end = np.datetime64(graphtools.string_to_datetime(
            str(start))) + np.timedelta64(slice_time, 'D')
        slices.append((start, str(end)[:10].replace('-', '')))
    return slices
示例#3
0
def plot_frequency_map(*maps, **plotargs):
    """                                                               
    A frequency map maps buckets to frequencies. An example would be  amount spent: 
    the bucket of spending 5 bitcoins is a key, and the value is the number of nodes that 
    spent that much.

    -- Parameters --
    maps - As many maps as you would care to plot. List 'em out, we'll pack them up right 
    plotargs - A bunch o' options:
      - title   - The title that you want for the graphs
      - xlabel  - The label for the x axis   
      - ylabel  - The label for the y axis
      - save_as - The name of the file you want to save the graph in. 
                  Only saves the graph if this parameter is specified
      - show    - Specifiy this parameter to be true to display the graph.

    TODO
      - Add log/semilog support
      - Add scatter support
      - Add style options
      _ Suggestions?
    """
    plt.figure()
    # plot all the maps that are given
    for mp in maps:
        xs =  sorted(mp.keys())
        ys = [mp[k] for k in xs]
        time_period = [graphtools.string_to_datetime(str(time)) for time in xs]
        dates = matplotlib.dates.date2num(time_period)
        plt.plot(time_period,ys)

    # look through kwargs, set appropriate plot features
    if 'xlabel' in plotargs:
        plt.xlabel(plotargs['xlabel'])
    if 'ylabel' in plotargs:
        plt.ylabel(plotargs['ylabel'])
    if 'title' in plotargs:
        plt.title(plotargs['title'])

    # display or save the plot
    if 'save_as' in plotargs:
        plt.savefig(plotargs['save_as'])
    if 'show' in plotargs and plotargs['show']:
        plt.show()
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 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()