-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rust, python): increment seed between samples (#9694)
- Loading branch information
Showing
13 changed files
with
293 additions
and
140 deletions.
There are no files selected for viewing
This file contains 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
55 changes: 55 additions & 0 deletions
55
polars/polars-lazy/polars-plan/src/dsl/function_expr/random.rs
This file contains 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,55 @@ | ||
use std::sync::atomic::Ordering; | ||
|
||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
use strum_macros::IntoStaticStr; | ||
|
||
use super::*; | ||
|
||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[derive(Copy, Clone, PartialEq, Debug, IntoStaticStr)] | ||
#[strum(serialize_all = "snake_case")] | ||
pub enum RandomMethod { | ||
Shuffle, | ||
SampleN { | ||
n: usize, | ||
with_replacement: bool, | ||
shuffle: bool, | ||
}, | ||
SampleFrac { | ||
frac: f64, | ||
with_replacement: bool, | ||
shuffle: bool, | ||
}, | ||
} | ||
|
||
pub(super) fn random( | ||
s: &Series, | ||
method: RandomMethod, | ||
atomic_seed: Option<&Arc<AtomicU64>>, | ||
seed: Option<u64>, | ||
fixed_seed: bool, | ||
) -> PolarsResult<Series> { | ||
let seed = if fixed_seed { | ||
seed | ||
} else { | ||
// ensure seeds differ between groupby groups | ||
// otherwise all groups would be sampled the same | ||
atomic_seed | ||
.as_ref() | ||
.map(|atomic| atomic.fetch_add(1, Ordering::Relaxed)) | ||
}; | ||
match method { | ||
RandomMethod::Shuffle => Ok(s.shuffle(seed)), | ||
RandomMethod::SampleFrac { | ||
frac, | ||
with_replacement, | ||
shuffle, | ||
} => s.sample_frac(frac, with_replacement, shuffle, seed), | ||
RandomMethod::SampleN { | ||
n, | ||
with_replacement, | ||
shuffle, | ||
} => s.sample_n(n, with_replacement, shuffle, seed), | ||
} | ||
} |
This file contains 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 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 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,58 @@ | ||
use std::sync::atomic::AtomicU64; | ||
|
||
use super::*; | ||
|
||
fn get_atomic_seed(seed: Option<u64>) -> Option<SpecialEq<Arc<AtomicU64>>> { | ||
seed.map(|v| SpecialEq::new(Arc::new(AtomicU64::new(v)))) | ||
} | ||
|
||
impl Expr { | ||
pub fn shuffle(self, seed: Option<u64>, fixed_seed: bool) -> Self { | ||
self.apply_private(FunctionExpr::Random { | ||
method: RandomMethod::Shuffle, | ||
atomic_seed: get_atomic_seed(seed), | ||
seed, | ||
fixed_seed, | ||
}) | ||
} | ||
|
||
pub fn sample_n( | ||
self, | ||
n: usize, | ||
with_replacement: bool, | ||
shuffle: bool, | ||
seed: Option<u64>, | ||
fixed_seed: bool, | ||
) -> Self { | ||
self.apply_private(FunctionExpr::Random { | ||
method: RandomMethod::SampleN { | ||
n, | ||
with_replacement, | ||
shuffle, | ||
}, | ||
atomic_seed: get_atomic_seed(seed), | ||
seed, | ||
fixed_seed, | ||
}) | ||
} | ||
|
||
pub fn sample_frac( | ||
self, | ||
frac: f64, | ||
with_replacement: bool, | ||
shuffle: bool, | ||
seed: Option<u64>, | ||
fixed_seed: bool, | ||
) -> Self { | ||
self.apply_private(FunctionExpr::Random { | ||
method: RandomMethod::SampleFrac { | ||
frac, | ||
with_replacement, | ||
shuffle, | ||
}, | ||
atomic_seed: get_atomic_seed(seed), | ||
seed, | ||
fixed_seed, | ||
}) | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.