File: _dwt.pyx

package info (click to toggle)
pywavelets 1.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,680 kB
  • sloc: python: 8,849; ansic: 5,134; makefile: 93
file content (510 lines) | stat: -rw-r--r-- 22,991 bytes parent folder | download
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
#cython: boundscheck=False, wraparound=False
from . cimport common
from . cimport c_wt
from .common cimport pywt_index_t, MODE
from ._pywt cimport _check_dtype

cimport numpy as np
import numpy as np

include "config.pxi"

np.import_array()

cpdef dwt_max_level(size_t data_len, size_t filter_len):
    return common.dwt_max_level(data_len, filter_len)


cpdef dwt_coeff_len(size_t data_len, size_t filter_len, MODE mode):
    if data_len < 1:
        raise ValueError("Value of data_len must be greater than zero.")
    if filter_len < 1:
        raise ValueError("Value of filter_len must be greater than zero.")

    return common.dwt_buffer_length(data_len, filter_len, mode)

cpdef dwt_single(cdata_t[::1] data, Wavelet wavelet, MODE mode):
    cdef size_t output_len = dwt_coeff_len(data.size, wavelet.dec_len, mode)
    cdef np.ndarray cA, cD
    cdef int retval_a, retval_d
    cdef size_t data_size = data.size
    if output_len < 1:
        raise RuntimeError("Invalid output length.")

    if data_size == 1 and (mode == MODE.MODE_REFLECT or mode == MODE.MODE_ANTIREFLECT):
        raise ValueError("Input data length must be greater than 1 for [anti]reflect mode.")

    if cdata_t is np.float64_t:
        # TODO: Don't think these have to be 0-initialized
        # TODO: Check other methods of allocating (e.g. Cython/CPython arrays)
        cA = np.zeros(output_len, np.float64)
        cD = np.zeros(output_len, np.float64)
        with nogil:
            retval_a = c_wt.double_dec_a(&data[0], data_size, wavelet.w,
                                <double *>cA.data, output_len, mode)
            retval_d = c_wt.double_dec_d(&data[0], data_size, wavelet.w,
                            <double *>cD.data, output_len, mode)
        if ( retval_a < 0 or retval_d < 0):
            raise RuntimeError("C dwt failed.")
    elif cdata_t is np.float32_t:
        cA = np.zeros(output_len, np.float32)
        cD = np.zeros(output_len, np.float32)
        with nogil:
            retval_a = c_wt.float_dec_a(&data[0], data_size, wavelet.w,
                               <float *>cA.data, output_len, mode)
            retval_d = c_wt.float_dec_d(&data[0], data_size, wavelet.w,
                           <float *>cD.data, output_len, mode)
        if ( retval_a < 0 or retval_d < 0):
            raise RuntimeError("C dwt failed.")

    IF HAVE_C99_CPLX:
        if cdata_t is np.complex128_t:
            cA = np.zeros(output_len, np.complex128)
            cD = np.zeros(output_len, np.complex128)
            with nogil:
                retval_a = c_wt.double_complex_dec_a(&data[0], data_size, wavelet.w,
                                    <double complex *>cA.data, output_len, mode)
                retval_d = c_wt.double_complex_dec_d(&data[0], data_size, wavelet.w,
                                <double complex *>cD.data, output_len, mode)
            if ( retval_a < 0 or retval_d < 0):
                raise RuntimeError("C dwt failed.")
        elif cdata_t is np.complex64_t:
            cA = np.zeros(output_len, np.complex64)
            cD = np.zeros(output_len, np.complex64)
            with nogil:
                retval_a = c_wt.float_complex_dec_a(&data[0], data_size, wavelet.w,
                                   <float complex *>cA.data, output_len, mode)
                retval_d = c_wt.float_complex_dec_d(&data[0], data_size, wavelet.w,
                               <float complex *>cD.data, output_len, mode)
            if ( retval_a < 0 or retval_d < 0):
                raise RuntimeError("C dwt failed.")

    return (cA, cD)


