diff options
| author | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-11-14 23:33:37 +0000 |
|---|---|---|
| committer | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-11-14 23:33:37 +0000 |
| commit | 12c2a37c3ef3a09f1ab0c5ea69b31b433b15f9f6 (patch) | |
| tree | 96f87fc751d709e7faad995619d1beafd6248381 /tests | |
| parent | 473ad82b6f4477bbbeeef977e3e9f830f59a9b7f (diff) | |
| download | correlation_rust-main.tar.gz | |
Expose Spearman, so both methods work without a dataset file main
`Spearman` was a private struct, so only `Pearson` could be constructed from outside the crate: a caller that already holds two value vectors -- a request handler, an enrichment step, a test -- could not compute the rank correlation without going through `Compute`, which means writing the dataset to a file and reading the result back out of a CSV. That is why `Pearson` was public and `Spearman` was not, and it is not a distinction worth keeping: the p-value code is the same either way. `Compute` remains the streaming path for a whole dataset. tests/in_memory.rs is an integration test (outside the crate, so it can only use the public surface) covering the three pieces that path needs: both methods in memory, the positional row parser dropping unparseable values together with their x, and the |r|-descending/NaN-last ordering. Also documents `Compute::sorter`, which orders by *signed* r while `custom_float_sorter` -- the one `compute` uses -- orders by magnitude. The two disagree for negative correlations; left as is because it is public API, but no longer silent.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/in_memory.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/in_memory.rs b/tests/in_memory.rs new file mode 100644 index 0000000..bd0637e --- /dev/null +++ b/tests/in_memory.rs @@ -0,0 +1,73 @@ +//! The in-memory path: both correlation methods, the row parser and the +//! ordering, usable from outside the crate. +//! +//! These are the pieces a caller that already holds two value vectors needs -- +//! no dataset file, no JSON, no CSV round trip. They exist as an integration +//! test (rather than a unit test inside `src/`) because that is what proves the +//! public surface: `Spearman` used to be a private struct, so only `Pearson` +//! could be constructed from outside. + +use correlation_rust::correlations::{Correlation, Pearson, Spearman}; +use correlation_rust::parser::parse_rows_with_names; +use correlation_rust::sorter::custom_float_sorter; + +#[test] +fn both_methods_correlate_in_memory() { + // A monotone relationship: both coefficients are 1. + let x = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]; + let y = [2.0, 4.0, 6.0, 8.0, 10.0, 12.0]; + let (pearson_r, pearson_p) = Pearson::new(x.len()).correlate(&x, &y); + let (spearman_rho, spearman_p) = Spearman::new(x.len()).correlate(&x, &y); + assert!((pearson_r - 1.0).abs() < 1e-12, "{pearson_r}"); + assert!((spearman_rho - 1.0).abs() < 1e-12, "{spearman_rho}"); + assert!(pearson_p.abs() < 1e-12, "{pearson_p}"); + assert!(spearman_p.abs() < 1e-12, "{spearman_p}"); + + // Reversed: both -1. + let reversed: Vec<f64> = y.iter().rev().copied().collect(); + assert!((Pearson::new(x.len()).correlate(&x, &reversed).0 + 1.0).abs() < 1e-12); + assert!((Spearman::new(x.len()).correlate(&x, &reversed).0 + 1.0).abs() < 1e-12); + + // A real (non-monotone) pair: the two methods differ, and both p-values + // are in range. Pearson is sensitive to the outlier, Spearman is not. + let x = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]; + let y = [1.0, 4.0, 9.0, 16.0, 25.0, 400.0]; + let (pearson_r, pearson_p) = Pearson::new(x.len()).correlate(&x, &y); + let (spearman_rho, spearman_p) = Spearman::new(x.len()).correlate(&x, &y); + assert!(pearson_r.is_finite() && pearson_r.abs() <= 1.0, "{pearson_r}"); + assert!(spearman_rho.is_finite() && spearman_rho.abs() <= 1.0, "{spearman_rho}"); + assert!((0.0..=1.0).contains(&pearson_p), "{pearson_p}"); + assert!((0.0..=1.0).contains(&spearman_p), "{spearman_p}"); + assert!(spearman_rho > pearson_r, "rank is robust to the outlier: {spearman_rho} vs {pearson_r}"); +} + +#[test] +fn a_row_is_parsed_positionally_and_bad_values_drop_their_x() { + // The row's first field is the trait name; the rest align positionally + // with x, and a value that does not parse takes its x with it. + let x = [10.0, 20.0, 30.0, 40.0]; + let row = ["trait_1", "1.5", "NA", "3.5", ""]; + let parsed = parse_rows_with_names(&x, &row); + assert_eq!(parsed.row_name, "trait_1"); + assert_eq!(parsed.x_vals, vec![10.0, 30.0]); + assert_eq!(parsed.y_vals, vec![1.5, 3.5]); + + // A short row keeps only as many pairs as it has values. + let parsed = parse_rows_with_names(&x, &["trait_2", "1.0", "2.0"]); + assert_eq!(parsed.x_vals, vec![10.0, 20.0]); + assert_eq!(parsed.y_vals, vec![1.0, 2.0]); +} + +#[test] +fn results_sort_by_magnitude_with_nans_last() { + let mut rows = vec![ + ("weak".to_string(), 0.1, 0.9, 30), + ("negative".to_string(), -0.9, 0.001, 30), + ("nan".to_string(), f64::NAN, f64::NAN, 30), + ("strong".to_string(), 0.95, 0.0001, 30), + ("middle".to_string(), -0.5, 0.2, 30), + ]; + custom_float_sorter(&mut rows); + let names: Vec<&str> = rows.iter().map(|row| row.0.as_str()).collect(); + assert_eq!(names, vec!["strong", "negative", "middle", "weak", "nan"]); +} |
