-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcakernel.py
283 lines (267 loc) · 12.5 KB
/
pcakernel.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
import argparse
import nibabel as nb
import numpy as np
import os
import time
import torch
from copy import deepcopy
import h5py, hdf5plugin
from util import create_train_idxs
def direct_project(y, normalizationmode, nostd=False, anon=False):
train_ids, _ = create_train_idxs(395)
if anon:
mask = nb.load("mni_icbm152_nlin_asym_09c/mni_icbm152_t1_tal_nlin_asym_09c_headmask_defaced.nii").get_fdata()
else:
mask = nb.load("mni_icbm152_nlin_asym_09c/mni_icbm152_t1_tal_nlin_asym_09c_headmask.nii").get_fdata()
mask = mask[:192,2:226,:192][None,None,:,:,:]
mask = torch.Tensor(mask)
with h5py.File(f"icov_x_{normalizationmode}.h5", "r") as hfout:
mean = np.moveaxis(hfout["mean"][:],3,0)[None,:,:,:,:].astype(np.float32)
std = np.sqrt(np.moveaxis(hfout["var"][:],3,0))[None,:,:,:,:].astype(np.float32)
y_z = (y-mean)/(1.0 if nostd else std)
residual = deepcopy(y_z)
with h5py.File(f"x_{normalizationmode}.h5", "r") as hf:
x = hf['x']
for ii,i in enumerate(train_ids):
print(f"{ii},{i}")
xi_z = torch.Tensor((x[i].astype(np.float32)-mean)/(1.0 if nostd else std))
xi_norm = torch.norm(xi_z*mask)
coef = (residual*xi_z*mask).sum()/xi_norm
print(coef)
residual = residual - coef * xi_z/xi_norm
#print(np.linalg.norm(residual))
return y_z - residual, residual
def pca_project(y, normalizationmode, nostd=False):
train_ids, _ = create_train_idxs(395)
#train_ids = train_ids[:10]
#kernelrow = compute_kernelrow(y, normalizationmode, nostd)
kernelrow = compute_kernelrow_local(y, normalizationmode, nostd)
print(kernelrow)
with h5py.File(f"localkernel_{normalizationmode}{'_nostd' if nostd else ''}.h5", "r") as hfout:
kernel = torch.Tensor(hfout['kernel'][0,0,0])
kernel = kernel[train_ids,:][:,train_ids]
eps = kernel.diagonal().mean()/10
coefrow = torch.matmul(kernelrow[0,0,0], torch.inverse(kernel + eps*torch.eye(kernel.shape[-1])))
projection = 0*y
with h5py.File(f"icov_x_{normalizationmode}.h5", "r") as hfout:
mean = np.moveaxis(hfout["mean"][:],3,0)[None,:,:,:,:]
std = np.sqrt(np.moveaxis(hfout["var"][:],3,0))[None,:,:,:,:]
y_z = (y-mean)/(1.0 if nostd else std)
with h5py.File(f"x_{normalizationmode}.h5", "r") as hf:
x = hf['x']
for ii,i in enumerate(train_ids):
print(coefrow[0,ii])
print(ii)
projection += coefrow[0,ii] * (x[i].astype(np.float32)-mean)/(1.0 if nostd else std)
return projection, y_z - projection
def pca_project_test(test_ids, cuda=False):
train_ids, _ = create_train_idxs(395)
with h5py.File(f"localkernel_z.h5", "r") as hfout:
kernel = torch.Tensor(hfout['kernel'][0,0,0])
if cuda: kernel.cuda()
kernelrows = kernel[test_ids,:][:,train_ids]
kernel = kernel[train_ids,:][:,train_ids]
eps = kernel.diagonal().mean()/10
epseye = torch.eye(kernel.shape[-1])
if cuda: epseye.cuda()
coefrows = torch.matmul(kernelrows, torch.inverse(kernel + eps*epseye))
print(coefrows)
residuals = torch.zeros(len(test_ids),9,192,224,192)
if cuda: residuals.cuda()
with h5py.File(f"icov_x_z.h5", "r") as hfout:
mean = torch.Tensor(np.moveaxis(hfout["mean"][:],3,0)[None,:,:,:,:])
std = torch.Tensor(np.sqrt(np.moveaxis(hfout["var"][:],3,0))[None,:,:,:,:])
if cuda:
mean.cuda()
std.cuda()
print("loading h5 file")
with h5py.File(f"x_z.h5", "r") as hf:
x = hf['x']
print("loading test data")
for ii,i in enumerate(test_ids):
print(ii)
xi = torch.Tensor(x[i].astype(np.float32))
if cuda: xi.cuda()
residuals[ii] = (xi-mean)/std
print("updating residuals")
for ii,i in enumerate(train_ids):
xi = torch.Tensor(x[i].astype(np.float32))
if cuda: xi.cuda()
print(ii)
residuals -= coefrows[:,ii].view(-1,1,1,1,1) * (xi-mean)/std
#print((residuals**2).sum())
return residuals
def compute_kernelrow_local(y, normalizationmode, nostd=False, anon=False):
if anon:
mask = nb.load("mni_icbm152_nlin_asym_09c/mni_icbm152_t1_tal_nlin_asym_09c_headmask_defaced.nii").get_fdata()
else:
mask = nb.load("mni_icbm152_nlin_asym_09c/mni_icbm152_t1_tal_nlin_asym_09c_headmask.nii").get_fdata()
mask = mask[:192,2:226,:192][None,None,:,:,:]
nmask = mask.sum()
mask = torch.Tensor(mask)
train_ids, _ = create_train_idxs(395)
#train_ids = train_ids[:10]
with h5py.File(f"icov_x_{normalizationmode}.h5", "r") as hfout:
mean = np.moveaxis(hfout["mean"][:],3,0)[None,:,:,:,:]
std = np.sqrt(np.moveaxis(hfout["var"][:],3,0))[None,:,:,:,:]
y_z = (y-mean)/(1.0 if nostd else std)
with h5py.File(f"x_{normalizationmode}.h5", "r") as hf:
x = hf['x']
n = len(train_ids)
chunks = x.chunks
kmeanrow = torch.zeros(1,1,1,1,n)
Nmask = mask.sum()
for i in range(x.shape[2]//x.chunks[2]):
for j in range(x.shape[3]//x.chunks[3]):
for k in range(x.shape[4]//x.chunks[4]):
print([i,j,k])
maxi = (i+1)*chunks[2]
maxj = (j+1)*chunks[3]
maxk = (k+1)*chunks[4]
nmask = mask[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
].sum()
if nmask == 0:
continue
r = np.concatenate([x[ii:ii+1,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
] for ii in train_ids], axis=0)
r = r.astype(np.float32)
ry = y_z[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
]
r = torch.Tensor(r)
#mean = r.mean(dim=0, keepdim=True)
r -= mean[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
]
#ry -= mean
#std = r.std(dim=0, keepdim=True)
if not nostd:
r /= std[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
]
#ry /= std
r = r*mask[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
]
ry = ry*mask[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
]
r = r.view(r.shape[0],-1)
ry = ry.view(ry.shape[0],-1)
localkernelrow = torch.matmul(ry, torch.transpose(r,0,1)).view(1,1,1,1,n)/nmask
kmeanrow += localkernelrow*nmask/Nmask
print(kmeanrow[0,0,0,0,0])
return kmeanrow
def compute_kernelrow(y, normalizationmode, nostd=False):
mask = nb.load("mni_icbm152_nlin_asym_09c/mni_icbm152_t1_tal_nlin_asym_09c_headmask.nii").get_fdata()
mask = mask[:192,2:226,:192][None,None,:,:,:]
mask = torch.Tensor(mask)
Nmask = mask.sum()
train_ids, _ = create_train_idxs(395)
with h5py.File(f"icov_x_{normalizationmode}.h5", "r") as hfout:
mean = np.moveaxis(hfout["mean"][:],3,0)[None,:,:,:,:]
std = np.sqrt(np.moveaxis(hfout["var"][:],3,0))[None,:,:,:,:]
yz = (y-mean)
if not nostd:
yz /= std
with h5py.File(f"x_{normalizationmode}.h5", "r") as hf:
x = hf['x']
kmeanrow = torch.zeros(1,1,1,1,len(train_ids))
for i in train_ids:
print(i)
xi = torch.Tensor((x[i].astype(np.float32)-mean)/(1.0 if nostd else std))
kmeanrow[0,0,0,0,i] = torch.sum((yz*xi)*mask/Nmask)
print(kmeanrow[0,0,0,0,i])
return kmeanrow
def main2(args):
mask = nb.load("mni_icbm152_nlin_asym_09c/mni_icbm152_t1_tal_nlin_asym_09c_headmask.nii").get_fdata()
mask = mask[:192,2:226,:192][None,None,:,:,:]
Nmask = mask.sum()
mask = torch.Tensor(mask)
with h5py.File(f"icov_x_{args.normalizationmode}.h5", "r") as hfout:
mean = np.moveaxis(hfout["mean"][:],3,0)[None,:,:,:,:]
std = np.sqrt(np.moveaxis(hfout["var"][:],3,0))[None,:,:,:,:]
with h5py.File(f"x_{args.normalizationmode}.h5", "r") as hf:
with h5py.File(f"localkernel_{args.normalizationmode}{'_nostd' if args.nostd else ''}.h5", "w") as hfout:
x = hf['x']
n = x.shape[0]
chunks = x.chunks
kmean = torch.zeros(1,1,1,n,n)
for i in range(x.shape[2]//x.chunks[2]):
for j in range(x.shape[3]//x.chunks[3]):
for k in range(x.shape[4]//x.chunks[4]):
print([i,j,k])
maxi = (i+1)*chunks[2]
maxj = (j+1)*chunks[3]
maxk = (k+1)*chunks[4]
nmask = mask[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
].sum()
if nmask > 0:
r = x[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
].astype(np.float32)
r = torch.Tensor(r)
r -= mean[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
]
if not args.nostd:
r /= std[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
]
#mean = r.mean(dim=0, keepdim=True)
#r -= mean
#print(mean[0,:,0,0,0])
#std = r.std(dim=0, keepdim=True)
#r /= std
r = r*mask[:,:,
i*x.chunks[2]:maxi,
j*x.chunks[3]:maxj,
k*x.chunks[4]:maxk
]
r = r.view(r.shape[0],-1)
localkernel = torch.matmul(r, torch.transpose(r,0,1)).view(1,1,1,n,n)/nmask
kmean += localkernel*nmask/Nmask
#print(kmean)
else:
localkernel = torch.zeros(1,1,1,n,n)
if i==0 and j==0 and k==0:
hfout.create_dataset('localkernel', data = localkernel.cpu().numpy().astype(np.float32),
dtype=np.float32, chunks=(1,1,1,n,n) ,
maxshape=(*(x.chunks[2:]), n, n), **hdf5plugin.Blosc())
else:
s = hfout["localkernel"].shape
hfout["localkernel"].resize((max(i+1,s[0]),max(j+1,s[1]),max(k+1,s[2]),n,n))
hfout["localkernel"][i:i+1,j:j+1,k:k+1] = localkernel.cpu().numpy().astype(np.float32)
hfout.create_dataset('kernel', data=kmean.cpu().numpy().astype(np.float32), dtype=np.float32, **hdf5plugin.Blosc())
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="parse args")
parser.add_argument('-n','--normalizationmode', default="z", type=str, help='normalization mode')
parser.add_argument('--nostd', action='store_true', default=False, help='whether not to divide by std')
args = parser.parse_args()
print(args)
main2(args)