cpdef dwt_axis(np.ndarray data, Wavelet wavelet, MODE mode, unsigned int axis=0):
    # memory-views do not support n-dimensional arrays, use np.ndarray instead
    cdef common.ArrayInfo data_info, output_info
    cdef np.ndarray cD, cA
    # Explicit input_shape necessary to prevent memory leak
    cdef size_t[::1] input_shape, output_shape
    cdef int retval = -5


    if data.shape[axis] == 1 and (mode == MODE.MODE_REFLECT or mode == MODE.MODE_ANTIREFLECT):
        raise ValueError("Input data length must be greater than 1 for [anti]reflect mode along the transformed axis.")

    data = data.astype(_check_dtype(data), copy=False)

    input_shape = <size_t [:data.ndim]> <size_t *> data.shape
    output_shape = input_shape.copy()
    output_shape[axis] = dwt_coeff_len(data.shape[axis], wavelet.dec_len, mode)

    cA = np.empty(output_shape, data.dtype)
    cD = np.empty(output_shape, data.dtype)

    data_info.ndim = data.ndim
    data_info.strides = <pywt_index_t *> data.strides
    data_info.shape = <size_t *> data.shape

    output_info.ndim = cA.ndim
    output_info.strides = <pywt_index_t *> cA.strides
    output_info.shape = <size_t *> cA.shape

    if data.dtype == np.float64:
        with nogil:
            retval = c_wt.double_downcoef_axis(<double *> data.data, data_info,
                                         <double *> cA.data, output_info,
                                         wavelet.w, axis, common.COEF_APPROX, mode,
                                         0, common.DWT_TRANSFORM)
        if retval:
            raise RuntimeError("C wavelet transform failed")
        with nogil:
            retval = c_wt.double_downcoef_axis(<double *> data.data, data_info,
                                     <double *> cD.data, output_info,
                                     wavelet.w, axis, common.COEF_DETAIL, mode,
                                     0, common.DWT_TRANSFORM)
        if retval:
            raise RuntimeError("C wavelet transform failed")
    elif data.dtype == np.float32:
        with nogil:
            retval = c_wt.float_downcoef_axis(<float *> data.data, data_info,
                                    <float *> cA.data, output_info,
                                    wavelet.w, axis, common.COEF_APPROX, mode,
                                    0, common.DWT_TRANSFORM)
        if retval:
            raise RuntimeError("C wavelet transform failed")
        with nogil:
            retval = c_wt.float_downcoef_axis(<float *> data.data, data_info,
                                    <float *> cD.data, output_info,
                                    wavelet.w, axis, common.COEF_DETAIL, mode,
                                    0, common.DWT_TRANSFORM)
        if retval:
            raise RuntimeError("C wavelet transform failed")
    IF HAVE_C99_CPLX:
        if data.dtype == np.complex64:
            with nogil:
                retval = c_wt.float_complex_downcoef_axis(<float complex *> data.data, data_info,
                                        <float complex *> cA.data, output_info,
                                        wavelet.w, axis, common.COEF_APPROX, mode,
                                        0, common.DWT_TRANSFORM)
            if retval:
                raise RuntimeError("C wavelet transform failed")
            with nogil:
                retval = c_wt.float_complex_downcoef_axis(<float complex *> data.data, data_info,
                                        <float complex *> cD.data, output_info,
                                        wavelet.w, axis, common.COEF_DETAIL, mode,
                                        0, common.DWT_TRANSFORM)
            if retval:
                raise RuntimeError("C wavelet transform failed")
        elif data.dtype == np.complex128:
            with nogil:
                retval = c_wt.double_complex_downcoef_axis(<double complex *> data.data, data_info,
                                             <double complex *> cA.data, output_info,
                                             wavelet.w, axis, common.COEF_APPROX, mode,
                                             0, common.DWT_TRANSFORM)
            if retval:
                raise RuntimeError("C wavelet transform failed")
            with nogil:
                retval = c_wt.double_complex_downcoef_axis(<double complex *> data.data, data_info,
                                         <double complex *> cD.data, output_info,
                                         wavelet.w, axis, common.COEF_DETAIL, mode,
                                         0, common.DWT_TRANSFORM)
            if retval:
                raise RuntimeError("C wavelet transform failed")

    if retval == -5:
        raise TypeError("Array must be floating point, not {}"
                        .format(data.dtype))
    return (cA, cD)


