numcodecs-ebcc 0.1.1

EBCC codec implementation for the numcodecs API
Documentation
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! [![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs]
//!
//! [CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/numcodecs-rs/ci.yml?branch=main
//! [workflow]: https://github.com/juntyr/numcodecs-rs/actions/workflows/ci.yml?query=branch%3Amain
//!
//! [MSRV]: https://img.shields.io/badge/MSRV-1.87.0-blue
//! [repo]: https://github.com/juntyr/numcodecs-rs
//!
//! [Latest Version]: https://img.shields.io/crates/v/numcodecs-ebcc
//! [crates.io]: https://crates.io/crates/numcodecs-ebcc
//!
//! [Rust Doc Crate]: https://img.shields.io/docsrs/numcodecs-ebcc
//! [docs.rs]: https://docs.rs/numcodecs-ebcc/
//!
//! [Rust Doc Main]: https://img.shields.io/badge/docs-main-blue
//! [docs]: https://juntyr.github.io/numcodecs-rs/numcodecs_ebcc
//!
//! EBCC codec implementation for the [`numcodecs`] API.

#![allow(clippy::multiple_crate_versions)] // embedded-io

#[cfg(test)]
use ::serde_json as _;

use std::borrow::Cow;

use ndarray::{Array, Array1, ArrayBase, ArrayViewMut, Axis, Data, DataMut, Dimension, IxDyn};
use num_traits::Float;
use numcodecs::{
    AnyArray, AnyArrayDType, AnyArrayView, AnyArrayViewMut, AnyCowArray, Codec, StaticCodec,
    StaticCodecConfig, StaticCodecVersion,
};
use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use thiserror::Error;

type EbccCodecVersion = StaticCodecVersion<0, 1, 0>;

/// Codec providing compression using EBCC.
///
/// EBCC combines JPEG2000 compression with error-bounded residual compression.
///
/// Arrays that are higher-dimensional than 3D are encoded by compressing each
/// 3D slice with EBCC independently. Specifically, the array's shape is
/// interpreted as `[.., depth, height, width]`. If you want to compress 3D
/// slices along three different axes, you can swizzle the array axes
/// beforehand.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[schemars(deny_unknown_fields)]
pub struct EbccCodec {
    /// EBCC residual compression
    #[serde(flatten)]
    pub residual: EbccResidualType,
    /// JPEG2000 positive base compression ratio
    pub base_cr: Positive<f32>,
    /// The codec's encoding format version. Do not provide this parameter explicitly.
    #[serde(default, rename = "_version")]
    pub version: EbccCodecVersion,
}

/// Residual compression types supported by EBCC.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "residual")]
#[serde(deny_unknown_fields)]
pub enum EbccResidualType {
    #[serde(rename = "jpeg2000-only")]
    /// No residual compression - base JPEG2000 only
    Jpeg2000Only,
    #[serde(rename = "absolute")]
    /// Residual compression with absolute maximum error bound
    AbsoluteError {
        /// The positive maximum absolute error bound
        error: Positive<f32>,
    },
    #[serde(rename = "relative")]
    /// Residual compression with relative error bound
    RelativeError {
        /// The positive maximum relative error bound
        error: Positive<f32>,
    },
}

impl Codec for EbccCodec {
    type Error = EbccCodecError;

    fn encode(&self, data: AnyCowArray) -> Result<AnyArray, Self::Error> {
        match data {
            AnyCowArray::F32(data) => Ok(AnyArray::U8(
                Array1::from(compress(data, self.residual, self.base_cr)?).into_dyn(),
            )),
            encoded => Err(EbccCodecError::UnsupportedDtype(encoded.dtype())),
        }
    }

    fn decode(&self, encoded: AnyCowArray) -> Result<AnyArray, Self::Error> {
        let AnyCowArray::U8(encoded) = encoded else {
            return Err(EbccCodecError::EncodedDataNotBytes {
                dtype: encoded.dtype(),
            });
        };

        if !matches!(encoded.shape(), [_]) {
            return Err(EbccCodecError::EncodedDataNotOneDimensional {
                shape: encoded.shape().to_vec(),
            });
        }

        decompress(&AnyCowArray::U8(encoded).as_bytes())
    }

    fn decode_into(
        &self,
        encoded: AnyArrayView,
        decoded: AnyArrayViewMut,
    ) -> Result<(), Self::Error> {
        let AnyArrayView::U8(encoded) = encoded else {
            return Err(EbccCodecError::EncodedDataNotBytes {
                dtype: encoded.dtype(),
            });
        };

        if !matches!(encoded.shape(), [_]) {
            return Err(EbccCodecError::EncodedDataNotOneDimensional {
                shape: encoded.shape().to_vec(),
            });
        }

        match decoded {
            AnyArrayViewMut::F32(decoded) => {
                decompress_into(&AnyArrayView::U8(encoded).as_bytes(), decoded)
            }
            decoded => Err(EbccCodecError::UnsupportedDtype(decoded.dtype())),
        }
    }
}

impl StaticCodec for EbccCodec {
    const CODEC_ID: &'static str = "ebcc.rs";

    type Config<'de> = Self;

    fn from_config(config: Self::Config<'_>) -> Self {
        config
    }

    fn get_config(&self) -> StaticCodecConfig<'_, Self> {
        StaticCodecConfig::from(self)
    }
}

