From a3c66d86ce81781d0d6b04327a7879ad9aa0299b Mon Sep 17 00:00:00 2001 From: JeroenVerstraelen Date: Wed, 25 Sep 2024 09:56:42 +0200 Subject: [PATCH 1/6] add support for apply_vectorcube udf #856 --- tests/test_vectorcube.py | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/test_vectorcube.py b/tests/test_vectorcube.py index 8a772651..4afc6a09 100644 --- a/tests/test_vectorcube.py +++ b/tests/test_vectorcube.py @@ -22,6 +22,8 @@ import numpy.testing as npt import geopandas as gpd +from utils import EvalEnv + @pytest.mark.parametrize( "file",[ @@ -247,3 +249,50 @@ def test_aggregatespatialresultcsv_vector_to_raster(imagecollection_with_two_ban else: assert mean_band0[0] == expected_band_values[0] and np.isnan(mean_band0[1]) assert mean_band1[0] == expected_band_values[1] and np.isnan(mean_band1[1]) + + +def test_raster_to_vector_and_apply_udf(imagecollection_with_two_bands_and_three_dates, backend_implementation): + vectorcube: DriverVectorCube = imagecollection_with_two_bands_and_three_dates.raster_to_vector() + # Check raster_to_vector result. + assert isinstance(vectorcube, DriverVectorCube) + cube: xarray.DataArray = vectorcube.get_cube() + num_geometries = vectorcube.geometry_count() + nr_bands, nr_dates = (1, 2) + assert cube is not None + assert num_geometries > 1 + assert cube.shape == (num_geometries, nr_dates, nr_bands) + assert cube.dims == ('geometry', 't', 'bands') + assert vectorcube._geometries.shape == (num_geometries,1) + + date1_values = np.array([[np.nan], [np.nan], [np.nan], [np.nan], [ 1.], [ 1.], [ 1.], [ 1.]]) + date2_values = np.array([[2.], [2.], [2.], [2.], [np.nan], [np.nan], [np.nan], [np.nan]]) + assert np.allclose(cube.isel(t=0).values, date1_values, equal_nan=True) + assert np.allclose(cube.isel(t=1).values, date2_values, equal_nan=True) + + # Test apply_dimension with UDF on raster_to_vector result. + process = { + "process_graph": { + "process_id": "run_udf", + "result": True, + "arguments": { + "data": { + "from_parameter": "data" + }, + "udf": "import openeo\nprint(\"hello\")\ndef apply_vectorcube(geometries, cube: xarray.DataArray):\n\treturn (geometries, cube)", + "runtime": "Python", + "version": "1.0.0", + "context": {} + } + } + } + env = EvalEnv(values = { + "backend_implementation": backend_implementation, + }) + vectorcube_udf_applied = vectorcube.apply_dimension(process=process, dimension=DriverVectorCube.DIM_GEOMETRY, env=env) + assert isinstance(vectorcube_udf_applied, DriverVectorCube) + cube_udf_applied = vectorcube_udf_applied.get_cube() + assert cube_udf_applied.shape == cube.shape + assert cube_udf_applied.dims == cube.dims + assert cube_udf_applied.shape == cube.shape + assert np.allclose(cube_udf_applied.values, cube.values, equal_nan=True) + assert all(vectorcube_udf_applied.get_geometries() == vectorcube.get_geometries()) From 506a4e05ffe46017f25ff7e601d5f704221b58d7 Mon Sep 17 00:00:00 2001 From: JeroenVerstraelen <3028262+JeroenVerstraelen@users.noreply.github.com> Date: Wed, 25 Sep 2024 16:56:02 +0200 Subject: [PATCH 2/6] update imports in tests/test_vectorcube.py Co-authored-by: Stefaan Lippens --- tests/test_vectorcube.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_vectorcube.py b/tests/test_vectorcube.py index 4afc6a09..28710865 100644 --- a/tests/test_vectorcube.py +++ b/tests/test_vectorcube.py @@ -22,7 +22,7 @@ import numpy.testing as npt import geopandas as gpd -from utils import EvalEnv +from openeo_driver.utils import EvalEnv @pytest.mark.parametrize( From 52d729309a436d1ddb27e3fadfd316b0422e0c31 Mon Sep 17 00:00:00 2001 From: JeroenVerstraelen Date: Thu, 26 Sep 2024 09:25:06 +0200 Subject: [PATCH 3/6] improve udf in raster_to_vector udf test #856 --- tests/test_vectorcube.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_vectorcube.py b/tests/test_vectorcube.py index 28710865..292f7094 100644 --- a/tests/test_vectorcube.py +++ b/tests/test_vectorcube.py @@ -278,7 +278,7 @@ def test_raster_to_vector_and_apply_udf(imagecollection_with_two_bands_and_three "data": { "from_parameter": "data" }, - "udf": "import openeo\nprint(\"hello\")\ndef apply_vectorcube(geometries, cube: xarray.DataArray):\n\treturn (geometries, cube)", + "udf": "import openeo\nprint(\"hello\")\ndef apply_vectorcube(geometries, cube: xarray.DataArray):\n\tcube_squared = cube ** 2\n\treturn (geometries, cube_squared)", "runtime": "Python", "version": "1.0.0", "context": {} @@ -294,5 +294,5 @@ def test_raster_to_vector_and_apply_udf(imagecollection_with_two_bands_and_three assert cube_udf_applied.shape == cube.shape assert cube_udf_applied.dims == cube.dims assert cube_udf_applied.shape == cube.shape - assert np.allclose(cube_udf_applied.values, cube.values, equal_nan=True) + assert np.allclose(cube_udf_applied.values, cube.values ** 2, equal_nan=True) assert all(vectorcube_udf_applied.get_geometries() == vectorcube.get_geometries()) From 93a60776b6e3dd90e1a305f3a142db73391619aa Mon Sep 17 00:00:00 2001 From: JeroenVerstraelen Date: Thu, 26 Sep 2024 09:47:00 +0200 Subject: [PATCH 4/6] add context parameter to apply_vectorcube udf test #856 --- tests/test_vectorcube.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_vectorcube.py b/tests/test_vectorcube.py index 292f7094..eda762c9 100644 --- a/tests/test_vectorcube.py +++ b/tests/test_vectorcube.py @@ -278,7 +278,7 @@ def test_raster_to_vector_and_apply_udf(imagecollection_with_two_bands_and_three "data": { "from_parameter": "data" }, - "udf": "import openeo\nprint(\"hello\")\ndef apply_vectorcube(geometries, cube: xarray.DataArray):\n\tcube_squared = cube ** 2\n\treturn (geometries, cube_squared)", + "udf": "import openeo\nprint(\"hello\")\ndef apply_vectorcube(geometries, cube: xarray.DataArray, context: dict):\n\tcube_squared = cube ** 2\n\treturn (geometries, cube_squared)", "runtime": "Python", "version": "1.0.0", "context": {} From 42ab296b2e7cddd66bb626bc2dbe516c91c6974c Mon Sep 17 00:00:00 2001 From: JeroenVerstraelen Date: Thu, 26 Sep 2024 11:58:29 +0200 Subject: [PATCH 5/6] add geodataframe annotation to vectorcube udf test #856 --- tests/test_vectorcube.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_vectorcube.py b/tests/test_vectorcube.py index eda762c9..5d6ff4dd 100644 --- a/tests/test_vectorcube.py +++ b/tests/test_vectorcube.py @@ -278,7 +278,7 @@ def test_raster_to_vector_and_apply_udf(imagecollection_with_two_bands_and_three "data": { "from_parameter": "data" }, - "udf": "import openeo\nprint(\"hello\")\ndef apply_vectorcube(geometries, cube: xarray.DataArray, context: dict):\n\tcube_squared = cube ** 2\n\treturn (geometries, cube_squared)", + "udf": "import openeo\nimport geopandas as gpd\nprint(\"hello\")\ndef apply_vectorcube(geometries: gpd.GeoDataFrame, cube: xarray.DataArray, context: dict):\n\tcube_squared = cube ** 2\n\treturn (geometries, cube_squared)", "runtime": "Python", "version": "1.0.0", "context": {} From 2e6628a6f6c20f9076376620278ec973af9f288a Mon Sep 17 00:00:00 2001 From: JeroenVerstraelen Date: Thu, 26 Sep 2024 19:31:41 +0200 Subject: [PATCH 6/6] bump client and driver dependencies #881 --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index f50d5374..361859d8 100644 --- a/setup.py +++ b/setup.py @@ -52,8 +52,8 @@ setup_requires=['pytest-runner'], tests_require=tests_require, install_requires=[ - "openeo>=0.30.0.a2.dev", - "openeo_driver>=0.107.7.dev", + "openeo>=0.32.0.a2.dev", + "openeo_driver>=0.108.0.dev", 'pyspark==3.4.2; python_version>="3.8"', 'pyspark>=2.3.1,<2.4.0; python_version<"3.8"', 'geopyspark==0.4.7+openeo',