-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildNetworkX.py
68 lines (60 loc) · 2.17 KB
/
buildNetworkX.py
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
import networkx as nx
def buildNetworkXFromAM(adjacencyLists):
G = nx.DiGraph()
for source in adjacencyLists:
if not G.has_node(source):
G.add_node(source)
for dest in adjacencyLists[source]:
if not G.has_node(dest):
G.add_node(dest)
G.add_edge(source, dest)
return G
def getGender(nodeToGender, node):
if (nodeToGender[node] == "1"):
return "male"
else:
return "female"
def buildGenderedNetworkXFromAM(adjacencyLists, nodeToGender):
G = nx.DiGraph()
for source in adjacencyLists:
if not G.has_node(source):
G.add_node(source, gender=getGender(nodeToGender, source))
for dest in adjacencyLists[source]:
if not G.has_node(dest):
G.add_node(dest, gender=getGender(nodeToGender, dest))
G.add_edge(source, dest)
return G
def test():
adjacencyLists = {}
adjacencyLists['1'] = {'2', '3'}
adjacencyLists['2'] = {'1', '5'}
adjacencyLists['3'] = {'2', '4'}
G = buildNetworkXFromAM(adjacencyLists)
if not G.has_node('1'):
print('tests failed! 1 missing from graph')
elif not G.has_node('2'):
print('tests failed! 2 missing from graph')
elif not G.has_node('3'):
print('tests failed! 3 missing from graph')
elif not G.has_node('4'):
print('tests failed! 4 missing from graph')
elif not G.has_node('5'):
print('tests failed! 5 missing from graph')
elif len(G.nodes()) != 5:
print('tests failed! does not have 5 nodes')
elif not G.has_edge('1','2'):
print('tests faild! no edge from 1 to 2')
elif not G.has_edge('1','3'):
print('tests faild! no edge from 1 to 3')
elif not G.has_edge('2','1'):
print('tests faild! no edge from 2 to 1')
elif not G.has_edge('2','5'):
print('tests faild! no edge from 2 to 5')
elif not G.has_edge('3','2'):
print('tests faild! no edge from 3 to 2')
elif not G.has_edge('3','4'):
print('tests faild! no edge from 3 to 4')
elif len(G.edges()) != 6:
print('tests failed! does not have 6 edges')
else:
print('tests passed!')