-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathts2kit.py
593 lines (345 loc) · 16.4 KB
/
ts2kit.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import os
import os.path as osp
import glob
import numpy as np
import torch
import torch.nn as nn
from scipy.special import gammaln
from math import pi as PI
sqrt2 = np.sqrt(2.0);
##########################
######## Cache ###########
##########################
## If you'd like to save cached files in a different directory, then change this
## to the absolute path of said directory
defaultCacheDir = 'cache'
try:
################################################
### For integration with Mobius Convolutions ###
################################################
from cache.cache import cacheDir
except:
#####################################
### For use as standalone package ###
#####################################
cacheDir = defaultCacheDir
def clearTS2KitCache(cacheDir=cacheDir):
cFiles = glob.glob(osp.join(cacheDir, '*.pt'));
for l in range(len(cFiles)):
os.remove(cFiles[l]);
return 1;
#############################
######## Utilities ##########
#############################
## Driscoll-Healy Sampling Grid ##
# Input: Bandlimit B
# Output: "Meshgrid" spherical coordinates (theta, phi) of 2B X 2B Driscoll-Healy spherical grid
# These correspond to a Y-Z spherical coordinate parameterzation:
# [X, Y, Z] = [cos(theta) * sin(phi), sin(theta) * sin(phi), cos(phi)]
def gridDH(B):
k = torch.arange(0, 2*B).double();
theta = 2*PI*k / (2*B)
phi = PI*(2*k + 1) / (4*B);
theta, phi = torch.meshgrid(theta, phi, indexing='ij');
return theta, phi;
###########################################################
################ Discrete Legendre Transform ##############
###########################################################
## Recursive computation of d^l_mn ( pi/2)
def triHalfRecur(l, m, n):
denom = (-1 + l)*np.sqrt( (l-m)*(l+m)*(l-n)*(l+n) );
c1 = (1 - 2*l)*m*n/denom;
c2 = -1.0*l*np.sqrt( ( (l-1)*(l-1) - m*m ) * ( (l-1)*(l-1) - n*n ) ) / denom;
return c1, c2;
def generateLittleHalf(B):
fName = osp.join(cacheDir, 'littleHalf_{}.pt'.format(B));
if (osp.isfile(fName) == False):
#m, n -> m + (B-1), n + (B-1)
d = torch.empty( B, 2*B - 1, 2*B - 1).double().fill_(0);
# Fill first two levels (l = 0, l = 1)
d[0, B-1, B-1] = 1
d[1, -1 +(B-1), -1 + (B-1)] = 0.5;
d[1, -1 +(B-1), B-1] = 1.0 / sqrt2;
d[1, -1 +(B-1), B] = 0.5;
d[1, (B-1), -1 + (B-1)] = -1.0 / sqrt2;
d[1, (B-1), B] = 1.0 / sqrt2;
d[1, B, -1 + (B-1)] = 0.5;
d[1, B, (B-1)] = -1.0 / sqrt2;
d[1, B, B] = 0.5;
## Fill rest of values through Kostelec-Rockmore recursion
for l in range(2, B):
for m in range(0, l):
for n in range(0, l):
if ( (m == 0) and (n == 0) ):
d[l, B-1, B-1] = -1.0*( (l-1)/l )*d[l-2, B-1, B-1];
else:
c1, c2 = triHalfRecur(l, m, n);
d[l, m + (B-1), n + (B-1)]= c1 * d[l-1, m + (B-1), n + (B-1)] + c2 * d[l-2, m+(B-1), n+(B-1)];
for m in range(0, l+1):
lnV = 0.5*( gammaln(2*l + 1) - gammaln(l+m +1) - gammaln(l-m + 1) ) - l*np.log(2.0);
d[l, m+(B-1), l+(B-1)] = np.exp(lnV);
d[l, l+(B-1), m+(B-1)] = np.power(-1.0, l - m) * np.exp(lnV);
for m in range(0, l+1):
for n in range(0, l+1):
val = d[l, m+(B-1), n+(B-1)]
if ( (m != 0) or (n != 0) ):
d[l, -m + (B-1), -n + (B-1)] = np.power(-1.0, m-n)*val;
d[l, -m + (B-1), n + (B-1)] = np.power(-1.0, l-n)*val;
d[l, m+(B-1), -n + (B-1) ] = np.power(-1.0, l+m)*val;
torch.save(d, fName)
print('Computed littleHalf_{}'.format(B), flush=True);
else:
d = torch.load(fName);
return d;
def dltWeightsDH(B):
fName = osp.join(cacheDir, 'dltWeights_{}.pt'.format(B));
if (osp.isfile(fName) == False):
W = torch.empty(2*B).double().fill_(0);
for k in range(0, 2*B):
C = (2.0/B)*np.sin( PI*(2*k + 1) / (4.0*B) );
wk = 0.0;
for p in range(0, B):
wk += (1.0 / (2*p + 1) ) * np.sin( (2*k + 1)*(2*p + 1) * PI / (4.0 * B));
W[k] = C * wk;
torch.save(W, fName);
print('Computed dltWeights_{}'.format(B), flush=True);
else:
W = torch.load(fName);
return W;
## Inverse (orthogonal) DCT Matrix of dimension N x N
def idctMatrix(N):
fName = osp.join(cacheDir, 'idctMatrix_{}.pt'.format(N));
if (osp.isfile(fName) == False):
DI = torch.empty(N, N).double().fill_(0);
for k in range(0, N):
for n in range(0, N):
DI[k, n] = np.cos(PI*n*(k + 0.5)/N)
DI[:, 0] = DI[:, 0] * np.sqrt(1.0 / N);
DI[:, 1:] = DI[:, 1:] * np.sqrt(2.0 / N);
torch.save(DI, fName);
print('Computed idctMatrix_{}'.format(N), flush=True);
else:
DI = torch.load(fName);
return DI;
## Inverse (orthogonal) DST Matrix of dimension N x N
def idstMatrix(N):
fName = osp.join(cacheDir, 'idstMatrix_{}.pt'.format(N));
if (osp.isfile(fName) == False):
DI = torch.empty(N, N).double().fill_(0);
for k in range(0, N):
for n in range(0, N):
if (n == (N-1) ):
DI[k, n] = np.power(-1.0, k);
else:
DI[k, n] = np.sin(PI*(n+1)*(k + 0.5)/N);
DI[:, N-1] = DI[:, N-1] * np.sqrt(1.0 / N);
DI[:, :(N-1)] = DI[:, :(N-1)] * np.sqrt(2.0 / N);
torch.save(DI, fName);
print('Computed idstMatrix_{}'.format(N), flush=True);
else:
DI = torch.load(fName);
return DI;
# Normalization coeffs for m-th frequency (C_m)
def normCm(B):
fName = osp.join(cacheDir, 'normCm_{}.pt'.format(B));
if (osp.isfile(fName) == False):
Cm = torch.empty(2*B - 1).double().fill_(0);
for m in range(-(B-1), B):
Cm[m + (B-1)] = np.power(-1.0, m) * np.sqrt(2.0 * PI);
torch.save(Cm, fName);
print('Computed normCm_{}'.format(B), flush=True);
else:
Cm = torch.load(fName);
return Cm;
# Computes sparse matrix of Wigner-d function cosine + sine series coefficients
def wignerCoeffs(B):
fName = osp.join(cacheDir, 'wignerCoeffs_{}.pt'.format(B));
if (osp.isfile(fName) == False):
d = generateLittleHalf(B).cpu().numpy();
H = 0;
W = 0;
indH = [];
indW = [];
val = [];
N = 2*B;
for m in range(-(B-1), B):
for l in range(np.absolute(m), B):
for n in range(0, l+1):
iH = l + H;
iW = n + W;
# Cosine series
if ( (m % 2) == 0 ):
if (n == 0):
c = np.sqrt( (2*l + 1)/2.0 ) * np.sqrt( N );
else:
c = np.sqrt( (2*l + 1)/2.0 ) * np.sqrt( 2.0*N );
if ( (m % 4) == 2 ):
c *= -1.0;
coeff = c * d[l, n + (B-1), -m + (B-1)] * d[l, n+(B-1), B-1];
# Sine series
else:
if (n == l):
coeff = 0.0;
else:
c = np.sqrt( (2*l + 1) / 2.0 ) * np.sqrt( 2.0 * N);
if ( (m % 4) == 1 ):
c *= -1.0;
coeff = c * d[l, (n+1) + (B-1), -m + (B-1)] * d[l, (n+1) + (B-1), B-1];
if ( np.absolute(coeff) > 1.0e-15 ):
indH.append(iH);
indW.append(iW);
val.append(coeff);
H += B;
W += N;
# Cat indices, turn into sparse matrix
ind = torch.cat( (torch.tensor(indH).long()[None, :], torch.tensor(indW).long()[None, :]), dim=0);
val = torch.tensor( val, dtype=torch.double );
D = torch.sparse_coo_tensor(ind, val, [B*(2*B - 1), 2*B*(2*B - 1)])
torch.save(D, fName);
print('Computed wignerCoeffs_{}'.format(B), flush=True);
else:
D = torch.load(fName);
return D;
# Weighted DCT and DST implemented as linear layers
# Adapted from https://github.com/zh217/torch-dct/blob/master/torch_dct/_dct.py
class weightedDCST(nn.Linear):
'''DCT or DST as a linear layer'''
def __init__(self, B, xform):
self.xform = xform
self.B = B
super(weightedDCST, self).__init__(2*B, 2*B, bias=False)
def reset_parameters(self):
B = self.B;
if (self.xform == 'c'):
W = torch.diag(dltWeightsDH(B))
XF = torch.matmul(W, idctMatrix(2*B))
elif(self.xform == 'ic'):
XF = idctMatrix(2*B).t()
elif(self.xform == 's'):
W = torch.diag(dltWeightsDH(B))
XF = torch.matmul(W, idstMatrix(2*B));
elif(self.xform == 'is'):
XF = idstMatrix(2*B).t()
self.weight.data = XF.t().data;
self.weight.requires_grad = False # don't learn this!
# Forward Discrete Legendre Transform
class FDLT(nn.Module):
def __init__(self, B):
super(FDLT, self).__init__()
self.B = B;
self.dct = weightedDCST(B, 'c');
self.dst = weightedDCST(B, 's');
if ( ((B-1)%2) == 1 ):
cInd = torch.arange(1, 2*B-1, 2);
sInd = torch.arange(0, 2*B-1, 2);
else:
sInd = torch.arange(1, 2*B-1, 2);
cInd = torch.arange(0, 2*B-1, 2);
self.register_buffer('cInd', cInd);
self.register_buffer('sInd', sInd);
self.register_buffer('Cm', normCm(B));
self.register_buffer('D', wignerCoeffs(B));
def forward(self, psiHat):
# psiHat = b x M x phi
B, b = self.B, psiHat.size()[0]
# Multiply by normalization coefficients
psiHat = torch.mul(self.Cm[None, :, None], psiHat);
# Apply DCT + DST to even + odd indexed m
psiHat[:, self.cInd, :] = self.dct(psiHat[:, self.cInd, :]);
psiHat[:, self.sInd, :] = self.dst(psiHat[:, self.sInd, :]);
# Reshape for sparse matrix multiplication
psiHat = torch.transpose(torch.reshape(psiHat, (b, 2*B*(2*B - 1) ) ), 0, 1);
# Psi = b x M x L
return torch.permute(torch.reshape(torch.mm(self.D, psiHat), (2*B - 1, B, b)), (2, 0, 1));
# Inverse Discrete Legendre Transform
class IDLT(nn.Module):
def __init__(self, B):
super(IDLT, self).__init__()
self.B = B;
self.dct = weightedDCST(B, 'ic');
self.dst = weightedDCST(B, 'is');
if ( ((B-1)%2) == 1 ):
cInd = torch.arange(1, 2*B-1, 2);
sInd = torch.arange(0, 2*B-1, 2);
else:
sInd = torch.arange(1, 2*B-1, 2);
cInd = torch.arange(0, 2*B-1, 2);
self.register_buffer('cInd', cInd);
self.register_buffer('sInd', sInd);
self.register_buffer('iCm', torch.reciprocal(normCm(B)));
self.register_buffer('DT', torch.transpose(wignerCoeffs(B), 0, 1));
def forward(self, Psi):
# Psi: b x M x L
B, b = self.B, Psi.size()[0]
psiHat = torch.reshape(torch.transpose(torch.mm(self.DT, torch.transpose(torch.reshape(Psi, (b, (2*B - 1)*B)), 0, 1)), 0, 1), (b, 2*B - 1, 2*B))
#Apply DCT + DST to even + odd indexed m
psiHat[:, self.cInd, :] = self.dct(psiHat[:, self.cInd, :]);
psiHat[:, self.sInd, :] = self.dst(psiHat[:, self.sInd, :]);
# f: b x theta x phi
return torch.mul(self.iCm[None, :, None], psiHat);
#############################################################
################ Spherical Harmonic Transforms ##############
#############################################################
class FTSHT(nn.Module):
'''
The Forward "Tensorized" Discrete Spherical Harmonic Transform
Input:
B: (int) Transform bandlimit
'''
def __init__(self, B):
super(FTSHT, self).__init__()
self.B = B;
self.FDL = FDLT(B);
def forward(self, psi):
'''
Input:
psi: ( b x 2B x 2B torch.double or torch.cdouble tensor )
Real or complex spherical signal sampled on the 2B X 2B DH grid with b batch dimensions
Output:
Psi: (b x (2B - 1) x B torch.cdouble tensor)
Complex tensor of SH coefficients over b batch dimensions
'''
# psi: b x theta x phi (real or complex)
B, b = self.B, psi.size()[0]
## FFT in polar component
# psiHat: b x M x Phi
psiHat = torch.fft.fftshift(torch.fft.fft( psi, dim=1, norm='forward'), dim=1)[:, 1:, :]
## Convert to real representation
psiHat = torch.reshape(torch.permute(torch.view_as_real(psiHat), (0, 3, 1, 2)), (2*b, 2*B - 1, 2*B));
# Forward DLT
Psi = self.FDL(psiHat);
# Convert back to complex and return
# Psi: b x M x L (complex)
return torch.view_as_complex(torch.permute(torch.reshape(Psi, (b, 2, 2*B-1, B)), (0, 2, 3, 1)));
class ITSHT(nn.Module):
'''
The Inverse "Tensorized" Discrete Spherical Harmonic Transform
Input:
B: (int) Transform bandlimit
'''
def __init__(self, B):
super(ITSHT, self).__init__()
self.B = B;
self.IDL = IDLT(B);
def forward(self, Psi):
'''
Input:
Psi: (b x (2B - 1) x B torch.cdouble tensor)
Complex tensor of SH coefficients over b batch dimensions
Output:
psi: ( b x 2B x 2B torch.cdouble tensor )
Complex spherical signal sampled on the 2B X 2B DH grid with b batch dimensions
'''
# Psi: b x M x L (complex)
B, b = self.B, Psi.size()[0];
# Convert to real
Psi = torch.reshape(torch.permute(torch.view_as_real(Psi), (0, 3, 1, 2)), (2*b, 2*B-1, B));
# Inverse DLT
psiHat = self.IDL(Psi);
# Convert back to complex
psiHat = torch.view_as_complex(torch.permute(torch.reshape(psiHat, (b, 2, 2*B - 1, 2*B)), (0, 2, 3, 1)));
## Set up for iFFT
psiHat = torch.cat( (torch.empty(b, 1, 2*B, device=psiHat.device).float().fill_(0), psiHat), dim=1);
# Inverse FFT and return
# psi: b x theta x phi (complex)
return torch.fft.ifft( torch.fft.ifftshift(psiHat, dim=1), dim=1, norm='forward');