-
-
Notifications
You must be signed in to change notification settings - Fork 370
t.rast.extract: Handle fully qualified map names and semantic labels #6300
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
Merged
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
935a306
add pytest for name replacement
ninsbl 817e519
add pytest for name replacement
ninsbl 49fcdf1
add pytest for name replacement
ninsbl be0d581
include semantic labels in test
ninsbl e2ff8d3
handle semantic labels
ninsbl 193207a
ensure safe name replacement
ninsbl 7845c74
add test for inconsistent names in expression
ninsbl 1ca283c
remove unneeded assert
ninsbl abf576d
Merge branch 'main' into strds_extract
ninsbl 9234918
ruff
ninsbl 6e872bd
Apply suggestions from code review
ninsbl c2b8d1e
Merge branch 'main' into strds_extract
ninsbl 21d49ce
fix
ninsbl b4db515
handle semantic_label properly
ninsbl dcddd66
address test failures and old DB layout
ninsbl e79471a
fix semantic label check and cleanup
ninsbl 0b30c74
move pytest and remove duplicate case
ninsbl b574ecf
moved
ninsbl 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
171 changes: 171 additions & 0 deletions
171
temporal/t.rast.extract/testsuite/test_t_rast_extract_pytest.py
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,171 @@ | ||
| """Test t.rast.extract. | ||
| This test checks that t.rast.extract works also when | ||
| a) a fully qualified name is used in the expression, | ||
| b) it is run with a STRDS from another mapset as input and | ||
| c) the STRDS contains maps with identical temporal extent but with | ||
| different semantic labels | ||
| (C) 2025 by the GRASS Development Team | ||
| This program is free software under the GNU General Public | ||
| License (>=v2). Read the file COPYING that comes with GRASS | ||
| for details. | ||
| @author Stefan Blumentrath | ||
| """ | ||
|
|
||
| import os | ||
| import shutil | ||
| from pathlib import Path | ||
|
|
||
| import grass.script as gs | ||
| import pytest | ||
| from grass.tools import Tools | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def session(tmp_path_factory): | ||
| """Pytest fixture to create a temporary GRASS project with a STRDS. | ||
| This setup initializes the GRASS environment, sets the computational region, | ||
| creates a series of raster maps with semantic labels, and rgisters | ||
| the maps in a new SpaceTimeRasterDataSet (STRDS). | ||
| Yields: | ||
| session: active GRASS session with the prepared project and STRDS. | ||
| """ | ||
| # Create a single temporary project directory once per module | ||
| tmp_path = tmp_path_factory.mktemp("raster_time_series") | ||
|
|
||
| # Remove if exists (defensive cleanup) | ||
| if tmp_path.exists(): | ||
| shutil.rmtree(tmp_path) | ||
|
|
||
| gs.create_project(tmp_path) | ||
|
|
||
| with gs.setup.init( | ||
| tmp_path.parent, | ||
| location=tmp_path.name, | ||
| env=os.environ.copy(), | ||
| ) as session: | ||
| tools = Tools(session=session) | ||
| tools.g_mapset(mapset="perc", flags="c") | ||
| tools.g_gisenv(set="TGIS_USE_CURRENT_MAPSET=1") | ||
| tools.g_region(s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10) | ||
| register_strings = [] | ||
| for i in range(1, 4): | ||
| for label in ["A", "B"]: | ||
| mapname = f"prec_{label}_{i}" | ||
| semantic_label = f"S2{label}_{i}" | ||
| tools.r_mapcalc(expression=f"{mapname} = {i}00", overwrite=True) | ||
| tools.r_semantic_label( | ||
| map=mapname, | ||
| semantic_label=semantic_label, | ||
| overwrite=True, | ||
| ) | ||
| register_strings.append(f"{mapname}|2025-08-0{i}|{semantic_label}\n") | ||
|
|
||
| tools.t_create( | ||
| type="strds", | ||
| temporaltype="absolute", | ||
| output="prec", | ||
| title="A test", | ||
| description="A test", | ||
| overwrite=True, | ||
| ) | ||
| tmp_file = tools.g_tempfile(pid=os.getpid()).text | ||
| Path(tmp_file).write_text("".join(register_strings), encoding="UTF8") | ||
| tools.t_register( | ||
| type="raster", | ||
| input="prec", | ||
| file=tmp_file, | ||
| overwrite=True, | ||
| ) | ||
| yield session | ||
|
|
||
|
|
||
| def test_selection(session): | ||
| """Perform a simple selection by datetime.""" | ||
| tools = Tools(session=session) | ||
| gisenv = gs.gisenv(env=session.env) | ||
| tools.t_rast_extract( | ||
| input="prec", | ||
| output="prec_1", | ||
| where="start_time > '2025-08-01'", | ||
| verbose=True, | ||
| overwrite=True, | ||
| ) | ||
| assert gisenv["MAPSET"] == "perc" | ||
ninsbl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_selection_and_expression(session): | ||
| """Perform a selection and a r.mapcalc expression with full name.""" | ||
| result = "prec_2" | ||
| tools = Tools(session=session) | ||
| gisenv = gs.gisenv(env=session.env) | ||
| tools.t_rast_extract( | ||
| input="prec", | ||
| output=result, | ||
| where="start_time > '2025-08-01'", | ||
| expression=" if(prec@perc>=200,prec@perc,null())", | ||
| basename="prec_2", | ||
| nprocs=2, | ||
| verbose=True, | ||
| overwrite=True, | ||
| ) | ||
| strds_info = gs.parse_key_val(tools.t_info(input=result, flags="g").text) | ||
ninsbl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expected_info = { | ||
| "start_time": "'2025-08-02 00:00:00'", | ||
| "end_time": "'2025-08-03 00:00:00'", | ||
| "name": result, | ||
| "min_min": "200.0", | ||
| "min_max": "300.0", | ||
| "max_min": "200.0", | ||
| "max_max": "300.0", | ||
| "aggregation_type": "None", | ||
| "number_of_semantic_labels": "4", | ||
| "semantic_labels": "S2A_2,S2A_3,S2B_2,S2B_3", | ||
| "number_of_maps": "4", | ||
| } | ||
| for k, v in expected_info.items(): | ||
| assert ( | ||
| strds_info[k] == v | ||
| ), f"Expected value for key '{k}' is {v}. Got: {strds_info[k]}" | ||
ninsbl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert gisenv["MAPSET"] == "perc" | ||
|
|
||
|
|
||
| def test_selection_and_expression_simple_name(session): | ||
| """Perform a selection and a r.mapcalc expression with simple name.""" | ||
| result = "prec_3" | ||
| tools = Tools(session=session) | ||
| gisenv = gs.gisenv(env=session.env) | ||
| tools.t_rast_extract( | ||
| input="prec", | ||
| output=result, | ||
| where="start_time > '2025-08-01'", | ||
| expression=" if(prec>= 200, prec, null())", | ||
| basename="prec_3", | ||
| nprocs=2, | ||
| verbose=True, | ||
| overwrite=True, | ||
| ) | ||
| strds_info = gs.parse_key_val(tools.t_info(input=result, flags="g").text) | ||
| expected_info = { | ||
| "start_time": "'2025-08-02 00:00:00'", | ||
| "end_time": "'2025-08-03 00:00:00'", | ||
| "name": result, | ||
| "min_min": "200.0", | ||
| "min_max": "300.0", | ||
| "max_min": "200.0", | ||
| "max_max": "300.0", | ||
| "aggregation_type": "None", | ||
| "number_of_semantic_labels": "4", | ||
| "semantic_labels": "S2A_2,S2A_3,S2B_2,S2B_3", | ||
| "number_of_maps": "4", | ||
| } | ||
| for k, v in expected_info.items(): | ||
| assert ( | ||
| strds_info[k] == v | ||
| ), f"Expected value for key '{k}' is {v}. Got: {strds_info[k]}" | ||
echoix marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ninsbl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert gisenv["MAPSET"] == "perc" | ||
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.