compression-safeguards 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. compression_safeguards/__init__.py +288 -0
  2. compression_safeguards/api.py +1429 -0
  3. compression_safeguards/py.typed +0 -0
  4. compression_safeguards/safeguards/__init__.py +112 -0
  5. compression_safeguards/safeguards/_qois/__init__.py +407 -0
  6. compression_safeguards/safeguards/_qois/__main__.py +32 -0
  7. compression_safeguards/safeguards/_qois/bound.py +294 -0
  8. compression_safeguards/safeguards/_qois/context.py +416 -0
  9. compression_safeguards/safeguards/_qois/expr/__init__.py +292 -0
  10. compression_safeguards/safeguards/_qois/expr/abc.py +416 -0
  11. compression_safeguards/safeguards/_qois/expr/abs.py +98 -0
  12. compression_safeguards/safeguards/_qois/expr/addsub.py +643 -0
  13. compression_safeguards/safeguards/_qois/expr/array.py +194 -0
  14. compression_safeguards/safeguards/_qois/expr/classification.py +283 -0
  15. compression_safeguards/safeguards/_qois/expr/combinators.py +492 -0
  16. compression_safeguards/safeguards/_qois/expr/comparison.py +1315 -0
  17. compression_safeguards/safeguards/_qois/expr/constfold.py +105 -0
  18. compression_safeguards/safeguards/_qois/expr/data.py +322 -0
  19. compression_safeguards/safeguards/_qois/expr/divmul.py +930 -0
  20. compression_safeguards/safeguards/_qois/expr/finite_difference.py +190 -0
  21. compression_safeguards/safeguards/_qois/expr/group.py +75 -0
  22. compression_safeguards/safeguards/_qois/expr/hashing.py +223 -0
  23. compression_safeguards/safeguards/_qois/expr/hyperbolic.py +656 -0
  24. compression_safeguards/safeguards/_qois/expr/literal.py +245 -0
  25. compression_safeguards/safeguards/_qois/expr/logexp.py +395 -0
  26. compression_safeguards/safeguards/_qois/expr/manipulation.py +159 -0
  27. compression_safeguards/safeguards/_qois/expr/modulo.py +1159 -0
  28. compression_safeguards/safeguards/_qois/expr/neg.py +80 -0
  29. compression_safeguards/safeguards/_qois/expr/power.py +580 -0
  30. compression_safeguards/safeguards/_qois/expr/reciprocal.py +129 -0
  31. compression_safeguards/safeguards/_qois/expr/reporter.py +134 -0
  32. compression_safeguards/safeguards/_qois/expr/round.py +391 -0
  33. compression_safeguards/safeguards/_qois/expr/sign.py +106 -0
  34. compression_safeguards/safeguards/_qois/expr/square.py +248 -0
  35. compression_safeguards/safeguards/_qois/expr/trigonometric.py +865 -0
  36. compression_safeguards/safeguards/_qois/expr/where.py +200 -0
  37. compression_safeguards/safeguards/_qois/interval.py +82 -0
  38. compression_safeguards/safeguards/_qois/lexer.py +457 -0
  39. compression_safeguards/safeguards/_qois/parser.py +1050 -0
  40. compression_safeguards/safeguards/_qois/typing.py +78 -0
  41. compression_safeguards/safeguards/abc.py +109 -0
  42. compression_safeguards/safeguards/combinators/__init__.py +3 -0
  43. compression_safeguards/safeguards/combinators/all.py +487 -0
  44. compression_safeguards/safeguards/combinators/any.py +572 -0
  45. compression_safeguards/safeguards/combinators/assume_safe.py +186 -0
  46. compression_safeguards/safeguards/combinators/everywhere.py +357 -0
  47. compression_safeguards/safeguards/combinators/select.py +643 -0
  48. compression_safeguards/safeguards/eb.py +549 -0
  49. compression_safeguards/safeguards/pointwise/__init__.py +3 -0
  50. compression_safeguards/safeguards/pointwise/abc.py +186 -0
  51. compression_safeguards/safeguards/pointwise/eb.py +366 -0
  52. compression_safeguards/safeguards/pointwise/lossless.py +203 -0
  53. compression_safeguards/safeguards/pointwise/qoi/__init__.py +3 -0
  54. compression_safeguards/safeguards/pointwise/qoi/eb.py +646 -0
  55. compression_safeguards/safeguards/pointwise/same.py +748 -0
  56. compression_safeguards/safeguards/pointwise/sign.py +362 -0
  57. compression_safeguards/safeguards/qois.py +446 -0
  58. compression_safeguards/safeguards/stencil/__init__.py +482 -0
  59. compression_safeguards/safeguards/stencil/abc.py +222 -0
  60. compression_safeguards/safeguards/stencil/qoi/__init__.py +3 -0
  61. compression_safeguards/safeguards/stencil/qoi/eb.py +1581 -0
  62. compression_safeguards/utils/__init__.py +3 -0
  63. compression_safeguards/utils/_compat.py +577 -0
  64. compression_safeguards/utils/_float128.py +30 -0
  65. compression_safeguards/utils/bindings.py +499 -0
  66. compression_safeguards/utils/cast.py +574 -0
  67. compression_safeguards/utils/error.py +833 -0
  68. compression_safeguards/utils/intervals.py +1466 -0
  69. compression_safeguards/utils/typing.py +42 -0
  70. compression_safeguards-1.0.0.dist-info/METADATA +599 -0
  71. compression_safeguards-1.0.0.dist-info/RECORD +73 -0
  72. compression_safeguards-1.0.0.dist-info/WHEEL +4 -0
  73. compression_safeguards-1.0.0.dist-info/licenses/LICENSE +373 -0
