File: test_numpy.py

package info (click to toggle)
zfp 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,744 kB
  • sloc: cpp: 20,656; ansic: 18,871; pascal: 1,231; f90: 907; python: 255; makefile: 183; sh: 79; fortran: 70
file content (238 lines) | stat: -rw-r--r-- 9,868 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python

import unittest

import zfpy
import test_utils
import numpy as np
try:
    from packaging.version import parse as version_parse
except ImportError:
    version_parse = None


class TestNumpy(unittest.TestCase):
    def lossless_round_trip(self, orig_array):
        compressed_array = zfpy.compress_numpy(orig_array, write_header=True)
        decompressed_array = zfpy.decompress_numpy(compressed_array)
        self.assertIsNone(np.testing.assert_array_equal(decompressed_array, orig_array))

    def test_different_dimensions(self):
        for dimensions in range(1, 5):
            shape = [5] * dimensions
            c_array = np.random.rand(*shape)
            self.lossless_round_trip(c_array)

            shape = range(2, 2 + dimensions)
            c_array = np.random.rand(*shape)
            self.lossless_round_trip(c_array)

    def test_different_dtypes(self):
        shape = (5, 5)
        num_elements = shape[0] * shape[1]

        for dtype in [np.float32, np.float64]:
            elements = np.random.random_sample(num_elements)
            elements = elements.astype(dtype, casting="same_kind")
            array = np.reshape(elements, newshape=shape)
            self.lossless_round_trip(array)

        if (version_parse is not None and
            (version_parse(np.__version__) >= version_parse("1.11.0"))
        ):
            for dtype in [np.int32, np.int64]:
                array = np.random.randint(2**30, size=shape, dtype=dtype)
                self.lossless_round_trip(array)
        else:
            array = np.random.randint(2**30, size=shape)
            self.lossless_round_trip(array)

    def test_advanced_decompression_checksum(self):
        ndims = 2
        ztype = zfpy.type_float
        random_array = test_utils.getRandNumpyArray(ndims, ztype)
        mode = zfpy.mode_fixed_accuracy
        compress_param_num = 1
        compression_kwargs = {
            "tolerance": test_utils.computeParameterValue(
                mode,
                compress_param_num
            ),
        }
        compressed_array = zfpy.compress_numpy(
            random_array,
            write_header=False,
            **compression_kwargs
        )

        # Decompression using the "advanced" interface which enforces no header,
        # and the user must provide all the metadata
        decompressed_array = np.empty_like(random_array)
        zfpy._decompress(
            compressed_array,
            ztype,
            random_array.shape,
            out=decompressed_array,
            **compression_kwargs
        )
        decompressed_array_dims = decompressed_array.shape + tuple(0 for i in range(4 - decompressed_array.ndim))
        decompressed_checksum = test_utils.getChecksumDecompArray(
            decompressed_array_dims,
            ztype,
            mode,
            compress_param_num
        )
        actual_checksum = test_utils.hashNumpyArray(
            decompressed_array
        )
        self.assertEqual(decompressed_checksum, actual_checksum)

    def test_memview_advanced_decompression_checksum(self):
        ndims = 2
        ztype = zfpy.type_float
        random_array = test_utils.getRandNumpyArray(ndims, ztype)
        mode = zfpy.mode_fixed_accuracy
        compress_param_num = 1
        compression_kwargs = {
            "tolerance": test_utils.computeParameterValue(
                mode,
                compress_param_num
            ),
        }
        compressed_array_tmp = zfpy.compress_numpy(
            random_array,
            write_header=False,
            **compression_kwargs
        )
        mem = memoryview(compressed_array_tmp)
        compressed_array = np.array(mem, copy=False)
        # Decompression using the "advanced" interface which enforces no header,
        # and the user must provide all the metadata
        decompressed_array = np.empty_like(random_array)
        zfpy._decompress(
            compressed_array,
            ztype,
            random_array.shape,
            out=decompressed_array,
            **compression_kwargs
        )
        decompressed_array_dims = decompressed_array.shape + tuple(0 for i in range(4 - decompressed_array.ndim))
        decompressed_checksum = test_utils.getChecksumDecompArray(
            decompressed_array_dims,
            ztype,
            mode,
            compress_param_num
        )
        actual_checksum = test_utils.hashNumpyArray(
            decompressed_array
        )
        self.assertEqual(decompressed_checksum, actual_checksum)

    def test_advanced_decompression_nonsquare(self):
        for dimensions in range(1, 5):
            shape = range(2, 2 + dimensions)
            random_array = np.random.rand(*shape)

            decompressed_array = np.empty_like(random_array)
            compressed_array = zfpy.compress_numpy(
                random_array,
                write_header=False,
            )
            zfpy._decompress(
                compressed_array,
                zfpy.dtype_to_ztype(random_array.dtype),
                random_array.shape,
                out= decompressed_array,
            )
            self.assertIsNone(np.testing.assert_array_equal(decompressed_array, random_array))

    def test_utils(self):
        for ndims in range(1, 5):
            for ztype, ztype_str in [
                    (zfpy.type_float,  "float"),
                    (zfpy.type_double, "double"),
                    (zfpy.type_int32,  "int32"),
                    (zfpy.type_int64,  "int64"),
            ]:
                orig_random_array = test_utils.getRandNumpyArray(ndims, ztype)
                orig_random_array_dims = orig_random_array.shape + tuple(0 for i in range(4 - orig_random_array.ndim))
                orig_checksum = test_utils.getChecksumOrigArray(orig_random_array_dims, ztype)
                actual_checksum = test_utils.hashNumpyArray(orig_random_array)
                self.assertEqual(orig_checksum, actual_checksum)

                for stride_str, stride_config in [
                        ("as_is", test_utils.stride_as_is),
                        ("permuted", test_utils.stride_permuted),
                        ("interleaved", test_utils.stride_interleaved),
                        #("reversed", test_utils.stride_reversed),
                ]:
                    # permuting a 1D array is not supported
                    if stride_config == test_utils.stride_permuted and ndims == 1:
                        continue
                    random_array = test_utils.generateStridedRandomNumpyArray(
                        stride_config,
                        orig_random_array
                    )
                    random_array_dims = random_array.shape + tuple(0 for i in range(4 - random_array.ndim))
                    self.assertTrue(np.equal(orig_random_array, random_array).all())

                    for compress_param_num in range(3):
                        modes = [(zfpy.mode_fixed_accuracy, "tolerance"),
                                 (zfpy.mode_fixed_precision, "precision"),
                                 (zfpy.mode_fixed_rate, "rate")]
                        if ztype in [zfpy.type_int32, zfpy.type_int64]:
                            modes = [modes[-1]] # only fixed-rate is supported for integers
                        for mode, mode_str in modes:
                            # Compression
                            compression_kwargs = {
                                mode_str: test_utils.computeParameterValue(
                                    mode,
                                    compress_param_num
                                ),
                            }

                            compressed_array = zfpy.compress_numpy(
                                random_array,
                                write_header=False,
                                **compression_kwargs
                            )
                            compressed_checksum = test_utils.getChecksumCompArray(
                                random_array_dims,
                                ztype,
                                mode,
                                compress_param_num
                            )
                            actual_checksum = test_utils.hashCompressedArray(
                                compressed_array
                            )
                            self.assertEqual(compressed_checksum, actual_checksum)

                            # Decompression
                            decompressed_checksum = test_utils.getChecksumDecompArray(
                                random_array_dims,
                                ztype,
                                mode,
                                compress_param_num
                            )

                            # Decompression using the "public" interface
                            # requires a header, so re-compress with the header
                            # included in the stream
                            compressed_array_tmp = zfpy.compress_numpy(
                                random_array,
                                write_header=True,
                                **compression_kwargs
                            )
                            mem = memoryview(compressed_array_tmp)
                            compressed_array = np.array(mem, copy=False)
                            decompressed_array = zfpy.decompress_numpy(
                                compressed_array,
                            )
                            actual_checksum = test_utils.hashNumpyArray(
                                decompressed_array
                            )
                            self.assertEqual(decompressed_checksum, actual_checksum)


if __name__ == "__main__":
    unittest.main(verbosity=2)