File size: 6,135 Bytes
9aa34d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import pytest
import os
import networkx as nx

def __import():
    # Gmatch4py use networkx graph 
    import networkx as nx 
    import gmatch4py as gm


def test_import():
    os.chdir(os.environ["HOME"] )
    __import()

def test_graph():
    os.chdir(os.environ["HOME"])
    import networkx as nx 
    import gmatch4py as gm

    # Simple Graph
    G1 = nx.Graph()
    G2 = nx.Graph()
    G1.add_edge("1","2")
    G1.add_edge("1","3")
    
    gm.graph.Graph(G1)

    # Digraph Graph
    G1 = nx.DiGraph()
    G1.add_edge("1","2")
    G1.add_edge("1","3")
    assert list(G1.edges()) == gm.graph.Graph(G1).edges()

    G1 = nx.DiGraph()
    G1.add_edge("1","2",color="blue")
    G1.add_edge("1","2",color="red")
    G1.add_edge("1","3",color="green")
    assert gm.graph.Graph(G1,edge_attr_key="color").density() == 2
    assert gm.graph.Graph(G1).density() == 2

    # Multi Graph
    G1 = nx.MultiGraph()
    G1.add_edge("1","2",color="blue")
    G1.add_edge("1","3",color="green")
    assert list(G1.edges()) == gm.graph.Graph(G1).edges()
    G1 = nx.MultiGraph()
    G1.add_edge("1","2",color="blue")
    G1.add_edge("1","3",color="green")
    assert len(set([gm.graph.Graph(G1).hash_edge_attr(ed[0],ed[1],ed[2]["color"]) for ed in list(G1.edges(data=True))]).intersection(gm.graph.Graph(G1,edge_attr_key="color").get_edges_hash())) == 2

    G1 = nx.MultiGraph()
    G1.add_edge("1","2",color="blue")
    G1.add_edge("1","2",color="red")
    G1.add_edge("1","3",color="green")
    assert gm.graph.Graph(G1,edge_attr_key="color").density() == len(G1.edges(data=True))
    assert gm.graph.Graph(G1).density() == len(G1.edges(data=True))

    # Multi DiGraph
    G1 = nx.MultiDiGraph()
    G1.add_edge("1","2",color="blue")
    G1.add_edge("1","2",color="red")
    G1.add_edge("1","3",color="green")
    assert gm.graph.Graph(G1,edge_attr_key="color").density() == len(G1.edges(data=True))
    assert gm.graph.Graph(G1).density() == len(G1.edges(data=True))

def test_hash():
    os.chdir(os.environ["HOME"])
    import networkx as nx 
    import gmatch4py as gm

    # Basic HASH
    G1 = nx.Graph()
    G_gm = gm.graph.Graph(G1)
    assert G_gm.hash_edge("1","2") == "1_2"
    assert G_gm.hash_edge("2","1") == "1_2"

    # IF directed
    G1 = nx.DiGraph()
    G1.add_edge("1","2")
    G_gm = gm.graph.Graph(G1)
    assert G_gm.hash_edge("3","2") == "3_2"
    assert G_gm.hash_edge("2","1") == "2_1"

    # IF color and directed
    G1 = nx.DiGraph()
    G1.add_edge("1","2",color="blue")
    G_gm = gm.graph.Graph(G1,edge_attr_key="color")
    assert G_gm.hash_edge_attr("3","2","blue") == "3_2_blue"
    assert G_gm.get_edges_hash() == {"1_2_blue"}

    # if color and not directed
    G1 = nx.Graph()
    G1.add_edge("1","2",color="blue")
    G_gm = gm.graph.Graph(G1,edge_attr_key="color")
    assert G_gm.hash_edge_attr("3","2","blue") == "2_3_blue"

