From 12f1af1d6b2e7fa8f0f04af4d2ce99cd8220f3f0 Mon Sep 17 00:00:00 2001 From: Andrew Moodie Date: Tue, 25 Jun 2024 14:52:27 -0500 Subject: [PATCH] add basic tests, and test of preprocessing. Testing the other options/functionality will be more difficult. --- deltametrics/plan.py | 9 ++++++--- tests/test_plan.py | 28 +++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/deltametrics/plan.py b/deltametrics/plan.py index 8fcdf221..28af80ab 100644 --- a/deltametrics/plan.py +++ b/deltametrics/plan.py @@ -1700,7 +1700,7 @@ def shaw_opening_angle_method( Binary image that has been thresholded to split water/land. At minimum, this should be a thresholded elevation matrix, or some classification of land/water based on pixel color or reflectance - intensity. This is the startin point (i.e., guess) for the opening + intensity. This is the starting point for the opening angle method. numviews : int, optional @@ -1748,12 +1748,15 @@ def shaw_opening_angle_method( # set refers to the points which bound the angle calculations. ## Preprocess - # Preprocess in orginal paper: "we pre-process by filling lakes - # (contiguous sets of water pixels surrounded by land)" if preprocess: + # Preprocess in orginal paper: "we pre-process by filling lakes + # (contiguous sets of water pixels surrounded by land)" below_mask = np.logical_not( binary_fill_holes(np.logical_not(below_mask)) ).astype(int) + else: + # Ensure array is integer binary + below_mask = below_mask.astype(int) ## Find land-water interface (`edges`) # find the edges of the below_mask by a gradient approach in x and y diff --git a/tests/test_plan.py b/tests/test_plan.py index 3c6164a7..e7f689fc 100644 --- a/tests/test_plan.py +++ b/tests/test_plan.py @@ -329,12 +329,30 @@ def test_bad_type(self): class TestShawOpeningAngleMethod: - simple_ocean = 1 - simple_land - - # NEED TESTS + simple_ocean = 1 - simple_land # ocean is at bottom of image - def test_null(self): - pass + def test_allblack(self): + with pytest.raises(ValueError, match=r"No pixels identified in below_mask.*"): + _ = plan.shaw_opening_angle_method(np.zeros((10, 10), dtype=int)) + + def test_simple_case_defaults(self): + oam = plan.shaw_opening_angle_method(self.simple_ocean) + assert np.all(oam <= 180) + assert np.all(oam >= 0) + assert np.all(oam[-1, :] == 180) + + def test_simple_case_preprocess(self): + # make a custom mask with a lake + _custom_ocean = np.copy(self.simple_ocean) + _custom_ocean[1:3, 1:3] = 1 # add a lake + + # the lake should be removed (default) + oam1 = plan.shaw_opening_angle_method(_custom_ocean, preprocess=True) + assert np.all(oam1[1:3, 1:3] == 0) + + # the lake should persist + oam2 = plan.shaw_opening_angle_method(_custom_ocean, preprocess=False) + assert np.all(oam2[1:3, 1:3] != 0) class TestDeltaArea: