-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_barplot.rs
More file actions
170 lines (143 loc) · 4.71 KB
/
test_barplot.rs
File metadata and controls
170 lines (143 loc) · 4.71 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
use plotpy::{Barplot, Plot, StrError};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
const OUT_DIR: &str = "/tmp/plotpy/integ_tests";
#[test]
fn test_barplot_1() -> Result<(), StrError> {
// data
let xx = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let yy = [5, 4, 3, 2, 1, 0, 1, 2, 3, 4];
// barplot object and options
let mut barplot = Barplot::new();
barplot.draw(&xx, &yy);
let mut plot = Plot::new();
plot.add(&barplot);
// save figure
let path = Path::new(OUT_DIR).join("integ_barplot_1.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 c = lines_iter.count();
assert!(c > 500 && c < 550);
Ok(())
}
#[test]
fn test_barplot_2() -> Result<(), StrError> {
// data
let species = ["Adelie", "Chinstrap", "Gentoo"];
let sex_counts = HashMap::from([
("Male", ([73.0, 34.0, 61.0], ["red", "green", "blue"])),
("Female", ([73.0, 34.0, 58.0], ["#DE3163", "#40E0D0", "#6495ED"])),
]);
// barplot object and options
let mut bar = Barplot::new();
bar.set_with_text("center")
.set_width(0.6)
.set_extra("edgecolor='black'");
// draw bars
let mut bottom = [0.0, 0.0, 0.0];
for (sex, (sex_count, colors)) in &sex_counts {
bar.set_label(sex)
.set_colors(colors)
.set_bottom(&bottom)
.draw_with_str(&species, sex_count);
for i in 0..sex_count.len() {
bottom[i] += sex_count[i];
}
}
// plot
let mut plot = Plot::new();
plot.add(&bar);
// save figure
let path = Path::new(OUT_DIR).join("integ_barplot_2.svg");
plot.set_title("Number of penguins by sex").legend().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 c = lines_iter.count();
assert!(c > 1180 && c < 1200);
Ok(())
}
#[test]
fn test_barplot_3() -> Result<(), StrError> {
// data
let fruits = [1.0, 2.0, 3.0];
let prices = [10.0, 20.0, 30.0];
let errors = [3.0, 2.0, 1.0];
// barplot object and options
let mut bar = Barplot::new();
bar.set_errors(&errors)
.set_horizontal(true)
.set_with_text("edge")
.draw(&fruits, &prices);
// plot
let mut plot = Plot::new();
plot.set_inv_y().add(&bar);
// save figure
let path = Path::new(OUT_DIR).join("integ_barplot_3.svg");
plot.set_title("Fruits").set_label_x("price").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 c = lines_iter.count();
assert!(c > 660 && c < 700);
Ok(())
}
#[test]
fn test_barplot_4() -> Result<(), StrError> {
// data
let fruits = ["Apple", "Banana", "Orange"];
let prices = [10.0, 20.0, 30.0];
let errors = [3.0, 2.0, 1.0];
// barplot object and options
let mut bar = Barplot::new();
bar.set_errors(&errors)
.set_horizontal(true)
.set_with_text("edge")
.draw_with_str(&fruits, &prices);
// plot
let mut plot = Plot::new();
plot.set_inv_y().add(&bar);
// save figure
let path = Path::new(OUT_DIR).join("integ_barplot_4.svg");
plot.set_title("Fruits").set_label_x("price").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 c = lines_iter.count();
assert!(c > 770 && c < 810);
Ok(())
}
#[test]
fn test_barplot_5() -> Result<(), StrError> {
// data
let y = vec![-2.5, -15.0, -5.0, -7.5, -2.5, -5.0, -17.5];
let e = vec![0.5, 0.4, 0.1, 0.7, 0.2, 0.0, 1.7];
let _x: Vec<f64> = (0..y.len()).map(|a| a as f64).collect();
let _x_str = vec!["Uno", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete"];
// barplot
let mut bar = Barplot::new();
bar.set_errors(&e)
// .draw(&_x, &y); // requires numbers, as expected
.draw_with_str(&_x_str, &y); // requires string, as expected
// plot
let mut plot = Plot::new();
plot.add(&bar);
// save figure
let path = Path::new(OUT_DIR).join("integ_barplot_5.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 c = lines_iter.count();
assert!(c > 830 && c < 900);
Ok(())
}