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
use super::fit;
use super::{PH10_STATIC, PH4_STATIC, TEMP_STATIC};
use float_cmp::ApproxEq;
use splines::{Interpolation, Key, Spline};
pub struct Calibration<F> {
pub slope: F,
pub offset: F,
pub rms: Option<F>,
pub rsq: Option<F>,
}
impl<'a, M: Copy + Default, F: Copy + ApproxEq<Margin = M>> ApproxEq for &'a Calibration<F> {
type Margin = M;
fn approx_eq<T: Into<Self::Margin>>(self, other: Self, margin: T) -> bool {
let margin = margin.into();
self.slope.approx_eq(other.slope, margin) && self.offset.approx_eq(other.offset, margin)
}
}
impl<F> Calibration<F>
where
F: Copy,
{
pub fn new(slope: F, offset: F, rms: Option<F>, rsq: Option<F>) -> Calibration<F> {
Calibration {
slope,
offset,
rms,
rsq,
}
}
pub fn with_slope(&self, slope: F) -> Calibration<F> {
Calibration {
slope,
offset: self.offset,
rms: self.rms,
rsq: self.rsq,
}
}
pub fn with_offset(&self, offset: F) -> Calibration<F> {
Calibration {
slope: self.slope,
offset: offset,
rms: self.rms,
rsq: self.rsq,
}
}
}
impl<F> Default for Calibration<F>
where
F: Default,
{
fn default() -> Self {
Calibration {
slope: F::default(),
offset: F::default(),
rms: None,
rsq: None,
}
}
}
pub fn ph_calibration(ph_measured: &[f64; 2], temperature: &f64) -> Calibration<f64> {
let ph4_cal = interp_ph4(temperature).unwrap();
let ph10_cal = interp_ph10(temperature).unwrap();
let ph_cal = [ph4_cal, ph10_cal];
let calibration = fit::fit(ph_measured, &ph_cal);
let fit_eval = fit::evaluate(ph_measured, &ph_cal, &calibration);
Calibration::new(
calibration[0],
calibration[1],
Some(fit_eval[0]),
Some(fit_eval[1]),
)
}
pub fn ph_convert(ph_measured: &f64, calibration: &[f64; 2]) -> f64 {
let ph_calibrated = fit::predict(ph_measured, calibration);
ph_calibrated
}
pub fn interp_ph4(temperature: &f64) -> Option<f64> {
let pairs_iter = TEMP_STATIC.iter().zip(PH4_STATIC.iter());
let zipped_points: Vec<_> = pairs_iter
.map(|(x, y)| Key::new(*x, *y, Interpolation::Linear))
.collect();
let spline = Spline::from_vec(zipped_points);
let val = spline.sample(*temperature);
val
}
pub fn interp_ph10(temperature: &f64) -> Option<f64> {
let pairs_iter = TEMP_STATIC.iter().zip(PH10_STATIC.iter());
let zipped_points: Vec<_> = pairs_iter
.map(|(x, y)| Key::new(*x, *y, Interpolation::Linear))
.collect();
let spline = Spline::from_vec(zipped_points);
let val = spline.sample(*temperature);
val
}
#[cfg(test)]
mod tests {
use float_cmp::approx_eq;
use crate::routines::Calibration;
use super::ph_calibration;
#[test]
fn test_ph_calibration() {
let temperature = 21.0;
let ph_measured = [3.75, 9.49];
let res = ph_calibration(&ph_measured, &temperature);
let slope = 1.053658536585366;
let offset = 0.05078048780487787;
let test_calib = Calibration::default().with_slope(slope).with_offset(offset);
assert!(approx_eq!(&Calibration<f64>, &res, &test_calib))
}
}