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

fix(rust, python): std when ddof>=n_values returns None even in rolling context #11750

Merged
merged 21 commits into from
Mar 8, 2024

Conversation

MarcoGorelli
Copy link
Collaborator

@MarcoGorelli MarcoGorelli commented Oct 15, 2023

closes #11140

need to make the same change in the group_by_rolling(...).agg case as that's being tested against done

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Oct 15, 2023
@MarcoGorelli

This comment was marked as resolved.

@MarcoGorelli MarcoGorelli force-pushed the ddof-le-nvalues-rolling branch from 5c4dbbd to 39c6c7d Compare October 16, 2023 09:05
@MarcoGorelli MarcoGorelli changed the title fix(rust, python): single-element std with ddof=1 now returns None in rolling setting fix(rust, python): std when ddof>=n_values returns None even in rolling context Oct 16, 2023
Comment on lines -98 to +99
# "std", blocked by https://github.com/pola-rs/polars/issues/11140
# "var", blocked by https://github.com/pola-rs/polars/issues/11140
"std",
"var",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woohoo finally

@MarcoGorelli MarcoGorelli marked this pull request as ready for review October 16, 2023 09:38
let sum = self.sum.update(start, end);
sum / NumCast::from(end - start).unwrap()
unsafe fn update(&mut self, start: usize, end: usize) -> Option<T> {
let sum = self.sum.update(start, end).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can unwrap_unchecked this one as we know we always get value from sum.

@@ -47,21 +47,36 @@ where
let len = values.len();
let (start, end) = det_offsets_fn(0, window_size, len);
let mut agg_window = Agg::new(values, start, end, params);
let mut validity = match create_validity(min_periods, len, window_size, &det_offsets_fn) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use unwrap_or_else here? That elides the Some branch implicitly.


let out = (0..len)
.map(|idx| {
let (start, end) = det_offsets_fn(idx, window_size, len);
// safety:
// we are in bounds
unsafe { agg_window.update(start, end) }
let agg = unsafe { agg_window.update(start, end) };
match agg {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap_or_else

Ok(Box::new(PrimitiveArray::new(
T::PRIMITIVE.into(),
out.into(),
validity.map(|b| b.into()),
Some(validity.into()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like we always create a validity even if we don't have any nulls. I think we should branch on the case where create_validity

  1. returns a validity (e.g all null). In that case all values are null and we can directly return and create a Vec<T::zero> for the masked out values.

  2. doesn't return a validity. Then we can collect a PrimitiveArray from an iterator over Option<T>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the validity only not necessary if there are no nulls?

Because here, even if there were no nulls to begin with and even if min_periods was 0, agg_window.update(start, end) might introduce nulls (for example, if taking the std of a window whose length is smaller than ddof)

} else {
let proportion = T::from(float_idx - idx as f64).unwrap();
proportion * (vals[top_idx] - vals[idx]) + vals[idx]
Some(proportion * (vals[top_idx] - vals[idx]) + vals[idx])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make these get_unchecked_release?

@MarcoGorelli MarcoGorelli marked this pull request as draft October 22, 2023 14:33
@MarcoGorelli MarcoGorelli marked this pull request as ready for review December 30, 2023 14:14
@MarcoGorelli MarcoGorelli marked this pull request as draft December 30, 2023 16:53
@stinodego
Copy link
Member

@MarcoGorelli this PR is pretty old and still not marked as reviewable - do you need help finishing this or can it perhaps be closed?

@MarcoGorelli
Copy link
Collaborator Author

Thanks for the ping - will pick this up again, hopefully now I'll be able to address it (if not, will ask for help)

@MarcoGorelli MarcoGorelli marked this pull request as ready for review March 1, 2024 11:23
Copy link

codecov bot commented Mar 1, 2024

Codecov Report

Attention: Patch coverage is 75.38462% with 16 lines in your changes are missing coverage. Please review.

Project coverage is 81.01%. Comparing base (0a56aa4) to head (dceff5b).

Files Patch % Lines
...polars-core/src/frame/group_by/aggregations/mod.rs 38.46% 8 Missing ⚠️
...s-arrow/src/legacy/kernels/rolling/no_nulls/mod.rs 71.42% 4 Missing ⚠️
...ow/src/legacy/kernels/rolling/no_nulls/quantile.rs 50.00% 3 Missing ⚠️
...ow/src/legacy/kernels/rolling/no_nulls/variance.rs 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11750      +/-   ##
==========================================
- Coverage   81.02%   81.01%   -0.01%     
==========================================
  Files        1332     1332              
  Lines      172865   172879      +14     
  Branches     2458     2458              
==========================================
+ Hits       140059   140064       +5     
- Misses      32339    32347       +8     
- Partials      467      468       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MarcoGorelli MarcoGorelli requested a review from ritchie46 March 1, 2024 12:38
if let Some(validity) = create_validity(min_periods, len, window_size, &det_offsets_fn) {
if validity.iter().all(|x| !x) {
// all null!
return Ok(Box::new(PrimitiveArray::new(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use new_null constructor here. The bitmap now first allocates a vec full of false and then iterates that to construct a bitmap. That can be done in a single allocation.

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Marco. Great one.

@ritchie46 ritchie46 merged commit 6b23f79 into pola-rs:main Mar 8, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

single-element rolling_std returns 0 (rather than null)
3 participants