-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMappingModelToData.py
206 lines (150 loc) · 10.3 KB
/
MappingModelToData.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
import numpy as np
import scipy.stats as ss
import time
from sklearn.decomposition import PCA
from MappingV36 import MappingV36 as Mapping
Mapping = Mapping()
resultdir = '/home/tahereh/Documents/Research/Results/Mapping_unit_test/'
class MappingModelToData:
def __init__(self, M, D, PCA_ncomponents_list, explained_var_ratio_list):
self.D = D
self.M = M
self.PCA_ncomponents_list = PCA_ncomponents_list
self.explained_var_ratio_list = explained_var_ratio_list
def get_transformed_model(self, PCA_ncomponents, explained_var_ratio):
# PCA_ncomponents=-1 means no PCA will be applied
# PCA_ncomponents=0 means we required a given explained_var_ratio
# PCA_ncomponents>0 means perform PCA with PCA_ncomponents components
if PCA_ncomponents == 0:
ncomponents = int(self.M.shape[1]*explained_var_ratio)
evp = 0
while evp < explained_var_ratio:
pca = PCA(n_components=ncomponents)
pca.fit(self.M)
evp = pca.explained_variance_ratio_
ncomponents += 1
M_PCA = pca.transform(self.M)
elif PCA_ncomponents > 0:
pca = PCA(n_components=PCA_ncomponents)
pca.fit(self.M)
evp = pca.explained_variance_ratio_
M_PCA = pca.transform(self.M)
elif PCA_ncomponents == -1:
M_PCA = self.M
return M_PCA
def get_mappings(self, Data_params, reg_methods, reg_params_list, spearman_brown, report_sitefit, report_popfit):
ni, nf, nt, nfoldi, nfoldt, trainfraci, splitfract, data_unit_indices = Data_params
r12 = np.zeros((len(data_unit_indices), nfoldi, nfoldt))
r11 = np.zeros((len(data_unit_indices), nfoldi, nfoldt))
r22 = np.zeros((len(data_unit_indices), nfoldi, nfoldt))
r12_sitefit = np.zeros((len(data_unit_indices), nfoldi, nfoldt))
r11_sitefit = np.zeros((len(data_unit_indices), nfoldi, nfoldt))
r22_sitefit = np.zeros((len(data_unit_indices), nfoldi, nfoldt))
r12_reg = np.zeros((len(data_unit_indices), len(reg_methods), nfoldi, nfoldt))
r11_reg = np.zeros((len(data_unit_indices), len(reg_methods), nfoldi, nfoldt))
r22_reg = np.zeros((len(data_unit_indices), len(reg_methods), nfoldi, nfoldt))
r12_reg_sitfit = np.zeros((len(data_unit_indices), len(reg_methods), nfoldi, nfoldt))
r11_reg_sitfit = np.zeros((len(data_unit_indices), len(reg_methods), nfoldi, nfoldt))
r22_reg_sitfit = np.zeros((len(data_unit_indices), len(reg_methods), nfoldi, nfoldt))
for fi in range(nfoldi):
# train/test image split
ind = np.random.permutation(ni)
indtraini = ind[:int(ni * trainfraci)]
indtesti = ind[int(ni * trainfraci):]
for ft in range(nfoldt):
indices = np.random.permutation(nt)
D1 = self.D[:, np.array(data_unit_indices)[:, np.newaxis], indices[:int(nt * splitfract)]].mean(2)
D2 = self.D[:, np.array(data_unit_indices)[:, np.newaxis], indices[int(nt * splitfract):]].mean(2)
# Pseudo-inverse
PCA_ncomponents = self.PCA_ncomponents_list[0]
explained_var_ratio = self.explained_var_ratio_list[0]
M_PCA = self.get_transformed_model(PCA_ncomponents, explained_var_ratio)
# pop fit
# NUMERATOR: Fit on train, test on test
Ahat = np.dot(np.linalg.pinv(M_PCA[indtraini, :]), D1[indtraini, :])
D1_test, D1_pred = D1[indtesti, :], np.dot(M_PCA[indtesti, :], Ahat)
r12[:, fi, ft] = [ss.pearsonr(D1_pred[:, indf], D1_test[:, indf])[0] for indf in range(len(data_unit_indices))]
# DENOMINATOR consistency between trial sets 1 & 2 on test images
D2_test = D2[indtesti, :]
r22[:, fi, ft] = [ss.pearsonr(D1_test[:, indf], D2_test[:, indf])[0] for indf in range(len(data_unit_indices))]
# DENOMINATOR LHS map consistency between trial sets 1 & 2 on test images
Ahat1 = np.dot(np.linalg.pinv(M_PCA[indtraini, :]), D1[indtraini, :])
Ahat2 = np.dot(np.linalg.pinv(M_PCA[indtraini, :]), D2[indtraini, :])
lhs1, lhs2 = np.dot(M_PCA[indtesti, :], Ahat1), np.dot(M_PCA[indtesti, :], Ahat2)
r11[:, fi, ft] = [ss.pearsonr(lhs1[:, indf], lhs2[:, indf])[0] for indf in range(len(data_unit_indices))]
# site fit
# NUMERATOR: Fit on train, test on test
for n in range(len(data_unit_indices)):
Ahat = np.dot(np.linalg.pinv(M_PCA[indtraini, n][:, np.newaxis]), D1[indtraini, n][:, np.newaxis])
print(M_PCA[indtraini, n].shape, M_PCA[indtraini, n][np.newaxis, 0].shape, Ahat.shape)
D1_test, D1_pred = D1[indtesti, n][:, np.newaxis], np.dot(M_PCA[indtesti, n][:, np.newaxis], Ahat)
r12_sitefit[n, fi, ft] = ss.pearsonr(D1_pred, D1_test)[0]
# DENOMINATOR consistency between trial sets 1 & 2 on test images
D2_test = D2[indtesti, n][:, np.newaxis]
r22_sitefit[n, fi, ft] = ss.pearsonr(D1_test, D2_test)[0]
# DENOMINATOR LHS map consistency between trial sets 1 & 2 on test images
Ahat1 = np.dot(np.linalg.pinv(M_PCA[indtraini, n][:, np.newaxis]), D1[indtraini, n][:, np.newaxis])
Ahat2 = np.dot(np.linalg.pinv(M_PCA[indtraini, n][:, np.newaxis]), D2[indtraini, n][:, np.newaxis])
lhs1, lhs2 = np.dot(M_PCA[indtesti, n][:, np.newaxis], Ahat1), np.dot(M_PCA[indtesti, n][:, np.newaxis], Ahat2)
r11_sitefit[n, fi, ft] = ss.pearsonr(lhs1, lhs2)[0]
# Regression
start = time.time()
time_sitefit = []
time_popfit = []
print(time.time() - start, end="")
for r, reg_method in enumerate(reg_methods):
reg_params = reg_params_list[r]
train_inds, test_inds = indtraini, indtesti
PCA_ncomponents = self.PCA_ncomponents_list[r+1] # index is r+1 because pinv was the first
explained_var_ratio = self.explained_var_ratio_list[r+1]
M_PCA = self.get_transformed_model(PCA_ncomponents, explained_var_ratio)
model_features_X, half1, half2 = M_PCA, D1, D2
zscored_observations = False
return_fitted_reg = False
# population fit
if report_popfit[r]:
start_popfit = time.time()
if spearman_brown:
_, r_Nom_sites = Mapping.Numerator(train_inds, test_inds, model_features_X, np.mean([half2, half2], axis=0), reg_method,
reg_params, zscored_observations, return_fitted_reg)
else:
_, r_Nom_sites = Mapping.Numerator(train_inds, test_inds, model_features_X, half1, reg_method,
reg_params, zscored_observations, return_fitted_reg)
_, r_RHS_sites = Mapping.Denom_RHS(test_inds, half1, half2)
_, r_LHS_sites = Mapping.Denom_LHS(train_inds, test_inds, model_features_X, half1, half2, reg_method, reg_params,
zscored_observations, return_fitted_reg)
r12_reg[:, r, fi, ft] = r_Nom_sites
if spearman_brown:
r_RHS_sites_sb = [Mapping.spearman_brown_correction(r) for r in r_RHS_sites]
r_LHS_sites_sb = [Mapping.spearman_brown_correction(r) for r in r_LHS_sites]
r22_reg[:, r, fi, ft] = r_RHS_sites_sb
r11_reg[:, r, fi, ft] = r_LHS_sites_sb
else:
r22_reg[:, r, fi, ft] = r_RHS_sites
r11_reg[:, r, fi, ft] = r_LHS_sites
time_popfit.append([time.time() - start_popfit])
# site fit
if report_sitefit[r]:
start_sitefit = time.time()
for n in range(len(data_unit_indices)):
return_fitted_reg = False
r_Nom, _ = Mapping.Numerator(train_inds, test_inds, model_features_X, half1[:, n], reg_method, reg_params,
zscored_observations, return_fitted_reg)
r_LHS, _ = Mapping.Denom_LHS(train_inds, test_inds, model_features_X, half1[:, n], half2[:, n], reg_method,
reg_params, zscored_observations, return_fitted_reg)
r12_reg_sitfit[n, r, fi, ft] = r_Nom
r11_reg_sitfit[n, r, fi, ft] = r_LHS
if report_popfit[r]:
r22_reg_sitfit[:, r, fi, ft] = r_RHS_sites
else:
r_RHS, _ = Mapping.Denom_RHS(test_inds, half1[:, n], half2[:, n])
r22_reg_sitfit[n, r, fi, ft] = r_RHS
time_sitefit.append([time.time() -start_sitefit])
print('popfit for %s took %.2f seconds' %(reg_method, np.mean(time_popfit)))
print('sitefit for %s took %.2f seconds' %(reg_method, np.mean(time_sitefit)))
r12, r11, r22 = np.mean(r12, 2), np.mean(r11, 2), np.mean(r22, 2)
r12_sitefit, r11_sitefit, r22_sitefit = np.mean(r12_sitefit, 2), np.mean(r11_sitefit, 2), np.mean(r22_sitefit, 2)
r12_reg, r11_reg, r22_reg = np.mean(r12_reg.mean(3), 2), np.mean(r11_reg.mean(3), 2), np.mean(r22_reg.mean(3), 2)
r12_reg_sitfit, r11_reg_sitfit, r22_reg_sitfit = np.mean(r12_reg_sitfit.mean(3), 2), np.mean(r11_reg_sitfit.mean(3), 2), np.mean(r22_reg_sitfit.mean(3), 2)
data_list = [r12,r11,r22,r12_sitefit,r11_sitefit,r22_sitefit,r12_reg,r11_reg,r22_reg,r12_reg_sitfit,r11_reg_sitfit,r22_reg_sitfit]
return data_list