Skip to content

Commit 279fff1

Browse files
committed
update example hands-on code and text for documentation and unit testing tutorial
1 parent 9c4d3ca commit 279fff1

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

lessons/python/docs_and_testing.ipynb

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"metadata": {},
7171
"source": [
7272
"### 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",
7474
"\n",
7575
"$$ SM = (T_a - T_0) * c_0 * t $$"
7676
]
@@ -441,12 +441,8 @@
441441
"metadata": {},
442442
"outputs": [],
443443
"source": [
444-
"\"\"\"\n",
445-
"Example for diffusion model class\n",
446-
"\"\"\"\n",
447-
"\n",
448444
"import numpy as np\n",
449-
"import matplotlib\n",
445+
"import matplotlib.pyplot as plt\n",
450446
"\n",
451447
"\n",
452448
"class Diffusion2D:\n",
@@ -464,18 +460,18 @@
464460
" Examples\n",
465461
" --------\n",
466462
" >>> 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",
469464
" >>> spacing = (1.0, 1.0)\n",
470465
" >>> diffusivity = 0.1\n",
471466
" >>> model = Diffusion2D(field, spacing, diffusivity)\n",
472467
" >>> model.calc_stable_time_step()\n",
473-
" 0.1\n",
468+
" 1.0\n",
474469
" >>> model.run_one_step(0.1)\n",
475470
" \"\"\"\n",
476471
"\n",
477472
" def __init__(self, field: np.ndarray, spacing: tuple, diffusivity: float):\n",
478473
" \"\"\"Initialize the model with a field of values and diffusivity parameter.\n",
474+
"\n",
479475
" Parameters\n",
480476
" ----------\n",
481477
" field : np.ndarray\n",
@@ -542,7 +538,8 @@
542538
" \n",
543539
"```python\n",
544540
"import numpy as np\n",
545-
"import matplotlib\n",
541+
"import matplotlib.pyplot as plt\n",
542+
"\n",
546543
"\n",
547544
"class Diffusion2D:\n",
548545
" \"\"\"Solves the 2D diffusion problem on a regular grid.\n",
@@ -559,12 +556,12 @@
559556
" Examples\n",
560557
" --------\n",
561558
" >>> 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",
563560
" >>> spacing = (1.0, 1.0)\n",
564561
" >>> diffusivity = 0.1\n",
565562
" >>> model = Diffusion2D(field, spacing, diffusivity)\n",
566563
" >>> model.calc_stable_time_step()\n",
567-
" 0.1\n",
564+
" 1.0\n",
568565
" >>> model.run_one_step(0.1)\n",
569566
" \"\"\"\n",
570567
"\n",
@@ -657,10 +654,6 @@
657654
"metadata": {},
658655
"outputs": [],
659656
"source": [
660-
"\"\"\"\n",
661-
"Unit tests for Diffusion2D class.\n",
662-
"\"\"\"\n",
663-
"\n",
664657
"import numpy as np\n",
665658
"import pytest\n",
666659
"\n",
@@ -680,15 +673,15 @@
680673
"\n",
681674
"\n",
682675
"def test_initialization(default_model):\n",
683-
" pass # remove pass and add you code\n",
676+
" pass # remove pass and add your code\n",
684677
"\n",
685678
"\n",
686679
"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",
688681
"\n",
689682
"\n",
690683
"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",
692685
"\n",
693686
"\n",
694687
"# feel free to add additional test functions"
@@ -707,18 +700,18 @@
707700
"import pytest\n",
708701
"from diffusion_model import Diffusion2D\n",
709702
"\n",
703+
"\n",
710704
"@pytest.fixture\n",
711705
"def default_model():\n",
712706
" \"\"\"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",
714708
" spacing = (1.0, 1.0)\n",
715709
" diffusivity = 0.1\n",
716710
" return Diffusion2D(field.copy(), spacing, diffusivity), field.copy(), spacing, diffusivity\n",
717711
"\n",
718712
"def test_initialization(default_model):\n",
719713
" \"\"\"Test that Diffusion2D initializes with correct parameters.\"\"\"\n",
720714
" model, field, spacing, diffusivity = default_model\n",
721-
" assert isinstance(model, Diffusion2D)\n",
722715
" assert np.array_equal(model.field, field)\n",
723716
" assert model.spacing == spacing\n",
724717
" assert model.k == diffusivity\n",
@@ -740,16 +733,11 @@
740733
"\n",
741734
"def test_run_one_step_no_diffusion():\n",
742735
" \"\"\"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",
744737
" model = Diffusion2D(field.copy(), (1.0, 1.0), 0.1)\n",
745738
" model.run_one_step(0.1)\n",
746739
" assert np.array_equal(model.field, field)\n",
747740
" 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",
753741
" \n",
754742
"</details>"
755743
]
@@ -762,7 +750,10 @@
762750
"**3) Bonus Exercise: Run Unit Tests**\n",
763751
"\n",
764752
"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."
766757
]
767758
}
768759
],

0 commit comments

Comments
 (0)