| 1 |
from wordcloud import WordCloud, get_single_color_func, ImageColorGenerator |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
import pytest |
| 5 |
|
| 6 |
from random import Random |
| 7 |
from numpy.testing import assert_array_equal |
| 8 |
from PIL import Image |
| 9 |
import xml.etree.ElementTree as ET |
| 10 |
|
| 11 |
import matplotlib |
| 12 |
matplotlib.use('Agg') |
| 13 |
|
| 14 |
THIS = """The Zen of Python, by Tim Peters |
| 15 |
|
| 16 |
Beautiful is better than ugly. |
| 17 |
Explicit is better than implicit. |
| 18 |
Simple is better than complex. |
| 19 |
Complex is better than complicated. |
| 20 |
Flat is better than nested. |
| 21 |
Sparse is better than dense. |
| 22 |
Readability counts. |
| 23 |
Special cases aren't special enough to break the rules. |
| 24 |
Although practicality beats purity. |
| 25 |
Errors should never pass silently. |
| 26 |
Unless explicitly silenced. |
| 27 |
In the face of ambiguity, refuse the temptation to guess. |
| 28 |
There should be one-- and preferably only one --obvious way to do it. |
| 29 |
Although that way may not be obvious at first unless you're Dutch. |
| 30 |
Now is better than never. |
| 31 |
Although never is often better than *right* now. |
| 32 |
If the implementation is hard to explain, it's a bad idea. |
| 33 |
If the implementation is easy to explain, it may be a good idea. |
| 34 |
Namespaces are one honking great idea -- let's do more of those! |
| 35 |
|
| 36 |
3 . 14 15 92 65 35 89 79 32 38 46 26 433 |
| 37 |
83 27 95 02 88 41 97 16 93 99 37 510 |
| 38 |
58 20 97 49 44 59 23 07 81 64 06 286 |
| 39 |
20 89 98 62 80 34 82 53 42 11 70 679 |
| 40 |
82 14 80 86 51 32 82 30 66 47 09 384 |
| 41 |
46 09 55 05 82 23 17 25 35 94 08 128 |
| 42 |
""" |
| 43 |
|
| 44 |
STOPWORDED_COLLOCATIONS = """ |
| 45 |
thank you very much |
| 46 |
thank you very much |
| 47 |
thank you very much |
| 48 |
thanks |
| 49 |
""" |
| 50 |
|
| 51 |
STOPWORDED_COLLOCATIONS_UPPERCASE = """ |
| 52 |
Thank you very much |
| 53 |
Thank you very much |
| 54 |
Thank you very much |
| 55 |
thank you very much |
| 56 |
hi There |
| 57 |
Hi there |
| 58 |
Hi There |
| 59 |
thanks |
| 60 |
""" |
| 61 |
|
| 62 |
SMALL_CANVAS = """ |
| 63 |
better late than never someone will say |
| 64 |
""" |
| 65 |
|
| 66 |
|
| 67 |
def test_collocations(): |
| 68 |
wc = WordCloud(collocations=False, stopwords=set()) |
| 69 |
wc.generate(THIS) |
| 70 |
|
| 71 |
wc2 = WordCloud(collocations=True, stopwords=set()) |
| 72 |
wc2.generate(THIS) |
| 73 |
|
| 74 |
assert "is better" in wc2.words_ |
| 75 |
assert "is better" not in wc.words_ |
| 76 |
assert "way may" not in wc2.words_ |
| 77 |
|
| 78 |
|
| 79 |
def test_collocation_stopwords(): |
| 80 |
wc = WordCloud(collocations=True, stopwords={"you", "very"}, collocation_threshold=9) |
| 81 |
wc.generate(STOPWORDED_COLLOCATIONS) |
| 82 |
|
| 83 |
assert "thank you" not in wc.words_ |
| 84 |
assert "very much" not in wc.words_ |
| 85 |
assert "thank" in wc.words_ |
| 86 |
# a bigram of all stopwords will be removed |
| 87 |
assert "you very" not in wc.words_ |
| 88 |
|
| 89 |
|
| 90 |
def test_collocation_stopwords_uppercase(): |
| 91 |
wc = WordCloud(collocations=True, stopwords={"thank", "hi", "there"}, collocation_threshold=9) |
| 92 |
wc.generate(STOPWORDED_COLLOCATIONS_UPPERCASE) |
| 93 |
|
| 94 |
assert "Thank you" not in wc.words_ |
| 95 |
assert "thank you" not in wc.words_ |
| 96 |
assert "Thank" not in wc.words_ |
| 97 |
# a bigram of all stopwords will be removed |
| 98 |
assert "hi There" not in wc.words_ |
| 99 |
assert "Hi there" not in wc.words_ |
| 100 |
assert "Hi There" not in wc.words_ |
| 101 |
|
| 102 |
|
| 103 |
def test_plurals_numbers(): |
| 104 |
text = THIS + "\n" + "1 idea 2 ideas three ideas although many Ideas" |
| 105 |
wc = WordCloud(stopwords=[]).generate(text) |
| 106 |
# not capitalized usually |
| 107 |
assert "Ideas" not in wc.words_ |
| 108 |
# plural removed |
| 109 |
assert "ideas" not in wc.words_ |
| 110 |
# usually capitalized |
| 111 |
assert "although" not in wc.words_ |
| 112 |
assert "idea" in wc.words_ |
| 113 |
assert "Although" in wc.words_ |
| 114 |
assert "better than" in wc.words_ |
| 115 |
|
| 116 |
|
| 117 |
def test_multiple_s(): |
| 118 |
text = 'flo flos floss flosss' |
| 119 |
wc = WordCloud(stopwords=[]).generate(text) |
| 120 |
assert "flo" in wc.words_ |
| 121 |
assert "flos" not in wc.words_ |
| 122 |
assert "floss" in wc.words_ |
| 123 |
assert "flosss" in wc.words_ |
| 124 |
# not normalizing means that the one with just one s is kept |
| 125 |
wc = WordCloud(stopwords=[], normalize_plurals=False).generate(text) |
| 126 |
assert "flo" in wc.words_ |
| 127 |
assert "flos" in wc.words_ |
| 128 |
assert "floss" in wc.words_ |
| 129 |
assert "flosss" in wc.words_ |
| 130 |
|
| 131 |
|
| 132 |
def test_empty_text(): |
| 133 |
# test originally empty text raises an exception |
| 134 |
wc = WordCloud(stopwords=[]) |
| 135 |
with pytest.raises(ValueError): |
| 136 |
wc.generate('') |
| 137 |
|
| 138 |
# test empty-after-filtering text raises an exception |
| 139 |
wc = WordCloud(stopwords=['a', 'b']) |
| 140 |
with pytest.raises(ValueError): |
| 141 |
wc.generate('a b a') |
| 142 |
|
| 143 |
|
| 144 |
def test_default(): |
| 145 |
# test that default word cloud creation and conversions work |
| 146 |
wc = WordCloud(max_words=50) |
| 147 |
wc.generate(THIS) |
| 148 |
|
| 149 |
# check for proper word extraction |
| 150 |
assert len(wc.words_) == wc.max_words |
| 151 |
|
| 152 |
# check that we got enough words |
| 153 |
assert len(wc.layout_) == wc.max_words |
| 154 |
|
| 155 |
# check image export |
| 156 |
wc_image = wc.to_image() |
| 157 |
assert wc_image.size == (wc.width, wc.height) |
| 158 |
|
| 159 |
# check that numpy conversion works |
| 160 |
wc_array = np.array(wc) |
| 161 |
assert_array_equal(wc_array, wc.to_array()) |
| 162 |
|
| 163 |
# check size |
| 164 |
assert wc_array.shape == (wc.height, wc.width, 3) |
| 165 |
|
| 166 |
|
| 167 |
def test_stopwords_lowercasing(): |
| 168 |
# test that capitalized stopwords work. |
| 169 |
wc = WordCloud(stopwords=["Beautiful"]) |
| 170 |
processed = wc.process_text(THIS) |
| 171 |
words = [count[0] for count in processed] |
| 172 |
assert "Beautiful" not in words |
| 173 |
|
| 174 |
|
| 175 |
def test_writing_to_file(tmpdir): |
| 176 |
wc = WordCloud() |
| 177 |
wc.generate(THIS) |
| 178 |
|
| 179 |
# check writing to file |
| 180 |
filename = str(tmpdir.join("word_cloud.png")) |
| 181 |
wc.to_file(filename) |
| 182 |
loaded_image = Image.open(filename) |
| 183 |
assert loaded_image.size == (wc.width, wc.height) |
| 184 |
|
| 185 |
|
| 186 |
def test_check_errors(): |
| 187 |
wc = WordCloud() |
| 188 |
|
| 189 |
try: |
| 190 |
np.array(wc) |
| 191 |
raise AssertionError("np.array(wc) didn't raise") |
| 192 |
except ValueError as e: |
| 193 |
assert "call generate" in str(e) |
| 194 |
|
| 195 |
try: |
| 196 |
wc.recolor() |
| 197 |
raise AssertionError("wc.recolor didn't raise") |
| 198 |
except ValueError as e: |
| 199 |
assert "call generate" in str(e) |
| 200 |
|
| 201 |
|
| 202 |
def test_svg_syntax(): |
| 203 |
wc = WordCloud() |
| 204 |
wc.generate(THIS) |
| 205 |
svg = wc.to_svg() |
| 206 |
ET.fromstring(svg) |
| 207 |
|
| 208 |
|
| 209 |
def test_recolor(): |
| 210 |
wc = WordCloud(max_words=50, colormap="jet") |
| 211 |
wc.generate(THIS) |
| 212 |
array_before = wc.to_array() |
| 213 |
wc.recolor() |
| 214 |
array_after = wc.to_array() |
| 215 |
# check that the same places are filled |
| 216 |
assert_array_equal(array_before.sum(axis=-1) != 0, |
| 217 |
array_after.sum(axis=-1) != 0) |
| 218 |
# check that they are not the same |
| 219 |
assert np.abs(array_before - array_after).sum() > 10000 |
| 220 |
|
| 221 |
# check that recoloring is deterministic |
| 222 |
wc.recolor(random_state=10) |
| 223 |
wc_again = wc.to_array() |
| 224 |
assert_array_equal(wc_again, wc.recolor(random_state=10)) |
| 225 |
|
| 226 |
|
| 227 |
def test_random_state(): |
| 228 |
# check that random state makes everything deterministic |
| 229 |
wc = WordCloud(random_state=0) |
| 230 |
wc2 = WordCloud(random_state=0) |
| 231 |
wc.generate(THIS) |
| 232 |
wc2.generate(THIS) |
| 233 |
assert_array_equal(wc, wc2) |
| 234 |
|
| 235 |
|
| 236 |
def test_mask(): |
| 237 |
# test masks |
| 238 |
|
| 239 |
# check that using an empty mask is equivalent to not using a mask |
| 240 |
wc = WordCloud(random_state=42) |
| 241 |
wc.generate(THIS) |
| 242 |
mask = np.zeros(np.array(wc).shape[:2], dtype=int) |
| 243 |
wc_mask = WordCloud(mask=mask, random_state=42) |
| 244 |
wc_mask.generate(THIS) |
| 245 |
assert_array_equal(wc, wc_mask) |
| 246 |
|
| 247 |
# use actual nonzero mask |
| 248 |
mask = np.zeros((234, 456), dtype=int) |
| 249 |
mask[100:150, 300:400] = 255 |
| 250 |
|
| 251 |
wc = WordCloud(mask=mask) |
| 252 |
wc.generate(THIS) |
| 253 |
wc_array = np.array(wc) |
| 254 |
assert mask.shape == wc_array.shape[:2] |
| 255 |
assert_array_equal(wc_array[mask != 0], 0) |
| 256 |
assert wc_array[mask == 0].sum() > 10000 |
| 257 |
|
| 258 |
|
| 259 |
def test_mask_contour(): |
| 260 |
# test mask contour is created, learn more at: |
| 261 |
# https://github.com/amueller/word_cloud/pull/348#issuecomment-370883873 |
| 262 |
mask = np.zeros((234, 456), dtype=int) |
| 263 |
mask[100:150, 300:400] = 255 |
| 264 |
|
| 265 |
sm = WordCloud(mask=mask, contour_width=1, contour_color='blue') |
| 266 |
sm.generate(THIS) |
| 267 |
sm_array = np.array(sm) |
| 268 |
sm_total = sm_array[100:150, 300:400].sum() |
| 269 |
|
| 270 |
lg = WordCloud(mask=mask, contour_width=20, contour_color='blue') |
| 271 |
lg.generate(THIS) |
| 272 |
lg_array = np.array(lg) |
| 273 |
lg_total = lg_array[100:150, 300:400].sum() |
| 274 |
|
| 275 |
sc = WordCloud(mask=mask, contour_width=1, scale=2, contour_color='blue') |
| 276 |
sc.generate(THIS) |
| 277 |
sc_array = np.array(sc) |
| 278 |
sc_total = sc_array[100:150, 300:400].sum() |
| 279 |
|
| 280 |
# test `contour_width` |
| 281 |
assert lg_total > sm_total |
| 282 |
|
| 283 |
# test contour varies with `scale` |
| 284 |
assert sc_total > sm_total |
| 285 |
|
| 286 |
# test `contour_color` |
| 287 |
assert all(sm_array[100, 300] == [0, 0, 255]) |
| 288 |
|
| 289 |
|
| 290 |
def test_single_color_func(): |
| 291 |
# test single color function for different color formats |
| 292 |
random = Random(42) |
| 293 |
|
| 294 |
red_function = get_single_color_func('red') |
| 295 |
assert red_function(random_state=random) == 'rgb(181, 0, 0)' |
| 296 |
|
| 297 |
hex_function = get_single_color_func('#00b4d2') |
| 298 |
assert hex_function(random_state=random) == 'rgb(0, 48, 56)' |
| 299 |
|
| 300 |
rgb_function = get_single_color_func('rgb(0,255,0)') |
| 301 |
assert rgb_function(random_state=random) == 'rgb(0, 107, 0)' |
| 302 |
|
| 303 |
rgb_perc_fun = get_single_color_func('rgb(80%,60%,40%)') |
| 304 |
assert rgb_perc_fun(random_state=random) == 'rgb(97, 72, 48)' |
| 305 |
|
| 306 |
hsl_function = get_single_color_func('hsl(0,100%,50%)') |
| 307 |
assert hsl_function(random_state=random) == 'rgb(201, 0, 0)' |
| 308 |
|
| 309 |
|
| 310 |
def test_single_color_func_grey(): |
| 311 |
# grey is special as it's a corner case |
| 312 |
random = Random(42) |
| 313 |
|
| 314 |
red_function = get_single_color_func('darkgrey') |
| 315 |
assert red_function(random_state=random) == 'rgb(181, 181, 181)' |
| 316 |
assert red_function(random_state=random) == 'rgb(56, 56, 56)' |
| 317 |
|
| 318 |
|
| 319 |
def test_process_text(): |
| 320 |
# test that process function returns a dict |
| 321 |
wc = WordCloud(max_words=50) |
| 322 |
result = wc.process_text(THIS) |
| 323 |
|
| 324 |
# check for proper return type |
| 325 |
assert isinstance(result, dict) |
| 326 |
|
| 327 |
|
| 328 |
def test_process_text_default_patterns(): |
| 329 |
wc = WordCloud(stopwords=set(), include_numbers=True, min_word_length=2) |
| 330 |
words = wc.process_text(THIS) |
| 331 |
|
| 332 |
wc2 = WordCloud(stopwords=set(), include_numbers=True, min_word_length=1) |
| 333 |
words2 = wc2.process_text(THIS) |
| 334 |
|
| 335 |
assert "a" not in words |
| 336 |
assert "3" not in words |
| 337 |
|
| 338 |
assert "a" in words2 |
| 339 |
assert "3" in words2 |
| 340 |
|
| 341 |
|
| 342 |
def test_process_text_regexp_parameter(): |
| 343 |
# test that word processing is influenced by `regexp` |
| 344 |
wc = WordCloud(max_words=50, regexp=r'\w{5}') |
| 345 |
words = wc.process_text(THIS) |
| 346 |
|
| 347 |
assert 'than' not in words |
| 348 |
|
| 349 |
|
| 350 |
def test_generate_from_frequencies(): |
| 351 |
# test that generate_from_frequencies() takes input argument dicts |
| 352 |
wc = WordCloud(max_words=50) |
| 353 |
words = wc.process_text(THIS) |
| 354 |
result = wc.generate_from_frequencies(words) |
| 355 |
|
| 356 |
assert isinstance(result, WordCloud) |
| 357 |
|
| 358 |
|
| 359 |
def test_relative_scaling_zero(): |
| 360 |
# non-regression test for non-integer font size |
| 361 |
wc = WordCloud(relative_scaling=0) |
| 362 |
wc.generate(THIS) |
| 363 |
|
| 364 |
|
| 365 |
def test_unicode_stopwords(): |
| 366 |
wc_unicode = WordCloud(stopwords=[u'Beautiful']) |
| 367 |
try: |
| 368 |
words_unicode = wc_unicode.process_text(unicode(THIS)) |
| 369 |
except NameError: # PY3 |
| 370 |
words_unicode = wc_unicode.process_text(THIS) |
| 371 |
|
| 372 |
wc_str = WordCloud(stopwords=['Beautiful']) |
| 373 |
words_str = wc_str.process_text(str(THIS)) |
| 374 |
|
| 375 |
assert words_unicode == words_str |
| 376 |
|
| 377 |
|
| 378 |
def test_include_numbers(): |
| 379 |
wc_numbers = WordCloud(include_numbers=True) |
| 380 |
wc = wc_numbers.process_text(THIS) |
| 381 |
|
| 382 |
assert '14' in wc.keys() |
| 383 |
|
| 384 |
|
| 385 |
def test_min_word_length(): |
| 386 |
wc_numbers = WordCloud(min_word_length=5) |
| 387 |
wc = wc_numbers.process_text(THIS) |
| 388 |
word_lengths = [len(word) for word in wc.keys()] |
| 389 |
|
| 390 |
assert min(word_lengths) == 5 |
| 391 |
|
| 392 |
|
| 393 |
def test_recolor_too_small(): |
| 394 |
# check exception is raised when image is too small |
| 395 |
colouring = np.array(Image.new('RGB', size=(20, 20))) |
| 396 |
wc = WordCloud(width=30, height=30, random_state=0, min_font_size=1).generate(THIS) |
| 397 |
image_colors = ImageColorGenerator(colouring) |
| 398 |
with pytest.raises(ValueError, match='ImageColorGenerator is smaller than the canvas'): |
| 399 |
wc.recolor(color_func=image_colors) |
| 400 |
|
| 401 |
|
| 402 |
def test_recolor_too_small_set_default(): |
| 403 |
# check no exception is raised when default colour is used |
| 404 |
colouring = np.array(Image.new('RGB', size=(20, 20))) |
| 405 |
wc = WordCloud(max_words=50, width=30, height=30, min_font_size=1).generate(THIS) |
| 406 |
image_colors = ImageColorGenerator(colouring, default_color=(0, 0, 0)) |
| 407 |
wc.recolor(color_func=image_colors) |
| 408 |
|
| 409 |
|
| 410 |
def test_small_canvas(): |
| 411 |
# check font size fallback works on small canvas |
| 412 |
wc = WordCloud(max_words=50, width=21, height=21) |
| 413 |
wc.generate(SMALL_CANVAS) |
| 414 |
assert len(wc.layout_) > 0 |
| 415 |
|
| 416 |
|
| 417 |
def test_tiny_canvas(): |
| 418 |
# check exception if canvas too small for fallback |
| 419 |
w = WordCloud(max_words=50, width=1, height=1) |
| 420 |
with pytest.raises(ValueError, match="Couldn't find space to draw"): |
| 421 |
w.generate(THIS) |
| 422 |
assert len(w.layout_) == 0 |
| 423 |
|
| 424 |
|
| 425 |
def test_coloring_black_works(): |
| 426 |
# check that using black colors works. |
| 427 |
mask = np.zeros((50, 50, 3)) |
| 428 |
image_colors = ImageColorGenerator(mask) |
| 429 |
wc = WordCloud(width=50, height=50, random_state=42, |
| 430 |
color_func=image_colors, min_font_size=1) |
| 431 |
wc.generate(THIS) |
| 432 |
|
| 433 |
|
| 434 |
def test_repeat(): |
| 435 |
short_text = "Some short text" |
| 436 |
wc = WordCloud(stopwords=[]).generate(short_text) |
| 437 |
assert len(wc.layout_) == 3 |
| 438 |
wc = WordCloud(max_words=50, stopwords=[], repeat=True).generate(short_text) |
| 439 |
# multiple of word count larger than max_words |
| 440 |
assert len(wc.layout_) == 51 |
| 441 |
# relative scaling doesn't work well with repeat |
| 442 |
assert wc.relative_scaling == 0 |
| 443 |
# all frequencies are 1 |
| 444 |
assert len(wc.words_) == 3 |
| 445 |
assert_array_equal(list(wc.words_.values()), 1) |
| 446 |
frequencies = [w[0][1] for w in wc.layout_] |
| 447 |
assert_array_equal(frequencies, 1) |
| 448 |
repetition_text = "Some short text with text" |
| 449 |
wc = WordCloud(max_words=52, stopwords=[], repeat=True) |
| 450 |
wc.generate(repetition_text) |
| 451 |
assert len(wc.words_) == 4 |
| 452 |
# normalized frequencies |
| 453 |
assert wc.words_['text'] == 1 |
| 454 |
assert wc.words_['with'] == .5 |
| 455 |
assert len(wc.layout_), wc.max_words |
| 456 |
frequencies = [w[0][1] for w in wc.layout_] |
| 457 |
# check that frequencies are sorted |
| 458 |
assert np.all(np.diff(frequencies) <= 0) |
| 459 |
|
| 460 |
|
| 461 |
def test_zero_frequencies(): |
| 462 |
|
| 463 |
word_cloud = WordCloud() |
| 464 |
|
| 465 |
word_cloud.generate_from_frequencies({'test': 1, 'test1': 0, 'test2': 0}) |
| 466 |
assert len(word_cloud.layout_) == 1 |
| 467 |
assert word_cloud.layout_[0][0][0] == 'test' |
| 468 |
|
| 469 |
|
| 470 |
def test_plural_stopwords(): |
| 471 |
x = '''was was was was was was was was was was was was was was was |
| 472 |
wa |
| 473 |
hello hello hello hello hello hello hello hello |
| 474 |
goodbye good bye maybe yes no''' |
| 475 |
w = WordCloud().generate(x) |
| 476 |
assert w.words_['wa'] < 1 |
| 477 |
|
| 478 |
w = WordCloud(collocations=False).generate(x) |
| 479 |
assert w.words_['wa'] < 1 |
| 480 |
|
| 481 |
|
| 482 |
def test_max_font_size_as_mask_height(): |
| 483 |
# test if max font size will respect the mask height |
| 484 |
x = '''hello hello hello |
| 485 |
bye''' |
| 486 |
|
| 487 |
# Get default wordcloud size |
| 488 |
wcd = WordCloud() |
| 489 |
default_size = (wcd.height, wcd.width) |
| 490 |
# Make sure the size we are using is larger than the default size |
| 491 |
size = (default_size[0] * 2, default_size[1] * 2) |
| 492 |
|
| 493 |
# using mask, all drawable |
| 494 |
mask = np.zeros(size, dtype=int) |
| 495 |
mask[:, :] = 0 |
| 496 |
wc = WordCloud(mask=mask, random_state=42) |
| 497 |
wc.generate(x) |
| 498 |
|
| 499 |
# no mask |
| 500 |
wc2 = WordCloud(width=size[1], height=size[0], random_state=42) |
| 501 |
wc2.generate(x) |
| 502 |
|
| 503 |
# Check if the biggest element has the same font size |
| 504 |
assert wc.layout_[0][1] == wc2.layout_[0][1] |