Skip to content

Commit d3bd30a

Browse files
committed
added class to compute graph metrics
1 parent d8195ae commit d3bd30a

13 files changed

+89
-70
lines changed

legacy.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import numpy as np
2+
import bct as bct
3+
import networkx as nx
4+
import matplotlib.pyplot as plt
5+
import pandas as pd
6+
import utils
7+
8+
# set seed
9+
np.random.seed(123)
10+
11+
if __name__ == '__main__':
12+
nnodes = 10
13+
ngraphs = 1000
14+
15+
# plot the variation of global efficiency with sparsity from 0 to 1
16+
sparsity = np.linspace(0, 1, 100)
17+
18+
global_efficiency_mean = []
19+
global_efficiency_std = []
20+
global_efficiency_fnirs = []
21+
22+
cluster_coef_mean = []
23+
cluster_coef_std = []
24+
cluster_coef_fnirs = []
25+
26+
fnirs_adj_matrix_stim_0 = np.loadtxt('output/stim_0_fc_mean.csv', delimiter=',')
27+
# get min and max weights ignoring nan values
28+
min_weight = np.nanmin(fnirs_adj_matrix_stim_0)
29+
max_weight = np.nanmax(fnirs_adj_matrix_stim_0)
30+
print('min weight: ', min_weight)
31+
print('max weight: ', max_weight)
32+
33+
for s in sparsity:
34+
ge_list = []
35+
cc_list = []
36+
for i in range(ngraphs):
37+
adj_matrix = utils.RandomBinGraph(nnodes=nnodes, binarize_type='threshold', binarize_param=s).generate(min_weight=min_weight, max_weight=max_weight)
38+
ge = bct.efficiency_bin(adj_matrix)
39+
cc = np.mean(bct.clustering_coef_bu(adj_matrix))
40+
ge_list.append(ge)
41+
cc_list.append(cc)
42+
43+
ge_mean = np.mean(ge_list)
44+
cc_mean = np.mean(cc_list)
45+
ge_std = np.std(ge_list)
46+
cc_std = np.std(cc_list)
47+
48+
global_efficiency_mean.append(ge_mean)
49+
cluster_coef_mean.append(cc_mean)
50+
global_efficiency_std.append(ge_std)
51+
cluster_coef_std.append(cc_std)
52+
global_efficiency_fnirs.append(bct.efficiency_bin(utils.BinarizeMatrix(fnirs_adj_matrix_stim_0, 'threshold', s).binarize()))
53+
cluster_coef_fnirs.append(np.mean(bct.clustering_coef_bu(utils.BinarizeMatrix(fnirs_adj_matrix_stim_0, 'threshold', s).binarize())))
54+
55+
plt.errorbar(sparsity, global_efficiency_mean, yerr=global_efficiency_std, fmt='-o', capsize=3)
56+
plt.plot(sparsity, global_efficiency_fnirs, '-o')
57+
plt.xlabel('Sparsity')
58+
plt.ylabel('Global Efficiency - Stim 0')
59+
plt.legend(['fnirs data', 'random graph'])
60+
plt.savefig('output/global_efficiency_stim_0.png')
61+
62+
plt.clf()
63+
64+
plt.errorbar(sparsity, cluster_coef_mean, yerr=cluster_coef_std, fmt='-o', capsize=3)
65+
plt.plot(sparsity, cluster_coef_fnirs, '-o')
66+
plt.xlabel('Sparsity')
67+
plt.ylabel('Clustering Coefficient - Stim 0')
68+
plt.legend(['fnirs data', 'random graph'])
69+
plt.savefig('output/clustering_coef_stim_0.png')

main.py

