forked from KirstieJane/DTI_PROCESSING
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_average_mat.py
executable file
·159 lines (127 loc) · 5.08 KB
/
create_average_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
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
#!/usr/bin/env python
'''
Combine a list of matrix files to create an average matrix
'''
#=============================================================================
# IMPORTS
#=============================================================================
import numpy as np
import matplotlib.pylab as plt
import argparse
import os
import sys
#=============================================================================
# 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 = ('Combine a list of matrices into an average 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(dest = 'M_file_list',
type=str,
metavar='M_file_list',
help='Text file containing full paths of all matrices to be averaged)')
arguments = parser.parse_args()
return arguments, parser
#-----------------------------------------------------------------------------
def save_mat(M, M_text_name):
# Save the matrix as a text file
# NOTE THAT THIS IS NOT THE SAME
# COMMAND AS IN calculate_connectivity_matrix.py
if not os.path.exists(M_text_name):
np.savetxt(M_text_name,
M[:,:],
fmt='%.5f',
delimiter='\t',
newline='\n')
#-----------------------------------------------------------------------------
def save_png(M, M_fig_name):
# Make a png image of the matrix
# NOTE THAT THIS IS NOT THE SAME
# COMMAND AS IN calculate_connectivity_matrix.py
if not os.path.exists(M_fig_name):
fig, ax = plt.subplots(figsize=(4,4))
# Plot the matrix on a log scale
axM = ax.imshow(np.log1p(M[:,:]),
interpolation='nearest',
cmap='jet')
# Add a colorbar
cbar = fig.colorbar(axM)
fig.savefig(M_fig_name, bbox_inches=0, dpi=600)
#=============================================================================
# Define some variables
#=============================================================================
# Read in the arguments from argparse
arguments, parser = setup_argparser()
M_file_list_file = arguments.M_file_list
if not M_file_list_file.endswith('_list'):
print "M file list file needs to end with the word list"
sys.exit
M_file_list = [ M.strip() for M in open(M_file_list_file) ]
#=============================================================================
# Create the three different average matrices
#=============================================================================
# Create empty matrices first
#----- AVERAGE -------------------
av_M = np.loadtxt(M_file_list[0]) * 0
#----- NORMALISE & AVERAGE -------
av_norm_M = np.loadtxt(M_file_list[0]) * 0
#----- BINARIZE & AVERAGE --------
av_bin_M = np.loadtxt(M_file_list[0]) * 0
# Loop through all the matrix files
for M_file in M_file_list:
# Load in the matrix
M = np.loadtxt(M_file)
#----- AVERAGE -------------------
# This one is easy: just add the
# matrix to the average
av_M += M
#----- NORMALISE & AVERAGE -------
# Normalize M and add that to the
# av_norm_M matrix
M_norm = M / (np.percentile(M[M>0], 50))
av_norm_M += M_norm
#----- BINARIZE & AVERAGE -------
# Finally binarize the matrix and
# add that to the av_bin_M matrix
M_bin = np.copy(M)
M_bin[M_bin>0] = 1
av_bin_M += M_bin
# Divide the average matrices by the number of files in the list
n = np.float(len(M_file_list))
av_M = av_M / n
av_norm_M = av_norm_M / n
av_bin_M = av_bin_M / n
# Threshold the average matrix so that you're only showing
# edges that are present in at least 5% of participants
av_common_M = np.copy(av_M)
av_common_M[av_bin_M<0.05] = 0
# Save the matrices as text files
#----- AVERAGE -------------------
M_text_name = M_file_list_file.replace('_list', '_avMat.txt')
save_mat(av_M, M_text_name)
M_png_name = M_text_name.replace('.txt', '.png')
save_png(av_M, M_png_name)
#----- NORMALISE & AVERAGE -------
M_text_name = M_file_list_file.replace('_list', '_avNormMat.txt')
save_mat(av_norm_M, M_text_name)
M_png_name = M_text_name.replace('.txt', '.png')
save_png(av_norm_M, M_png_name)
#----- BINARIZE & AVERAGE -------
M_text_name = M_file_list_file.replace('_list', '_avBinMat.txt')
save_mat(av_bin_M, M_text_name)
M_png_name = M_text_name.replace('.txt', '.png')
save_png(av_bin_M, M_png_name)
#----- COMMON AVERAGE ONLY -----
M_text_name = M_file_list_file.replace('_list', '_avMat_gt05.txt')
save_mat(av_common_M, M_text_name)
M_png_name = M_text_name.replace('.txt', '.png')
save_png(av_common_M, M_png_name)