-
Notifications
You must be signed in to change notification settings - Fork 0
/
R6_ego_network_statistics.py
160 lines (88 loc) · 4.62 KB
/
R6_ego_network_statistics.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
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
#!/usr/bin/env python
"""
Created by Julia Poncela on March 2010
Given a network.gml (with role attributes) it calculates averages and standar deviation of
weight change, BMI change and activity for all N6's egonetworks.
It takes as argument the path/network.gml and creates a buch of files: ego_R6s_average_weight_change300.txt,
"""
import sys
import os
import networkx as nx
import math
from pylab import *
import numpy
def main(graph_name):
G = nx.read_gml(graph_name)
G = nx.connected_component_subgraphs(G)[0] # Giant component
dir=graph_name.split("fr")[0]
dir=dir+"roles/"
time_in_system=200 #minimum amount of time in the sytem for a user to be included in the statistics
cont=1
list_R6s=[]
for node in G.nodes() :
if str(G.node[node]['role']) == "R6" :
list_R6s.append(node)
for node in list_R6s:
neighbors=G.neighbors(node)#a list of nodes
average_BMI_change=0.0
list_BMI_changes=[]
average_weight_change=0.0
list_weight_changes=[]
average_activity=0.0 # ojo! sera dividida por el numero de dias!!!!!
list_activities=[]
eff_degree=0
for n in G.neighbors(node):
if int(G.node[n]['time_in_system']) > time_in_system:
eff_degree=eff_degree+1.0
list_BMI_changes.append(float(G.node[n]['final_BMI'])-float(G.node[n]['initial_BMI']))
list_weight_changes.append(float(G.node[n]['weight_change']))
list_activities.append(float(G.node[n]['activity'])/float(G.node[n]['time_in_system']))
#print float(G.node[n]['final_BMI'])-float(G.node[n]['initial_BMI']),float(G.node[n]['weight_change']),float(G.node[n]['activity'])/float(G.node[n]['time_in_system'])
#averages
average_weight_change=sum(list_weight_changes)/eff_degree
average_BMI_change=sum(list_BMI_changes)/eff_degree
average_activity=sum(list_activities)/eff_degree
print cont,"R6: ",average_weight_change,average_BMI_change,average_activity
#standard deviation
deviation_BMI=0.0
for i in list_BMI_changes:
deviation_BMI=deviation_BMI +(i - average_BMI_change)**2
deviation_BMI=deviation_BMI/eff_degree
deviation_BMI=sqrt(deviation_BMI)
deviation_weight=0.0
for i in list_weight_changes:
deviation_weight=deviation_weight +(i - average_weight_change)**2
deviation_weight=deviation_weight/eff_degree
deviation_weight=sqrt(deviation_weight)
deviation_activity=0.0
for i in list_activities:
deviation_activity=deviation_activity +(i - average_activity)**2
deviation_activity=deviation_activity/eff_degree
deviation_activity=sqrt(deviation_activity)
#print out
name1=dir+"ego_R6s_average_BMI_change_"+str(time_in_system)+"days.dat"
file1=open(name1, 'at')
print >> file1,cont,G.node[node]['role'],G.node[node]['label'],len(G.neighbors(node)),eff_degree,average_BMI_change,deviation_BMI#,list_BMI_changes
file1.close()
name2=dir+"ego_R6s_average_weight_change_"+str(time_in_system)+"days.dat"
file2=open(name2, 'at')
print >> file2,cont,G.node[node]['role'],G.node[node]['label'],len(G.neighbors(node)),eff_degree,average_weight_change,deviation_weight#,list_weight_changes
file2.close()
name3=dir+"ego_R6s_average_activity_"+str(time_in_system)+"days.dat"
file3=open(name3, 'at')
print >> file3,cont,G.node[node]['role'],G.node[node]['label'],len(G.neighbors(node)),eff_degree,average_activity,deviation_activity#,list_activities
file3.close()
name4=dir+"ego_R6s_dispersions_"+str(time_in_system)+"days.dat"
file4=open(name4, 'at')
for i in range(len(list_activities)):
print >> file4,cont, list_BMI_changes[i],list_weight_changes[i],list_activities[i]
print >> file4,"\n\n" #to separate roles
file4.close()
cont=cont+1
######################################3
if __name__ == '__main__':
if len(sys.argv) > 1:
graph_filename = sys.argv[1]
main(graph_filename)
else:
print "usage: python R6_ego_network_statistics.py path/network_file.gml"