mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-11-19 15:46:10 +00:00
Compare commits
67 Commits
927f4af5f9
...
csv
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef25e77f04 | ||
|
|
4ba5cfea18 | ||
|
|
23367c7ca3 | ||
|
|
df8c1d2a12 | ||
|
|
1381c77eaf | ||
| c56574f0f3 | |||
| c53693fa7b | |||
| 109d39b248 | |||
|
|
18ad6c689a | ||
| 1fead78b69 | |||
|
|
6fb32e743c | ||
| 2cb4e46217 | |||
|
|
a53ba63f30 | ||
|
|
dae60ea1bd | ||
|
|
755dee58e7 | ||
|
|
9e6e22fc37 | ||
|
|
b687fd4e6b | ||
|
|
68a01ab528 | ||
|
|
23a01dab07 | ||
|
|
f4ebd78234 | ||
|
|
1475156855 | ||
|
|
080680d095 | ||
|
|
2845f357b7 | ||
|
|
3d11226d57 | ||
|
|
039fb1a98e | ||
|
|
31a5ba2460 | ||
|
|
1a9f397702 | ||
|
|
ecd06eb352 | ||
|
|
ae327b6060 | ||
|
|
83ac9d4821 | ||
|
|
ae27ed9373 | ||
|
|
c7552f2264 | ||
|
|
3654c7053c | ||
|
|
1dcd9727b4 | ||
|
|
b62152b4f0 | ||
|
|
a6a901d6ab | ||
|
|
676af850ef | ||
|
|
ca2ca2a738 | ||
|
|
4876a74e01 | ||
|
|
b78dd75e77 | ||
|
|
9db8853d75 | ||
|
|
9738154dac | ||
| 7d0978e5fb | |||
|
|
ed01c4b8f2 | ||
|
|
e6964795e3 | ||
|
|
d1dd7ea6d2 | ||
|
|
676f78bb1e | ||
|
|
f7325a9558 | ||
|
|
18b9eef063 | ||
|
|
f99f78d508 | ||
| cd3aa84e60 | |||
| 27275e2479 | |||
| 9ef719316a | |||
| 960fd345c2 | |||
| 325e75419c | |||
| b1dc18d05b | |||
| 8cbb957764 | |||
| b937ed1cdf | |||
| 2e071a6974 | |||
| 689169bab2 | |||
| a45a5ecf4e | |||
| 84e1b423f4 | |||
| 197739bc2f | |||
| d2c2ebca0f | |||
| f5f3f2c100 | |||
| 9fcb1ea2cf | |||
|
|
623303cf72 |
11
.github/htmldocs/index.html
vendored
11
.github/htmldocs/index.html
vendored
@@ -58,6 +58,14 @@
|
||||
<h2>A lightweight dataframe & math toolkit for Rust</h2>
|
||||
<hr style="border: 1px solid #d4d4d4; margin: 20px 0;">
|
||||
<p>
|
||||
|
||||
🐙 <a href="https://github.com/Magnus167/rustframe">GitHub</a>
|
||||
<br><br>
|
||||
|
||||
📖 <a href="https://magnus167.github.io/rustframe/user-guide">User Guide</a>
|
||||
<br><br>
|
||||
|
||||
|
||||
📚 <a href="https://magnus167.github.io/rustframe/docs">Docs</a> |
|
||||
📊 <a href="https://magnus167.github.io/rustframe/benchmark-report/">Benchmarks</a>
|
||||
|
||||
@@ -65,8 +73,7 @@
|
||||
🦀 <a href="https://crates.io/crates/rustframe">Crates.io</a> |
|
||||
🔖 <a href="https://docs.rs/rustframe/latest/rustframe/">docs.rs</a>
|
||||
<br><br>
|
||||
🐙 <a href="https://github.com/Magnus167/rustframe">GitHub</a> |
|
||||
🌐 <a href="https://gitea.nulltech.uk/Magnus167/rustframe">Gitea mirror</a>
|
||||
<!-- 🌐 <a href="https://gitea.nulltech.uk/Magnus167/rustframe">Gitea mirror</a> -->
|
||||
</p>
|
||||
</main>
|
||||
</body>
|
||||
|
||||
21
.github/workflows/docs-and-testcov.yml
vendored
21
.github/workflows/docs-and-testcov.yml
vendored
@@ -153,7 +153,6 @@ jobs:
|
||||
|
||||
echo "<meta http-equiv=\"refresh\" content=\"0; url=../docs/index.html\">" > target/doc/rustframe/index.html
|
||||
|
||||
mkdir output
|
||||
cp tarpaulin-report.html target/doc/docs/
|
||||
cp tarpaulin-report.json target/doc/docs/
|
||||
cp tarpaulin-badge.json target/doc/docs/
|
||||
@@ -166,16 +165,30 @@ jobs:
|
||||
# copy the benchmark report to the output directory
|
||||
cp -r benchmark-report target/doc/
|
||||
|
||||
mkdir output
|
||||
cp -r target/doc/* output/
|
||||
|
||||
- name: Build user guide
|
||||
run: |
|
||||
cargo binstall mdbook
|
||||
bash ./docs/build.sh
|
||||
|
||||
- name: Copy user guide to output directory
|
||||
run: |
|
||||
mkdir output/user-guide
|
||||
cp -r docs/book/* output/user-guide/
|
||||
|
||||
- name: Add index.html to output directory
|
||||
run: |
|
||||
cp .github/htmldocs/index.html target/doc/index.html
|
||||
cp .github/rustframe_logo.png target/doc/rustframe_logo.png
|
||||
cp .github/htmldocs/index.html output/index.html
|
||||
cp .github/rustframe_logo.png output/rustframe_logo.png
|
||||
|
||||
- name: Upload Pages artifact
|
||||
# if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
|
||||
uses: actions/upload-pages-artifact@v3
|
||||
with:
|
||||
path: target/doc/
|
||||
# path: target/doc/
|
||||
path: output/
|
||||
|
||||
- name: Deploy to GitHub Pages
|
||||
# if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
|
||||
|
||||
5
.github/workflows/run-unit-tests.yml
vendored
5
.github/workflows/run-unit-tests.yml
vendored
@@ -78,3 +78,8 @@ jobs:
|
||||
uses: codecov/test-results-action@v1
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
|
||||
- name: Test build user guide
|
||||
run: |
|
||||
cargo binstall mdbook
|
||||
bash ./docs/build.sh
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -17,3 +17,5 @@ data/
|
||||
tarpaulin-report.*
|
||||
|
||||
.github/htmldocs/rustframe_logo.png
|
||||
|
||||
docs/book/
|
||||
@@ -1,11 +1,12 @@
|
||||
[package]
|
||||
name = "rustframe"
|
||||
authors = ["Palash Tyagi (https://github.com/Magnus167)"]
|
||||
version = "0.0.1-a.20250716"
|
||||
version = "0.0.1-a.20250805"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
readme = "README.md"
|
||||
description = "A simple dataframe library"
|
||||
description = "A simple dataframe and math toolkit"
|
||||
documentation = "https://magnus167.github.io/rustframe/"
|
||||
|
||||
[lib]
|
||||
name = "rustframe"
|
||||
|
||||
18
README.md
18
README.md
@@ -1,11 +1,12 @@
|
||||
# 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/)
|
||||
🐙 [GitHub](https://github.com/Magnus167/rustframe) | 📚 [Docs](https://magnus167.github.io/rustframe/) | 📖 [User Guide](https://magnus167.github.io/rustframe/user-guide/) | 🦀 [Crates.io](https://crates.io/crates/rustframe) | 🔖 [docs.rs](https://docs.rs/rustframe/latest/rustframe/)
|
||||
|
||||
<!-- [](https://github.com/Magnus167/rustframe) -->
|
||||
|
||||
[](https://codecov.io/gh/Magnus167/rustframe)
|
||||
[](https://magnus167.github.io/rustframe/docs/tarpaulin-report.html)
|
||||
[](https://gitea.nulltech.uk/Magnus167/rustframe)
|
||||
|
||||
---
|
||||
|
||||
@@ -152,7 +153,7 @@ let zipped_matrix = a.zip(&b, |x, y| x + y);
|
||||
assert_eq!(zipped_matrix.data(), &[6.0, 8.0, 10.0, 12.0]);
|
||||
```
|
||||
|
||||
### More examples
|
||||
## More examples
|
||||
|
||||
See the [examples](./examples/) directory for some demonstrations of Rustframe's syntax and functionality.
|
||||
|
||||
@@ -191,10 +192,21 @@ cargo run --example
|
||||
|
||||
Each demo runs a couple of mini-scenarios showcasing the APIs.
|
||||
|
||||
### Running benchmarks
|
||||
## Running benchmarks
|
||||
|
||||
To run the benchmarks, use:
|
||||
|
||||
```bash
|
||||
cargo bench --features "bench"
|
||||
```
|
||||
|
||||
## Building the user-guide
|
||||
|
||||
To build the user guide, use:
|
||||
|
||||
```bash
|
||||
cargo binstall mdbook
|
||||
bash docs/build.sh
|
||||
```
|
||||
|
||||
This will generate the user guide in the `docs/book` directory.
|
||||
|
||||
7
docs/book.toml
Normal file
7
docs/book.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[book]
|
||||
title = "Rustframe User Guide"
|
||||
authors = ["Palash Tyagi (https://github.com/Magnus167)"]
|
||||
description = "Guided journey through Rustframe capabilities."
|
||||
|
||||
[build]
|
||||
build-dir = "book"
|
||||
7
docs/build.sh
Executable file
7
docs/build.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env sh
|
||||
# Build and test the Rustframe user guide using mdBook.
|
||||
set -e
|
||||
|
||||
cd docs
|
||||
bash gen.sh "$@"
|
||||
cd ..
|
||||
14
docs/gen.sh
Normal file
14
docs/gen.sh
Normal file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
set -e
|
||||
|
||||
cargo clean
|
||||
|
||||
cargo build --manifest-path ../Cargo.toml
|
||||
|
||||
mdbook test -L ../target/debug/deps "$@"
|
||||
|
||||
mdbook build "$@"
|
||||
|
||||
cargo build
|
||||
# cargo build --release
|
||||
7
docs/src/SUMMARY.md
Normal file
7
docs/src/SUMMARY.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Summary
|
||||
|
||||
- [Introduction](./introduction.md)
|
||||
- [Data Manipulation](./data-manipulation.md)
|
||||
- [Compute Features](./compute.md)
|
||||
- [Machine Learning](./machine-learning.md)
|
||||
- [Utilities](./utilities.md)
|
||||
222
docs/src/compute.md
Normal file
222
docs/src/compute.md
Normal file
@@ -0,0 +1,222 @@
|
||||
# Compute Features
|
||||
|
||||
The `compute` module hosts numerical routines for exploratory data analysis.
|
||||
It covers descriptive statistics, correlations, probability distributions and
|
||||
some basic inferential tests.
|
||||
|
||||
## Basic Statistics
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::{mean, mean_horizontal, mean_vertical, stddev, median, population_variance, percentile};
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let m = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
assert_eq!(mean(&m), 2.5);
|
||||
assert_eq!(stddev(&m), 1.118033988749895);
|
||||
assert_eq!(median(&m), 2.5);
|
||||
assert_eq!(population_variance(&m), 1.25);
|
||||
assert_eq!(percentile(&m, 50.0), 3.0);
|
||||
// column averages returned as 1 x n matrix
|
||||
let row_means = mean_horizontal(&m);
|
||||
assert_eq!(row_means.data(), &[2.0, 3.0]);
|
||||
let col_means = mean_vertical(&m);
|
||||
assert_eq!(col_means.data(), & [1.5, 3.5]);
|
||||
```
|
||||
|
||||
### Axis-specific Operations
|
||||
|
||||
Operations can be applied along specific axes (rows or columns):
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::{mean_vertical, mean_horizontal, stddev_vertical, stddev_horizontal};
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
// 3x2 matrix
|
||||
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2);
|
||||
|
||||
// Mean along columns (vertical) - returns 1 x cols matrix
|
||||
let col_means = mean_vertical(&m);
|
||||
assert_eq!(col_means.shape(), (1, 2));
|
||||
assert_eq!(col_means.data(), &[3.0, 4.0]); // [(1+3+5)/3, (2+4+6)/3]
|
||||
|
||||
// Mean along rows (horizontal) - returns rows x 1 matrix
|
||||
let row_means = mean_horizontal(&m);
|
||||
assert_eq!(row_means.shape(), (3, 1));
|
||||
assert_eq!(row_means.data(), &[1.5, 3.5, 5.5]); // [(1+2)/2, (3+4)/2, (5+6)/2]
|
||||
|
||||
// Standard deviation along columns
|
||||
let col_stddev = stddev_vertical(&m);
|
||||
assert_eq!(col_stddev.shape(), (1, 2));
|
||||
|
||||
// Standard deviation along rows
|
||||
let row_stddev = stddev_horizontal(&m);
|
||||
assert_eq!(row_stddev.shape(), (3, 1));
|
||||
```
|
||||
|
||||
## Correlation
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::{pearson, covariance};
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
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 corr = pearson(&x, &y);
|
||||
let cov = covariance(&x, &y);
|
||||
assert!((corr - 1.0).abs() < 1e-8);
|
||||
assert!((cov - 2.5).abs() < 1e-8);
|
||||
```
|
||||
|
||||
## Covariance
|
||||
|
||||
### `covariance`
|
||||
|
||||
Computes the population covariance between two equally sized matrices by flattening
|
||||
their values.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::covariance;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
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 cov = covariance(&x, &y);
|
||||
assert!((cov - 2.5).abs() < 1e-8);
|
||||
```
|
||||
|
||||
### `covariance_vertical`
|
||||
|
||||
Evaluates covariance between columns (i.e. across rows) and returns a matrix of
|
||||
column pair covariances.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::covariance_vertical;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
let cov = covariance_vertical(&m);
|
||||
assert_eq!(cov.shape(), (2, 2));
|
||||
assert!(cov.data().iter().all(|&v| (v - 1.0).abs() < 1e-8));
|
||||
```
|
||||
|
||||
### `covariance_horizontal`
|
||||
|
||||
Computes covariance between rows (i.e. across columns) returning a matrix that
|
||||
describes how each pair of rows varies together.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::covariance_horizontal;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
let cov = covariance_horizontal(&m);
|
||||
assert_eq!(cov.shape(), (2, 2));
|
||||
assert!(cov.data().iter().all(|&v| (v - 0.25).abs() < 1e-8));
|
||||
```
|
||||
|
||||
### `covariance_matrix`
|
||||
|
||||
Builds a covariance matrix either between columns (`Axis::Col`) or rows
|
||||
(`Axis::Row`). Each entry represents how two series co-vary.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::covariance_matrix;
|
||||
use rustframe::matrix::{Axis, Matrix};
|
||||
|
||||
let data = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
|
||||
// Covariance between columns
|
||||
let cov_cols = covariance_matrix(&data, Axis::Col);
|
||||
assert!((cov_cols.get(0, 0) - 2.0).abs() < 1e-8);
|
||||
|
||||
// Covariance between rows
|
||||
let cov_rows = covariance_matrix(&data, Axis::Row);
|
||||
assert!((cov_rows.get(0, 1) + 0.5).abs() < 1e-8);
|
||||
```
|
||||
|
||||
## Distributions
|
||||
|
||||
Probability distribution helpers are available for common PDFs and CDFs.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::distributions::normal_pdf;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let x = Matrix::from_vec(vec![0.0, 1.0], 1, 2);
|
||||
let pdf = normal_pdf(x, 0.0, 1.0);
|
||||
assert_eq!(pdf.data().len(), 2);
|
||||
```
|
||||
|
||||
### Additional Distributions
|
||||
|
||||
Rustframe provides several other probability distributions:
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::stats::distributions::{normal_cdf, binomial_pmf, binomial_cdf, poisson_pmf};
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
// Normal distribution CDF
|
||||
let x = Matrix::from_vec(vec![0.0, 1.0], 1, 2);
|
||||
let cdf = normal_cdf(x, 0.0, 1.0);
|
||||
assert_eq!(cdf.data().len(), 2);
|
||||
|
||||
// Binomial distribution PMF
|
||||
// Probability of k successes in n trials with probability p
|
||||
let k = Matrix::from_vec(vec![0_u64, 1, 2, 3], 1, 4);
|
||||
let pmf = binomial_pmf(3, k.clone(), 0.5);
|
||||
assert_eq!(pmf.data().len(), 4);
|
||||
|
||||
// Binomial distribution CDF
|
||||
let cdf = binomial_cdf(3, k, 0.5);
|
||||
assert_eq!(cdf.data().len(), 4);
|
||||
|
||||
// Poisson distribution PMF
|
||||
// Probability of k events with rate parameter lambda
|
||||
let k = Matrix::from_vec(vec![0_u64, 1, 2], 1, 3);
|
||||
let pmf = poisson_pmf(2.0, k);
|
||||
assert_eq!(pmf.data().len(), 3);
|
||||
```
|
||||
|
||||
### Inferential Statistics
|
||||
|
||||
Rustframe provides several inferential statistical tests:
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::matrix::Matrix;
|
||||
use rustframe::compute::stats::inferential::{t_test, chi2_test, anova};
|
||||
|
||||
// Two-sample t-test
|
||||
let sample1 = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], 1, 5);
|
||||
let sample2 = Matrix::from_vec(vec![6.0, 7.0, 8.0, 9.0, 10.0], 1, 5);
|
||||
let (t_statistic, p_value) = t_test(&sample1, &sample2);
|
||||
assert!((t_statistic + 5.0).abs() < 1e-5);
|
||||
assert!(p_value > 0.0 && p_value < 1.0);
|
||||
|
||||
// Chi-square test of independence
|
||||
let observed = Matrix::from_vec(vec![12.0, 5.0, 8.0, 10.0], 2, 2);
|
||||
let (chi2_statistic, p_value) = chi2_test(&observed);
|
||||
assert!(chi2_statistic > 0.0);
|
||||
assert!(p_value > 0.0 && p_value < 1.0);
|
||||
|
||||
// One-way ANOVA
|
||||
let group1 = Matrix::from_vec(vec![1.0, 2.0, 3.0], 1, 3);
|
||||
let group2 = Matrix::from_vec(vec![2.0, 3.0, 4.0], 1, 3);
|
||||
let group3 = Matrix::from_vec(vec![3.0, 4.0, 5.0], 1, 3);
|
||||
let groups = vec![&group1, &group2, &group3];
|
||||
let (f_statistic, p_value) = anova(groups);
|
||||
assert!(f_statistic > 0.0);
|
||||
assert!(p_value > 0.0 && p_value < 1.0);
|
||||
```
|
||||
|
||||
With the basics covered, explore predictive models in the
|
||||
[machine learning](./machine-learning.md) chapter.
|
||||
157
docs/src/data-manipulation.md
Normal file
157
docs/src/data-manipulation.md
Normal file
@@ -0,0 +1,157 @@
|
||||
# Data Manipulation
|
||||
|
||||
Rustframe's `Frame` type couples tabular data with
|
||||
column labels and a typed row index. Frames expose a familiar API for loading
|
||||
data, selecting rows or columns and performing aggregations.
|
||||
|
||||
## Creating a Frame
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::frame::{Frame, RowIndex};
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let data = Matrix::from_cols(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
|
||||
let frame = Frame::new(data, vec!["A", "B"], None);
|
||||
assert_eq!(frame["A"], vec![1.0, 2.0]);
|
||||
```
|
||||
|
||||
## Indexing Rows
|
||||
|
||||
Row labels can be integers, dates or a default range. Retrieving a row returns a
|
||||
view that lets you inspect values by column name or position.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
# extern crate chrono;
|
||||
use chrono::NaiveDate;
|
||||
use rustframe::frame::{Frame, RowIndex};
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let d = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
|
||||
let data = Matrix::from_cols(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
|
||||
let index = RowIndex::Date(vec![d(2024, 1, 1), d(2024, 1, 2)]);
|
||||
let mut frame = Frame::new(data, vec!["A", "B"], Some(index));
|
||||
assert_eq!(frame.get_row_date(d(2024, 1, 2))["B"], 4.0);
|
||||
|
||||
// mutate by row key
|
||||
frame.get_row_date_mut(d(2024, 1, 1)).set_by_index(0, 9.0);
|
||||
assert_eq!(frame.get_row_date(d(2024, 1, 1))["A"], 9.0);
|
||||
```
|
||||
|
||||
## Column operations
|
||||
|
||||
Columns can be inserted, renamed, removed or reordered in place.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::frame::{Frame, RowIndex};
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let data = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]);
|
||||
let mut frame = Frame::new(data, vec!["X", "Y"], Some(RowIndex::Range(0..2)));
|
||||
|
||||
frame.add_column("Z", vec![5, 6]);
|
||||
frame.rename("Y", "W");
|
||||
let removed = frame.delete_column("X");
|
||||
assert_eq!(removed, vec![1, 2]);
|
||||
frame.sort_columns();
|
||||
assert_eq!(frame.columns(), &["W", "Z"]);
|
||||
```
|
||||
|
||||
## Aggregations
|
||||
|
||||
Any numeric aggregation available on `Matrix` is forwarded to `Frame`.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::frame::Frame;
|
||||
use rustframe::matrix::{Matrix, SeriesOps};
|
||||
|
||||
let frame = Frame::new(Matrix::from_cols(vec![vec![1.0, 2.0], vec![3.0, 4.0]]), vec!["A", "B"], None);
|
||||
assert_eq!(frame.sum_vertical(), vec![3.0, 7.0]);
|
||||
assert_eq!(frame.sum_horizontal(), vec![4.0, 6.0]);
|
||||
```
|
||||
|
||||
## Matrix Operations
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let data1 = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
let data2 = Matrix::from_vec(vec![5.0, 6.0, 7.0, 8.0], 2, 2);
|
||||
|
||||
let sum = data1.clone() + data2.clone();
|
||||
assert_eq!(sum.data(), vec![6.0, 8.0, 10.0, 12.0]);
|
||||
|
||||
let product = data1.clone() * data2.clone();
|
||||
assert_eq!(product.data(), vec![5.0, 12.0, 21.0, 32.0]);
|
||||
|
||||
let scalar_product = data1.clone() * 2.0;
|
||||
assert_eq!(scalar_product.data(), vec![2.0, 4.0, 6.0, 8.0]);
|
||||
|
||||
let equals = data1 == data1.clone();
|
||||
assert_eq!(equals, true);
|
||||
```
|
||||
|
||||
### Advanced Matrix Operations
|
||||
|
||||
Matrices support a variety of advanced operations:
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::matrix::{Matrix, SeriesOps};
|
||||
|
||||
// Matrix multiplication (dot product)
|
||||
let a = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
let b = Matrix::from_vec(vec![5.0, 6.0, 7.0, 8.0], 2, 2);
|
||||
let product = a.matrix_mul(&b);
|
||||
assert_eq!(product.data(), vec![23.0, 34.0, 31.0, 46.0]);
|
||||
|
||||
// Transpose
|
||||
let m = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
let transposed = m.transpose();
|
||||
assert_eq!(transposed.data(), vec![1.0, 3.0, 2.0, 4.0]);
|
||||
|
||||
// Map function over all elements
|
||||
let m = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
let squared = m.map(|x| x * x);
|
||||
assert_eq!(squared.data(), vec![1.0, 4.0, 9.0, 16.0]);
|
||||
|
||||
// Zip two matrices with a function
|
||||
let a = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
let b = Matrix::from_vec(vec![5.0, 6.0, 7.0, 8.0], 2, 2);
|
||||
let zipped = a.zip(&b, |x, y| x + y);
|
||||
assert_eq!(zipped.data(), vec![6.0, 8.0, 10.0, 12.0]);
|
||||
```
|
||||
|
||||
### Matrix Reductions
|
||||
|
||||
Matrices support various reduction operations:
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::matrix::{Matrix, SeriesOps};
|
||||
|
||||
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2);
|
||||
|
||||
// Sum along columns (vertical)
|
||||
let col_sums = m.sum_vertical();
|
||||
assert_eq!(col_sums, vec![9.0, 12.0]); // [1+3+5, 2+4+6]
|
||||
|
||||
// Sum along rows (horizontal)
|
||||
let row_sums = m.sum_horizontal();
|
||||
assert_eq!(row_sums, vec![3.0, 7.0, 11.0]); // [1+2, 3+4, 5+6]
|
||||
|
||||
// Cumulative sum along columns
|
||||
let col_cumsum = m.cumsum_vertical();
|
||||
assert_eq!(col_cumsum.data(), vec![1.0, 4.0, 9.0, 2.0, 6.0, 12.0]);
|
||||
|
||||
// Cumulative sum along rows
|
||||
let row_cumsum = m.cumsum_horizontal();
|
||||
assert_eq!(row_cumsum.data(), vec![1.0, 3.0, 5.0, 3.0, 7.0, 11.0]);
|
||||
```
|
||||
|
||||
With the basics covered, continue to the [compute features](./compute.md)
|
||||
chapter for statistics and analytics.
|
||||
40
docs/src/introduction.md
Normal file
40
docs/src/introduction.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# Introduction
|
||||
|
||||
🐙 [GitHub](https://github.com/Magnus167/rustframe) | 📚 [Docs](https://magnus167.github.io/rustframe/) | 📖 [User Guide](https://magnus167.github.io/rustframe/user-guide/) | 🦀 [Crates.io](https://crates.io/crates/rustframe) | 🔖 [docs.rs](https://docs.rs/rustframe/latest/rustframe/)
|
||||
|
||||
Welcome to the **Rustframe User Guide**. Rustframe is a lightweight dataframe
|
||||
and math toolkit for Rust written in 100% safe Rust. It focuses on keeping the
|
||||
API approachable while offering handy features for small analytical or
|
||||
educational projects.
|
||||
|
||||
Rustframe bundles:
|
||||
|
||||
- column‑labelled frames built on a fast column‑major matrix
|
||||
- familiar element‑wise math and aggregation routines
|
||||
- a growing `compute` module for statistics and machine learning
|
||||
- utilities for dates and random numbers
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::{frame::Frame, matrix::{Matrix, SeriesOps}};
|
||||
|
||||
let data = Matrix::from_cols(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
|
||||
let frame = Frame::new(data, vec!["A", "B"], None);
|
||||
|
||||
// Perform column wise aggregation
|
||||
assert_eq!(frame.sum_vertical(), vec![3.0, 7.0]);
|
||||
```
|
||||
|
||||
## Resources
|
||||
|
||||
- [GitHub repository](https://github.com/Magnus167/rustframe)
|
||||
- [Crates.io](https://crates.io/crates/rustframe) & [API docs](https://docs.rs/rustframe)
|
||||
- [Code coverage](https://codecov.io/gh/Magnus167/rustframe)
|
||||
|
||||
This guide walks through the main building blocks of the library. Each chapter
|
||||
contains runnable snippets so you can follow along:
|
||||
|
||||
1. [Data manipulation](./data-manipulation.md) for loading and transforming data
|
||||
2. [Compute features](./compute.md) for statistics and analytics
|
||||
3. [Machine learning](./machine-learning.md) for predictive models
|
||||
4. [Utilities](./utilities.md) for supporting helpers and upcoming modules
|
||||
282
docs/src/machine-learning.md
Normal file
282
docs/src/machine-learning.md
Normal file
@@ -0,0 +1,282 @@
|
||||
# Machine Learning
|
||||
|
||||
The `compute::models` module bundles several learning algorithms that operate on
|
||||
`Matrix` structures. These examples highlight the basic training and prediction
|
||||
APIs. For more end‑to‑end walkthroughs see the examples directory in the
|
||||
repository.
|
||||
|
||||
Currently implemented models include:
|
||||
|
||||
- Linear and logistic regression
|
||||
- K‑means clustering
|
||||
- Principal component analysis (PCA)
|
||||
- Gaussian Naive Bayes
|
||||
- Dense neural networks
|
||||
|
||||
## Linear Regression
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::models::linreg::LinReg;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let x = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 4, 1);
|
||||
let y = Matrix::from_vec(vec![2.0, 3.0, 4.0, 5.0], 4, 1);
|
||||
let mut model = LinReg::new(1);
|
||||
model.fit(&x, &y, 0.01, 100);
|
||||
let preds = model.predict(&x);
|
||||
assert_eq!(preds.rows(), 4);
|
||||
```
|
||||
|
||||
## K-means Walkthrough
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::models::k_means::KMeans;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let data = Matrix::from_vec(vec![1.0, 1.0, 5.0, 5.0], 2, 2);
|
||||
let (model, _labels) = KMeans::fit(&data, 2, 10, 1e-4);
|
||||
let new_point = Matrix::from_vec(vec![0.0, 0.0], 1, 2);
|
||||
let cluster = model.predict(&new_point)[0];
|
||||
```
|
||||
|
||||
## Logistic Regression
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::models::logreg::LogReg;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let x = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 4, 1);
|
||||
let y = Matrix::from_vec(vec![0.0, 0.0, 1.0, 1.0], 4, 1);
|
||||
let mut model = LogReg::new(1);
|
||||
model.fit(&x, &y, 0.1, 200);
|
||||
let preds = model.predict_proba(&x);
|
||||
assert_eq!(preds.rows(), 4);
|
||||
```
|
||||
|
||||
## Principal Component Analysis
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::models::pca::PCA;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
let data = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
let pca = PCA::fit(&data, 1, 0);
|
||||
let transformed = pca.transform(&data);
|
||||
assert_eq!(transformed.cols(), 1);
|
||||
```
|
||||
|
||||
## Gaussian Naive Bayes
|
||||
|
||||
Gaussian Naive Bayes classifier for continuous features:
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::models::gaussian_nb::GaussianNB;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
// Training data with 2 features
|
||||
let x = Matrix::from_rows_vec(vec![
|
||||
1.0, 2.0,
|
||||
2.0, 3.0,
|
||||
3.0, 4.0,
|
||||
4.0, 5.0
|
||||
], 4, 2);
|
||||
|
||||
// Class labels (0 or 1)
|
||||
let y = Matrix::from_vec(vec![0.0, 0.0, 1.0, 1.0], 4, 1);
|
||||
|
||||
// Train the model
|
||||
let mut model = GaussianNB::new(1e-9, true);
|
||||
model.fit(&x, &y);
|
||||
|
||||
// Make predictions
|
||||
let predictions = model.predict(&x);
|
||||
assert_eq!(predictions.rows(), 4);
|
||||
```
|
||||
|
||||
## Dense Neural Networks
|
||||
|
||||
Simple fully connected neural network:
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::models::dense_nn::{DenseNN, DenseNNConfig, ActivationKind, InitializerKind, LossKind};
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
// Training data with 2 features
|
||||
let x = Matrix::from_rows_vec(vec![
|
||||
0.0, 0.0,
|
||||
0.0, 1.0,
|
||||
1.0, 0.0,
|
||||
1.0, 1.0
|
||||
], 4, 2);
|
||||
|
||||
// XOR target outputs
|
||||
let y = Matrix::from_vec(vec![0.0, 1.0, 1.0, 0.0], 4, 1);
|
||||
|
||||
// Create a neural network with 2 hidden layers
|
||||
let config = DenseNNConfig {
|
||||
input_size: 2,
|
||||
hidden_layers: vec![4, 4],
|
||||
output_size: 1,
|
||||
activations: vec![ActivationKind::Sigmoid, ActivationKind::Sigmoid, ActivationKind::Sigmoid],
|
||||
initializer: InitializerKind::Uniform(0.5),
|
||||
loss: LossKind::MSE,
|
||||
learning_rate: 0.1,
|
||||
epochs: 1000,
|
||||
};
|
||||
let mut model = DenseNN::new(config);
|
||||
|
||||
// Train the model
|
||||
model.train(&x, &y);
|
||||
|
||||
// Make predictions
|
||||
let predictions = model.predict(&x);
|
||||
assert_eq!(predictions.rows(), 4);
|
||||
```
|
||||
|
||||
## Real-world Examples
|
||||
|
||||
### Housing Price Prediction
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::models::linreg::LinReg;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
// Features: square feet and bedrooms
|
||||
let features = Matrix::from_rows_vec(vec![
|
||||
2100.0, 3.0,
|
||||
1600.0, 2.0,
|
||||
2400.0, 4.0,
|
||||
1400.0, 2.0,
|
||||
], 4, 2);
|
||||
|
||||
// Sale prices
|
||||
let target = Matrix::from_vec(vec![400_000.0, 330_000.0, 369_000.0, 232_000.0], 4, 1);
|
||||
|
||||
let mut model = LinReg::new(2);
|
||||
model.fit(&features, &target, 1e-8, 10_000);
|
||||
|
||||
// Predict price of a new home
|
||||
let new_home = Matrix::from_vec(vec![2000.0, 3.0], 1, 2);
|
||||
let predicted_price = model.predict(&new_home);
|
||||
println!("Predicted price: ${}", predicted_price.data()[0]);
|
||||
```
|
||||
|
||||
### Spam Detection
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::models::logreg::LogReg;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
// 20 e-mails × 5 features = 100 numbers (row-major, spam first)
|
||||
let x = Matrix::from_rows_vec(
|
||||
vec![
|
||||
// ─────────── spam examples ───────────
|
||||
2.0, 1.0, 1.0, 1.0, 1.0, // "You win a FREE offer - click for money-back bonus!"
|
||||
1.0, 0.0, 1.0, 1.0, 0.0, // "FREE offer! Click now!"
|
||||
0.0, 2.0, 0.0, 1.0, 1.0, // "Win win win - money inside, click…"
|
||||
1.0, 1.0, 0.0, 0.0, 1.0, // "Limited offer to win easy money…"
|
||||
1.0, 0.0, 1.0, 0.0, 1.0, // ...
|
||||
0.0, 1.0, 1.0, 1.0, 0.0, // ...
|
||||
2.0, 0.0, 0.0, 1.0, 1.0, // ...
|
||||
0.0, 1.0, 1.0, 0.0, 1.0, // ...
|
||||
1.0, 1.0, 1.0, 1.0, 0.0, // ...
|
||||
1.0, 0.0, 0.0, 1.0, 1.0, // ...
|
||||
// ─────────── ham examples ───────────
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, // "See you at the meeting tomorrow."
|
||||
0.0, 0.0, 0.0, 1.0, 0.0, // "Here's the Zoom click-link."
|
||||
0.0, 0.0, 0.0, 0.0, 1.0, // "Expense report: money attached."
|
||||
0.0, 0.0, 0.0, 1.0, 1.0, // ...
|
||||
0.0, 1.0, 0.0, 0.0, 0.0, // "Did we win the bid?"
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, // ...
|
||||
0.0, 0.0, 0.0, 1.0, 0.0, // ...
|
||||
1.0, 0.0, 0.0, 0.0, 0.0, // "Special offer for staff lunch."
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, // ...
|
||||
0.0, 0.0, 0.0, 1.0, 0.0,
|
||||
],
|
||||
20,
|
||||
5,
|
||||
);
|
||||
|
||||
// Labels: 1 = spam, 0 = ham
|
||||
let y = Matrix::from_vec(
|
||||
vec![
|
||||
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, // 10 spam
|
||||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, // 10 ham
|
||||
],
|
||||
20,
|
||||
1,
|
||||
);
|
||||
|
||||
// Train
|
||||
let mut model = LogReg::new(5);
|
||||
model.fit(&x, &y, 0.01, 5000);
|
||||
|
||||
// Predict
|
||||
// e.g. "free money offer"
|
||||
let email_data = vec![1.0, 0.0, 1.0, 0.0, 1.0];
|
||||
let email = Matrix::from_vec(email_data, 1, 5);
|
||||
let prob_spam = model.predict_proba(&email);
|
||||
println!("Probability of spam: {:.4}", prob_spam.data()[0]);
|
||||
```
|
||||
|
||||
### Iris Flower Classification
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::models::gaussian_nb::GaussianNB;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
// Features: sepal length and petal length
|
||||
let x = Matrix::from_rows_vec(vec![
|
||||
5.1, 1.4, // setosa
|
||||
4.9, 1.4, // setosa
|
||||
6.2, 4.5, // versicolor
|
||||
5.9, 5.1, // virginica
|
||||
], 4, 2);
|
||||
|
||||
let y = Matrix::from_vec(vec![0.0, 0.0, 1.0, 2.0], 4, 1);
|
||||
let names = vec!["setosa", "versicolor", "virginica"];
|
||||
|
||||
let mut model = GaussianNB::new(1e-9, true);
|
||||
model.fit(&x, &y);
|
||||
|
||||
let sample = Matrix::from_vec(vec![5.0, 1.5], 1, 2);
|
||||
let predicted_class = model.predict(&sample);
|
||||
let class_name = names[predicted_class.data()[0] as usize];
|
||||
println!("Predicted class: {} ({:?})", class_name, predicted_class.data()[0]);
|
||||
```
|
||||
|
||||
### Customer Segmentation
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::compute::models::k_means::KMeans;
|
||||
use rustframe::matrix::Matrix;
|
||||
|
||||
// Each row: [age, annual_income]
|
||||
let customers = Matrix::from_rows_vec(
|
||||
vec![
|
||||
25.0, 40_000.0, 34.0, 52_000.0, 58.0, 95_000.0, 45.0, 70_000.0,
|
||||
],
|
||||
4,
|
||||
2,
|
||||
);
|
||||
|
||||
let (model, labels) = KMeans::fit(&customers, 2, 20, 1e-4);
|
||||
|
||||
let new_customer = Matrix::from_vec(vec![30.0, 50_000.0], 1, 2);
|
||||
let cluster = model.predict(&new_customer)[0];
|
||||
println!("New customer belongs to cluster: {}", cluster);
|
||||
println!("Cluster labels: {:?}", labels);
|
||||
```
|
||||
|
||||
For helper functions and upcoming modules, visit the
|
||||
[utilities](./utilities.md) section.
|
||||
63
docs/src/utilities.md
Normal file
63
docs/src/utilities.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# Utilities
|
||||
|
||||
Utilities provide handy helpers around the core library. Existing tools
|
||||
include:
|
||||
|
||||
- Date utilities for generating calendar sequences and business‑day sets
|
||||
- Random number generators for simulations and testing
|
||||
|
||||
## Date Helpers
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::utils::dateutils::{BDatesList, BDateFreq, DatesList, DateFreq};
|
||||
|
||||
// Calendar sequence
|
||||
let list = DatesList::new("2024-01-01".into(), "2024-01-03".into(), DateFreq::Daily);
|
||||
assert_eq!(list.count().unwrap(), 3);
|
||||
|
||||
// Business days starting from 2024‑01‑02
|
||||
let bdates = BDatesList::from_n_periods("2024-01-02".into(), BDateFreq::Daily, 3).unwrap();
|
||||
assert_eq!(bdates.list().unwrap().len(), 3);
|
||||
```
|
||||
|
||||
## Random Numbers
|
||||
|
||||
The `random` module offers deterministic and cryptographically secure RNGs.
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::random::{Prng, Rng};
|
||||
|
||||
let mut rng = Prng::new(42);
|
||||
let v1 = rng.next_u64();
|
||||
let v2 = rng.next_u64();
|
||||
assert_ne!(v1, v2);
|
||||
```
|
||||
|
||||
## Stats Functions
|
||||
|
||||
```rust
|
||||
# extern crate rustframe;
|
||||
use rustframe::matrix::Matrix;
|
||||
use rustframe::compute::stats::descriptive::{mean, median, stddev};
|
||||
|
||||
let data = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], 1, 5);
|
||||
|
||||
let mean_value = mean(&data);
|
||||
assert_eq!(mean_value, 3.0);
|
||||
|
||||
let median_value = median(&data);
|
||||
assert_eq!(median_value, 3.0);
|
||||
|
||||
let std_value = stddev(&data);
|
||||
assert_eq!(std_value, 2.0_f64.sqrt());
|
||||
```
|
||||
|
||||
Upcoming utilities will cover:
|
||||
|
||||
- Data import/export helpers
|
||||
- Visualization adapters
|
||||
- Streaming data interfaces
|
||||
|
||||
Contributions to these sections are welcome!
|
||||
@@ -1,3 +1,16 @@
|
||||
//! Algorithms and statistical utilities built on top of the core matrices.
|
||||
//!
|
||||
//! This module groups together machine‑learning models and statistical helper
|
||||
//! functions. For quick access to basic statistics see [`stats`](crate::compute::stats), while
|
||||
//! [`models`](crate::compute::models) contains small learning algorithms.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::stats;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let m = Matrix::from_vec(vec![1.0, 2.0, 3.0], 3, 1);
|
||||
//! assert_eq!(stats::mean(&m), 2.0);
|
||||
//! ```
|
||||
pub mod models;
|
||||
|
||||
pub mod stats;
|
||||
|
||||
@@ -1,3 +1,15 @@
|
||||
//! Common activation functions used in neural networks.
|
||||
//!
|
||||
//! Functions operate element-wise on [`Matrix`] values.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::models::activations::sigmoid;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let x = Matrix::from_vec(vec![0.0], 1, 1);
|
||||
//! let y = sigmoid(&x);
|
||||
//! assert!((y.get(0,0) - 0.5).abs() < 1e-6);
|
||||
//! ```
|
||||
use crate::matrix::{Matrix, SeriesOps};
|
||||
|
||||
pub fn sigmoid(x: &Matrix<f64>) -> Matrix<f64> {
|
||||
|
||||
@@ -1,3 +1,30 @@
|
||||
//! A minimal dense neural network implementation for educational purposes.
|
||||
//!
|
||||
//! Layers operate on [`Matrix`] values and support ReLU and Sigmoid
|
||||
//! activations. This is not meant to be a performant deep‑learning framework
|
||||
//! but rather a small example of how the surrounding matrix utilities can be
|
||||
//! composed.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::models::dense_nn::{ActivationKind, DenseNN, DenseNNConfig, InitializerKind, LossKind};
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! // Tiny network with one input and one output neuron.
|
||||
//! let config = DenseNNConfig {
|
||||
//! input_size: 1,
|
||||
//! hidden_layers: vec![],
|
||||
//! output_size: 1,
|
||||
//! activations: vec![ActivationKind::Relu],
|
||||
//! initializer: InitializerKind::Uniform(0.5),
|
||||
//! loss: LossKind::MSE,
|
||||
//! learning_rate: 0.1,
|
||||
//! epochs: 1,
|
||||
//! };
|
||||
//! let mut nn = DenseNN::new(config);
|
||||
//! let x = Matrix::from_vec(vec![1.0, 2.0], 2, 1);
|
||||
//! let y = Matrix::from_vec(vec![2.0, 3.0], 2, 1);
|
||||
//! nn.train(&x, &y);
|
||||
//! ```
|
||||
use crate::compute::models::activations::{drelu, relu, sigmoid};
|
||||
use crate::matrix::{Matrix, SeriesOps};
|
||||
use crate::random::prelude::*;
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
//! Gaussian Naive Bayes classifier for dense matrices.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::models::gaussian_nb::GaussianNB;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let x = Matrix::from_vec(vec![1.0, 2.0, 1.0, 2.0], 2, 2); // two samples
|
||||
//! let y = Matrix::from_vec(vec![0.0, 1.0], 2, 1);
|
||||
//! let mut model = GaussianNB::new(1e-9, false);
|
||||
//! model.fit(&x, &y);
|
||||
//! let preds = model.predict(&x);
|
||||
//! assert_eq!(preds.rows(), 2);
|
||||
//! ```
|
||||
use crate::matrix::Matrix;
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
//! Simple k-means clustering working on [`Matrix`] data.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::models::k_means::KMeans;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let data = Matrix::from_vec(vec![1.0, 1.0, 5.0, 5.0], 2, 2);
|
||||
//! let (model, labels) = KMeans::fit(&data, 2, 10, 1e-4);
|
||||
//! assert_eq!(model.centroids.rows(), 2);
|
||||
//! assert_eq!(labels.len(), 2);
|
||||
//! ```
|
||||
use crate::compute::stats::mean_vertical;
|
||||
use crate::matrix::Matrix;
|
||||
use crate::random::prelude::*;
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
//! Ordinary least squares linear regression.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::models::linreg::LinReg;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let x = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 4, 1);
|
||||
//! let y = Matrix::from_vec(vec![2.0, 3.0, 4.0, 5.0], 4, 1);
|
||||
//! let mut model = LinReg::new(1);
|
||||
//! model.fit(&x, &y, 0.01, 100);
|
||||
//! let preds = model.predict(&x);
|
||||
//! assert_eq!(preds.rows(), 4);
|
||||
//! ```
|
||||
use crate::matrix::{Matrix, SeriesOps};
|
||||
|
||||
pub struct LinReg {
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
//! Binary logistic regression classifier.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::models::logreg::LogReg;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let x = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 4, 1);
|
||||
//! let y = Matrix::from_vec(vec![0.0, 0.0, 1.0, 1.0], 4, 1);
|
||||
//! let mut model = LogReg::new(1);
|
||||
//! model.fit(&x, &y, 0.1, 100);
|
||||
//! let preds = model.predict(&x);
|
||||
//! assert_eq!(preds[(0,0)], 0.0);
|
||||
//! ```
|
||||
use crate::compute::models::activations::sigmoid;
|
||||
use crate::matrix::{Matrix, SeriesOps};
|
||||
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
//! Lightweight machine‑learning models built on matrices.
|
||||
//!
|
||||
//! Models are intentionally minimal and operate on the [`Matrix`](crate::matrix::Matrix) type for
|
||||
//! inputs and parameters.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::models::linreg::LinReg;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let x = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 4, 1);
|
||||
//! let y = Matrix::from_vec(vec![2.0, 3.0, 4.0, 5.0], 4, 1);
|
||||
//! let mut model = LinReg::new(1);
|
||||
//! model.fit(&x, &y, 0.01, 1000);
|
||||
//! let preds = model.predict(&x);
|
||||
//! assert_eq!(preds.rows(), 4);
|
||||
//! ```
|
||||
pub mod activations;
|
||||
pub mod dense_nn;
|
||||
pub mod gaussian_nb;
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
//! Principal Component Analysis using covariance matrices.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::models::pca::PCA;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let data = Matrix::from_rows_vec(vec![1.0, 1.0, 2.0, 2.0], 2, 2);
|
||||
//! let pca = PCA::fit(&data, 1, 0);
|
||||
//! let projected = pca.transform(&data);
|
||||
//! assert_eq!(projected.cols(), 1);
|
||||
//! ```
|
||||
use crate::compute::stats::correlation::covariance_matrix;
|
||||
use crate::compute::stats::descriptive::mean_vertical;
|
||||
use crate::matrix::{Axis, Matrix, SeriesOps};
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
//! Covariance and correlation helpers.
|
||||
//!
|
||||
//! This module provides routines for measuring the relationship between
|
||||
//! columns or rows of matrices.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::stats::correlation;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let x = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
//! let cov = correlation::covariance(&x, &x);
|
||||
//! assert!((cov - 1.25).abs() < 1e-8);
|
||||
//! ```
|
||||
use crate::compute::stats::{mean, mean_horizontal, mean_vertical, stddev};
|
||||
use crate::matrix::{Axis, Matrix, SeriesOps};
|
||||
|
||||
|
||||
@@ -1,3 +1,15 @@
|
||||
//! Descriptive statistics for matrices.
|
||||
//!
|
||||
//! Provides means, variances, medians and other aggregations computed either
|
||||
//! across the whole matrix or along a specific axis.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::stats::descriptive;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let m = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
//! assert_eq!(descriptive::mean(&m), 2.5);
|
||||
//! ```
|
||||
use crate::matrix::{Axis, Matrix, SeriesOps};
|
||||
|
||||
pub fn mean(x: &Matrix<f64>) -> f64 {
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
//! Probability distribution functions applied element-wise to matrices.
|
||||
//!
|
||||
//! Includes approximations for the normal, uniform and gamma distributions as
|
||||
//! well as the error function.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::stats::distributions;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let x = Matrix::from_vec(vec![0.0], 1, 1);
|
||||
//! let pdf = distributions::normal_pdf(x.clone(), 0.0, 1.0);
|
||||
//! assert!((pdf.get(0,0) - 0.3989).abs() < 1e-3);
|
||||
//! ```
|
||||
use crate::matrix::{Matrix, SeriesOps};
|
||||
|
||||
use std::f64::consts::PI;
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
//! Basic inferential statistics such as t‑tests and chi‑square tests.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::stats::inferential;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let a = Matrix::from_vec(vec![1.0, 2.0], 2, 1);
|
||||
//! let b = Matrix::from_vec(vec![1.1, 1.9], 2, 1);
|
||||
//! let (t, _p) = inferential::t_test(&a, &b);
|
||||
//! assert!(t.abs() < 1.0);
|
||||
//! ```
|
||||
use crate::matrix::{Matrix, SeriesOps};
|
||||
|
||||
use crate::compute::stats::{gamma_cdf, mean, sample_variance};
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
//! Statistical routines for matrices.
|
||||
//!
|
||||
//! Functions are grouped into submodules for descriptive statistics,
|
||||
//! correlations, probability distributions and basic inferential tests.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::compute::stats;
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let m = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||
//! let cov = stats::covariance(&m, &m);
|
||||
//! assert!((cov - 1.25).abs() < 1e-8);
|
||||
//! ```
|
||||
pub mod correlation;
|
||||
pub mod descriptive;
|
||||
pub mod distributions;
|
||||
|
||||
411
src/csv/csv_core.rs
Normal file
411
src/csv/csv_core.rs
Normal file
@@ -0,0 +1,411 @@
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
|
||||
/// Represents the target type for a CSV column.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DataType {
|
||||
Int,
|
||||
Float,
|
||||
Bool,
|
||||
UInt,
|
||||
String,
|
||||
Date,
|
||||
DateTime,
|
||||
}
|
||||
|
||||
/// Represents a value parsed from the CSV.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
UInt(u64),
|
||||
String(String),
|
||||
Date(NaiveDate),
|
||||
DateTime(NaiveDateTime),
|
||||
}
|
||||
|
||||
/// Convenience alias for a parsed CSV record.
|
||||
pub type Record = HashMap<String, Value>;
|
||||
|
||||
/// A simple CSV reader that reads records line by line.
|
||||
pub struct CsvReader<R: BufRead> {
|
||||
reader: R,
|
||||
separators: Vec<char>,
|
||||
headers: Vec<String>,
|
||||
types: Option<HashMap<String, DataType>>,
|
||||
}
|
||||
|
||||
/// Builder for [`CsvReader`] allowing chained configuration of headers, types, and separators.
|
||||
pub struct CsvReaderBuilder<R: BufRead> {
|
||||
reader: R,
|
||||
separators: Vec<char>,
|
||||
headers: Vec<String>,
|
||||
types: Option<HashMap<String, DataType>>,
|
||||
}
|
||||
|
||||
impl<R: BufRead> CsvReader<R> {
|
||||
/// Create a new CSV reader from a [`BufRead`] source.
|
||||
/// The first line is expected to contain headers.
|
||||
/// `separators` is a list of characters considered as field separators.
|
||||
/// `types` optionally maps column names to target data types.
|
||||
pub fn new(
|
||||
mut reader: R,
|
||||
separators: Vec<char>,
|
||||
types: Option<HashMap<String, DataType>>,
|
||||
) -> io::Result<Self> {
|
||||
let mut first_line = String::new();
|
||||
reader.read_line(&mut first_line)?;
|
||||
let headers = parse_line(&first_line, &separators);
|
||||
Ok(Self {
|
||||
reader,
|
||||
separators,
|
||||
headers,
|
||||
types,
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a reader with default settings (comma separator, automatic typing).
|
||||
pub fn new_default(reader: R) -> io::Result<Self> {
|
||||
Self::new(reader, vec![','], None)
|
||||
}
|
||||
|
||||
/// Create a reader with default separators and explicit type mapping.
|
||||
pub fn new_with_types(reader: R, types: HashMap<String, DataType>) -> io::Result<Self> {
|
||||
Self::new(reader, vec![','], Some(types))
|
||||
}
|
||||
|
||||
/// Start building a reader from a source that lacks headers.
|
||||
pub fn new_with_headers(reader: R, headers: Vec<String>) -> CsvReaderBuilder<R> {
|
||||
CsvReaderBuilder {
|
||||
reader,
|
||||
separators: vec![','],
|
||||
headers,
|
||||
types: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the headers of the CSV file.
|
||||
pub fn headers(&self) -> &[String] {
|
||||
&self.headers
|
||||
}
|
||||
|
||||
/// Read the next record. Returns `Ok(None)` on EOF.
|
||||
pub fn read_record(&mut self) -> io::Result<Option<Record>> {
|
||||
let mut line = String::new();
|
||||
if self.reader.read_line(&mut line)? == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let fields = parse_line(&line, &self.separators);
|
||||
let mut record = HashMap::new();
|
||||
|
||||
for (i, header) in self.headers.iter().enumerate() {
|
||||
let field = fields.get(i).cloned().unwrap_or_default();
|
||||
let value = match &self.types {
|
||||
Some(map) => {
|
||||
if let Some(dt) = map.get(header) {
|
||||
parse_with_type(&field, dt)
|
||||
} else {
|
||||
Value::String(field)
|
||||
}
|
||||
}
|
||||
None => parse_auto(&field),
|
||||
};
|
||||
record.insert(header.clone(), value);
|
||||
}
|
||||
|
||||
Ok(Some(record))
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: BufRead> Iterator for CsvReader<R> {
|
||||
type Item = io::Result<Record>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self.read_record() {
|
||||
Ok(Some(rec)) => Some(Ok(rec)),
|
||||
Ok(None) => None,
|
||||
Err(e) => Some(Err(e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: BufRead> CsvReaderBuilder<R> {
|
||||
/// Override field separators for the upcoming reader.
|
||||
pub fn separators(mut self, separators: Vec<char>) -> Self {
|
||||
self.separators = separators;
|
||||
self
|
||||
}
|
||||
|
||||
/// Finalize the builder with an explicit type mapping.
|
||||
pub fn new_with_types(mut self, types: HashMap<String, DataType>) -> CsvReader<R> {
|
||||
self.types = Some(types);
|
||||
self.build()
|
||||
}
|
||||
|
||||
/// Finalize the builder without specifying types.
|
||||
pub fn build(self) -> CsvReader<R> {
|
||||
CsvReader {
|
||||
reader: self.reader,
|
||||
separators: self.separators,
|
||||
headers: self.headers,
|
||||
types: self.types,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: BufRead> CsvReader<R> {
|
||||
/// Read all remaining records into a vector.
|
||||
pub fn read_all(&mut self) -> io::Result<Vec<Record>> {
|
||||
let mut records = Vec::new();
|
||||
while let Some(rec) = self.read_record()? {
|
||||
records.push(rec);
|
||||
}
|
||||
Ok(records)
|
||||
}
|
||||
}
|
||||
|
||||
impl CsvReader<BufReader<File>> {
|
||||
/// Create a [`CsvReader`] from a file path using comma separators and
|
||||
/// automatic type detection.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use rustframe::csv::{CsvReader, Value};
|
||||
/// # let path = std::env::temp_dir().join("from_path_auto.csv");
|
||||
/// # std::fs::write(&path, "a,b\n1,true\n").unwrap();
|
||||
/// let mut reader = CsvReader::from_path_auto(&path).unwrap();
|
||||
/// let rec = reader.next().unwrap().unwrap();
|
||||
/// assert_eq!(rec.get("a"), Some(&Value::Int(1)));
|
||||
/// assert_eq!(rec.get("b"), Some(&Value::Bool(true)));
|
||||
/// # std::fs::remove_file(path).unwrap();
|
||||
/// ```
|
||||
pub fn from_path_auto<P: AsRef<Path>>(path: P) -> io::Result<Self> {
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
CsvReader::new_default(reader)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create an iterator over records from a file path using default settings.
|
||||
pub fn reader<P: AsRef<Path>>(path: P) -> io::Result<CsvReader<BufReader<File>>> {
|
||||
reader_with(path, vec![','], None)
|
||||
}
|
||||
|
||||
/// Create an iterator over records from a file path with custom separators and type mapping.
|
||||
pub fn reader_with<P: AsRef<Path>>(
|
||||
path: P,
|
||||
separators: Vec<char>,
|
||||
types: Option<HashMap<String, DataType>>,
|
||||
) -> io::Result<CsvReader<BufReader<File>>> {
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
CsvReader::new(reader, separators, types)
|
||||
}
|
||||
|
||||
/// Read an entire CSV file into memory using default settings.
|
||||
pub fn read_file<P: AsRef<Path>>(path: P) -> io::Result<Vec<Record>> {
|
||||
read_file_with(path, vec![','], None)
|
||||
}
|
||||
|
||||
/// Read an entire CSV file into memory with custom separators and type mapping.
|
||||
pub fn read_file_with<P: AsRef<Path>>(
|
||||
path: P,
|
||||
separators: Vec<char>,
|
||||
types: Option<HashMap<String, DataType>>,
|
||||
) -> io::Result<Vec<Record>> {
|
||||
let mut reader = reader_with(path, separators, types)?;
|
||||
reader.read_all()
|
||||
}
|
||||
|
||||
fn parse_with_type(s: &str, ty: &DataType) -> Value {
|
||||
match ty {
|
||||
DataType::Int => s
|
||||
.parse::<i64>()
|
||||
.map(Value::Int)
|
||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
||||
DataType::Float => s
|
||||
.parse::<f64>()
|
||||
.map(Value::Float)
|
||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
||||
DataType::Bool => s
|
||||
.parse::<bool>()
|
||||
.map(Value::Bool)
|
||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
||||
DataType::UInt => s
|
||||
.parse::<u64>()
|
||||
.map(Value::UInt)
|
||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
||||
DataType::String => Value::String(s.to_string()),
|
||||
DataType::Date => s
|
||||
.parse::<NaiveDate>()
|
||||
.map(Value::Date)
|
||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
||||
DataType::DateTime => NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S")
|
||||
.map(Value::DateTime)
|
||||
.unwrap_or_else(|_| Value::String(s.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_auto(s: &str) -> Value {
|
||||
if let Ok(i) = s.parse::<i64>() {
|
||||
Value::Int(i)
|
||||
} else if let Ok(f) = s.parse::<f64>() {
|
||||
Value::Float(f)
|
||||
} else if let Ok(b) = s.parse::<bool>() {
|
||||
Value::Bool(b)
|
||||
} else if let Ok(dt) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
|
||||
Value::DateTime(dt)
|
||||
} else if let Ok(d) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
|
||||
Value::Date(d)
|
||||
} else {
|
||||
Value::String(s.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_line(line: &str, separators: &[char]) -> Vec<String> {
|
||||
let mut fields = Vec::new();
|
||||
let mut current = String::new();
|
||||
let mut in_quotes: Option<char> = None;
|
||||
let chars: Vec<char> = line.chars().collect();
|
||||
let mut i = 0;
|
||||
|
||||
while i < chars.len() {
|
||||
let c = chars[i];
|
||||
if let Some(q) = in_quotes {
|
||||
if c == q {
|
||||
if i + 1 < chars.len() && chars[i + 1] == q {
|
||||
current.push(q);
|
||||
i += 1; // skip escaped quote
|
||||
} else {
|
||||
in_quotes = None;
|
||||
}
|
||||
} else {
|
||||
current.push(c);
|
||||
}
|
||||
} else if c == '"' || c == '\'' {
|
||||
in_quotes = Some(c);
|
||||
} else if separators.contains(&c) {
|
||||
fields.push(current.clone());
|
||||
current.clear();
|
||||
} else if c == '\r' {
|
||||
// Ignore carriage returns
|
||||
} else if c == '\n' {
|
||||
break;
|
||||
} else {
|
||||
current.push(c);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
fields.push(current);
|
||||
fields
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn test_parse_line() {
|
||||
let line = "a,'b,c',\"d\"\"e\",f";
|
||||
let fields = parse_line(line, &[',']);
|
||||
assert_eq!(fields, vec!["a", "b,c", "d\"e", "f"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reader_auto() {
|
||||
let data = "a,b,c\n1,2.5,true\n4,5.0,false\n";
|
||||
let cursor = Cursor::new(data);
|
||||
let mut reader = CsvReader::new_default(cursor).unwrap();
|
||||
let rec = reader.next().unwrap().unwrap();
|
||||
assert_eq!(rec.get("a"), Some(&Value::Int(1)));
|
||||
assert_eq!(rec.get("b"), Some(&Value::Float(2.5)));
|
||||
assert_eq!(rec.get("c"), Some(&Value::Bool(true)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reader_with_types() {
|
||||
let data = "a,b,c\n1,2,3\n";
|
||||
let cursor = Cursor::new(data);
|
||||
let mut types = HashMap::new();
|
||||
types.insert("a".to_string(), DataType::Int);
|
||||
types.insert("b".to_string(), DataType::Int);
|
||||
types.insert("c".to_string(), DataType::String);
|
||||
let mut reader = CsvReader::new_with_types(cursor, types).unwrap();
|
||||
let rec = reader.next().unwrap().unwrap();
|
||||
assert_eq!(rec.get("a"), Some(&Value::Int(1)));
|
||||
assert_eq!(rec.get("b"), Some(&Value::Int(2)));
|
||||
assert_eq!(rec.get("c"), Some(&Value::String("3".to_string())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_chain_headers_and_types() {
|
||||
let data = "1,2\n3,4\n";
|
||||
let cursor = Cursor::new(data);
|
||||
let headers = vec!["x".to_string(), "y".to_string()];
|
||||
let mut types = HashMap::new();
|
||||
types.insert("x".to_string(), DataType::Int);
|
||||
types.insert("y".to_string(), DataType::UInt);
|
||||
let mut reader = CsvReader::new_with_headers(cursor, headers).new_with_types(types);
|
||||
let rec = reader.next().unwrap().unwrap();
|
||||
assert_eq!(rec.get("x"), Some(&Value::Int(1)));
|
||||
assert_eq!(rec.get("y"), Some(&Value::UInt(2)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_date_types() {
|
||||
let data = "d,dt\n2024-01-01,2024-01-01 12:00:00\n";
|
||||
let cursor = Cursor::new(data);
|
||||
let mut types = HashMap::new();
|
||||
types.insert("d".to_string(), DataType::Date);
|
||||
types.insert("dt".to_string(), DataType::DateTime);
|
||||
let mut reader = CsvReader::new_with_types(cursor, types).unwrap();
|
||||
let rec = reader.next().unwrap().unwrap();
|
||||
let date = NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
|
||||
let datetime: NaiveDateTime = NaiveDate::from_ymd_opt(2024, 1, 1)
|
||||
.unwrap()
|
||||
.and_hms_opt(12, 0, 0)
|
||||
.unwrap();
|
||||
assert_eq!(rec.get("d"), Some(&Value::Date(date)));
|
||||
assert_eq!(rec.get("dt"), Some(&Value::DateTime(datetime)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_file_all() {
|
||||
let path = std::env::temp_dir().join("csv_full_test.csv");
|
||||
std::fs::write(&path, "a,b\n1,2\n3,4\n").unwrap();
|
||||
let records = read_file(&path).unwrap();
|
||||
assert_eq!(records.len(), 2);
|
||||
assert_eq!(records[1].get("b"), Some(&Value::Int(4)));
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reader_from_path() {
|
||||
let path = std::env::temp_dir().join("csv_iter_test.csv");
|
||||
std::fs::write(&path, "a,b\n5,6\n").unwrap();
|
||||
let mut iter = reader(&path).unwrap();
|
||||
let rec = iter.next().unwrap().unwrap();
|
||||
assert_eq!(rec.get("a"), Some(&Value::Int(5)));
|
||||
assert_eq!(rec.get("b"), Some(&Value::Int(6)));
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_path_auto_method() {
|
||||
let path = std::env::temp_dir().join("csv_method_auto.csv");
|
||||
std::fs::write(&path, "a,b\n7,true\n").unwrap();
|
||||
let mut reader = CsvReader::from_path_auto(&path).unwrap();
|
||||
let rec = reader.next().unwrap().unwrap();
|
||||
assert_eq!(rec.get("a"), Some(&Value::Int(7)));
|
||||
assert_eq!(rec.get("b"), Some(&Value::Bool(true)));
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
}
|
||||
69
src/csv/mod.rs
Normal file
69
src/csv/mod.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
//! CSV handling utilities.
|
||||
//!
|
||||
//! The [`csv`] module offers a flexible [`CsvReader`] with automatic type
|
||||
//! detection and optional builders for custom headers and types.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! Read from a file with auto type detection:
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::csv::CsvReader;
|
||||
//! # let path = std::env::temp_dir().join("docs_auto.csv");
|
||||
//! # std::fs::write(&path, "a,b\n1,true\n").unwrap();
|
||||
//! let mut reader = CsvReader::from_path_auto(&path).unwrap();
|
||||
//! for rec in reader {
|
||||
//! let rec = rec.unwrap();
|
||||
//! println!("{:?}", rec);
|
||||
//! }
|
||||
//! # std::fs::remove_file(path).unwrap();
|
||||
//! ```
|
||||
//!
|
||||
//! Specify column types explicitly:
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::csv::{CsvReader, DataType, Value};
|
||||
//! use std::collections::HashMap;
|
||||
//! use std::io::Cursor;
|
||||
//! let data = "a,b\n1,2\n";
|
||||
//! let mut types = HashMap::new();
|
||||
//! types.insert("a".into(), DataType::Int);
|
||||
//! types.insert("b".into(), DataType::Float);
|
||||
//! let mut reader = CsvReader::new_with_types(Cursor::new(data), types).unwrap();
|
||||
//! let rec = reader.next().unwrap().unwrap();
|
||||
//! assert_eq!(rec.get("b"), Some(&Value::Float(2.0)));
|
||||
//! ```
|
||||
//!
|
||||
//! Building from custom headers and types:
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::csv::{CsvReader, DataType, Value};
|
||||
//! use std::collections::HashMap;
|
||||
//! use std::io::Cursor;
|
||||
//! let data = "1,2\n";
|
||||
//! let headers = vec!["x".to_string(), "y".to_string()];
|
||||
//! let mut types = HashMap::new();
|
||||
//! types.insert("x".into(), DataType::Int);
|
||||
//! types.insert("y".into(), DataType::UInt);
|
||||
//! let mut reader = CsvReader::new_with_headers(Cursor::new(data), headers).new_with_types(types);
|
||||
//! let rec = reader.next().unwrap().unwrap();
|
||||
//! assert_eq!(rec.get("y"), Some(&Value::UInt(2)));
|
||||
//! ```
|
||||
//!
|
||||
//! Reading an entire file into memory:
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::csv::read_file;
|
||||
//! # let path = std::env::temp_dir().join("docs_full.csv");
|
||||
//! # std::fs::write(&path, "a,b\n1,2\n3,4\n").unwrap();
|
||||
//! let records = read_file(&path).unwrap();
|
||||
//! assert_eq!(records.len(), 2);
|
||||
//! # std::fs::remove_file(path).unwrap();
|
||||
//! ```
|
||||
|
||||
pub mod csv_core;
|
||||
|
||||
pub use csv_core::{
|
||||
CsvReader, CsvReaderBuilder, DataType, Record, Value, reader, reader_with,
|
||||
read_file, read_file_with,
|
||||
};
|
||||
@@ -1,3 +1,19 @@
|
||||
//! Core data-frame structures such as [`Frame`] and [`RowIndex`].
|
||||
//!
|
||||
//! The [`Frame`] type stores column-labelled data with an optional row index
|
||||
//! and builds upon the [`crate::matrix::Matrix`] type.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::frame::{Frame, RowIndex};
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let data = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]);
|
||||
//! let frame = Frame::new(data, vec!["L", "R"], Some(RowIndex::Int(vec![10, 20])));
|
||||
//! assert_eq!(frame.columns(), &["L", "R"]);
|
||||
//! assert_eq!(frame.index(), &RowIndex::Int(vec![10, 20]));
|
||||
//! ```
|
||||
use crate::matrix::Matrix;
|
||||
use chrono::NaiveDate;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -1,3 +1,21 @@
|
||||
//! High-level interface for working with columnar data and row indices.
|
||||
//!
|
||||
//! The [`Frame`](crate::frame::Frame) type combines a matrix with column labels and a typed row
|
||||
//! index, similar to data frames in other data-analysis libraries.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::frame::{Frame, RowIndex};
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! // Build a frame from two columns labelled "A" and "B".
|
||||
//! let data = Matrix::from_cols(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
|
||||
//! let frame = Frame::new(data, vec!["A", "B"], None);
|
||||
//!
|
||||
//! assert_eq!(frame["A"], vec![1.0, 2.0]);
|
||||
//! assert_eq!(frame.index(), &RowIndex::Range(0..2));
|
||||
//! ```
|
||||
pub mod base;
|
||||
pub mod ops;
|
||||
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
//! Trait implementations that allow [`Frame`] to reuse matrix operations.
|
||||
//!
|
||||
//! These modules forward numeric and boolean aggregation methods from the
|
||||
//! underlying [`Matrix`](crate::matrix::Matrix) type so that they can be called
|
||||
//! directly on a [`Frame`].
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::frame::Frame;
|
||||
//! use rustframe::matrix::{Matrix, SeriesOps};
|
||||
//!
|
||||
//! let frame = Frame::new(Matrix::from_cols(vec![vec![1.0, 2.0]]), vec!["A"], None);
|
||||
//! assert_eq!(frame.sum_vertical(), vec![3.0]);
|
||||
//! ```
|
||||
use crate::frame::Frame;
|
||||
use crate::matrix::{Axis, BoolMatrix, BoolOps, FloatMatrix, SeriesOps};
|
||||
|
||||
|
||||
@@ -14,3 +14,6 @@ pub mod compute;
|
||||
|
||||
/// Documentation for the [`crate::random`] module.
|
||||
pub mod random;
|
||||
|
||||
/// Documentation for the [`crate::csv`] module.
|
||||
pub mod csv;
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
//! Logical reductions for boolean matrices.
|
||||
//!
|
||||
//! The [`BoolOps`] trait mirrors common boolean aggregations such as `any` and
|
||||
//! `all` over rows or columns of a [`BoolMatrix`].
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::matrix::{BoolMatrix, BoolOps};
|
||||
//!
|
||||
//! let m = BoolMatrix::from_vec(vec![true, false], 2, 1);
|
||||
//! assert!(m.any());
|
||||
//! ```
|
||||
use crate::matrix::{Axis, BoolMatrix};
|
||||
|
||||
/// Boolean operations on `Matrix<bool>`
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
//! Core matrix types and operations.
|
||||
//!
|
||||
//! The [`Matrix`](crate::matrix::Matrix) struct provides a simple column‑major 2D array with a
|
||||
//! suite of numeric helpers. Additional traits like [`SeriesOps`](crate::matrix::SeriesOps) and
|
||||
//! [`BoolOps`](crate::matrix::BoolOps) extend functionality for common statistics and logical reductions.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::matrix::Matrix;
|
||||
//!
|
||||
//! let m = Matrix::from_cols(vec![vec![1, 2], vec![3, 4]]);
|
||||
//! assert_eq!(m.shape(), (2, 2));
|
||||
//! assert_eq!(m[(0,1)], 3);
|
||||
//! ```
|
||||
pub mod boolops;
|
||||
pub mod mat;
|
||||
pub mod seriesops;
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
//! Numeric reductions and transformations over matrix axes.
|
||||
//!
|
||||
//! [`SeriesOps`] provides methods like [`SeriesOps::sum_vertical`] or
|
||||
//! [`SeriesOps::map`] that operate on [`FloatMatrix`] values.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::matrix::{Matrix, SeriesOps};
|
||||
//!
|
||||
//! let m = Matrix::from_cols(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
|
||||
//! assert_eq!(m.sum_horizontal(), vec![4.0, 6.0]);
|
||||
//! ```
|
||||
use crate::matrix::{Axis, BoolMatrix, FloatMatrix};
|
||||
|
||||
/// "Series-like" helpers that work along a single axis.
|
||||
|
||||
@@ -1,3 +1,13 @@
|
||||
//! Cryptographically secure random number generator.
|
||||
//!
|
||||
//! On Unix systems this reads from `/dev/urandom`; on Windows it uses the
|
||||
//! system's preferred CNG provider.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::random::{crypto_rng, Rng};
|
||||
//! let mut rng = crypto_rng();
|
||||
//! let _v = rng.next_u64();
|
||||
//! ```
|
||||
#[cfg(unix)]
|
||||
use std::{fs::File, io::Read};
|
||||
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
//! Random number generation utilities.
|
||||
//!
|
||||
//! Provides both a simple pseudo-random generator [`Prng`](crate::random::Prng) and a
|
||||
//! cryptographically secure alternative [`CryptoRng`](crate::random::CryptoRng). The
|
||||
//! [`SliceRandom`](crate::random::SliceRandom) trait offers shuffling of slices using any RNG
|
||||
//! implementing [`Rng`](crate::random::Rng).
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::random::{rng, SliceRandom};
|
||||
//!
|
||||
//! let mut rng = rng();
|
||||
//! let mut data = [1, 2, 3, 4];
|
||||
//! data.shuffle(&mut rng);
|
||||
//! assert_eq!(data.len(), 4);
|
||||
//! ```
|
||||
pub mod crypto;
|
||||
pub mod prng;
|
||||
pub mod random_core;
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
//! A tiny XorShift64-based pseudo random number generator.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::random::{rng, Rng};
|
||||
//! let mut rng = rng();
|
||||
//! let x = rng.next_u64();
|
||||
//! assert!(x >= 0);
|
||||
//! ```
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::random::Rng;
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
//! Core traits for random number generators and sampling ranges.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::random::{rng, Rng};
|
||||
//! let mut r = rng();
|
||||
//! let value: f64 = r.random_range(0.0..1.0);
|
||||
//! assert!(value >= 0.0 && value < 1.0);
|
||||
//! ```
|
||||
use std::f64::consts::PI;
|
||||
use std::ops::Range;
|
||||
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
//! Extensions for shuffling slices with a random number generator.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::random::{rng, SliceRandom};
|
||||
//! let mut data = [1, 2, 3];
|
||||
//! data.shuffle(&mut rng());
|
||||
//! assert_eq!(data.len(), 3);
|
||||
//! ```
|
||||
use crate::random::Rng;
|
||||
|
||||
/// Trait for randomizing slices.
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
//! Generation and manipulation of calendar date sequences.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::utils::dateutils::dates::{DateFreq, DatesList};
|
||||
//! let list = DatesList::new("2024-01-01".into(), "2024-01-03".into(), DateFreq::Daily);
|
||||
//! assert_eq!(list.count().unwrap(), 3);
|
||||
//! ```
|
||||
use chrono::{Datelike, Duration, NaiveDate, Weekday};
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
|
||||
@@ -1,3 +1,13 @@
|
||||
//! Generators for sequences of calendar and business dates.
|
||||
//!
|
||||
//! See [`dates`] for all-day calendars and [`bdates`] for business-day aware
|
||||
//! variants.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::utils::dateutils::{DatesList, DateFreq};
|
||||
//! let list = DatesList::new("2024-01-01".into(), "2024-01-02".into(), DateFreq::Daily);
|
||||
//! assert_eq!(list.count().unwrap(), 2);
|
||||
//! ```
|
||||
pub mod bdates;
|
||||
pub mod dates;
|
||||
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
//! Assorted helper utilities.
|
||||
//!
|
||||
//! Currently this module exposes date generation utilities in [`dateutils`](crate::utils::dateutils),
|
||||
//! including calendar and business date sequences.
|
||||
//!
|
||||
//! ```
|
||||
//! use rustframe::utils::DatesList;
|
||||
//! use rustframe::utils::DateFreq;
|
||||
//! let dates = DatesList::new("2024-01-01".into(), "2024-01-03".into(), DateFreq::Daily);
|
||||
//! assert_eq!(dates.count().unwrap(), 3);
|
||||
//! ```
|
||||
pub mod dateutils;
|
||||
|
||||
pub use dateutils::{BDateFreq, BDatesGenerator, BDatesList};
|
||||
|
||||
Reference in New Issue
Block a user