diff options
| author | Alexander_Kabui <alexanderkabua@gmail.com> | 2022-07-04 22:31:52 +0300 |
|---|---|---|
| committer | Alexander_Kabui <alexanderkabua@gmail.com> | 2022-07-04 22:31:52 +0300 |
| commit | 9405abf4fa18b4263c49af67bfcdf176bcf2aeed (patch) | |
| tree | 431190279beff77c49fc6cfd7a0ab3c19e556b68 /src | |
| parent | 19f2b9afa1698cab1158359ecd1cd65ffef99c5f (diff) | |
| download | correlation_rust-9405abf4fa18b4263c49af67bfcdf176bcf2aeed.tar.gz | |
add output file path
Diffstat (limited to 'src')
| -rw-r--r-- | src/analysis.rs | 1 | ||||
| -rw-r--r-- | src/correlations.rs | 33 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/main.rs | 20 | ||||
| -rw-r--r-- | src/parser.rs | 4 | ||||
| -rw-r--r-- | src/sorter.rs | 37 |
6 files changed, 52 insertions, 45 deletions
diff --git a/src/analysis.rs b/src/analysis.rs index 39cda67..08486a9 100644 --- a/src/analysis.rs +++ b/src/analysis.rs @@ -19,6 +19,7 @@ impl Analysis { &json_results.method, &json_results.file_path, &x_vals, + &json_results.output_file, ) .compute(); diff --git a/src/correlations.rs b/src/correlations.rs index 74ab26e..fcc8d29 100644 --- a/src/correlations.rs +++ b/src/correlations.rs @@ -1,6 +1,6 @@ use crate::parser::parse_rows_with_names; -use crate::sorter::sort_write_to_file; use crate::reader::BufferReader; +use crate::sorter::sort_write_to_file; //way to parse the args use rgsl::{ @@ -89,6 +89,7 @@ pub struct Compute<'a> { dataset_path: &'a str, method: CorrelationMethod, file_delimiter: char, + output_file: &'a str, } #[allow(dead_code)] impl<'a> Compute<'a> { @@ -97,6 +98,7 @@ impl<'a> Compute<'a> { method: &str, dataset_path: &'a str, x_vals: &'a [f64], + output_file: &'a str, ) -> Self { //capitalize method/same cases @@ -110,6 +112,7 @@ impl<'a> Compute<'a> { x_vals: x_vals, dataset_path: dataset_path, method: method, + output_file: output_file, file_delimiter: file_delimeter, } } @@ -202,13 +205,12 @@ impl<'a> Compute<'a> { //naive implementation try extern sorting could save 3 seconds - corr_results.sort_by(|a, b| b.1.abs().partial_cmp(&a.1.abs()).unwrap()); - // corr_results.sort_by(|a,b|b.1.abs().partial_cmp(&a.1.abs()).unwrap()); - sort_write_to_file(String::from("sorted_results.txt"), corr_results).unwrap(); - - return vec![] + // corr_results.sort_by(|a,b|b.1.abs().partial_cmp(&a.1.abs()).unwrap()); + sort_write_to_file(String::from(self.output_file), corr_results).unwrap(); + + return vec![]; } } #[derive(PartialEq, Debug)] @@ -271,8 +273,8 @@ mod test { fn test_compute_obj() { let x_vals = [1., 3., 4., 5., 6., 7., 7.]; let new_compute = [ - Compute::new(',', "spearman", "./notes.txt", &x_vals), - Compute::new(',', "pearson", "./note2.txt", &x_vals), + Compute::new(',', "spearman", "./notes.txt", &x_vals, "/output.txt"), + Compute::new(',', "pearson", "./note2.txt", &x_vals, "/output.txt"), ]; assert_eq!( @@ -281,12 +283,14 @@ mod test { file_delimiter: ',', x_vals: &[1., 3., 4., 5., 6., 7., 7.], dataset_path: "./notes.txt", + output_file: "/output.txt", method: CorrelationMethod::Spearman }, Compute { file_delimiter: ',', x_vals: &[1., 3., 4., 5., 6., 7., 7.], dataset_path: "./note2.txt", + output_file: "/output.txt", method: CorrelationMethod::Pearson } ], @@ -297,14 +301,21 @@ mod test { #[test] #[should_panic] fn test_compute_panics() { - let new_compute = Compute::new(',', "unknown", "/notes.txt", &[1.2, 1.4, 1.5]); + let new_compute = Compute::new( + ',', + "unknown", + "/notes.txt", + &[1.2, 1.4, 1.5], + "/output.txt", + ); assert_eq!( new_compute, Compute { file_delimiter: ',', x_vals: &[1.2, 1.4, 1.5], dataset_path: "/notes.txt", - method: CorrelationMethod::Pearson + method: CorrelationMethod::Pearson, + output_file: "/output.txt" } ) } @@ -316,6 +327,7 @@ mod test { "pearson", "/home/kabui/correlation_rust/tests/data/mock_dataset.txt", &[12., 15., 11., 11., 16., 11., 8., 7.], + "/output.txt", ); let corr_results = compute_obj.compute(); @@ -379,6 +391,7 @@ mod test { "pearson", "/home/kabui/correlation_rust/tests/data/matrix_80.txt", &x_vals, + "/output.txt", ); assert_eq!( diff --git a/src/lib.rs b/src/lib.rs index 80d4900..fc157b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,4 @@ pub mod analysis; pub mod correlations; pub mod parser; pub mod reader; -pub mod sorter; \ No newline at end of file +pub mod sorter; diff --git a/src/main.rs b/src/main.rs index d64a59f..d8cb54b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,17 @@ use correlation_rust::analysis::Analysis; use std::env; -fn main () { - +fn main() { use std::time::Instant; let now = Instant::now(); - match env::args().nth(1){ - Some(tmp_file) => { - - match Analysis::compute(&tmp_file){ - Ok(results) => println!("{:?}",results), - Err(err) => println!("{:?}",err) - } - } - None => panic!("expected an arugment for the json file") + match env::args().nth(1) { + Some(tmp_file) => match Analysis::compute(&tmp_file) { + Ok(results) => println!("{:?}", results), + Err(err) => println!("{:?}", err), + }, + None => panic!("expected an arugment for the json file"), }; - let elapsed = now.elapsed(); println!("Elapsed:>>>>>>>>>>>>>>>>>>>>{:.2?}", elapsed); - } diff --git a/src/parser.rs b/src/parser.rs index 7bddc83..97ed18a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,4 +1,5 @@ use serde::Deserialize; +use std::fs::File; use std::io::Read; #[derive(Debug, Deserialize, PartialEq)] //#[serde(rename_all = "PascalCase")] @@ -9,6 +10,7 @@ pub struct JsonData { pub sample_values: String, pub method: String, pub file_delimiter: char, + pub output_file: String, } impl JsonData { @@ -158,7 +160,7 @@ mod tests { let json_struct = JsonData::new("/home/kabui/correlation_rust/tests/data/sample_json_file.json"); - let k = JsonData { file_path: String::from("/home/kabui/correlation_rust/tests/data/matrix_80.txt"), x_vals: String::from("25.08439, 72.02225, 47.56293, 22.87893, 14.28721, 71.84655, 87.81991, 84.86824, 6.72478, 5.72373, 73.47078, 63.74703"), sample_values: String::from("bxd1"),method:String::from("pearson"),file_delimiter:','}; + let k = JsonData { file_path: String::from("/home/kabui/correlation_rust/tests/data/matrix_80.txt"),output_file:String::from("/home/kabui/correlation_rust/output.txt"),x_vals: String::from("25.08439, 72.02225, 47.56293, 22.87893, 14.28721, 71.84655, 87.81991, 84.86824, 6.72478, 5.72373, 73.47078, 63.74703"), sample_values: String::from("bxd1"),method:String::from("pearson"),file_delimiter:','}; assert_eq!(k, json_struct) } diff --git a/src/sorter.rs b/src/sorter.rs index 49846a0..b5ed26c 100644 --- a/src/sorter.rs +++ b/src/sorter.rs @@ -1,18 +1,15 @@ // implementation of extern sorter with rust //https://betterprogramming.pub/how-to-sort-a-20g-file-in-rust-12abfffbd92b -use std::io::{Write,Read,BufWriter}; use std::fs::File; - //use extsort::*; - +use std::io::{BufWriter, Read, Write}; +//use extsort::*; const BUFFER_CAPACITY: usize = 4_000_000_000; //struct corr_results #[derive(Debug, PartialEq, PartialOrd)] -struct CorrResults(String,f64,f64); - - +struct CorrResults(String, f64, f64); // impl Sortable for CorrResults { @@ -24,15 +21,14 @@ struct CorrResults(String,f64,f64); // fn encode<W: Write>(&self, writer: &mut W) { // writer.write_fmt(format_args!("{} {} {}",self.0,self.1,self.2)).unwrap(); - + // } - + // } // fn ext_sorter (unsorted_vec:Vec<CorrResults>,file_name:&str,n_top:usize)-> std::io::Result<Vec<CorrResults>>{ - -// File::create(file_name)?; +// File::create(file_name)?; // let sorter = ExternalSorter::new().with_segment_size(4_000_000); @@ -40,23 +36,24 @@ struct CorrResults(String,f64,f64); // let sorted = sorter.sort_by(into_iterator, |a,b|b.1.abs().partial_cmp(&a.1.abs()).unwrap())?.collect::<Vec<CorrResults>>(); - //sorted.collect::<Vec<CorrResults>>().truncate(n_top); +//sorted.collect::<Vec<CorrResults>>().truncate(n_top); // Ok(sorted) - //corr_results.sort_by(|a,b|b.1.abs().partial_cmp(&a.1.abs()).unwrap()); +//corr_results.sort_by(|a,b|b.1.abs().partial_cmp(&a.1.abs()).unwrap()); // } +pub fn sort_write_to_file( + filename: String, + mut v: Vec<(String, f64, f64)>, +) -> std::io::Result<String> { + File::create(filename.clone())?; -pub fn sort_write_to_file(filename: String, mut v: Vec<(String,f64,f64)>) ->std::io::Result<String>{ - - File::create(filename.clone())?; - - v.sort_by(|a,b|b.1.abs().partial_cmp(&a.1.abs()).unwrap()); + v.sort_by(|a, b| b.1.abs().partial_cmp(&a.1.abs()).unwrap()); let mut buffer = BufWriter::with_capacity(BUFFER_CAPACITY, File::create(&filename).unwrap()); - for (name,rho,p_val) in v.iter() { - writeln!(buffer, "{},{},{}", name,rho,p_val).unwrap(); + for (name, rho, p_val) in v.iter() { + writeln!(buffer, "{},{},{}", name, rho, p_val).unwrap(); } buffer.flush().unwrap(); @@ -65,4 +62,4 @@ pub fn sort_write_to_file(filename: String, mut v: Vec<(String,f64,f64)>) ->std: pub fn create_large_file(filename: &str) { File::create(filename).unwrap(); -} \ No newline at end of file +} |
