-
Notifications
You must be signed in to change notification settings - Fork 1
/
compute_connectivity.py
265 lines (225 loc) · 7.82 KB
/
compute_connectivity.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 8 10:31:13 2014
@author: sb238920
"""
from __future__ import print_function
import numpy as np
import copy
from scipy import linalg
from classify_covs import load_data, get_region_signals
from covariance import CovEmbedding
from sklearn.base import BaseEstimator
import covariance
def cov_to_corr(cov):
"""
Computes correlation from covariance
Parameters
==========
cov: np.array
covariance matrix
Returns
=======
corr: np.array
correlation matrix
"""
d = np.sqrt(np.diag(cov))
corr = cov / d
corr = corr / d[:, np.newaxis]
return corr
def prec_to_partial_corr(prec):
"""
Computes partial correlations from precision matrix.
Parameters
==========
prec: np.array
precision matrix
Returns
=======
partial_corr: np.array
partial correlation matrix. Formulae is partial_corr[i,i] =1 and
partial_corr[i,j] = - prec[i,j] / sqrt(prec[i,i] * prec[j,j])
for i!= j
"""
d = np.sqrt(np.diag(prec))
partial_corr = prec / d
partial_corr = partial_corr / d[:, np.newaxis]
partial_corr *= -1
np.fill_diagonal(partial_corr, 1)
return partial_corr
class FC(BaseEstimator, TransformerMixin):
"""Functional connectivity class
Attributes
----------
`signals`: array
ROIs time series, shpae (n_samples, n_features)
`standardize`: bool, default to False
standardize time series
"""
def __init__(self, standardize=False, estimator=None):
self.standardize = standardize
self.base_estimator = base_estimator
def fit(self, X):
X = X - X.mean(axis=0)
self.emp_cov_ = EmpiricalCovariance(X, assume_centered=True)
def transform(self, X, y):
if estimator is None:
estimator = "tangent"
if estimator == "tangent":
elif estimator == "covariance":
elif estimator == "precision":
elif estimator == "correlation":
elif estimator == "partialcorr":
def compute_cov(self):
"""Compute empirical covariances
Attributes
----------
cov_: array
covariance matrix, shape (n_features, n_features)
"""
subject = copy.copy(self.signals)
subject -= subject.mean(axis=0)
if self.standardize:
subject = subject / subject.std(axis=0) # copy on purpose
n_samples = subject.shape[0]
self.cov_ = np.dot(subject.T, subject) / n_samples
return self
def compute_corr(self):
"""Compute empirical correlation matrix
Attributes
----------
corr_: array
correlation matrix, shape (n_features, n_features)
"""
self.corr_ = cov_to_corr(self.cov_)
return self
def compute_prec(self):
"""Compute empirical precision matrix
Attributes
----------
prec_: array
precision matrix, shape (n_features, n_features)
"""
cond_number = np.linalg.cond(self.cov_)
if cond_number > 100: # 1/sys.float_info.epsilon:
print('Bad conditioning! ' +
'condition number is {}'.format(cond_number))
self.prec_ = linalg.inv(self.cov_)
return self
def compute_partial(self):
"""Compute empirical partial correlation matrix
Attributes
----------
partial_corr_: array
partial correlation matrix, shape (n_features, n_features)
"""
self.partial_corr_ = prec_to_partial_corr(self.prec_)
return self
def compute_tangent(self):
"""Compute empirical partial correlation matrix
Attributes
----------
tangent_: array
projection of the covariance matrix on the tangent plane,
shape (n_features, n_features)
"""
ce = CovEmbedding()
ce.fit([self.cov_])
self.tangent_ = ce.transform([self.cov_])[0]
return self
def compute(self, *args):
"""Computes the specified connectivity measures
Parameters
----------
*args: list of str
measures names
Returns
-------
self.conn_: dict
keys: str, measures names
values: array, measures values
"""
computs = {0: self.compute_cov(), 1: self.compute_corr(),
2: self.compute_prec(), 3: self.compute_partial(),
5: self.compute_tangent()}
measures_steps = {'correlations': [0, 1],
'partial correlations': [0, 2, 3],
'covariances': [0],
'precisions': [0, 2],
'tangent plane': [0, 5]}
steps = [step for name in args for step in measures_steps[name]]
steps = set(steps)
for n_step in steps:
computs[n_step]
output = {'correlations': self.corr_,
'partial correlations': self.partial_corr_,
'covariances': self.cov_,
'precisions': self.prec_,
'tangent plane': self.tangent_}
self.conn = {}
for measure_name in args:
self.conn[measure_name] = output[measure_name]
return self
def analysis(region_signals, standardize=False, *args):
""" Computes for given signals the connectivity matrices for specified
measures
Parameters
----------
region_signals: array or list of region_signals
regions time series, shape of each array n_samples, n_regions
standardize: bool (optional, default to False)
standardize roi signals or not
*args: optional str, default to "covariances"
names of the connectivity measures.
Returns
-------
fc_: dict
keys: str, names of connectivity measures
values: array, shape n_subjects, n_regions, n_regions
associated connectivity values,
"""
if type(region_signals) == 'numpy.ndarray':
region_signals = [region_signals]
n_subjects = len(region_signals)
print('{} subjects'.format(n_subjects))
fc_ = {}
for n_subject, subject in enumerate(region_signals):
if n_subject == 0:
fcs = []
myFC = FC(subject, standardize)
myFC.compute(*args)
for n_measure, measure_name in enumerate(args):
if n_subject == 0:
print(measure_name)
n_features = myFC.conn[measure_name].shape[0]
print("{}, {}".format(n_features, n_subjects))
fcs.append(np.empty((n_subjects, n_features, n_features)))
fcs[n_measure][n_subject] = myFC.conn[measure_name]
for n_measure, measure_name in enumerate(args):
fc_[measure_name] = fcs[n_measure]
print('\ncomputed measures: ', end='')
print(*args, sep=', ')
return fc_
if __name__ == "__main__":
# Load conditions names and ROIs time series
df, region_signals = load_data(
root_dir="/home", # /media/Elements/volatile/new/salma",
data_set="ds107")
df2 = get_region_signals(df, region_signals)
groups = df2.groupby("condition")
cond_names = []
all_cond_signals = []
for condition, group in groups:
cond_names.append(condition)
the_cond_signals = []
for ix_ in range(len(group)):
the_cond_signals.append(group.iloc[ix_]["region_signals"])
all_cond_signals.append(the_cond_signals)
# Compute connectivity matrices for each condition and each subject
fcs = {}
for cond_name, region_signals in zip(cond_names, all_cond_signals):
print(cond_name)
fc = analysis(region_signals, True, "covariances",
"precisions", "tangent plane")
for measure_name, measure_values in fc.iteritems():
fcs[(cond_name, measure_name)] = measure_values