@@ -0,0 +1,288 @@
1
+ """
2
+ # Safe, Trusted, and Fearless lossy compression with `compression-safeguards`
3
+
4
+ Lossy compression can be *scary* as valuable information or features of the
5
+ data may be lost.
6
+
7
+ By using safeguards to **guarantee** your safety requirements, lossy
8
+ compression can be applied safely and *without fear*.
9
+
10
+ ## Overview
11
+
12
+ This package provides several
13
+ [`Safeguard`][.safeguards.abc.Safeguard]s (refer to the
14
+ [`SafeguardKind`][.safeguards.SafeguardKind] for an enumeration) with which you
15
+ can express *your* requirements for lossy compression to be safe to use.
16
+
17
+ The safeguards are then combined in the
18
+ [`Safeguards`][.api.Safeguards], which can be used to compute and apply the
19
+ required correction to lossy-compressed data so that it satisfies your safety
20
+ guarantees.
21
+
22
+ This package provides the implementations of the safeguards and the low-level
23
+ `Safeguards` API. Please also refer to the following integrations of the
24
+ safeguards with popular compression APIs:
25
+
26
+ - [`numcodecs-safeguards`][numcodecs_safeguards]: provides the
27
+ [`SafeguardedCodec`][numcodecs_safeguards.SafeguardedCodec] meta-compressor that
28
+ conveniently applies safeguards to any compressor using the
29
+ [`numcodecs.abc.Codec`][numcodecs.abc.Codec] API.
30
+ - [`xarray-safeguards`][xarray_safeguards]: provides functionality to use
31
+ safeguards with (chunked) [`xarray.DataArray`][xarray.DataArray]s.
32
+
33
+ ## Examples
34
+
35
+ In the following examples, we assume that there is *some* compressor that can
36
+ `compress` and `decompress` n-dimensional `data` using functions of that name.
37
+
38
+ ### Basic Usage
39
+
40
+ You can guarantee an absolute error bound of $eb_{abs} = 0.1$ as follows:
41
+
42
+ <!--
43
+ ```py
44
+ import numpy as np
45
+ def compress(data):
46
+ return data
47
+ def decompress(data):
48
+ return np.zeros_like(data)
49
+ ```
50
+ -->
51
+ <!--pytest-codeblocks:cont-->
52
+ ```py
53
+ import numpy as np
54
+ from compression_safeguards import Safeguards
55
+
56
+ # create the `Safeguards`
57
+ sg = Safeguards(safeguards=[
58
+ # guarantee an absolute error bound of 0.1:
59
+ # |x - x'| <= 0.1
60
+ dict(kind="eb", type="abs", eb=0.1),
61
+ ])
62
+
63
+ # generate some random data to compress
64
+ data = np.random.normal(size=(10, 10, 10))
65
+
66
+ ## compression
67
+
68
+ # compress and decompress the data using *some* compressor
69
+ compressed = compress(data)
70
+ decompressed = decompress(compressed)
71
+
72
+ # compute the correction that the safeguards would need to apply to
73
+ # guarantee the selected safety requirements
74
+ correction = sg.compute_correction(data, decompressed)
75
+
76
+ # now the compressed data and correction can be stored somewhere
77
+ # ...
78
+ # and loaded again to decompress
79
+
80
+ ## decompression
81
+ decompressed = decompress(compressed)
82
+ decompressed = sg.apply_correction(decompressed, correction)
83
+
84
+ # the safeguard properties are now guaranteed to hold
85
+ assert np.all(np.abs(data - decompressed) <= 0.1)
86
+ ```
87
+
88
+ ### Instantiating the safeguards
89
+
90
+ The safeguards can be instantiated from JSON-like configuration:
91
+
92
+ ```py
93
+ from compression_safeguards import Safeguards
94
+
95
+ sg = Safeguards(safeguards=[
96
+ dict(kind="eb", type="abs", eb=0.1),
97
+ ])
98
+ ```
99
+
100
+ or by using the [`SafeguardKind`][.safeguards.SafeguardKind]:
101
+
102
+ ```py
103
+ from compression_safeguards import Safeguards, SafeguardKind
104
+
105
+ sg = Safeguards(safeguards=[
106
+ SafeguardKind.eb.value(type="abs", eb=0.1),
107
+ ])
108
+ ```
109
+
110
+ These two methods can be freely combined.
111
+
112
+ The entire safeguards can also be turned into a JSON configuration and
113
+ recreated from such configuration:
114
+
115
+ ```py
116
+ from compression_safeguards import Safeguards
117
+
118
+ sg = Safeguards(safeguards=[
119
+ dict(kind="eb", type="abs", eb=0.1),
120
+ ])
121
+ config = sg.get_config()
122
+
123
+ sg = Safeguards.from_config(config)
124
+ assert sg.get_config() == config
125
+ ```
126
+
127
+ ### Combining several safeguards
128
+
129
+ All of the provided safeguards can be freely combined and are guaranteed to
130
+ work together.
131
+
132
+ Providing several safeguards means that *all* the specified
133
+ safety requirements must be upheld:
134
+
135
+ ```py
136
+ from compression_safeguards import Safeguards
137
+
138
+ sg = Safeguards(safeguards=[
139
+ # guarantee an absolute error bound
140
+ dict(kind="eb", type="abs", eb=0.1),
141
+ # and that the data sign is preserved
142
+ dict(kind="sign"),
143
+ ])
144
+ ```
145
+
146
+ This package also provides several combinators that can be used to express
147
+ pointwise logical combinations of safeguards:
148
+
149
+ ```py
150
+ from compression_safeguards import Safeguards
151
+
152
+ sg = Safeguards(safeguards=[
153
+ # guarantee that, for each element, *both* an absolute error bound of 0.1
154
+ # *and* a relative error bound of 1% are upheld
155
+ dict(kind="all", safeguards=[
156
+ dict(kind="eb", type="abs", eb=0.1),
157
+ dict(kind="eb", type="rel", eb=0.01),
158
+ ]),
159
+ ])
160
+
161
+ sg = Safeguards(safeguards=[
162
+ # guarantee that, for each element, an absolute error bound of 0.1
163
+ # *or* a relative error bound of 1% are upheld
164
+ dict(kind="any", safeguards=[
165
+ dict(kind="eb", type="abs", eb=0.1),
166
+ dict(kind="eb", type="rel", eb=0.01),
167
+ ]),
168
+ ])
169
+ ```
170
+
171
+ ### Regionally varying safeguards using late-bound parameters
172
+
173
+ By default, all safeguards apply the same safety guarantees across the entire
174
+ data domain. This package supports two approaches for regionally varying the
175
+ guarantees, i.e. applying different guarantees to different data regions.
176
+
177
+ First, the `select` combinator can be used to switch between two or more
178
+ safeguards (or safeguard combinations) using a selection indices array. Unlike
179
+ normal safeguard parameters, this selector is a late-bound parameter whose
180
+ value is not specified during safeguard initialisation but only later when the
181
+ safeguard is applied:
182
+
183
+ <!--
184
+ ```py
185
+ import numpy as np
186
+ def compress(data):
187
+ return data
188
+ def decompress(data):
189
+ return np.zeros_like(data)
190
+ ```
191
+ -->
192
+ <!--pytest-codeblocks:cont-->
193
+ ```py
194
+ from compression_safeguards import Safeguards
195
+ from compression_safeguards.utils.bindings import Bindings
196
+
197
+ sg = Safeguards(safeguards=[
198
+ # select between a coarser, medium, and finer absolute error bound
199
+ # safeguard based on the late-bound "mask" parameter
200
+ dict(kind="select", selector="mask", safeguards=[
201
+ dict(kind="eb", type="abs", eb=1.0),
202
+ dict(kind="eb", type="abs", eb=0.1),
203
+ dict(kind="eb", type="abs", eb=0.01),
204
+ ]),
205
+ ])
206
+
207
+ # generate some random data to compress
208
+ data = np.random.normal(size=(10, 10, 10))
209
+
210
+ ## compression (now with late-bound parameters)
211
+
212
+ # compress and decompress the data using *some* compressor
213
+ compressed = compress(data)
214
+ decompressed = decompress(compressed)
215
+
216
+ # compute the correction that the safeguards would need to apply to
217
+ # guarantee the selected safety requirements
218
+ correction = sg.compute_correction(data, decompressed, late_bound=Bindings(
219
+ # bind the selection mask that selects between the three safeguards
220
+ # the mask must be broadcastable to the data shape
221
+ mask=np.array([0, 0, 0, 1, 1, 1, 1, 2, 2, 2]).reshape((1, 10, 1)),
222
+ ))
223
+
224
+ ## decompression (as before)
225
+ decompressed = decompress(compressed)
226
+ decompressed = sg.apply_correction(decompressed, correction)
227
+ ```
228
+
229
+ It is worth noting that the late-bound parameters are only needed at
230
+ compression time, decompression is unchanged.
231
+
232
+ While this first method can combine over any safeguards, it is only convenient
233
+ for selecting between a small number of different safeguards.
234
+
235
+ For the error bound safeguards (pointwise and on quantities of interest), the
236
+ error bounds themselves can be provided as late-bound parameters to allow for
237
+ smoothly varying error bounds across the data domain. The above example could
238
+ then be equivalently expressed as:
239
+
240
+ <!--
241
+ ```py
242
+ import numpy as np
243
+ def compress(data):
244
+ return data
245
+ def decompress(data):
246
+ return np.zeros_like(data)
247
+ ```
248
+ -->
249
+ <!--pytest-codeblocks:cont-->
250
+ ```py
251
+ from compression_safeguards import Safeguards
252
+ from compression_safeguards.utils.bindings import Bindings
253
+
254
+ sg = Safeguards(safeguards=[
255
+ # absolute error bound with a late-bound "eb" parameter
256
+ dict(kind="eb", type="abs", eb="eb"),
257
+ ])
258
+
259
+ # generate some random data to compress
260
+ data = np.random.normal(size=(10, 10, 10))
261
+
262
+ ## compression (now with late-bound parameters)
263
+
264
+ # compress and decompress the data using *some* compressor
265
+ compressed = compress(data)
266
+ decompressed = decompress(compressed)
267
+
268
+ # compute the correction that the safeguards would need to apply to
269
+ # guarantee the selected safety requirements
270
+ correction = sg.compute_correction(data, decompressed, late_bound=Bindings(
271
+ # bind the late-bound absolute error bound
272
+ # the bound must be broadcastable to the data shape
273
+ eb=np.array([
274
+ 1.0, 1.0, 1.0, 0.1, 0.1, 0.1, 0.1, 0.01, 0.01, 0.01,
275
+ ]).reshape((1, 10, 1)),
276
+ ))
277
+
278
+ ## decompression (as before)
279
+ decompressed = decompress(compressed)
280
+ decompressed = sg.apply_correction(decompressed, correction)
281
+ ```
282
+ """
283
+
284
+ # keep in sync with docs/hooks/api_index.py
285
+ __all__ = ["Safeguards", "SafeguardKind"]
286
+
287
+ from .api import Safeguards
288
+ from .safeguards import SafeguardKind