-
-
Notifications
You must be signed in to change notification settings - Fork 559
/
test_palettes.py
328 lines (267 loc) · 10.1 KB
/
test_palettes.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# tests.test_style.test_palettes
# Tests the palettes module of the yellowbrick library.
#
# Author: Benjamin Bengfort <bbengfort@districtdatalabs.com>
# Created: Tue Oct 04 16:21:58 2016 -0400
#
# Copyright (C) 2016 District Data Labs
# For license information, see LICENSE.txt
#
# ID: test_palettes.py [c6aff34] benjamin@bengfort.com $
"""
Tests the palettes module of the yellowbrick library.
"""
##########################################################################
## Imports
##########################################################################
import warnings
import unittest
import numpy as np
import matplotlib as mpl
from yellowbrick.exceptions import *
from yellowbrick.style.palettes import *
from yellowbrick.style.colors import get_color_cycle
from yellowbrick.style.rcmod import set_aesthetic, set_palette
from yellowbrick.style.palettes import color_sequence, color_palette
from yellowbrick.style.palettes import ColorPalette, PALETTES, SEQUENCES
from tests.base import VisualTestCase
##########################################################################
## Color Palette Tests
##########################################################################
class ColorPaletteObjectTests(VisualTestCase):
"""
Tests the ColorPalette object
"""
def test_init_palette_by_name(self):
"""
Test that a palette can be initialized by name
"""
# Try all the names in the palettes
for name, value in PALETTES.items():
try:
palette = ColorPalette(name)
except YellowbrickValueError:
self.fail(
"Could not instantiate {} color palette by name".format(name)
)
self.assertEqual(value, palette)
# Try a name not in PALETTES
with self.assertRaises(YellowbrickValueError):
self.assertNotIn('foo', PALETTES, "Cannot test bad name 'foo' it is in PALETTES!")
palette = ColorPalette('foo')
def test_init_palette_by_list(self):
"""
Test that a palette can be initialized by a list
"""
# Try all the values in the palettes (HEX)
for value in PALETTES.values():
palette = ColorPalette(value)
self.assertEqual(len(value), len(palette))
# Try all the values converted to RGB
for value in PALETTES.values():
palette = ColorPalette(map(mpl.colors.colorConverter.to_rgb, value))
self.assertEqual(len(value), len(palette))
def test_color_palette_context(self):
"""
Test ColorPalette context management
"""
default = color_palette()
context = color_palette('dark')
with ColorPalette('dark') as palette:
self.assertIsInstance(palette, ColorPalette)
self.assertEqual(get_color_cycle(), context)
self.assertEqual(get_color_cycle(), default)
def test_as_hex_as_rgb(self):
"""
Test the conversion of a ColorPalette to hex values and back to rgb
"""
palette = color_palette('flatui')
expected = PALETTES['flatui']
morgified = palette.as_hex()
self.assertIsNot(morgified, palette)
self.assertIsInstance(morgified, ColorPalette)
self.assertEqual(morgified, expected)
remorgified = morgified.as_rgb()
self.assertIsNot(remorgified, morgified)
self.assertIsNot(remorgified, palette)
self.assertEqual(remorgified, palette)
@unittest.skip("not implemented yet")
def test_plot_color_palette(self):
"""
Test the plotting of a color palette for color visualization
"""
raise NotImplementedError(
"Not quite sure how to implement this yet"
)
class ColorPaletteFunctionTests(VisualTestCase):
"""
Tests the color_palette function.
"""
def test_current_palette(self):
"""
Test modifying the current palette with a simple palette
"""
pal = color_palette(["red", "blue", "green"], 3)
set_palette(pal, 3)
self.assertEqual(pal, get_color_cycle())
# Reset the palette
set_aesthetic()
def test_palette_context(self):
"""
Test the context manager for the color_palette function
"""
default_pal = color_palette()
context_pal = color_palette("muted")
with color_palette(context_pal):
self.assertEqual(get_color_cycle(), context_pal)
self.assertEqual(get_color_cycle(), default_pal)
def test_big_palette_context(self):
"""
Test that the context manager also resets the number of colors
"""
original_pal = color_palette("accent", n_colors=8)
context_pal = color_palette("bold", 10)
set_palette(original_pal)
with color_palette(context_pal, 10):
self.assertEqual(get_color_cycle(), context_pal)
self.assertEqual(get_color_cycle(), original_pal)
# Reset default
set_aesthetic()
def test_yellowbrick_palettes(self):
"""
Test the yellowbrick palettes have length 6 (bgrmyck)
"""
pals = ["accent", "dark", "pastel", "bold", "muted"]
for name in pals:
pal_out = color_palette(name)
self.assertEqual(len(pal_out), 6, "{} is not of len 6".format(name))
def test_seaborn_palettes(self):
"""
Test the seaborn palettes have length 6 (bgrmyck)
"""
pals = ["sns_deep", "sns_muted", "sns_pastel",
"sns_bright", "sns_dark", "sns_colorblind"]
for name in pals:
pal_out = color_palette(name)
self.assertEqual(len(pal_out), 6)
def test_other_palettes(self):
"""
Test that the other palettes exist
"""
pals = ["flatui", "paired", "neural_paint", "set1"]
for name in pals:
pal_out = color_palette(name)
self.assertTrue(pal_out)
def test_bad_palette_name(self):
"""
Test that a bad palette name raises an exception
"""
with self.assertRaises(ValueError):
color_palette("IAmNotAPalette")
with self.assertRaises(YellowbrickValueError):
color_palette("IAmNotAPalette")
def test_bad_palette_colors(self):
"""
Test that bad color names raise an exception
"""
pal = ["red", "blue", "iamnotacolor"]
with self.assertRaises(ValueError):
color_palette(pal)
with self.assertRaises(YellowbrickValueError):
color_palette(pal)
def test_palette_is_list_of_tuples(self):
"""
Assert that color_palette returns a list of RGB tuples
"""
pal_in = np.array(["red", "blue", "green"])
pal_out = color_palette(pal_in, 3)
self.assertIsInstance(pal_out, list)
self.assertIsInstance(pal_out[0], tuple)
self.assertIsInstance(pal_out[0][0], float)
self.assertEqual(len(pal_out[0]), 3)
def test_palette_cycles(self):
"""
Test that the color palette cycles for more colors
"""
accent = color_palette("accent")
double_accent = color_palette("accent", 12)
self.assertEqual(double_accent, accent + accent)
@unittest.skip("Discovered this commented out, don't know why")
def test_cbrewer_qual(self):
"""
Test colorbrewer qualitative palettes
"""
pal_short = mpl_palette("Set1", 4)
pal_long = mpl_palette("Set1", 6)
self.assertEqual(pal_short, pal_long[:4])
pal_full = palettes.mpl_palette("Set2", 8)
pal_long = palettes.mpl_palette("Set2", 10)
self.assertEqual(pal_full, pal_long[:8])
def test_color_codes(self):
"""
Test the setting of color codes
"""
set_color_codes("accent")
colors = color_palette("accent") + ["0.06666666666666667"]
for code, color in zip("bgrmyck", colors):
rgb_want = mpl.colors.colorConverter.to_rgb(color)
rgb_got = mpl.colors.colorConverter.to_rgb(code)
self.assertEqual(rgb_want, rgb_got)
set_color_codes("reset")
def test_as_hex(self):
"""
Test converting a color palette to hex and back to rgb.
"""
pal = color_palette("accent")
for rgb, hex in zip(pal, pal.as_hex()):
self.assertEqual(mpl.colors.rgb2hex(rgb), hex)
for rgb_e, rgb_v in zip(pal, pal.as_hex().as_rgb()):
self.assertEqual(rgb_e, rgb_v)
def test_get_color_cycle(self):
"""
Test getting the default color cycle
"""
with warnings.catch_warnings():
warnings.simplefilter('ignore')
result = get_color_cycle()
expected = mpl.rcParams['axes.color_cycle']
self.assertEqual(result, expected)
def test_preserved_palette_length(self):
"""
Test palette length is preserved when modified
"""
pal_in = color_palette("Set1", 10)
pal_out = color_palette(pal_in)
self.assertEqual(pal_in, pal_out)
def test_color_sequence(self):
"""
Ensure the color sequence returns listed colors.
"""
for name, ncols in SEQUENCES.items():
for n in ncols.keys():
cmap = color_sequence(name, n)
self.assertEqual(name, cmap.name)
self.assertEqual(n, cmap.N)
def test_color_sequence_default(self):
"""
Assert the default color sequence is RdBu
"""
cmap = color_sequence()
self.assertEqual(cmap.name, "RdBu")
self.assertEqual(cmap.N, 11)
def test_color_sequence_unrecocognized(self):
"""
Test value errors for unrecognized sequences
"""
with self.assertRaises(YellowbrickValueError):
color_sequence('PepperBucks', 3)
def test_color_sequence_bounds(self):
"""
Test color sequence out of bounds value error
"""
with self.assertRaises(YellowbrickValueError):
color_sequence('RdBu', 18)
with self.assertRaises(YellowbrickValueError):
color_sequence('RdBu', 2)
if __name__ == "__main__":
unittest.main()