Exemplo n.º 1
0
def test_conv_fwt():
    """Test multiple convolution fwt, for various levels and padding options."""
    generator = MackeyGenerator(batch_size=2,
                                tmax=128,
                                delta_t=1,
                                device="cpu")

    mackey_data_1 = torch.squeeze(generator())
    for level in [1, 2, 3, None]:
        for wavelet_string in ["db1", "db2", "db3", "db4", "db5"]:
            for mode in ["reflect", "zero"]:
                wavelet = pywt.Wavelet(wavelet_string)
                ptcoeff = wavedec(mackey_data_1,
                                  wavelet,
                                  level=level,
                                  mode=mode)
                pycoeff = pywt.wavedec(mackey_data_1[0, :].numpy(),
                                       wavelet,
                                       level=level,
                                       mode=mode)
                cptcoeff = torch.cat(ptcoeff, -1)[0, :]
                cpycoeff = np.concatenate(pycoeff, -1)
                err = np.mean(np.abs(cpycoeff - cptcoeff.numpy()))
                print(
                    "db5 coefficient error scale 3:",
                    err,
                    ["ok" if err < 1e-4 else "failed!"],
                    "mode",
                    mode,
                )
                assert np.allclose(cptcoeff.numpy(), cpycoeff, atol=1e-6)

                res = waverec(
                    wavedec(mackey_data_1, wavelet, level=3, mode=mode),
                    wavelet,
                )
                err = torch.mean(torch.abs(mackey_data_1 - res)).numpy()
                print(
                    "db5 reconstruction error scale 3:",
                    err,
                    ["ok" if err < 1e-4 else "failed!"],
                    "mode",
                    mode,
                )
                assert np.allclose(mackey_data_1.numpy(), res.numpy())

                res = waverec(
                    wavedec(mackey_data_1, wavelet, level=4, mode=mode),
                    wavelet,
                )
                err = torch.mean(torch.abs(mackey_data_1 - res)).numpy()
                print(
                    "db5 reconstruction error scale 4:",
                    err,
                    ["ok" if err < 1e-4 else "failed!"],
                    "mode",
                    mode,
                )
                assert np.allclose(mackey_data_1.numpy(), res.numpy())
Exemplo n.º 2
0
def test_conv_fwt_db5_lvl3():
    """Test a third level db5 conv-fwt."""
    generator = MackeyGenerator(batch_size=2,
                                tmax=128,
                                delta_t=1,
                                device="cpu")

    mackey_data_1 = torch.squeeze(generator())
    wavelet = pywt.Wavelet("db5")
    for mode in ["reflect", "zero"]:
        ptcoeff = wavedec(mackey_data_1, wavelet, level=3, mode=mode)
        pycoeff = pywt.wavedec(mackey_data_1[0, :].numpy(),
                               wavelet,
                               level=3,
                               mode=mode)
        cptcoeff = torch.cat(ptcoeff, -1)[0, :]
        cpycoeff = np.concatenate(pycoeff, -1)
        err = np.mean(np.abs(cpycoeff - cptcoeff.numpy()))
        print(
            "db5 coefficient error scale 3:",
            err,
            ["ok" if err < 1e-4 else "failed!"],
            "mode",
            mode,
        )
        assert np.allclose(cpycoeff, cptcoeff.numpy(), atol=1e-6)

        res = waverec(wavedec(mackey_data_1, wavelet, level=3, mode=mode),
                      wavelet)
        err = torch.mean(torch.abs(mackey_data_1 - res)).numpy()
        print(
            "db5 reconstruction error scale 3:",
            err,
            ["ok" if err < 1e-4 else "failed!"],
            "mode",
            mode,
        )
        assert np.allclose(mackey_data_1.numpy(), res.numpy())
        res = waverec(wavedec(mackey_data_1, wavelet, level=4, mode=mode),
                      wavelet)
        err = torch.mean(torch.abs(mackey_data_1 - res)).numpy()
        print(
            "db5 reconstruction error scale 4:",
            err,
            ["ok" if err < 1e-4 else "failed!"],
            "mode",
            mode,
        )
        assert np.allclose(mackey_data_1.numpy(), res.numpy())
Exemplo n.º 3
0
def test_conv_fwt_haar_lvl2_odd():
    """Test an odd Haar wavelet convolution fwt."""
    data = np.array([
        1.0,
        2.0,
        3.0,
        4.0,
        5.0,
        6.0,
        7.0,
        8.0,
        9.0,
        10.0,
        11.0,
        12.0,
        13.0,
        14.0,
        15.0,
        16.0,
        17.0,
    ])
    wavelet = pywt.Wavelet("haar")

    pycoeff = pywt.wavedec(data, wavelet, level=2, mode="reflect")
    pywt_coeffs = np.concatenate(pycoeff)
    ptcoeff = wavedec(torch.from_numpy(data), wavelet, level=2, mode="reflect")
    ptwt_coeffs = torch.cat(ptcoeff, -1)[0, :].numpy()
    assert np.allclose(pywt_coeffs, ptwt_coeffs)
    rec = waverec(ptcoeff, wavelet)
    assert np.allclose(data, rec[:, :-1].numpy())
