-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_contour.rs
More file actions
250 lines (216 loc) · 7.38 KB
/
test_contour.rs
File metadata and controls
250 lines (216 loc) · 7.38 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use plotpy::{generate3d, Contour, Plot, StrError};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
const OUT_DIR: &str = "/tmp/plotpy/integ_tests";
#[test]
fn test_contour() -> Result<(), StrError> {
// contour object and options
let mut contour = Contour::new();
contour
.set_colors(&vec!["#fcaeae", "#da98d1", "#c45178", "#5594d2", "#e6af69", "#e6d969"])
.set_levels(&vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
.set_colorbar_label("temperature")
.set_line_color("yellow")
.set_line_style(":")
.set_line_width(2.5)
.set_selected_line_color("#69e699")
.set_selected_line_width(5.0)
.set_selected_level(1.0, true)
.set_extra_filled("alpha=0.8")
.set_extra_line("alpha=0.3");
// draw contour
let n = 9;
let (x, y, z) = generate3d(-2.0, 2.0, -2.0, 2.0, n, n, |x, y| x * x + y * y);
contour.draw(&x, &y, &z);
// add contour to plot
let mut plot = Plot::new();
plot.add(&contour);
// save figure
let path = Path::new(OUT_DIR).join("integ_contour.svg");
plot.save(&path)?;
// check number of lines
let file = File::open(path).map_err(|_| "cannot open file")?;
let buffered = BufReader::new(file);
let lines_iter = buffered.lines();
let n = lines_iter.count();
assert!(n > 1400 && n < 1700);
Ok(())
}
#[test]
fn test_contour_colors() -> Result<(), StrError> {
// contour object and options
let mut contour = Contour::new();
contour
.set_colors(&vec!["red", "green", "blue"])
.set_levels(&vec![1.0, 3.0, 5.0, 7.0])
.set_no_lines(true)
.set_no_labels(true)
.set_no_inline_labels(true)
.set_no_colorbar(true);
// draw contour
let n = 9;
let (x, y, z) = generate3d(-2.0, 2.0, -2.0, 2.0, n, n, |x, y| x * x + y * y);
contour.draw(&x, &y, &z);
// add contour to plot
let mut plot = Plot::new();
plot.add(&contour);
// save figure
let path = Path::new(OUT_DIR).join("integ_contour_colors.svg");
plot.save(&path)?;
// check number of lines
let file = File::open(path).map_err(|_| "cannot open file")?;
let buffered = BufReader::new(file);
let lines_iter = buffered.lines();
assert!(lines_iter.count() > 580);
Ok(())
}
#[test]
fn test_contour_colormap_index() -> Result<(), StrError> {
for index in 0..10 {
// contour object and options
let mut contour = Contour::new();
contour
.set_colormap_index(index)
.set_no_lines(true)
.set_no_labels(true)
.set_no_inline_labels(true)
.set_no_colorbar(true);
// draw contour
let n = 9;
let (x, y, z) = generate3d(-2.0, 2.0, -2.0, 2.0, n, n, |x, y| x * x + y * y);
contour.draw(&x, &y, &z);
// add contour to plot
let mut plot = Plot::new();
plot.add(&contour);
// save figure
let filename = format!("integ_contour_colormap_{}.svg", index);
let path = Path::new(OUT_DIR).join(&filename);
plot.save(&path)?;
// check number of lines
let file = File::open(path).map_err(|_| "cannot open file")?;
let buffered = BufReader::new(file);
let lines_iter = buffered.lines();
assert!(lines_iter.count() > 770);
}
Ok(())
}
#[test]
fn test_contour_colormap_name() -> Result<(), StrError> {
for name in ["Pastel1", "tab20c", "gnuplot2"] {
// contour object and options
let mut contour = Contour::new();
contour
.set_colormap_name(name)
.set_no_lines(true)
.set_no_labels(true)
.set_no_inline_labels(true)
.set_no_colorbar(true);
// draw contour
let n = 9;
let (x, y, z) = generate3d(-2.0, 2.0, -2.0, 2.0, n, n, |x, y| x * x + y * y);
contour.draw(&x, &y, &z);
// add contour to plot
let mut plot = Plot::new();
plot.add(&contour);
// save figure
let filename = format!("integ_contour_colormap_{}.svg", name);
let path = Path::new(OUT_DIR).join(&filename);
plot.save(&path)?;
// check number of lines
let file = File::open(path).map_err(|_| "cannot open file")?;
let buffered = BufReader::new(file);
let lines_iter = buffered.lines();
assert!(lines_iter.count() > 770);
}
Ok(())
}
#[test]
fn test_contour_draw_tri() -> Result<(), StrError> {
// point coordinates (two triangles in a square)
let x = vec![0.0, 1.0, 2.0, 3.0, 0.5, 1.5, 2.5, 1.0, 2.0, 1.5];
let y = vec![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 3.0];
let connectivity = vec![
vec![0, 1, 4],
vec![1, 5, 4],
vec![2, 6, 5],
vec![4, 5, 7],
vec![5, 8, 7],
vec![7, 8, 9],
vec![1, 2, 5],
vec![2, 3, 6],
];
// elevations
let mut z = vec![0.0; x.len()];
for i in 0..x.len() {
z[i] = x[i] + y[i];
}
// contour
let mut contour = Contour::new();
contour
.set_no_fill(false)
.set_colors(&vec!["#fcaeae", "#da98d1", "#c45178", "#5594d2", "#e6af69", "#e6d969"])
.set_colorbar_label("x + y")
.set_line_color("black")
.set_line_style("-")
.set_line_width(1.5)
.set_fill_alpha(1.0)
.set_line_alpha(1.0)
.set_tri_show_edges(true)
.set_tri_edges_color("green")
.set_tri_edges_line_width(1.0)
.set_tri_edges_line_style("--");
// draw contour
contour.draw_tri(&x, &y, &z, &connectivity);
// add contour to plot
let mut plot = Plot::new();
plot.add(&contour);
// save figure
let path = Path::new(OUT_DIR).join("integ_contour_draw_tri.svg");
plot.set_equal_axes(true).set_show_errors(true).save(&path)?;
// check number of lines
let file = File::open(path).map_err(|_| "cannot open file")?;
let buffered = BufReader::new(file);
let lines_iter = buffered.lines();
let n = lines_iter.count();
assert!(n > 1030 && n < 1090);
Ok(())
}
#[test]
fn test_contour_colorbar_options() -> Result<(), StrError> {
// data
let n = 9;
let (x, y, z) = generate3d(-2.0, 2.0, 0.0, 2.0, n, n, |x, y| x * x + y * y);
// contour A
let mut contour_a = Contour::new();
contour_a
.set_colorbar_axes("right", 5.0, 0.1)
.set_colorbar_label("x² + y²")
.draw(&x, &y, &z);
// contour B
let mut contour_b = Contour::new();
contour_b
.set_colorbar_extra("fraction=0.12,pad=0.12")
.set_colorbar_location("bottom")
.set_colorbar_label("x² + y²")
.set_colors(&vec!["#fcaeae", "#da98d1", "#c45178", "#5594d2", "#e6af69", "#e6d969"])
.draw(&x, &y, &z);
// add contour to plot
let mut plot = Plot::new();
plot.set_gaps(0.3, 0.0);
plot.set_subplot(1, 2, 1).set_equal_axes(true).add(&contour_a);
plot.set_subplot(1, 2, 2).set_equal_axes(true).add(&contour_b);
// save figure
let path = Path::new(OUT_DIR).join("integ_contour_colorbar_options.svg");
plot.set_figure_size_points(600.0, 300.0)
.set_equal_axes(true)
.set_show_errors(true)
.save(&path)?;
// check number of lines
let file = File::open(path).map_err(|_| "cannot open file")?;
let buffered = BufReader::new(file);
let lines_iter = buffered.lines();
let n = lines_iter.count();
assert!(n > 2150 && n < 2200);
Ok(())
}