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
177
178
179
pub use crate::traits::{Configurable, Plotable, Saveable};
use core::ops::Add;
use average::Variance;
pub mod comparison;
pub use comparison::SequenceErrors;
#[derive(Debug, PartialEq, Clone)]
pub struct SequenceError {
data: Vec<(f64, f64)>,
config: crate::configuration::Configuration,
}
impl SequenceError {
pub fn new<I, J, T>(data: I) -> SequenceError
where
I: IntoIterator<Item = J>,
J: IntoIterator<Item = T>,
T: Into<f64>,
{
let data: Vec<(f64, f64)> = data
.into_iter()
.map(|j| {
let v: Variance = j.into_iter().map(|t| t.into()).collect();
(v.mean(), v.error())
})
.collect();
let config = crate::configuration::Configuration::default();
SequenceError { data, config }
}
}
impl Add for SequenceError {
type Output = crate::SequenceErrors;
fn add(self, other: crate::SequenceError) -> crate::SequenceErrors {
let mut cmp = self.into();
cmp += other;
cmp
}
}
impl Configurable for SequenceError {
fn configuration_mut(&mut self) -> &mut crate::configuration::Configuration {
&mut self.config
}
fn configuration(&self) -> &crate::configuration::Configuration {
&self.config
}
}
impl Saveable for SequenceError {
fn plotable_data(&self) -> String {
if self.data.is_empty() {
eprintln!("Warning: There is no data.");
}
let mut plotable_data = String::new();
for (counter, (value, error)) in self.data.clone().into_iter().enumerate() {
plotable_data.push_str(&format!("{}\t{}\t{}\n", counter, value, error));
}
plotable_data
}
}
impl Plotable for SequenceError {
fn plot_script(&self) -> String {
let mut gnuplot_script = self.opening_plot_script();
let dashtype = match self.dashtype() {
Some(dashtype) => dashtype,
None => 1,
};
gnuplot_script += &format!(
"plot {:?} using 1:2 with {} dashtype {}, \"\" using 1:2:3 with yerrorbars \n",
self.data_path(),
self.style(),
dashtype,
);
gnuplot_script += &self.ending_plot_script();
gnuplot_script
}
}
impl<T> From<crate::Densities<T>> for SequenceError
where
T: Into<f64> + core::fmt::Display + Clone,
{
fn from(mut densities: crate::Densities<T>) -> Self {
let data: Vec<Vec<f64>> = (0..densities.data_set.len())
.map(|i| {
densities.data_set[i]
.realizations
.iter()
.map(|t| t.clone().into())
.collect()
})
.collect();
let mut seq_err = SequenceError::new(data);
let config = seq_err.configuration_mut();
*config = densities.configuration_mut().clone();
seq_err
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn set_style() {
let data = vec![vec![0., 1.], vec![0., 1., 2.], vec![3., 4., 5.]];
let mut seq = SequenceError::new(data);
seq.set_style("points");
assert_eq!(
&crate::configuration::plot::style::Style::Points,
seq.style()
);
}
#[test]
fn from_densitites() {
use crate::prelude::*;
let many_dens = (0..5).map(|_| pre::Density::new(0..10));
let mut densities: pre::Densities<u32> = pre::Densities::new(many_dens);
densities.set_title("My title");
let seq_err = pre::SequenceError::from(densities.clone());
assert_eq!(seq_err.title(), densities.title());
}
}