From ea2be8e691613334120ffc259fdf2be9bbd53e58 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 4 May 2022 02:26:35 +0300 Subject: code refactoring --- src/correlations.rs | 31 +++++++++++-------------------- src/parser.rs | 2 +- src/reader.rs | 49 ------------------------------------------------- 3 files changed, 12 insertions(+), 70 deletions(-) (limited to 'src') diff --git a/src/correlations.rs b/src/correlations.rs index 95b892d..ae0e00d 100644 --- a/src/correlations.rs +++ b/src/correlations.rs @@ -1,11 +1,10 @@ use crate::reader::BufferReader; - +use crate::parser::parse_rows; use std::env; - //way to parse the args use rgsl::{ randist::t_distribution::{tdist_P, tdist_Q}, @@ -109,7 +108,7 @@ impl <'a> Compute<'a>{ fn new (file_delimeter:char,method:&str,dataset_path:&'a str,x_vals:&'a [f64]) -> Self{ //capitalize method/same cases - let method = match method { + let method = match method.to_lowercase().as_str() { "pearson" => CorrelationMethod::Pearson, "spearman" => CorrelationMethod::Spearman, _ => panic!("method cannot be found") @@ -141,28 +140,21 @@ impl <'a> Compute<'a>{ while let Some(val) = buffer_read.read_line(& mut n_string){ if let Ok(array_new_val) = val { - //issue here - - let x_vals = [9.,5.,0.,7.,6.,1.,5.,0.,1.,5.,4.,2.,11.,15.,17.,21.]; - - - let new_array:[f64;16]= array_new_val - .split(",") - .map(|s| s.trim().parse::().expect("parse error")) - .collect::>().try_into().unwrap_or_else(|_v|panic!("Expected vla")); - + + let (parsed_x_val,parsed_y_val) = parse_rows(self.x_vals,&array_new_val + + .split(self.file_delimiter) + .collect::>()); - let pearson = Pearson::new(5); - let results = pearson.correlate(&x_vals,&new_array); + let results = Pearson::new(parsed_x_val.len()) + .correlate(&parsed_x_val,&parsed_y_val); corr_results.push(results); } - } - - + } } @@ -311,10 +303,9 @@ mod test{ fn test_huge_dataset(){ let x_vals = [25.08439 ,72.02225 ,47.56293 ,22.87893 ,14.28721 ,71.84655 ,87.81991 ,84.86824 ,6.72478 ,5.72373 ,73.47078 ,63.74703]; - - let compute_obj = Compute::new(',',"pearson","/home/kabui/correlation_rust/src/db300.txt",&x_vals); + let compute_obj = Compute::new(',',"pearson","/home/kabui/correlation_rust/src/matrix_80.txt",&x_vals); assert_eq!(vec![(1.2,1.5)],compute_obj.compute()) diff --git a/src/parser.rs b/src/parser.rs index 7ce317d..f2dacc3 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,5 +1,5 @@ -fn parse_rows (x_vals:&[f64],y_vals:&[&str]) -> (Vec,Vec){ +pub fn parse_rows (x_vals:&[f64],y_vals:&[&str]) -> (Vec,Vec){ //pointer let mut parsed_x_vals= Vec::new(); diff --git a/src/reader.rs b/src/reader.rs index 197638a..016a119 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -40,55 +40,6 @@ impl BufferReader{ }).transpose() } - pub fn fetch_keys <'a>(&self,row:& 'a str) -> (&str,[&str]){ - - - // parse the line and return (trait_name and trait_values) - - //parse the other body to float type - - let mut buf = String::new(); - // should be taken to own line - - - let sample_buffer = self.read_line(&mut buf); - - let sample_names = match sample_buffer{ - - Some(val)=> { - if let Ok(new_vak) = val{ - - new_vak - - } - - else { - panic!("error happened"); - } - }, - - None => panic!("no sample values found") - - }; - - - - - let string_vec : Vec<&str>= row.split(",").collect(); - - //issue is that spaces are included - - return [string_vec[0],string_vec[1..]]; - } -pub fn row_parser <'a>(&self,row :&'a mut String) ->(& 'a str,Vec<&'a str>){ - - let parsed_row:Vec<&str> = row.split(",").map(|s|s.trim()).collect(); - - let (trait_name,trait_vals) = (parsed_row[0],parsed_row[1..].to_vec()); - - (parsed_row[0],parsed_row[1..].to_vec()) - -} } -- cgit 1.4.1