notfilippo commented on code in PR #62:
URL: https://github.com/apache/datasketches-rust/pull/62#discussion_r2805762022


##########
datasketches/src/density/sketch.rs:
##########
@@ -0,0 +1,601 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::io::Write;
+
+use crate::codec::SketchBytes;
+use crate::codec::SketchSlice;
+use crate::common::RandomSource;
+use crate::common::XorShift64;
+use crate::density::serialization::DENSITY_FAMILY_ID;
+use crate::density::serialization::FLAGS_IS_EMPTY;
+use crate::density::serialization::PREAMBLE_INTS_LONG;
+use crate::density::serialization::PREAMBLE_INTS_SHORT;
+use crate::density::serialization::SERIAL_VERSION;
+use crate::error::Error;
+use crate::error::ErrorKind;
+
+type SerializeValue<T> = fn(&mut SketchBytes, T);
+type DeserializeValue<T> = fn(&mut SketchSlice<'_>) -> std::io::Result<T>;
+type Point<T> = Vec<T>;
+type Level<T> = Vec<Point<T>>;
+type Levels<T> = Vec<Level<T>>;
+
+/// Floating point types supported by the density sketch.
+pub trait DensityValue: Copy + PartialOrd + 'static {
+    /// Converts from f64.
+    fn from_f64(value: f64) -> Self;
+    /// Converts to f64 for accumulation.
+    fn to_f64(self) -> f64;
+}
+
+macro_rules! impl_density_value {
+    ($name:ty, $from:expr, $to:expr) => {
+        impl DensityValue for $name {
+            #[inline(always)]
+            fn from_f64(value: f64) -> Self {
+                ($from)(value)
+            }
+
+            #[inline(always)]
+            fn to_f64(self) -> f64 {
+                ($to)(self)
+            }
+        }
+    };
+}
+
+impl_density_value!(f64, |value: f64| value, |value: f64| value);
+impl_density_value!(f32, |value: f64| value as f32, |value: f32| value as f64);
+
+/// Kernel used to compute density contributions between points.
+pub trait DensityKernel {
+    /// Returns the kernel evaluation for the two points.
+    fn evaluate<T: DensityValue>(&self, left: &[T], right: &[T]) -> T;
+}
+
+/// Gaussian kernel based on squared Euclidean distance.
+#[derive(Debug, Default, Clone, Copy)]
+pub struct GaussianKernel;
+
+impl DensityKernel for GaussianKernel {
+    fn evaluate<T: DensityValue>(&self, left: &[T], right: &[T]) -> T {
+        let mut sum = 0.0f64;
+        for (a, b) in left.iter().zip(right.iter()) {
+            let diff = a.to_f64() - b.to_f64();
+            sum += diff * diff;
+        }
+        T::from_f64((-sum).exp())
+    }
+}
+
+/// Density sketch for streaming density estimation.
+pub struct DensitySketch<
+    T: DensityValue,
+    K: DensityKernel = GaussianKernel,
+    R: RandomSource = XorShift64,
+> {
+    kernel: K,
+    rng: R,
+    k: u16,
+    dim: u32,
+    num_retained: u32,
+    n: u64,
+    levels: Levels<T>,
+}
+
+impl<T: DensityValue> DensitySketch<T, GaussianKernel, XorShift64> {
+    /// Creates a new sketch using the Gaussian kernel.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `k` is less than 2.
+    pub fn new(k: u16, dim: u32) -> Self {
+        Self::with_kernel(k, dim, GaussianKernel)
+    }
+}
+
+impl DensitySketch<f32, GaussianKernel, XorShift64> {
+    /// Deserializes a sketch using the Gaussian kernel.
+    pub fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
+        Self::deserialize_with_kernel(bytes, GaussianKernel)
+    }
+}
+
+impl DensitySketch<f64, GaussianKernel, XorShift64> {
+    /// Deserializes a sketch using the Gaussian kernel.
+    pub fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
+        Self::deserialize_with_kernel(bytes, GaussianKernel)
+    }
+}
+
+impl<K: DensityKernel> DensitySketch<f32, K, XorShift64> {
+    /// Deserializes a sketch using the provided kernel.
+    pub fn deserialize_with_kernel(bytes: &[u8], kernel: K) -> Result<Self, 
Error> {
+        Self::deserialize_with_kernel_and_rng(bytes, kernel, 
XorShift64::default())
+    }
+}
+
+impl<K: DensityKernel> DensitySketch<f64, K, XorShift64> {
+    /// Deserializes a sketch using the provided kernel.
+    pub fn deserialize_with_kernel(bytes: &[u8], kernel: K) -> Result<Self, 
Error> {
+        Self::deserialize_with_kernel_and_rng(bytes, kernel, 
XorShift64::default())
+    }
+}
+
+impl<K: DensityKernel, R: RandomSource> DensitySketch<f32, K, R> {
+    /// Deserializes a sketch using the provided kernel and random source.
+    pub fn deserialize_with_kernel_and_rng(bytes: &[u8], kernel: K, rng: R) -> 
Result<Self, Error> {
+        deserialize_inner(bytes, kernel, rng, read_f32_value)
+    }
+
+    /// Serializes the sketch to a byte vector.
+    pub fn serialize(&self) -> Vec<u8> {
+        serialize_inner(self, 4, write_f32_value)
+    }
+
+    /// Serializes the sketch to a writer.
+    pub fn serialize_to_writer(&self, writer: &mut dyn Write) -> 
std::io::Result<()> {
+        writer.write_all(&self.serialize())
+    }
+}
+
+impl<K: DensityKernel, R: RandomSource> DensitySketch<f64, K, R> {
+    /// Deserializes a sketch using the provided kernel and random source.
+    pub fn deserialize_with_kernel_and_rng(bytes: &[u8], kernel: K, rng: R) -> 
Result<Self, Error> {
+        deserialize_inner(bytes, kernel, rng, read_f64_value)
+    }
+
+    /// Serializes the sketch to a byte vector.
+    pub fn serialize(&self) -> Vec<u8> {
+        serialize_inner(self, 8, write_f64_value)
+    }
+
+    /// Serializes the sketch to a writer.
+    pub fn serialize_to_writer(&self, writer: &mut dyn Write) -> 
std::io::Result<()> {
+        writer.write_all(&self.serialize())
+    }
+}
+
+impl<T: DensityValue, K: DensityKernel> DensitySketch<T, K, XorShift64> {
+    /// Creates a new sketch with a custom kernel.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `k` is less than 2.
+    pub fn with_kernel(k: u16, dim: u32, kernel: K) -> Self {
+        Self::with_kernel_and_rng(k, dim, kernel, XorShift64::default())
+    }
+}
+
+impl<T: DensityValue, K: DensityKernel, R: RandomSource> DensitySketch<T, K, 
R> {
+    /// Creates a new sketch with a custom kernel and random source.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `k` is less than 2.
+    pub fn with_kernel_and_rng(k: u16, dim: u32, kernel: K, rng: R) -> Self {
+        assert!(k >= 2, "k must be > 1. Found: {k}");
+        Self {
+            kernel,
+            rng,
+            k,
+            dim,
+            num_retained: 0,
+            n: 0,
+            levels: vec![Vec::new()],
+        }
+    }
+
+    /// Returns the configured parameter k.
+    pub fn k(&self) -> u16 {
+        self.k
+    }
+
+    /// Returns the configured dimension.
+    pub fn dim(&self) -> u32 {
+        self.dim
+    }
+
+    /// Returns true if the sketch is empty.
+    pub fn is_empty(&self) -> bool {
+        self.num_retained == 0
+    }
+
+    /// Returns the number of points observed by this sketch.
+    pub fn n(&self) -> u64 {
+        self.n
+    }
+
+    /// Returns the number of retained points.
+    pub fn num_retained(&self) -> u32 {
+        self.num_retained
+    }
+
+    /// Returns true if the sketch is in estimation mode.
+    pub fn is_estimation_mode(&self) -> bool {
+        self.levels.len() > 1
+    }
+
+    /// Updates this sketch with a given point.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the point dimension does not match this sketch.
+    pub fn update(&mut self, point: Point<T>) {
+        self.ensure_dim(point.len());
+        self.update_point(point);
+    }
+
+    /// Updates this sketch with a slice, copying the point into the sketch.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the point dimension does not match this sketch.
+    pub fn update_slice(&mut self, point: &[T]) {
+        self.ensure_dim(point.len());
+        self.update_point(point.to_vec());
+    }
+
+    /// Merges another sketch into this one.
+    ///
+    /// # Panics
+    ///
+    /// Panics if dimensions do not match.
+    pub fn merge(&mut self, other: &Self) {
+        if other.is_empty() {
+            return;
+        }
+        if other.dim != self.dim {
+            panic!("dimension mismatch");
+        }
+        while self.levels.len() < other.levels.len() {
+            self.levels.push(Vec::new());
+        }
+        for (height, level) in other.levels.iter().enumerate() {
+            self.levels[height].extend(level.iter().cloned());
+        }
+        self.num_retained += other.num_retained;
+        self.n += other.n;
+        while self.num_retained >= self.k as u32 * self.levels.len() as u32 {
+            self.compact();
+        }
+    }
+
+    /// Returns a density estimate at a given point.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the sketch is empty.
+    pub fn estimate(&self, point: &[T]) -> T {
+        if self.is_empty() {
+            panic!("operation is undefined for an empty sketch");
+        }
+        self.ensure_dim(point.len());
+        let n = self.n as f64;
+        let mut density = 0.0f64;
+        for (height, level) in self.levels.iter().enumerate() {
+            let height_weight = weight_for_level(height) as f64;
+            for p in level {
+                density += height_weight * self.kernel.evaluate(p, 
point).to_f64() / n;
+            }
+        }
+        T::from_f64(density)
+    }
+
+    /// Returns an iterator over retained points with their weights.
+    pub fn iter(&self) -> DensityIter<'_, T> {
+        DensityIter {
+            levels: &self.levels,
+            level_index: 0,
+            item_index: 0,
+        }
+    }
+
+    fn compact(&mut self) {
+        for height in 0..self.levels.len() {
+            if self.levels[height].len() >= self.k as usize {
+                if height + 1 >= self.levels.len() {
+                    self.levels.push(Vec::new());
+                }
+                self.compact_level(height);
+                break;
+            }
+        }
+    }
+
+    fn compact_level(&mut self, height: usize) {
+        let level_len = self.levels[height].len();
+        if level_len == 0 {
+            return;
+        }
+        let bits = {
+            let rng = &mut self.rng;
+            let level = &mut self.levels[height];
+            let kernel = &self.kernel;
+            let mut bits = vec![false; level_len];
+            shuffle_with_rng(rng, level);
+            bits[0] = random_bit(rng);
+            for i in 1..level_len {
+                let mut delta = 0.0f64;
+                for (j, bit) in bits.iter().enumerate().take(i) {
+                    let weight = if *bit { 1.0 } else { -1.0 };
+                    delta += weight * kernel.evaluate(&level[i], 
&level[j]).to_f64();
+                }
+                bits[i] = delta < 0.0;

Review Comment:
   Python 
([gde.py](https://github.com/edoliberty/streaming-quantiles/blob/f688c8161a25582457b0a09deb4630a81406293b/gde.py))
 keeps ties: `signs[i] = -np.sign(delta)` then keeps `sign >= 0` (gde.py:132, 
gde.py:135), so delta == 0 seems to be retained.
   
   Rust instead drops ties: `bits[i] = delta < 0.0` 
(datasketches/src/density/sketch.rs:339), so `delta == 0` is discarded.
   
   C++ seems to do the same as the Rust implementation.
   
   This could change retained points and density estimates for 
duplicate/symmetric data.



##########
datasketches/src/density/serialization.rs:
##########
@@ -0,0 +1,22 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+pub(super) const PREAMBLE_INTS_SHORT: u8 = 3;
+pub(super) const PREAMBLE_INTS_LONG: u8 = 6;
+pub(super) const SERIAL_VERSION: u8 = 1;
+pub(super) const DENSITY_FAMILY_ID: u8 = 19;
+pub(super) const FLAGS_IS_EMPTY: u8 = 1 << 2;

Review Comment:
   These can all be just `pub` since the `serialization` module is not exposed 
(e.g. `mod serialization` in `density/mod.rs`)



##########
datasketches/src/density/sketch.rs:
##########
@@ -0,0 +1,601 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::io::Write;
+
+use crate::codec::SketchBytes;
+use crate::codec::SketchSlice;
+use crate::common::RandomSource;
+use crate::common::XorShift64;
+use crate::density::serialization::DENSITY_FAMILY_ID;
+use crate::density::serialization::FLAGS_IS_EMPTY;
+use crate::density::serialization::PREAMBLE_INTS_LONG;
+use crate::density::serialization::PREAMBLE_INTS_SHORT;
+use crate::density::serialization::SERIAL_VERSION;
+use crate::error::Error;
+use crate::error::ErrorKind;
+
+type SerializeValue<T> = fn(&mut SketchBytes, T);
+type DeserializeValue<T> = fn(&mut SketchSlice<'_>) -> std::io::Result<T>;
+type Point<T> = Vec<T>;
+type Level<T> = Vec<Point<T>>;
+type Levels<T> = Vec<Level<T>>;
+
+/// Floating point types supported by the density sketch.
+pub trait DensityValue: Copy + PartialOrd + 'static {

Review Comment:
   Since this `DensityValue` is implemented for `f64` and `f32`, wouldn't it be 
better to use something like: 
https://docs.rs/num-traits/latest/num_traits/float/trait.Float.html
   
   This would allow you to call methods (e.g. `mul`) to operate on those types 
without needing to convert from and back `f64`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to