def test_intersect_union():
    os.chdir(os.environ["HOME"])
    import networkx as nx 
    import gmatch4py as gm

    # Basic 
    G1 = nx.Graph()
    G1.add_edge("1","2")
    G1.add_edge("1","3")
    G2 = G1.copy()
    G2.add_edge("3","4")
    GM1 = gm.graph.Graph(G1)
    GM2 = gm.graph.Graph(G2)

    assert GM1.size_edge_union(GM2) == 3
    assert GM1.size_node_union(GM2) == 4

    assert GM1.size_edge_intersect(GM2) == 2
    assert GM1.size_node_intersect(GM2) == 3

    # BASIC and noised for hash
    G1 = nx.Graph()
    G1.add_edge("1","2")
    G1.add_edge("1","3")
    G2 = nx.Graph()
    G2.add_edge("1","2")
    G2.add_edge("3","1") # Changing the direction (no impact if working)
    G2.add_edge("3","4")
    GM1 = gm.graph.Graph(G1)
    GM2 = gm.graph.Graph(G2)

    assert GM1.size_edge_union(GM2) == 3
    assert GM1.size_node_union(GM2) == 4
    
    assert GM1.size_edge_intersect(GM2) == 2
    assert GM1.size_node_intersect(GM2) == 3


    # Directed 
    G1 = nx.DiGraph()
    G1.add_edge("1","2")
    G1.add_edge("1","3")
    G2 = nx.DiGraph()
    G2.add_edge("1","2")
    G2.add_edge("3","1") # Changing the direction (no impact if working)
    G2.add_edge("3","4")
    GM1 = gm.graph.Graph(G1)
    GM2 = gm.graph.Graph(G2)

    assert GM1.size_edge_union(GM2) == 4
    assert GM1.size_node_union(GM2) == 4
    
    assert GM1.size_edge_intersect(GM2) == 1
    assert GM1.size_node_intersect(GM2) == 3


    # IF COLOR
    G1 = nx.DiGraph(); G1.add_node("1",color="blue")
    G2 = nx.DiGraph(); G2.add_node("1",color="red")

    GM1,GM2 = gm.graph.Graph(G1),gm.graph.Graph(G2) 
    assert GM1.size_node_intersect(GM2) == 1
    GM1,GM2 = gm.graph.Graph(G1,node_attr_key="color"),gm.graph.Graph(G2,node_attr_key="color") 
    assert GM1.size_node_intersect(GM2) == 0
    

    G1 = nx.DiGraph(); G1.add_edge("1","2",color="blue")
    G2 = nx.DiGraph(); G2.add_edge("1","2",color="red")

    GM1,GM2 = gm.graph.Graph(G1),gm.graph.Graph(G2) 
    assert GM1.size_edge_intersect(GM2) == 1
    assert GM1.size_edge_union(GM2) == 1
    GM1,GM2 = gm.graph.Graph(G1,edge_attr_key="color"),gm.graph.Graph(G2,edge_attr_key="color") 
    assert GM1.size_edge_intersect(GM2) == 0
    assert GM1.size_edge_union(GM2) == 2

def test_degree():
    os.chdir(os.environ["HOME"])
    import networkx as nx 
    import gmatch4py as gm

    # Not DIRECTED and no attr
    G1 = nx.Graph()
    G1.add_edge("1","2")
    G1.add_edge("1","3")
    GM1 = gm.graph.Graph(G1)
    assert GM1.degree('1') == 2

    G1 = nx.DiGraph()
    G1.add_edge("1","2")
    G1.add_edge("3","1")
    GM1 = gm.graph.Graph(G1)
    assert GM1.degree('1') == 2
    assert GM1.in_degree('1') == 1
    assert GM1.out_degree('1') == 1

    G1 = nx.MultiGraph()
    G1.add_edge("1","2",color="blue")
    G1.add_edge("1","2",color="red")
    G1.add_edge("1","3",color="blue")
    GM1 = gm.graph.Graph(G1,edge_attr_key ="color")
    
    assert GM1.degree_attr('1',"blue") == 2
    assert GM1.degree('1') == 3

    G1 = nx.MultiDiGraph()
    G1.add_edge("1","2",color="blue")
    G1.add_edge("1","2",color="red")
    G1.add_edge("1","3",color="green")
    GM1 = gm.graph.Graph(G1,edge_attr_key ="color")
    assert GM1.in_degree_attr('2','red') == 1
    assert GM1.in_degree('2') == 2