import numpy as np
from kwave.reconstruction.tools import log_compression


def test_log_compression():
    signal = np.array(
        [
            0.0,
            0.00099663,
            0.00646706,
            0.01316044,
            0.01851998,
            0.02355139,
            0.02753738,
            0.02966112,
            0.02907933,
            0.02502059,
            0.01690274,
            0.00445909,
            -0.01214073,
            -0.03219275,
            -0.05440197,
            -0.0768851,
            -0.09724674,
            -0.1127371,
            -0.12049029,
            -0.11783127,
            -0.10262828,
            -0.07365651,
            -0.03093011,
            0.02404454,
            0.0881411,
            0.15665039,
            0.22350821,
            0.28172052,
            0.3239688,
            0.34335346,
            0.33420925,
            0.29290732,
            0.21854807,
            0.11344786,
            -0.01666495,
            -0.16280571,
            -0.31326688,
            -0.45451349,
            -0.5723795,
            -0.65346175,
            -0.68657424,
            -0.66410437,
            -0.58311018,
            -0.4460133,
            -0.2607779,
            -0.04051683,
            0.19747155,
            0.43316678,
            0.64565891,
            0.81517002,
            0.92505348,
            0.96355291,
            0.92512102,
            0.81114165,
            0.62996031,
            0.39620447,
            0.12945356,
            -0.14760764,
            -0.41135944,
            -0.63951227,
            -0.81322602,
            -0.91886114,
            -0.94918152,
            -0.90389224,
            -0.78947066,
            -0.6183277,
            -0.40740841,
            -0.1763986,
            0.05425963,
            0.26532811,
            0.44050654,
            0.56777338,
            0.64021864,
            0.65631606,
            0.61964583,
            0.53813765,
            0.42295011,
            0.28713217,
            0.14422376,
            0.00694453,
            -0.11390314,
            -0.21022661,
            -0.27708217,
            -0.3127555,
            -0.31851672,
            -0.29811426,
            -0.257092,
            -0.20202238,
            -0.139745,
            -0.07668802,
            -0.01832983,
            0.03116454,
            0.06912143,
            0.0943617,
            0.10705662,
            0.10847543,
            0.10066873,
            0.08613128,
            0.06748245,
            0.04719401,
            0.02738443,
            0.00968836,
            -0.00479991,
            -0.01551776,
            -0.02237439,
            -0.02565662,
            -0.0259173,
            -0.02386159,
            -0.02024351,
            -0.01578123,
            -0.00892002,
            -0.00185916,
            -0.0,
        ]
    )
    expected_compressed_signal = np.array(
        [
            0.00000000e00,
            1.35591626e-03,
            8.77922211e-03,
            1.78182273e-02,
            2.50214896e-02,
            3.17561021e-02,
            3.70725573e-02,
            3.98983923e-02,
            3.91247308e-02,
            3.37176404e-02,
            2.28511210e-02,
            6.05819504e-03,
            -1.66047676e-02,
            -4.43896464e-02,
            -7.57027176e-02,
            -1.08000128e-01,
            -1.37788649e-01,
            -1.60805184e-01,
            -1.72443344e-01,
            -1.68442958e-01,
            -1.45749626e-01,
            -1.03324319e-01,
            -4.26266355e-02,
            3.24147595e-02,
            1.15922773e-01,
            2.00868311e-01,
            2.79867775e-01,
            3.45785453e-01,
            3.92075543e-01,
            4.12900386e-01,
            4.03108556e-01,
            3.58165590e-01,
            2.74131186e-01,
            1.47797048e-01,
            -2.28341902e-02,
            -2.37404533e-01,
            -4.90825038e-01,
            -7.68892818e-01,
            -1.04172392e00,
            -1.25854415e00,
            -1.35570148e00,
            -1.28917039e00,
            -1.06888097e00,
            -7.50809989e-01,
            -3.98069509e-01,
            -5.60584616e-02,
            2.49537348e-01,
            5.06192935e-01,
            7.08459315e-01,
            8.54168757e-01,
            9.42365207e-01,
            9.72215529e-01,
            9.42418038e-01,
            8.50846927e-01,
            6.94311624e-01,
            4.68416152e-01,
            1.67652485e-01,
            -2.13786510e-01,
            -6.79016139e-01,
            -1.21921894e00,
            -1.78888538e00,
            -2.25993732e00,
            -2.42331423e00,
            -2.18471894e00,
            -1.69878347e00,
            -1.16118333e00,
            -6.71019758e-01,
            -2.58809478e-01,
            7.22915321e-02,
            3.27480100e-01,
            5.13595628e-01,
            6.37082818e-01,
            7.03569815e-01,
            7.17997001e-01,
            6.84951683e-01,
            6.09117443e-01,
            4.95834739e-01,
            3.51785630e-01,
            1.85771929e-01,
            9.42560943e-03,
            -1.62550429e-01,
            -3.13278219e-01,
            -4.26343806e-01,
            -4.89896528e-01,
            -5.00386632e-01,
            -4.63525010e-01,
            -3.91742171e-01,
            -2.99906977e-01,
            -2.01695338e-01,
            -1.07714330e-01,
            -2.51323109e-02,
            4.18960027e-02,
            9.15678839e-02,
            1.23813253e-01,
            1.39803369e-01,
            1.41581150e-01,
            1.31776163e-01,
            1.13365585e-01,
            8.94527423e-02,
            6.30499609e-02,
            3.68688594e-02,
            1.31353908e-02,
            -6.54543545e-03,
            -2.12524482e-02,
            -3.07282290e-02,
            -3.52829902e-02,
            -3.56452574e-02,
            -3.27905068e-02,
            -2.77777264e-02,
            -2.16155965e-02,
            -1.21840122e-02,
            -2.53226773e-03,
            0.00000000e00,
        ]
    )

    compressed_signal = log_compression(signal, 0.8)
    assert (abs(expected_compressed_signal - compressed_signal) < 1e-6).any(), "log_compression not passing"
