File: test_timedenoising.py

package info (click to toggle)
pynpoint 0.11.0-7
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,412 kB
  • sloc: python: 13,645; makefile: 79
file content (210 lines) | stat: -rw-r--r-- 9,059 bytes parent folder | download | duplicates (3)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os

import h5py
import pytest
import numpy as np

from pynpoint.core.pypeline import Pypeline
from pynpoint.readwrite.fitsreading import FitsReadingModule
from pynpoint.processing.resizing import AddLinesModule
from pynpoint.processing.timedenoising import CwtWaveletConfiguration, DwtWaveletConfiguration, \
                                              WaveletTimeDenoisingModule, TimeNormalizationModule
from pynpoint.util.tests import create_config, remove_test_data, create_star_data


class TestTimeDenoising:

    def setup_class(self) -> None:

        self.limit = 1e-10
        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(self.test_dir+'images')
        create_config(self.test_dir+'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self) -> None:

        remove_test_data(self.test_dir, folders=['images'])

    def test_read_data(self) -> None:

        module = FitsReadingModule(name_in='read',
                                   image_tag='images',
                                   input_dir=self.test_dir+'images',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('read')

        data = self.pipeline.get_data('images')
        assert np.sum(data) == pytest.approx(105.54278879805277, rel=self.limit, abs=0.)
        assert data.shape == (10, 11, 11)

    def test_wavelet_denoising_cwt_dog(self) -> None:

        cwt_config = CwtWaveletConfiguration(wavelet='dog',
                                             wavelet_order=2,
                                             keep_mean=False,
                                             resolution=0.5)

        assert cwt_config.m_wavelet == 'dog'
        assert cwt_config.m_wavelet_order == 2
        assert not cwt_config.m_keep_mean
        assert cwt_config.m_resolution == 0.5

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_cwt_dog',
                                            image_in_tag='images',
                                            image_out_tag='wavelet_cwt_dog',
                                            padding='zero',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_cwt_dog')

        data = self.pipeline.get_data('wavelet_cwt_dog')
        assert np.sum(data) == pytest.approx(105.1035789572968, rel=self.limit, abs=0.)
        assert data.shape == (10, 11, 11)

        with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 4

        self.pipeline.run_module('wavelet_cwt_dog')

        data_multi = self.pipeline.get_data('wavelet_cwt_dog')
        assert data == pytest.approx(data_multi, rel=self.limit, abs=0.)
        assert data.shape == data_multi.shape

    def test_wavelet_denoising_cwt_morlet(self) -> None:

        with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 1

        cwt_config = CwtWaveletConfiguration(wavelet='morlet',
                                             wavelet_order=5,
                                             keep_mean=False,
                                             resolution=0.5)

        assert cwt_config.m_wavelet == 'morlet'
        assert cwt_config.m_wavelet_order == 5
        assert not cwt_config.m_keep_mean
        assert cwt_config.m_resolution == 0.5

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_cwt_morlet',
                                            image_in_tag='images',
                                            image_out_tag='wavelet_cwt_morlet',
                                            padding='mirror',
                                            median_filter=False,
                                            threshold_function='hard')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_cwt_morlet')

        data = self.pipeline.get_data('wavelet_cwt_morlet')
        assert np.sum(data) == pytest.approx(104.86262840716438, rel=self.limit, abs=0.)
        assert data.shape == (10, 11, 11)

        data = self.pipeline.get_attribute('wavelet_cwt_morlet', 'NFRAMES', static=False)
        assert data[0] == data[1] == 5

    def test_wavelet_denoising_dwt(self) -> None:

        dwt_config = DwtWaveletConfiguration(wavelet='db8')

        assert dwt_config.m_wavelet == 'db8'

        module = WaveletTimeDenoisingModule(wavelet_configuration=dwt_config,
                                            name_in='wavelet_dwt',
                                            image_in_tag='images',
                                            image_out_tag='wavelet_dwt',
                                            padding='zero',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_dwt')

        data = self.pipeline.get_data('wavelet_dwt')
        assert np.sum(data) == pytest.approx(105.54278879805277, rel=self.limit, abs=0.)
        assert data.shape == (10, 11, 11)

    def test_time_normalization(self) -> None:

        module = TimeNormalizationModule(name_in='timenorm',
                                         image_in_tag='images',
                                         image_out_tag='timenorm')

        self.pipeline.add_module(module)
        self.pipeline.run_module('timenorm')

        data = self.pipeline.get_data('timenorm')
        assert np.sum(data) == pytest.approx(56.443663773873, rel=self.limit, abs=0.)
        assert data.shape == (10, 11, 11)

    def test_wavelet_denoising_even_size(self) -> None:

        module = AddLinesModule(name_in='add',
                                image_in_tag='images',
                                image_out_tag='images_even',
                                lines=(1, 0, 1, 0))

        self.pipeline.add_module(module)
        self.pipeline.run_module('add')

        data = self.pipeline.get_data('images_even')
        assert np.sum(data) == pytest.approx(105.54278879805275, rel=self.limit, abs=0.)
        assert data.shape == (10, 12, 12)

        cwt_config = CwtWaveletConfiguration(wavelet='dog',
                                             wavelet_order=2,
                                             keep_mean=False,
                                             resolution=0.5)

        assert cwt_config.m_wavelet == 'dog'
        assert cwt_config.m_wavelet_order == 2
        assert not cwt_config.m_keep_mean
        assert cwt_config.m_resolution == 0.5

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_even_1',
                                            image_in_tag='images_even',
                                            image_out_tag='wavelet_even_1',
                                            padding='zero',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_even_1')

        data = self.pipeline.get_data('wavelet_even_1')
        assert np.sum(data) == pytest.approx(105.1035789572968, rel=self.limit, abs=0.)
        assert data.shape == (10, 12, 12)

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_even_2',
                                            image_in_tag='images_even',
                                            image_out_tag='wavelet_even_2',
                                            padding='mirror',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_even_2')

        data = self.pipeline.get_data('wavelet_even_2')
        assert np.sum(data) == pytest.approx(105.06809820408587, rel=self.limit, abs=0.)
        assert data.shape == (10, 12, 12)

        data = self.pipeline.get_attribute('images', 'NFRAMES', static=False)
        assert data == pytest.approx([5, 5], rel=self.limit, abs=0.)

        data = self.pipeline.get_attribute('wavelet_even_1', 'NFRAMES', static=False)
        assert data == pytest.approx([5, 5], rel=self.limit, abs=0.)

        data = self.pipeline.get_attribute('wavelet_even_2', 'NFRAMES', static=False)
        assert data == pytest.approx([5, 5], rel=self.limit, abs=0.)