forked from turicas/plotter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_plotter.py
309 lines (279 loc) · 10.2 KB
/
test_plotter.py
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python
# coding: utf-8
import unittest
import os
import shutil
import tempfile
import inspect
from textwrap import dedent
from plotter import Plotter
TEST_RESULTS_PATH = 'test_results'
try:
shutil.rmtree(TEST_RESULTS_PATH)
except OSError:
pass
os.mkdir(TEST_RESULTS_PATH)
def create_temp_csv(contents):
temp_fp = tempfile.NamedTemporaryFile(delete=False)
temp_fp.write(contents)
temp_fp.close()
return temp_fp.name
def get_filename_from_frame(frame):
test_name = frame.f_code.co_name
return os.path.join(TEST_RESULTS_PATH, test_name + '.png')
class TestCsvPlot(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.data = {}
file_contents = dedent('''
"X Values"
1
2
3
4
5
6
7
8
9
''')
cls.data['int'] = create_temp_csv(file_contents)
file_contents = dedent('''
"X Values","Y Values"
1,1
2,4
3,9
4,16
5,25
6,36
7,49
8,64
9,81
''')
cls.data['int-int'] = create_temp_csv(file_contents)
file_contents = dedent('''
"X Values","Y Values"
8,64
9,81
1,1
3,9
5,25
2,4
4,16
6,36
7,49
''')
cls.data['int-int-unordered'] = create_temp_csv(file_contents)
file_contents = dedent('''
"X Values","Y Values"
2011-01-01,1
2011-01-02,4
2011-01-03,9
2011-01-04,16
2011-01-05,25
2011-01-06,36
2011-01-07,49
2011-01-08,64
2011-01-09,81
''')
cls.data['date-int'] = create_temp_csv(file_contents)
file_contents = dedent('''
"X Values","Y Values","Z Values"
1,1,2011-01-01
4,8,2011-01-02
9,27,2011-01-03
16,64,2011-01-04
25,125,2011-01-05
36,216,2011-01-06
49,343,2011-01-07
64,512,2011-01-08
81,729,2011-01-09
''')
cls.data['int-int-date'] = create_temp_csv(file_contents)
file_contents = dedent('''
"X Values","Y Values","Z Values"
1,1,1
4,8,12
9,27,36
16,64,80
25,125,150
36,216,252
49,343,392
64,512,576
81,729,810
''')
cls.data['int-int-int'] = create_temp_csv(file_contents)
file_contents = dedent('''
animal
dog
cat
dog
dog
dog
cat
cat
human
dog
tiger
tiger
coati
''')
cls.data['animals'] = create_temp_csv(file_contents)
file_contents = dedent('''
product_name,quantity,year
E,235,2004
D,154,2001
A,200,2000
B,300,2000
C,345,2001
D,156,2004
E,233,2001
C,250,2000
D,245,2000
E,190,2000
B,286,2001
D,254,2003
A,249,2004
B,288,2004
C,347,2004
D,144,2002
E,256,2002
A,339,2003
B,386,2003
C,243,2003
C,195,2005
D,231,2005
E,234,2005
''')
cls.data['bar-data'] = create_temp_csv(file_contents)
@classmethod
def tearDownClass(cls):
for filename in cls.data.values():
os.remove(filename)
def test_01_2_columns_with_only_data_no_scatter(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int'])
my_plot.linear(title='Hello, world')
my_plot.save(image_filename)
def test_02_2_columns_with_only_data_scatter(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int'])
my_plot.scatter(x_column='X Values')
my_plot.save(image_filename)
def test_02_2_columns_with_unordered_data_scatter(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int-unordered'])
my_plot.scatter(x_column='X Values')
my_plot.save(image_filename)
def test_03_2_columns_with_one_as_date_no_scatter(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['date-int'])
my_plot.linear()
my_plot.save(image_filename)
def test_04_2_columns_with_one_as_date_scatter(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['date-int'])
my_plot.scatter(x_column='X Values')
my_plot.save(image_filename)
def test_05_3_columns_with_one_as_date_scatter_no_labels(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['date-int'])
my_plot.scatter(x_column='X Values', labels=False)
my_plot.save(image_filename)
def test_06_3_columns_with_one_as_date_and_x_as_int_scatter(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int-date'])
my_plot.scatter(x_column='Y Values')
my_plot.save(image_filename)
def test_06_scatter_with_x_column_as_string(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int-date'])
my_plot.scatter(x_column='Y Values')
my_plot.save(image_filename)
def test_07_1_column_barplot(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int'])
my_plot.bar()
my_plot.save(image_filename)
def test_08_2_columns_barplot(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int'])
my_plot.bar()
my_plot.save(image_filename)
def test_08_2_columns_barplot_with_x_rotation(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int'])
my_plot.bar(x_rotation=45)
my_plot.save(image_filename)
def test_09_bar_plot_and_1_column_with_count(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['animals'])
my_plot.bar(count='animal', legends={'animal': 'Animals'})
my_plot.save(image_filename)
def test_10_2_columns_plot_with_one_as_label(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int'])
my_plot.linear(x_labels='Y Values')
my_plot.save(image_filename)
def test_11_ignore_columns_linear(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int-int'])
my_plot.linear(x_labels='Y Values', ignore=['X Values', 'Y Values'])
my_plot.save(image_filename)
def test_11_ignore_columns_scatter(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int-int'])
my_plot.scatter(x_column='Y Values', ignore=['X Values'])
my_plot.save(image_filename)
def test_12_plot_stacked_bars(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['bar-data'])
my_plot.stacked_bar(x_column='year', y_column='quantity',
y_labels='product_name',
title='Stacked bars example')
my_plot.save(image_filename)
def test_13_use_colors_instead_of_colormap_linear(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int-int'])
my_plot.linear(x_labels='Y Values', colors=['red', 'blue'])
my_plot.save(image_filename)
def test_13_use_colors_instead_of_colormap_scatter(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int-int'])
my_plot.scatter(x_column='Y Values', colors=['red', 'blue'])
my_plot.save(image_filename)
def test_13_use_colors_instead_of_colormap_bar(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['int-int-int'])
my_plot.bar(colors=['red', 'blue', 'yellow'])
my_plot.save(image_filename)
def test_13_use_colors_instead_of_colormap_stacked_bar(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['bar-data'])
my_plot.stacked_bar(x_column='year', y_column='quantity',
y_labels='product_name',
title='Stacked bars example',
colors=['red', 'blue', 'yellow', 'magenta', 'gray'])
my_plot.save(image_filename)
def test_15_a_figure_with_a_lot_of_subplots(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['bar-data'], rows=2, cols=2)
my_plot.linear(ignore=['product_name', 'year'], title='Linear plot')
my_plot.scatter(x_column='quantity', title='Scatter plot')
my_plot.stacked_bar(x_column='year', y_column='quantity',
y_labels='product_name', title='Stacked bars plot',
x_rotation=45)
my_plot.bar(count='year', x_rotation=45, title='Bar plot')
my_plot.save(image_filename)
def test_should_raises_OverflowError_when_exceed_number_of_subplots(self):
my_plot = Plotter(self.data['bar-data'], rows=2, cols=1)
my_plot.linear(ignore=['product_name', 'year'])
my_plot.linear(ignore=['product_name', 'year'])
with self.assertRaises(OverflowError):
my_plot.linear(ignore=['product_name', 'year'])
def test_16_radar_plot(self):
image_filename = get_filename_from_frame(inspect.currentframe())
my_plot = Plotter(self.data['bar-data'])
my_plot.radar(axis_labels='product_name', values='quantity',
legends='year', title='Products by year')
my_plot.save(image_filename)