From 09a8160d993a1b14ce58bf3eb28c205537d80884 Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Mon, 18 Nov 2024 15:43:47 -0500 Subject: [PATCH 1/4] Added tests for r.clump tool --- raster/r.clump/testsuite/test_clump.py | 70 ++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 raster/r.clump/testsuite/test_clump.py diff --git a/raster/r.clump/testsuite/test_clump.py b/raster/r.clump/testsuite/test_clump.py new file mode 100644 index 00000000000..a552fc6c4bd --- /dev/null +++ b/raster/r.clump/testsuite/test_clump.py @@ -0,0 +1,70 @@ +import pytest +from grass.script import run_command, read_command, parse_command + + +@pytest.fixture(scope="module") +def setup_map(): + """Set up a temporary region and generate a clumped map.""" + # Set the region + run_command("g.region", n=3, s=0, e=3, w=0, res=1) + + # Create the custom map + # 5 null null + # null 5 null + # null 6 6 + run_command( + "r.mapcalc", + expression=( + "custom_map = " + "if(row() == 1 && col() == 1, 5, " + "if(row() == 2 && col() == 2, 5, " + "if(row() == 3 && col() >= 2, 6, null())))" + ), + overwrite=True, + ) + yield + + # Teardown: Remove maps + run_command( + "g.remove", flags="f", type="raster", name=["custom_map", "clumped_map"] + ) + + +def test_clump_basic(setup_map): + """Test basic clumped map.""" + + run_command("r.clump", input="custom_map", output="clumped_map", overwrite=True) + + output_maps = parse_command("g.list", type="raster") + assert "clumped_map" in output_maps, "Output raster map 'clumped_map' should exist" + + category_output = read_command("r.category", map="clumped_map").strip().split("\n") + actual_categories = { + int(line.split("\t")[0]): line.split("\t")[1].strip() if "\t" in line else "" + for line in category_output + } + + expected_categories = {1: "", 2: "", 3: ""} + + assert set(actual_categories.keys()) == set(expected_categories.keys()) + + +def test_clump_diagonal(setup_map): + """Test clumped map with diagonal connectivity.""" + + run_command( + "r.clump", input="custom_map", output="clumped_map", flags="d", overwrite=True + ) + + output_maps = parse_command("g.list", type="raster") + assert "clumped_map" in output_maps, "Output raster map 'clumped_map' should exist" + + category_output = read_command("r.category", map="clumped_map").strip().split("\n") + actual_categories = { + int(line.split("\t")[0]): line.split("\t")[1].strip() if "\t" in line else "" + for line in category_output + } + + expected_categories = {1: "", 2: ""} + + assert set(actual_categories.keys()) == set(expected_categories.keys()) From 930711d0bcd8554535bba4c33ae36b3155c5bf45 Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Thu, 21 Nov 2024 15:11:43 -0500 Subject: [PATCH 2/4] Added the test files to "tests" folder --- raster/r.clump/{testsuite => tests}/test_clump.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename raster/r.clump/{testsuite => tests}/test_clump.py (100%) diff --git a/raster/r.clump/testsuite/test_clump.py b/raster/r.clump/tests/test_clump.py similarity index 100% rename from raster/r.clump/testsuite/test_clump.py rename to raster/r.clump/tests/test_clump.py From 925b2d68eb83661f87ffb9f196885b29b917864e Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Mon, 27 Jan 2025 19:16:45 -0500 Subject: [PATCH 3/4] added minsize and threshold tests, added conftest file and worked on other fixes --- raster/r.clump/tests/conftest.py | 50 +++++++++++ raster/r.clump/tests/test_clump.py | 129 ++++++++++++++++++++--------- 2 files changed, 138 insertions(+), 41 deletions(-) create mode 100644 raster/r.clump/tests/conftest.py diff --git a/raster/r.clump/tests/conftest.py b/raster/r.clump/tests/conftest.py new file mode 100644 index 00000000000..6cbab2a256b --- /dev/null +++ b/raster/r.clump/tests/conftest.py @@ -0,0 +1,50 @@ +import os +import pytest +import grass.script as gs + + +@pytest.fixture +def setup_maps(tmp_path): + """Set up a GRASS session and create test raster maps.""" + + # Initialize GRASS project + project = tmp_path / "r_clump_project" + gs.create_project(project) + with gs.setup.init(project, env=os.environ.copy()) as session: + # Set the region + gs.run_command( + "g.region", + n=3, + s=0, + e=3, + w=0, + res=1, + env=session.env, + ) + + gs.mapcalc( + "custom_map = " + "if(row() == 1 && col() == 1, 5, " + "if(row() == 1 && col() == 2, 5.5, " + "if(row() == 2 && col() == 2, 5, " + "if(row() == 3 && col() >= 2, 5.5, null()))))", + overwrite=True, + env=session.env, + ) + + gs.mapcalc( + "custom_map1 = " + "if(row() == 1 && col() == 1, 1, " + "if(row() == 1 && col() == 2, 2, " + "if(row() == 1 && col() == 3, 3, " + "if(row() == 2 && col() == 1, 4, " + "if(row() == 2 && col() == 2, 5, " + "if(row() == 2 && col() == 3, 6, " + "if(row() == 3 && col() == 1, 7, " + "if(row() == 3 && col() == 2, 8, " + "if(row() == 3 && col() == 3, 9, null())))))))))", + overwrite=True, + env=session.env, + ) + + yield session # Pass the session to tests \ No newline at end of file diff --git a/raster/r.clump/tests/test_clump.py b/raster/r.clump/tests/test_clump.py index a552fc6c4bd..c719a2a209f 100644 --- a/raster/r.clump/tests/test_clump.py +++ b/raster/r.clump/tests/test_clump.py @@ -1,44 +1,58 @@ -import pytest -from grass.script import run_command, read_command, parse_command - - -@pytest.fixture(scope="module") -def setup_map(): - """Set up a temporary region and generate a clumped map.""" - # Set the region - run_command("g.region", n=3, s=0, e=3, w=0, res=1) - - # Create the custom map - # 5 null null - # null 5 null - # null 6 6 - run_command( - "r.mapcalc", - expression=( - "custom_map = " - "if(row() == 1 && col() == 1, 5, " - "if(row() == 2 && col() == 2, 5, " - "if(row() == 3 && col() >= 2, 6, null())))" - ), +import grass.script as gs + + +def results_check(actual_categories, expected_categories): + # Test if keys match + assert set(actual_categories.keys()) == set(expected_categories.keys()) + + # Test to check if labels are empty + for key in expected_categories.keys(): + assert actual_categories[key] == expected_categories[key] + + +def test_clump_basic(setup_maps): + """Test basic clumped map.""" + session = setup_maps + gs.run_command( + "r.clump", + input="custom_map", + output="clumped_map", overwrite=True, + env=session.env, ) - yield - # Teardown: Remove maps - run_command( - "g.remove", flags="f", type="raster", name=["custom_map", "clumped_map"] - ) + output_maps = gs.parse_command("g.list", type="raster", env=session.env) + assert "clumped_map" in output_maps, "Output raster map 'clumped_map' should exist" + category_output = gs.parse_command("r.category", map="clumped_map", env=session.env) -def test_clump_basic(setup_map): - """Test basic clumped map.""" + actual_categories = { + int(line.split("\t")[0]): line.split("\t")[1].strip() if "\t" in line else "" + for line in category_output + } + + expected_categories = {1: "", 2: "", 3: "", 4: ""} + + results_check(actual_categories, expected_categories) - run_command("r.clump", input="custom_map", output="clumped_map", overwrite=True) - output_maps = parse_command("g.list", type="raster") +def test_clump_diagonal(setup_maps): + """Test clumped map with diagonal connectivity.""" + session = setup_maps + gs.run_command( + "r.clump", + input="custom_map", + output="clumped_map", + flags="d", + overwrite=True, + env=session.env, + ) + + output_maps = gs.parse_command("g.list", type="raster", env=session.env) assert "clumped_map" in output_maps, "Output raster map 'clumped_map' should exist" - category_output = read_command("r.category", map="clumped_map").strip().split("\n") + category_output = gs.parse_command("r.category", map="clumped_map", env=session.env) + actual_categories = { int(line.split("\t")[0]): line.split("\t")[1].strip() if "\t" in line else "" for line in category_output @@ -46,20 +60,26 @@ def test_clump_basic(setup_map): expected_categories = {1: "", 2: "", 3: ""} - assert set(actual_categories.keys()) == set(expected_categories.keys()) - + results_check(actual_categories, expected_categories) -def test_clump_diagonal(setup_map): - """Test clumped map with diagonal connectivity.""" - run_command( - "r.clump", input="custom_map", output="clumped_map", flags="d", overwrite=True +def test_clump_minsize(setup_maps): + """Test clumped map with minimum size parameter.""" + session = setup_maps + gs.run_command( + "r.clump", + input="custom_map", + output="clumped_map", + minsize=2, + overwrite=True, + env=session.env, ) - output_maps = parse_command("g.list", type="raster") + output_maps = gs.parse_command("g.list", type="raster", env=session.env) assert "clumped_map" in output_maps, "Output raster map 'clumped_map' should exist" - category_output = read_command("r.category", map="clumped_map").strip().split("\n") + category_output = gs.parse_command("r.category", map="clumped_map", env=session.env) + actual_categories = { int(line.split("\t")[0]): line.split("\t")[1].strip() if "\t" in line else "" for line in category_output @@ -67,4 +87,31 @@ def test_clump_diagonal(setup_map): expected_categories = {1: "", 2: ""} - assert set(actual_categories.keys()) == set(expected_categories.keys()) + results_check(actual_categories, expected_categories) + + +def test_clump_threshold(setup_maps): + """Test clumped map with threshold parameter.""" + session = setup_maps + gs.run_command( + "r.clump", + input="custom_map1", + output="clumped_map", + threshold=0.2, + overwrite=True, + env=session.env, + ) + + output_maps = gs.parse_command("g.list", type="raster", env=session.env) + assert "clumped_map" in output_maps, "Output raster map 'clumped_map' should exist" + + category_output = gs.parse_command("r.category", map="clumped_map", env=session.env) + + actual_categories = { + int(line.split("\t")[0]): line.split("\t")[1].strip() if "\t" in line else "" + for line in category_output + } + + expected_categories = {1: "", 2: "", 3: ""} + + results_check(actual_categories, expected_categories) From 51e235129431c9da1a93c06aa03d5c69267e2029 Mon Sep 17 00:00:00 2001 From: Shreshth-Malik Date: Tue, 11 Feb 2025 13:51:29 -0500 Subject: [PATCH 4/4] Formatted conftest.py --- raster/r.clump/tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raster/r.clump/tests/conftest.py b/raster/r.clump/tests/conftest.py index 6cbab2a256b..393bd31ba25 100644 --- a/raster/r.clump/tests/conftest.py +++ b/raster/r.clump/tests/conftest.py @@ -47,4 +47,4 @@ def setup_maps(tmp_path): env=session.env, ) - yield session # Pass the session to tests \ No newline at end of file + yield session # Pass the session to tests