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.clump: added tests for r.clump module #4733

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions raster/r.clump/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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

Check failure on line 50 in raster/r.clump/tests/conftest.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (W292)

raster/r.clump/tests/conftest.py:50:51: W292 No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ruff] reported by reviewdog 🐶

Suggested change
yield session # Pass the session to tests
yield session # Pass the session to tests

117 changes: 117 additions & 0 deletions raster/r.clump/tests/test_clump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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,
)

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: "", 4: ""}

results_check(actual_categories, expected_categories)


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 = 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: ""}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why dict with empty keys?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output for r.category came in the form of {'1': None, '2': None, '3': None}, the categories did not have any labels so i kept the expected categories values empty as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you should test that the category label is empty as well.


results_check(actual_categories, expected_categories)


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 = 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: ""}

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)
Loading