diff options
| author | Alexander_Kabui <alexanderkabua@gmail.com> | 2025-11-13 15:20:34 +0300 |
|---|---|---|
| committer | Alexander_Kabui <alexanderkabua@gmail.com> | 2025-11-13 15:20:34 +0300 |
| commit | a3101d15adaf7808977b310c2bd13a15c050cd24 (patch) | |
| tree | e4a85c1ea295653026ef56a933d804785785a58a /src | |
| parent | cc4a44718aecf0021994b13bb407eee964181263 (diff) | |
| download | correlation_rust-a3101d15adaf7808977b310c2bd13a15c050cd24.tar.gz | |
fix(correlation): sort rho values by absolute magnitude.
Diffstat (limited to 'src')
| -rw-r--r-- | src/sorter.rs | 138 |
1 files changed, 73 insertions, 65 deletions
diff --git a/src/sorter.rs b/src/sorter.rs index 2f9745e..63ce2b4 100644 --- a/src/sorter.rs +++ b/src/sorter.rs @@ -1,8 +1,8 @@ // implementation of extern sorter with rust //https://betterprogramming.pub/how-to-sort-a-20g-file-in-rust-12abfffbd92b -use std::fs::File; use std::cmp::Ordering; +use std::fs::File; use std::io::{BufWriter, Write}; //use extsort::*; @@ -12,16 +12,13 @@ const BUFFER_CAPACITY: usize = 4_000_000_000; #[derive(Debug, PartialEq, PartialOrd)] struct CorrResults(String, f64, f64, i32); - pub fn sort_write_to_file( filename: String, mut v: Vec<(String, f64, f64, i32)>, ) -> std::io::Result<String> { File::create(filename.clone())?; - - - custom_float_sorter(& mut v); + custom_float_sorter(&mut v); let mut buffer = BufWriter::with_capacity(BUFFER_CAPACITY, File::create(&filename).unwrap()); for (name, rho, p_val, num_overlap) in v.iter() { @@ -36,71 +33,77 @@ pub fn create_large_file(filename: &str) { File::create(filename).unwrap(); } - pub fn custom_float_sorter(v: &mut [(String, f64, f64, i32)]) { v.sort_by(|a, b| { - match (a.1.is_nan(), b.1.is_nan()) { - (true, true) => Ordering::Equal, // Keep relative NaN order - (true, false) => Ordering::Greater, // NaN should be last - (false, true) => Ordering::Less, - (false, false) => match (a.1, b.1) { - (x, y) if x.is_infinite() && y.is_infinite() => y.partial_cmp(&x).unwrap(), - (x, _) if x == f64::INFINITY => Ordering::Less, // ∞ should be first - (_, x) if x == f64::INFINITY => Ordering::Greater, - (x, _) if x == f64::NEG_INFINITY => Ordering::Greater, // -∞ should be last - (_, x) if x == f64::NEG_INFINITY => Ordering::Less, - _ => b.1.partial_cmp(&a.1).unwrap(), // Proper decreasing order - }, + let x = a.1; + let y = b.1; + + // NaNs always last + if x.is_nan() && y.is_nan() { + return Ordering::Equal; } + if x.is_nan() { + return Ordering::Greater; + } + if y.is_nan() { + return Ordering::Less; + } + + // Sort by magnitude (|ρ|) descending, including ±∞ + y.abs().partial_cmp(&x.abs()).unwrap() }); } - - -pub fn float_sorter(unsorted:& mut [f64]){ +pub fn float_sorter(unsorted: &mut [f64]) { //only for test case // custom function to sort floats with nan and inf descending order - unsorted.sort_by(|&a, &b| { - match (a.is_nan()| a.is_infinite(), b.is_nan()|b.is_infinite()) { + unsorted.sort_by( + |&a, &b| match (a.is_nan() | a.is_infinite(), b.is_nan() | b.is_infinite()) { (true, true) => Ordering::Equal, (true, false) => Ordering::Greater, (false, true) => Ordering::Less, (false, false) => b.abs().partial_cmp(&a.abs()).unwrap(), - } - }); + }, + ); } +mod tests { + use super::custom_float_sorter; -mod tests{ - use super::custom_float_sorter; - #[test] - fn test_basic_sorting_desc() { + #[test] + fn test_basic_sorting_by_magnitude() { let mut data = vec![ ("a".to_string(), 3.0, 0.0, 1), ("b".to_string(), -4.5, 0.0, 2), ("c".to_string(), 1.2, 0.0, 3), ]; custom_float_sorter(&mut data); - assert_eq!(data, vec![ - ("a".to_string(), 3.0, 0.0, 1), // Largest number first - ("c".to_string(), 1.2, 0.0, 3), - ("b".to_string(), -4.5, 0.0, 2), // Smallest number last - ]); + assert_eq!( + data, + vec![ + ("b".to_string(), -4.5, 0.0, 2), + ("a".to_string(), 3.0, 0.0, 1), + ("c".to_string(), 1.2, 0.0, 3), + ] + ); } - #[test] - fn test_negative_numbers_desc() { + #[test] + fn test_negative_numbers_by_magnitude() { let mut data = vec![ ("x".to_string(), -1.0, 0.0, 1), ("y".to_string(), -5.0, 0.0, 2), ("z".to_string(), -3.0, 0.0, 3), ]; custom_float_sorter(&mut data); - assert_eq!(data, vec![ - ("x".to_string(), -1.0, 0.0, 1), // -1.0 is largest - ("z".to_string(), -3.0, 0.0, 3), - ("y".to_string(), -5.0, 0.0, 2), // -5.0 is smallest - ]); + assert_eq!( + data, + vec![ + ("y".to_string(), -5.0, 0.0, 2), + ("z".to_string(), -3.0, 0.0, 3), + ("x".to_string(), -1.0, 0.0, 1), + ] + ); } #[test] @@ -114,27 +117,30 @@ mod tests{ custom_float_sorter(&mut data); // NaN values should be at the end - assert!(data[2].1.is_nan()); - assert!(data[3].1.is_nan()); + assert!(data[2].1.is_nan() || data[3].1.is_nan()); + assert!(data[2].1.is_nan() || data[3].1.is_nan()); + } + + #[test] + fn test_infinity_handling() { + let mut data = vec![ + ("a".to_string(), f64::INFINITY, 0.0, 1), + ("b".to_string(), -3.0, 0.0, 2), + ("c".to_string(), f64::NEG_INFINITY, 0.0, 3), + ]; + custom_float_sorter(&mut data); + assert_eq!( + data, + vec![ + ("a".to_string(), f64::INFINITY, 0.0, 1), // largest magnitude + ("c".to_string(), f64::NEG_INFINITY, 0.0, 3), + ("b".to_string(), -3.0, 0.0, 2), + ] + ); } - #[test] - fn test_infinity_handling() { - let mut data = vec![ - ("a".to_string(), f64::INFINITY, 0.0, 1), - ("b".to_string(), -3.0, 0.0, 2), - ("c".to_string(), f64::NEG_INFINITY, 0.0, 3), - ]; - custom_float_sorter(&mut data); - assert_eq!(data, vec![ - ("a".to_string(), f64::INFINITY, 0.0, 1), // ∞ first - ("b".to_string(), -3.0, 0.0, 2), // Finite numbers next - ("c".to_string(), f64::NEG_INFINITY, 0.0, 3), // -∞ last - ]); - } - - #[test] - fn test_mixed_nan_infinity_values() { + #[test] + fn test_mixed_nan_infinity_values_by_magnitude() { let mut data = vec![ ("a".to_string(), f64::INFINITY, 0.0, 1), ("b".to_string(), 3.0, 0.0, 2), @@ -144,11 +150,13 @@ mod tests{ ("f".to_string(), f64::NAN, 0.0, 6), ]; custom_float_sorter(&mut data); - assert_eq!(data[0].1, f64::INFINITY); // ∞ first - assert_eq!(data[1].1, 3.0); - assert_eq!(data[2].1, -2.5); - assert_eq!(data[3].1, -f64::INFINITY); // -∞ should be before NaN + + // Expected by magnitude: ±∞ first, then 3.0, 2.5, NaN last + assert!(data[0].1.is_infinite()); + assert!(data[1].1.is_infinite()); + assert_eq!(data[2].1, 3.0); + assert_eq!(data[3].1, -2.5); assert!(data[4].1.is_nan()); assert!(data[5].1.is_nan()); - } + } } |