Lines changed: 20 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -63,85 +63,35 @@ def plot_features(self):
6363
Plot the features of the random graphs.
6464
'''
6565
for i, feature in enumerate(self.features):
66-
plt.errorbar(self.search_space, [np.mean(self.random_graphs_features['{}_{}'.format(feature, sparsity)]) for sparsity in self.search_space],
67-
yerr=[np.std(self.random_graphs_features['{}_{}'.format(feature, sparsity)]) for sparsity in self.search_space], label='Random Graphs')
68-
plt.plot(self.search_space, [self.fc_graph_features['{}_{}'.format(feature, sparsity)] for sparsity in self.search_space], label='Real Graph', marker='o')
69-
plt.xlabel('Sparsity')
66+
plt.errorbar(self.search_space,
67+
[np.mean(self.random_graphs_features['{}_{}'.format(feature, sparsity)]) for sparsity in self.search_space],
68+
yerr=[np.std(self.random_graphs_features['{}_{}'.format(feature, sparsity)]) for sparsity in self.search_space],
69+
label='Random Graphs',
70+
fmt='-o',
71+
capsize=3)
72+
plt.plot(self.search_space,
73+
[self.fc_graph_features['{}_{}'.format(feature, sparsity)] for sparsity in self.search_space],
74+
label='Real Graph',
75+
marker='o')
76+
plt.xlabel(self.analysis_type)
7077
plt.ylabel(feature)
7178
plt.legend()
72-
plt.savefig('output/{}_sparsity.png'.format(feature))
79+
plt.savefig('output/{}_{}.png'.format(feature, self.analysis_type), dpi=600)
7380
plt.clf()
7481

82+
def compute_auc(self):
83+
'''
84+
Compute the AUC for a given feature.
85+
'''
86+
pass
87+
7588

7689
if __name__ == '__main__':
7790
ngraphs = 1000
7891
fc_matrix = np.loadtxt('output/stim_0_fc_mean.csv', delimiter=',')
79-
x = RandomGraphAnalyzer(fc_matrix, n_rnd_graphs=ngraphs, analysis_type='threshold', features=['global_efficiency', 'local_efficiency', 'clustering_coefficient'])
92+
x = RandomGraphAnalyzer(fc_matrix, n_rnd_graphs=ngraphs, analysis_type='sparsity', features=['global_efficiency', 'local_efficiency', 'clustering_coefficient'])
8093
rgraphs = x.gen_random_graphs()
8194
print(len(rgraphs))
8295
print(len(rgraphs[0]))
8396
x.compute_features()
84-
x.plot_features()
85-
86-
87-
88-
89-
# if __name__ == '__main__':
90-
# nnodes = 10
91-
# ngraphs = 1000
92-
93-
# # plot the variation of global efficiency with sparsity from 0 to 1
94-
# sparsity = np.linspace(0, 1, 100)
95-
96-
# global_efficiency_mean = []
97-
# global_efficiency_std = []
98-
# global_efficiency_fnirs = []
99-
100-
# cluster_coef_mean = []
101-
# cluster_coef_std = []
102-
# cluster_coef_fnirs = []
103-
104-
# fnirs_adj_matrix_stim_0 = np.loadtxt('output/stim_0_fc_mean.csv', delimiter=',')
105-
# # get min and max weights ignoring nan values
106-
# min_weight = np.nanmin(fnirs_adj_matrix_stim_0)
107-
# max_weight = np.nanmax(fnirs_adj_matrix_stim_0)
108-
# print('min weight: ', min_weight)
109-
# print('max weight: ', max_weight)
110-
111-
# for s in sparsity:
112-
# ge_list = []
113-
# cc_list = []
114-
# for i in range(ngraphs):
115-
# adj_matrix = utils.RandomBinGraph(nnodes=nnodes, binarize_type='threshold', binarize_param=s).generate(min_weight=min_weight, max_weight=max_weight)
116-
# ge = bct.efficiency_bin(adj_matrix)
117-
# cc = np.mean(bct.clustering_coef_bu(adj_matrix))
118-
# ge_list.append(ge)
119-
# cc_list.append(cc)
120-
121-
# ge_mean = np.mean(ge_list)
122-
# cc_mean = np.mean(cc_list)
123-
# ge_std = np.std(ge_list)
124-
# cc_std = np.std(cc_list)
125-
126-
# global_efficiency_mean.append(ge_mean)
127-
# cluster_coef_mean.append(cc_mean)
128-
# global_efficiency_std.append(ge_std)
129-
# cluster_coef_std.append(cc_std)
130-
# global_efficiency_fnirs.append(bct.efficiency_bin(utils.BinarizeMatrix(fnirs_adj_matrix_stim_0, 'threshold', s).binarize()))
131-
# cluster_coef_fnirs.append(np.mean(bct.clustering_coef_bu(utils.BinarizeMatrix(fnirs_adj_matrix_stim_0, 'threshold', s).binarize())))
132-
133-
# plt.errorbar(sparsity, global_efficiency_mean, yerr=global_efficiency_std, fmt='-o', capsize=3)
134-
# plt.plot(sparsity, global_efficiency_fnirs, '-o')
135-
# plt.xlabel('Sparsity')
136-
# plt.ylabel('Global Efficiency - Stim 0')
137-
# plt.legend(['fnirs data', 'random graph'])
138-
# plt.savefig('output/global_efficiency_stim_0.png')
139-
140-
# plt.clf()
141-
142-
# plt.errorbar(sparsity, cluster_coef_mean, yerr=cluster_coef_std, fmt='-o', capsize=3)
143-
# plt.plot(sparsity, cluster_coef_fnirs, '-o')
144-
# plt.xlabel('Sparsity')
145-
# plt.ylabel('Clustering Coefficient - Stim 0')
146-
# plt.legend(['fnirs data', 'random graph'])
147-
# plt.savefig('output/clustering_coef_stim_0.png')
97+
x.plot_features()

output/.DS_Store

6 KB
Binary file not shown.

output/clustering_coef_stim_0.png

-30.4 KB
Binary file not shown.

output/clustering_coef_stim_1.png

-30.3 KB
Binary file not shown.
245 KB
Loading
229 KB
Loading
221 KB
Loading
-29.4 KB
Binary file not shown.
-29.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)