/// Errors that may occur when applying the [`EbccCodec`].
#[derive(Debug, thiserror::Error)]
pub enum EbccCodecError {
    /// [`EbccCodec`] does not support the dtype
    #[error("Ebcc does not support the dtype {0}")]
    UnsupportedDtype(AnyArrayDType),
    /// [`EbccCodec`] failed to encode the header
    #[error("Ebcc failed to encode the header")]
    HeaderEncodeFailed {
        /// Opaque source error
        source: EbccHeaderError,
    },
    /// [`EbccCodec`] can only encode >2D data where the last two dimensions
    /// must be at least 32x32 but received an array with an insufficient shape
    #[error(
        "Ebcc can only encode >2D data where the last two dimensions must be at least 32x32 but received an array of shape {shape:?}"
    )]
    InsufficientDimensions {
        /// The unexpected shape of the array
        shape: Vec<usize>,
    },
    /// [`EbccCodec`] failed to encode the data
    #[error("Ebcc failed to encode the data")]
    EbccEncodeFailed {
        /// Opaque source error
        source: EbccCodingError,
    },
    /// [`EbccCodec`] failed to encode a 3D slice
    #[error("Ebcc failed to encode a 3D slice")]
    SliceEncodeFailed {
        /// Opaque source error
        source: EbccSliceError,
    },
    /// [`EbccCodec`] can only decode one-dimensional byte arrays but received
    /// an array of a different dtype
    #[error(
        "Ebcc can only decode one-dimensional byte arrays but received an array of dtype {dtype}"
    )]
    EncodedDataNotBytes {
        /// The unexpected dtype of the encoded array
        dtype: AnyArrayDType,
    },
    /// [`EbccCodec`] can only decode one-dimensional byte arrays but received
    /// an array of a different shape
    #[error(
        "Ebcc can only decode one-dimensional byte arrays but received a byte array of shape {shape:?}"
    )]
    EncodedDataNotOneDimensional {
        /// The unexpected shape of the encoded array
        shape: Vec<usize>,
    },
    /// [`EbccCodec`] failed to decode the header
    #[error("Ebcc failed to decode the header")]
    HeaderDecodeFailed {
        /// Opaque source error
        source: EbccHeaderError,
    },
    /// [`EbccCodec`] cannot decode into an array with a mismatching shape
    #[error("Ebcc cannot decode an array of shape {decoded:?} into an array of shape {array:?}")]
    DecodeIntoShapeMismatch {
        /// The shape of the decoded data
        decoded: Vec<usize>,
        /// The mismatching shape of the array to decode into
        array: Vec<usize>,
    },
    /// [`EbccCodec`] failed to decode a 3D slice
    #[error("Ebcc failed to decode a slice")]
    SliceDecodeFailed {
        /// Opaque source error
        source: EbccSliceError,
    },
    /// [`EbccCodec`] failed to decode from an excessive number of slices
    #[error("Ebcc failed to decode from an excessive number of slices")]
    DecodeTooManySlices,
    /// [`EbccCodec`] failed to decode the data
    #[error("Ebcc failed to decode the data")]
    EbccDecodeFailed {
        /// Opaque source error
        source: EbccCodingError,
    },
}

#[expect(clippy::derive_partial_eq_without_eq)] // floats are not Eq
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Hash)]
/// Positive floating point number
pub struct Positive<T: Float>(T);

impl<T: Float> PartialEq<T> for Positive<T> {
    fn eq(&self, other: &T) -> bool {
        self.0 == *other
    }
}

impl Serialize for Positive<f32> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_f32(self.0)
    }
}

impl<'de> Deserialize<'de> for Positive<f32> {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let x = f32::deserialize(deserializer)?;

