-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils_attributes.py
75 lines (71 loc) · 2.14 KB
/
utils_attributes.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
69
70
71
72
73
74
75
#!/usr/bin/env python
# encoding: utf-8
import random
import networkx as nx
def create_scalar_attributes_0_1(G,source=[]):
F=nx.Graph()
for ed in G.edges():
attr_dic=G.edge[ed[0]][ed[1]]
F.add_edge(ed[0],ed[1],attr_dict=attr_dic)
# print source
# print len(source)
# print aaaaa
for nd in G.nodes():
attr_dic=G.node[nd]
if len(source)<2:
if nd==source[0]:
# print aaa
rand=1.
irand=1
F.add_node(nd,attr_dict=attr_dic,scalar_attribute=rand,scalar_attribute_numeric=irand)
else:
rand=0.
irand=0
F.add_node(nd,attr_dict=attr_dic,scalar_attribute=rand,scalar_attribute_numeric=irand)
elif len(source)==2:
if nd==source[0]:
rand=1.
irand=1
F.add_node(nd,attr_dict=attr_dic,scalar_attribute=rand,scalar_attribute_numeric=irand)
elif nd==source[1]:
rand=0.
irand=0
F.add_node(nd,attr_dict=attr_dic,scalar_attribute=rand,scalar_attribute_numeric=irand)
else:
rand=random.random()
irand=int(rand)
F.add_node(nd,attr_dict=attr_dic,scalar_attribute=rand,scalar_attribute_numeric=irand)
# print F.nodes(data=True)
# print aaa
return F, nx.numeric_assortativity_coefficient(F,'scalar_attribute_numeric')
def create_random_scalar_attributes(G,scale):
F=nx.Graph()
# print F.nodes(data=True)
for ed in G.edges():
attr_dic=G.edges[ed[0],ed[1]]
F.add_edge(ed[0],ed[1],attr_dict=attr_dic)
for nd in G.nodes():
attr_dic=G.node[nd]
rand=random.random()
irand=int(rand*scale)
F.add_node(nd,attr_dict=attr_dic,scalar_attribute=rand,scalar_attribute_numeric=irand)
return F, nx.numeric_assortativity_coefficient(F,'scalar_attribute_numeric')
def create_random_discrete_attributes(G,k):
F=nx.Graph()
for ed in G.edges():
# print ed
attr_dic=G.edge[ed[0]][ed[1]]
F.add_edge(ed[0],ed[1],attr_dict=attr_dic)
range_list=range(k)
# print F.nodes(data=True)
for nd in G.nodes():
attr_dic=G.node[nd]
if len(range_list)!=0:
raa=random.choice(range_list)
range_list.remove(raa)
else:
range_list=range(k)
raa=random.choice(range_list)
F.add_node(nd,attr_dict=attr_dic,discrete_attribute=raa)
return F,nx.attribute_assortativity_coefficient(F,'discrete_attribute')
# G=nx.Graph()