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
use {
crate::Measure,
std::{fs::read_to_string, io::Error, path::Path},
};
/// Object to read data from a file with all required parameters.
pub struct Reader<'a> {
file: &'a str,
separator: &'a str,
line: &'a str,
decimal: &'a str,
headers: usize,
by_columns: bool,
}
impl<'a> Reader<'a> {
/// Constructs a new Reader with some default values that can be changed.
pub fn new(file: &str, headers: usize) -> Reader {
Reader {
file,
separator: "\t",
line: "\n",
decimal: ",",
headers,
by_columns: true,
}
}
/// Character separating the columns in a row, by default "\t".
pub fn separator(mut self, separator: &'a str) -> Self {
self.separator = separator;
self
}
/// Character separating rows, by default "\n".
pub fn line(mut self, line: &'a str) -> Self {
self.line = line;
self
}
/// Decimal separator, "," by default.
pub fn decimal(mut self, decimal: &'a str) -> Self {
self.decimal = decimal;
self
}
/// Changes how to read the data, false for horizontal and true for vertical,
/// true by default.
pub fn by_columns(mut self, by_columns: bool) -> Self {
self.by_columns = by_columns;
self
}
/// Extracts data from a file with csv format or similar.
pub fn read_file(self) -> Result<Vec<Vec<Option<f64>>>, Error> {
read_file(
self.file,
self.separator,
self.line,
self.decimal,
self.headers,
self.by_columns,
)
}
/// Extracts data from a file creating measures by asuming each pair of columns
/// correspond to the value and error of a measure.
pub fn read_to_measures(self) -> Vec<Measure> {
read_to_measures(
self.file,
self.separator,
self.line,
self.decimal,
self.headers,
).unwrap()
}
}
fn read_file(
file: &str,
separator: &str,
line: &str,
decimal: &str,
headers: usize,
by_columns: bool,
) -> Result<Vec<Vec<Option<f64>>>, Error> {
let file = read_to_string(Path::new(file))?;
let rows: Vec<&str> = file
.split(line)
.filter(|str| !str.trim().is_empty())
.skip(headers)
.collect();
let mut data: Vec<Vec<Option<f64>>> = rows
.into_iter()
.map(|row| {
row.split(separator)
.map(|str| {
if str.trim().is_empty() {
None
} else {
Some(str.trim())
}
})
.collect()
})
.collect::<Vec<Vec<Option<&str>>>>()
.into_iter()
.map(|row_vec| {
row_vec
.into_iter()
.map(|str_option| {
str_option.as_ref().map(|str| {
str.trim()
.replace(decimal, ".")
.parse()
.expect("Non number found")
})
})
.collect()
})
.collect();
if by_columns {
let max_len = data.iter().map(|vec| vec.len()).max().unwrap();
data = (0..max_len)
.map(|index| {
data.iter()
.filter(|vec| index < vec.len() || vec[index].is_some())
.map(|vec| vec[index])
.collect()
})
.collect();
}
Ok(data)
}
fn read_to_measures(
file: &str,
separator: &str,
line: &str,
decimal: &str,
headers: usize,
) -> Result<Vec<Measure>, Error> {
let data = read_file(file, separator, line, decimal, headers, true)?;
Ok(data
.iter()
.step_by(2)
.zip(data.iter().skip(1).step_by(2))
.map(|(value, error)| {
Measure::new(
value
.iter()
.take_while(|val| val.is_some())
.map(|val| val.unwrap())
.collect(),
error
.iter()
.take_while(|err| err.is_some())
.map(|err| err.unwrap())
.collect(),
true,
)
.unwrap()
})
.collect())
}