From f8d72569f4e4843c3c894bf38a0e37dcab655d9a Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Sat, 5 Oct 2024 18:38:40 -0400 Subject: [PATCH 1/8] Added test script for r.buffer --- raster/r.buffer/testsuite/test_buffer.py | 88 ++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 raster/r.buffer/testsuite/test_buffer.py diff --git a/raster/r.buffer/testsuite/test_buffer.py b/raster/r.buffer/testsuite/test_buffer.py new file mode 100644 index 00000000000..43d95f78557 --- /dev/null +++ b/raster/r.buffer/testsuite/test_buffer.py @@ -0,0 +1,88 @@ +from grass.gunittest.case import TestCase +from grass.gunittest.main import test +from grass.gunittest.gmodules import SimpleModule +import grass.script as gs + + +class TestRBuffer(TestCase): + + @classmethod + def setUpClass(cls): + cls.use_temp_region() + cls.runModule("g.region", raster="elevation") + + @classmethod + def tearDownClass(cls): + cls.del_temp_region() + + def test_buffer_creation(self): + output = "buf_test" + distances = [100, 200, 300, 400, 500] + + module = SimpleModule("r.buffer", input="elevation", output=output, + distances=distances, overwrite=True) + self.assertModule(module) + + self.assertRasterExists(output) + + expected_categories = [1] + [i + 1 for i in range(len(distances))] + + self.assertRasterMinMax( + map=output, + refmin=min(expected_categories), + refmax=max(expected_categories), + msg=("Buffer zones should have category values from 1 to " + #Checking if there are no abnormal values in the output raster map + str(max(expected_categories))) + ) + + category_values = gs.read_command("r.stats", flags="n", input=output).splitlines() + category_values = [int(line.split()[0]) for line in category_values] + + print("Category values:", category_values) + + unique_actual_categories = set(category_values) + self.assertTrue( + unique_actual_categories.issubset(set(expected_categories)), + msg=("Output categories should be a subset of expected categories: " + + str(expected_categories)) + ) + + def test_no_non_null_values(self): + null_map = "null_map" + self.runModule("r.mapcalc", expression=f"{null_map} = null()") + + output = "buf_no_non_null" + distances = [100, 200, 300] + + module = SimpleModule("r.buffer", input=null_map, output=output, + distances=distances, overwrite=True) + self.assertModule(module) + + self.assertRasterExists(output) + stats = gs.read_command("r.univar", map=output, flags="g") + self.assertIn("n=0", stats, msg="Output should have no non-NULL cells") # Checking an edge case where the input raster map is null + + def test_ignore_zero_values(self): + zero_map = "zero_map" + self.runModule("r.mapcalc", expression=f"{zero_map} = if(row() % 2 == 0, 0, 1)") + + output = "buf_ignore_zero" + distances = [100, 200] + + module = SimpleModule("r.buffer", input=zero_map, output=output, + distances=distances, flags="z", overwrite=True) + self.assertModule(module) + + self.assertRasterExists(output) + + category_values = gs.read_command("r.stats", flags="n", input=output).splitlines() + category_values = [int(line.split()[0]) for line in category_values] + + print("Category values:", category_values) + + self.assertNotIn(0, category_values, + msg="Output should not contain buffer zones around zero values") # Check if the output raster map ignored 0 values + + +if __name__ == "__main__": + test() From 7c35d81a6af1711b2db7b8af57bb1cfb7dce4b39 Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Tue, 8 Oct 2024 17:53:05 -0400 Subject: [PATCH 2/8] Corrected input map and added tearDown method to delete temp maps --- raster/r.buffer/testsuite/test_buffer.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/raster/r.buffer/testsuite/test_buffer.py b/raster/r.buffer/testsuite/test_buffer.py index 43d95f78557..9455e7bc298 100644 --- a/raster/r.buffer/testsuite/test_buffer.py +++ b/raster/r.buffer/testsuite/test_buffer.py @@ -9,23 +9,28 @@ class TestRBuffer(TestCase): @classmethod def setUpClass(cls): cls.use_temp_region() - cls.runModule("g.region", raster="elevation") + cls.runModule("g.region", raster="roadsmajor") @classmethod def tearDownClass(cls): cls.del_temp_region() + def tearDown(self): + # Remove temporary maps created during tests + gs.run_command('g.remove', type='raster', + name='null_map,zero_map,buf_test,buf_no_non_null,buf_ignore_zero', flags='f') + def test_buffer_creation(self): output = "buf_test" distances = [100, 200, 300, 400, 500] - module = SimpleModule("r.buffer", input="elevation", output=output, + module = SimpleModule("r.buffer", input="roadsmajor", output=output, distances=distances, overwrite=True) self.assertModule(module) self.assertRasterExists(output) - expected_categories = [1] + [i + 1 for i in range(len(distances))] + expected_categories = [1] + [i + 1 for i in range(len(distances)+1)] self.assertRasterMinMax( map=output, From cb4368f01352d367cddc5cf392e65345233bde28 Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Wed, 9 Oct 2024 16:15:59 -0400 Subject: [PATCH 3/8] Added pre commit fixes --- raster/r.buffer/testsuite/test_buffer.py | 69 +++++++++++++++++------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/raster/r.buffer/testsuite/test_buffer.py b/raster/r.buffer/testsuite/test_buffer.py index 9455e7bc298..068411c64a9 100644 --- a/raster/r.buffer/testsuite/test_buffer.py +++ b/raster/r.buffer/testsuite/test_buffer.py @@ -17,30 +17,43 @@ def tearDownClass(cls): def tearDown(self): # Remove temporary maps created during tests - gs.run_command('g.remove', type='raster', - name='null_map,zero_map,buf_test,buf_no_non_null,buf_ignore_zero', flags='f') + gs.run_command( + "g.remove", + type="raster", + name="null_map,zero_map,buf_test,buf_no_non_null,buf_ignore_zero", + flags="f", + ) def test_buffer_creation(self): output = "buf_test" distances = [100, 200, 300, 400, 500] - module = SimpleModule("r.buffer", input="roadsmajor", output=output, - distances=distances, overwrite=True) + module = SimpleModule( + "r.buffer", + input="roadsmajor", + output=output, + distances=distances, + overwrite=True, + ) self.assertModule(module) self.assertRasterExists(output) - expected_categories = [1] + [i + 1 for i in range(len(distances)+1)] + expected_categories = [1] + [i + 1 for i in range(len(distances) + 1)] self.assertRasterMinMax( map=output, refmin=min(expected_categories), refmax=max(expected_categories), - msg=("Buffer zones should have category values from 1 to " + #Checking if there are no abnormal values in the output raster map - str(max(expected_categories))) + msg=( + "Buffer zones should have category values from 1 to " # Checking if there are no abnormal values in the output raster map + + str(max(expected_categories)) + ), ) - category_values = gs.read_command("r.stats", flags="n", input=output).splitlines() + category_values = gs.read_command( + "r.stats", flags="n", input=output + ).splitlines() category_values = [int(line.split()[0]) for line in category_values] print("Category values:", category_values) @@ -48,8 +61,10 @@ def test_buffer_creation(self): unique_actual_categories = set(category_values) self.assertTrue( unique_actual_categories.issubset(set(expected_categories)), - msg=("Output categories should be a subset of expected categories: " + - str(expected_categories)) + msg=( + "Output categories should be a subset of expected categories: " + + str(expected_categories) + ), ) def test_no_non_null_values(self): @@ -59,13 +74,20 @@ def test_no_non_null_values(self): output = "buf_no_non_null" distances = [100, 200, 300] - module = SimpleModule("r.buffer", input=null_map, output=output, - distances=distances, overwrite=True) + module = SimpleModule( + "r.buffer", + input=null_map, + output=output, + distances=distances, + overwrite=True, + ) self.assertModule(module) self.assertRasterExists(output) stats = gs.read_command("r.univar", map=output, flags="g") - self.assertIn("n=0", stats, msg="Output should have no non-NULL cells") # Checking an edge case where the input raster map is null + self.assertIn( + "n=0", stats, msg="Output should have no non-NULL cells" + ) # Checking an edge case where the input raster map is null def test_ignore_zero_values(self): zero_map = "zero_map" @@ -74,19 +96,30 @@ def test_ignore_zero_values(self): output = "buf_ignore_zero" distances = [100, 200] - module = SimpleModule("r.buffer", input=zero_map, output=output, - distances=distances, flags="z", overwrite=True) + module = SimpleModule( + "r.buffer", + input=zero_map, + output=output, + distances=distances, + flags="z", + overwrite=True, + ) self.assertModule(module) self.assertRasterExists(output) - category_values = gs.read_command("r.stats", flags="n", input=output).splitlines() + category_values = gs.read_command( + "r.stats", flags="n", input=output + ).splitlines() category_values = [int(line.split()[0]) for line in category_values] print("Category values:", category_values) - self.assertNotIn(0, category_values, - msg="Output should not contain buffer zones around zero values") # Check if the output raster map ignored 0 values + self.assertNotIn( + 0, + category_values, + msg="Output should not contain buffer zones around zero values", + ) # Check if the output raster map ignored 0 values if __name__ == "__main__": From b231515a10b12c94add15ab0431d5ad9efa84a24 Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Tue, 22 Oct 2024 21:59:47 -0400 Subject: [PATCH 4/8] Added resolution for feedback --- raster/r.buffer/testsuite/test_buffer.py | 32 +++++++----------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/raster/r.buffer/testsuite/test_buffer.py b/raster/r.buffer/testsuite/test_buffer.py index 068411c64a9..c92993a49bf 100644 --- a/raster/r.buffer/testsuite/test_buffer.py +++ b/raster/r.buffer/testsuite/test_buffer.py @@ -20,7 +20,7 @@ def tearDown(self): gs.run_command( "g.remove", type="raster", - name="null_map,zero_map,buf_test,buf_no_non_null,buf_ignore_zero", + name="null_map,buf_test,consist_test,zero_map,buf_no_non_null,buf_ignore_zero", flags="f", ) @@ -39,7 +39,7 @@ def test_buffer_creation(self): self.assertRasterExists(output) - expected_categories = [1] + [i + 1 for i in range(len(distances) + 1)] + expected_categories = [i + 1 for i in range(len(distances) + 1)] self.assertRasterMinMax( map=output, @@ -51,22 +51,6 @@ def test_buffer_creation(self): ), ) - category_values = gs.read_command( - "r.stats", flags="n", input=output - ).splitlines() - category_values = [int(line.split()[0]) for line in category_values] - - print("Category values:", category_values) - - unique_actual_categories = set(category_values) - self.assertTrue( - unique_actual_categories.issubset(set(expected_categories)), - msg=( - "Output categories should be a subset of expected categories: " - + str(expected_categories) - ), - ) - def test_no_non_null_values(self): null_map = "null_map" self.runModule("r.mapcalc", expression=f"{null_map} = null()") @@ -84,10 +68,10 @@ def test_no_non_null_values(self): self.assertModule(module) self.assertRasterExists(output) - stats = gs.read_command("r.univar", map=output, flags="g") - self.assertIn( - "n=0", stats, msg="Output should have no non-NULL cells" - ) # Checking an edge case where the input raster map is null + + expected_stats = {"n": 0} + + self.assertRasterFitsUnivar(output, reference=expected_stats) def test_ignore_zero_values(self): zero_map = "zero_map" @@ -115,8 +99,10 @@ def test_ignore_zero_values(self): print("Category values:", category_values) + expected_categories = [1, 2] + self.assertNotIn( - 0, + expected_categories, category_values, msg="Output should not contain buffer zones around zero values", ) # Check if the output raster map ignored 0 values From 968e3142a97e5615f2441684acd63a8b9b7a4fb3 Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Sun, 3 Nov 2024 14:40:43 -0500 Subject: [PATCH 5/8] Tweaked the last test and used f string --- raster/r.buffer/testsuite/test_buffer.py | 28 +++++------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/raster/r.buffer/testsuite/test_buffer.py b/raster/r.buffer/testsuite/test_buffer.py index c92993a49bf..03d988c14fa 100644 --- a/raster/r.buffer/testsuite/test_buffer.py +++ b/raster/r.buffer/testsuite/test_buffer.py @@ -20,7 +20,7 @@ def tearDown(self): gs.run_command( "g.remove", type="raster", - name="null_map,buf_test,consist_test,zero_map,buf_no_non_null,buf_ignore_zero", + name="buf_test,zero_map,buf_no_non_null,null_map,zero_map", flags="f", ) @@ -45,10 +45,7 @@ def test_buffer_creation(self): map=output, refmin=min(expected_categories), refmax=max(expected_categories), - msg=( - "Buffer zones should have category values from 1 to " # Checking if there are no abnormal values in the output raster map - + str(max(expected_categories)) - ), + msg=f"Buffer zones should have category values from 1 to {max(expected_categories)}", ) def test_no_non_null_values(self): @@ -70,15 +67,14 @@ def test_no_non_null_values(self): self.assertRasterExists(output) expected_stats = {"n": 0} - self.assertRasterFitsUnivar(output, reference=expected_stats) def test_ignore_zero_values(self): zero_map = "zero_map" - self.runModule("r.mapcalc", expression=f"{zero_map} = if(row() % 2 == 0, 0, 1)") + self.runModule("r.mapcalc", expression=f"{zero_map} = 0") output = "buf_ignore_zero" - distances = [100, 200] + distances = [100] module = SimpleModule( "r.buffer", @@ -92,20 +88,8 @@ def test_ignore_zero_values(self): self.assertRasterExists(output) - category_values = gs.read_command( - "r.stats", flags="n", input=output - ).splitlines() - category_values = [int(line.split()[0]) for line in category_values] - - print("Category values:", category_values) - - expected_categories = [1, 2] - - self.assertNotIn( - expected_categories, - category_values, - msg="Output should not contain buffer zones around zero values", - ) # Check if the output raster map ignored 0 values + expected_stats = {"n": 0} + self.assertRasterFitsUnivar(output, reference=expected_stats) if __name__ == "__main__": From c9e6724b7c2b8034e670075bc2fa08977cd9a66c Mon Sep 17 00:00:00 2001 From: Shreshth Malik Date: Tue, 5 Nov 2024 20:55:49 -0500 Subject: [PATCH 6/8] Update raster/r.buffer/testsuite/test_buffer.py Reduced the region of the test Co-authored-by: Anna Petrasova --- raster/r.buffer/testsuite/test_buffer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raster/r.buffer/testsuite/test_buffer.py b/raster/r.buffer/testsuite/test_buffer.py index 03d988c14fa..b92dc39d54d 100644 --- a/raster/r.buffer/testsuite/test_buffer.py +++ b/raster/r.buffer/testsuite/test_buffer.py @@ -9,7 +9,7 @@ class TestRBuffer(TestCase): @classmethod def setUpClass(cls): cls.use_temp_region() - cls.runModule("g.region", raster="roadsmajor") + cls.runModule("g.region", n=223000, s=220000, w=640000, e=643000, nsres=100) @classmethod def tearDownClass(cls): From 846778204a70de7be816b828af329408786457d0 Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Wed, 6 Nov 2024 01:40:20 -0500 Subject: [PATCH 7/8] Corrected redudancy --- raster/r.buffer/testsuite/test_buffer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raster/r.buffer/testsuite/test_buffer.py b/raster/r.buffer/testsuite/test_buffer.py index 03d988c14fa..74730f7b1b1 100644 --- a/raster/r.buffer/testsuite/test_buffer.py +++ b/raster/r.buffer/testsuite/test_buffer.py @@ -20,7 +20,7 @@ def tearDown(self): gs.run_command( "g.remove", type="raster", - name="buf_test,zero_map,buf_no_non_null,null_map,zero_map", + name="buf_test,zero_map,buf_no_non_null,null_map,buf_ignore_zero", flags="f", ) From 1ff7cbe451f244a2cd906b6585b130000f883dbc Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Thu, 7 Nov 2024 18:01:28 -0500 Subject: [PATCH 8/8] Streamlined output map initialization --- raster/r.buffer/testsuite/test_buffer.py | 31 +++++++++++++----------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/raster/r.buffer/testsuite/test_buffer.py b/raster/r.buffer/testsuite/test_buffer.py index 724fbccc5fa..97fea861104 100644 --- a/raster/r.buffer/testsuite/test_buffer.py +++ b/raster/r.buffer/testsuite/test_buffer.py @@ -8,88 +8,91 @@ class TestRBuffer(TestCase): @classmethod def setUpClass(cls): + """Set up a temporary region for testing.""" + cls.output = "test_buffer" cls.use_temp_region() cls.runModule("g.region", n=223000, s=220000, w=640000, e=643000, nsres=100) @classmethod def tearDownClass(cls): + """Clean up after tests.""" cls.del_temp_region() def tearDown(self): - # Remove temporary maps created during tests + """Remove temporary maps created during tests""" gs.run_command( "g.remove", type="raster", - name="buf_test,zero_map,buf_no_non_null,null_map,buf_ignore_zero", + name="test_buffer,zero_map,null_map", flags="f", ) def test_buffer_creation(self): - output = "buf_test" + """Test creating a buffer around roadsmajor with multiple distances.""" distances = [100, 200, 300, 400, 500] module = SimpleModule( "r.buffer", input="roadsmajor", - output=output, + output=self.output, distances=distances, overwrite=True, ) self.assertModule(module) - self.assertRasterExists(output) + self.assertRasterExists(self.output) expected_categories = [i + 1 for i in range(len(distances) + 1)] self.assertRasterMinMax( - map=output, + map=self.output, refmin=min(expected_categories), refmax=max(expected_categories), msg=f"Buffer zones should have category values from 1 to {max(expected_categories)}", ) def test_no_non_null_values(self): + """Test r.buffer with null input raster resulting in an empty output.""" null_map = "null_map" self.runModule("r.mapcalc", expression=f"{null_map} = null()") - output = "buf_no_non_null" distances = [100, 200, 300] module = SimpleModule( "r.buffer", input=null_map, - output=output, + output=self.output, distances=distances, overwrite=True, ) self.assertModule(module) - self.assertRasterExists(output) + self.assertRasterExists(self.output) expected_stats = {"n": 0} - self.assertRasterFitsUnivar(output, reference=expected_stats) + self.assertRasterFitsUnivar(self.output, reference=expected_stats) def test_ignore_zero_values(self): + """Test r.buffer with input raster of only zero values using -z flag.""" zero_map = "zero_map" self.runModule("r.mapcalc", expression=f"{zero_map} = 0") - output = "buf_ignore_zero" distances = [100] module = SimpleModule( "r.buffer", input=zero_map, - output=output, + output=self.output, distances=distances, flags="z", overwrite=True, ) self.assertModule(module) - self.assertRasterExists(output) + self.assertRasterExists(self.output) expected_stats = {"n": 0} - self.assertRasterFitsUnivar(output, reference=expected_stats) + self.assertRasterFitsUnivar(self.output, reference=expected_stats) if __name__ == "__main__":