|
5 | 5 | import mock |
6 | 6 | import modAL.models.base |
7 | 7 | import modAL.models.learners |
8 | | -import modAL.uncertainty |
9 | | -import modAL.disagreement |
10 | | -import modAL.density |
11 | 8 | import modAL.utils.selection |
12 | 9 | import modAL.utils.validation |
13 | 10 | import modAL.utils.combination |
| 11 | +import modAL.acquisition |
| 12 | +import modAL.batch |
| 13 | +import modAL.density |
| 14 | +import modAL.disagreement |
| 15 | +import modAL.expected_error |
14 | 16 | import modAL.multilabel |
| 17 | +import modAL.uncertainty |
15 | 18 |
|
16 | 19 | from copy import deepcopy |
17 | 20 | from itertools import chain, product |
@@ -280,6 +283,119 @@ def test_selection(self): |
280 | 283 | modAL.acquisition.max_UCB(optimizer, X, beta=np.random.rand(), n_instances=n_instances) |
281 | 284 |
|
282 | 285 |
|
| 286 | +class TestDensity(unittest.TestCase): |
| 287 | + |
| 288 | + def test_similarize_distance(self): |
| 289 | + from scipy.spatial.distance import cosine |
| 290 | + sim = modAL.density.similarize_distance(cosine) |
| 291 | + for _ in range(100): |
| 292 | + for n_dim in range(1, 10): |
| 293 | + X_1, X_2 = np.random.rand(n_dim), np.random.rand(n_dim) |
| 294 | + np.testing.assert_almost_equal( |
| 295 | + sim(X_1, X_2), |
| 296 | + 1/(1 + cosine(X_1, X_2)) |
| 297 | + ) |
| 298 | + |
| 299 | + def test_information_density(self): |
| 300 | + for n_samples in range(1, 10): |
| 301 | + for n_dim in range(1, 10): |
| 302 | + X_pool = np.random.rand(n_samples, n_dim) |
| 303 | + similarities = modAL.density.information_density(X_pool) |
| 304 | + np.testing.assert_equal(len(similarities), n_samples) |
| 305 | + |
| 306 | + |
| 307 | +class TestDisagreements(unittest.TestCase): |
| 308 | + |
| 309 | + def test_vote_entropy(self): |
| 310 | + for n_samples in range(1, 10): |
| 311 | + for n_classes in range(1, 10): |
| 312 | + for true_query_idx in range(n_samples): |
| 313 | + # 1. fitted committee |
| 314 | + vote_return = np.zeros(shape=(n_samples, n_classes), dtype=np.int16) |
| 315 | + vote_return[true_query_idx] = np.asarray(range(n_classes), dtype=np.int16) |
| 316 | + committee = mock.MockCommittee(classes_=np.asarray(range(n_classes)), vote_return=vote_return) |
| 317 | + vote_entr = modAL.disagreement.vote_entropy( |
| 318 | + committee, np.random.rand(n_samples, n_classes) |
| 319 | + ) |
| 320 | + true_entropy = np.zeros(shape=(n_samples, )) |
| 321 | + true_entropy[true_query_idx] = entropy(np.ones(n_classes)/n_classes) |
| 322 | + np.testing.assert_array_almost_equal(vote_entr, true_entropy) |
| 323 | + |
| 324 | + # 2. unfitted committee |
| 325 | + committee = mock.MockCommittee(fitted=False) |
| 326 | + true_entropy = np.zeros(shape=(n_samples,)) |
| 327 | + vote_entr = modAL.disagreement.vote_entropy( |
| 328 | + committee, np.random.rand(n_samples, n_classes) |
| 329 | + ) |
| 330 | + np.testing.assert_almost_equal(vote_entr, true_entropy) |
| 331 | + |
| 332 | + def test_consensus_entropy(self): |
| 333 | + for n_samples in range(1, 10): |
| 334 | + for n_classes in range(2, 10): |
| 335 | + for true_query_idx in range(n_samples): |
| 336 | + # 1. fitted committee |
| 337 | + proba = np.zeros(shape=(n_samples, n_classes)) |
| 338 | + proba[:, 0] = 1.0 |
| 339 | + proba[true_query_idx] = np.ones(n_classes)/n_classes |
| 340 | + committee = mock.MockCommittee(predict_proba_return=proba) |
| 341 | + consensus_entropy = modAL.disagreement.consensus_entropy( |
| 342 | + committee, np.random.rand(n_samples, n_classes) |
| 343 | + ) |
| 344 | + true_entropy = np.zeros(shape=(n_samples,)) |
| 345 | + true_entropy[true_query_idx] = entropy(np.ones(n_classes) / n_classes) |
| 346 | + np.testing.assert_array_almost_equal(consensus_entropy, true_entropy) |
| 347 | + |
| 348 | + # 2. unfitted committee |
| 349 | + committee = mock.MockCommittee(fitted=False) |
| 350 | + true_entropy = np.zeros(shape=(n_samples,)) |
| 351 | + consensus_entropy = modAL.disagreement.consensus_entropy( |
| 352 | + committee, np.random.rand(n_samples, n_classes) |
| 353 | + ) |
| 354 | + np.testing.assert_almost_equal(consensus_entropy, true_entropy) |
| 355 | + |
| 356 | + def test_KL_max_disagreement(self): |
| 357 | + for n_samples in range(1, 10): |
| 358 | + for n_classes in range(2, 10): |
| 359 | + for n_learners in range (2, 10): |
| 360 | + # 1. fitted committee |
| 361 | + vote_proba = np.zeros(shape=(n_samples, n_learners, n_classes)) |
| 362 | + vote_proba[:, :, 0] = 1.0 |
| 363 | + committee = mock.MockCommittee( |
| 364 | + n_learners=n_learners, classes_=range(n_classes), |
| 365 | + vote_proba_return=vote_proba |
| 366 | + ) |
| 367 | + |
| 368 | + true_KL_disagreement = np.zeros(shape=(n_samples, )) |
| 369 | + |
| 370 | + try: |
| 371 | + np.testing.assert_array_almost_equal( |
| 372 | + true_KL_disagreement, |
| 373 | + modAL.disagreement.KL_max_disagreement(committee, np.random.rand(n_samples, 1)) |
| 374 | + ) |
| 375 | + except: |
| 376 | + modAL.disagreement.KL_max_disagreement(committee, np.random.rand(n_samples, 1)) |
| 377 | + |
| 378 | + # 2. unfitted committee |
| 379 | + committee = mock.MockCommittee(fitted=False) |
| 380 | + true_KL_disagreement = np.zeros(shape=(n_samples,)) |
| 381 | + returned_KL_disagreement = modAL.disagreement.KL_max_disagreement( |
| 382 | + committee, np.random.rand(n_samples, n_classes) |
| 383 | + ) |
| 384 | + np.testing.assert_almost_equal(returned_KL_disagreement, true_KL_disagreement) |
| 385 | + |
| 386 | + |
| 387 | +class TestEER(unittest.TestCase): |
| 388 | + def test_eer(self): |
| 389 | + for n_pool, n_features, n_classes in product(range(1, 10), range(1, 5), range(2, 5)): |
| 390 | + X_training, y_training = np.random.rand(10, n_features), np.random.randint(0, n_classes, size=10) |
| 391 | + X_pool, y_pool = np.random.rand(n_pool, n_features), np.random.randint(0, n_classes+1, size=n_pool) |
| 392 | + |
| 393 | + learner = modAL.models.ActiveLearner(RandomForestClassifier(n_estimators=2), |
| 394 | + X_training=X_training, y_training=y_training) |
| 395 | + |
| 396 | + modAL.expected_error.expected_error_reduction(learner, X_pool) |
| 397 | + |
| 398 | + |
283 | 399 | class TestUncertainties(unittest.TestCase): |
284 | 400 |
|
285 | 401 | def test_classifier_uncertainty(self): |
@@ -383,107 +499,6 @@ def test_entropy_sampling(self): |
383 | 499 | np.testing.assert_array_equal(query_idx, true_query_idx) |
384 | 500 |
|
385 | 501 |
|
386 | | -class TestDensity(unittest.TestCase): |
387 | | - |
388 | | - def test_similarize_distance(self): |
389 | | - from scipy.spatial.distance import cosine |
390 | | - sim = modAL.density.similarize_distance(cosine) |
391 | | - for _ in range(100): |
392 | | - for n_dim in range(1, 10): |
393 | | - X_1, X_2 = np.random.rand(n_dim), np.random.rand(n_dim) |
394 | | - np.testing.assert_almost_equal( |
395 | | - sim(X_1, X_2), |
396 | | - 1/(1 + cosine(X_1, X_2)) |
397 | | - ) |
398 | | - |
399 | | - def test_information_density(self): |
400 | | - for n_samples in range(1, 10): |
401 | | - for n_dim in range(1, 10): |
402 | | - X_pool = np.random.rand(n_samples, n_dim) |
403 | | - similarities = modAL.density.information_density(X_pool) |
404 | | - np.testing.assert_equal(len(similarities), n_samples) |
405 | | - |
406 | | - |
407 | | -class TestDisagreements(unittest.TestCase): |
408 | | - |
409 | | - def test_vote_entropy(self): |
410 | | - for n_samples in range(1, 10): |
411 | | - for n_classes in range(1, 10): |
412 | | - for true_query_idx in range(n_samples): |
413 | | - # 1. fitted committee |
414 | | - vote_return = np.zeros(shape=(n_samples, n_classes), dtype=np.int16) |
415 | | - vote_return[true_query_idx] = np.asarray(range(n_classes), dtype=np.int16) |
416 | | - committee = mock.MockCommittee(classes_=np.asarray(range(n_classes)), vote_return=vote_return) |
417 | | - vote_entr = modAL.disagreement.vote_entropy( |
418 | | - committee, np.random.rand(n_samples, n_classes) |
419 | | - ) |
420 | | - true_entropy = np.zeros(shape=(n_samples, )) |
421 | | - true_entropy[true_query_idx] = entropy(np.ones(n_classes)/n_classes) |
422 | | - np.testing.assert_array_almost_equal(vote_entr, true_entropy) |
423 | | - |
424 | | - # 2. unfitted committee |
425 | | - committee = mock.MockCommittee(fitted=False) |
426 | | - true_entropy = np.zeros(shape=(n_samples,)) |
427 | | - vote_entr = modAL.disagreement.vote_entropy( |
428 | | - committee, np.random.rand(n_samples, n_classes) |
429 | | - ) |
430 | | - np.testing.assert_almost_equal(vote_entr, true_entropy) |
431 | | - |
432 | | - def test_consensus_entropy(self): |
433 | | - for n_samples in range(1, 10): |
434 | | - for n_classes in range(2, 10): |
435 | | - for true_query_idx in range(n_samples): |
436 | | - # 1. fitted committee |
437 | | - proba = np.zeros(shape=(n_samples, n_classes)) |
438 | | - proba[:, 0] = 1.0 |
439 | | - proba[true_query_idx] = np.ones(n_classes)/n_classes |
440 | | - committee = mock.MockCommittee(predict_proba_return=proba) |
441 | | - consensus_entropy = modAL.disagreement.consensus_entropy( |
442 | | - committee, np.random.rand(n_samples, n_classes) |
443 | | - ) |
444 | | - true_entropy = np.zeros(shape=(n_samples,)) |
445 | | - true_entropy[true_query_idx] = entropy(np.ones(n_classes) / n_classes) |
446 | | - np.testing.assert_array_almost_equal(consensus_entropy, true_entropy) |
447 | | - |
448 | | - # 2. unfitted committee |
449 | | - committee = mock.MockCommittee(fitted=False) |
450 | | - true_entropy = np.zeros(shape=(n_samples,)) |
451 | | - consensus_entropy = modAL.disagreement.consensus_entropy( |
452 | | - committee, np.random.rand(n_samples, n_classes) |
453 | | - ) |
454 | | - np.testing.assert_almost_equal(consensus_entropy, true_entropy) |
455 | | - |
456 | | - def test_KL_max_disagreement(self): |
457 | | - for n_samples in range(1, 10): |
458 | | - for n_classes in range(2, 10): |
459 | | - for n_learners in range (2, 10): |
460 | | - # 1. fitted committee |
461 | | - vote_proba = np.zeros(shape=(n_samples, n_learners, n_classes)) |
462 | | - vote_proba[:, :, 0] = 1.0 |
463 | | - committee = mock.MockCommittee( |
464 | | - n_learners=n_learners, classes_=range(n_classes), |
465 | | - vote_proba_return=vote_proba |
466 | | - ) |
467 | | - |
468 | | - true_KL_disagreement = np.zeros(shape=(n_samples, )) |
469 | | - |
470 | | - try: |
471 | | - np.testing.assert_array_almost_equal( |
472 | | - true_KL_disagreement, |
473 | | - modAL.disagreement.KL_max_disagreement(committee, np.random.rand(n_samples, 1)) |
474 | | - ) |
475 | | - except: |
476 | | - modAL.disagreement.KL_max_disagreement(committee, np.random.rand(n_samples, 1)) |
477 | | - |
478 | | - # 2. unfitted committee |
479 | | - committee = mock.MockCommittee(fitted=False) |
480 | | - true_KL_disagreement = np.zeros(shape=(n_samples,)) |
481 | | - returned_KL_disagreement = modAL.disagreement.KL_max_disagreement( |
482 | | - committee, np.random.rand(n_samples, n_classes) |
483 | | - ) |
484 | | - np.testing.assert_almost_equal(returned_KL_disagreement, true_KL_disagreement) |
485 | | - |
486 | | - |
487 | 502 | class TestQueries(unittest.TestCase): |
488 | 503 |
|
489 | 504 | def test_multi_argmax(self): |
@@ -963,7 +978,6 @@ def test_strategies(self): |
963 | 978 | modAL.multilabel.avg_score(classifier, X_pool, n_query_instances) |
964 | 979 |
|
965 | 980 |
|
966 | | - |
967 | 981 | class TestExamples(unittest.TestCase): |
968 | 982 |
|
969 | 983 | def test_examples(self): |
|
0 commit comments