test_processing.py 2.89 KB
Newer Older
matthmey's avatar
matthmey committed
1
2
'''MIT License

matthmey's avatar
matthmey committed
3
4
Copyright (c) 2019, Swiss Federal Institute of Technology (ETH Zurich), Matthias Meyer

matthmey's avatar
matthmey committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.'''

matthmey's avatar
matthmey committed
24
import stuett
matthmey's avatar
matthmey committed
25
import datetime as dt
matthmey's avatar
matthmey committed
26
27
import xarray as xr
import numpy as np
matthmey's avatar
matthmey committed
28
import pandas as pd
matthmey's avatar
matthmey committed
29
30
31
32
33

from tests.stuett.sample_data import *

import pytest

matthmey's avatar
matthmey committed
34

matthmey's avatar
matthmey committed
35
class TestMinMaxDownsampling(object):
matthmey's avatar
matthmey committed
36
    def test_minmax(self):
37
38
        minmax_default = stuett.data.MinMaxDownsampling(dim="time")
        minmax_rate2 = stuett.data.MinMaxDownsampling(rate=2, dim="time")
matthmey's avatar
matthmey committed
39
40

        np.random.seed(123)
matthmey's avatar
matthmey committed
41
42
43
44
45
46
47
        da = xr.DataArray(
            np.random.rand(9, 2),
            [
                ("time", pd.date_range("2000-01-01", periods=9)),
                ("channel", ["EHE", "EHZ"]),
            ],
        )
matthmey's avatar
matthmey committed
48
49
50

        x = minmax_rate2(da)

51
52
        assert x.shape == (4, 2)
        assert x.mean() == 0.46291252327532006
matthmey's avatar
matthmey committed
53

54
    @pytest.mark.slow
matthmey's avatar
matthmey committed
55
    def test_seismic(self):
matthmey's avatar
matthmey committed
56

57
        seismic_source = stuett.data.SeismicSource(use_arclink=True)
58
        minmax_rate2 = stuett.data.MinMaxDownsampling(2, dim="time")
matthmey's avatar
matthmey committed
59
60
61
62
63
64

        x = seismic_source(config)
        x = minmax_rate2(x)

        x.compute()
        # TODO: proper test
matthmey's avatar
matthmey committed
65
66


67
# TestMinMaxDownsampling().test_minmax()
matthmey's avatar
matthmey committed
68
# TestMinMaxDownsampling().test_seismic()
69
70
71


def test_spectrogram():
72
    spectrogram = stuett.data.Spectrogram(nfft=2, stride=1, dim="time")
73
74
75

    np.random.seed(123)
    da = xr.DataArray(
matthmey's avatar
matthmey committed
76
        np.random.rand(9, 3),
77
78
        [
            ("time", pd.date_range("2000-01-01", periods=9)),
matthmey's avatar
matthmey committed
79
            ("channel", ["EHE", "EHZ", "EHN"]),
80
        ],
81
        attrs={"sampling_rate": 0.000011574},
82
83
84
85
    )

    x = spectrogram(da)

matthmey's avatar
matthmey committed
86

87
# test_spectrogram()
matthmey's avatar
matthmey committed
88
89
90


def test_lttbdownsampling():
91
    lttb_node = stuett.data.LTTBDownsampling(rate=2, dim="time")
matthmey's avatar
matthmey committed
92
93
94

    np.random.seed(123)
    da = xr.DataArray(
95
96
        np.random.rand(9, 1),
        [("time", pd.date_range("2000-01-01", periods=9)), ("channel", ["EHE"]),],
matthmey's avatar
matthmey committed
97
98
99
100
101
102
103
    )

    x = lttb_node(da)

    print(x)


104
test_lttbdownsampling()