        if x > 0.0 {
            Ok(Self(x))
        } else {
            Err(serde::de::Error::invalid_value(
                serde::de::Unexpected::Float(f64::from(x)),
                &"a positive value",
            ))
        }
    }
}

impl JsonSchema for Positive<f32> {
    fn schema_name() -> Cow<'static, str> {
        Cow::Borrowed("PositiveF32")
    }

    fn schema_id() -> Cow<'static, str> {
        Cow::Borrowed(concat!(module_path!(), "::", "Positive<f32>"))
    }

    fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "number",
            "exclusiveMinimum": 0.0
        })
    }
}

#[derive(Debug, Error)]
#[error(transparent)]
/// Opaque error for when encoding or decoding the header fails
pub struct EbccHeaderError(postcard::Error);

#[derive(Debug, Error)]
#[error(transparent)]
/// Opaque error for when encoding or decoding a 3D slice fails
pub struct EbccSliceError(postcard::Error);

#[derive(Debug, Error)]
#[error(transparent)]
/// Opaque error for when encoding or decoding with EBCC fails
pub struct EbccCodingError(ebcc::EBCCError);

/// Compress the `data` array using EBCC with the provided `residual` and
/// `base_cr`.
///
/// # Errors
///
/// Errors with
/// - [`EbccCodecError::HeaderEncodeFailed`] if encoding the header failed
/// - [`EbccCodecError::InsufficientDimensions`] if the `data` has fewer than
///   two dimensions or the last two dimensions are not at least 32x32
/// - [`EbccCodecError::EbccEncodeFailed`] if encoding with EBCC failed
/// - [`EbccCodecError::SliceEncodeFailed`] if encoding a 3D slice failed
#[allow(clippy::missing_panics_doc)]
pub fn compress<S: Data<Elem = f32>, D: Dimension>(
    data: ArrayBase<S, D>,
    residual: EbccResidualType,
    base_cr: Positive<f32>,
) -> Result<Vec<u8>, EbccCodecError> {
    let mut encoded = postcard::to_extend(
        &CompressionHeader {
            dtype: EbccDType::F32,
            shape: Cow::Borrowed(data.shape()),
            version: StaticCodecVersion,
        },
        Vec::new(),
    )
    .map_err(|err| EbccCodecError::HeaderEncodeFailed {
        source: EbccHeaderError(err),
    })?;

    // EBCC cannot handle zero-length dimensions
    if data.is_empty() {
        return Ok(encoded);
    }

    let mut chunk_size = Vec::from(data.shape());
    let (width, height, depth) = match *chunk_size.as_mut_slice() {
        [ref mut rest @ .., depth, height, width] => {
            for r in rest {
                *r = 1;
            }
            (width, height, depth)
        }
        [height, width] => (width, height, 1),
        _ => {
            return Err(EbccCodecError::InsufficientDimensions {
                shape: Vec::from(data.shape()),
            });
        }
    };

    if (width < 32) || (height < 32) {
        return Err(EbccCodecError::InsufficientDimensions {
            shape: Vec::from(data.shape()),
        });
    }

    for mut slice in data.into_dyn().exact_chunks(chunk_size.as_slice()) {
        while slice.ndim() < 3 {
            slice = slice.insert_axis(Axis(0));
        }
        #[expect(clippy::unwrap_used)]
        // slice must now have at least three axes, and all but the last three
        //  must be of size 1
        let slice = slice.into_shape_with_order((depth, height, width)).unwrap();

        let encoded_slice = ebcc::ebcc_encode(
            slice,
            &ebcc::EBCCConfig {
                base_cr: base_cr.0,
                residual_compression_type: match residual {
                    EbccResidualType::Jpeg2000Only => ebcc::EBCCResidualType::Jpeg2000Only,
                    EbccResidualType::AbsoluteError { error } => {
                        ebcc::EBCCResidualType::AbsoluteError(error.0)
                    }
                    EbccResidualType::RelativeError { error } => {
                        ebcc::EBCCResidualType::RelativeError(error.0)
                    }
                },
            },
        )
        .map_err(|err| EbccCodecError::EbccEncodeFailed {
            source: EbccCodingError(err),
        })?;

        encoded = postcard::to_extend(encoded_slice.as_slice(), encoded).map_err(|err| {
            EbccCodecError::SliceEncodeFailed {
                source: EbccSliceError(err),
            }
        })?;
    }

    Ok(encoded)
}

