numcodecs_fixed_offset_scale/
lib.rsuse ndarray::{Array, ArrayBase, ArrayView, ArrayViewMut, Data, Dimension};
use num_traits::Float;
use numcodecs::{
AnyArray, AnyArrayAssignError, AnyArrayDType, AnyArrayView, AnyArrayViewMut, AnyCowArray,
Codec, StaticCodec, StaticCodecConfig,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct FixedOffsetScaleCodec {
pub offset: f64,
pub scale: f64,
}
impl Codec for FixedOffsetScaleCodec {
type Error = FixedOffsetScaleCodecError;
fn encode(&self, data: AnyCowArray) -> Result<AnyArray, Self::Error> {
match data {
#[allow(clippy::cast_possible_truncation)]
AnyCowArray::F32(data) => Ok(AnyArray::F32(scale(
data,
self.offset as f32,
self.scale as f32,
))),
AnyCowArray::F64(data) => Ok(AnyArray::F64(scale(data, self.offset, self.scale))),
encoded => Err(FixedOffsetScaleCodecError::UnsupportedDtype(
encoded.dtype(),
)),
}
}
fn decode(&self, encoded: AnyCowArray) -> Result<AnyArray, Self::Error> {
match encoded {
#[allow(clippy::cast_possible_truncation)]
AnyCowArray::F32(encoded) => Ok(AnyArray::F32(unscale(
encoded,
self.offset as f32,
self.scale as f32,
))),
AnyCowArray::F64(encoded) => {
Ok(AnyArray::F64(unscale(encoded, self.offset, self.scale)))
}
encoded => Err(FixedOffsetScaleCodecError::UnsupportedDtype(
encoded.dtype(),
)),
}
}
fn decode_into(
&self,
encoded: AnyArrayView,
decoded: AnyArrayViewMut,
) -> Result<(), Self::Error> {
match (encoded, decoded) {
#[allow(clippy::cast_possible_truncation)]
(AnyArrayView::F32(encoded), AnyArrayViewMut::F32(decoded)) => {
unscale_into(encoded, decoded, self.offset as f32, self.scale as f32)
}
(AnyArrayView::F64(encoded), AnyArrayViewMut::F64(decoded)) => {
unscale_into(encoded, decoded, self.offset, self.scale)
}
(encoded @ (AnyArrayView::F32(_) | AnyArrayView::F64(_)), decoded) => {
Err(FixedOffsetScaleCodecError::MismatchedDecodeIntoArray {
source: AnyArrayAssignError::DTypeMismatch {
src: encoded.dtype(),
dst: decoded.dtype(),
},
})
}
(encoded, _decoded) => Err(FixedOffsetScaleCodecError::UnsupportedDtype(
encoded.dtype(),
)),
}
}
}
impl StaticCodec for FixedOffsetScaleCodec {
const CODEC_ID: &'static str = "fixed-offset-scale";
type Config<'de> = Self;
fn from_config(config: Self::Config<'_>) -> Self {
config
}
fn get_config(&self) -> StaticCodecConfig<Self> {
StaticCodecConfig::from(self)
}
}
#[derive(Debug, Error)]
pub enum FixedOffsetScaleCodecError {
#[error("FixedOffsetScale does not support the dtype {0}")]
UnsupportedDtype(AnyArrayDType),
#[error("FixedOffsetScale cannot decode into the provided array")]
MismatchedDecodeIntoArray {
#[from]
source: AnyArrayAssignError,
},
}
pub fn scale<T: Float, S: Data<Elem = T>, D: Dimension>(
data: ArrayBase<S, D>,
offset: T,
scale: T,
) -> Array<T, D> {
let inverse_scale = scale.recip();
let mut data = data.into_owned();
data.mapv_inplace(|x| (x - offset) * inverse_scale);
data
}
pub fn unscale<T: Float, S: Data<Elem = T>, D: Dimension>(
data: ArrayBase<S, D>,
offset: T,
scale: T,
) -> Array<T, D> {
let mut data = data.into_owned();
data.mapv_inplace(|x| x.mul_add(scale, offset));
data
}
#[allow(clippy::needless_pass_by_value)]
pub fn unscale_into<T: Float, D: Dimension>(
data: ArrayView<T, D>,
mut out: ArrayViewMut<T, D>,
offset: T,
scale: T,
) -> Result<(), FixedOffsetScaleCodecError> {
if data.shape() != out.shape() {
return Err(FixedOffsetScaleCodecError::MismatchedDecodeIntoArray {
source: AnyArrayAssignError::ShapeMismatch {
src: data.shape().to_vec(),
dst: out.shape().to_vec(),
},
});
}
for (d, o) in data.iter().zip(out.iter_mut()) {
*o = (*d).mul_add(scale, offset);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn identity() {
let data = (0..1000).map(f64::from).collect::<Vec<_>>();
let data = Array::from_vec(data);
let encoded = scale(data.view(), 0.0, 1.0);
for (r, e) in data.iter().zip(encoded.iter()) {
assert_eq!((*r).to_bits(), (*e).to_bits());
}
let decoded = unscale(encoded, 0.0, 1.0);
for (r, d) in data.iter().zip(decoded.iter()) {
assert_eq!((*r).to_bits(), (*d).to_bits());
}
}
#[test]
fn roundtrip() {
let data = (0..1000).map(f64::from).collect::<Vec<_>>();
let data = Array::from_vec(data);
let encoded = scale(data.view(), 512.0, 64.0);
for (r, e) in data.iter().zip(encoded.iter()) {
assert_eq!((((*r) - 512.0) / 64.0).to_bits(), (*e).to_bits());
}
let decoded = unscale(encoded, 512.0, 64.0);
for (r, d) in data.iter().zip(decoded.iter()) {
assert_eq!((*r).to_bits(), (*d).to_bits());
}
}
}