Skip to content
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

MRG: in core, enable downsample within select #2931

Merged
merged 21 commits into from
Jan 23, 2024
Merged
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions src/core/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,14 @@ impl Select for Signature {
} else {
valid
};
// TODO: execute downsample if needed

// keep compatible scaled if applicable
if let Some(sel_scaled) = selection.scaled() {
valid = if let Sketch::MinHash(mh) = s {
valid && mh.scaled() <= sel_scaled as u64
} else {
valid
};
}
/*
valid = if let Some(abund) = selection.abund() {
valid && *s.with_abundance() == abund
Expand All @@ -785,8 +791,20 @@ impl Select for Signature {
valid
};
*/

valid
});

// downsample the retained sketches if needed.
if let Some(sel_scaled) = selection.scaled() {
for sketch in self.signatures.iter_mut() {
if let Sketch::MinHash(mh) = sketch {
if (mh.scaled() as u32) < sel_scaled {
*sketch = Sketch::MinHash(mh.downsample_scaled(sel_scaled as u64)?);
}
}
}
}
Ok(self)
}
}
Expand Down Expand Up @@ -841,6 +859,10 @@ mod test {

use super::Signature;

use crate::prelude::Select;
use crate::selection::Selection;
use crate::sketch::Sketch;

#[test]
fn load_sig() {
let mut filename = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand Down Expand Up @@ -979,4 +1001,47 @@ mod test {
assert_eq!(sk.size(), 500);
}
}

#[test]
fn selection_with_downsample() {
let mut filename = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
filename.push("../../tests/test-data/47+63-multisig.sig");

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");

// create Selection object
let mut selection = Selection::default();
selection.set_scaled(2000);
// iterate and check scaled
for sig in &sigs {
let modified_sig = sig.clone().select(&selection).unwrap();
for sketch in modified_sig.sketches() {
if let Sketch::MinHash(mh) = sketch {
eprintln!("scaled: {:?}", mh.scaled());
bluegenes marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(mh.scaled(), 2000);
}
}
}
}

#[test]
fn selection_scaled_too_low() {
let mut filename = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
filename.push("../../tests/test-data/47+63-multisig.sig");

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");

// create Selection object
let mut selection = Selection::default();
selection.set_scaled(100);
// iterate and check no sigs are returned (original scaled is 1000)
for sig in &sigs {
let modified_sig = sig.clone().select(&selection).unwrap();
assert_eq!(modified_sig.size(), 0);
}
}
}
Loading