Skip to content

Commit

Permalink
pre-commit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
clausmichele committed Nov 9, 2023
1 parent 44e5a20 commit 9eabf4f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from .general import *
from .indices import *
from .load import *
from .mask import *
from .mask_polygon import *
from .merge import *
from .reduce import *
from .mask import *
from .resample import *
from .resample import *
40 changes: 22 additions & 18 deletions openeo_processes_dask/process_implementations/cubes/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import numpy as np

from openeo_processes_dask.process_implementations.cubes.resample import (
resample_cube_spatial,
)
from openeo_processes_dask.process_implementations.cubes.utils import notnull
from openeo_processes_dask.process_implementations.cubes.resample import resample_cube_spatial
from openeo_processes_dask.process_implementations.data_model import RasterCube
from openeo_processes_dask.process_implementations.exceptions import (
DimensionLabelCountMismatch,
Expand Down Expand Up @@ -43,7 +45,7 @@ def mask(data: RasterCube, mask: RasterCube, replacement=None) -> RasterCube:
mask_temporal_labels = mask[n].values
data_n_labels = len(data_temporal_labels)
mask_n_labels = len(mask_temporal_labels)

if not data_n_labels == mask_n_labels:
raise DimensionLabelCountMismatch(
f"data and mask temporal dimensions do no match: data has {data_n_labels} temporal dimensions labels and mask {mask_n_labels}."
Expand Down Expand Up @@ -84,32 +86,34 @@ def mask(data: RasterCube, mask: RasterCube, replacement=None) -> RasterCube:
)

if apply_resample_cube_spatial:
logger.info(
f"mask is aligned to data using resample_cube_spatial."
)
mask = resample_cube_spatial(data=mask,target=data)
logger.info(f"mask is aligned to data using resample_cube_spatial.")
mask = resample_cube_spatial(data=mask, target=data)

original_dim_order = data.dims
# If bands dimension in data but not in mask, ensure that it comes first and all the other dimensions at the end
if (len(data_band_dims) !=0 and len(mask_band_dims) == 0):
required_dim_order = (data_band_dims[0] if len(data_band_dims)>0 else (),
data_temporal_dims[0] if len(data_temporal_dims)>0 else (),
data.openeo.y_dim,
data.openeo.x_dim)
if len(data_band_dims) != 0 and len(mask_band_dims) == 0:
required_dim_order = (
data_band_dims[0] if len(data_band_dims) > 0 else (),
data_temporal_dims[0] if len(data_temporal_dims) > 0 else (),
data.openeo.y_dim,
data.openeo.x_dim,
)
data = data.transpose(*required_dim_order, missing_dims="ignore")
mask = mask.transpose(*required_dim_order, missing_dims="ignore")

elif (len(data_temporal_dims) !=0 and len(mask_temporal_dims) == 0):
required_dim_order = (data_temporal_dims[0] if len(data_temporal_dims)>0 else (),
data_band_dims[0] if len(data_band_dims)>0 else (),
data.openeo.y_dim,
data.openeo.x_dim)
elif len(data_temporal_dims) != 0 and len(mask_temporal_dims) == 0:
required_dim_order = (
data_temporal_dims[0] if len(data_temporal_dims) > 0 else (),
data_band_dims[0] if len(data_band_dims) > 0 else (),
data.openeo.y_dim,
data.openeo.x_dim,
)
data = data.transpose(*required_dim_order, missing_dims="ignore")
mask = mask.transpose(*required_dim_order, missing_dims="ignore")

data = data.where(_not(mask), replacement)

if len(data_band_dims) !=0 and len(mask_band_dims) ==0:
if len(data_band_dims) != 0 and len(mask_band_dims) == 0:
# Order axes back to how they were before
data = data.transpose(*original_dim_order)

Expand Down
31 changes: 22 additions & 9 deletions openeo_processes_dask/process_implementations/cubes/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
from typing import Optional, Union

import odc.geo.xr
import rioxarray # needs to be imported to set .rio accessor on xarray objects.
from odc.geo.geobox import resolution_from_affine
from pyproj.crs import CRS, CRSError
import rioxarray # needs to be imported to set .rio accessor on xarray objects.

from openeo_processes_dask.process_implementations.data_model import RasterCube
from openeo_processes_dask.process_implementations.exceptions import OpenEOException, DimensionMissing
from openeo_processes_dask.process_implementations.exceptions import (
DimensionMissing,
OpenEOException,
)

logger = logging.getLogger(__name__)

__all__ = ["resample_spatial","resample_cube_spatial"]
__all__ = ["resample_spatial", "resample_cube_spatial"]

resample_methods_list = [
"near",
Expand All @@ -34,12 +37,14 @@ def resample_spatial(
projection: Optional[Union[str, int]] = None,
resolution: int = 0,
method: str = "near",
align = "upper-left",
align="upper-left",
):
"""Resamples the spatial dimensions (x,y) of the data cube to a specified resolution and/or warps the data cube to the target projection. At least resolution or projection must be specified."""

logger.info("resample_spatial: in the current implementation the align parameter is not considered!")


logger.info(
"resample_spatial: in the current implementation the align parameter is not considered!"
)

# Assert resampling method is correct.
if method == "near":
method = "nearest"
Expand Down Expand Up @@ -85,6 +90,7 @@ def resample_spatial(

return reprojected


def resample_cube_spatial(
data: RasterCube, target: RasterCube, method="near", options=None
) -> RasterCube:
Expand All @@ -103,8 +109,15 @@ def resample_cube_spatial(
"q3",
]

if data.openeo.y_dim is None or data.openeo.x_dim is None or target.openeo.y_dim is None or target.openeo.x_dim is None:
raise DimensionMissing(f"Spatial dimension missing from data or target. Available dimensions for data: {data.dims} for target: {target.dims}")
if (
data.openeo.y_dim is None
or data.openeo.x_dim is None
or target.openeo.y_dim is None
or target.openeo.x_dim is None
):
raise DimensionMissing(
f"Spatial dimension missing from data or target. Available dimensions for data: {data.dims} for target: {target.dims}"
)

# ODC reproject requires y to be before x
required_dim_order = (..., data.openeo.y_dim, data.openeo.x_dim)
Expand Down
25 changes: 14 additions & 11 deletions tests/test_mask.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
from functools import partial

import numpy as np
import pytest
from openeo_pg_parser_networkx.pg_schema import (
TemporalInterval,
ParameterReference,
)
from openeo_pg_parser_networkx.pg_schema import ParameterReference, TemporalInterval

from openeo_processes_dask.process_implementations.cubes.mask import mask
from openeo_processes_dask.process_implementations.cubes.mask_polygon import (
mask_polygon,
)
from openeo_processes_dask.process_implementations.cubes.mask import (
mask,
)
from openeo_processes_dask.process_implementations.cubes.reduce import (
reduce_dimension,
reduce_spatial,
Expand Down Expand Up @@ -40,6 +37,7 @@ def test_mask_polygon(
assert len(output_cube.y) == len(input_cube.y)
assert len(output_cube.x) == len(input_cube.x)


@pytest.mark.parametrize("size", [(30, 30, 20, 2)])
@pytest.mark.parametrize("dtype", [np.float32])
def test_mask(
Expand Down Expand Up @@ -75,14 +73,19 @@ def test_mask(
output_cube = mask(data=input_cube, mask=mask_cube_no_x)

# Mask should work without bands
mask_cube_no_bands = reduce_dimension(data=mask_cube, dimension="bands", reducer=_process)
mask_cube_no_bands = reduce_dimension(
data=mask_cube, dimension="bands", reducer=_process
)
output_cube = mask(data=input_cube, mask=mask_cube_no_bands)

# Mask should work without time
mask_cube_no_time = reduce_dimension(data=mask_cube, dimension="t", reducer=_process)
mask_cube_no_time = reduce_dimension(
data=mask_cube, dimension="t", reducer=_process
)
output_cube = mask(data=input_cube, mask=mask_cube_no_time)

# Mask should work without time and bands
mask_cube_no_time_bands = reduce_dimension(data=mask_cube_no_bands, dimension="t", reducer=_process)
mask_cube_no_time_bands = reduce_dimension(
data=mask_cube_no_bands, dimension="t", reducer=_process
)
output_cube = mask(data=input_cube, mask=mask_cube_no_time_bands)

7 changes: 4 additions & 3 deletions tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from pyproj.crs import CRS

from openeo_processes_dask.process_implementations.cubes.resample import (
resample_spatial,
resample_cube_spatial,
resample_spatial,
)
from tests.general_checks import general_output_checks
from tests.mockdata import create_fake_rastercube
Expand Down Expand Up @@ -64,6 +64,8 @@ def test_resample_spatial(

assert min(output_cube.y) >= -90
assert max(output_cube.y) <= 90


@pytest.mark.parametrize(
"output_crs",
[
Expand All @@ -73,7 +75,6 @@ def test_resample_spatial(
"4326",
],
)

@pytest.mark.parametrize("output_res", [5, 30, 60])
@pytest.mark.parametrize("size", [(30, 30, 20, 4)])
@pytest.mark.parametrize("dtype", [np.float32])
Expand Down Expand Up @@ -105,7 +106,7 @@ def test_resample_cube_spatial(
general_output_checks(
input_cube=input_cube,
output_cube=output_cube,
expected_dims = input_cube.dims,
expected_dims=input_cube.dims,
verify_attrs=False,
verify_crs=False,
)
Expand Down

0 comments on commit 9eabf4f

Please sign in to comment.