1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
use serde::Deserialize;
use std::io::Read;
#[derive(Debug, Deserialize, PartialEq)]
//#[serde(rename_all = "PascalCase")]
pub struct JsonData {
pub file_path: String,
pub x_vals: Vec<f64>,
pub sample_values: String,
pub method: String,
pub file_delimiter: char,
pub output_file: String,
}
impl JsonData {
pub fn new(json_file_path: &str) -> JsonData {
let mut file = std::fs::File::open(json_file_path).unwrap();
let mut buff = String::new();
file.read_to_string(&mut buff).unwrap();
let results = match serde_json::from_str(&buff) {
Ok(val) => val,
Err(error) => panic!("json file not well formatted {:?}", error),
};
results
}
}
#[derive(PartialEq, Debug)]
pub struct CorrelationEvaluateRow {
pub row_name: String,
pub x_vals: Vec<f64>,
pub y_vals: Vec<f64>,
}
impl CorrelationEvaluateRow {
#[allow(dead_code)]
fn new(row_name: &str, x_vals: Vec<f64>, y_vals: Vec<f64>) -> Self {
Self {
row_name: String::from(row_name),
x_vals,
y_vals,
}
}
}
pub fn parse_rows_with_names(x_vals: &[f64], y_vals: &[&str]) -> CorrelationEvaluateRow {
//initial stage
//extract names
let string_floats = &y_vals[1..];
let row_name = match y_vals.get(0) {
Some(row_key) => String::from(*row_key),
None => String::from(""),
};
let results = parse_rows(x_vals, string_floats);
CorrelationEvaluateRow {
row_name,
x_vals: results.0,
y_vals: results.1,
}
}
pub fn parse_rows(x_vals: &[f64], y_vals: &[&str]) -> (Vec<f64>, Vec<f64>) {
//optimization ?? memory
// assumes first item in row is the name of values i.e trait_1 , 12 , 12 , 12,14
let mut parsed_x_vals = Vec::new();
let mut parsed_y_vals = Vec::new();
for (index, x_val) in x_vals.iter().enumerate() {
if let Some(val) = y_vals.get(index) {
match val.trim().parse::<f64>() {
Ok(float_type) => {
parsed_x_vals.push(*x_val);
parsed_y_vals.push(float_type);
}
Err(_not_float) => continue,
}
}
}
(parsed_x_vals, parsed_y_vals)
}
pub fn file_path_validator() {
todo!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parsing_rows() {
//todo add test cases
let x_vals = [12.2, 12.1, 16.5, 11.1];
let pure_string = ["12.1", "11.1", "11.6", "11.7"];
let string_with_nans = ["", "1.2", "11.1", "", "4.5", "nan"];
assert_eq!(
parse_rows(&x_vals, &pure_string),
(vec![12.2, 12.1, 16.5, 11.1], vec![12.1, 11.1, 11.6, 11.7])
);
assert_eq!(
parse_rows(&x_vals, &string_with_nans),
(vec![12.1, 16.5], vec![1.2, 11.1])
);
}
#[test]
fn test_extract_keys() {
//check on allocation
let x_vals = [12.2, 12.1, 16.5, 11.1, 11.6];
let expected_results = [
CorrelationEvaluateRow::new("trait_1", vec![12.1, 16.5, 11.6], vec![1.2, 11.1, 4.5]),
CorrelationEvaluateRow::new("trait_2", vec![12.1, 16.5, 11.6], vec![1.6, 11.5, 4.2]),
CorrelationEvaluateRow::new(
"trait_3",
vec![12.2, 12.1, 16.5, 11.6],
vec![6.5, 1.6, 11.5, 4.2],
),
CorrelationEvaluateRow::new(
"trait_4",
vec![12.2, 12.1, 16.5, 11.6],
vec![12.5, 1.6, 11.5, 4.2],
),
];
for (index, test_case) in [
["trait_1", "", "1.2", "11.1", "", "4.5", "nan"],
["trait_2", "", "1.6", "11.5", "", "4.2", "nan"],
["trait_3", "6.5", "1.6", "11.5", "", "4.2", "12.1"],
["trait_4", "12.5", "1.6", "11.5", "", "4.2", "1.1"],
]
.iter()
.enumerate()
{
assert_eq!(
parse_rows_with_names(&x_vals, &*test_case),
expected_results[index]
)
}
}
#[test]
fn test_json_file_read() {
let json_struct = JsonData::new("tests/data/sample_json_file.json");
let k = JsonData {
file_path: String::from("tests/data/db300.txt"),
output_file: String::from("output.txt"),
x_vals: vec![
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)
}
}
|