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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::ops::Range;
use crate::{
basis::Basis,
display::PolynomialDisplay,
plotting::PlottingElement,
statistics::{Confidence, ConfidenceBand, Tolerance},
value::Value,
CurveFit, Polynomial,
};
pub mod plotters;
/// Trait for plot backends
pub trait PlotBackend {
/// Error type for the plot backend
type Error: std::error::Error;
/// Root type for the plot backend
type Root;
/// Color type for the plot backend
type Color: Clone;
/// Get the next color in the palette
fn next_color(&mut self) -> Self::Color;
/// Set the alpha (opacity) of a color
fn color_with_alpha(color: &Self::Color, alpha: f64) -> Self::Color;
/// Create a new plot with the given title and ranges on the given root
///
/// # Errors
/// Returns an error if the plot cannot be created.
#[allow(clippy::too_many_arguments)]
fn new_plot<T: Value>(
root: &Self::Root,
title: &str,
x_label: Option<String>,
y_label: Option<String>,
x_range: Range<T>,
y_range: Range<T>,
hide_legend: bool,
margins: Option<i32>,
x_axis_labels: Option<usize>,
y_axis_labels: Option<usize>,
) -> Result<Self, Self::Error>
where
Self: Sized;
/// Add a line to the plot
///
/// # Errors
/// Returns an error if the plot cannot be modified.
fn add_line<T: Value>(
&mut self,
data: &[(T, T)],
label: &str,
width: u32,
color: Self::Color,
) -> Result<(), Self::Error>;
/// Add a marker to the plot
///
/// # Errors
/// Returns an error if the plot cannot be modified.
fn add_marker<T: Value>(&mut self, x: T, y: T, label: Option<&str>) -> Result<(), Self::Error>;
/// Add a dashed line to the plot
///
/// # Errors
/// Returns an error if the plot cannot be modified.
fn add_dashed_line<T: Value>(
&mut self,
data: &[(T, T)],
label: &str,
width: u32,
sizing: (u32, u32),
color: Self::Color,
) -> Result<(), Self::Error>;
/// Add a confidence band to the plot
///
/// # Errors
/// Returns an error if the plot cannot be modified.
fn add_confidence<T: Value>(
&mut self,
data: &[(T, ConfidenceBand<T>)],
color: Self::Color,
) -> Result<(), Self::Error>;
/// Finalize the plot
///
/// # Errors
/// Returns an error if the plot cannot be modified.
fn finalize(self) -> Result<(), Self::Error>;
/// Add a plotting element to the plot
///
/// # Errors
/// Returns an error if the plot cannot be created.
fn add_element<T: Value>(&mut self, element: &PlottingElement<T>) -> Result<(), Self::Error> {
match element {
PlottingElement::Fit(data, bands, equation) => {
let fit_color = self.next_color();
// For sanity sake, sample evenly to get ~100 points max
let step_size = (data.len() / 100).max(1);
let data: Vec<(T, T)> = data.iter().step_by(step_size).copied().collect();
self.add_dashed_line(&data, "Source Data", 1, (1, 2), fit_color)?;
let solution = bands
.iter()
.map(|(x, band)| (*x, band.value()))
.collect::<Vec<_>>();
let color = self.next_color();
self.add_line(&solution, equation, 1, color.clone())?;
let confidence_color = Self::color_with_alpha(&color, 0.3);
// Sample the bands the same way as the data
let bands: Vec<(T, ConfidenceBand<T>)> =
bands.iter().step_by(step_size).copied().collect();
self.add_confidence(&bands, confidence_color)
}
PlottingElement::Canonical(data, equation) => {
let color = self.next_color();
self.add_line(data, equation, 1, color)
}
PlottingElement::Data(data, label) => {
let color = self.next_color();
// For sanity sake, sample evenly to get ~100 points max
let step_size = (data.len() / 100).max(1);
let data: Vec<(T, T)> = data.iter().step_by(step_size).copied().collect();
self.add_line(&data, label.as_deref().unwrap_or("Data"), 1, color)
}
PlottingElement::Markers(points) => {
for (x, y, label) in points {
self.add_marker(*x, *y, label.as_ref().map(String::as_str))?;
}
Ok(())
}
}
}
/// Add a polynomial to the plot
///
/// # Errors
/// Returns an error if the plot cannot be created.
fn add_polynomial<T: Value, B: Basis<T> + PolynomialDisplay<T>>(
&mut self,
function: &Polynomial<B, T>,
x: &[T],
) -> Result<(), Self::Error> {
let element = PlottingElement::from_polynomial(function, x);
self.add_element(&element)
}
/// Add a curve fit to the plot
///
/// # Errors
/// Returns an error if the plot cannot be created.
fn add_fit<T: Value, B: Basis<T> + PolynomialDisplay<T>>(
&mut self,
fit: &CurveFit<B, T>,
confidence: Confidence,
noise_tolerance: Option<Tolerance<T>>,
) -> Result<(), Self::Error> {
self.add_element(&PlottingElement::from_curve_fit(
fit,
confidence,
noise_tolerance,
))
}
/// Add raw data to the plot
///
/// # Errors
/// Returns an error if the plot cannot be created.
fn add_data<T: Value>(
&mut self,
data: &[(T, T)],
label: Option<String>,
) -> Result<(), Self::Error> {
self.add_element(&PlottingElement::from_data(data.iter().copied(), label))
}
/// Add a set of markers to the plot
///
/// # Errors
/// Returns an error if the plot cannot be created.
fn add_markers<T: Value>(
&mut self,
points: &[(T, T, Option<String>)],
) -> Result<(), Self::Error> {
self.add_element(&PlottingElement::Markers(points.to_vec()))
}
}