forked from BNUCNL/ATT
/
analyzer.py
561 lines (470 loc) · 19.8 KB
/
analyzer.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 et:
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from atlas import UserDefinedException
from sklearn.linear_model import LinearRegression
def plot_mat(mat, title, xlabels, ylabels):
"""
Parameters
----------
mat : matrix to be plotted, a 2d np.array
title : title for the fig
xlabels: labels for x axis
ylabels: labels for y axis
Returns
-------
"""
fig, ax = plt.subplots()
heatmap = ax.pcolor(mat)
ax.set_xticks(np.arange(mat.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(mat.shape[0]) + 0.5, minor=False)
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.set_xticklabels(xlabels, minor=False)
ax.set_yticklabels(ylabels, minor=False)
plt.xticks(rotation=45)
ax.grid(False)
cbar = plt.colorbar(heatmap)
#cbar.set_label('Pearson correlation')
# turn off all the ticks
ax = plt.gca()
for t in ax.xaxis.get_major_ticks():
t.tick1On = False
t.tick2On = False
for t in ax.yaxis.get_major_ticks():
t.tick1On = False
t.tick2On = False
plt.title(title)
plt.show()
def plot_bar(data, title, xlabels, ylabels, err=None):
"""
Parameters
----------
data : data to be plotted, a 1d np.array
err : error for data, same shape as data
title
xlabels
ylabels
Returns
-------
"""
ind = np.arange(data.shape[0])
width = 0.35
fig, ax = plt.subplots()
if err is None:
rects1 = ax.bar(ind, data, width, color='r')
else:
rects1 = ax.bar(ind, data, width, color='r', yerr=err)
x0, x1 = ax.get_xlim()
y0, y1 = ax.get_ylim()
ax.set_aspect((x1-x0)/(y1-y0))
ax.set_ylabel(ylabels)
ax.set_xticks(ind + width)
plt.xticks(rotation=45)
ax.set_xticklabels(xlabels)
ax.set_title(title)
plt.show()
def cohen_d(x, y):
nx, ny = x.shape[0], y.shape[0]
dof = nx + ny - 2
d = (np.mean(x) - np.mean(y)) / \
np.sqrt(((nx-1)*np.std(x, ddof=1) ** 2 + (ny-1)*np.std(y, ddof=1) ** 2) / dof)
return d
class Analyzer(object):
def __init__(self, meas, meas_type, meas_name, roi_name, subj_id, subj_gender):
"""
Parameters
----------
meas : n_subj x n_feature 2d array
meas_type: scalar or geometry
meas_name: list which keep measures name
roi_name: list which keep roi name
subj_id: list which keep subject id
subj_gender: list which keep subject gender
Returns
-------
"""
self.meas = meas
self.type = meas_type
self.subj_id = subj_id
self.roi_name = roi_name
self.meas_name = meas_name
self.subj_gender = subj_gender
self.feat_name = []
n_roi = len(self.roi_name) # number of ROI
if self.type is 'scalar':
for f in np.arange(meas.shape[1]):
meas_name = self.meas_name[np.floor(np.divide(f, n_roi)).astype(int)]
roi_name = self.roi_name[np.mod(f, n_roi).astype(int)]
self.feat_name.append(roi_name + '-' + meas_name)
elif self.type is 'geometry':
geo = ['x', 'y', 'z']
for f in np.arange(meas.shape[1]):
meas_name = self.meas_name[np.floor(np.divide(f, n_roi*3)).astype(int)]
roi_name = self.roi_name[np.mod(np.floor(f/3), n_roi).astype(int)]
geo_name = geo[np.mod(f, 3)]
self.feat_name.append(roi_name + '-' + meas_name + '-' + geo_name)
else:
raise UserDefinedException('Measure type is error!')
def hemi_merge(self, meth='single', weight=None):
"""
Parameters
----------
meth : 'single' or 'both'.single, keep roi which appear in a single hemisphere;
both, only keep roi which appear in both hemisphere
weight: weight for each roi, n_subj x n_roi np.array
Returns
-------
"""
if self.type is 'scalar':
self.roi_name = [self.roi_name[i] for i in np.arange(0, len(self.roi_name), 2)]
odd_f = np.arange(0, len(self.feat_name), 2)
self.feat_name = [self.feat_name[i] for i in odd_f]
if weight is None:
weight = np.ones(self.meas.shape)
weight[np.isnan(self.meas)] = 0
else:
weight = np.repeat(weight, self.meas.shape[1]/weight.shape[1], axis=1)
if meth is 'single':
for f in odd_f:
meas = self.meas[:, f:f+2]
bool_nan = np.isnan(self.meas)
index = np.logical_xor(bool_nan[:, 0], bool_nan[:, 1])
value = np.where(np.isnan(meas[index, 0]), meas[index, 1], meas[index, 0])
meas[index, :] = np.repeat(value[..., np.newaxis], 2, axis=1)
elif meth is 'both':
pass
odd_meas = self.meas[:, odd_f] * weight[:, odd_f]
even_meas = self.meas[:, odd_f+1] * weight[:, odd_f+1]
self.meas = (odd_meas + even_meas)/(weight[:, odd_f] + weight[:, odd_f+1])
else:
self.roi_name = [self.roi_name[i] for i in np.arange(0, len(self.roi_name), 2)]
n_subj, n_feat = self.meas.shape
meas = np.reshape(self.meas, (n_subj, -1, 3))
odd_f = np.arange(0, meas.shape[1], 2)
f_index = []
for i in np.arange(0, meas.shape[1], 2):
for j in [0, 1, 2]:
f_index.append(i*3+j)
self.feat_name = [self.feat_name[i] for i in f_index]
if meth is 'single':
for f in odd_f:
f_meas = meas[:, f:f+2, :]
bool_nan = np.isnan(np.prod(f_meas, axis=2))
index = np.logical_xor(bool_nan[:, 0], bool_nan[:, 1])
value = np.where(np.isnan(f_meas[index, 0, :]), f_meas[index, 1, :], f_meas[index, 0, :])
meas[index, f:f+2, :] = np.repeat(value[:, np.newaxis, :], 2, axis=1)
meas[:, odd_f+1, 0] = -meas[:, odd_f+1, 0]
elif meth is 'both':
meas[:, odd_f+1, 0] = -meas[:, odd_f+1, 0]
self.meas = np.reshape((meas[:, odd_f, :] + meas[:, odd_f+1, :])/2, (n_subj, -1))
def feature_description(self, feat_sel=None, figure=False):
"""
feature description and plot
Parameters
----------
feat_sel: feature selection, index for feature of interest, a np.array
figure : to indicate whether to plot figures, True or False
Returns
-------
feat_stats: statistics for each feature, a 5xnFeat np.array
rows are [mean, std, n_sample, t, p], respectively.
"""
if feat_sel is None:
feat_sel = np.arange(self.meas.shape[1])
elif isinstance(feat_sel, list):
feat_sel = np.array(feat_sel)
feat_stats = np.zeros((5, feat_sel.shape[0]))
for f in np.arange(feat_sel.shape[0]):
meas = self.meas[:, feat_sel[f]]
meas = meas[~np.isnan(meas)]
[t, p] = stats.ttest_1samp(meas, 0)
feat_stats[:, f] = [np.mean(meas), np.std(meas), meas.shape[0], t, p]
if figure:
for f in feat_sel:
feat_name = self.feat_name[f]
meas = self.meas[:, f]
meas = meas[~np.isnan(meas)]
if meas.shape[0] < 100:
n_bin = 10
else:
n_bin = np.fix(meas.shape[0]/10)
fig, ax = plt.subplots()
plt.hist(meas, bins=n_bin)
plt.xlabel(feat_name)
plt.ylabel('Frequency counts')
plt.title('Histogram')
x0, x1 = ax.get_xlim()
y0, y1 = ax.get_ylim()
ax.set_aspect((x1-x0)/(y1-y0))
plt.show()
return feat_stats
def feature_relation(self, feat_sel=None, figure=False):
"""
relations among features
Parameters
----------
feat_sel: feature selection, index for feature of interest, a np.array
figure : to indicate whether to plot figures, True or False
Returns
-------
feat_corr: correlation matrix of features, nFeat x nFeat np.array
n_sample: number of samples which have all features
"""
if feat_sel is None:
feat_sel = np.arange(self.meas.shape[1])
elif isinstance(feat_sel, list):
feat_sel = np.array(feat_sel)
corr = np.zeros((feat_sel.shape[0], feat_sel.shape[0]))
pval = np.copy(corr)
n_sample = np.copy(corr)
for i in np.arange(feat_sel.shape[0]):
for j in np.arange(i+1, feat_sel.shape[0], 1):
meas1 = self.meas[:, feat_sel[i]]
meas2 = self.meas[:, feat_sel[j]]
samp_sel = ~np.isnan(meas1 * meas2)
n_sample[i, j] = np.count_nonzero(samp_sel)
x = meas1[samp_sel]
y = meas2[samp_sel]
[c, p] = stats.pearsonr(x, y)
corr[i, j] = c
pval[i, j] = p
if figure:
labels = [self.feat_name[i] for i in feat_sel]
plot_mat(corr.T, 'Feature correlation', labels, labels)
# plot for each feature
for i in np.arange(feat_sel.shape[0]):
for j in np.arange(i+1, feat_sel.shape[0], 1):
meas1 = self.meas[:, feat_sel[i]]
meas2 = self.meas[:, feat_sel[j]]
samp_sel = ~np.isnan(meas1 * meas2)
x = meas1[samp_sel]
y = meas2[samp_sel]
fig, ax = plt.subplots()
plt.scatter(x, y)
plt.plot(x, np.poly1d(np.polyfit(x, y, 1))(x))
x0, x1 = ax.get_xlim()
y0, y1 = ax.get_ylim()
ax.set_aspect((x1-x0)/(y1-y0))
ax.text(x0+0.1*(x1-x0), y0+0.9*(y1-y0), 'r = %.3f, p = %.3f' % (corr[i, j], pval[i, j]))
plt.xlabel(labels[i])
plt.ylabel(labels[j])
plt.title('Feature correlation')
plt.show()
return corr, pval, n_sample
def behavior_predict1(self, beh_meas, beh_name, feat_sel=None, figure=False):
"""
Univariate feature-wise predict for behavior
Parameters
----------
beh_meas: behavior measures, nSubj x nBeh np.array
beh_name: behavior name, a list
feat_sel: feature selection, index for feature of interest, a np.array
figure: true or false
Returns
-------
corr: correlation matrix between brain measurements and behavior measurements,
nFeat x nBeh np.array
p: p value matrix
n_sample: number of samples
"""
if feat_sel is None:
feat_sel = np.arange(self.meas.shape[1])
elif isinstance(feat_sel, list):
feat_sel = np.array(feat_sel)
if beh_meas.ndim == 1:
beh_meas = np.expand_dims(beh_meas, axis=1)
corr = np.zeros((feat_sel.shape[0], beh_meas.shape[1]))
pval = np.copy(corr)
n_sample = np.copy(corr)
for f in np.arange(feat_sel.shape[0]):
for b in np.arange(beh_meas.shape[1]):
meas = self.meas[:, feat_sel[f]]
beh = beh_meas[:, b]
samp_sel = ~np.isnan(meas * beh)
n_sample[f, b] = np.count_nonzero(samp_sel)
[c, p] = stats.pearsonr(meas[samp_sel], beh[samp_sel])
corr[f, b] = c
pval[f, b] = p
if figure:
beh_labels = beh_name
feat_labels = [self.feat_name[i] for i in feat_sel]
plot_mat(corr, 'Feature correlation', beh_labels, feat_labels)
# plot for each feature
for f in feat_sel:
for b in np.arange(beh_meas.shape[1]):
meas = self.meas[:, f]
beh = beh_meas[:, b]
samp_sel = ~np.isnan(meas * beh)
x = meas[samp_sel]
y = beh[samp_sel]
fig, ax = plt.subplots()
plt.scatter(x, y)
plt.plot(x, np.poly1d(np.polyfit(x, y, 1))(x))
x0, x1 = ax.get_xlim()
y0, y1 = ax.get_ylim()
ax.set_aspect((x1-x0)/(y1-y0))
ax.text(x0+0.1*(x1-x0), y0+0.9*(y1-y0),'r = %.3f, p = %.3f') % (corr[f, b], pval[f, b])
plt.xlabel(self.feat_name[f])
plt.ylabel(beh_name[b])
plt.title('Behavior predict')
plt.show()
return corr, pval, n_sample
def behavior_predict2(self, beh_meas, beh_name, contrast=None, feat_sel=None, figure=False):
"""
Parameters
----------
beh_meas: matrix for behavior measurements, n_subj x n_feat
beh_name: name for behavior measurements
contrast: contrast matrix, each row is a contrast, n_contrast x n_feat.
if contrast is set as None, we will contrast each feature to zero
feat_sel
figure
Returns
-------
stats: stats for the regression,(n_beh*3) x n_contrast np.array,
for each behavior, 1st row is slope, 2nd is t, 3rd is p
rows are behaviors, columns are features
dof: degree of freedom, n_beh x 1
r2 : r square of the fit, n_beh x 1
"""
if feat_sel is None:
feat_sel = np.arange(self.meas.shape[1])
elif isinstance(feat_sel, list):
feat_sel = np.array(feat_sel)
if contrast is None:
contrast = np.identity(feat_sel.shape[0])
if beh_meas.ndim == 1:
beh_meas = np.expand_dims(beh_meas, axis=1)
samp_sel = ~np.isnan(np.prod(self.meas, axis=1))
slope_stats = np.zeros((beh_meas.shape[1]*3, feat_sel.shape[0]))
for b in np.arange(beh_meas.shape[1]):
beh_sel = ~np.isnan(beh_meas[:, b])
sel = np.logical_and(samp_sel, beh_sel)
dof = np.count_nonzero(sel)
x = self.meas[np.ix_(sel, feat_sel)]
y = np.expand_dims(beh_meas[sel, b], axis=1)
glm = LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
glm.fit(x, y)
y_pred = glm.predict(x)
# total sum of squares
sst = ((y - y.mean())**2).sum()
# sum of squares of error
sse = ((y - y_pred)**2).sum()
r2 = 1 - sse/sst
beta = glm.coef_
slope_stats[b*3, :] = beta
t = np.zeros(beta.shape[1])
for i in np.arange(contrast.shape[0]):
c = np.expand_dims(contrast[i, :], axis=0)
t[i] = np.dot(c, beta.T)/np.sqrt((sse * np.dot(np.dot(c, np.linalg.inv(np.dot(x.T, x))), c.T)))
slope_stats[b*3+1, :] = t
slope_stats[b*3+2, :] = stats.t.sf(np.abs(t), dof)*2
if figure:
labels = [self.feat_name[i] for i in feat_sel]
for b in np.arange(0, slope_stats.shape[0], 3):
plot_bar(slope_stats[b, :], 'Behavior predict for %s' % beh_name[b], labels, 'Slope')
return slope_stats, r2, dof
def outlier_remove(self, outlier_sel):
"""
remove outlier
Parameters
----------
outlier_sel: outlier index, 1-d np.array
Returns
-------
self.meas: de-outlierd measurements
"""
nSamp = self.meas.shape[0] # number of sample
good_samp = np.ones(nSamp, dtype=bool)
good_samp[outlier_sel] = False
self.meas = self.meas[good_samp, :]
return self.meas
def hemi_asymmetry(self, feat_sel=None, figure=False):
"""
Parameters
----------
feat_sel: feature selection which hold the feature index of interest.
when meas type is scalar, selection index should be paired; when meas
type is geometry, selection index should be triple paired, and ordered
as x, y, z in each triple group
figure
Returns
-------
li_stats: stats for laterality index, 5xnFeat np.array,
rows are [mean, std, n_sample, t, p], columns are features
"""
if feat_sel is None:
feat_sel = np.arange(self.meas.shape[1])
elif isinstance(feat_sel, list):
feat_sel = np.array(feat_sel)
if self.type is 'scalar':
if (feat_sel.shape[0] % 2) != 0:
raise UserDefinedException('Feature index should be paired')
li_stats = np.zeros((5, feat_sel.shape[0]/2))
for f in np.arange(0, feat_sel.shape[0], 2):
meas = self.meas[:, feat_sel[f:f+2]]
meas = meas[~np.isnan(np.prod(meas, axis=1)), :]
li = (meas[:, 0] - meas[:, 1])/(meas[:, 0] + meas[:, 1])
[t, p] = stats.ttest_1samp(li, 0)
li_stats[:, f/2] = [np.mean(li), np.std(li), li.shape[0], t, p]
else:
if (feat_sel.shape[0] % 2) != 0 and (feat_sel.shape[0] % 3) != 0:
raise UserDefinedException('Feature index should triple paired')
li_stats = np.zeros((5, feat_sel.shape[0]/2))
n_subj, n_feat = self.meas.shape
meas = self.meas[:, feat_sel]
meas = np.reshape(meas, (n_subj, -1, 3))
for f in np.arange(0, meas.shape[1], 2):
f_meas = meas[:, feat_sel[f:f+2], :]
f_meas = f_meas[~np.isnan(np.prod(f_meas, axis=(1, 2))), :, :]
f_meas[:, :, 0] = np.absolute(f_meas[:, :, 0])
li = np.squeeze(f_meas[:, 0, :] - f_meas[:, 1, :])
[t, p] = stats.ttest_1samp(li, 0)
f_stats = np.vstack((np.mean(li, axis=0), np.std(li, axis=0), np.repeat(li.shape[0], 3), t, p))
li_stats[:, np.arange((f/2)*3, ((f/2)+1)*3)] = f_stats
if figure:
if self.type is 'scalar':
feat_labels = [self.feat_name[i] for i in feat_sel[::2]]
else:
feat_labels = []
for i in np.arange(0, feat_sel.shape[0]/3, 2):
for j in [0, 1, 2]:
feat_labels.append(self.feat_name[i*3+j])
plot_bar(li_stats[0, :], 'Laterality index', feat_labels, 'LI score', li_stats[1, :])
return li_stats
def gender_diff(self, feat_sel=None, figure=False):
"""
Parameters
----------
feat_sel
figure
Returns
-------
gd_stats: statistics for gender difference, 5xnFeat
rows are [cohen_d, n_male, n_female, t, p]; columns are features
"""
if feat_sel is None:
feat_sel = np.arange(self.meas.shape[1])
elif isinstance(feat_sel, list):
feat_sel = np.array(feat_sel)
subj_gender = np.ones(len(self.subj_gender), dtype=bool)
f_idx = [i for i, g in enumerate(self.subj_gender) if g == 'f']
subj_gender[f_idx] = False
gd_stats = np.zeros((5, feat_sel.shape[0]))
for f in np.arange(feat_sel.shape[0]):
meas = self.meas[:, feat_sel[f]]
idx = ~np.isnan(meas)
meas = meas[idx]
gender = subj_gender[idx]
n_male = np.count_nonzero(gender)
n_female = meas.shape[0] - n_male
d = cohen_d(meas[gender], meas[~gender])
[t, p] = stats.ttest_ind(meas[gender], meas[~gender], equal_var=0)
gd_stats[:, f] = [d, n_male, n_female, t, p]
if figure:
xlabels = [self.feat_name[i] for i in feat_sel]
plot_bar(gd_stats[0, :], 'Gender differences', xlabels, 'Cohen d')
return gd_stats