cpdef idwt_single(np.ndarray cA, np.ndarray cD, Wavelet wavelet, MODE mode):
    cdef size_t input_len, rec_len
    cdef int retval
    cdef np.ndarray rec

    # check for size difference between arrays
    if cA.size != cD.size:
        raise ValueError("Coefficients arrays must have the same size.")
    else:
        input_len = cA.size

    if cA.dtype != cD.dtype:
        raise ValueError("Coefficients arrays must have the same dtype.")

    # find reconstruction buffer length
    rec_len = common.idwt_buffer_length(input_len, wavelet.rec_len, mode)
    if rec_len < 1:
        msg = ("Invalid coefficient arrays length for specified wavelet. "
               "Wavelet and mode must be the same as used for decomposition.")
        raise ValueError(msg)

        # call idwt func.  one of cA/cD can be None, then only
    # reconstruction of non-null part will be performed
    if cA.dtype == np.float64:
        rec = np.zeros(rec_len, dtype=np.float64)
        with nogil:
            retval = c_wt.double_idwt(<double *>cA.data, input_len,
                            <double *>cD.data, input_len,
                            <double *>rec.data, rec_len,
                            wavelet.w, mode)
        if retval < 0:
            raise RuntimeError("C idwt failed.")
    elif cA.dtype == np.float32:
        rec = np.zeros(rec_len, dtype=np.float32)
        with nogil:
            retval = c_wt.float_idwt(<float *>cA.data, input_len,
                           <float *>cD.data, input_len,
                           <float *>rec.data, rec_len,
                           wavelet.w, mode)
        if retval < 0:
            raise RuntimeError("C idwt failed.")
    IF HAVE_C99_CPLX:
        if cA.dtype == np.complex128:
            rec = np.zeros(rec_len, dtype=np.complex128)
            with nogil:
                retval = c_wt.double_complex_idwt(<double complex *>cA.data, input_len,
                                <double complex *>cD.data, input_len,
                                <double complex *>rec.data, rec_len,
                                wavelet.w, mode)
            if retval < 0:
                raise RuntimeError("C idwt failed.")
        elif cA.dtype == np.complex64:
            rec = np.zeros(rec_len, dtype=np.complex64)
            with nogil:
                retval = c_wt.float_complex_idwt(<float complex *>cA.data, input_len,
                               <float complex *>cD.data, input_len,
                               <float complex *>rec.data, rec_len,
                               wavelet.w, mode)
            if retval < 0:
                raise RuntimeError("C idwt failed.")

    return rec


cpdef idwt_axis(np.ndarray coefs_a, np.ndarray coefs_d,
                Wavelet wavelet, MODE mode, unsigned int axis=0):
    cdef common.ArrayInfo a_info, d_info, output_info
    cdef common.ArrayInfo *a_info_p = NULL
    cdef common.ArrayInfo *d_info_p = NULL
    cdef np.ndarray output
    cdef np.dtype output_dtype
    cdef void *data_a = NULL
    cdef void *data_d = NULL
    # Explicit input_shape necessary to prevent memory leak
    cdef size_t[::1] input_shape, output_shape
    cdef int retval = -5

    if coefs_a is not None:
        if coefs_d is not None and coefs_d.dtype.itemsize > coefs_a.dtype.itemsize:
            coefs_a = coefs_a.astype(_check_dtype(coefs_d), copy=False)
        else:
            coefs_a = coefs_a.astype(_check_dtype(coefs_a), copy=False)
        a_info.ndim = coefs_a.ndim
        a_info.strides = <pywt_index_t *> coefs_a.strides
        a_info.shape = <size_t *> coefs_a.shape
        a_info_p = &a_info
        data_a = <void *> coefs_a.data
    if coefs_d is not None:
        if coefs_a is not None and coefs_a.dtype.itemsize > coefs_d.dtype.itemsize:
            coefs_d = coefs_d.astype(_check_dtype(coefs_a), copy=False)
        else:
            coefs_d = coefs_d.astype(_check_dtype(coefs_d), copy=False)
        d_info.ndim = coefs_d.ndim
        d_info.strides = <pywt_index_t *> coefs_d.strides
        d_info.shape = <size_t *> coefs_d.shape
        d_info_p = &d_info
        data_d = <void *> coefs_d.data

    if coefs_a is not None:
        input_shape = <size_t [:coefs_a.ndim]> <size_t *> coefs_a.shape
        output_dtype = coefs_a.dtype
    elif coefs_d is not None:
        input_shape = <size_t [:coefs_d.ndim]> <size_t *> coefs_d.shape
        output_dtype = coefs_d.dtype
    else:
        return None

    output_shape = input_shape.copy()
    output_shape[axis] = common.idwt_buffer_length(input_shape[axis],
                                                   wavelet.rec_len, mode)
    output = np.empty(output_shape, output_dtype)

    output_info.ndim = output.ndim
    output_info.strides = <pywt_index_t *> output.strides
    output_info.shape = <size_t *> output.shape

    if output.dtype == np.float64:
        with nogil:
            retval = c_wt.double_idwt_axis(<double *> data_a, a_info_p,
                                 <double *> data_d, d_info_p,
                                 <double *> output.data, output_info,
                                 wavelet.w, axis, mode)
        if retval:
            raise RuntimeError("C inverse wavelet transform failed")
    elif output.dtype == np.float32:
        with nogil:
            retval = c_wt.float_idwt_axis(<float *> data_a, a_info_p,
                                <float *> data_d, d_info_p,
                                <float *> output.data, output_info,
                                wavelet.w, axis, mode)
        if retval:
            raise RuntimeError("C inverse wavelet transform failed")
    IF HAVE_C99_CPLX:
        if output.dtype == np.complex128:
            with nogil:
                retval = c_wt.double_complex_idwt_axis(<double complex *> data_a, a_info_p,
                                     <double complex *> data_d, d_info_p,
                                     <double complex *> output.data, output_info,
                                     wavelet.w, axis, mode)
            if retval:
                raise RuntimeError("C inverse wavelet transform failed")
        elif output.dtype == np.complex64:
            with nogil:
                retval = c_wt.float_complex_idwt_axis(<float complex *> data_a, a_info_p,
                                    <float complex *> data_d, d_info_p,
                                    <float complex *> output.data, output_info,
                                    wavelet.w, axis, mode)
            if retval:
                raise RuntimeError("C inverse wavelet transform failed")

    if retval == -5:
        raise TypeError("Array must be floating point, not {}"
                        .format(output.dtype))

    return output


