|
70 | 70 | "metadata": {}, |
71 | 71 | "source": [ |
72 | 72 | "### Documentation Example\n", |
73 | | - "We will write documentation for a function to calcuate the snowmelt using the snow-degree method. This method mainly requires inputs of air temperature ($T_a$), degree-day threshold temperature ($T_0$, usually set as 0), and degree-day factor ($c_0$). The snowmelt ($SM$) with a given period of time ($ t$) is calculated with the following function:\n", |
| 73 | + "We will write documentation for a function to calculate the snowmelt using the snow-degree method. This method mainly requires inputs of air temperature ($T_a$), degree-day threshold temperature ($T_0$, usually set as 0), and degree-day factor ($c_0$). The snowmelt ($SM$) with a given period of time ($ t$) is calculated with the following function:\n", |
74 | 74 | "\n", |
75 | 75 | "$$ SM = (T_a - T_0) * c_0 * t $$" |
76 | 76 | ] |
|
441 | 441 | "metadata": {}, |
442 | 442 | "outputs": [], |
443 | 443 | "source": [ |
444 | | - "\"\"\"\n", |
445 | | - "Example for diffusion model class\n", |
446 | | - "\"\"\"\n", |
447 | | - "\n", |
448 | 444 | "import numpy as np\n", |
449 | | - "import matplotlib\n", |
| 445 | + "import matplotlib.pyplot as plt\n", |
450 | 446 | "\n", |
451 | 447 | "\n", |
452 | 448 | "class Diffusion2D:\n", |
|
464 | 460 | " Examples\n", |
465 | 461 | " --------\n", |
466 | 462 | " >>> import numpy as np\n", |
467 | | - " >>> import matplotlib\n", |
468 | | - " >>> field = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]])\n", |
| 463 | + " >>> field = np.array([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0]])\n", |
469 | 464 | " >>> spacing = (1.0, 1.0)\n", |
470 | 465 | " >>> diffusivity = 0.1\n", |
471 | 466 | " >>> model = Diffusion2D(field, spacing, diffusivity)\n", |
472 | 467 | " >>> model.calc_stable_time_step()\n", |
473 | | - " 0.1\n", |
| 468 | + " 1.0\n", |
474 | 469 | " >>> model.run_one_step(0.1)\n", |
475 | 470 | " \"\"\"\n", |
476 | 471 | "\n", |
477 | 472 | " def __init__(self, field: np.ndarray, spacing: tuple, diffusivity: float):\n", |
478 | 473 | " \"\"\"Initialize the model with a field of values and diffusivity parameter.\n", |
| 474 | + "\n", |
479 | 475 | " Parameters\n", |
480 | 476 | " ----------\n", |
481 | 477 | " field : np.ndarray\n", |
|
542 | 538 | " \n", |
543 | 539 | "```python\n", |
544 | 540 | "import numpy as np\n", |
545 | | - "import matplotlib\n", |
| 541 | + "import matplotlib.pyplot as plt\n", |
| 542 | + "\n", |
546 | 543 | "\n", |
547 | 544 | "class Diffusion2D:\n", |
548 | 545 | " \"\"\"Solves the 2D diffusion problem on a regular grid.\n", |
|
559 | 556 | " Examples\n", |
560 | 557 | " --------\n", |
561 | 558 | " >>> import numpy as np\n", |
562 | | - " >>> field = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]])\n", |
| 559 | + " >>> field = np.array([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0]])\n", |
563 | 560 | " >>> spacing = (1.0, 1.0)\n", |
564 | 561 | " >>> diffusivity = 0.1\n", |
565 | 562 | " >>> model = Diffusion2D(field, spacing, diffusivity)\n", |
566 | 563 | " >>> model.calc_stable_time_step()\n", |
567 | | - " 0.1\n", |
| 564 | + " 1.0\n", |
568 | 565 | " >>> model.run_one_step(0.1)\n", |
569 | 566 | " \"\"\"\n", |
570 | 567 | "\n", |
|
657 | 654 | "metadata": {}, |
658 | 655 | "outputs": [], |
659 | 656 | "source": [ |
660 | | - "\"\"\"\n", |
661 | | - "Unit tests for Diffusion2D class.\n", |
662 | | - "\"\"\"\n", |
663 | | - "\n", |
664 | 657 | "import numpy as np\n", |
665 | 658 | "import pytest\n", |
666 | 659 | "\n", |
|
680 | 673 | "\n", |
681 | 674 | "\n", |
682 | 675 | "def test_initialization(default_model):\n", |
683 | | - " pass # remove pass and add you code\n", |
| 676 | + " pass # remove pass and add your code\n", |
684 | 677 | "\n", |
685 | 678 | "\n", |
686 | 679 | "def test_calc_stable_time_step(default_model):\n", |
687 | | - " pass # remove pass and add you code\n", |
| 680 | + " pass # remove pass and add your code\n", |
688 | 681 | "\n", |
689 | 682 | "\n", |
690 | 683 | "def test_run_one_step(default_model):\n", |
691 | | - " pass # remove pass and add you code\n", |
| 684 | + " pass # remove pass and add your code\n", |
692 | 685 | "\n", |
693 | 686 | "\n", |
694 | 687 | "# feel free to add additional test functions" |
|
707 | 700 | "import pytest\n", |
708 | 701 | "from diffusion_model import Diffusion2D\n", |
709 | 702 | "\n", |
| 703 | + "\n", |
710 | 704 | "@pytest.fixture\n", |
711 | 705 | "def default_model():\n", |
712 | 706 | " \"\"\"Fixture to create a default Diffusion2D model.\"\"\"\n", |
713 | | - " field = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]])\n", |
| 707 | + " field = np.array([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0]])\n", |
714 | 708 | " spacing = (1.0, 1.0)\n", |
715 | 709 | " diffusivity = 0.1\n", |
716 | 710 | " return Diffusion2D(field.copy(), spacing, diffusivity), field.copy(), spacing, diffusivity\n", |
717 | 711 | "\n", |
718 | 712 | "def test_initialization(default_model):\n", |
719 | 713 | " \"\"\"Test that Diffusion2D initializes with correct parameters.\"\"\"\n", |
720 | 714 | " model, field, spacing, diffusivity = default_model\n", |
721 | | - " assert isinstance(model, Diffusion2D)\n", |
722 | 715 | " assert np.array_equal(model.field, field)\n", |
723 | 716 | " assert model.spacing == spacing\n", |
724 | 717 | " assert model.k == diffusivity\n", |
|
740 | 733 | "\n", |
741 | 734 | "def test_run_one_step_no_diffusion():\n", |
742 | 735 | " \"\"\"Test that a zero-initialized field remains unchanged after one step.\"\"\"\n", |
743 | | - " field = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])\n", |
| 736 | + " field = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]])\n", |
744 | 737 | " model = Diffusion2D(field.copy(), (1.0, 1.0), 0.1)\n", |
745 | 738 | " model.run_one_step(0.1)\n", |
746 | 739 | " assert np.array_equal(model.field, field)\n", |
747 | 740 | " assert model.time_elapsed == 0.1\n", |
748 | | - "\n", |
749 | | - "def test_plot_field(default_model):\n", |
750 | | - " \"\"\"Test that plot_field runs without errors.\"\"\"\n", |
751 | | - " model, _, _, _ = default_model\n", |
752 | | - " model.plot_field() # Just checking for error-free execution\n", |
753 | 741 | " \n", |
754 | 742 | "</details>" |
755 | 743 | ] |
|
762 | 750 | "**3) Bonus Exercise: Run Unit Tests**\n", |
763 | 751 | "\n", |
764 | 752 | "Follow the steps shown in the \"Run Unit Tests\" section to run the unit tests for the Diffution2D class on your computer. \n", |
765 | | - "Please remember that in step 3, add one line of python code to import the Diffusion2D class in the unit test file." |
| 753 | + "\n", |
| 754 | + "For step 3, remember to add one line of python code to import the Diffusion2D class in the unit test file.\n", |
| 755 | + "\n", |
| 756 | + "For step 4, you will need to install additional Python packages used by the Diffusion2D class (e.g. numpy, matplotlib) within the conda environment you created for unit testing." |
766 | 757 | ] |
767 | 758 | } |
768 | 759 | ], |
|
0 commit comments