File: test_filon.py

package info (click to toggle)
python-dynasor 2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,008 kB
  • sloc: python: 5,263; sh: 20; makefile: 3
file content (65 lines) | stat: -rw-r--r-- 1,910 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
import os
import numpy as np
import pytest
from dynasor.post_processing.filon import _alpha_beta_gamma_single, fourier_cos_filon


def test_abg():
    thetas = np.array([0.0, 3.14, -1, 0.5])

    # 3x4 due to historical reasons
    res = np.array([[0.00000000, 0.31830964, -0.03850188, 0.00536042],
                    [0.66666667, 0.40590123,  0.76525831, 0.69767347],
                    [1.33333333, 0.40590123,  1.20467472, 1.30029625]])

    new = np.array([_alpha_beta_gamma_single(t) for t in thetas])  # 4x3
    assert np.allclose(res, new.T)


def test_filon_regression():

    np.random.seed(42)
    ref = '0100010001000010111010111111110011101000001111101101010110000000'
    assert ''.join(map(str, np.random.choice([0, 1], 64))) == ref

    dt_list = [1.0, 1.234]
    Nt_list = [3, 5, 101]

    results = []

    for dt in dt_list:
        for Nt in Nt_list:
            w = np.arange(Nt) * np.pi/dt/(Nt-1)
            f = np.random.normal(size=(3, Nt))
            w2, F = fourier_cos_filon(f, dt)
            F /= 2
            assert np.allclose(w, w2)
            results.extend(F.flatten())

    this_dir = os.path.dirname(__file__)
    ref = np.load(os.path.join(this_dir, 'regression_results.npy'))
    # np.save(os.path.join(this_dir, 'regression_results.npy'), results)
    assert np.allclose(ref, np.array(results))


def test_value_error_bad_input():

    # 1-D array
    f = np.random.random((11,))
    with pytest.raises(ValueError):
        fourier_cos_filon(f, dt=1.0)

    # 3-D array
    f = np.random.random((11, 31, 5))
    with pytest.raises(ValueError):
        fourier_cos_filon(f, dt=1.0)

    # len of last axis is even number
    f = np.random.random((11, 30))
    with pytest.raises(ValueError):
        fourier_cos_filon(f, dt=1.0)

    # len of last axis is one
    f = np.random.random((11, 1))
    with pytest.raises(ValueError):
        fourier_cos_filon(f, dt=1.0)