def create_temporal_graph(bt_activity_dict, macs, include_perifery=False):

    end_time = max(bt_activity_dict.keys())
    G = temporal_graph(end_time)
    G.add_vertices(macs)
    edges = []
    for t in bt_activity_dict.keys():
        for pair in bt_activity_dict[t]:
            if include_perifery:
                if pair[0] not in G.vertices:
                    G.add_vertices(pair[0])
                if pair[1] not in G.vertices:
                    G.add_vertices(pair[1])
                edges.append(((pair[0].pair[1]),(t,t)))
            elif pair[0] in G.vertices and pair[1] in G.vertices:
                edges.append(((pair[0],pair[1]),(t,t)))
    G.add_temporal_edges(edges)
    return G
    def init_gnp(self):
        # generate the village graphs
        for i in xrange(0, self.nu):
            G_i = nx.fast_gnp_random_graph(self.gamma, self.p)

            # mapping = lambda x: x + i*self.nu
            new_labels = [v + i * self.gamma for v in G_i.nodes_iter()]
            mapping = dict(zip(G_i.nodes_iter(), new_labels))

            G_i = nx.relabel_nodes(G_i, mapping, copy=True)
            self.graph.add_nodes_from(G_i.nodes())
            self.graph.add_edges_from(G_i.edges())

        # connect the merchants to a random node in a random graph

        for j in xrange(0, self.eta):
            merchant_label = "m_" + str(j)
            self.graph.add_node(merchant_label)

            # pick a random graph
            i = np.random.randint(self.nu)

            # pick a random resident
            r = np.random.randint(self.gamma)

            # add the edge from the merchant to the corresponding resident
            self.graph.add_edge(merchant_label, r + i * self.gamma)

            # set the probability of mobility for this merchant to a number between 0.5 and 1
            self.prob_mobility.append(np.random.uniform(0.5, 1))

        # initialize temporal graph
        self.temporal_graph = temporal_graph(0)
        # add vertices
        self.temporal_graph.add_vertices(self.graph.nodes())
        # add current edges to the temporal graph
        self.temporal_graph.append_snapshot(self.graph)
        self.current_timestep = 0
#!/usr/bin/env python2
from temporal_graph import *
from matplotlib import pyplot as plt

if __name__ == "__main__":
    et = 3
    G = temporal_graph(et)
    G.add_vertices(['A','B','C','D'])
    #G.add_temporal_edges([(('A','C'),(1,1)),(('C','B'),(2,2)),(('A','D'),(3,3)),(('C','D'),(3,3)),(('C','A'),(4,4)),(('B','D'),(4,4))])
    #G.add_temporal_edges([(('A','C'),(1,1)),(('A','D'),(2,2)),(('B','D'),(3,3)),(('C','B'),(3,3))])
    #G.add_temporal_edges([(('A','C'),(1,1)),(('A','D'),(2,2)),(('B','D'),(2,3)),(('C','D'),(3,3))])
    #G.add_temporal_edges([(('A','C'),(1,1)),(('C','D'),(3,3))])
    G.add_temporal_edges([(('A','B'),(1,3)),(('A','C'),(1,3)),(('A','D'),(1,3)),(('B','A'),(1,1)),(('B','C'),(1,1)),(('B','D'),(1,1)),(('C','A'),(1,1)),(('C','B'),(1,1)),(('C','D'),(1,1)),(('D','A'),(1,1)),(('D','B'),(1,1)),(('D','C'),(1,1))])
    temp_degree =  compute_temporal_degree(G,0,et)
    temp_closeness = compute_temporal_closeness(G,0,et)
    temp_betweenness = compute_temporal_betweenness(G,0,et)

    static_stats = compute_static_graph_statistics(G,0,et)
    print "\tNode\t|\t Type   \t|\tDegree  \t|\tCloseness  \t|\tBetweenness\t|"
    for v in G.vertices:
        print "\t%s\t|\tTemporal  \t|\t%f\t|\t%f\t|\t%f\t|"%(v,temp_degree[v],temp_closeness[v],temp_betweenness[v])
        print "\t%s\t|\tAggregated\t|\t%f\t|\t%f\t|\t%f\t|"%(v,static_stats[0][0][v],static_stats[0][1][v],static_stats[0][2][v])
        print "\t%s\t|\tAverage  \t|\t%f\t|\t%f\t|\t%f\t|"%(v,static_stats[1][0][v],static_stats[1][1][v],static_stats[1][2][v])

    #t = 0
    #for G_t in G.snapshots:


    G.draw_time_ordered_graph()
    plt.show()