/// Decompress the `encoded` data into an array using EBCC.
///
/// # Errors
///
/// Errors with
/// - [`EbccCodecError::HeaderDecodeFailed`] if decoding the header failed
/// - [`EbccCodecError::SliceDecodeFailed`] if decoding a 3D slice failed
/// - [`EbccCodecError::EbccDecodeFailed`] if decoding with EBCC failed
/// - [`EbccCodecError::DecodeTooManySlices`] if the encoded data contains
///   too many slices
pub fn decompress(encoded: &[u8]) -> Result<AnyArray, EbccCodecError> {
    fn decompress_typed(
        encoded: &[u8],
        shape: &[usize],
    ) -> Result<Array<f32, IxDyn>, EbccCodecError> {
        let mut decoded = Array::<f32, _>::zeros(shape);
        decompress_into_typed(encoded, decoded.view_mut())?;
        Ok(decoded)
    }

    let (header, encoded) =
        postcard::take_from_bytes::<CompressionHeader>(encoded).map_err(|err| {
            EbccCodecError::HeaderDecodeFailed {
                source: EbccHeaderError(err),
            }
        })?;

    // Return empty data for zero-size arrays
    if header.shape.iter().copied().any(|s| s == 0) {
        return match header.dtype {
            EbccDType::F32 => Ok(AnyArray::F32(Array::zeros(&*header.shape))),
        };
    }

    match header.dtype {
        EbccDType::F32 => Ok(AnyArray::F32(decompress_typed(encoded, &header.shape)?)),
    }
}

/// Decompress the `encoded` data into the `decoded` array using EBCC.
///
/// # Errors
///
/// Errors with
/// - [`EbccCodecError::HeaderDecodeFailed`] if decoding the header failed
/// - [`EbccCodecError::DecodeIntoShapeMismatch`] is the `decoded` array shape
///   does not match the shape of the decoded data
/// - [`EbccCodecError::SliceDecodeFailed`] if decoding a 3D slice failed
/// - [`EbccCodecError::EbccDecodeFailed`] if decoding with EBCC failed
/// - [`EbccCodecError::DecodeTooManySlices`] if the encoded data contains
///   too many slices
pub fn decompress_into<S: DataMut<Elem = f32>, D: Dimension>(
    encoded: &[u8],
    decoded: ArrayBase<S, D>,
) -> Result<(), EbccCodecError> {
    let (header, encoded) =
        postcard::take_from_bytes::<CompressionHeader>(encoded).map_err(|err| {
            EbccCodecError::HeaderDecodeFailed {
                source: EbccHeaderError(err),
            }
        })?;

    if decoded.shape() != &*header.shape {
        return Err(EbccCodecError::DecodeIntoShapeMismatch {
            decoded: header.shape.into_owned(),
            array: Vec::from(decoded.shape()),
        });
    }

    // Return empty data for zero-size arrays
    if header.shape.iter().copied().any(|s| s == 0) {
        return match header.dtype {
            EbccDType::F32 => Ok(()),
        };
    }

    match header.dtype {
        EbccDType::F32 => decompress_into_typed(encoded, decoded.into_dyn().view_mut()),
    }
}

fn decompress_into_typed(
    mut encoded: &[u8],
    mut decoded: ArrayViewMut<f32, IxDyn>,
) -> Result<(), EbccCodecError> {
    let mut chunk_size = Vec::from(decoded.shape());
    let (width, height, depth) = match *chunk_size.as_mut_slice() {
        [ref mut rest @ .., depth, height, width] => {
            for r in rest {
                *r = 1;
            }
            (width, height, depth)
        }
        [height, width] => (width, height, 1),
        [width] => (width, 1, 1),
        [] => (1, 1, 1),
    };

    for mut slice in decoded.exact_chunks_mut(chunk_size.as_slice()) {
        let (encoded_slice, rest) =
            postcard::take_from_bytes::<Cow<[u8]>>(encoded).map_err(|err| {
                EbccCodecError::SliceDecodeFailed {
                    source: EbccSliceError(err),
                }
            })?;
        encoded = rest;

        while slice.ndim() < 3 {
            slice = slice.insert_axis(Axis(0));
        }
        #[expect(clippy::unwrap_used)]
        // slice must now have at least three axes, and all but the last
        //  three must be of size 1
        let slice = slice.into_shape_with_order((depth, height, width)).unwrap();

        ebcc::ebcc_decode_into(&encoded_slice, slice).map_err(|err| {
            EbccCodecError::EbccDecodeFailed {
                source: EbccCodingError(err),
            }
        })?;
    }

    if !encoded.is_empty() {
        return Err(EbccCodecError::DecodeTooManySlices);
    }

    Ok(())
}

