File size: 10,629 Bytes
2571f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Compute spline interpolating coefficients

These functions are ported from the C routines in SPM's bsplines.c
by John Ashburner, which are themselves ports from Philippe Thevenaz's
code. JA furthermore derived the initial conditions for the DFT ("wrap around")
boundary conditions.

Note that similar routines are available in scipy with boundary conditions
DCT1 ("mirror"), DCT2 ("reflect") and DFT ("wrap"); all derived by P. Thevenaz,
according to the comments. Our DCT2 boundary conditions are ported from
scipy.

Only boundary conditions DCT1, DCT2 and DFT are implemented.

References
----------
..[1]  M. Unser, A. Aldroubi and M. Eden.
       "B-Spline Signal Processing: Part I-Theory,"
       IEEE Transactions on Signal Processing 41(2):821-832 (1993).
..[2]  M. Unser, A. Aldroubi and M. Eden.
       "B-Spline Signal Processing: Part II-Efficient Design and Applications,"
       IEEE Transactions on Signal Processing 41(2):834-848 (1993).
..[3]  M. Unser.
       "Splines: A Perfect Fit for Signal and Image Processing,"
       IEEE Signal Processing Magazine 16(6):22-38 (1999).
"""
import torch
import math
from typing import List, Optional
from .jit_utils import movedim1
from .pushpull import pad_list_int


@torch.jit.script
def get_poles(order: int) -> List[float]:
    empty: List[float] = []
    if order in (0, 1):
        return empty
    if order == 2:
        return [math.sqrt(8.) - 3.]
    if order == 3:
        return [math.sqrt(3.) - 2.]
    if order == 4:
        return [math.sqrt(664. - math.sqrt(438976.)) + math.sqrt(304.) - 19.,
                math.sqrt(664. + math.sqrt(438976.)) - math.sqrt(304.) - 19.]
    if order == 5:
        return [math.sqrt(67.5 - math.sqrt(4436.25)) + math.sqrt(26.25) - 6.5,
                math.sqrt(67.5 + math.sqrt(4436.25)) - math.sqrt(26.25) - 6.5]
    if order == 6:
        return [-0.488294589303044755130118038883789062112279161239377608394,
                -0.081679271076237512597937765737059080653379610398148178525368,
                -0.00141415180832581775108724397655859252786416905534669851652709]
    if order == 7:
        return [-0.5352804307964381655424037816816460718339231523426924148812,
                -0.122554615192326690515272264359357343605486549427295558490763,
                -0.0091486948096082769285930216516478534156925639545994482648003]
    raise NotImplementedError


@torch.jit.script
def get_gain(poles: List[float]) -> float:
    lam: float = 1.
    for pole in poles:
        lam *= (1. - pole) * (1. - 1./pole)
    return lam


@torch.jit.script
def dft_initial(inp, pole: float, dim: int = -1, keepdim: bool = False):

    assert inp.shape[dim] > 1
    max_iter: int = int(math.ceil(-30./math.log(abs(pole))))
    max_iter = min(max_iter, inp.shape[dim])

    poles = torch.as_tensor(pole, dtype=inp.dtype, device=inp.device)
    poles = poles.pow(torch.arange(1, max_iter, dtype=inp.dtype, device=inp.device))
    poles = poles.flip(0)

    inp = movedim1(inp, dim, 0)
    inp0 = inp[0]
    inp = inp[1-max_iter:]
    inp = movedim1(inp, 0, -1)
    out = torch.matmul(inp.unsqueeze(-2), poles.unsqueeze(-1)).squeeze(-1)
    out = out + inp0.unsqueeze(-1)
    if keepdim:
        out = movedim1(out, -1, dim)
    else:
        out = out.squeeze(-1)

    pole = pole ** max_iter
    out = out / (1 - pole)
    return out


@torch.jit.script
def dct1_initial(inp, pole: float, dim: int = -1, keepdim: bool = False):

    n = inp.shape[dim]
    max_iter: int = int(math.ceil(-30./math.log(abs(pole))))

    if max_iter < n:

        poles = torch.as_tensor(pole, dtype=inp.dtype, device=inp.device)
        poles = poles.pow(torch.arange(1, max_iter, dtype=inp.dtype, device=inp.device))

        inp = movedim1(inp, dim, 0)
        inp0 = inp[0]
        inp = inp[1:max_iter]
        inp = movedim1(inp, 0, -1)
        out = torch.matmul(inp.unsqueeze(-2), poles.unsqueeze(-1)).squeeze(-1)
        out = out + inp0.unsqueeze(-1)
        if keepdim:
            out = movedim1(out, -1, dim)
        else:
            out = out.squeeze(-1)

    else:
        max_iter = n

        polen = pole ** (n - 1)
        inp0 = inp[0] + polen * inp[-1]
        inp = inp[1:-1]
        inp = movedim1(inp, 0, -1)

        poles = torch.as_tensor(pole, dtype=inp.dtype, device=inp.device)
        poles = poles.pow(torch.arange(1, n-1, dtype=inp.dtype, device=inp.device))
        poles = poles + (polen * polen) / poles

        out = torch.matmul(inp.unsqueeze(-2), poles.unsqueeze(-1)).squeeze(-1)
        out = out + inp0.unsqueeze(-1)
        if keepdim:
            out = movedim1(out, -1, dim)
        else:
            out = out.squeeze(-1)

        pole = pole ** (max_iter - 1)
        out = out / (1 - pole * pole)

    return out


@torch.jit.script
def dct2_initial(inp, pole: float, dim: int = -1, keepdim: bool = False):
    # Ported from scipy:
    # https://github.com/scipy/scipy/blob/master/scipy/ndimage/src/ni_splines.c
    #
    # I (YB) unwarped and simplied the terms so that I could use a dot
    # product instead of a loop.
    # It should certainly be possible to derive a version for max_iter < n,
    # as JA did for DCT1, to avoid long recursions when `n` is large. But
    # I think it would require a more complicated anticausal/final condition.

    n = inp.shape[dim]

    polen = pole ** n
    pole_last = polen * (1 + 1/(pole + polen * polen))
    inp00 = inp[0]
    inp0 = inp[0] + pole_last * inp[-1]
    inp = inp[1:-1]
    inp = movedim1(inp, 0, -1)

    poles = torch.as_tensor(pole, dtype=inp.dtype, device=inp.device)
    poles = (poles.pow(torch.arange(1, n-1, dtype=inp.dtype, device=inp.device)) +
             poles.pow(torch.arange(2*n-2, n, -1, dtype=inp.dtype, device=inp.device)))

    out = torch.matmul(inp.unsqueeze(-2), poles.unsqueeze(-1)).squeeze(-1)

    out = out + inp0.unsqueeze(-1)
    out = out * (pole / (1 - polen * polen))
    out = out + inp00.unsqueeze(-1)

    if keepdim:
        out = movedim1(out, -1, dim)
    else:
        out = out.squeeze(-1)

    return out


@torch.jit.script
def dft_final(inp, pole: float, dim: int = -1, keepdim: bool = False):

    assert inp.shape[dim] > 1
    max_iter: int = int(math.ceil(-30./math.log(abs(pole))))
    max_iter = min(max_iter, inp.shape[dim])

    poles = torch.as_tensor(pole, dtype=inp.dtype, device=inp.device)
    poles = poles.pow(torch.arange(2, max_iter+1, dtype=inp.dtype, device=inp.device))

    inp = movedim1(inp, dim, 0)
    inp0 = inp[-1]
    inp = inp[:max_iter-1]
    inp = movedim1(inp, 0, -1)
    out = torch.matmul(inp.unsqueeze(-2), poles.unsqueeze(-1)).squeeze(-1)
    out = out.add(inp0.unsqueeze(-1), alpha=pole)
    if keepdim:
        out = movedim1(out, -1, dim)
    else:
        out = out.squeeze(-1)

    pole = pole ** max_iter
    out = out / (pole - 1)
    return out


@torch.jit.script
def dct1_final(inp, pole: float, dim: int = -1, keepdim: bool = False):
    inp = movedim1(inp, dim, 0)
    out = pole * inp[-2] + inp[-1]
    out = out * (pole / (pole*pole - 1))
    if keepdim:
        out = movedim1(out.unsqueeze(0), 0, dim)
    return out


@torch.jit.script
def dct2_final(inp, pole: float, dim: int = -1, keepdim: bool = False):
    # Ported from scipy:
    # https://github.com/scipy/scipy/blob/master/scipy/ndimage/src/ni_splines.c
    inp = movedim1(inp, dim, 0)
    out = inp[-1] * (pole / (pole - 1))
    if keepdim:
        out = movedim1(out.unsqueeze(0), 0, dim)
    return out


@torch.jit.script
class CoeffBound:

    def __init__(self, bound: int):
        self.bound = bound

    def initial(self, inp, pole: float, dim: int = -1, keepdim: bool = False):
        if self.bound in (0, 2):    # zero, dct1
            return dct1_initial(inp, pole, dim, keepdim)
        elif self.bound in (1, 3):  # nearest, dct2
            return dct2_initial(inp, pole, dim, keepdim)
        elif self.bound == 6:       # dft
            return dft_initial(inp, pole, dim, keepdim)
        else:
            raise NotImplementedError

    def final(self, inp, pole: float, dim: int = -1, keepdim: bool = False):
        if self.bound in (0, 2):    # zero, dct1
            return dct1_final(inp, pole, dim, keepdim)
        elif self.bound in (1, 3):  # nearest, dct2
            return dct2_final(inp, pole, dim, keepdim)
        elif self.bound == 6:       # dft
            return dft_final(inp, pole, dim, keepdim)
        else:
            raise NotImplementedError


@torch.jit.script
def filter(inp, bound: CoeffBound, poles: List[float],
           dim: int = -1, inplace: bool = False):

    if not inplace:
        inp = inp.clone()

    if inp.shape[dim] == 1:
        return inp

    gain = get_gain(poles)
    inp *= gain
    inp = movedim1(inp, dim, 0)
    n = inp.shape[0]

    for pole in poles:
        inp[0] = bound.initial(inp, pole, dim=0, keepdim=False)

        for i in range(1, n):
            inp[i].add_(inp[i-1], alpha=pole)

        inp[-1] = bound.final(inp, pole, dim=0, keepdim=False)

        for i in range(n-2, -1, -1):
            inp[i].neg_().add_(inp[i+1]).mul_(pole)

    inp = movedim1(inp, 0, dim)
    return inp


@torch.jit.script
def spline_coeff(inp, bound: int, order: int, dim: int = -1,
                 inplace: bool = False):
    """Compute the interpolating spline coefficients, for a given spline order
    and boundary conditions, along a single dimension.

    Parameters
    ----------
    inp : tensor
    bound : {2: dct1, 6: dft}
    order : {0..7}
    dim : int, default=-1
    inplace : bool, default=False

    Returns
    -------
    out : tensor

    """
    if not inplace:
        inp = inp.clone()

    if order in (0, 1):
        return inp

    poles = get_poles(order)
    return filter(inp, CoeffBound(bound), poles, dim=dim, inplace=True)


@torch.jit.script
def spline_coeff_nd(inp, bound: List[int], order: List[int],
                    dim: Optional[int] = None, inplace: bool = False):
    """Compute the interpolating spline coefficients, for a given spline order
    and boundary condition, along the last `dim` dimensions.

    Parameters
    ----------
    inp : (..., *spatial) tensor
    bound : List[{2: dct1, 6: dft}]
    order : List[{0..7}]
    dim : int, default=`inp.dim()`
    inplace : bool, default=False

    Returns
    -------
    out : (..., *spatial) tensor

    """
    if not inplace:
        inp = inp.clone()

    if dim is None:
        dim = inp.dim()

    bound = pad_list_int(bound, dim)
    order = pad_list_int(order, dim)

    for d, b, o in zip(range(dim), bound, order):
        inp = spline_coeff(inp, b, o, dim=-dim + d, inplace=True)

    return inp