From 1bb2f52839b75b9c2ee9aa08a11d7d3823dbe647 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 26 Apr 2022 14:56:53 +0300 Subject: modularize code and tests --- src/correlations.rs | 57 ++++++++++++++- src/lib.rs | 197 +-------------------------------------------------- src/mock_dataset.txt | 12 ---- src/reader.rs | 45 +++++++++++- 4 files changed, 100 insertions(+), 211 deletions(-) (limited to 'src') diff --git a/src/correlations.rs b/src/correlations.rs index f84bb65..596240e 100644 --- a/src/correlations.rs +++ b/src/correlations.rs @@ -5,7 +5,8 @@ use rgsl::{ pub trait Correlation { - fn correlate(&self,x:&[f64],y:&[f64]) ->(f64,f64); + fn correlate(&self,x:&[f64],y:&[f64]) ->(f64,f64); + } #[derive(PartialEq, Debug)] @@ -29,6 +30,16 @@ impl Pearson { } } } + +impl Spearman { + pub fn new(n:usize) -> Self { + + Self { + n, + degrees_of_freedom:(n - 2) as f64, + } + } +} impl Correlation for Pearson { fn correlate(&self,x:&[f64],y:&[f64]) ->(f64,f64){ @@ -72,3 +83,47 @@ impl Correlation for Spearman { return (rho,p_val); } } + + +#[cfg(test)] + +mod test{ + use assert_approx_eq::assert_approx_eq; + use super::*; + + + + #[test] + fn test_pearson_obj(){ + let new_pearson = Pearson::new(5); + assert_eq!(new_pearson,Pearson{n:5, degrees_of_freedom:3.0}); + } + #[test] + fn test_pearson_correlation(){ + let new_pearson = Pearson::new(3); + let (corr_coeff,p_val) = new_pearson.correlate(&[1.,2.,3.,4.,5.] ,&[1.,2.,3.,4.,5.]); + + assert_eq!(format!("{:.2}", corr_coeff),"1.00"); + assert_approx_eq!(p_val,1.341575855,2f64); + } + + #[test] + fn test_spearman_obj(){ + let new_spearman = Spearman::new(5); + + assert_eq!(new_spearman,Spearman{n:5, degrees_of_freedom:3.0}); + + } + #[test] + fn test_spearman_correlation(){ + let new_spearman = Spearman::new(5); + + let (s_rho,p_val) = new_spearman.correlate(&[1.,2.,3.,4.,5.], &[5.,6.,7.,8.,7.]); + + + assert_approx_eq!(s_rho,0.079603960396039,2f64); + assert_approx_eq!(p_val,0.43111687,2f64) + + } + +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 34de040..9dfa7dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,197 +1,2 @@ pub mod reader; -use rgsl::{ - randist::t_distribution::{tdist_P, tdist_Q}, - statistics::{correlation, spearman}, -}; -use assert_approx_eq::assert_approx_eq; - - - - -trait Correlation { - fn correlate(&self,x:&[f64],y:&[f64]) ->(f64,f64); - - -} - - -pub fn add(a: i32, b: i32) -> i32 { - a + b -} - - -#[derive(PartialEq, Debug)] -struct Pearson { - n: usize, - degrees_of_freedom:f64, -} - - - -impl Pearson { - fn new(n:usize) -> Self { - - Self { - n, - degrees_of_freedom:(n - 2) as f64, - } - } -} - - - - -impl Correlation for Pearson { - fn correlate(&self,x:&[f64],y:&[f64]) ->(f64,f64){ - - let corr_coeff = correlation(x, 1, y, 1, self.n); - - //compute p_val two sided - - // P-value (two-sided) - // Based on R's cor.test method (https://github.com/SurajGupta/r-source/blob/a28e609e72ed7c47f6ddfbb86c85279a0750f0b7/src/library/stats/R/cor.test.R#L21 - - - let statistic = self.degrees_of_freedom.sqrt() * corr_coeff / (1.0 - corr_coeff.powi(2)).sqrt(); - - let p_val = 2.0 - * tdist_P(statistic, self.degrees_of_freedom) - .min(tdist_Q(statistic, self.degrees_of_freedom)); - - (corr_coeff,p_val) - } -} - - -#[derive(PartialEq, Debug)] -struct Spearman { - n: usize, - degrees_of_freedom:f64, -} - - -impl Spearman { - fn new(n:usize) ->Spearman { - Spearman { n: n, degrees_of_freedom: (n - 2) as f64 } - } -} - -impl Correlation for Spearman { - - fn correlate(&self,x:&[f64],y:&[f64]) -> (f64,f64){ - - match x.len() == y.len() { - true => println!("success"), - false => panic!("array should be of the same size") - }; - - //dditional workspace of size 2*n is required in work - let mut vec = Vec::with_capacity(2 * self.n); - let workspace: &mut [f64] = vec.as_mut_slice(); - let rho = spearman(x, 1, y, 1, self.n, workspace); - - //p-val two sided - - let t = rho * (self.degrees_of_freedom / ((rho + 1.0) * (1.0 - rho))).sqrt(); - let p_val = 2.0* (tdist_Q(t.abs(), self.degrees_of_freedom)); - - return (rho,p_val); - } -} - - - -#[cfg(test)] -mod tests { - - - use crate::reader::{ - add_numbers_2, - BufferReader, - file_reader - }; - - use super::*; - #[test] - fn test_add_numbers() { - assert_eq!(add(2,3),5) - } - - #[test] - fn test_pearson_obj(){ - let new_pearson = Pearson::new(5); - assert_eq!(new_pearson,Pearson{n:5, degrees_of_freedom:3.0}); - } - #[test] - fn test_pearson_correlation(){ - let new_pearson = Pearson::new(3); - let (corr_coeff,p_val) = new_pearson.correlate(&[1.,2.,3.,4.,5.] ,&[1.,2.,3.,4.,5.]); - - assert_eq!(format!("{:.2}", corr_coeff),"1.00"); - assert_approx_eq!(p_val,1.341575855,2f64); - } - - #[test] - fn test_spearman_obj(){ - let new_spearman = Spearman::new(5); - - assert_eq!(new_spearman,Spearman{n:5, degrees_of_freedom:3.0}); - - } - #[test] - fn test_spearman_correlation(){ - let new_spearman = Spearman::new(5); - - let (s_rho,p_val) = new_spearman.correlate(&[1.,2.,3.,4.,5.], &[5.,6.,7.,8.,7.]); - - - assert_approx_eq!(s_rho,0.079603960396039,2f64); - assert_approx_eq!(p_val,0.43111687,2f64) - - - } - - #[test] - fn test_add_2(){ - assert_eq!(add_numbers_2(4, 5),9) - } - - #[test] - - fn test_file_reader_error(){ - assert!(file_reader("./non_file.txt").is_err()); - } - - - #[test] - - fn test_file_reader() { - let expected_results = file_reader("/home/kabui/correlation_rust/src/mock_dataset.txt"); - - assert!(expected_results.is_ok()); - - } - - # [test] - fn test_file_reader_2(){ - - let mut buffer_reader = BufferReader::new("/home/kabui/correlation_rust/src/mock_dataset.txt").unwrap(); - let mut buf = String::new(); - let mut data = [ - "9 ,5 ,0 ,7 ,6 ,1 ,5 ,0\n", - "3 ,9 ,4 ,2 ,2 ,7 ,2 ,4\n", - "7 ,1 ,7 ,2 ,2 ,5 ,6 ,5\n", - "4 ,5 ,1 ,1 ,6 ,1 ,8 ,7\n" - ].iter(); - - - while let Some(val) = buffer_reader.read_line(& mut buf){ - - if let Some(expected) = data.next(){ - assert_eq!(*expected,val.unwrap()) - } - - } - - } -} \ No newline at end of file +pub mod correlations; \ No newline at end of file diff --git a/src/mock_dataset.txt b/src/mock_dataset.txt index 71ced29..c6feeaf 100644 --- a/src/mock_dataset.txt +++ b/src/mock_dataset.txt @@ -2,15 +2,3 @@ 3 ,9 ,4 ,2 ,2 ,7 ,2 ,4 7 ,1 ,7 ,2 ,2 ,5 ,6 ,5 4 ,5 ,1 ,1 ,6 ,1 ,8 ,7 -1 ,2 ,0 ,9 ,1 ,3 ,9 ,5 -8 ,6 ,9 ,6 ,8 ,8 ,8 ,7 -4 ,1 ,5 ,7 ,8 ,7 ,7 ,1 -8 ,2 ,6 ,8 ,0 ,7 ,3 ,5 -0 ,5 ,4 ,6 ,2 ,1 ,5 ,3 -1 ,0 ,1 ,6 ,0 ,3 ,2 ,2 -9 ,9 ,7 ,1 ,7 ,5 ,6 ,5 -7 ,2 ,0 ,8 ,9 ,4 ,9 ,4 -6 ,2 ,4 ,5 ,5 ,5 ,0 ,0 -1 ,9 ,0 ,3 ,3 ,6 ,0 ,7 -3 ,7 ,2 ,6 ,8 ,0 ,4 ,9 -6 ,6 ,4 ,5 ,0 ,3 ,4 ,4 \ No newline at end of file diff --git a/src/reader.rs b/src/reader.rs index ce08ece..fd8c742 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -35,8 +35,7 @@ impl BufferReader{ Some(buf) - - + }).transpose() } @@ -58,3 +57,45 @@ pub fn add_numbers_2(a:i32,b:i32) -> i32{ +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_file_reader_error(){ + assert!(file_reader("./non_file.txt").is_err()); + } + + + #[test] + + fn test_file_reader() { + let expected_results = file_reader("/home/kabui/correlation_rust/src/mock_dataset.txt"); + + assert!(expected_results.is_ok()); + + } + + # [test] + fn test_file_reader_2(){ + + let mut buffer_reader = BufferReader::new("/home/kabui/correlation_rust/src/mock_dataset.txt").unwrap(); + let mut buf = String::new(); + let mut data = [ + "9 ,5 ,0 ,7 ,6 ,1 ,5 ,0\n", + "3 ,9 ,4 ,2 ,2 ,7 ,2 ,4\n", + "7 ,1 ,7 ,2 ,2 ,5 ,6 ,5\n", + "4 ,5 ,1 ,1 ,6 ,1 ,8 ,7\n" + ].iter(); + + + while let Some(val) = buffer_reader.read_line(& mut buf){ + + if let Some(expected) = data.next(){ + assert_eq!(*expected,val.unwrap()) + } + + } + + } +} \ No newline at end of file -- cgit 1.4.1