#[derive(Serialize, Deserialize)]
struct CompressionHeader<'a> {
    dtype: EbccDType,
    #[serde(borrow)]
    shape: Cow<'a, [usize]>,
    version: EbccCodecVersion,
}

/// Dtypes that EBCC can compress and decompress
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
enum EbccDType {
    #[serde(rename = "f32", alias = "float32")]
    F32,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_unsupported_dtype() {
        let codec = EbccCodec {
            residual: EbccResidualType::Jpeg2000Only,
            base_cr: Positive(10.0),
            version: StaticCodecVersion,
        };

        let data = Array1::<i32>::zeros(100);
        let result = codec.encode(AnyCowArray::I32(data.into_dyn().into()));

        assert!(matches!(result, Err(EbccCodecError::UnsupportedDtype(_))));
    }

    #[test]
    fn test_invalid_dimensions() {
        let codec = EbccCodec {
            residual: EbccResidualType::Jpeg2000Only,
            base_cr: Positive(10.0),
            version: StaticCodecVersion,
        };

        // Test dimensions too small (32 < 32x32 requirement)
        let data = Array::zeros(32);
        let result = codec.encode(AnyCowArray::F32(data.into_dyn().into()));
        assert!(
            matches!(result, Err(EbccCodecError::InsufficientDimensions { shape }) if shape == [32])
        );

        // Test dimensions too small (16x16 < 32x32 requirement)
        let data = Array::zeros((16, 16));
        let result = codec.encode(AnyCowArray::F32(data.into_dyn().into()));
        assert!(
            matches!(result, Err(EbccCodecError::InsufficientDimensions { shape }) if shape == [16, 16])
        );

        // Test mixed valid/invalid dimensions
        let data = Array::zeros((1, 32, 16));
        let result = codec.encode(AnyCowArray::F32(data.into_dyn().into()));
        assert!(
            matches!(result, Err(EbccCodecError::InsufficientDimensions { shape }) if shape == [1, 32, 16])
        );

        // Test valid dimensions
        let data = Array::zeros((1, 32, 32));
        let result = codec.encode(AnyCowArray::F32(data.into_dyn().into()));
        assert!(result.is_ok());

        // Test valid dimensions with slicing
        let data = Array::zeros((2, 2, 2, 32, 32));
        let result = codec.encode(AnyCowArray::F32(data.into_dyn().into()));
        assert!(result.is_ok());
    }

    #[test]
    fn test_large_array() -> Result<(), EbccCodecError> {
        // Test with a larger array (similar to small climate dataset)
        let height = 721; // Quarter degree resolution
        let width = 1440;
        let frames = 1;

        #[expect(clippy::suboptimal_flops, clippy::cast_precision_loss)]
        let data = Array::from_shape_fn((frames, height, width), |(_k, i, j)| {
            let lat = -90.0 + (i as f32 / height as f32) * 180.0;
            let lon = -180.0 + (j as f32 / width as f32) * 360.0;
            #[allow(clippy::let_and_return)]
            let temp = 273.15 + 30.0 * (1.0 - lat.abs() / 90.0) + 5.0 * (lon / 180.0).sin();
            temp
        });

        let codec_error = 0.1;
        let codec = EbccCodec {
            residual: EbccResidualType::AbsoluteError {
                error: Positive(codec_error),
            },
            base_cr: Positive(20.0),
            version: StaticCodecVersion,
        };

        let encoded = codec.encode(AnyArray::F32(data.clone().into_dyn()).into_cow())?;
        let decoded = codec.decode(encoded.cow())?;

        let AnyArray::U8(encoded) = encoded else {
            return Err(EbccCodecError::EncodedDataNotBytes {
                dtype: encoded.dtype(),
            });
        };

        let AnyArray::F32(decoded) = decoded else {
            return Err(EbccCodecError::UnsupportedDtype(decoded.dtype()));
        };

        // Check compression ratio
        let original_size = data.len() * std::mem::size_of::<f32>();
        #[allow(clippy::cast_precision_loss)]
        let compression_ratio = original_size as f64 / encoded.len() as f64;

        assert!(
            compression_ratio > 5.0,
            "Compression ratio {compression_ratio} should be at least 5:1",
        );

        // Check error bound is respected
        let max_error = data
            .iter()
            .zip(decoded.iter())
            .map(|(&orig, &decomp)| (orig - decomp).abs())
            .fold(0.0f32, f32::max);

        assert!(
            max_error <= (codec_error + 1e-6),
            "Max error {max_error} exceeds error bound {codec_error}",
        );

        Ok(())
    }
}