def test_approx_correlation(testfiles):
    name = testfiles["dataset1_normalized.h5"]
    name2 = testfiles["database1.h5"]

    c = FourierApproximation(name)

    a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
    b = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10])
    m = len(a)
    a_fft = np.fft.fft(normalize(a)) / m
    b_fft = np.fft.fft(normalize(b)) / m
    assert abs(sum(abs(a_fft)**2) - 1) < 0.000000001
    assert abs(sum(abs(b_fft)**2) - 1) < 0.000000001

    approx_corr = 1 - (np.linalg.norm(a_fft - b_fft)**2) / 2
    assert abs(corr(a, b) - approx_corr) < 0.0000000001

    orig_ds = DatasetH5(name2)
    t1 = 0
    t2 = 3
    t1_orig = orig_ds[t1][:]
    t2_orig = orig_ds[t2][:]
    m = len(t1_orig)
    t1_fft = np.fft.fft(normalize(t1_orig)) / m
    t2_fft = np.fft.fft(normalize(t2_orig)) / m

    approx_corr = 1 - (np.linalg.norm(t1_fft - t2_fft)**2) / 2
    assert abs(corr(t1_orig, t2_orig) - approx_corr) < 0.00000001
def test_approx_correlation(testfiles):
    name = testfiles["dataset1_normalized.h5"]
    name2 = testfiles["database1.h5"]

    c = FourierApproximation(name)

    a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
    b = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10])
    m = len(a)
    a_fft = np.fft.fft(normalize(a)) / m
    b_fft = np.fft.fft(normalize(b)) / m
    assert abs(sum(abs(a_fft) ** 2) - 1) < 0.000000001
    assert abs(sum(abs(b_fft) ** 2) - 1) < 0.000000001

    approx_corr = 1 - (np.linalg.norm(a_fft - b_fft) ** 2)/2
    assert abs(corr(a, b) - approx_corr) < 0.0000000001

    orig_ds = DatasetH5(name2)
    t1 = 0
    t2 = 3
    t1_orig = orig_ds[t1][:]
    t2_orig = orig_ds[t2][:]
    m = len(t1_orig)
    t1_fft = np.fft.fft(normalize(t1_orig)) / m
    t2_fft = np.fft.fft(normalize(t2_orig)) / m

    approx_corr = 1 - (np.linalg.norm(t1_fft - t2_fft) ** 2)/2
    assert abs(corr(t1_orig, t2_orig) - approx_corr) < 0.00000001
def test_approx_correlation_error(testfiles):
    name = testfiles["dataset1_normalized.h5"]
    name2 = testfiles["database1.h5"]

    c = FourierApproximation(name)
    orig_ds = DatasetH5(name2)

    e = 0.04
    for t1 in range(5):
        for t2 in range(5):
            t1_norm = c.norm_ds[t1][:]
            t2_norm = c.norm_ds[t2][:]
            m = len(t1_norm)
            t1_orig = orig_ds[t1][:]
            t2_orig = orig_ds[t2][:]
            real_corr = np.average(t1_norm * t2_norm)
            real_corr_verify = corr(t1_orig, t2_orig)
            real_corr_verify2 = np.average(
                normalize(t1_orig) * normalize(t2_orig))
            assert abs(real_corr - real_corr_verify) < 0.0000001
            assert abs(real_corr - real_corr_verify2) < 0.0000001

            t1_fft = np.fft.fft(t1_norm) / m
            t2_fft = np.fft.fft(t2_norm) / m

            assert abs(sum(abs(t1_fft)**2) - 1) < 0.00001
            assert abs(sum(abs(t2_fft)**2) - 1) < 0.00001

            approx_corr_all_coeff = 1 - (np.linalg.norm(t1_fft - t2_fft)**
                                         2) / 2

            approx_corr = c._FourierApproximation__correlate(t1, t2, e, None)

            print("Real correlation:   " + str(real_corr))
            print("Approx correlation: " + str(approx_corr_all_coeff) +
                  " (all coefficients)")
            print("Approx correlation: " + str(approx_corr) +
                  " (k coefficients)")

            assert abs(real_corr - approx_corr) <= e
def test_approx_correlation_error(testfiles):
    name = testfiles["dataset1_normalized.h5"]
    name2 = testfiles["database1.h5"]

    c = FourierApproximation(name)
    orig_ds = DatasetH5(name2)

    e = 0.04
    for t1 in range(5):
        for t2 in range(5):
            t1_norm = c.norm_ds[t1][:]
            t2_norm = c.norm_ds[t2][:]
            m = len(t1_norm)
            t1_orig = orig_ds[t1][:]
            t2_orig = orig_ds[t2][:]
            real_corr = np.average(t1_norm * t2_norm)
            real_corr_verify = corr(t1_orig, t2_orig)
            real_corr_verify2 = np.average(normalize(t1_orig) * normalize(t2_orig))
            assert abs(real_corr - real_corr_verify) < 0.0000001
            assert abs(real_corr - real_corr_verify2) < 0.0000001

            t1_fft = np.fft.fft(t1_norm) / m
            t2_fft = np.fft.fft(t2_norm) / m

            assert abs(sum(abs(t1_fft) ** 2) - 1) < 0.00001
            assert abs(sum(abs(t2_fft) ** 2) - 1) < 0.00001

            approx_corr_all_coeff = 1 - (np.linalg.norm(t1_fft - t2_fft) ** 2)/2

            approx_corr = c._FourierApproximation__correlate(t1, t2, e, None)

            print("Real correlation:   " + str(real_corr))
            print("Approx correlation: " + str(approx_corr_all_coeff) + " (all coefficients)")
            print("Approx correlation: " + str(approx_corr) + " (k coefficients)")

            assert abs(real_corr - approx_corr) <= e