cpdef upcoef(bint do_rec_a, cdata_t[::1] coeffs, Wavelet wavelet, int level,
             size_t take):
    cdef cdata_t[::1] rec
    cdef int i, retval
    cdef size_t rec_len, left_bound, right_bound, coeffs_size

    rec_len = 0

    if level < 1:
        raise ValueError("Value of level must be greater than 0.")

    for i in range(level):
        coeffs_size = coeffs.size
        # output len
        rec_len = common.reconstruction_buffer_length(coeffs.size, wavelet.dec_len)
        if rec_len < 1:
            raise RuntimeError("Invalid output length.")

        # To mirror multi-level wavelet reconstruction behaviour, when detail
        # reconstruction is requested, the dec_d variant is only called at the
        # first level to generate the approximation coefficients at the second
        # level.  Subsequent levels apply the reconstruction filter.
        if cdata_t is np.float64_t:
            rec = np.zeros(rec_len, dtype=np.float64)
            if do_rec_a or i > 0:
                with nogil:
                    retval = c_wt.double_rec_a(&coeffs[0], coeffs_size, wavelet.w,
                                     &rec[0], rec_len)
                if retval < 0:
                    raise RuntimeError("C rec_a failed.")
            else:
                with nogil:
                    retval = c_wt.double_rec_d(&coeffs[0], coeffs_size, wavelet.w,
                                     &rec[0], rec_len)
                if retval < 0:
                    raise RuntimeError("C rec_d failed.")
        elif cdata_t is np.float32_t:
            rec = np.zeros(rec_len, dtype=np.float32)
            if do_rec_a or i > 0:
                with nogil:
                    retval = c_wt.float_rec_a(&coeffs[0], coeffs_size, wavelet.w,
                                    &rec[0], rec_len)
                if retval < 0:
                    raise RuntimeError("C rec_a failed.")
            else:
                with nogil:
                    retval = c_wt.float_rec_d(&coeffs[0], coeffs_size, wavelet.w,
                                    &rec[0], rec_len)
                if retval < 0:
                    raise RuntimeError("C rec_d failed.")
        IF HAVE_C99_CPLX:
            if cdata_t is np.complex128_t:
                rec = np.zeros(rec_len, dtype=np.complex128)
                if do_rec_a or i > 0:
                    with nogil:
                        retval = c_wt.double_complex_rec_a(&coeffs[0], coeffs_size, wavelet.w,
                                         &rec[0], rec_len)
                    if retval < 0:
                        raise RuntimeError("C rec_a failed.")
                else:
                    with nogil:
                        retval = c_wt.double_complex_rec_d(&coeffs[0], coeffs_size, wavelet.w,
                                         &rec[0], rec_len)
                    if retval < 0:
                        raise RuntimeError("C rec_d failed.")
            elif cdata_t is np.complex64_t:
                rec = np.zeros(rec_len, dtype=np.complex64)
                if do_rec_a or i > 0:
                    with nogil:
                        retval = c_wt.float_complex_rec_a(&coeffs[0], coeffs_size, wavelet.w,
                                        &rec[0], rec_len)
                    if retval < 0:
                        raise RuntimeError("C rec_a failed.")
                else:
                    with nogil:
                        retval = c_wt.float_complex_rec_d(&coeffs[0], coeffs_size, wavelet.w,
                                        &rec[0], rec_len)
                    if retval < 0:
                        raise RuntimeError("C rec_d failed.")
        # TODO: this algorithm needs some explaining
        coeffs = rec

    if take > 0 and take < rec_len:
        left_bound = right_bound = (rec_len-take) // 2
        if (rec_len-take) % 2:
            # right_bound must never be zero for indexing to work
            right_bound = right_bound + 1

        return rec[left_bound:-right_bound]

    return rec


