Skip to content

Commit 393c916

Browse files
authored
Cleanup with numcodecs-replace (#67)
1 parent 715b58d commit 393c916

2 files changed

Lines changed: 21 additions & 32 deletions

File tree

pyproject.toml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,22 @@ dependencies = [
1515
"numcodecs>=0.13.0,<0.17",
1616
"numcodecs-combinators[xarray]~=0.2.13",
1717
"numcodecs-observers~=0.1.2",
18+
"numcodecs-replace==0.1.0",
1819
"numcodecs-safeguards==0.1.0b2",
19-
"numcodecs-wasm~=0.2.2",
20-
"numcodecs-wasm-bit-round~=0.4.0",
21-
"numcodecs-wasm-fixed-offset-scale~=0.4.0",
22-
"numcodecs-wasm-jpeg2000~=0.3.0",
23-
"numcodecs-wasm-pco~=0.3.0",
24-
"numcodecs-wasm-round~=0.5.0",
25-
"numcodecs-wasm-sperr~=0.2.0",
26-
"numcodecs-wasm-stochastic-rounding~=0.2.0",
27-
"numcodecs-wasm-sz3~=0.7.0",
28-
"numcodecs-wasm-tthresh~=0.3.0",
29-
"numcodecs-wasm-zfp~=0.6.0",
30-
"numcodecs-wasm-zfp-classic~=0.4.0",
31-
"numcodecs-wasm-zstd~=0.4.0",
32-
"numcodecs-zero~=0.1.2",
20+
"numcodecs-wasm==0.2.2",
21+
"numcodecs-wasm-bit-round==0.4.0",
22+
"numcodecs-wasm-fixed-offset-scale==0.4.0",
23+
"numcodecs-wasm-jpeg2000==0.3.0",
24+
"numcodecs-wasm-pco==0.3.0",
25+
"numcodecs-wasm-round==0.5.0",
26+
"numcodecs-wasm-sperr==0.2.0",
27+
"numcodecs-wasm-stochastic-rounding==0.2.0",
28+
"numcodecs-wasm-sz3==0.7.0",
29+
"numcodecs-wasm-tthresh==0.3.0",
30+
"numcodecs-wasm-zfp==0.6.0",
31+
"numcodecs-wasm-zfp-classic==0.4.0",
32+
"numcodecs-wasm-zstd==0.4.0",
33+
"numcodecs-zero==0.1.2",
3334
"pandas~=2.2",
3435
"scipy~=1.14",
3536
"seaborn~=0.13.2",
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
__all__ = ["SafeguardedSperr"]
22

3-
import numcodecs
4-
import numcodecs.abc
5-
import numcodecs.compat
3+
import numcodecs_replace
64
import numcodecs_safeguards
75
import numcodecs_wasm_sperr
86
import numpy as np
@@ -20,8 +18,11 @@ class SafeguardedSperr(Compressor):
2018
@staticmethod
2119
def abs_bound_codec(error_bound, **kwargs):
2220
return numcodecs_safeguards.SafeguardedCodec(
21+
# inspired by H5Z-SPERR's treatment of NaN values:
22+
# https://github.com/NCAR/H5Z-SPERR/blob/72ebcb00e382886c229c5ef5a7e237fe451d5fb8/src/h5z-sperr.c#L464-L473
23+
# https://github.com/NCAR/H5Z-SPERR/blob/72ebcb00e382886c229c5ef5a7e237fe451d5fb8/src/h5zsperr_helper.cpp#L179-L212
2324
codec=CodecStack(
24-
NaNToMean(),
25+
numcodecs_replace.ReplaceFilterCodec(replacements={np.nan: "nan_mean"}),
2526
numcodecs_wasm_sperr.Sperr(mode="pwe", pwe=error_bound),
2627
),
2728
safeguards=[
@@ -35,7 +36,7 @@ def rel_bound_codec(error_bound, *, data_abs_min=None, **kwargs):
3536

3637
return numcodecs_safeguards.SafeguardedCodec(
3738
codec=CodecStack(
38-
NaNToMean(),
39+
numcodecs_replace.ReplaceFilterCodec(replacements={np.nan: "nan_mean"}),
3940
# conservative rel->abs error bound transformation,
4041
# same as convert_rel_error_to_abs_error
4142
# so that we can inform the safeguards of the rel bound
@@ -45,16 +46,3 @@ def rel_bound_codec(error_bound, *, data_abs_min=None, **kwargs):
4546
dict(kind="eb", type="rel", eb=error_bound, equal_nan=True),
4647
],
4748
)
48-
49-
50-
# inspired by H5Z-SPERR's treatment of NaN values:
51-
# https://github.com/NCAR/H5Z-SPERR/blob/72ebcb00e382886c229c5ef5a7e237fe451d5fb8/src/h5z-sperr.c#L464-L473
52-
# https://github.com/NCAR/H5Z-SPERR/blob/72ebcb00e382886c229c5ef5a7e237fe451d5fb8/src/h5zsperr_helper.cpp#L179-L212
53-
class NaNToMean(numcodecs.abc.Codec):
54-
codec_id = "nan-to-mean" # type: ignore
55-
56-
def encode(self, buf):
57-
return np.nan_to_num(buf, nan=np.nanmean(buf), posinf=np.inf, neginf=-np.inf)
58-
59-
def decode(self, buf, out=None):
60-
return numcodecs.compat.ndarray_copy(buf, out)

0 commit comments

Comments
 (0)