mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-20 07:20:01 +00:00
Compare commits
33 Commits
d8afef049d
...
bcca7406f7
Author | SHA1 | Date | |
---|---|---|---|
![]() |
bcca7406f7 | ||
d851c500af | |||
![]() |
d741c7f472 | ||
![]() |
7720312354 | ||
![]() |
5509416d5f | ||
![]() |
a451ba8cc7 | ||
39a95e63d9 | |||
1de8ba4f2d | |||
74bec4b69e | |||
58b38311b5 | |||
4ed23069fc | |||
![]() |
7d7794627b | ||
d9bdf8ee96 | |||
a61ff8a4e1 | |||
![]() |
26ee580710 | ||
![]() |
96934cd89f | ||
![]() |
27ab1ac129 | ||
![]() |
eb4fefe363 | ||
![]() |
60cc97e702 | ||
![]() |
7e2a5ec18d | ||
![]() |
4038d25b07 | ||
![]() |
aa15248b58 | ||
![]() |
fa392ec631 | ||
![]() |
8b6f16236a | ||
![]() |
58acea8467 | ||
![]() |
2607d9c3b0 | ||
![]() |
57ed06f79b | ||
![]() |
01a132264f | ||
![]() |
ff4535c56b | ||
9b480e8130 | |||
![]() |
fe666a4ddb | ||
![]() |
b80d5ab381 | ||
![]() |
49f7558225 |
149
README.md
149
README.md
@ -1,9 +1,5 @@
|
|||||||
# rustframe
|
# rustframe
|
||||||
|
|
||||||
<!-- # <img align="center" alt="Rustframe" src=".github/rustframe_logo.png" height="50px" /> rustframe -->
|
|
||||||
|
|
||||||
<!-- though the centre tag doesn't work as it would normally, it achieves the desired effect -->
|
|
||||||
|
|
||||||
📚 [Docs](https://magnus167.github.io/rustframe/) | 🐙 [GitHub](https://github.com/Magnus167/rustframe) | 🌐 [Gitea mirror](https://gitea.nulltech.uk/Magnus167/rustframe) | 🦀 [Crates.io](https://crates.io/crates/rustframe) | 🔖 [docs.rs](https://docs.rs/rustframe/latest/rustframe/)
|
📚 [Docs](https://magnus167.github.io/rustframe/) | 🐙 [GitHub](https://github.com/Magnus167/rustframe) | 🌐 [Gitea mirror](https://gitea.nulltech.uk/Magnus167/rustframe) | 🦀 [Crates.io](https://crates.io/crates/rustframe) | 🔖 [docs.rs](https://docs.rs/rustframe/latest/rustframe/)
|
||||||
|
|
||||||
<!-- [](https://github.com/Magnus167/rustframe) -->
|
<!-- [](https://github.com/Magnus167/rustframe) -->
|
||||||
@ -131,10 +127,6 @@ let mc: Matrix<f64> = Matrix::from_cols(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
|
|||||||
let md: Matrix<f64> = Matrix::from_cols(vec![vec![5.0, 6.0], vec![7.0, 8.0]]);
|
let md: Matrix<f64> = Matrix::from_cols(vec![vec![5.0, 6.0], vec![7.0, 8.0]]);
|
||||||
let mul_result: Matrix<f64> = mc.matrix_mul(&md);
|
let mul_result: Matrix<f64> = mc.matrix_mul(&md);
|
||||||
// Expected:
|
// Expected:
|
||||||
// 1*5 + 3*6 = 5 + 18 = 23
|
|
||||||
// 2*5 + 4*6 = 10 + 24 = 34
|
|
||||||
// 1*7 + 3*8 = 7 + 24 = 31
|
|
||||||
// 2*7 + 4*8 = 14 + 32 = 46
|
|
||||||
assert_eq!(mul_result.data(), &[23.0, 34.0, 31.0, 46.0]);
|
assert_eq!(mul_result.data(), &[23.0, 34.0, 31.0, 46.0]);
|
||||||
|
|
||||||
// Dot product (alias for matrix_mul for FloatMatrix)
|
// Dot product (alias for matrix_mul for FloatMatrix)
|
||||||
@ -143,14 +135,7 @@ assert_eq!(dot_result, mul_result);
|
|||||||
|
|
||||||
// Transpose
|
// Transpose
|
||||||
let original_matrix: Matrix<f64> = Matrix::from_cols(vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]]);
|
let original_matrix: Matrix<f64> = Matrix::from_cols(vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]]);
|
||||||
// Original:
|
|
||||||
// 1 4
|
|
||||||
// 2 5
|
|
||||||
// 3 6
|
|
||||||
let transposed_matrix: Matrix<f64> = original_matrix.transpose();
|
let transposed_matrix: Matrix<f64> = original_matrix.transpose();
|
||||||
// Transposed:
|
|
||||||
// 1 2 3
|
|
||||||
// 4 5 6
|
|
||||||
assert_eq!(transposed_matrix.rows(), 2);
|
assert_eq!(transposed_matrix.rows(), 2);
|
||||||
assert_eq!(transposed_matrix.cols(), 3);
|
assert_eq!(transposed_matrix.cols(), 3);
|
||||||
assert_eq!(transposed_matrix.data(), &[1.0, 4.0, 2.0, 5.0, 3.0, 6.0]);
|
assert_eq!(transposed_matrix.data(), &[1.0, 4.0, 2.0, 5.0, 3.0, 6.0]);
|
||||||
@ -159,10 +144,6 @@ assert_eq!(transposed_matrix.data(), &[1.0, 4.0, 2.0, 5.0, 3.0, 6.0]);
|
|||||||
let matrix = Matrix::from_cols(vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]]);
|
let matrix = Matrix::from_cols(vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]]);
|
||||||
// Map function to double each value
|
// Map function to double each value
|
||||||
let mapped_matrix = matrix.map(|x| x * 2.0);
|
let mapped_matrix = matrix.map(|x| x * 2.0);
|
||||||
// Expected data after mapping
|
|
||||||
// 2 8
|
|
||||||
// 4 10
|
|
||||||
// 6 12
|
|
||||||
assert_eq!(mapped_matrix.data(), &[2.0, 4.0, 6.0, 8.0, 10.0, 12.0]);
|
assert_eq!(mapped_matrix.data(), &[2.0, 4.0, 6.0, 8.0, 10.0, 12.0]);
|
||||||
|
|
||||||
// Zip
|
// Zip
|
||||||
@ -170,12 +151,136 @@ let a = Matrix::from_cols(vec![vec![1.0, 2.0], vec![3.0, 4.0]]); // 2x2 matrix
|
|||||||
let b = Matrix::from_cols(vec![vec![5.0, 6.0], vec![7.0, 8.0]]); // 2x2 matrix
|
let b = Matrix::from_cols(vec![vec![5.0, 6.0], vec![7.0, 8.0]]); // 2x2 matrix
|
||||||
// Zip function to add corresponding elements
|
// Zip function to add corresponding elements
|
||||||
let zipped_matrix = a.zip(&b, |x, y| x + y);
|
let zipped_matrix = a.zip(&b, |x, y| x + y);
|
||||||
// Expected data after zipping
|
|
||||||
// 6 10
|
|
||||||
// 8 12
|
|
||||||
assert_eq!(zipped_matrix.data(), &[6.0, 8.0, 10.0, 12.0]);
|
assert_eq!(zipped_matrix.data(), &[6.0, 8.0, 10.0, 12.0]);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## DataFrame Usage Example
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use chrono::NaiveDate;
|
||||||
|
use rustframe::dataframe::DataFrame;
|
||||||
|
use rustframe::utils::{BDateFreq, BDatesList};
|
||||||
|
use std::any::TypeId;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
// Helper for NaiveDate
|
||||||
|
fn d(y: i32, m: u32, d: u32) -> NaiveDate {
|
||||||
|
NaiveDate::from_ymd_opt(y, m, d).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new DataFrame
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
|
||||||
|
// Add columns of different types
|
||||||
|
df.add_column("col_int1", vec![1, 2, 3, 4, 5]);
|
||||||
|
df.add_column("col_float1", vec![1.1, 2.2, 3.3, 4.4, 5.5]);
|
||||||
|
df.add_column(
|
||||||
|
"col_string",
|
||||||
|
vec![
|
||||||
|
"apple".to_string(),
|
||||||
|
"banana".to_string(),
|
||||||
|
"cherry".to_string(),
|
||||||
|
"date".to_string(),
|
||||||
|
"elderberry".to_string(),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
df.add_column("col_bool", vec![true, false, true, false, true]);
|
||||||
|
// df.add_column("col_date", vec![d(2023,1,1), d(2023,1,2), d(2023,1,3), d(2023,1,4), d(2023,1,5)]);
|
||||||
|
df.add_column(
|
||||||
|
"col_date",
|
||||||
|
BDatesList::from_n_periods("2023-01-01".to_string(), BDateFreq::Daily, 5)
|
||||||
|
.unwrap()
|
||||||
|
.list()
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
println!("DataFrame after initial column additions:\n{}", df);
|
||||||
|
|
||||||
|
// Demonstrate frame re-use when adding columns of existing types
|
||||||
|
let initial_frames_count = df.num_internal_frames();
|
||||||
|
println!(
|
||||||
|
"\nInitial number of internal frames: {}",
|
||||||
|
initial_frames_count
|
||||||
|
);
|
||||||
|
|
||||||
|
df.add_column("col_int2", vec![6, 7, 8, 9, 10]);
|
||||||
|
df.add_column("col_float2", vec![6.6, 7.7, 8.8, 9.9, 10.0]);
|
||||||
|
|
||||||
|
let frames_after_reuse = df.num_internal_frames();
|
||||||
|
println!(
|
||||||
|
"Number of internal frames after adding more columns of existing types: {}",
|
||||||
|
frames_after_reuse
|
||||||
|
);
|
||||||
|
assert_eq!(initial_frames_count, frames_after_reuse); // Should be equal, demonstrating re-use
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"\nDataFrame after adding more columns of existing types:\n{}",
|
||||||
|
df
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get number of rows and columns
|
||||||
|
println!("Rows: {}", df.rows()); // Output: Rows: 5
|
||||||
|
println!("Columns: {}", df.cols()); // Output: Columns: 5
|
||||||
|
|
||||||
|
// Get column names
|
||||||
|
println!("Column names: {:?}", df.get_column_names());
|
||||||
|
// Output: Column names: ["col_int", "col_float", "col_string", "col_bool", "col_date"]
|
||||||
|
|
||||||
|
// Get a specific column by name and type
|
||||||
|
let int_col = df.get_column::<i32>("col_int1").unwrap();
|
||||||
|
// Output: Integer column: [1, 2, 3, 4, 5]
|
||||||
|
println!("Integer column (col_int1): {:?}", int_col);
|
||||||
|
|
||||||
|
let int_col2 = df.get_column::<i32>("col_int2").unwrap();
|
||||||
|
// Output: Integer column: [6, 7, 8, 9, 10]
|
||||||
|
println!("Integer column (col_int2): {:?}", int_col2);
|
||||||
|
|
||||||
|
let float_col = df.get_column::<f64>("col_float1").unwrap();
|
||||||
|
// Output: Float column: [1.1, 2.2, 3.3, 4.4, 5.5]
|
||||||
|
println!("Float column (col_float1): {:?}", float_col);
|
||||||
|
|
||||||
|
// Attempt to get a column with incorrect type (returns None)
|
||||||
|
let wrong_type_col = df.get_column::<bool>("col_int1");
|
||||||
|
// Output: Wrong type column: None
|
||||||
|
println!("Wrong type column: {:?}", wrong_type_col);
|
||||||
|
|
||||||
|
// Get a row by index
|
||||||
|
let row_0 = df.get_row(0).unwrap();
|
||||||
|
println!("Row 0: {:?}", row_0);
|
||||||
|
// Output: Row 0: {"col_int1": "1", "col_float1": "1.1", "col_string": "apple", "col_bool": "true", "col_date": "2023-01-01", "col_int2": "6", "col_float2": "6.6"}
|
||||||
|
|
||||||
|
let row_2 = df.get_row(2).unwrap();
|
||||||
|
println!("Row 2: {:?}", row_2);
|
||||||
|
// Output: Row 2: {"col_int1": "3", "col_float1": "3.3", "col_string": "cherry", "col_bool": "true", "col_date": "2023-01-03", "col_int2": "8", "col_float2": "8.8"}
|
||||||
|
|
||||||
|
// Attempt to get an out-of-bounds row (returns None)
|
||||||
|
let row_out_of_bounds = df.get_row(10);
|
||||||
|
// Output: Row out of bounds: None
|
||||||
|
println!("Row out of bounds: {:?}", row_out_of_bounds);
|
||||||
|
|
||||||
|
// Drop a column
|
||||||
|
df.drop_column("col_bool");
|
||||||
|
println!("\nDataFrame after dropping 'col_bool':\n{}", df);
|
||||||
|
|
||||||
|
println!("Columns after drop: {}", df.cols());
|
||||||
|
println!("Column names after drop: {:?}", df.get_column_names());
|
||||||
|
|
||||||
|
// Drop another column, ensuring the underlying Frame is removed if empty
|
||||||
|
df.drop_column("col_float1");
|
||||||
|
println!("\nDataFrame after dropping 'col_float1':\n{}", df);
|
||||||
|
|
||||||
|
println!("Columns after second drop: {}", df.cols());
|
||||||
|
println!(
|
||||||
|
"Column names after second drop: {:?}",
|
||||||
|
df.get_column_names()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Attempt to drop a non-existent column (will panic)
|
||||||
|
// df.drop_column("non_existent_col"); // Uncomment to see panic
|
||||||
|
```
|
||||||
|
|
||||||
### More examples
|
### More examples
|
||||||
|
|
||||||
See the [examples](./examples/) directory for some demonstrations of Rustframe's syntax and functionality.
|
See the [examples](./examples/) directory for some demonstrations of Rustframe's syntax and functionality.
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
//! It demonstrates matrix operations like shifting, counting neighbors, and applying game rules.
|
//! It demonstrates matrix operations like shifting, counting neighbors, and applying game rules.
|
||||||
//! The game runs in a loop, updating the board state and printing it to the console.
|
//! The game runs in a loop, updating the board state and printing it to the console.
|
||||||
//! To modify the behaviour of the example, please change the constants at the top of this file.
|
//! To modify the behaviour of the example, please change the constants at the top of this file.
|
||||||
//! By default,
|
|
||||||
|
|
||||||
use rustframe::matrix::{BoolMatrix, BoolOps, IntMatrix, Matrix};
|
use rustframe::matrix::{BoolMatrix, BoolOps, IntMatrix, Matrix};
|
||||||
use rustframe::random::{rng, Rng};
|
use rustframe::random::{rng, Rng};
|
||||||
@ -21,8 +21,6 @@ fn main() {
|
|||||||
let debug_mode = args.contains(&"--debug".to_string());
|
let debug_mode = args.contains(&"--debug".to_string());
|
||||||
let print_mode = if debug_mode { false } else { PRINT_BOARD };
|
let print_mode = if debug_mode { false } else { PRINT_BOARD };
|
||||||
|
|
||||||
// Initialize the game board.
|
|
||||||
// This demonstrates `BoolMatrix::from_vec`.
|
|
||||||
let mut current_board =
|
let mut current_board =
|
||||||
BoolMatrix::from_vec(vec![false; BOARD_SIZE * BOARD_SIZE], BOARD_SIZE, BOARD_SIZE);
|
BoolMatrix::from_vec(vec![false; BOARD_SIZE * BOARD_SIZE], BOARD_SIZE, BOARD_SIZE);
|
||||||
|
|
||||||
@ -31,15 +29,11 @@ fn main() {
|
|||||||
add_simulated_activity(&mut current_board, BOARD_SIZE);
|
add_simulated_activity(&mut current_board, BOARD_SIZE);
|
||||||
|
|
||||||
let mut generation_count: u32 = 0;
|
let mut generation_count: u32 = 0;
|
||||||
// `previous_board_state` will store a clone of the board.
|
|
||||||
// This demonstrates `Matrix::clone()` and later `PartialEq` for `Matrix`.
|
|
||||||
let mut previous_board_state: Option<BoolMatrix> = None;
|
let mut previous_board_state: Option<BoolMatrix> = None;
|
||||||
let mut board_hashes = Vec::new();
|
let mut board_hashes = Vec::new();
|
||||||
// let mut print_board_bool = true;
|
|
||||||
let mut print_bool_int = 0;
|
let mut print_bool_int = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// if print_board_bool {
|
|
||||||
if print_bool_int % SKIP_FRAMES == 0 {
|
if print_bool_int % SKIP_FRAMES == 0 {
|
||||||
print_board(¤t_board, generation_count, print_mode);
|
print_board(¤t_board, generation_count, print_mode);
|
||||||
|
|
||||||
@ -47,7 +41,6 @@ fn main() {
|
|||||||
} else {
|
} else {
|
||||||
print_bool_int += 1;
|
print_bool_int += 1;
|
||||||
}
|
}
|
||||||
// `current_board.count()` demonstrates a method from `BoolOps`.
|
|
||||||
board_hashes.push(hash_board(¤t_board, primes.clone()));
|
board_hashes.push(hash_board(¤t_board, primes.clone()));
|
||||||
if detect_stable_state(¤t_board, &previous_board_state) {
|
if detect_stable_state(¤t_board, &previous_board_state) {
|
||||||
println!(
|
println!(
|
||||||
@ -68,10 +61,8 @@ fn main() {
|
|||||||
add_simulated_activity(&mut current_board, BOARD_SIZE);
|
add_simulated_activity(&mut current_board, BOARD_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// `current_board.clone()` demonstrates `Clone` for `Matrix`.
|
|
||||||
previous_board_state = Some(current_board.clone());
|
previous_board_state = Some(current_board.clone());
|
||||||
|
|
||||||
// This is the core call to your game logic.
|
|
||||||
let next_board = game_of_life_next_frame(¤t_board);
|
let next_board = game_of_life_next_frame(¤t_board);
|
||||||
current_board = next_board;
|
current_board = next_board;
|
||||||
|
|
||||||
@ -106,7 +97,6 @@ fn print_board(board: &BoolMatrix, generation_count: u32, print_mode: bool) {
|
|||||||
print_str.push_str("| ");
|
print_str.push_str("| ");
|
||||||
for c in 0..board.cols() {
|
for c in 0..board.cols() {
|
||||||
if board[(r, c)] {
|
if board[(r, c)] {
|
||||||
// Using Index trait for Matrix<bool>
|
|
||||||
print_str.push_str("██");
|
print_str.push_str("██");
|
||||||
} else {
|
} else {
|
||||||
print_str.push_str(" ");
|
print_str.push_str(" ");
|
||||||
@ -188,74 +178,38 @@ pub fn game_of_life_next_frame(current_game: &BoolMatrix) -> BoolMatrix {
|
|||||||
if rows == 0 && cols == 0 {
|
if rows == 0 && cols == 0 {
|
||||||
return BoolMatrix::from_vec(vec![], 0, 0); // Return an empty BoolMatrix
|
return BoolMatrix::from_vec(vec![], 0, 0); // Return an empty BoolMatrix
|
||||||
}
|
}
|
||||||
// Assuming valid non-empty dimensions (e.g., 25x25) as per typical GOL.
|
|
||||||
// Your Matrix::from_vec would panic for other invalid 0-dim cases.
|
|
||||||
|
|
||||||
// Define the 8 neighbor offsets (row_delta, col_delta)
|
// Define the 8 neighbor offsets (row_delta, col_delta)
|
||||||
let neighbor_offsets: [(isize, isize); 8] = [
|
let neighbor_offsets: [(isize, isize); 8] = [
|
||||||
(-1, -1),
|
(-1, -1),
|
||||||
(-1, 0),
|
(-1, 0),
|
||||||
(-1, 1), // Top row (NW, N, NE)
|
(-1, 1),
|
||||||
(0, -1),
|
(0, -1),
|
||||||
(0, 1), // Middle row (W, E)
|
(0, 1),
|
||||||
(1, -1),
|
(1, -1),
|
||||||
(1, 0),
|
(1, 0),
|
||||||
(1, 1), // Bottom row (SW, S, SE)
|
(1, 1),
|
||||||
];
|
];
|
||||||
|
|
||||||
// 1. Initialize `neighbor_counts` with the first shifted layer.
|
|
||||||
// This demonstrates creating an IntMatrix from a function and using it as a base.
|
|
||||||
let (first_dr, first_dc) = neighbor_offsets[0];
|
let (first_dr, first_dc) = neighbor_offsets[0];
|
||||||
let mut neighbor_counts = get_shifted_neighbor_layer(current_game, first_dr, first_dc);
|
let mut neighbor_counts = get_shifted_neighbor_layer(current_game, first_dr, first_dc);
|
||||||
|
|
||||||
// 2. Add the remaining 7 neighbor layers.
|
|
||||||
// This demonstrates element-wise addition of matrices (`Matrix + Matrix`).
|
|
||||||
for i in 1..neighbor_offsets.len() {
|
for i in 1..neighbor_offsets.len() {
|
||||||
let (dr, dc) = neighbor_offsets[i];
|
let (dr, dc) = neighbor_offsets[i];
|
||||||
let next_neighbor_layer = get_shifted_neighbor_layer(current_game, dr, dc);
|
let next_neighbor_layer = get_shifted_neighbor_layer(current_game, dr, dc);
|
||||||
// `neighbor_counts` (owned IntMatrix) + `next_neighbor_layer` (owned IntMatrix)
|
|
||||||
// uses `impl Add for Matrix`, consumes both, returns new owned `IntMatrix`.
|
|
||||||
neighbor_counts = neighbor_counts + next_neighbor_layer;
|
neighbor_counts = neighbor_counts + next_neighbor_layer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Apply Game of Life rules using element-wise operations.
|
|
||||||
|
|
||||||
// Rule: Survival or Birth based on neighbor counts.
|
|
||||||
// A cell is alive in the next generation if:
|
|
||||||
// (it's currently alive AND has 2 or 3 neighbors) OR
|
|
||||||
// (it's currently dead AND has exactly 3 neighbors)
|
|
||||||
|
|
||||||
// `neighbor_counts.eq_elem(scalar)`:
|
|
||||||
// Demonstrates element-wise comparison of a Matrix with a scalar (broadcast).
|
|
||||||
// Returns an owned `BoolMatrix`.
|
|
||||||
let has_2_neighbors = neighbor_counts.eq_elem(2);
|
let has_2_neighbors = neighbor_counts.eq_elem(2);
|
||||||
let has_3_neighbors = neighbor_counts.eq_elem(3); // This will be reused
|
let has_3_neighbors = neighbor_counts.eq_elem(3);
|
||||||
|
|
||||||
// `has_2_neighbors | has_3_neighbors`:
|
let has_2_or_3_neighbors = has_2_neighbors | has_3_neighbors.clone();
|
||||||
// Demonstrates element-wise OR (`Matrix<bool> | Matrix<bool>`).
|
|
||||||
// Consumes both operands, returns an owned `BoolMatrix`.
|
|
||||||
let has_2_or_3_neighbors = has_2_neighbors | has_3_neighbors.clone(); // Clone has_3_neighbors as it's used again
|
|
||||||
|
|
||||||
// `current_game & &has_2_or_3_neighbors`:
|
|
||||||
// `current_game` is `&BoolMatrix`. `has_2_or_3_neighbors` is owned.
|
|
||||||
// Demonstrates element-wise AND (`&Matrix<bool> & &Matrix<bool>`).
|
|
||||||
// Borrows both operands, returns an owned `BoolMatrix`.
|
|
||||||
let survives = current_game & &has_2_or_3_neighbors;
|
let survives = current_game & &has_2_or_3_neighbors;
|
||||||
|
|
||||||
// `!current_game`:
|
|
||||||
// Demonstrates element-wise NOT (`!&Matrix<bool>`).
|
|
||||||
// Borrows operand, returns an owned `BoolMatrix`.
|
|
||||||
let is_dead = !current_game;
|
let is_dead = !current_game;
|
||||||
|
|
||||||
// `is_dead & &has_3_neighbors`:
|
|
||||||
// `is_dead` is owned. `has_3_neighbors` is owned.
|
|
||||||
// Demonstrates element-wise AND (`Matrix<bool> & &Matrix<bool>`).
|
|
||||||
// Consumes `is_dead`, borrows `has_3_neighbors`, returns an owned `BoolMatrix`.
|
|
||||||
let births = is_dead & &has_3_neighbors;
|
let births = is_dead & &has_3_neighbors;
|
||||||
|
|
||||||
// `survives | births`:
|
|
||||||
// Demonstrates element-wise OR (`Matrix<bool> | Matrix<bool>`).
|
|
||||||
// Consumes both operands, returns an owned `BoolMatrix`.
|
|
||||||
let next_frame_game = survives | births;
|
let next_frame_game = survives | births;
|
||||||
|
|
||||||
next_frame_game
|
next_frame_game
|
||||||
|
@ -16,7 +16,7 @@ fn student_passing_example() {
|
|||||||
|
|
||||||
// Hours studied for each student
|
// Hours studied for each student
|
||||||
let hours = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
|
let hours = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
|
||||||
// 0 = fail, 1 = pass
|
// Label: 0 denotes failure and 1 denotes success
|
||||||
let passed = vec![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0];
|
let passed = vec![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0];
|
||||||
|
|
||||||
let x = Matrix::from_vec(hours.clone(), hours.len(), 1);
|
let x = Matrix::from_vec(hours.clone(), hours.len(), 1);
|
||||||
|
@ -6,9 +6,9 @@ use rustframe::matrix::{Axis, Matrix};
|
|||||||
/// Demonstrates some of the statistics utilities in Rustframe.
|
/// Demonstrates some of the statistics utilities in Rustframe.
|
||||||
///
|
///
|
||||||
/// The example is split into three parts:
|
/// The example is split into three parts:
|
||||||
/// 1. Basic descriptive statistics on a small data set.
|
/// - Basic descriptive statistics on a small data set
|
||||||
/// 2. Covariance and correlation calculations.
|
/// - Covariance and correlation calculations
|
||||||
/// 3. Simple inferential tests (t-test and chi-square).
|
/// - Simple inferential tests (t-test and chi-square)
|
||||||
fn main() {
|
fn main() {
|
||||||
descriptive_demo();
|
descriptive_demo();
|
||||||
println!("\n-----\n");
|
println!("\n-----\n");
|
||||||
|
@ -44,11 +44,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_pca_basic() {
|
fn test_pca_basic() {
|
||||||
// Simple 2D data, points along y=x line
|
// Simple 2D data with points along the y = x line
|
||||||
// Data:
|
|
||||||
// 1.0, 1.0
|
|
||||||
// 2.0, 2.0
|
|
||||||
// 3.0, 3.0
|
|
||||||
let data = Matrix::from_rows_vec(vec![1.0, 1.0, 2.0, 2.0, 3.0, 3.0], 3, 2);
|
let data = Matrix::from_rows_vec(vec![1.0, 1.0, 2.0, 2.0, 3.0, 3.0], 3, 2);
|
||||||
let (_n_samples, _n_features) = data.shape();
|
let (_n_samples, _n_features) = data.shape();
|
||||||
|
|
||||||
@ -71,15 +67,7 @@ mod tests {
|
|||||||
assert!((pca.components.get(0, 0) - 1.0).abs() < EPSILON);
|
assert!((pca.components.get(0, 0) - 1.0).abs() < EPSILON);
|
||||||
assert!((pca.components.get(0, 1) - 1.0).abs() < EPSILON);
|
assert!((pca.components.get(0, 1) - 1.0).abs() < EPSILON);
|
||||||
|
|
||||||
// Test transform
|
// Test transform: centered data projects to [-2.0, 0.0, 2.0]
|
||||||
// Centered data:
|
|
||||||
// -1.0, -1.0
|
|
||||||
// 0.0, 0.0
|
|
||||||
// 1.0, 1.0
|
|
||||||
// Projected: (centered_data * components.transpose())
|
|
||||||
// (-1.0 * 1.0 + -1.0 * 1.0) = -2.0
|
|
||||||
// ( 0.0 * 1.0 + 0.0 * 1.0) = 0.0
|
|
||||||
// ( 1.0 * 1.0 + 1.0 * 1.0) = 2.0
|
|
||||||
let transformed_data = pca.transform(&data);
|
let transformed_data = pca.transform(&data);
|
||||||
assert_eq!(transformed_data.rows(), 3);
|
assert_eq!(transformed_data.rows(), 3);
|
||||||
assert_eq!(transformed_data.cols(), 1);
|
assert_eq!(transformed_data.cols(), 1);
|
||||||
|
@ -137,10 +137,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_covariance_scalar_same_matrix() {
|
fn test_covariance_scalar_same_matrix() {
|
||||||
// M =
|
// Matrix with rows [1, 2] and [3, 4]; mean is 2.5
|
||||||
// 1,2
|
|
||||||
// 3,4
|
|
||||||
// mean = 2.5
|
|
||||||
let data = vec![1.0, 2.0, 3.0, 4.0];
|
let data = vec![1.0, 2.0, 3.0, 4.0];
|
||||||
let m = Matrix::from_vec(data.clone(), 2, 2);
|
let m = Matrix::from_vec(data.clone(), 2, 2);
|
||||||
|
|
||||||
@ -152,10 +149,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_covariance_scalar_diff_matrix() {
|
fn test_covariance_scalar_diff_matrix() {
|
||||||
// x =
|
// Matrix x has rows [1, 2] and [3, 4]; y is two times x
|
||||||
// 1,2
|
|
||||||
// 3,4
|
|
||||||
// y = 2*x
|
|
||||||
let x = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
let x = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
let y = Matrix::from_vec(vec![2.0, 4.0, 6.0, 8.0], 2, 2);
|
let y = Matrix::from_vec(vec![2.0, 4.0, 6.0, 8.0], 2, 2);
|
||||||
|
|
||||||
@ -167,10 +161,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_covariance_vertical() {
|
fn test_covariance_vertical() {
|
||||||
// M =
|
// Matrix with rows [1, 2] and [3, 4]; columns are [1,3] and [2,4], each var=1, cov=1
|
||||||
// 1,2
|
|
||||||
// 3,4
|
|
||||||
// cols are [1,3] and [2,4], each var=1, cov=1
|
|
||||||
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
let cov_mat = covariance_vertical(&m);
|
let cov_mat = covariance_vertical(&m);
|
||||||
|
|
||||||
@ -184,10 +175,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_covariance_horizontal() {
|
fn test_covariance_horizontal() {
|
||||||
// M =
|
// Matrix with rows [1,2] and [3,4], each var=0.25, cov=0.25
|
||||||
// 1,2
|
|
||||||
// 3,4
|
|
||||||
// rows are [1,2] and [3,4], each var=0.25, cov=0.25
|
|
||||||
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
let cov_mat = covariance_horizontal(&m);
|
let cov_mat = covariance_horizontal(&m);
|
||||||
|
|
||||||
@ -201,10 +189,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_covariance_matrix_vertical() {
|
fn test_covariance_matrix_vertical() {
|
||||||
// Test with a simple 2x2 matrix
|
// Test with a simple 2x2 matrix with rows [1, 2] and [3, 4]
|
||||||
// M =
|
|
||||||
// 1, 2
|
|
||||||
// 3, 4
|
|
||||||
// Expected covariance matrix (vertical, i.e., between columns):
|
// Expected covariance matrix (vertical, i.e., between columns):
|
||||||
// Col1: [1, 3], mean = 2
|
// Col1: [1, 3], mean = 2
|
||||||
// Col2: [2, 4], mean = 3
|
// Col2: [2, 4], mean = 3
|
||||||
@ -212,9 +197,7 @@ mod tests {
|
|||||||
// Cov(Col2, Col2) = ((2-3)^2 + (4-3)^2) / (2-1) = (1+1)/1 = 2
|
// Cov(Col2, Col2) = ((2-3)^2 + (4-3)^2) / (2-1) = (1+1)/1 = 2
|
||||||
// Cov(Col1, Col2) = ((1-2)*(2-3) + (3-2)*(4-3)) / (2-1) = ((-1)*(-1) + (1)*(1))/1 = (1+1)/1 = 2
|
// Cov(Col1, Col2) = ((1-2)*(2-3) + (3-2)*(4-3)) / (2-1) = ((-1)*(-1) + (1)*(1))/1 = (1+1)/1 = 2
|
||||||
// Cov(Col2, Col1) = 2
|
// Cov(Col2, Col1) = 2
|
||||||
// Expected:
|
// Expected matrix filled with 2
|
||||||
// 2, 2
|
|
||||||
// 2, 2
|
|
||||||
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
let cov_mat = covariance_matrix(&m, Axis::Col);
|
let cov_mat = covariance_matrix(&m, Axis::Col);
|
||||||
|
|
||||||
@ -226,10 +209,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_covariance_matrix_horizontal() {
|
fn test_covariance_matrix_horizontal() {
|
||||||
// Test with a simple 2x2 matrix
|
// Test with a simple 2x2 matrix with rows [1, 2] and [3, 4]
|
||||||
// M =
|
|
||||||
// 1, 2
|
|
||||||
// 3, 4
|
|
||||||
// Expected covariance matrix (horizontal, i.e., between rows):
|
// Expected covariance matrix (horizontal, i.e., between rows):
|
||||||
// Row1: [1, 2], mean = 1.5
|
// Row1: [1, 2], mean = 1.5
|
||||||
// Row2: [3, 4], mean = 3.5
|
// Row2: [3, 4], mean = 3.5
|
||||||
@ -237,9 +217,7 @@ mod tests {
|
|||||||
// Cov(Row2, Row2) = ((3-3.5)^2 + (4-3.5)^2) / (2-1) = (0.25+0.25)/1 = 0.5
|
// Cov(Row2, Row2) = ((3-3.5)^2 + (4-3.5)^2) / (2-1) = (0.25+0.25)/1 = 0.5
|
||||||
// Cov(Row1, Row2) = ((1-1.5)*(3-3.5) + (2-1.5)*(4-3.5)) / (2-1) = ((-0.5)*(-0.5) + (0.5)*(0.5))/1 = (0.25+0.25)/1 = 0.5
|
// Cov(Row1, Row2) = ((1-1.5)*(3-3.5) + (2-1.5)*(4-3.5)) / (2-1) = ((-0.5)*(-0.5) + (0.5)*(0.5))/1 = (0.25+0.25)/1 = 0.5
|
||||||
// Cov(Row2, Row1) = 0.5
|
// Cov(Row2, Row1) = 0.5
|
||||||
// Expected:
|
// Expected matrix: [[0.5, -0.5], [-0.5, 0.5]]
|
||||||
// 0.5, -0.5
|
|
||||||
// -0.5, 0.5
|
|
||||||
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
let cov_mat = covariance_matrix(&m, Axis::Row);
|
let cov_mat = covariance_matrix(&m, Axis::Row);
|
||||||
|
|
||||||
|
@ -350,11 +350,7 @@ mod tests {
|
|||||||
let data: Vec<f64> = (1..=24).map(|x| x as f64).collect();
|
let data: Vec<f64> = (1..=24).map(|x| x as f64).collect();
|
||||||
let x = Matrix::from_vec(data, 4, 6);
|
let x = Matrix::from_vec(data, 4, 6);
|
||||||
|
|
||||||
// columns:
|
// columns contain sequences increasing by four starting at 1 through 4
|
||||||
// 1, 5, 9, 13, 17, 21
|
|
||||||
// 2, 6, 10, 14, 18, 22
|
|
||||||
// 3, 7, 11, 15, 19, 23
|
|
||||||
// 4, 8, 12, 16, 20, 24
|
|
||||||
|
|
||||||
let er0 = vec![1., 5., 9., 13., 17., 21.];
|
let er0 = vec![1., 5., 9., 13., 17., 21.];
|
||||||
let er50 = vec![3., 7., 11., 15., 19., 23.];
|
let er50 = vec![3., 7., 11., 15., 19., 23.];
|
||||||
|
659
src/dataframe/df.rs
Normal file
659
src/dataframe/df.rs
Normal file
@ -0,0 +1,659 @@
|
|||||||
|
use crate::frame::{Frame, RowIndex};
|
||||||
|
use std::any::{Any, TypeId};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::fmt; // Import TypeId
|
||||||
|
|
||||||
|
const DEFAULT_DISPLAY_ROWS: usize = 5;
|
||||||
|
const DEFAULT_DISPLAY_COLS: usize = 10;
|
||||||
|
|
||||||
|
// Trait to enable type-agnostic operations on Frame objects within DataFrame
|
||||||
|
pub trait SubFrame: Send + Sync + fmt::Debug + Any {
|
||||||
|
fn rows(&self) -> usize;
|
||||||
|
fn get_value_as_string(&self, physical_row_idx: usize, col_name: &str) -> String;
|
||||||
|
fn clone_box(&self) -> Box<dyn SubFrame>;
|
||||||
|
fn delete_column_from_frame(&mut self, col_name: &str);
|
||||||
|
fn get_frame_cols(&self) -> usize; // Add a method to get the number of columns in the underlying frame
|
||||||
|
|
||||||
|
// Methods for downcasting to concrete types
|
||||||
|
fn as_any(&self) -> &dyn Any;
|
||||||
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implement SubFrame for any Frame<T> that meets the requirements
|
||||||
|
impl<T> SubFrame for Frame<T>
|
||||||
|
where
|
||||||
|
T: Clone + PartialEq + fmt::Display + fmt::Debug + 'static + Send + Sync + Any,
|
||||||
|
{
|
||||||
|
fn rows(&self) -> usize {
|
||||||
|
self.rows()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_value_as_string(&self, physical_row_idx: usize, col_name: &str) -> String {
|
||||||
|
self.get_row(physical_row_idx).get(col_name).to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clone_box(&self) -> Box<dyn SubFrame> {
|
||||||
|
Box::new(self.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn delete_column_from_frame(&mut self, col_name: &str) {
|
||||||
|
self.delete_column(col_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_frame_cols(&self) -> usize {
|
||||||
|
self.cols()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_any(&self) -> &dyn Any {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DataFrame {
|
||||||
|
frames_by_type: HashMap<TypeId, Box<dyn SubFrame>>, // Maps TypeId to the Frame holding columns of that type
|
||||||
|
column_to_type: HashMap<String, TypeId>, // Maps column name to its TypeId
|
||||||
|
column_names: Vec<String>,
|
||||||
|
index: RowIndex,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DataFrame {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
DataFrame {
|
||||||
|
frames_by_type: HashMap::new(),
|
||||||
|
column_to_type: HashMap::new(),
|
||||||
|
column_names: Vec::new(),
|
||||||
|
index: RowIndex::Range(0..0), // Initialize with an empty range index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the number of rows in the DataFrame.
|
||||||
|
pub fn rows(&self) -> usize {
|
||||||
|
self.index.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the number of columns in the DataFrame.
|
||||||
|
pub fn cols(&self) -> usize {
|
||||||
|
self.column_names.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a reference to the vector of column names.
|
||||||
|
pub fn get_column_names(&self) -> &Vec<String> {
|
||||||
|
&self.column_names
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the number of internal Frame objects (one per unique data type).
|
||||||
|
pub fn num_internal_frames(&self) -> usize {
|
||||||
|
self.frames_by_type.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a reference to a column of a specific type, if it exists.
|
||||||
|
pub fn get_column<T>(&self, col_name: &str) -> Option<&[T]>
|
||||||
|
where
|
||||||
|
T: Clone + PartialEq + fmt::Display + fmt::Debug + 'static + Send + Sync + Any,
|
||||||
|
{
|
||||||
|
let expected_type_id = TypeId::of::<T>();
|
||||||
|
if let Some(actual_type_id) = self.column_to_type.get(col_name) {
|
||||||
|
if *actual_type_id == expected_type_id {
|
||||||
|
if let Some(sub_frame_box) = self.frames_by_type.get(actual_type_id) {
|
||||||
|
if let Some(frame) = sub_frame_box.as_any().downcast_ref::<Frame<T>>() {
|
||||||
|
return Some(frame.column(col_name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a HashMap representing a row, mapping column names to their string values.
|
||||||
|
pub fn get_row(&self, row_idx: usize) -> Option<HashMap<String, String>> {
|
||||||
|
if row_idx >= self.rows() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut row_data = HashMap::new();
|
||||||
|
for col_name in &self.column_names {
|
||||||
|
if let Some(type_id) = self.column_to_type.get(col_name) {
|
||||||
|
if let Some(sub_frame_box) = self.frames_by_type.get(type_id) {
|
||||||
|
let value = sub_frame_box.get_value_as_string(row_idx, col_name);
|
||||||
|
row_data.insert(col_name.clone(), value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(row_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_column<T>(&mut self, col_name: &str, data: Vec<T>)
|
||||||
|
where
|
||||||
|
T: Clone + PartialEq + fmt::Display + fmt::Debug + 'static + Send + Sync + Any,
|
||||||
|
{
|
||||||
|
let type_id = TypeId::of::<T>();
|
||||||
|
let col_name_string = col_name.to_string();
|
||||||
|
|
||||||
|
// Check for duplicate column name across the entire DataFrame
|
||||||
|
if self.column_to_type.contains_key(&col_name_string) {
|
||||||
|
panic!(
|
||||||
|
"DataFrame::add_column: duplicate column name: '{}'",
|
||||||
|
col_name_string
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this is the first column being added, set the DataFrame's index
|
||||||
|
if self.column_names.is_empty() {
|
||||||
|
self.index = RowIndex::Range(0..data.len());
|
||||||
|
} else {
|
||||||
|
// Ensure new column has the same number of rows as existing columns
|
||||||
|
if data.len() != self.index.len() {
|
||||||
|
panic!(
|
||||||
|
"DataFrame::add_column: new column '{}' has {} rows, but existing columns have {} rows",
|
||||||
|
col_name_string,
|
||||||
|
data.len(),
|
||||||
|
self.index.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if a Frame of this type already exists
|
||||||
|
if let Some(sub_frame_box) = self.frames_by_type.get_mut(&type_id) {
|
||||||
|
// Downcast to the concrete Frame<T> and add the column
|
||||||
|
if let Some(frame) = sub_frame_box.as_any_mut().downcast_mut::<Frame<T>>() {
|
||||||
|
frame.add_column(col_name_string.clone(), data);
|
||||||
|
} else {
|
||||||
|
// This should ideally not happen if TypeId matches, but good for safety
|
||||||
|
panic!(
|
||||||
|
"Type mismatch when downcasting existing SubFrame for TypeId {:?}",
|
||||||
|
type_id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// No Frame of this type exists, create a new one
|
||||||
|
// The Frame::new constructor expects a Matrix and column names.
|
||||||
|
// We create a Matrix from a single column vector.
|
||||||
|
let new_frame = Frame::new(
|
||||||
|
crate::matrix::Matrix::from_cols(vec![data]),
|
||||||
|
vec![col_name_string.clone()],
|
||||||
|
Some(self.index.clone()), // Pass the DataFrame's index to the new Frame
|
||||||
|
);
|
||||||
|
self.frames_by_type.insert(type_id, Box::new(new_frame));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update column mappings and names
|
||||||
|
self.column_to_type.insert(col_name_string.clone(), type_id);
|
||||||
|
self.column_names.push(col_name_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drops a column from the DataFrame.
|
||||||
|
/// Panics if the column does not exist.
|
||||||
|
pub fn drop_column(&mut self, col_name: &str) {
|
||||||
|
let col_name_string = col_name.to_string();
|
||||||
|
|
||||||
|
// 1. Get the TypeId associated with the column
|
||||||
|
let type_id = self
|
||||||
|
.column_to_type
|
||||||
|
.remove(&col_name_string)
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
panic!(
|
||||||
|
"DataFrame::drop_column: column '{}' not found",
|
||||||
|
col_name_string
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 2. Remove the column name from the ordered list
|
||||||
|
self.column_names.retain(|name| name != &col_name_string);
|
||||||
|
|
||||||
|
// 3. Find the Frame object and delete the column from it
|
||||||
|
if let Some(sub_frame_box) = self.frames_by_type.get_mut(&type_id) {
|
||||||
|
sub_frame_box.delete_column_from_frame(&col_name_string);
|
||||||
|
|
||||||
|
// 4. If the Frame object for this type becomes empty, remove it from frames_by_type
|
||||||
|
if sub_frame_box.get_frame_cols() == 0 {
|
||||||
|
self.frames_by_type.remove(&type_id);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// This should not happen if column_to_type was consistent
|
||||||
|
panic!(
|
||||||
|
"DataFrame::drop_column: internal error, no frame found for type_id {:?}",
|
||||||
|
type_id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for DataFrame {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
// Display column headers
|
||||||
|
for col_name in self.column_names.iter().take(DEFAULT_DISPLAY_COLS) {
|
||||||
|
write!(f, "{:<15}", col_name)?;
|
||||||
|
}
|
||||||
|
if self.column_names.len() > DEFAULT_DISPLAY_COLS {
|
||||||
|
write!(f, "...")?;
|
||||||
|
}
|
||||||
|
writeln!(f)?;
|
||||||
|
|
||||||
|
// Display data rows
|
||||||
|
let mut displayed_rows = 0;
|
||||||
|
for i in 0..self.index.len() {
|
||||||
|
if displayed_rows >= DEFAULT_DISPLAY_ROWS {
|
||||||
|
writeln!(f, "...")?;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for col_name in self.column_names.iter().take(DEFAULT_DISPLAY_COLS) {
|
||||||
|
if let Some(type_id) = self.column_to_type.get(col_name) {
|
||||||
|
if let Some(sub_frame_box) = self.frames_by_type.get(type_id) {
|
||||||
|
write!(f, "{:<15}", sub_frame_box.get_value_as_string(i, col_name))?;
|
||||||
|
} else {
|
||||||
|
// This case indicates an inconsistency: column_to_type has an entry,
|
||||||
|
// but frames_by_type doesn't have the corresponding Frame.
|
||||||
|
write!(f, "{:<15}", "[ERROR]")?;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// This case indicates an inconsistency: column_names has an entry,
|
||||||
|
// but column_to_type doesn't have the corresponding column.
|
||||||
|
write!(f, "{:<15}", "[ERROR]")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.column_names.len() > DEFAULT_DISPLAY_COLS {
|
||||||
|
write!(f, "...")?;
|
||||||
|
}
|
||||||
|
writeln!(f)?;
|
||||||
|
displayed_rows += 1;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for DataFrame {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.debug_struct("DataFrame")
|
||||||
|
.field("column_names", &self.column_names)
|
||||||
|
.field("index", &self.index)
|
||||||
|
.field("column_to_type", &self.column_to_type)
|
||||||
|
.field("frames_by_type", &self.frames_by_type)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::frame::Frame;
|
||||||
|
use crate::matrix::Matrix;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_new() {
|
||||||
|
let df = DataFrame::new();
|
||||||
|
assert_eq!(df.rows(), 0);
|
||||||
|
assert_eq!(df.cols(), 0);
|
||||||
|
assert!(df.get_column_names().is_empty());
|
||||||
|
assert!(df.frames_by_type.is_empty());
|
||||||
|
assert!(df.column_to_type.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_add_column_initial() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
let data = vec![1, 2, 3];
|
||||||
|
df.add_column("col_int", data.clone());
|
||||||
|
|
||||||
|
assert_eq!(df.rows(), 3);
|
||||||
|
assert_eq!(df.cols(), 1);
|
||||||
|
assert_eq!(df.get_column_names(), &vec!["col_int".to_string()]);
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<i32>()));
|
||||||
|
assert_eq!(df.column_to_type.get("col_int"), Some(&TypeId::of::<i32>()));
|
||||||
|
|
||||||
|
// Verify the underlying frame
|
||||||
|
let sub_frame_box = df.frames_by_type.get(&TypeId::of::<i32>()).unwrap();
|
||||||
|
let frame = sub_frame_box.as_any().downcast_ref::<Frame<i32>>().unwrap();
|
||||||
|
assert_eq!(frame.rows(), 3);
|
||||||
|
assert_eq!(frame.cols(), 1);
|
||||||
|
assert_eq!(frame.columns(), &vec!["col_int".to_string()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_add_column_same_type() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int1", vec![1, 2, 3]);
|
||||||
|
df.add_column("col_int2", vec![4, 5, 6]);
|
||||||
|
|
||||||
|
assert_eq!(df.rows(), 3);
|
||||||
|
assert_eq!(df.cols(), 2);
|
||||||
|
assert_eq!(
|
||||||
|
df.get_column_names(),
|
||||||
|
&vec!["col_int1".to_string(), "col_int2".to_string()]
|
||||||
|
);
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<i32>()));
|
||||||
|
assert_eq!(
|
||||||
|
df.column_to_type.get("col_int1"),
|
||||||
|
Some(&TypeId::of::<i32>())
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
df.column_to_type.get("col_int2"),
|
||||||
|
Some(&TypeId::of::<i32>())
|
||||||
|
);
|
||||||
|
|
||||||
|
// Verify the underlying frame
|
||||||
|
let sub_frame_box = df.frames_by_type.get(&TypeId::of::<i32>()).unwrap();
|
||||||
|
let frame = sub_frame_box.as_any().downcast_ref::<Frame<i32>>().unwrap();
|
||||||
|
assert_eq!(frame.rows(), 3);
|
||||||
|
assert_eq!(frame.cols(), 2);
|
||||||
|
assert_eq!(
|
||||||
|
frame.columns(),
|
||||||
|
&vec!["col_int1".to_string(), "col_int2".to_string()]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_add_column_different_type() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int", vec![1, 2, 3]);
|
||||||
|
df.add_column("col_float", vec![1.1, 2.2, 3.3]);
|
||||||
|
df.add_column(
|
||||||
|
"col_string",
|
||||||
|
vec!["a".to_string(), "b".to_string(), "c".to_string()],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(df.rows(), 3);
|
||||||
|
assert_eq!(df.cols(), 3);
|
||||||
|
assert_eq!(
|
||||||
|
df.get_column_names(),
|
||||||
|
&vec![
|
||||||
|
"col_int".to_string(),
|
||||||
|
"col_float".to_string(),
|
||||||
|
"col_string".to_string()
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<i32>()));
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<f64>()));
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<String>()));
|
||||||
|
|
||||||
|
assert_eq!(df.column_to_type.get("col_int"), Some(&TypeId::of::<i32>()));
|
||||||
|
assert_eq!(
|
||||||
|
df.column_to_type.get("col_float"),
|
||||||
|
Some(&TypeId::of::<f64>())
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
df.column_to_type.get("col_string"),
|
||||||
|
Some(&TypeId::of::<String>())
|
||||||
|
);
|
||||||
|
|
||||||
|
// Verify underlying frames
|
||||||
|
let int_frame = df
|
||||||
|
.frames_by_type
|
||||||
|
.get(&TypeId::of::<i32>())
|
||||||
|
.unwrap()
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<Frame<i32>>()
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(int_frame.columns(), &vec!["col_int".to_string()]);
|
||||||
|
|
||||||
|
let float_frame = df
|
||||||
|
.frames_by_type
|
||||||
|
.get(&TypeId::of::<f64>())
|
||||||
|
.unwrap()
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<Frame<f64>>()
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(float_frame.columns(), &vec!["col_float".to_string()]);
|
||||||
|
|
||||||
|
let string_frame = df
|
||||||
|
.frames_by_type
|
||||||
|
.get(&TypeId::of::<String>())
|
||||||
|
.unwrap()
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<Frame<String>>()
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(string_frame.columns(), &vec!["col_string".to_string()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_get_column() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int", vec![1, 2, 3]);
|
||||||
|
df.add_column("col_float", vec![1.1, 2.2, 3.3]);
|
||||||
|
df.add_column(
|
||||||
|
"col_string",
|
||||||
|
vec!["a".to_string(), "b".to_string(), "c".to_string()],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Test getting existing columns with correct type
|
||||||
|
assert_eq!(
|
||||||
|
df.get_column::<i32>("col_int").unwrap(),
|
||||||
|
vec![1, 2, 3].as_slice()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
df.get_column::<f64>("col_float").unwrap(),
|
||||||
|
vec![1.1, 2.2, 3.3].as_slice()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
df.get_column::<String>("col_string").unwrap(),
|
||||||
|
vec!["a".to_string(), "b".to_string(), "c".to_string()].as_slice()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Test getting non-existent column
|
||||||
|
assert_eq!(df.get_column::<i32>("non_existent"), None);
|
||||||
|
|
||||||
|
// Test getting existing column with incorrect type
|
||||||
|
assert_eq!(df.get_column::<f64>("col_int"), None);
|
||||||
|
assert_eq!(df.get_column::<i32>("col_float"), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_get_row() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int", vec![1, 2, 3]);
|
||||||
|
df.add_column("col_float", vec![1.1, 2.2, 3.3]);
|
||||||
|
df.add_column(
|
||||||
|
"col_string",
|
||||||
|
vec!["a".to_string(), "b".to_string(), "c".to_string()],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Test getting an existing row
|
||||||
|
let row0 = df.get_row(0).unwrap();
|
||||||
|
assert_eq!(row0.get("col_int"), Some(&"1".to_string()));
|
||||||
|
assert_eq!(row0.get("col_float"), Some(&"1.1".to_string()));
|
||||||
|
assert_eq!(row0.get("col_string"), Some(&"a".to_string()));
|
||||||
|
|
||||||
|
let row1 = df.get_row(1).unwrap();
|
||||||
|
assert_eq!(row1.get("col_int"), Some(&"2".to_string()));
|
||||||
|
assert_eq!(row1.get("col_float"), Some(&"2.2".to_string()));
|
||||||
|
assert_eq!(row1.get("col_string"), Some(&"b".to_string()));
|
||||||
|
|
||||||
|
// Test getting an out-of-bounds row
|
||||||
|
assert_eq!(df.get_row(3), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic(expected = "DataFrame::add_column: duplicate column name: 'col_int'")]
|
||||||
|
fn test_dataframe_add_column_duplicate_name() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int", vec![1, 2, 3]);
|
||||||
|
df.add_column("col_int", vec![4, 5, 6]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic(
|
||||||
|
expected = "DataFrame::add_column: new column 'col_int2' has 2 rows, but existing columns have 3 rows"
|
||||||
|
)]
|
||||||
|
fn test_dataframe_add_column_mismatched_rows() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int1", vec![1, 2, 3]);
|
||||||
|
df.add_column("col_int2", vec![4, 5]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_display() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int", vec![1, 2, 3, 4, 5, 6]);
|
||||||
|
df.add_column("col_float", vec![1.1, 2.2, 3.3, 4.4, 5.5, 6.6]);
|
||||||
|
df.add_column(
|
||||||
|
"col_string",
|
||||||
|
vec![
|
||||||
|
"a".to_string(),
|
||||||
|
"b".to_string(),
|
||||||
|
"c".to_string(),
|
||||||
|
"d".to_string(),
|
||||||
|
"e".to_string(),
|
||||||
|
"f".to_string(),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
let expected_output = "\
|
||||||
|
col_int col_float col_string
|
||||||
|
1 1.1 a
|
||||||
|
2 2.2 b
|
||||||
|
3 3.3 c
|
||||||
|
4 4.4 d
|
||||||
|
5 5.5 e
|
||||||
|
...
|
||||||
|
";
|
||||||
|
assert_eq!(format!("{}", df), expected_output);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_debug() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int", vec![1, 2, 3]);
|
||||||
|
df.add_column("col_float", vec![1.1, 2.2, 3.3]);
|
||||||
|
|
||||||
|
let debug_output = format!("{:?}", df);
|
||||||
|
assert!(debug_output.contains("DataFrame {"));
|
||||||
|
assert!(debug_output.contains("column_names: [\"col_int\", \"col_float\"]"));
|
||||||
|
assert!(debug_output.contains("index: Range(0..3)"));
|
||||||
|
assert!(debug_output.contains("column_to_type: {"));
|
||||||
|
assert!(debug_output.contains("frames_by_type: {"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_drop_column_single_type() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int1", vec![1, 2, 3]);
|
||||||
|
df.add_column("col_int2", vec![4, 5, 6]);
|
||||||
|
df.add_column("col_float", vec![1.1, 2.2, 3.3]);
|
||||||
|
|
||||||
|
assert_eq!(df.cols(), 3);
|
||||||
|
assert_eq!(
|
||||||
|
df.get_column_names(),
|
||||||
|
&vec![
|
||||||
|
"col_int1".to_string(),
|
||||||
|
"col_int2".to_string(),
|
||||||
|
"col_float".to_string()
|
||||||
|
]
|
||||||
|
);
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<i32>()));
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<f64>()));
|
||||||
|
|
||||||
|
df.drop_column("col_int1");
|
||||||
|
|
||||||
|
assert_eq!(df.cols(), 2);
|
||||||
|
assert_eq!(
|
||||||
|
df.get_column_names(),
|
||||||
|
&vec!["col_int2".to_string(), "col_float".to_string()]
|
||||||
|
);
|
||||||
|
assert!(df.column_to_type.get("col_int1").is_none());
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<i32>())); // Frame<i32> should still exist
|
||||||
|
let int_frame = df
|
||||||
|
.frames_by_type
|
||||||
|
.get(&TypeId::of::<i32>())
|
||||||
|
.unwrap()
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<Frame<i32>>()
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(int_frame.columns(), &vec!["col_int2".to_string()]);
|
||||||
|
|
||||||
|
df.drop_column("col_int2");
|
||||||
|
|
||||||
|
assert_eq!(df.cols(), 1);
|
||||||
|
assert_eq!(df.get_column_names(), &vec!["col_float".to_string()]);
|
||||||
|
assert!(df.column_to_type.get("col_int2").is_none());
|
||||||
|
assert!(!df.frames_by_type.contains_key(&TypeId::of::<i32>())); // Frame<i32> should be removed
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<f64>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_drop_column_mixed_types() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int", vec![1, 2, 3]);
|
||||||
|
df.add_column("col_float", vec![1.1, 2.2, 3.3]);
|
||||||
|
df.add_column(
|
||||||
|
"col_string",
|
||||||
|
vec!["a".to_string(), "b".to_string(), "c".to_string()],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(df.cols(), 3);
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<i32>()));
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<f64>()));
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<String>()));
|
||||||
|
|
||||||
|
df.drop_column("col_float");
|
||||||
|
|
||||||
|
assert_eq!(df.cols(), 2);
|
||||||
|
assert_eq!(
|
||||||
|
df.get_column_names(),
|
||||||
|
&vec!["col_int".to_string(), "col_string".to_string()]
|
||||||
|
);
|
||||||
|
assert!(df.column_to_type.get("col_float").is_none());
|
||||||
|
assert!(!df.frames_by_type.contains_key(&TypeId::of::<f64>())); // Frame<f64> should be removed
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<i32>()));
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<String>()));
|
||||||
|
|
||||||
|
df.drop_column("col_int");
|
||||||
|
df.drop_column("col_string");
|
||||||
|
|
||||||
|
assert_eq!(df.cols(), 0);
|
||||||
|
assert!(df.get_column_names().is_empty());
|
||||||
|
assert!(df.frames_by_type.is_empty());
|
||||||
|
assert!(df.column_to_type.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic(expected = "DataFrame::drop_column: column 'non_existent' not found")]
|
||||||
|
fn test_dataframe_drop_column_non_existent() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int", vec![1, 2, 3]);
|
||||||
|
df.drop_column("non_existent");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dataframe_add_column_reuses_existing_frame() {
|
||||||
|
let mut df = DataFrame::new();
|
||||||
|
df.add_column("col_int1", vec![1, 2, 3]);
|
||||||
|
df.add_column("col_float1", vec![1.1, 2.2, 3.3]);
|
||||||
|
|
||||||
|
// Initially, there should be two frames (one for i32, one for f64)
|
||||||
|
assert_eq!(df.frames_by_type.len(), 2);
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<i32>()));
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<f64>()));
|
||||||
|
|
||||||
|
// Add another integer column
|
||||||
|
df.add_column("col_int2", vec![4, 5, 6]);
|
||||||
|
|
||||||
|
// The number of frames should still be 2, as the existing i32 frame should be reused
|
||||||
|
assert_eq!(df.frames_by_type.len(), 2);
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<i32>()));
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<f64>()));
|
||||||
|
|
||||||
|
// Verify the i32 frame now contains both integer columns
|
||||||
|
let int_frame = df.frames_by_type.get(&TypeId::of::<i32>()).unwrap().as_any().downcast_ref::<Frame<i32>>().unwrap();
|
||||||
|
assert_eq!(int_frame.columns(), &vec!["col_int1".to_string(), "col_int2".to_string()]);
|
||||||
|
assert_eq!(int_frame.cols(), 2);
|
||||||
|
|
||||||
|
// Add another float column
|
||||||
|
df.add_column("col_float2", vec![4.4, 5.5, 6.6]);
|
||||||
|
|
||||||
|
// The number of frames should still be 2, as the existing f64 frame should be reused
|
||||||
|
assert_eq!(df.frames_by_type.len(), 2);
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<i32>()));
|
||||||
|
assert!(df.frames_by_type.contains_key(&TypeId::of::<f64>()));
|
||||||
|
|
||||||
|
// Verify the f64 frame now contains both float columns
|
||||||
|
let float_frame = df.frames_by_type.get(&TypeId::of::<f64>()).unwrap().as_any().downcast_ref::<Frame<f64>>().unwrap();
|
||||||
|
assert_eq!(float_frame.columns(), &vec!["col_float1".to_string(), "col_float2".to_string()]);
|
||||||
|
assert_eq!(float_frame.cols(), 2);
|
||||||
|
}
|
||||||
|
}
|
4
src/dataframe/mod.rs
Normal file
4
src/dataframe/mod.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
//! This module provides the DataFrame structure for handling tabular data with mixed types.
|
||||||
|
pub mod df;
|
||||||
|
|
||||||
|
pub use df::{DataFrame, SubFrame};
|
@ -316,7 +316,7 @@ impl<T: Clone + PartialEq> Frame<T> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an immutable slice of the specified column's data.
|
/// Returns an immutable slice of the specified column's data by name.
|
||||||
/// Panics if the column name is not found.
|
/// Panics if the column name is not found.
|
||||||
pub fn column(&self, name: &str) -> &[T] {
|
pub fn column(&self, name: &str) -> &[T] {
|
||||||
let idx = self
|
let idx = self
|
||||||
@ -325,7 +325,13 @@ impl<T: Clone + PartialEq> Frame<T> {
|
|||||||
self.matrix.column(idx)
|
self.matrix.column(idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable slice of the specified column's data.
|
/// Returns an immutable slice of the specified column's data by its physical index.
|
||||||
|
/// Panics if the index is out of bounds.
|
||||||
|
pub fn column_by_physical_idx(&self, idx: usize) -> &[T] {
|
||||||
|
self.matrix.column(idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a mutable slice of the specified column's data by name.
|
||||||
/// Panics if the column name is not found.
|
/// Panics if the column name is not found.
|
||||||
pub fn column_mut(&mut self, name: &str) -> &mut [T] {
|
pub fn column_mut(&mut self, name: &str) -> &mut [T] {
|
||||||
let idx = self
|
let idx = self
|
||||||
@ -334,6 +340,12 @@ impl<T: Clone + PartialEq> Frame<T> {
|
|||||||
self.matrix.column_mut(idx)
|
self.matrix.column_mut(idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a mutable slice of the specified column's data by its physical index.
|
||||||
|
/// Panics if the index is out of bounds.
|
||||||
|
pub fn column_mut_by_physical_idx(&mut self, idx: usize) -> &mut [T] {
|
||||||
|
self.matrix.column_mut(idx)
|
||||||
|
}
|
||||||
|
|
||||||
// Row access methods
|
// Row access methods
|
||||||
|
|
||||||
/// Returns an immutable view of the row for the given integer key.
|
/// Returns an immutable view of the row for the given integer key.
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
|
|
||||||
|
/// Documentation for the [`crate::dataframe`] module.
|
||||||
|
pub mod dataframe;
|
||||||
|
|
||||||
/// Documentation for the [`crate::matrix`] module.
|
/// Documentation for the [`crate::matrix`] module.
|
||||||
pub mod matrix;
|
pub mod matrix;
|
||||||
|
|
||||||
|
@ -1028,9 +1028,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_from_rows_vec() {
|
fn test_from_rows_vec() {
|
||||||
// Representing:
|
// Matrix with rows [1, 2, 3] and [4, 5, 6]
|
||||||
// 1 2 3
|
|
||||||
// 4 5 6
|
|
||||||
let rows_data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
|
let rows_data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
|
||||||
let matrix = Matrix::from_rows_vec(rows_data, 2, 3);
|
let matrix = Matrix::from_rows_vec(rows_data, 2, 3);
|
||||||
|
|
||||||
@ -1042,19 +1040,14 @@ mod tests {
|
|||||||
|
|
||||||
// Helper function to create a basic Matrix for testing
|
// Helper function to create a basic Matrix for testing
|
||||||
fn static_test_matrix() -> Matrix<i32> {
|
fn static_test_matrix() -> Matrix<i32> {
|
||||||
// Column-major data:
|
// Column-major data representing a 3x3 matrix of sequential integers
|
||||||
// 1 4 7
|
|
||||||
// 2 5 8
|
|
||||||
// 3 6 9
|
|
||||||
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
|
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||||
Matrix::from_vec(data, 3, 3)
|
Matrix::from_vec(data, 3, 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Another helper for a different size
|
// Another helper for a different size
|
||||||
fn static_test_matrix_2x4() -> Matrix<i32> {
|
fn static_test_matrix_2x4() -> Matrix<i32> {
|
||||||
// Column-major data:
|
// Column-major data representing a 2x4 matrix of sequential integers
|
||||||
// 1 3 5 7
|
|
||||||
// 2 4 6 8
|
|
||||||
let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
|
let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
|
||||||
Matrix::from_vec(data, 2, 4)
|
Matrix::from_vec(data, 2, 4)
|
||||||
}
|
}
|
||||||
@ -1132,10 +1125,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_from_cols_basic() {
|
fn test_from_cols_basic() {
|
||||||
// Representing:
|
// Matrix with columns forming a 3x3 sequence
|
||||||
// 1 4 7
|
|
||||||
// 2 5 8
|
|
||||||
// 3 6 9
|
|
||||||
let cols_data = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
|
let cols_data = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
|
||||||
let matrix = Matrix::from_cols(cols_data);
|
let matrix = Matrix::from_cols(cols_data);
|
||||||
|
|
||||||
@ -1512,8 +1502,7 @@ mod tests {
|
|||||||
|
|
||||||
// Delete the first row
|
// Delete the first row
|
||||||
matrix.delete_row(0);
|
matrix.delete_row(0);
|
||||||
// Should be:
|
// Resulting data should be [3, 6, 9]
|
||||||
// 3 6 9
|
|
||||||
assert_eq!(matrix.rows(), 1);
|
assert_eq!(matrix.rows(), 1);
|
||||||
assert_eq!(matrix.cols(), 3);
|
assert_eq!(matrix.cols(), 3);
|
||||||
assert_eq!(matrix.data(), &[3, 6, 9]);
|
assert_eq!(matrix.data(), &[3, 6, 9]);
|
||||||
|
@ -215,20 +215,13 @@ mod tests {
|
|||||||
|
|
||||||
// Helper function to create a FloatMatrix for SeriesOps testing
|
// Helper function to create a FloatMatrix for SeriesOps testing
|
||||||
fn create_float_test_matrix() -> FloatMatrix {
|
fn create_float_test_matrix() -> FloatMatrix {
|
||||||
// 3x3 matrix (column-major) with some NaNs
|
// 3x3 column-major matrix containing a few NaN values
|
||||||
// 1.0 4.0 7.0
|
|
||||||
// 2.0 NaN 8.0
|
|
||||||
// 3.0 6.0 NaN
|
|
||||||
let data = vec![1.0, 2.0, 3.0, 4.0, f64::NAN, 6.0, 7.0, 8.0, f64::NAN];
|
let data = vec![1.0, 2.0, 3.0, 4.0, f64::NAN, 6.0, 7.0, 8.0, f64::NAN];
|
||||||
FloatMatrix::from_vec(data, 3, 3)
|
FloatMatrix::from_vec(data, 3, 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_float_test_matrix_4x4() -> FloatMatrix {
|
fn create_float_test_matrix_4x4() -> FloatMatrix {
|
||||||
// 4x4 matrix (column-major) with some NaNs
|
// 4x4 column-major matrix with NaNs inserted at positions where index % 5 == 0
|
||||||
// 1.0 5.0 9.0 13.0
|
|
||||||
// 2.0 NaN 10.0 NaN
|
|
||||||
// 3.0 6.0 NaN 14.0
|
|
||||||
// NaN 7.0 11.0 NaN
|
|
||||||
// first make array with 16 elements
|
// first make array with 16 elements
|
||||||
FloatMatrix::from_vec(
|
FloatMatrix::from_vec(
|
||||||
(0..16)
|
(0..16)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user