Exemplo n.º 1
0
def dynamic_retweet_graph(data, nodes_texts, graph=None):
    # retweet dynamic directed graph initialization
    if graph is None:
        graph = dn.DynDiGraph()
    # add nodes and their attributes to the graph
    for item in list(nodes_texts.keys()):
        graph.add_node(key, text=nodes_texts[item])
    # adding weights to 'graph'
    heads = []
    tails = []
    for i in range(len(data)):
        heads.append(data.iloc[i]['mentionField'])
        tails.append(data.iloc[i]['ownerName'])
    for i in range(len(heads)):
        if graph.has_edge(tails[i], heads[i]):
            graph[tails[i]][heads[i]]['weight'] += 1
        else:
            graph.add_edges_from([[tails[i], heads[i]]], weight=1)
    # adding interactions to 'graph'
    for i in range(len(df)):
        if df.iloc[i]['mentionField'] != '""':
            graph.add_interaction(u=df.iloc[i]['ownerName'],
                                  v=df.iloc[i]['mentionField'],
                                  t=df.iloc[i]['timestamp'])
    return graph
Exemplo n.º 2
0
    def test_number_of_interactions(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(1, 2, 5)
        g.add_interaction(2, 3, 5)
        g.add_interaction(3, 4, 5)
        g.add_interaction(4, 5, 6)
        g.add_interaction(5, 6, 6)
        g.add_interaction(6, 7, 6)
        g.add_interaction(7, 8, 6)

        its = g.number_of_interactions()
        self.assertEqual(its, 8)

        its = g.number_of_interactions(0)
        self.assertEqual(its, None)

        its = g.number_of_interactions(0, 1)
        self.assertEqual(its, 1)

        its = g.number_of_interactions(0, 1, 5)
        self.assertEqual(its, 1)

        its = g.number_of_interactions(0, 1, 6)
        self.assertEqual(its, 0)
Exemplo n.º 3
0
    def test_nodes(self):

        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(0, 2, 5)
        g.add_interaction(0, 3, 5)
        g.add_interaction(0, 4, 5)

        nds = len(g.nodes())
        self.assertEqual(nds, 5)

        g.add_interaction(5, 1, 6)
        g.add_interaction(5, 2, 6)
        g.add_interaction(2, 3, 6)
        g.add_interaction(5, 4, 6)

        nds = len(g.nodes())
        self.assertEqual(nds, 6)

        nds = len(g.nodes(t=6))
        self.assertEqual(nds, 5)

        nds = len(g.nodes(t=9))
        self.assertEqual(nds, 0)

        self.assertEqual(g.has_node(0), True)
        self.assertEqual(g.has_node(0, 5), True)
        self.assertEqual(g.has_node(0, 6), False)
        self.assertEqual(g.has_node(0, 0), False)
Exemplo n.º 4
0
    def test_time_slice(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(0, 2, 5)
        g.add_interaction(0, 3, 5)
        g.add_interaction(0, 4, 5)
        g.add_interaction(4, 5, 6)
        g.add_interaction(4, 6, 6)
        g.add_interaction(4, 7, 6)
        g.add_interaction(4, 8, 6)

        h = g.time_slice(5)
        self.assertIsInstance(h, dn.DynDiGraph)
        self.assertEqual(h.number_of_nodes(), 5)
        self.assertEqual(h.number_of_interactions(), 4)

        h = g.time_slice(5, 5)
        self.assertIsInstance(h, dn.DynDiGraph)
        self.assertEqual(h.number_of_nodes(), 5)
        self.assertEqual(h.number_of_interactions(), 4)

        h = g.time_slice(5, 6)
        self.assertIsInstance(h, dn.DynDiGraph)
        self.assertEqual(h.number_of_nodes(), 9)
        self.assertEqual(h.number_of_interactions(), 8)

        h = g.time_slice(0)
        self.assertIsInstance(h, dn.DynDiGraph)
        self.assertEqual(h.number_of_nodes(), 0)
        self.assertEqual(h.number_of_interactions(), 0)
Exemplo n.º 5
0
 def test_json_directed(self):
     g = dn.DynDiGraph()
     g.add_interaction(1, 2, 2)
     g.add_interaction(2, 1, 2)
     g.add_interaction(1, 2, 2, e=6)
     g.add_interaction(1, 2, 7, e=11)
     g.add_interaction(1, 2, 8, e=15)
     g.add_interaction(1, 2, 18)
     g.add_interaction(1, 2, 19)
     data = json_graph.node_link_data(g)
     h = json_graph.node_link_graph(data)
     self.assertIsInstance(h, dn.DynDiGraph)
Exemplo n.º 6
0
 def test_self_loop(self):
     G = dn.DynDiGraph()
     G.add_interaction(0, 1, t=0)
     G.add_interaction(0, 2, t=0)
     G.add_interaction(0, 0, t=0)
     G.add_interaction(1, 1, t=0)
     G.add_interaction(2, 2, t=0)
     G.add_interaction(2, 2, t=2)
     ints = G.interactions(t=0)
     self.assertEqual(len(ints), 5)
     self.assertEqual(G.has_interaction(0, 0, t=0), True)
     self.assertEqual(G.has_interaction(2, 0, t=0), False)
Exemplo n.º 7
0
    def test_temporal_snapshots_ids(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(1, 2, 5)
        g.add_interaction(2, 3, 5)
        g.add_interaction(3, 4, 5)
        g.add_interaction(4, 5, 6)
        g.add_interaction(5, 6, 6)
        g.add_interaction(6, 7, 6)
        g.add_interaction(7, 8, 6)
        tsd = g.temporal_snapshots_ids()

        self.assertEqual(tsd, [5, 6])
Exemplo n.º 8
0
    def test_conversion(self):
        G = dn.DynDiGraph()
        G.add_interaction(0, 1, t=0)
        G.add_interaction(0, 2, t=0)
        G.add_interaction(0, 0, t=0)
        G.add_interaction(1, 1, t=0)
        G.add_interaction(2, 2, t=0)
        G.add_interaction(2, 2, t=2)

        H = G.to_undirected()
        self.assertIsInstance(H, dn.DynGraph)
        self.assertEqual(H.number_of_nodes(), 3)
        print(H.edges, H.number_of_edges())
        self.assertEqual(H.number_of_edges(), 3)
Exemplo n.º 9
0
    def test_stream_interactions(self):
        g = dn.DynDiGraph()
        g.add_interaction(1, 2, 2)
        g.add_interaction(1, 2, 2, e=6)
        g.add_interaction(1, 2, 7, e=11)
        g.add_interaction(1, 2, 8, e=15)
        g.add_interaction(1, 2, 18)
        g.add_interaction(1, 2, 19)
        g.add_interactions_from([(1, 3), (1, 5)], t=2, e=3)

        sres = list(g.stream_interactions())

        cres = [(1, 2, '+', 2), (1, 3, '+', 2), (1, 5, '+', 2), (1, 3, '-', 3),
                (1, 5, '-', 3), (1, 2, '-', 6), (1, 2, '+', 7), (1, 2, '-', 15), (1, 2, '+', 18)]
        self.assertEqual(sorted(sres), sorted(cres))
Exemplo n.º 10
0
    def test_has_interaction(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(1, 2, 5)
        g.add_interaction(2, 3, 5)
        g.add_interaction(3, 4, 5)
        g.add_interaction(4, 5, 6)
        g.add_interaction(5, 6, 6)
        g.add_interaction(6, 7, 6)
        g.add_interaction(7, 8, 6)

        self.assertEqual(g.has_interaction(0, 1), True)
        self.assertEqual(g.has_interaction(1, 0), False)
        self.assertEqual(g.has_interaction(0, 1, 5), True)
        self.assertEqual(g.has_interaction(1, 0, 5), False)
        self.assertEqual(g.has_interaction(0, 1, 6), False)
        self.assertEqual(g.has_interaction(0, 1, 9), False)
Exemplo n.º 11
0
    def test_neighbors(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(1, 2, 5)
        g.add_interaction(2, 3, 5)
        g.add_interaction(3, 4, 5)
        g.add_interaction(4, 5, 6)
        g.add_interaction(5, 6, 6)
        g.add_interaction(6, 7, 6)
        g.add_interaction(7, 8, 6)

        ng = len(g.neighbors(0))
        self.assertEqual(ng, 1)

        ng = len(g.neighbors(0, 5))
        self.assertEqual(ng, 1)

        ng = len(g.neighbors(0, 6))
        self.assertEqual(ng, 0)

        ng = len(g.neighbors(0, 0))
        self.assertEqual(ng, 0)

        ng = len(g.predecessors(0))
        self.assertEqual(ng, 0)

        ng = len(g.successors(0))
        self.assertEqual(ng, 1)

        ng = len(g.predecessors(1))
        self.assertEqual(ng, 1)

        ng = len(g.successors(1))
        self.assertEqual(ng, 1)

        ng = len(g.predecessors(1, 5))
        self.assertEqual(ng, 1)

        ng = len(g.successors(1, 5))
        self.assertEqual(ng, 1)

        ng = len(g.predecessors(1, 6))
        self.assertEqual(ng, 0)

        ng = len(g.successors(1, 6))
        self.assertEqual(ng, 0)
Exemplo n.º 12
0
 def test_accumulative_growth(self):
     g = dn.DynDiGraph(edge_removal=False)
     g.add_interaction(1, 2, 2)
     g.add_interaction(1, 2, 2, e=6)
     g.add_interaction(1, 2, 7, e=11)
     g.add_interaction(1, 2, 8, e=15)
     g.add_interaction(1, 2, 18)
     g.add_interaction(1, 2, 19)
     g.add_interactions_from([(1, 3), (1, 5)], t=2, e=3)
     sres = list(g.stream_interactions())
     cres = [(1, 2, "+", 2), (1, 5, "+", 2), (1, 3, "+", 2)]
     self.assertEquals(sorted(sres), sorted(cres))
     self.assertEqual(g.has_interaction(1, 2, 18), True)
     self.assertEqual(g.has_interaction(1, 2, 40), False)
     try:
         g.add_interaction(2, 1, 7)
     except:
         pass
Exemplo n.º 13
0
    def test_in_out_interactions(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(1, 2, 5)
        g.add_interaction(2, 3, 5)
        g.add_interaction(3, 4, 5)
        g.add_interaction(4, 5, 6)
        g.add_interaction(5, 6, 6)
        g.add_interaction(6, 7, 6)
        g.add_interaction(7, 8, 6)

        sr = g.in_interactions()
        self.assertEqual(sr, [(0, 1, {'t': [[5, 5]]}), (1, 2, {'t': [[5, 5]]}), (2, 3, {'t': [[5, 5]]}),
                              (3, 4, {'t': [[5, 5]]}), (4, 5, {'t': [[6, 6]]}), (5, 6, {'t': [[6, 6]]}),
                              (6, 7, {'t': [[6, 6]]}), (7, 8, {'t': [[6, 6]]})])

        sr = g.in_interactions([0, 1])
        self.assertEqual(sr, [(0, 1, {'t': [[5, 5]]})])

        sr = g.in_interactions([9, 10])
        self.assertEqual(sr, [])

        sr = g.in_interactions([0, 1], 5)
        self.assertEqual(sr, [(0, 1, {'t': [5]})])

        sr = g.in_interactions([0, 1], 7)
        self.assertEqual(sr, [])

        sr = g.out_interactions()
        self.assertEqual(sr, [(0, 1, {'t': [[5, 5]]}), (1, 2, {'t': [[5, 5]]}), (2, 3, {'t': [[5, 5]]}),
                              (3, 4, {'t': [[5, 5]]}), (4, 5, {'t': [[6, 6]]}), (5, 6, {'t': [[6, 6]]}),
                              (6, 7, {'t': [[6, 6]]}), (7, 8, {'t': [[6, 6]]})])

        sr = g.out_interactions([0])
        self.assertEqual(sr, [(0, 1, {'t': [[5, 5]]})])

        sr = g.out_interactions([9, 10])
        self.assertEqual(sr, [])

        sr = g.out_interactions([0, 1], 5)
        self.assertEqual(sr, [(0, 1, {'t': [5]}), (1, 2, {'t': [5]})])

        sr = g.out_interactions([0, 1], 7)
        self.assertEqual(sr, [])
Exemplo n.º 14
0
    def test_interactions_per_snapshots(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(1, 2, 5)
        g.add_interaction(2, 3, 5)
        g.add_interaction(3, 4, 5)
        g.add_interaction(4, 5, 6)
        g.add_interaction(5, 6, 6)
        g.add_interaction(6, 7, 6)
        g.add_interaction(7, 8, 6)

        tsd = g.interactions_per_snapshots()
        self.assertDictEqual(tsd, {5: 4, 6: 4})

        tsd = g.interactions_per_snapshots(t=5)
        self.assertEqual(tsd, 4)

        tsd = g.interactions_per_snapshots(t=0)
        self.assertEqual(tsd, 0)
Exemplo n.º 15
0
    def test_number_of_nodes(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(0, 2, 5)
        g.add_interaction(0, 3, 5)
        g.add_interaction(0, 4, 5)
        g.add_interaction(4, 5, 6)
        g.add_interaction(4, 6, 6)
        g.add_interaction(4, 7, 6)
        g.add_interaction(4, 8, 6)

        nn = g.number_of_nodes()
        self.assertEqual(nn, 9)

        nn = g.number_of_nodes(t=5)
        self.assertEqual(nn, 5)

        nn = g.number_of_nodes(t=0)
        self.assertEqual(nn, 0)
Exemplo n.º 16
0
 def test_nodes_with_data(self):
     g = dn.DynDiGraph()
     g.add_node(self.node4uuid, test_param='test_val_4')
     g.add_node(self.node1uuid, test_param='test_val_1')
     g.add_node(self.node3uuid, test_param='test_val_3')
     g.add_node(self.node5uuid, test_param='test_val_5')
     g.add_node(self.node2uuid, test_param='test_val_2')
     g.add_interaction(u=self.node0uuid, v=self.node1uuid, t=self.t5ts)
     g.add_interaction(u=self.node0uuid, v=self.node2uuid, t=self.t5ts)
     g.add_interaction(u=self.node0uuid, v=self.node3uuid, t=self.t5ts)
     g.add_interaction(u=self.node0uuid, v=self.node4uuid, t=self.t5ts)
     data = g.nodes(data=True)
     dataDict = {k: v for (k, v) in data}
     cmpDict = {self.node4uuid: {'test_param': 'test_val_4'},
                self.node1uuid: {'test_param': 'test_val_1'},
                self.node3uuid: {'test_param': 'test_val_3'},
                self.node5uuid: {'test_param': 'test_val_5'},
                self.node2uuid: {'test_param': 'test_val_2'},
                self.node0uuid: {}}
     self.assertDictEqual(dataDict, cmpDict)
Exemplo n.º 17
0
    def test_degree(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(0, 2, 5)
        g.add_interaction(0, 3, 5)
        g.add_interaction(0, 4, 5)
        g.add_interaction(4, 5, 6)
        g.add_interaction(4, 6, 6)
        g.add_interaction(4, 7, 6)
        g.add_interaction(4, 8, 6)

        ng = g.degree(4)
        self.assertEqual(ng, 5)

        ng = g.in_degree(4)
        self.assertEqual(ng, 1)

        ng = g.out_degree(4)
        self.assertEqual(ng, 4)

        ng = g.degree(4, 5)
        self.assertEqual(ng, 1)

        ng = g.in_degree(4, 5)
        self.assertEqual(ng, 1)

        ng = g.out_degree(4, 5)
        self.assertEqual(ng, 0)

        ng = g.degree(4, 6)
        self.assertEqual(ng, 4)

        ng = g.degree(4, 0)
        self.assertEqual(ng, 0)

        ng = g.in_degree(4, 6)
        self.assertEqual(ng, 0)

        ng = g.out_degree(4, 0)
        self.assertEqual(ng, 0)
Exemplo n.º 18
0
    def test_nodes_with_data(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(0, 2, 5)
        g.add_interaction(0, 3, 5)
        g.add_interaction(0, 4, 5)
        g._node[0]['key'] = 'val0'
        g._node[1]['key'] = 'val1'
        g._node[2]['key'] = 'val2'
        g._node[3]['key'] = 'val3'
        g._node[4]['key'] = 'val4'

        data = g.nodes(data=True)
        self.assertEqual(data, [(0, {
            'key': 'val0'
        }), (1, {
            'key': 'val1'
        }), (2, {
            'key': 'val2'
        }), (3, {
            'key': 'val3'
        }), (4, {
            'key': 'val4'
        })])
Exemplo n.º 19
0
    def test_inter_out_event_time(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 2)
        g.add_interaction(0, 2, 2)
        g.add_interaction(0, 3, 2)
        g.add_interaction(0, 4, 2)
        g.add_interaction(4, 5, 3)
        g.add_interaction(4, 6, 3)
        g.add_interaction(4, 7, 3)
        g.add_interaction(4, 8, 3)
        g.add_interaction(0, 2, 4)

        # complete

        ivt = g.inter_event_time_distribution()
        self.assertDictEqual(ivt, {0: 6, 1: 2})

        ivt = g.inter_event_time_distribution(4)
        self.assertDictEqual(ivt, {0: 3, 1: 1})

        ivt = g.inter_event_time_distribution(0)
        self.assertDictEqual(ivt, {0: 3, 2: 1})

        ivt = g.inter_event_time_distribution(0, 1)
        self.assertDictEqual(ivt, {})

        ivt = g.inter_event_time_distribution(0, 2)
        self.assertDictEqual(ivt, {2: 1})

        ivt = g.inter_event_time_distribution(2, 0)
        self.assertDictEqual(ivt, {2: 1})

        # out

        ivt = g.inter_out_event_time_distribution()
        self.assertDictEqual(ivt, {0: 6, 1: 2})

        ivt = g.inter_out_event_time_distribution(4)
        self.assertDictEqual(ivt, {0: 3})

        ivt = g.inter_out_event_time_distribution(0)
        self.assertDictEqual(ivt, {0: 3, 2: 1})

        ivt = g.inter_out_event_time_distribution(0, 1)
        self.assertDictEqual(ivt, {})

        ivt = g.inter_out_event_time_distribution(0, 2)
        self.assertDictEqual(ivt, {2: 1})

        # in

        ivt = g.inter_in_event_time_distribution()
        self.assertDictEqual(ivt, {0: 6, 1: 2})

        ivt = g.inter_in_event_time_distribution(4)
        self.assertDictEqual(ivt, {})

        ivt = g.inter_in_event_time_distribution(2)
        self.assertDictEqual(ivt, {2: 1})

        ivt = g.inter_in_event_time_distribution(0, 1)
        self.assertDictEqual(ivt, {})

        ivt = g.inter_in_event_time_distribution(2, 0)
        self.assertDictEqual(ivt, {2: 1})

        ivt = g.inter_in_event_time_distribution(0, 2)
        self.assertDictEqual(ivt, {})
Exemplo n.º 20
0
    def test_functions_directed(self):
        g = dn.DynDiGraph()
        g.add_interaction(1, 2, 2)
        g.add_interaction(1, 2, 2, e=6)
        g.add_interaction(1, 2, 7, e=11)
        g.add_interaction(1, 2, 8, e=15)
        g.add_interaction(1, 2, 18)
        g.add_interaction(1, 2, 19)

        self.assertEqual(len(dn.nodes(g)), 2)
        self.assertEqual(len(dn.nodes(g, t=0)), 0)
        self.assertEqual(len(dn.nodes(g, t=2)), 2)
        self.assertEqual(len(dn.interactions(g)), 1)
        self.assertEqual(len(dn.interactions(g, t=0)), 0)
        self.assertEqual(len(dn.interactions(g, t=2)), 1)
        self.assertDictEqual(dn.degree(g), {1: 1, 2: 1})
        self.assertDictEqual(dn.degree(g, t=0), {1: 0, 2: 0})
        self.assertDictEqual(dn.degree(g, [1, 2]), {1: 1, 2: 1})
        self.assertEqual(len(dn.neighbors(g, 1)), 1)
        self.assertEqual(len(dn.neighbors(g, 1, t=2)), 1)
        self.assertEqual(len(dn.neighbors(g, 1, t=0)), 0)
        self.assertEqual(dn.number_of_nodes(g), 2)
        self.assertEqual(dn.number_of_nodes(g, t=0), 0)
        self.assertEqual(dn.number_of_nodes(g, t=2), 2)
        self.assertEqual(dn.number_of_interactions(g, t=0), 0)
        self.assertEqual(dn.number_of_interactions(g, t=2), 1)
        self.assertEqual(dn.number_of_interactions(g), 1)
        self.assertEqual(dn.density(g), 0.5)
        self.assertEqual(dn.density(g, t=0), 0)
        self.assertEqual(dn.density(g, t=2), 0)
        self.assertEqual(dn.degree_histogram(g), [0, 2])
        self.assertEqual(dn.degree_histogram(g, t=0), [2])
        self.assertEqual(dn.degree_histogram(g, t=2), [0, 2])
        self.assertEqual(dn.is_directed(g), True)

        g.add_interaction(1, 2, 30)
        g.add_interaction(2, 3, 30)
        g.add_interaction(3, 4, 30)
        g.add_interaction(4, 1, 30)

        g.add_interaction(4, 6, 40)
        g.add_interaction(6, 7, 40)
        g.add_interaction(7, 8, 40)

        g.add_interaction(1, 2, 50)
        g.add_interaction(1, 3, 50)
        g.add_interaction(1, 4, 50)

        dn.add_star(g, [1, 2, 3, 4], t=50)
        dn.subgraph(g, [1, 3, 4])
        dn.create_empty_copy(g)

        self.assertEqual(len(list(dn.all_neighbors(g, 1, t=0))), 0)
        self.assertEqual(len(list(dn.all_neighbors(g, 1))), 4)
        self.assertEqual(len(list(dn.all_neighbors(g, 1, t=2))), 1)
        self.assertEqual(len(list(dn.non_neighbors(g, 1, t=0))), 6)
        self.assertEqual(len(list(dn.non_neighbors(g, 1))), 3)
        self.assertEqual(len(list(dn.non_neighbors(g, 1, t=2))), 5)
        dn.non_interactions(g, 2)
        dn.non_interactions(g, 0)
        dn.non_interactions(g)
        self.assertEqual(dn.is_empty(g), False)
        h = dn.DynGraph()
        self.assertEqual(dn.is_empty(h), True)
        h = dn.time_slice(g, 2, 4)
        self.assertEqual(len(h.nodes()), 2)
        dn.stream_interactions(g)
        dn.temporal_snapshots_ids(g)
        dn.interactions_per_snapshots(g, 0)
        dn.interactions_per_snapshots(g, 2)
        dn.interactions_per_snapshots(g)
        dn.inter_event_time_distribution(g)
        dn.inter_event_time_distribution(g, 1)
        dn.inter_event_time_distribution(g, 1, 2)
        dn.set_node_attributes(g,
                               values={n: 0
                                       for n in g.nodes()},
                               name="test")
        try:
            dn.set_node_attributes(g, values={90000: 0}, name="test")
        except:
            pass
        try:
            dn.set_node_attributes(g, "test1", name="test")
        except:
            pass
        dn.set_node_attributes(g, values={n: {"a": 3} for n in g.nodes()})
        try:
            dn.set_node_attributes(g, values={9000: {"a": 3}})
        except:
            pass

        dn.get_node_attributes(g, name="test")
        try:
            dn.set_edge_attributes(g, 3, "dog")
        except:
            pass
        try:
            dn.get_edge_attributes(g, "test")
        except:
            pass

        dn.freeze(g)
        self.assertEqual(dn.is_frozen(g), True)

        self.assertEqual(len(g.interactions()), 8)

        h = g.to_undirected()
        self.assertEqual(len(h.interactions()), 8)
        dn.all_neighbors(h, 1)

        self.assertEqual(len(list(dn.non_interactions(g))), 13)

        h = g.to_undirected(reciprocal=True)
        self.assertEqual(h.number_of_interactions(), 0)

        g.add_interaction(2, 1, 50)
        h = g.to_undirected(reciprocal=True)
        self.assertEqual(h.number_of_interactions(), 1)

        g.add_interaction(10, 11, 3, e=7)
        g.add_interaction(11, 10, 3, e=7)
        h = g.to_undirected(reciprocal=True)
        self.assertEqual(h.number_of_interactions(), 2)

        g.add_interaction(12, 14, 3, e=5)
        g.add_interaction(14, 12, 4, e=14)

        h = g.to_undirected(reciprocal=True)
        self.assertEqual(h.number_of_interactions(), 3)
Exemplo n.º 21
0
    def test_dyndigraph_add_interaction(self):
        g = dn.DynDiGraph()
        self.assertIsInstance(g, dn.DynDiGraph)

        g.add_interaction(1, 2, 2)
        g.add_interaction(1, 2, 2, e=6)
        g.add_interaction(1, 2, 7, e=11)
        g.add_interaction(1, 2, 8, e=15)
        g.add_interaction(1, 2, 18)
        g.add_interaction(1, 2, 19)

        its = g.interactions()
        self.assertEqual(len(its), 1)

        g.add_interactions_from([(1, 3), (1, 5)], t=2)

        its = g.interactions()
        self.assertEqual(len(its), 3)

        its = g.interactions(t=18)
        self.assertEqual(len(its), 1)

        its = g.interactions(t=20)
        self.assertEqual(len(its), 0)

        self.assertEqual(len(list(g.neighbors_iter(1))), 3)
        self.assertEqual(len(list(g.neighbors_iter(1, 7))), 1)
        self.assertEqual(len(list(g.neighbors_iter(1, 0))), 0)
        self.assertEqual(g.order(), len(g.nodes()))

        self.assertEqual(g.has_node(42), False)

        self.assertEqual(g.has_node(42, 3), False)
        g.add_interaction(3, 4, 34)
        g.add_interaction(4, 5, 34)
        g.add_interaction(5, 6, 34)
        g.add_interaction(6, 4, 34)
        try:
            g.time_slice(2, 1)
        except:
            pass

        g.interactions_iter([1, 2])

        try:
            g.add_interaction(1, 5)
        except:
            pass

        try:
            g.add_interactions_from([(1, 4), (3, 6)])
        except:
            pass

        try:
            g.remove_edge(1, 2)
        except:
            pass

        try:
            g.remove_edges_from([(1, 2)])
        except:
            pass
        try:
            g.remove_node(1)
        except:
            pass
        try:
            g.remove_nodes_from([1, 2])
        except:
            pass

        self.assertEqual(g.number_of_interactions(1, 90), 0)
Exemplo n.º 22
0
    def test_time_slice_with_data(self):
        g = dn.DynDiGraph()
        g.add_interaction(0, 1, 5)
        g.add_interaction(0, 2, 5)
        g.add_interaction(0, 3, 5)
        g.add_interaction(0, 4, 5)
        g.add_interaction(4, 5, 6)
        g.add_interaction(4, 6, 6)
        g.add_interaction(4, 7, 6)
        g.add_interaction(4, 8, 6)
        g._node[0]['key'] = 'val0'
        g._node[1]['key'] = 'val1'
        g._node[2]['key'] = 'val2'
        g._node[3]['key'] = 'val3'
        g._node[4]['key'] = 'val4'
        g._node[5]['key'] = 'val5'

        h = g.time_slice(5)
        self.assertDictEqual(
            h._node, {
                0: {
                    'key': 'val0'
                },
                1: {
                    'key': 'val1'
                },
                2: {
                    'key': 'val2'
                },
                3: {
                    'key': 'val3'
                },
                4: {
                    'key': 'val4'
                }
            })

        h = g.time_slice(5, 6)
        self.assertDictEqual(
            h._node, {
                0: {
                    'key': 'val0'
                },
                1: {
                    'key': 'val1'
                },
                2: {
                    'key': 'val2'
                },
                3: {
                    'key': 'val3'
                },
                4: {
                    'key': 'val4'
                },
                5: {
                    'key': 'val5'
                },
                6: {},
                7: {},
                8: {}
            })
Exemplo n.º 23
0
import matplotlib.pyplot as plt
import sys

sys.path.append('../../')
import dynetx as dnx
# import seaborn as sns
G = dnx.DynDiGraph(edge_removal=True)
G.add_node(1)
G.add_interaction(1, 2, t=0)
G.add_interaction(2, 5, t=0)
G.add_interaction(2, 3, t=1)
G.add_interaction(3, 4, t=2)

dnx.draw(G)