forked from KirstieJane/DTI_PROCESSING
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_connectivity_weights.py
executable file
·121 lines (95 loc) · 3.87 KB
/
plot_connectivity_weights.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
#!/usr/bin/env python
# plot_connectivity_weights(M_file)
'''
Connectivity plots for a matrix
'''
#=============================================================================
# IMPORTS
#=============================================================================
import numpy as np
import matplotlib.pylab as plt
import argparse
#=============================================================================
# FUNCTIONS
#=============================================================================
# Set up the argparser so you can read arguments from the command line
def setup_argparser():
'''
# Code to read in arguments from the command line
# Also allows you to change some settings
'''
# Build a basic parser.
help_text = ('Create a histogram of weights from a connectivity matrix')
sign_off = 'Author: Kirstie Whitaker <kw401@cam.ac.uk>'
parser = argparse.ArgumentParser(description=help_text, epilog=sign_off)
# Now add the arguments
# Required argument: M_file
parser.add_argument('M_file',
type=str,
metavar='M_file',
help='Matrix (text file)')
# Optional argument: minimum
parser.add_argument('--hist_min',
dest='hist_min',
type=float,
help='histogram minimum value',
default=0.0,
action='store')
# Optional argument: maximum
parser.add_argument('--hist_max',
dest='hist_max',
type=float,
help='histogram maximum value',
default=300,
action='store')
# Optional argument: color
parser.add_argument('--hist_color',
dest='hist_color',
type=str,
help='histogram color',
default='SteelBlue',
action='store')
# Optional argument: no_cost_box
parser.add_argument('--no_cost_box',
dest='no_cost_box',
help='do not show cost in text box',
action='store_true',
default=False)
arguments = parser.parse_args()
return arguments, parser
#=============================================================================
# Define some variables
#=============================================================================
# Read in the arguments from argparse
arguments, parser = setup_argparser()
M_file = arguments.M_file
hist_min = arguments.hist_min
hist_max = arguments.hist_max
hist_color = arguments.hist_color
# Load in the matrix
M = np.loadtxt(M_file)
# Zero out the lower triangle and the diagonal
M_triu = np.triu(M, 1)
# Calculate the density (cost)
n = M.shape[0]
cost = (np.count_nonzero(M_triu) * 2) / np.float(n * (n-1))
# Create a histogram of all (non-zero) connections
M_fig_name = M_file.replace('.txt', '_weights.png')
fig, ax = plt.subplots(figsize=(4,4))
n, bins, patches = ax.hist(M_triu[M_triu>0],
log=True,
range=(hist_min,hist_max),
color=hist_color)
ax.set_xlim([hist_min, hist_max])
ax.set_ylim([1, 10000])
ax.set_xlabel('Connection weight')
ax.set_ylabel('Frequency (log scale)')
# Add in the cost in the top right corner
if not arguments.no_cost_box:
ax.text(0.95, 0.95,
'cost = {:.2f}%'.format(cost*100),
transform=ax.transAxes,
horizontalalignment='right',
verticalalignment='top')
plt.tight_layout()
fig.savefig(M_fig_name, bbox_inches=0, dpi=600)