-
Notifications
You must be signed in to change notification settings - Fork 17
feat: impl densitysketch #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PsiACE
wants to merge
5
commits into
apache:main
Choose a base branch
from
PsiACE:density
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d209b3c
feat: imple densitysketch
PsiACE 10c8526
refactor: align density sketch kernel rng
PsiACE cdd821e
Merge upstream/main
PsiACE 45b1d2e
Merge remote-tracking branch 'origin/main' into density
PsiACE 698c143
refactor(density): polish code
PsiACE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // 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. | ||
|
|
||
| //! Shared random utilities for sketches. | ||
|
|
||
| use std::time::SystemTime; | ||
| use std::time::UNIX_EPOCH; | ||
|
|
||
| /// Random number source for sketches. | ||
| pub trait RandomSource { | ||
| /// Returns the next random 64-bit value. | ||
| fn next_u64(&mut self) -> u64; | ||
|
|
||
| /// Returns a random boolean value. | ||
| fn next_bool(&mut self) -> bool { | ||
| (self.next_u64() & 1) != 0 | ||
| } | ||
| } | ||
|
|
||
| /// Xorshift-based random generator for sketch operations. | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct XorShift64 { | ||
| state: u64, | ||
| } | ||
|
|
||
| impl XorShift64 { | ||
| /// Creates a new generator using the provided seed. | ||
| pub fn seeded(seed: u64) -> Self { | ||
| let state = if seed == 0 { 0x9e3779b97f4a7c15 } else { seed }; | ||
| Self { state } | ||
| } | ||
| } | ||
|
|
||
| impl Default for XorShift64 { | ||
| fn default() -> Self { | ||
| let nanos = SystemTime::now() | ||
| .duration_since(UNIX_EPOCH) | ||
| .unwrap_or_default() | ||
| .as_nanos(); | ||
| let mut seed = nanos as u64 ^ (std::process::id() as u64); | ||
| if seed == 0 { | ||
| seed = 0x9e3779b97f4a7c15; | ||
| } | ||
| Self::seeded(seed) | ||
| } | ||
| } | ||
|
|
||
| impl RandomSource for XorShift64 { | ||
| fn next_u64(&mut self) -> u64 { | ||
| let mut x = self.state; | ||
| x ^= x << 13; | ||
| x ^= x >> 7; | ||
| x ^= x << 17; | ||
| self.state = x; | ||
| x | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // 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. | ||
|
|
||
| //! Density sketch implementation for density estimation from streaming data. | ||
| //! | ||
| //! The sketch maintains a coreset of points using a compaction scheme and | ||
| //! provides density estimates at query points via a kernel function. | ||
| //! | ||
| //! # References | ||
| //! | ||
| //! - Zohar Karnin, Edo Liberty, "Discrepancy, Coresets, and Sketches in Machine Learning". | ||
| //! - Apache DataSketches C++ density sketch implementation (density_sketch.hpp). | ||
| //! | ||
| //! # Usage | ||
| //! | ||
| //! ```rust | ||
| //! # use datasketches::density::DensitySketch; | ||
| //! let mut sketch: DensitySketch<f64> = DensitySketch::new(10, 3); | ||
| //! sketch.update(vec![0.0, 0.0, 0.0]); | ||
| //! sketch.update(vec![1.0, 2.0, 3.0]); | ||
| //! let estimate = sketch.estimate(&[0.0, 0.0, 0.0]); | ||
| //! assert!(estimate > 0.0); | ||
| //! ``` | ||
|
|
||
| mod serialization; | ||
| mod sketch; | ||
|
|
||
| pub use self::sketch::DensityItem; | ||
| pub use self::sketch::DensityKernel; | ||
| pub use self::sketch::DensitySketch; | ||
| pub use self::sketch::DensityValue; | ||
| pub use self::sketch::GaussianKernel; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can all be just
pubsince theserializationmodule is not exposed (i.e.mod serializationindensity/mod.rs)