-
-
Notifications
You must be signed in to change notification settings - Fork 332
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
base: main
Are you sure you want to change the base?
Changes from all commits
09a8160
930711d
c1338d9
925b2d6
0783946
19f0598
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
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: ""} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why dict with empty keys? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ruff] reported by reviewdog 🐶