Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r.basins.fill: Fix Broken Tests and Preserve Test Dataset Integrity #5198

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions raster/r.basins.fill/testsuite/test_r_basins_fill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""
Name: r.basins.fill
Purpose: Tests r.basins.fill and its flags/options.

Author: Sunveer Singh
Copyright: (C) 2017 by Sunveer Singh and the GRASS Development Team
Licence: This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
"""

from grass.gunittest.case import TestCase
import grass.script as gs


def get_categories(map_name):
"""
Retrieve unique integer category values for a given raster map using r.stats.
"""
stats = gs.read_command("r.stats", flags="nc", input=map_name, quiet=True).strip()
return {int(line.split()[0]) for line in stats.splitlines() if line}


class TestRasterbasin(TestCase):
"""Test case for r.basins.fill module"""

# Setup variables to be used for outputs
streams = "test_streams"
geomorphons = "test_geomorphons"
ridges = "test_ridges"
subbasins = "test_subbasins"

@classmethod
def setUpClass(cls):
cls.use_temp_region()
# Set the computational region based on the "elevation" map.
cls.runModule("g.region", n=220000, s=218000, e=640000, w=632000, res=10)
# Run r.watershed to generate the coded stream network.
cls.runModule(
"r.watershed",
elevation="elevation",
threshold=1000,
stream=cls.streams,
)
# Ensure null values are replaced with 0 in streams
cls.runModule("r.null", map=cls.streams, null=0)

# Run r.geomorphon to generate geomorphometric forms.
cls.runModule(
"r.geomorphon",
elevation="elevation",
forms=cls.geomorphons,
search=36,
skip=6,
quiet=True,
)
# Extract ridges from the geomorphon output.
cls.runModule(
"r.mapcalc", expression=f"{cls.ridges} = if({cls.geomorphons}==3,1,0)"
)

@classmethod
def tearDownClass(cls):
"""Remove temporary maps and region created during the test."""
cls.runModule(
"g.remove",
flags="f",
type="raster",
name=[cls.streams, cls.ridges, cls.subbasins, cls.geomorphons],
)
cls.del_temp_region()

def test_r_basins_fill_method(self):
"""Test r.basins.fill to verify that it generates the correct watershed subbasins raster map."""

self.assertModule(
"r.basins.fill",
cnetwork=self.streams,
tnetwork=self.ridges,
output=self.subbasins,
number=2,
)

self.assertRasterExists(self.subbasins)
self.assertRasterMinMax(
map=self.subbasins,
refmin=0,
refmax=2482,
msg="subbasins in degrees must be between 0 and 2482",
)

# Retrieve unique category values from the streams and subbasins maps.
streams_cats = get_categories(self.streams)
self.assertTrue(streams_cats, "No categories found in the 'streams' map.")

subbasins_cats = get_categories(self.subbasins)
self.assertTrue(subbasins_cats, "No categories found in the 'subbasins' map.")

# Check that every nonzero channel code in subbasins is present in the streams map.
self.assertTrue(
subbasins_cats.issubset(streams_cats),
"Missing basins for some stream codes",
)


if __name__ == "__main__":
from grass.gunittest.main import test

test()
122 changes: 0 additions & 122 deletions raster/r.basins.fill/testsuite/testrbf.py

This file was deleted.

Loading