diff options
| author | Alexander <alexanderkabua@gmail.com> | 2022-04-27 21:59:30 +0300 |
|---|---|---|
| committer | Alexander <alexanderkabua@gmail.com> | 2022-04-27 21:59:30 +0300 |
| commit | 31a6c41340558e121554fc191bf682c463097818 (patch) | |
| tree | 6579266b40199df14d13d27b0323d13906f197a5 /src | |
| parent | 8ffde4938793d2d50bd72b61d5bb35f58bc14e1e (diff) | |
| download | correlation_rust-31a6c41340558e121554fc191bf682c463097818.tar.gz | |
fix failing tests
Diffstat (limited to 'src')
| -rw-r--r-- | src/correlations.rs | 133 | ||||
| -rw-r--r-- | src/mock_dataset.txt | 8 |
2 files changed, 76 insertions, 65 deletions
diff --git a/src/correlations.rs b/src/correlations.rs index 7d5851d..c7dc003 100644 --- a/src/correlations.rs +++ b/src/correlations.rs @@ -1,5 +1,5 @@ -use core::panic; -use std::io::BufRead; + +use crate::reader::BufferReader; use rgsl::{ randist::t_distribution::{tdist_P, tdist_Q}, @@ -88,52 +88,6 @@ impl Correlation for Spearman { } -#[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) - - } - -} - - -//structure we need file for y vals,x_val ,correlation method to use - #[allow(dead_code)] @@ -164,11 +118,9 @@ impl <'a> Compute<'a>{ } - fn compute(&self) -> Vec<f64>{ + fn compute(&self) -> Vec<(f64,f64)>{ //read from file); - use crate::reader::BufferReader; - use crate::correlations::Correlation; let mut corr_results:Vec<(f64,f64)> = Vec::new(); let reader = BufferReader::new(self.dataset_path); @@ -181,13 +133,13 @@ impl <'a> Compute<'a>{ while let Some(val) = buffer_read.read_line(& mut n_string){ if let Ok(array_new_val) = val { let new_array= array_new_val - .split_whitespace() - .map(|s| s.parse().expect("parse error")) + .split(",") + .map(|s| s.trim().parse::<f64>().expect("parse error")) .collect::<Vec<f64>>(); let pearson = Pearson::new(new_array.len()); let results = pearson.correlate(&[1.2,1.1,1.3,1.6,1.8],&[1.2,1.1,1.3,1.6,1.8]); - corr_results.push(results) + corr_results.push(results); } @@ -198,12 +150,12 @@ impl <'a> Compute<'a>{ } - Err(_) =>panic!("an error ocurred") + Err(err) =>panic!("an error ocurrexxxxxxxxxxxxxxd {:?}",err) } - todo!() + corr_results //where the magic happens //reading the file ?? should happen in new @@ -223,12 +175,50 @@ enum CorrelationMethod { -#[cfg(test)] -mod tests { +#[cfg(test)] +mod test{ use super::*; + use assert_approx_eq::assert_approx_eq; + + + + + #[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_compute_obj(){ let x_vals = [1.,3.,4.,5.,6.,7.,7.]; @@ -262,11 +252,32 @@ mod tests { #[test] fn test_compute(){ - let compute_obj = Compute::new("pearson","/home/kabui/correlation_rust/mock_dataset.txt",&[4. ,5. ,1. ,1. ,6. ,1. ,8. ,7. + let compute_obj = Compute::new("pearson","/home/kabui/correlation_rust/src/mock_dataset.txt",&[4. ,5. ,1. ,1. ,6. ,1. ,8. ,7. ]); let corr_results = compute_obj.compute(); - assert_eq!(corr_results,[]) + assert_eq!(corr_results,[(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0)]) + } + + #[test] + + fn test_parse_f64(){ + let data = "9. ,5. ,0. ,7. ,6. ,1. ,5. ,0.\n"; + + let expected_results = vec![9.,5.,0.,7.,6.,1.,5.,0.]; + + + let results:Vec<f64>= data + .split(",") + .map(|f_str|f_str.trim().parse::<f64>().expect("parse failed")) + .collect(); + + assert_eq!(results,expected_results) + } -} \ No newline at end of file +} + + + + diff --git a/src/mock_dataset.txt b/src/mock_dataset.txt index c6feeaf..a4cec8f 100644 --- a/src/mock_dataset.txt +++ b/src/mock_dataset.txt @@ -1,4 +1,4 @@ -9 ,5 ,0 ,7 ,6 ,1 ,5 ,0 -3 ,9 ,4 ,2 ,2 ,7 ,2 ,4 -7 ,1 ,7 ,2 ,2 ,5 ,6 ,5 -4 ,5 ,1 ,1 ,6 ,1 ,8 ,7 +9. ,5. ,0. ,7. ,6. ,1. ,5. ,0. +3. ,9. ,4. ,2. ,2. ,7. ,2. ,4. +7. ,1. ,7. ,2. ,2. ,5. ,6. ,5. +4. ,5. ,1. ,1. ,6. ,1. ,8. ,7. |
