-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping.py
378 lines (281 loc) · 12.4 KB
/
mapping.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
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 23 09:43:36 2016
Functions to project cortical data onto a 2D plane via the sphere and back again;
select patches and augment data/labels
@author: ecr05
"""
import sys
import os
import numpy as np
import copy
import matplotlib.pyplot as plt
import Interpolation as intp
import pandas as pd
def sliceplane(plane, start_height, start_width, x_size, y_size):
# slice patch from 3D array dim (x,y,channels)
newdata = plane[int(start_height):int(start_height+x_size), int(start_width):int(start_width+y_size),:]
return newdata
def normalize(data,norm_axis):
"""
normalise feature maps
Parameters
----------
data : multivariate feature maps as 3d np.arrays dims (x,y,channels)
Returns
-------
datanormed : normalised data
"""
datastd = np.std(data,axis=norm_axis)
# print('in normalise', data.shape ,norm_axis, datastd.shape )
# print('mean',np.mean(data,axis=norm_axis).shape )
if np.where(datastd == 0)[0].shape != (0,):
print('isnan')
datastd[np.where(datastd==0)]=1;
datanormed = (data - np.mean(data,axis=norm_axis)) / datastd
if np.where(np.isnan(datanormed))[0].shape != (0,):
print('isnan2')
#print('mean normalised',np.mean(datanormed,axis=norm_axis))
#print('std normalised',np.std(datanormed,axis=norm_axis))
return datanormed
def rodrigues_rotation(origin,newpt):
"""
rotate sphere to move longitude 0, latitude 45 degrees to a new position centred on the newpt
Parameters
----------
origin: current projection centre
newpt: coordinate intended as new projection centre
Returns
-------
Rot: (3x3) array rotation matrix
"""
#
#http://electroncastle.com/wp/?p=39
#https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
p1=intp.normalize(origin)
p2=intp.normalize(newpt)
angle = np.arccos(np.dot(p1,p2)) # get angle of rotation
if angle> np.pi:
raise ValueError('rodrigues rotation error: the specified rotation angle exceeds pi')
else:
print("rodrigues rotation: angle of rotation is %f"%angle)
ID = np.identity(3)
# get axis of rotation
cross = np.cross(p1,p2)
cross = intp.normalize(cross)
# get rotation matrix
if angle == 0:
Rot = ID
elif intp.normalize(cross) == 0:
Rot = np.negative(ID);
else:
u = np.array([[0, -cross[2], cross[1]],
[cross[2], 0, -cross[0]],
[-cross[1], cross[0],0]])
Rot = ID+np.sin(angle)*u+(1-np.cos(angle))*(u.dot(u))
return Rot
def rotate(rotation, coords):
"""
rotate coordinates
Parameters
----------
rotation: rotation matrix
coords: coordinate matrix
Returns
-------
Rot: (nx3) array of rotated n points
"""
coordsrot = np.dot(rotation,np.transpose(coords));
return np.transpose(coordsrot)
def rescale_labels(labels,labelfunc):
# rest original label indexing
func=copy.deepcopy(labelfunc)
for l,label in enumerate(labels):
x = np.where(labelfunc == l+1)#[0]
func[(x)]=label
return func
def zero_labels(labels,labelfunc):
# labels must index concurrently from 0
func=copy.deepcopy(labelfunc)
func = func * 0
newlabel=[]
for l,label in enumerate(labels):
x = np.where(labelfunc == label)#[0]
func[(x)]=l+1
newlabel.append(l+2)
return func,newlabel
def invert_projection_test(data2D,coords,interp):
DATA=np.zeros((coords.shape[0],data2D.shape[2]))
# fill data with cortical features
for x,index in enumerate(coords):
flatindex=interp.invcorrespondence[x];
latind=flatindex[0]
lonind=flatindex[1]
# fill data with cortical features
DATA[x,:]=data2D[latind,lonind,:]
return DATA
def invert_patch_categories_full(img,coords,interp,newH,newW,labels=[]):
#plt.imshow(img[0,:,:,0])
#plt.show()
#zeropadded=inv_slice(croppedrescale2,newH,newW,h,w)
SURFdata=invert_projection_test(img,coords,interp)
#SURFdata=invert_projection_test(img[0,:,:,:],coords,interp,newH,newW,lons)
if len(labels) > 0:
rescaledlabels = rescale_labels(labels, SURFdata)
print('rescaled shape', rescaledlabels.shape)
return rescaledlabels
else:
return SURFdata
def remove_outliers(d,data):
# quick hack to get rid of xtreme outliers (before there was some really high values)
if (np.max(data)>np.mean(data)+10*np.std(data)) or (np.min(data)<np.mean(data)-10*np.std(data)):
print(d, 'data stats:', np.mean(data), np.std(data), np.max(data), np.min(data),np.mean(data)+10*np.std(data),np.mean(data)-10*np.std(data))
data[np.where(data>np.mean(data)+10*np.std(data))]= np.mean(data)
def reformat_datamat_rows_to_columns(ALLDATA):
newmat=np.zeros((ALLDATA.samples*ALLDATA.DATA.shape[0],ALLDATA.features))
for f in range(ALLDATA.features):
for s in range(ALLDATA.samples):
#print(f,s,'ALLDATA.DATA[:,f*s] shape',s*ALLDATA.DATA.shape[0],(s+1)*ALLDATA.DATA.shape[0],newmat.shape,ALLDATA.DATA.shape[0],ALLDATA.DATA[:,f*s].shape,newmat[s*ALLDATA.DATA.shape[0]:(s+1)*ALLDATA.DATA.shape[0],f].shape)
newmat[s*ALLDATA.DATA.shape[0]:(s+1)*ALLDATA.DATA.shape[0],f]= ALLDATA.DATA[:,s*ALLDATA.features+f]
return newmat
def reformat_datamat_columns_to_rows(datamat,ALLDATA):
newmat=np.zeros(ALLDATA.DATA.shape)
for f in range(ALLDATA.features):
for s in range(ALLDATA.samples):
#print(f,s,'datamat[s*ALLDATA.DATA.shape[0]:,f]',datamat[s*ALLDATA.DATA.shape[0]:(s+1)*ALLDATA.DATA.shape[0],f].shape,newmat[:,f*s].shape)
newmat[:,s*ALLDATA.features+f]= datamat[s*ALLDATA.DATA.shape[0]:(s+1)*ALLDATA.DATA.shape[0]:,f]
return newmat
def group_normalise(ALLDATA):
"""
normalise data across group; first restructures data s.t. all examples for each feature fit into one column
i.e. (n_subjects*n_vertices,n_features)
then it normalises and returns data to (n_vertices,n_subjects*n_features) format
Parameters
----------
ALLDATA: data for all subjects in (n_vertices,n_subjects*n_features) format
Returns
-------
"""
#alldatamean=np.mean(ALLDATA.DATA,axis=0)
newmat=reformat_datamat_rows_to_columns(ALLDATA)
newmat=normalize(newmat,0) # normalise along each column
newmat2=reformat_datamat_columns_to_rows(newmat,ALLDATA)
# =============================================================================
# diff=ALLDATA.DATA-newmat2
# print('diff',np.sum(diff))
# =============================================================================
#print('group_normalise_mean',np.mean(newmat,axis=0))
#ALLDATA._replace(DATA=newmat)
return newmat2
def project(DATAset, interp, nlats, nlons, lons):
"""
project points from sphere onto 2D grid
Parameters
----------
DATAset: labels/features to be projected
interp: saved correspondences between the sphere and 2D grid
nlats: number of bins in x direction
nlons: number of bins in y direction
lons: edges of longitude bins
Returns
-------
Rot: (nx3) array of rotated n points
"""
data = np.zeros((nlats, nlons, DATAset.shape[1]))
# fill data with cortical features
for index, x in np.ndenumerate(data[:, :, 0]):
latind = index[0]
lonind = index[1]
data[latind, lonind, :] = DATAset[interp.correspondence[index], :]
return data
def project_data(DATA,interp,data_paths,newH,newW,use_normalisation):
"""
project labels, featuremaps and optionally feature-correlation maps onto 2D plan
Parameters
----------
alllabels: np.array containing label surface data for all subjects within a group
alldata: np.array containing multivariate feature maps for all subjects in a group
allcorr: np.array containing correlation (of feature) maps for all subjects in a group
interp: saved correspondences between the sphere and 2D grid
outdir: output directory
newH: y dimensions of 2d projection
newW: x dimensions of 2d projection
lons: edges of longitude bins
abr: group type
aug: numerical index relating to the number of projection centres
usegroup: use group labels
usecorrelations: if defined use feature correlation maps and project these to 2D plane
Returns
-------
"""
delta_d=2.*np.pi/(newW-1); # width of longitude bin if w not in randw2:s
lons = (delta_d*np.indices((newW,1))[0,:,:]) #edges of longitude bins
# project data to 2D
alldata=DATA['data']
twoD = project(alldata.DATA, interp, newH, newW, lons)
if 'labels' in DATA:
print('in project data, labels== true')
# if working with label files then also project these to 2D
twoDL = project(DATA['labels'],interp,newH,newW,lons)
start=0
for subj in range(0,alldata.samples):
#print('subj', subj, DATA['data'].ids[subj] )
if 'labels' in DATA :
np.save(os.path.join(data_paths['Odirname'],data_paths['abr'] +'GrayScaleLabels-subj-'+ DATA['data'].ids[subj]), twoDL[:,:,subj])
if use_normalisation:
data_normed=normalize(np.reshape(twoD[:, :, start:start + alldata.features],(twoD.shape[0]*twoD.shape[1],alldata.features)),0)
# print(data_normed.shape,np.std(data_normed,0).shape,np.std(data_normed,0),np.mean(data_normed,0))
data=np.reshape(data_normed,twoD[:, :, start:start + alldata.features].shape)
else:
data=twoD[:, :, start:start + alldata.features]
np.save(os.path.join(data_paths['Odirname'], data_paths['abr'] + 'data_-subj-' + DATA['data'].ids[subj]), data)
start = start+alldata.features
def write_projection_paths(DATA, filename, data_paths, use_labels, use_normalisation):
"""
write paths out to file
Parameters
----------
samples: list of subj ids
filename: output filename
indir: path to data files
aug: indexing projection centres
use_grouplabels: ue group labels
use_correlations: save featuremap correlations
Returns
-------
"""
df = pd.DataFrame()
print('write projection paths')
for subj in range(0,DATA.samples):
#print('subj', subj, DATA.ids[subj],type(DATA.ids[subj]) )
if use_labels:
label = os.path.join(data_paths['Odirname'], data_paths['abr'] + 'GrayScaleLabels-subj-' + DATA.ids[subj]+ '.npy')
data = os.path.join(data_paths['Odirname'], data_paths['abr'] + 'data_-subj-' +DATA.ids[subj]+ '.npy')
row={};
row['fileid']= DATA.ids[subj]
row['data']= data
if use_labels:
row['labels']= label
df = df.append(row, ignore_index=True)
print('types',df['fileid'].dtype)
# print('df shape', df.shape)
# df['fileid']=df['fileid'].astype(int)
if data_paths['meta_csv'] is not None:
meta_data=pd.read_pickle(data_paths['meta_csv'])
print('types',type(meta_data['fileid']),type(df['fileid']))
# print('meta shape', meta_data.shape)
meta_data['fileid']=meta_data['fileid'].astype(object)
output=df.merge(meta_data, on=['fileid'])
else:
output=df
#output=output.drop(['fileid'], axis=1)
output.to_pickle(os.path.join(data_paths['Odirname'],filename))
# =============================================================================
# if use_correlations:
# corrdata=os.path.join(indir, abr + 'featurecorrelations-subj-' +DATA.ids[subj] + '-aug-' + aug + '.npy')
# meancorrdata=os.path.join(indir, abr + 'meanfeaturecorrelations-aug-' + aug+'.npy')
# #target.write(data + ' ' + label + ' ' + corrdata + ' ' + meancorrdata + '\n')
# else:
# target.write(data + ' ' + label + '\n')
#
# =============================================================================