Exemplo n.º 4
0
def test_conv_fwt_db2_lvl1():
    """Test a second level db2 conv-fwt."""
    data = np.array([
        1.0,
        2.0,
        3.0,
        4.0,
        5.0,
        6.0,
        7.0,
        8.0,
        9.0,
        10.0,
        11.0,
        12.0,
        13.0,
        14.0,
        15.0,
        16.0,
    ])
    # ------------------------- db2 wavelet tests ----------------------------
    wavelet = pywt.Wavelet("db2")
    coeffs = pywt.wavedec(data, wavelet, level=1, mode="reflect")
    coeffs2 = wavedec(torch.from_numpy(data), wavelet, level=1, mode="reflect")
    ccoeffs = np.concatenate(coeffs, -1)
    ccoeffs2 = torch.cat(coeffs2, -1).numpy()
    err = np.mean(np.abs(ccoeffs - ccoeffs2))
    print("db2 coefficient error scale 1:", err,
          ["ok" if err < 1e-4 else "failed!"])
    assert np.allclose(ccoeffs, ccoeffs2, atol=1e-6)
    rec = waverec(coeffs2, wavelet)
    err = np.mean(np.abs(data - rec.numpy()))
    print("db2 reconstruction error scale 1:", err,
          ["ok" if err < 1e-4 else "failed!"])
    assert np.allclose(data, rec.numpy())
 def wavelet_reconstruction(self, x):
     """Reconstruction from a tensor input.
     Args:
         x (torch.Tensor): Analysis coefficient tensor.
     Returns:
         torch.Tensor: Input reconstruction.
     """
     coeff_lst = []
     start = 0
     # turn tensor into list
     for s in range(self.scales + 1):
         stop = start + self.coefficient_len_lst[::-1][s]
         coeff_lst.append(x[..., start:stop])
         start = self.coefficient_len_lst[s]
     y = waverec(coeff_lst, self.wavelet)
     return y
Exemplo n.º 6
0
def test_orth_wavelet():
    """Test an orthogonal wavelet fwt."""
    generator = MackeyGenerator(batch_size=2, tmax=64, delta_t=1, device="cpu")

    mackey_data_1 = torch.squeeze(generator())
    # orthogonal wavelet object test
    wavelet = pywt.Wavelet("db5")
    orthwave = SoftOrthogonalWavelet(
        torch.tensor(wavelet.rec_lo),
        torch.tensor(wavelet.rec_hi),
        torch.tensor(wavelet.dec_lo),
        torch.tensor(wavelet.dec_hi),
    )
    res = waverec(wavedec(mackey_data_1, orthwave), orthwave)
    err = torch.mean(torch.abs(mackey_data_1 - res.detach())).numpy()
    print("orth reconstruction error scale 4:", err,
          ["ok" if err < 1e-4 else "failed!"])
    assert np.allclose(res.detach().numpy(), mackey_data_1.numpy())
Exemplo n.º 7
0
def test_conv_fwt_haar_lvl4():
    """Test a fourth level Haar wavelet conv-fwt."""
    generator = MackeyGenerator(batch_size=2, tmax=64, delta_t=1, device="cpu")
    mackey_data_1 = torch.squeeze(generator())
    wavelet = pywt.Wavelet("haar")
    ptcoeff = wavedec(mackey_data_1, wavelet, level=4)
    pycoeff = pywt.wavedec(mackey_data_1[0, :].numpy(), wavelet, level=4)
    ptwt_coeff = torch.cat(ptcoeff, -1)[0, :].numpy()
    pywt_coeff = np.concatenate(pycoeff)
    err = np.mean(np.abs(pywt_coeff - ptwt_coeff))
    print("haar coefficient error scale 4:", err,
          ["ok" if err < 1e-4 else "failed!"])
    assert np.allclose(pywt_coeff, ptwt_coeff, atol=1e-06)

    reconstruction = waverec(wavedec(mackey_data_1, wavelet), wavelet)
    err = torch.mean(torch.abs(mackey_data_1 - reconstruction)).numpy()
    print("haar reconstruction error scale 4:", err,
          ["ok" if err < 1e-4 else "failed!"])
    assert np.allclose(reconstruction.numpy(), mackey_data_1.numpy())
Exemplo n.º 8
0
def test_conv_fwt_haar_lvl2():
    """Test Haar wavelet level two conv fwt."""
    data = np.array([
        1.0,
        2.0,
        3.0,
        4.0,
        5.0,
        6.0,
        7.0,
        8.0,
        9.0,
        10.0,
        11.0,
        12.0,
        13.0,
        14.0,
        15.0,
        16.0,
    ])
    wavelet = pywt.Wavelet("haar")
    coeffs = pywt.wavedec(data, wavelet, level=2)
    coeffs2 = wavedec(torch.from_numpy(data), wavelet, level=2)
    assert len(coeffs) == len(coeffs2)

    pywt_coeffs = np.concatenate(coeffs)
    ptwt_coeffs = torch.cat(coeffs2, -1).squeeze().numpy()
    err = np.mean(np.abs(pywt_coeffs - ptwt_coeffs))
    print("haar coefficient error scale 2", err,
          ["ok" if err < 1e-6 else "failed!"])
    assert np.allclose(pywt_coeffs, ptwt_coeffs)
    rec = waverec(coeffs2, wavelet).squeeze().numpy()
    err = np.mean(np.abs((data - rec)))
    print("haar reconstruction error scale 2", err,
          ["ok" if err < 1e-6 else "failed!"])
    assert np.allclose(data, rec)