cpdef downcoef(bint do_dec_a, cdata_t[::1] data, Wavelet wavelet, MODE mode, int level):
    cdef cdata_t[::1] coeffs
    cdef int i, retval
    cdef size_t output_len, data_size

    if level < 1:
        raise ValueError("Value of level must be greater than 0.")

    for i in range(level):
        data_size = data.size
        output_len = common.dwt_buffer_length(data.size, wavelet.dec_len, mode)
        if output_len < 1:
            raise RuntimeError("Invalid output length.")

        # To mirror multi-level wavelet decomposition behaviour, when detail
        # coefficients are requested, the dec_d variant is only called at the
        # final level.  All prior levels use dec_a.  In other words, the detail
        # coefficients at level n are those produced via the operation of the
        # detail filter on the approximation coefficients of level n-1.
        if cdata_t is np.float64_t:
            coeffs = np.zeros(output_len, dtype=np.float64)
            if do_dec_a or (i < level - 1):
                with nogil:
                    retval = c_wt.double_dec_a(&data[0], data_size, wavelet.w,
                                     &coeffs[0], output_len, mode)
                if retval < 0:
                    raise RuntimeError("C dec_a failed.")
            else:
                with nogil:
                    retval = c_wt.double_dec_d(&data[0], data_size, wavelet.w,
                                     &coeffs[0], output_len, mode)
                if retval < 0:
                    raise RuntimeError("C dec_d failed.")
        elif cdata_t is np.float32_t:
            coeffs = np.zeros(output_len, dtype=np.float32)
            if do_dec_a or (i < level - 1):
                with nogil:
                    retval = c_wt.float_dec_a(&data[0], data_size, wavelet.w,
                                    &coeffs[0], output_len, mode)
                if retval < 0:
                    raise RuntimeError("C dec_a failed.")
            else:
                with nogil:
                    retval = c_wt.float_dec_d(&data[0], data_size, wavelet.w,
                                    &coeffs[0], output_len, mode)
                if retval < 0:
                    raise RuntimeError("C dec_d failed.")
        IF HAVE_C99_CPLX:
            if cdata_t is np.complex128_t:
                coeffs = np.zeros(output_len, dtype=np.complex128)
                if do_dec_a or (i < level - 1):
                    with nogil:
                        retval = c_wt.double_complex_dec_a(&data[0], data_size, wavelet.w,
                                         &coeffs[0], output_len, mode)
                    if retval < 0:
                        raise RuntimeError("C dec_a failed.")
                else:
                    with nogil:
                        retval = c_wt.double_complex_dec_d(&data[0], data_size, wavelet.w,
                                         &coeffs[0], output_len, mode)
                    if retval < 0:
                        raise RuntimeError("C dec_d failed.")
            elif cdata_t is np.complex64_t:
                coeffs = np.zeros(output_len, dtype=np.complex64)
                if do_dec_a or (i < level - 1):
                    with nogil:
                        retval = c_wt.float_complex_dec_a(&data[0], data_size, wavelet.w,
                                        &coeffs[0], output_len, mode)
                    if retval < 0:
                        raise RuntimeError("C dec_a failed.")
                else:
                    with nogil:
                        retval = c_wt.float_complex_dec_d(&data[0], data_size, wavelet.w,
                                        &coeffs[0], output_len, mode)
                    if retval < 0:
                        raise RuntimeError("C dec_d failed.")
        data = coeffs

    return coeffs