-
Notifications
You must be signed in to change notification settings - Fork 3
/
cnt_mat.py
executable file
·80 lines (59 loc) · 2.52 KB
/
cnt_mat.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
#!/usr/bin/env python
import sys
import networkx as nx
import numpy as np
import scipy.io as sio
import argparse
#----------------
# Parse arguments
parser = argparse.ArgumentParser( description='The connectome matrix tool: Generating matrices from pickles')
parser.add_argument( '-p', '--pickle',
action='store',
dest='pickle',
required=True,
help='Target pickle file.' )
parser.add_argument( '-b', '--base',
action='store',
dest='base',
default='',
help='Output basename for the files to be generated. A "test" base would generate test_***.extension files.' )
parser.add_argument( '-e', '--extension',
action='store',
dest='extension',
default='txt',
choices=['txt', 'mat'],
help='Output extension for the files to be generated. A "txt" extension would generate base_***.txt files.' )
options = parser.parse_args()
#----------------
# setup parameters
print ('Setup parameters...')
listkeys = ['number_of_fibers', 'fiber_length_mean', 'fiber_length_std']
listformat = ['%d', '%f', '%f']
#----------------
# Load pickle and generate matrices
print ('Load %s and generate matrices...') % (options.pickle)
g = nx.read_gpickle(options.pickle)
# Write out different matrices, each with different edge values
for mat in listkeys:
exec(mat +' = np.zeros([len(g.nodes())+1,len(g.nodes())+1])')
for i in g.edge.keys():
for j in g.edge[i].keys():
for mat in listkeys:
exec(mat +'[i-1][j-1]=g.edge[i][j][\'' + mat + '\']')
#----------------
# save matrices in the requested file format
print 'Saving matrices as: *.' + options.extension + ' files'
if options.extension == 'txt':
for mat in listkeys:
np.savetxt( options.base + '_' + mat + '.' + options.extension ,eval(mat),fmt=listformat[listkeys.index(mat)], delimiter='\t', newline='\n')
elif options.extension == 'mat':
mlab_dict = {}
for mat in listkeys:
mlab_dict['cmatrix'] = eval(mat);
sio.savemat(options.base + '_' + mat + '.' + options.extension,mlab_dict)
else:
print 'Invalid file extension: ' + options.extension
print 'Done'
# connectome_to_matlab(options.base + '_number_of_fibers.mat', g, 'number_of_fibers')
# connectome_to_matlab(options.base + '_fiber_length_mean.mat', g, 'fiber_length_mean')
# connectome_to_matlab(options.base + '_fiber_length_std.mat', g, 'fiber_length_std')