Skip to content

Commit

Permalink
💩 refactor: first pass of refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsPraet committed Sep 19, 2023
1 parent aa2f038 commit 6c178e1
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 215 deletions.
115 changes: 115 additions & 0 deletions downsample_rs/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use num_traits::AsPrimitive;
use polars::prelude::{ChunkAgg, ChunkAnyValue, ChunkedArray, Float64Type, TakeRandom};

use crate::types::Num;

Expand Down Expand Up @@ -31,3 +32,117 @@ where
self.iter().fold(0f64, |acc, &x| acc + x.as_()) as f64 / self.len() as f64
}
}

impl<T> Average for Vec<T>
where
T: Num + AsPrimitive<f64>,
{
fn average(&self) -> f64 {
self.iter().fold(0f64, |acc, &x| acc + x.as_()) as f64 / self.len() as f64
}
}

pub trait LttbParam {
type IterType: Iterator<Item = f64>;
type SliceType: LttbParam;

fn slice(&self, start_idx: usize, end_idx: usize) -> Self::SliceType;

fn get(&self, index: usize) -> f64;

fn into_iter(&self) -> Self::IterType;

fn len(&self) -> usize;

fn average(&self) -> f64;
}

impl<T> LttbParam for [T]
where
T: Num + AsPrimitive<f64>,
{
type IterType = std::vec::IntoIter<f64>;
type SliceType = std::vec::Vec<f64>;

fn slice(&self, start_idx: usize, end_idx: usize) -> Self::SliceType {
self[start_idx..end_idx].iter().map(|v| v.as_()).collect()
}

fn get(&self, index: usize) -> f64 {
self[index].as_()
}

fn into_iter(&self) -> Self::IterType {
IntoIterator::into_iter(self.iter().map(|v| v.as_()).collect::<Vec<f64>>())
}

fn len(&self) -> usize {
self.len()
}

fn average(&self) -> f64 {
Average::average(self)
}
}

impl<T> LttbParam for Vec<T>
where
T: Num + AsPrimitive<f64>,
{
type IterType = std::vec::IntoIter<f64>;
type SliceType = std::vec::Vec<f64>;

fn slice(&self, start_idx: usize, end_idx: usize) -> Self::SliceType {
self[start_idx..end_idx].iter().map(|v| v.as_()).collect()
}

fn get(&self, index: usize) -> f64 {
self[index].as_()
}

fn into_iter(&self) -> Self::IterType {
IntoIterator::into_iter(self.iter().map(|v| v.as_()).collect::<Vec<f64>>())
}

fn len(&self) -> usize {
self.len()
}

fn average(&self) -> f64 {
Average::average(self)
}
}

impl LttbParam for ChunkedArray<Float64Type> {
type IterType = std::vec::IntoIter<f64>;
type SliceType = ChunkedArray<Float64Type>;

fn slice(&self, start_idx: usize, end_idx: usize) -> Self::SliceType {
self.slice(start_idx as i64, end_idx - start_idx)
}

fn get(&self, index: usize) -> f64 {
match self
.get_any_value(index)
.unwrap()
.cast(&polars::prelude::DataType::Float64)
.unwrap()
{
polars::prelude::AnyValue::Float64(x) => x,
_ => panic!(""), // this can never be reached, as it should have panicked when casting
}
}

fn into_iter(&self) -> Self::IterType {
// TODO: fix this so we don't do any needless copying
self.into_no_null_iter().collect::<Vec<f64>>().into_iter()
}

fn len(&self) -> usize {
self.len()
}

fn average(&self) -> f64 {
self.mean().unwrap_or(0.0)
}
}
Loading

0 comments on commit 6c178e1

Please sign in to comment.