Merge pull request #45 from Magnus167/frame_ops_enhance

Enhance element-wise and bitwise operations for Frame<T> and Frame<bool>
This commit is contained in:
Palash Tyagi 2025-06-07 12:27:56 +01:00 committed by GitHub
commit 092aebd5fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 159 additions and 26 deletions

View File

@ -781,14 +781,13 @@ impl<T: Clone + PartialEq> IndexMut<&str> for Frame<T> {
/// Panics if column labels or row indices differ between operands. /// Panics if column labels or row indices differ between operands.
macro_rules! impl_elementwise_frame_op { macro_rules! impl_elementwise_frame_op {
($OpTrait:ident, $method:ident) => { ($OpTrait:ident, $method:ident) => {
// &Frame<T> $OpTrait &Frame<T>
impl<'a, 'b, T> std::ops::$OpTrait<&'b Frame<T>> for &'a Frame<T> impl<'a, 'b, T> std::ops::$OpTrait<&'b Frame<T>> for &'a Frame<T>
where where
T: Clone + PartialEq + std::ops::$OpTrait<Output = T>, T: Clone + PartialEq + std::ops::$OpTrait<Output = T>,
{ {
type Output = Frame<T>; type Output = Frame<T>;
fn $method(self, rhs: &'b Frame<T>) -> Frame<T> { fn $method(self, rhs: &'b Frame<T>) -> Frame<T> {
// Verify matching schema
if self.column_names != rhs.column_names { if self.column_names != rhs.column_names {
panic!( panic!(
"Element-wise {}: column names do not match. Left: {:?}, Right: {:?}", "Element-wise {}: column names do not match. Left: {:?}, Right: {:?}",
@ -805,21 +804,47 @@ macro_rules! impl_elementwise_frame_op {
rhs.index rhs.index
); );
} }
// Apply the matrix operation
let result_matrix = (&self.matrix).$method(&rhs.matrix); let result_matrix = (&self.matrix).$method(&rhs.matrix);
// Determine index for the result
let new_index = match self.index { let new_index = match self.index {
RowIndex::Range(_) => None, RowIndex::Range(_) => None,
_ => Some(self.index.clone()), _ => Some(self.index.clone()),
}; };
Frame::new(result_matrix, self.column_names.clone(), new_index) Frame::new(result_matrix, self.column_names.clone(), new_index)
} }
} }
// Frame<T> $OpTrait &Frame<T>
impl<'b, T> std::ops::$OpTrait<&'b Frame<T>> for Frame<T>
where
T: Clone + PartialEq + std::ops::$OpTrait<Output = T>,
{
type Output = Frame<T>;
fn $method(self, rhs: &'b Frame<T>) -> Frame<T> {
(&self).$method(rhs)
}
}
// &Frame<T> $OpTrait Frame<T>
impl<'a, T> std::ops::$OpTrait<Frame<T>> for &'a Frame<T>
where
T: Clone + PartialEq + std::ops::$OpTrait<Output = T>,
{
type Output = Frame<T>;
fn $method(self, rhs: Frame<T>) -> Frame<T> {
self.$method(&rhs)
}
}
// Frame<T> $OpTrait Frame<T>
impl<T> std::ops::$OpTrait<Frame<T>> for Frame<T>
where
T: Clone + PartialEq + std::ops::$OpTrait<Output = T>,
{
type Output = Frame<T>;
fn $method(self, rhs: Frame<T>) -> Frame<T> {
(&self).$method(&rhs)
}
}
}; };
} }
impl_elementwise_frame_op!(Add, add); impl_elementwise_frame_op!(Add, add);
impl_elementwise_frame_op!(Sub, sub); impl_elementwise_frame_op!(Sub, sub);
impl_elementwise_frame_op!(Mul, mul); impl_elementwise_frame_op!(Mul, mul);
@ -830,11 +855,10 @@ impl_elementwise_frame_op!(Div, div);
/// Panics if column labels or row indices differ between operands. /// Panics if column labels or row indices differ between operands.
macro_rules! impl_bitwise_frame_op { macro_rules! impl_bitwise_frame_op {
($OpTrait:ident, $method:ident) => { ($OpTrait:ident, $method:ident) => {
// &Frame<bool> $OpTrait &Frame<bool>
impl<'a, 'b> std::ops::$OpTrait<&'b Frame<bool>> for &'a Frame<bool> { impl<'a, 'b> std::ops::$OpTrait<&'b Frame<bool>> for &'a Frame<bool> {
type Output = Frame<bool>; type Output = Frame<bool>;
fn $method(self, rhs: &'b Frame<bool>) -> Frame<bool> { fn $method(self, rhs: &'b Frame<bool>) -> Frame<bool> {
// Verify matching schema
if self.column_names != rhs.column_names { if self.column_names != rhs.column_names {
panic!( panic!(
"Bitwise {}: column names do not match. Left: {:?}, Right: {:?}", "Bitwise {}: column names do not match. Left: {:?}, Right: {:?}",
@ -851,25 +875,43 @@ macro_rules! impl_bitwise_frame_op {
rhs.index rhs.index
); );
} }
// Apply the matrix operation
let result_matrix = (&self.matrix).$method(&rhs.matrix); let result_matrix = (&self.matrix).$method(&rhs.matrix);
// Determine index for the result
let new_index = match self.index { let new_index = match self.index {
RowIndex::Range(_) => None, RowIndex::Range(_) => None,
_ => Some(self.index.clone()), _ => Some(self.index.clone()),
}; };
Frame::new(result_matrix, self.column_names.clone(), new_index) Frame::new(result_matrix, self.column_names.clone(), new_index)
} }
} }
// Frame<bool> $OpTrait &Frame<bool>
impl<'b> std::ops::$OpTrait<&'b Frame<bool>> for Frame<bool> {
type Output = Frame<bool>;
fn $method(self, rhs: &'b Frame<bool>) -> Frame<bool> {
(&self).$method(rhs)
}
}
// &Frame<bool> $OpTrait Frame<bool>
impl<'a> std::ops::$OpTrait<Frame<bool>> for &'a Frame<bool> {
type Output = Frame<bool>;
fn $method(self, rhs: Frame<bool>) -> Frame<bool> {
self.$method(&rhs)
}
}
// Frame<bool> $OpTrait Frame<bool>
impl std::ops::$OpTrait<Frame<bool>> for Frame<bool> {
type Output = Frame<bool>;
fn $method(self, rhs: Frame<bool>) -> Frame<bool> {
(&self).$method(&rhs)
}
}
}; };
} }
impl_bitwise_frame_op!(BitAnd, bitand); impl_bitwise_frame_op!(BitAnd, bitand);
impl_bitwise_frame_op!(BitOr, bitor); impl_bitwise_frame_op!(BitOr, bitor);
impl_bitwise_frame_op!(BitXor, bitxor); impl_bitwise_frame_op!(BitXor, bitxor);
/* ---------- Logical NOT ---------- */
/// Implements logical NOT (`!`) for `Frame<bool>`, consuming the frame. /// Implements logical NOT (`!`) for `Frame<bool>`, consuming the frame.
impl Not for Frame<bool> { impl Not for Frame<bool> {
type Output = Frame<bool>; type Output = Frame<bool>;
@ -888,12 +930,30 @@ impl Not for Frame<bool> {
} }
} }
/// Implements logical NOT (`!`) for `&Frame<bool>`, borrowing the frame.
impl Not for &Frame<bool> {
type Output = Frame<bool>;
fn not(self) -> Frame<bool> {
// Apply NOT to the underlying matrix
let result_matrix = !&self.matrix;
// Determine index for the result
let new_index = match self.index {
RowIndex::Range(_) => None,
_ => Some(self.index.clone()),
};
Frame::new(result_matrix, self.column_names.clone(), new_index)
}
}
// --- Tests --- // --- Tests ---
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
// Assume Matrix is available from crate::matrix or similar // Assume Matrix is available from crate::matrix or similar
use crate::matrix::Matrix; use crate::matrix::{BoolOps, Matrix};
use chrono::NaiveDate; use chrono::NaiveDate;
// HashMap needed for direct inspection in tests if required // HashMap needed for direct inspection in tests if required
use std::collections::HashMap; use std::collections::HashMap;
@ -1349,7 +1409,7 @@ mod tests {
fn test_row_view_name_panic() { fn test_row_view_name_panic() {
let frame = create_test_frame_f64(); let frame = create_test_frame_f64();
let row_view = frame.get_row(0); let row_view = frame.get_row(0);
let _ = row_view["C"]; // Access non-existent column name let _ = row_view["C"]; // Access non-existent column Z
} }
#[test] #[test]
#[should_panic(expected = "column index 3 out of bounds")] // Check specific message #[should_panic(expected = "column index 3 out of bounds")] // Check specific message
@ -1666,6 +1726,79 @@ mod tests {
assert_eq!(frame_div["Y"], vec![10, -10]); assert_eq!(frame_div["Y"], vec![10, -10]);
} }
#[test]
fn tests_for_frame_arithmetic_ops() {
let ops: Vec<(
&str,
fn(&Frame<f64>, &Frame<f64>) -> Frame<f64>,
fn(&Frame<f64>, &Frame<f64>) -> Frame<f64>,
)> = vec![
("addition", |a, b| a + b, |a, b| (&*a) + (&*b)),
("subtraction", |a, b| a - b, |a, b| (&*a) - (&*b)),
("multiplication", |a, b| a * b, |a, b| (&*a) * (&*b)),
("division", |a, b| a / b, |a, b| (&*a) / (&*b)),
];
for (op_name, owned_op, ref_op) in ops {
let f1 = create_test_frame_f64();
let f2 = create_test_frame_f64_alt();
let result_owned = owned_op(&f1, &f2);
let expected = ref_op(&f1, &f2);
assert_eq!(
result_owned.columns(),
f1.columns(),
"Column mismatch for {}",
op_name
);
assert_eq!(
result_owned.index(),
f1.index(),
"Index mismatch for {}",
op_name
);
let bool_mat = result_owned.matrix().eq_elem(expected.matrix().clone());
assert!(bool_mat.all(), "Element-wise {} failed", op_name);
}
}
// test not , and or on frame
#[test]
fn tests_for_frame_bool_ops() {
let ops: Vec<(
&str,
fn(&Frame<bool>, &Frame<bool>) -> Frame<bool>,
fn(&Frame<bool>, &Frame<bool>) -> Frame<bool>,
)> = vec![
("and", |a, b| a & b, |a, b| (&*a) & (&*b)),
("or", |a, b| a | b, |a, b| (&*a) | (&*b)),
("xor", |a, b| a ^ b, |a, b| (&*a) ^ (&*b)),
];
for (op_name, owned_op, ref_op) in ops {
let f1 = create_test_frame_bool();
let f2 = create_test_frame_bool_alt();
let result_owned = owned_op(&f1, &f2);
let expected = ref_op(&f1, &f2);
assert_eq!(
result_owned.columns(),
f1.columns(),
"Column mismatch for {}",
op_name
);
assert_eq!(
result_owned.index(),
f1.index(),
"Index mismatch for {}",
op_name
);
let bool_mat = result_owned.matrix().eq_elem(expected.matrix().clone());
assert!(bool_mat.all(), "Element-wise {} failed", op_name);
}
}
#[test] #[test]
fn test_frame_arithmetic_ops_date_index() { fn test_frame_arithmetic_ops_date_index() {
let dates = vec![d(2024, 1, 1), d(2024, 1, 2)]; let dates = vec![d(2024, 1, 1), d(2024, 1, 2)];

View File

@ -40,7 +40,7 @@ pub struct BDatesList {
/// ```rust /// ```rust
/// use chrono::NaiveDate; /// use chrono::NaiveDate;
/// use std::error::Error; /// use std::error::Error;
/// use rustframe::utils::{BDatesList, DateFreq}; // Replace bdates with your actual crate/module name /// use rustframe::utils::{BDatesList, DateFreq};
/// ///
/// fn main() -> Result<(), Box<dyn Error>> { /// fn main() -> Result<(), Box<dyn Error>> {
/// let start_date = "2023-11-01".to_string(); // Wednesday /// let start_date = "2023-11-01".to_string(); // Wednesday
@ -68,7 +68,7 @@ pub struct BDatesList {
/// ```rust /// ```rust
/// use chrono::NaiveDate; /// use chrono::NaiveDate;
/// use std::error::Error; /// use std::error::Error;
/// use rustframe::utils::{BDatesList, DateFreq}; // Replace bdates with your actual crate/module name /// use rustframe::utils::{BDatesList, DateFreq};
/// ///
/// fn main() -> Result<(), Box<dyn Error>> { /// fn main() -> Result<(), Box<dyn Error>> {
/// let start_date = "2024-02-28".to_string(); // Wednesday /// let start_date = "2024-02-28".to_string(); // Wednesday
@ -98,7 +98,7 @@ pub struct BDatesList {
/// ```rust /// ```rust
/// use chrono::NaiveDate; /// use chrono::NaiveDate;
/// use std::error::Error; /// use std::error::Error;
/// use rustframe::utils::{BDatesList, DateFreq}; // Replace bdates with your actual crate/module name /// use rustframe::utils::{BDatesList, DateFreq};
/// ///
/// fn main() -> Result<(), Box<dyn Error>> { /// fn main() -> Result<(), Box<dyn Error>> {
/// let start_date = "2023-11-20".to_string(); // Mon, Week 47 /// let start_date = "2023-11-20".to_string(); // Mon, Week 47
@ -294,7 +294,7 @@ impl BDatesList {
/// ```rust /// ```rust
/// use chrono::NaiveDate; /// use chrono::NaiveDate;
/// use std::error::Error; /// use std::error::Error;
/// use rustframe::utils::{BDatesGenerator, DateFreq}; // Replace bdates with your actual crate/module name /// use rustframe::utils::{BDatesGenerator, DateFreq};
/// ///
/// fn main() -> Result<(), Box<dyn Error>> { /// fn main() -> Result<(), Box<dyn Error>> {
/// let start = NaiveDate::from_ymd_opt(2024, 4, 29).unwrap(); // Monday /// let start = NaiveDate::from_ymd_opt(2024, 4, 29).unwrap(); // Monday

View File

@ -160,7 +160,7 @@ enum GroupKey {
/// ```rust /// ```rust
/// use chrono::NaiveDate; /// use chrono::NaiveDate;
/// use std::error::Error; /// use std::error::Error;
/// # use rustframe::utils::{DatesList, DateFreq}; // Assuming the crate/module is named 'dates' /// use rustframe::utils::{DatesList, DateFreq};
/// ///
/// # fn main() -> Result<(), Box<dyn Error>> { /// # fn main() -> Result<(), Box<dyn Error>> {
/// let start_date = "2023-11-01".to_string(); // Wednesday /// let start_date = "2023-11-01".to_string(); // Wednesday