|
12 | 12 | },
|
13 | 13 | {
|
14 | 14 | "cell_type": "code",
|
15 |
| - "execution_count": null, |
| 15 | + "execution_count": 1, |
16 | 16 | "metadata": {},
|
17 | 17 | "outputs": [],
|
18 | 18 | "source": [
|
|
78 | 78 | },
|
79 | 79 | {
|
80 | 80 | "cell_type": "code",
|
81 |
| - "execution_count": null, |
| 81 | + "execution_count": 2, |
82 | 82 | "metadata": {},
|
83 | 83 | "outputs": [],
|
84 | 84 | "source": [
|
|
273 | 273 | },
|
274 | 274 | {
|
275 | 275 | "cell_type": "code",
|
276 |
| - "execution_count": null, |
| 276 | + "execution_count": 11, |
277 | 277 | "metadata": {},
|
278 | 278 | "outputs": [],
|
279 | 279 | "source": [
|
280 |
| - "def test_entropy_parameter_checking():\n", |
| 280 | + "def entropy_parameter_checking():\n", |
281 | 281 | " # first, let's try not summing to 1\n",
|
282 | 282 | " try:\n",
|
283 | 283 | " entropy([.9, .9])\n",
|
|
295 | 295 | "metadata": {},
|
296 | 296 | "outputs": [],
|
297 | 297 | "source": [
|
298 |
| - "test_entropy_parameter_checking()" |
| 298 | + "entropy_parameter_checking()" |
299 | 299 | ]
|
300 | 300 | },
|
301 | 301 | {
|
|
319 | 319 | "outputs": [],
|
320 | 320 | "source": [
|
321 | 321 | "# Pattern test\n",
|
322 |
| - "def test_equal_probabilities(n):\n", |
| 322 | + "def check_equal_probabilities(n):\n", |
323 | 323 | " prob = 1.0/n\n",
|
324 | 324 | " ps = np.repeat(prob , n)\n",
|
325 | 325 | " if np.isclose(entropy(ps), -np.log2(prob)):\n",
|
|
330 | 330 | " \n",
|
331 | 331 | " \n",
|
332 | 332 | "# Run a test\n",
|
333 |
| - "test_equal_probabilities(100000)" |
| 333 | + "check_equal_probabilities(100000)" |
334 | 334 | ]
|
335 | 335 | },
|
336 | 336 | {
|
|
354 | 354 | },
|
355 | 355 | {
|
356 | 356 | "cell_type": "code",
|
357 |
| - "execution_count": null, |
| 357 | + "execution_count": 14, |
358 | 358 | "metadata": {},
|
359 | 359 | "outputs": [],
|
360 | 360 | "source": [
|
361 |
| - "def makeProbabilityMatrix(column_names, nrows):\n", |
| 361 | + "def make_probability_matrix(column_names, nrows):\n", |
362 | 362 | " \"\"\"\n",
|
363 | 363 | " Makes a dataframe with the specified column names such that each\n",
|
364 | 364 | " cell is a value in [0, 1] and columns sum to 1.\n",
|
|
386 | 386 | "metadata": {},
|
387 | 387 | "outputs": [],
|
388 | 388 | "source": [
|
389 |
| - "makeProbabilityMatrix(['a', 'b'], 3)" |
| 389 | + "make_probability_matrix(['a', 'b'], 3)" |
390 | 390 | ]
|
391 | 391 | },
|
392 | 392 | {
|
|
397 | 397 | "source": [
|
398 | 398 | "# Test 2: Check columns\n",
|
399 | 399 | "COLUMNS = ['a', 'b']\n",
|
400 |
| - "df = makeProbabilityMatrix(COLUMNS, 3)\n", |
| 400 | + "df = make_probability_matrix(COLUMNS, 3)\n", |
401 | 401 | "set(COLUMNS) == set(df.columns)"
|
402 | 402 | ]
|
403 | 403 | },
|
|
429 | 429 | "- The infrastructure provides a uniform way to report test results, and to handle test failures.\n",
|
430 | 430 | "- A test infrastructure can tell you about coverage so you know what tests to add.\n",
|
431 | 431 | "\n",
|
432 |
| - "We'll be using the `Pytest` framework. This is a separate Python package. Using this infrastructure, requires the following:\n", |
433 |
| - "1. import the pytest module\n", |
| 432 | + "We'll be using the `pytest` framework. This is a separate Python package. Using this infrastructure, requires the following:\n", |
434 | 433 | "1. write methods that run the code to be tested and check the outcomes.\n",
|
435 |
| - "\n", |
436 |
| - "You indicate that a method is to be run as a test by having the method name begin with \"test\".\n", |
437 |
| - "\n", |
438 |
| - "Second, the \"test methods\" should communicate with the infrastructure the results of evaluating output from the code under test. This is done by using `assert` statements. For example, `np.testing.assert_equal` takes two arguments. If these are objects for which `==` returns `True`, then the test passes. Otherwise, the test fails." |
| 434 | + "1. Indicate that a method is to be run as a test by having the method name begin with \"test\".\n", |
| 435 | + "1. The \"test methods\" should communicate with the infrastructure the results of evaluating output from the code under test. This is done by using `assert` statements. For example, `np.testing.assert_equal` takes two arguments. If these are objects for which `==` returns `True`, then the test passes. Otherwise, the test fails." |
439 | 436 | ]
|
440 | 437 | },
|
441 | 438 | {
|
|
455 | 452 | },
|
456 | 453 | {
|
457 | 454 | "cell_type": "code",
|
458 |
| - "execution_count": null, |
| 455 | + "execution_count": 17, |
459 | 456 | "metadata": {},
|
460 | 457 | "outputs": [],
|
461 | 458 | "source": [
|
462 |
| - "import pytest\n", |
463 | 459 | "import ipytest\n",
|
464 | 460 | "import numpy as np\n",
|
465 | 461 | "ipytest.autoconfig()"
|
466 | 462 | ]
|
467 | 463 | },
|
468 | 464 | {
|
469 | 465 | "cell_type": "code",
|
470 |
| - "execution_count": null, |
| 466 | + "execution_count": 18, |
471 | 467 | "metadata": {
|
472 | 468 | "scrolled": true
|
473 | 469 | },
|
474 | 470 | "outputs": [],
|
475 | 471 | "source": [
|
476 |
| - "%%ipytest\n", |
477 |
| - "\n", |
478 | 472 | "# Define test functions\n",
|
479 | 473 | "\n",
|
480 | 474 | "def test_success():\n",
|
|
484 | 478 | " assert 1 == 1\n",
|
485 | 479 | "\n",
|
486 | 480 | "def test_failure():\n",
|
487 |
| - " assert 1 ==2\n" |
| 481 | + " assert 1 == 2\n" |
| 482 | + ] |
| 483 | + }, |
| 484 | + { |
| 485 | + "cell_type": "code", |
| 486 | + "execution_count": null, |
| 487 | + "metadata": {}, |
| 488 | + "outputs": [], |
| 489 | + "source": [ |
| 490 | + "ipytest.run()" |
488 | 491 | ]
|
489 | 492 | },
|
490 | 493 | {
|
|
525 | 528 | "outputs": [],
|
526 | 529 | "source": [
|
527 | 530 | "# Implementating a pattern test. Use functions in the test.\n",
|
528 |
| - "import pytest\n", |
529 |
| - " \n", |
530 | 531 | "def test_equal_probability():\n",
|
531 | 532 | " def test(count):\n",
|
532 | 533 | " \"\"\"\n",
|
|
549 | 550 | "metadata": {},
|
550 | 551 | "outputs": [],
|
551 | 552 | "source": [
|
552 |
| - "import pytest\n", |
553 |
| - "\n", |
554 | 553 | "# Define test functions for the entropy function"
|
555 | 554 | ]
|
556 | 555 | },
|
|
565 | 564 | },
|
566 | 565 | {
|
567 | 566 | "cell_type": "code",
|
568 |
| - "execution_count": null, |
| 567 | + "execution_count": 21, |
569 | 568 | "metadata": {},
|
570 | 569 | "outputs": [],
|
571 | 570 | "source": [
|
572 |
| - "%%ipytest\n", |
573 |
| - "\n", |
574 |
| - "import pytest\n", |
575 |
| - "\n", |
576 | 571 | "# Define tests to run\n",
|
577 | 572 | " \n",
|
578 | 573 | "def test_invalid_probability():\n",
|
|
583 | 578 | " assert True"
|
584 | 579 | ]
|
585 | 580 | },
|
| 581 | + { |
| 582 | + "cell_type": "code", |
| 583 | + "execution_count": null, |
| 584 | + "metadata": {}, |
| 585 | + "outputs": [], |
| 586 | + "source": [ |
| 587 | + "ipytest.run()" |
| 588 | + ] |
| 589 | + }, |
586 | 590 | {
|
587 | 591 | "cell_type": "markdown",
|
588 | 592 | "metadata": {},
|
|
592 | 596 | },
|
593 | 597 | {
|
594 | 598 | "cell_type": "code",
|
595 |
| - "execution_count": null, |
| 599 | + "execution_count": 24, |
596 | 600 | "metadata": {},
|
597 | 601 | "outputs": [],
|
598 | 602 | "source": [
|
599 |
| - "%%ipytest\n", |
600 |
| - "\n", |
601 | 603 | "import pytest\n",
|
602 | 604 | "\n",
|
603 | 605 | "# Define tests\n",
|
604 | 606 | "\n",
|
605 | 607 | "def test_invalid_probability():\n",
|
606 |
| - " with pytest.raises(ValueError):\n", |
| 608 | + " with pytest.raises(ValueError, match=\"The list of input probabilities does not sum to 1\"):\n", |
607 | 609 | " entropy([0.1, 0.5])\n",
|
608 | 610 | " \n"
|
609 | 611 | ]
|
610 | 612 | },
|
| 613 | + { |
| 614 | + "cell_type": "code", |
| 615 | + "execution_count": null, |
| 616 | + "metadata": {}, |
| 617 | + "outputs": [], |
| 618 | + "source": [ |
| 619 | + "ipytest.run()" |
| 620 | + ] |
| 621 | + }, |
611 | 622 | {
|
612 | 623 | "cell_type": "markdown",
|
613 | 624 | "metadata": {},
|
|
638 | 649 | },
|
639 | 650 | {
|
640 | 651 | "cell_type": "code",
|
641 |
| - "execution_count": null, |
| 652 | + "execution_count": 26, |
642 | 653 | "metadata": {},
|
643 | 654 | "outputs": [],
|
644 | 655 | "source": [
|
645 |
| - "%%ipytest\n", |
646 | 656 | "import pytest\n",
|
647 | 657 | "\n",
|
648 | 658 | "# Define tests\n",
|
|
660 | 670 | "execution_count": null,
|
661 | 671 | "metadata": {},
|
662 | 672 | "outputs": [],
|
| 673 | + "source": [ |
| 674 | + "ipytest.run()" |
| 675 | + ] |
| 676 | + }, |
| 677 | + { |
| 678 | + "cell_type": "code", |
| 679 | + "execution_count": 9, |
| 680 | + "metadata": {}, |
| 681 | + "outputs": [], |
663 | 682 | "source": [
|
664 | 683 | "#def geomean(argument?):\n",
|
665 | 684 | "# return ?"
|
666 | 685 | ]
|
667 | 686 | },
|
| 687 | + { |
| 688 | + "cell_type": "code", |
| 689 | + "execution_count": null, |
| 690 | + "metadata": {}, |
| 691 | + "outputs": [], |
| 692 | + "source": [ |
| 693 | + "ipytest.run()" |
| 694 | + ] |
| 695 | + }, |
668 | 696 | {
|
669 | 697 | "cell_type": "markdown",
|
670 | 698 | "metadata": {},
|
|
711 | 739 | ],
|
712 | 740 | "metadata": {
|
713 | 741 | "kernelspec": {
|
714 |
| - "display_name": "Python 3 (ipykernel)", |
| 742 | + "display_name": "cse583", |
715 | 743 | "language": "python",
|
716 | 744 | "name": "python3"
|
717 | 745 | },
|
|
725 | 753 | "name": "python",
|
726 | 754 | "nbconvert_exporter": "python",
|
727 | 755 | "pygments_lexer": "ipython3",
|
728 |
| - "version": "3.11.5" |
| 756 | + "version": "3.13.0" |
729 | 757 | }
|
730 | 758 | },
|
731 | 759 | "nbformat": 4,
|
|
0 commit comments