-
-
Notifications
You must be signed in to change notification settings - Fork 436
r.geomorphon: replace testsuite with pytest reference #7785
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
Merged
+191
−346
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1674d91
r.geomorphon: replace the gunittest testsuite with pytest tests
krcoder123 b7ccfa6
r.geomorphon: clarify the test module docstring
krcoder123 7769336
r.geomorphon: address the test review feedback
krcoder123 0d6ae22
Merge branch 'main' into rgeomorphon-pytest
krcoder123 e6e26d2
Merge branch 'main' into rgeomorphon-pytest
krcoder123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """Fixtures building a deterministic DEM for the r.geomorphon tests.""" | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| import grass.script as gs | ||
| from grass.tools import Tools | ||
|
|
||
| # One hill and one pit on a flat base, so all ten landform classes appear. | ||
| DEM_EXPRESSION = ( | ||
| "200.0 + 15.0 * exp(-((x() - 13.0)^2 + (y() - 13.0)^2) / 8.0) " | ||
| "- 15.0 * exp(-((x() - 27.0)^2 + (y() - 27.0)^2) / 8.0)" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def geomorphon_session(tmp_path_factory): | ||
| """Active session in an XY project with a fixed region and a generated DEM.""" | ||
| project = tmp_path_factory.mktemp("geomorphon_xy") / "project" | ||
| gs.create_project(project) | ||
| with gs.setup.init(project, env=os.environ.copy()) as session: | ||
| with Tools(session=session) as tools: | ||
| tools.g_region(s=0, n=40, w=0, e=40, res=1) | ||
| tools.r_mapcalc(expression=f"dem = {DEM_EXPRESSION}") | ||
| yield session | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| """This is a pytest for r.geomorphon using a deterministic DEM. It covers reference tests, landform tests and a profile check.""" | ||
|
|
||
| import json | ||
|
|
||
| import pytest | ||
|
|
||
| from grass.tools import Tools | ||
|
|
||
| SEARCH = 5 | ||
|
|
||
| REFERENCE = { | ||
| "forms_default": { | ||
| "n": 1444, | ||
| "null_cells": 156, | ||
| "min": 1, | ||
| "max": 10, | ||
| "mean": 2.34349030470914, | ||
| "stddev": 2.31663094892283, | ||
| "sum": 3384, | ||
| }, | ||
| "intensity_default": { | ||
| "n": 1444, | ||
| "null_cells": 156, | ||
| "min": -8.88377952575684, | ||
| "max": 8.88377952575684, | ||
| "mean": 0, | ||
| "stddev": 0.974596266625059, | ||
| "sum": 0, | ||
| }, | ||
| "range_default": { | ||
| "n": 1444, | ||
| "null_cells": 156, | ||
| "min": 0, | ||
| "max": 13.9962615966797, | ||
| "mean": 2.27708118047741, | ||
| "stddev": 3.85280566774583, | ||
| "sum": 3288.10522460938, | ||
| }, | ||
| "forms_extended": { | ||
| "n": 1444, | ||
| "null_cells": 156, | ||
| "min": 1, | ||
| "max": 10, | ||
| "mean": 2.50277008310249, | ||
| "stddev": 2.39415680072826, | ||
| "sum": 3614, | ||
| }, | ||
| "forms_skip1": { | ||
| "n": 1296, | ||
| "null_cells": 304, | ||
| "min": 1, | ||
| "max": 10, | ||
| "mean": 2.49691358024691, | ||
| "stddev": 2.44381110076768, | ||
| "sum": 3236, | ||
| }, | ||
| } | ||
|
|
||
| # Each entry maps a reference key to its output option and any extra options. | ||
| COMBOS = [ | ||
| ("forms_default", "forms", {}), | ||
| ("intensity_default", "intensity", {}), | ||
| ("range_default", "range", {}), | ||
| ("forms_extended", "forms", {"flags": "e", "search": 12}), | ||
| ("forms_skip1", "forms", {"skip": 1}), | ||
| ] | ||
|
|
||
| # Landform categories from flat (1) to pit (10). | ||
| ALL_CLASSES = set(range(1, 11)) | ||
|
|
||
| # Category labels that r.category reports for the forms output. | ||
| EXPECTED_LABELS = { | ||
| 1: "flat", | ||
| 2: "peak", | ||
| 3: "ridge", | ||
| 4: "shoulder", | ||
| 5: "spur", | ||
| 6: "slope", | ||
| 7: "hollow", | ||
| 8: "footslope", | ||
| 9: "valley", | ||
| 10: "pit", | ||
| } | ||
|
|
||
| # Expected final_results of the one-off profile at easting 13, northing 16. | ||
| PROFILE_FINAL = { | ||
| "azimuth": 90, | ||
| "elongation": 3, | ||
| "width_m": 2, | ||
| "intensity_m": 1.1735076904296875, | ||
| "exposition_m": 5.1710052490234375, | ||
| "range_m": 9.4890289306640625, | ||
| "variance": 13.911697387695312, | ||
| "extends": 0.11313708498984762, | ||
| "octagon_perimeter_m": 13.152982445134985, | ||
| "octagon_area_m2": 8, | ||
| "mesh_perimeter_m": 26.577144026719676, | ||
| "mesh_area_m2": 32.973703233080755, | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def fixed_region(geomorphon_session): | ||
| """Session with the region reset to the DEM extent.""" | ||
| Tools(session=geomorphon_session).g_region(s=0, n=40, w=0, e=40, res=1) | ||
| return geomorphon_session | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("key", "option", "extra"), COMBOS) | ||
| def test_reference_values(fixed_region, key, option, extra): | ||
| """Module statistics match the captured serial reference.""" | ||
| tools = Tools(session=fixed_region) | ||
| output = f"out_{key}" | ||
| call = {"elevation": "dem", option: output, "search": SEARCH} | ||
| call.update(extra) | ||
| tools.r_geomorphon(**call) | ||
|
|
||
| stats = tools.r_univar(map=output, format="json") | ||
| ref = REFERENCE[key] | ||
| assert stats["n"] == ref["n"] | ||
| assert stats["null_cells"] == ref["null_cells"] | ||
| for field in ("min", "max", "mean", "stddev", "sum"): | ||
| assert stats[field] == pytest.approx(ref[field], rel=1e-6, abs=5e-8) | ||
|
|
||
|
|
||
| def test_landform_class_set(fixed_region): | ||
|
petrasovaa marked this conversation as resolved.
|
||
| """The forms output contains every landform class with its category label.""" | ||
| tools = Tools(session=fixed_region) | ||
| tools.r_geomorphon(elevation="dem", forms="forms_all", search=SEARCH) | ||
| text = tools.r_stats(input="forms_all", flags="cn").stdout | ||
| classes = {int(line.split()[0]) for line in text.splitlines() if line.strip()} | ||
| assert classes == ALL_CLASSES | ||
| categories = tools.r_category(map="forms_all").stdout | ||
| for cat, label in EXPECTED_LABELS.items(): | ||
| assert f"{cat}\t{label}" in categories | ||
|
|
||
|
|
||
| def test_profile_json(fixed_region): | ||
| """The one-off profile reports the expected spur landform and geometry.""" | ||
| tools = Tools(session=fixed_region) | ||
| out = tools.r_geomorphon( | ||
| elevation="dem", | ||
| coordinates="13,16", | ||
| profiledata="-", | ||
| profileformat="json", | ||
| search=SEARCH, | ||
| ).stdout | ||
| result = json.loads(out) | ||
|
|
||
| assert { | ||
| "map_info", | ||
| "computation_parameters", | ||
| "intermediate_data", | ||
| "final_results", | ||
| } <= set(result) | ||
| final = result["final_results"] | ||
| assert final["landform_cat"] == 5 | ||
| assert final["landform_code"] == "SP" | ||
| assert final["landform_name"] == "spur" | ||
| for field, value in PROFILE_FINAL.items(): | ||
| assert final[field] == pytest.approx(value, rel=1e-6, abs=5e-8) | ||
| inter = result["intermediate_data"] | ||
| assert inter["num_positives"] == 3 | ||
| assert inter["num_negatives"] == 5 | ||
| assert inter["pattern_size"] == 8 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.