Skip to content

Commit

Permalink
Rename interpol fields to method
Browse files Browse the repository at this point in the history
  • Loading branch information
pomo-mondreganto committed Oct 14, 2024
1 parent 6d745bf commit eacf072
Show file tree
Hide file tree
Showing 32 changed files with 167 additions and 164 deletions.
2 changes: 1 addition & 1 deletion crates/polars-arrow/src/legacy/kernels/rolling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ pub struct RollingVarParams {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RollingQuantileParams {
pub prob: f64,
pub interpol: QuantileMethod,
pub method: QuantileMethod,
}
28 changes: 14 additions & 14 deletions crates/polars-arrow/src/legacy/kernels/rolling/no_nulls/quantile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::*;
pub struct QuantileWindow<'a, T: NativeType> {
sorted: SortedBuf<'a, T>,
prob: f64,
interpol: QuantileMethod,
method: QuantileMethod,
}

impl<
Expand All @@ -34,15 +34,15 @@ impl<
Self {
sorted: SortedBuf::new(slice, start, end),
prob: params.prob,
interpol: params.interpol,
method: params.method,
}
}

unsafe fn update(&mut self, start: usize, end: usize) -> Option<T> {
let vals = self.sorted.update(start, end);
let length = vals.len();

let idx = match self.interpol {
let idx = match self.method {
Linear => {
// Maybe add a fast path for median case? They could branch depending on odd/even.
let length_f = length as f64;
Expand Down Expand Up @@ -135,7 +135,7 @@ where
unreachable!("expected Quantile params");
};
let out = super::quantile_filter::rolling_quantile::<_, Vec<_>>(
params.interpol,
params.method,
min_periods,
window_size,
values,
Expand Down Expand Up @@ -171,7 +171,7 @@ where
Ok(rolling_apply_weighted_quantile(
values,
params.prob,
params.interpol,
params.method,
window_size,
min_periods,
offset_fn,
Expand All @@ -183,7 +183,7 @@ where
}

#[inline]
fn compute_wq<T>(buf: &[(T, f64)], p: f64, wsum: f64, interp: QuantileMethod) -> T
fn compute_wq<T>(buf: &[(T, f64)], p: f64, wsum: f64, method: QuantileMethod) -> T
where
T: Debug + NativeType + Mul<Output = T> + Sub<Output = T> + NumCast + ToPrimitive + Zero,
{
Expand All @@ -202,7 +202,7 @@ where
(s_old, v_old, vk) = (s, vk, v);
s += w;
}
match (h == s_old, interp) {
match (h == s_old, method) {
(true, _) => v_old, // If we hit the break exactly interpolation shouldn't matter
(_, Lower) => v_old,
(_, Higher) => vk,
Expand Down Expand Up @@ -233,7 +233,7 @@ where
fn rolling_apply_weighted_quantile<T, Fo>(
values: &[T],
p: f64,
interpolation: QuantileMethod,
method: QuantileMethod,
window_size: usize,
min_periods: usize,
det_offsets_fn: Fo,
Expand Down Expand Up @@ -261,7 +261,7 @@ where
.for_each(|(b, (i, w))| *b = (*values.get_unchecked(i + start), **w));
}
buf.sort_unstable_by(|&a, &b| a.0.tot_cmp(&b.0));
compute_wq(&buf, p, wsum, interpolation)
compute_wq(&buf, p, wsum, method)
})
.collect_trusted::<Vec<T>>();

Expand All @@ -282,7 +282,7 @@ mod test {
let values = &[1.0, 2.0, 3.0, 4.0];
let med_pars = Some(RollingFnParams::Quantile(RollingQuantileParams {
prob: 0.5,
interpol: Linear,
method: Linear,
}));
let out = rolling_quantile(values, 2, 2, false, None, med_pars.clone()).unwrap();
let out = out.as_any().downcast_ref::<PrimitiveArray<f64>>().unwrap();
Expand Down Expand Up @@ -314,7 +314,7 @@ mod test {
fn test_rolling_quantile_limits() {
let values = &[1.0f64, 2.0, 3.0, 4.0];

let interpol_options = vec![
let methods = vec![
QuantileMethod::Lower,
QuantileMethod::Higher,
QuantileMethod::Nearest,
Expand All @@ -323,10 +323,10 @@ mod test {
QuantileMethod::Equiprobable,
];

for interpol in interpol_options {
for method in methods {
let min_pars = Some(RollingFnParams::Quantile(RollingQuantileParams {
prob: 0.0,
interpol,
method,
}));
let out1 = rolling_min(values, 2, 2, false, None, None).unwrap();
let out1 = out1.as_any().downcast_ref::<PrimitiveArray<f64>>().unwrap();
Expand All @@ -338,7 +338,7 @@ mod test {

let max_pars = Some(RollingFnParams::Quantile(RollingQuantileParams {
prob: 1.0,
interpol,
method,
}));
let out1 = rolling_max(values, 2, 2, false, None, None).unwrap();
let out1 = out1.as_any().downcast_ref::<PrimitiveArray<f64>>().unwrap();
Expand Down
20 changes: 10 additions & 10 deletions crates/polars-arrow/src/legacy/kernels/rolling/nulls/quantile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::array::MutablePrimitiveArray;
pub struct QuantileWindow<'a, T: NativeType + IsFloat + PartialOrd> {
sorted: SortedBufNulls<'a, T>,
prob: f64,
interpol: QuantileMethod,
method: QuantileMethod,
}

impl<
Expand Down Expand Up @@ -39,7 +39,7 @@ impl<
Self {
sorted: SortedBufNulls::new(slice, validity, start, end),
prob: params.prob,
interpol: params.interpol,
method: params.method,
}
}

Expand All @@ -53,7 +53,7 @@ impl<
let values = &values[null_count..];
let length = values.len();

let mut idx = match self.interpol {
let mut idx = match self.method {
QuantileMethod::Nearest => ((length as f64) * self.prob) as usize,
QuantileMethod::Lower
| QuantileMethod::Midpoint
Expand All @@ -69,7 +69,7 @@ impl<
idx = std::cmp::min(idx, length - 1);

// we can unwrap because we sliced of the nulls
match self.interpol {
match self.method {
QuantileMethod::Midpoint => {
let top_idx = ((length as f64 - 1.0) * self.prob).ceil() as usize;
Some(
Expand Down Expand Up @@ -139,7 +139,7 @@ where
};

let out = super::quantile_filter::rolling_quantile::<_, MutablePrimitiveArray<_>>(
params.interpol,
params.method,
min_periods,
window_size,
arr.clone(),
Expand Down Expand Up @@ -174,7 +174,7 @@ mod test {
);
let med_pars = Some(RollingFnParams::Quantile(RollingQuantileParams {
prob: 0.5,
interpol: QuantileMethod::Linear,
method: QuantileMethod::Linear,
}));

let out = rolling_quantile(arr, 2, 2, false, None, med_pars.clone());
Expand Down Expand Up @@ -213,7 +213,7 @@ mod test {
Some(Bitmap::from(&[true, false, false, true, true])),
);

let interpol_options = vec![
let methods = vec![
QuantileMethod::Lower,
QuantileMethod::Higher,
QuantileMethod::Nearest,
Expand All @@ -222,10 +222,10 @@ mod test {
QuantileMethod::Equiprobable,
];

for interpol in interpol_options {
for method in methods {
let min_pars = Some(RollingFnParams::Quantile(RollingQuantileParams {
prob: 0.0,
interpol,
method,
}));
let out1 = rolling_min(values, 2, 1, false, None, None);
let out1 = out1.as_any().downcast_ref::<PrimitiveArray<f64>>().unwrap();
Expand All @@ -237,7 +237,7 @@ mod test {

let max_pars = Some(RollingFnParams::Quantile(RollingQuantileParams {
prob: 1.0,
interpol,
method,
}));
let out1 = rolling_max(values, 2, 1, false, None, None);
let out1 = out1.as_any().downcast_ref::<PrimitiveArray<f64>>().unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,20 +573,20 @@ struct QuantileUpdate<M: LenGet> {
inner: M,
quantile: f64,
min_periods: usize,
interpol: QuantileMethod,
method: QuantileMethod,
}

impl<M> QuantileUpdate<M>
where
M: LenGet,
<M as LenGet>::Item: Default + IsNull + Copy + FinishLinear + Debug,
{
fn new(interpol: QuantileMethod, min_periods: usize, quantile: f64, inner: M) -> Self {
fn new(method: QuantileMethod, min_periods: usize, quantile: f64, inner: M) -> Self {
Self {
min_periods,
quantile,
inner,
interpol,
method,
}
}

Expand All @@ -603,7 +603,7 @@ where
let valid_length_f = valid_length as f64;

use QuantileMethod::*;
match self.interpol {
match self.method {
Linear => {
let float_idx_top = (valid_length_f - 1.0) * self.quantile;
let idx = float_idx_top.floor() as usize;
Expand Down Expand Up @@ -655,7 +655,7 @@ where
}

pub(super) fn rolling_quantile<A, Out: Pushable<<A as Indexable>::Item>>(
interpol: QuantileMethod,
method: QuantileMethod,
min_periods: usize,
k: usize,
values: A,
Expand Down Expand Up @@ -713,7 +713,7 @@ where
// SAFETY: bounded by capacity
unsafe { block_left.undelete(i) };

let mut mu = QuantileUpdate::new(interpol, min_periods, quantile, &mut block_left);
let mut mu = QuantileUpdate::new(method, min_periods, quantile, &mut block_left);
out.push(mu.quantile());
}
for i in 1..n_blocks + 1 {
Expand Down Expand Up @@ -751,7 +751,7 @@ where
let mut union = BlockUnion::new(&mut *ptr_left, &mut *ptr_right);
union.set_state(j);
let q: <A as Indexable>::Item =
QuantileUpdate::new(interpol, min_periods, quantile, union).quantile();
QuantileUpdate::new(method, min_periods, quantile, union).quantile();
out.push(q);
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/polars-arrow/src/legacy/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::array::{BinaryArray, ListArray, Utf8Array};
pub use crate::legacy::array::default_arrays::*;
pub use crate::legacy::array::*;
pub use crate::legacy::index::*;
pub use crate::legacy::kernels::rolling::no_nulls::{QuantileMethod, QuantileInterpolOptions};
pub use crate::legacy::kernels::rolling::no_nulls::QuantileMethod;
pub use crate::legacy::kernels::rolling::{
RollingFnParams, RollingQuantileParams, RollingVarParams,
};
Expand All @@ -11,3 +11,6 @@ pub use crate::legacy::kernels::{Ambiguous, NonExistent};
pub type LargeStringArray = Utf8Array<i64>;
pub type LargeBinaryArray = BinaryArray<i64>;
pub type LargeListArray = ListArray<i64>;

#[allow(deprecated)]
pub use crate::legacy::kernels::rolling::no_nulls::QuantileInterpolOptions;
Loading

0 comments on commit eacf072

Please sign in to comment.