-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optionally order categorical with lexical ordering (#2726)
* lexical_cats * impl CategoricalTakeRandom * expose python
- Loading branch information
Showing
24 changed files
with
679 additions
and
224 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
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
2 changes: 2 additions & 0 deletions
2
polars/polars-core/src/chunked_array/logical/categorical/ops/mod.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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod append; | ||
mod full; | ||
mod take_random; | ||
mod unique; | ||
#[cfg(feature = "zip_with")] | ||
mod zip; | ||
|
||
use super::*; | ||
pub(crate) use take_random::{CategoricalTakeRandomGlobal, CategoricalTakeRandomLocal}; |
81 changes: 81 additions & 0 deletions
81
polars/polars-core/src/chunked_array/logical/categorical/ops/take_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,81 @@ | ||
use crate::prelude::compare_inner::PartialOrdInner; | ||
use crate::prelude::{ | ||
CategoricalChunked, IntoTakeRandom, NumTakeRandomChunked, NumTakeRandomCont, | ||
NumTakeRandomSingleChunk, PlHashMap, RevMapping, TakeRandBranch3, TakeRandom, | ||
}; | ||
use arrow::array::Utf8Array; | ||
use std::cmp::Ordering; | ||
|
||
type TakeCats<'a> = TakeRandBranch3< | ||
NumTakeRandomCont<'a, u32>, | ||
NumTakeRandomSingleChunk<'a, u32>, | ||
NumTakeRandomChunked<'a, u32>, | ||
>; | ||
|
||
pub(crate) struct CategoricalTakeRandomLocal<'a> { | ||
rev_map: &'a Utf8Array<i64>, | ||
cats: TakeCats<'a>, | ||
} | ||
|
||
impl<'a> CategoricalTakeRandomLocal<'a> { | ||
pub(crate) fn new(ca: &'a CategoricalChunked) -> Self { | ||
// should be rechunked upstream | ||
assert_eq!(ca.logical.chunks.len(), 1, "implementation error"); | ||
if let RevMapping::Local(rev_map) = &**ca.get_rev_map() { | ||
let cats = ca.logical().take_rand(); | ||
Self { rev_map, cats } | ||
} else { | ||
unreachable!() | ||
} | ||
} | ||
} | ||
|
||
impl PartialOrdInner for CategoricalTakeRandomLocal<'_> { | ||
unsafe fn cmp_element_unchecked(&self, idx_a: usize, idx_b: usize) -> Ordering { | ||
let a = self | ||
.cats | ||
.get_unchecked(idx_a) | ||
.map(|cat| self.rev_map.value_unchecked(cat as usize)); | ||
let b = self | ||
.cats | ||
.get_unchecked(idx_b) | ||
.map(|cat| self.rev_map.value_unchecked(cat as usize)); | ||
a.partial_cmp(&b).unwrap() | ||
} | ||
} | ||
|
||
pub(crate) struct CategoricalTakeRandomGlobal<'a> { | ||
rev_map_part_1: &'a PlHashMap<u32, u32>, | ||
rev_map_part_2: &'a Utf8Array<i64>, | ||
cats: TakeCats<'a>, | ||
} | ||
impl<'a> CategoricalTakeRandomGlobal<'a> { | ||
pub(crate) fn new(ca: &'a CategoricalChunked) -> Self { | ||
// should be rechunked upstream | ||
assert_eq!(ca.logical.chunks.len(), 1, "implementation error"); | ||
if let RevMapping::Global(rev_map_part_1, rev_map_part_2, _) = &**ca.get_rev_map() { | ||
let cats = ca.logical().take_rand(); | ||
Self { | ||
rev_map_part_1, | ||
rev_map_part_2, | ||
cats, | ||
} | ||
} else { | ||
unreachable!() | ||
} | ||
} | ||
} | ||
|
||
impl PartialOrdInner for CategoricalTakeRandomGlobal<'_> { | ||
unsafe fn cmp_element_unchecked(&self, idx_a: usize, idx_b: usize) -> Ordering { | ||
let a = self.cats.get_unchecked(idx_a).map(|cat| { | ||
let idx = self.rev_map_part_1.get(&cat).unwrap(); | ||
self.rev_map_part_2.value_unchecked(*idx as usize) | ||
}); | ||
let b = self.cats.get_unchecked(idx_b).map(|cat| { | ||
let idx = self.rev_map_part_1.get(&cat).unwrap(); | ||
self.rev_map_part_2.value_unchecked(*idx as usize) | ||
}); | ||
a.partial_cmp(&b).unwrap() | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
polars/polars-core/src/chunked_array/ops/sort/argsort_multiple.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,54 @@ | ||
use super::*; | ||
|
||
pub(crate) fn args_validate<T: PolarsDataType>( | ||
ca: &ChunkedArray<T>, | ||
other: &[Series], | ||
reverse: &[bool], | ||
) -> Result<()> { | ||
for s in other { | ||
assert_eq!(ca.len(), s.len()); | ||
} | ||
if other.len() != (reverse.len() - 1) { | ||
return Err(PolarsError::ValueError( | ||
format!( | ||
"The amount of ordering booleans: {} does not match that no. of Series: {}", | ||
reverse.len(), | ||
other.len() + 1 | ||
) | ||
.into(), | ||
)); | ||
} | ||
|
||
assert_eq!(other.len(), reverse.len() - 1); | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn argsort_multiple_impl<T: PartialOrd>( | ||
mut vals: Vec<(IdxSize, Option<T>)>, | ||
other: &[Series], | ||
reverse: &[bool], | ||
) -> Result<IdxCa> { | ||
let compare_inner: Vec<_> = other | ||
.iter() | ||
.map(|s| s.into_partial_ord_inner()) | ||
.collect_trusted(); | ||
|
||
vals.sort_by( | ||
|tpl_a, tpl_b| match (reverse[0], sort_with_nulls(&tpl_a.1, &tpl_b.1)) { | ||
// if ordering is equal, we check the other arrays until we find a non-equal ordering | ||
// if we have exhausted all arrays, we keep the equal ordering. | ||
(_, Ordering::Equal) => { | ||
let idx_a = tpl_a.0 as usize; | ||
let idx_b = tpl_b.0 as usize; | ||
ordering_other_columns(&compare_inner, &reverse[1..], idx_a, idx_b) | ||
} | ||
(true, Ordering::Less) => Ordering::Greater, | ||
(true, Ordering::Greater) => Ordering::Less, | ||
(_, ord) => ord, | ||
}, | ||
); | ||
let ca: NoNull<IdxCa> = vals.into_iter().map(|(idx, _v)| idx).collect_trusted(); | ||
let mut ca = ca.into_inner(); | ||
ca.set_sorted(reverse[0]); | ||
Ok(ca) | ||
} |
Oops, something went wrong.