|
| 1 | +from math import prod |
1 | 2 | from typing import Final, Tuple |
2 | 3 |
|
3 | 4 | import numpy as np |
4 | 5 | import pytest |
5 | 6 | from scipy.interpolate import CubicSpline |
6 | 7 |
|
7 | 8 | from pylops.signalprocessing import InterpCubicSpline |
| 9 | +from pylops.utils import dottest |
8 | 10 |
|
9 | 11 | TEST_ARRAY_SHAPE: Final[Tuple] = ( |
10 | 12 | 20, |
|
16 | 18 | MIN_NUM_TEST_SAMPLES: Final[int] = 1 |
17 | 19 |
|
18 | 20 |
|
| 21 | +def test_cubic_spline_raises_on_not_supported_bc_type() -> None: |
| 22 | + """ |
| 23 | + Tests whether ``pylops.signalprocessing.InterpCubicSpline`` raises a |
| 24 | + ``NotImplementedError`` for boundary conditions that are not supported. |
| 25 | +
|
| 26 | + """ |
| 27 | + |
| 28 | + with pytest.raises(NotImplementedError): |
| 29 | + InterpCubicSpline( |
| 30 | + dims=(5, 2), |
| 31 | + iava=np.array([0.5, 2.3]), |
| 32 | + bc_type="erroneous", # type: ignore |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "with_complex", |
| 38 | + [ |
| 39 | + pytest.param(False, id="real"), |
| 40 | + pytest.param(True, id="complex"), |
| 41 | + ], |
| 42 | +) |
| 43 | +@pytest.mark.parametrize( |
| 44 | + "axis", |
| 45 | + [ |
| 46 | + 0, |
| 47 | + 1, |
| 48 | + 2, |
| 49 | + 3, |
| 50 | + -1, |
| 51 | + -2, |
| 52 | + -3, |
| 53 | + ], |
| 54 | +) |
| 55 | +@pytest.mark.parametrize( |
| 56 | + "subsample_fraction", |
| 57 | + [ |
| 58 | + pytest.param(0.5, id="decimation"), |
| 59 | + pytest.param(5.0, id="upsampling"), |
| 60 | + ], |
| 61 | +) |
| 62 | +def test_natural_cubic_spline_dottest( |
| 63 | + subsample_fraction: float, |
| 64 | + axis: int, |
| 65 | + with_complex: bool, |
| 66 | +) -> None: |
| 67 | + """ |
| 68 | + Tests ``pylops.signalprocessing.InterpCubicSpline`` with the ``dottest``. |
| 69 | +
|
| 70 | + """ |
| 71 | + |
| 72 | + # Setup |
| 73 | + |
| 74 | + num_samples = TEST_ARRAY_SHAPE[axis] |
| 75 | + x_eval_fractions = np.random.rand( |
| 76 | + max( |
| 77 | + round(num_samples * subsample_fraction), |
| 78 | + MIN_NUM_TEST_SAMPLES, |
| 79 | + ) |
| 80 | + ) |
| 81 | + x_eval_for_pylops = (num_samples - 1) * x_eval_fractions |
| 82 | + |
| 83 | + shape_list = list(TEST_ARRAY_SHAPE) |
| 84 | + shape_list[axis] = x_eval_fractions.size # type: ignore |
| 85 | + num_rows = prod(shape_list, start=1) |
| 86 | + num_columns = prod(TEST_ARRAY_SHAPE, start=1) |
| 87 | + |
| 88 | + # Test |
| 89 | + |
| 90 | + splinop = InterpCubicSpline( |
| 91 | + dims=TEST_ARRAY_SHAPE, |
| 92 | + iava=x_eval_for_pylops, |
| 93 | + axis=axis, |
| 94 | + dtype="complex128" if with_complex else "float64", |
| 95 | + ) |
| 96 | + |
| 97 | + assert dottest( |
| 98 | + Op=splinop, |
| 99 | + nr=num_rows, |
| 100 | + nc=num_columns, |
| 101 | + complexflag=0 if not with_complex else 3, |
| 102 | + ) |
| 103 | + |
| 104 | + |
19 | 105 | @pytest.mark.parametrize( |
20 | 106 | "with_complex", |
21 | 107 | [ |
|
0 commit comments