def test_run_udf_code_apply_timeseries_tb(): udf_code = textwrap.dedent(""" import pandas as pd def apply_timeseries(series: pd.Series, context: dict) -> pd.Series: return series - series.mean() """) xdc = _build_xdc(ts=[2018, 2019, 2020, 2021], bands=["red", "green", "blue"]) udf_data = UdfData(datacube_list=[xdc]) result = run_udf_code(code=udf_code, data=udf_data) aa, = result.get_datacube_list() assert isinstance(aa, XarrayDataCube) expected_dims = [ { 'name': 't', 'coordinates': [2018, 2019, 2020, 2021] }, { 'name': 'bands', 'coordinates': ["red", "green", "blue"] }, ] assert aa.to_dict() == { 'data': [[-15, -15, -15], [-5, -5, -5], [5, 5, 5], [15, 15, 15]], 'dimensions': expected_dims, }
def test_run_udf_code_reduce_time_mean(): udf_code = _get_udf_code("reduce_time_mean.py") a = _build_txy_data(ts=[2018, 2019, 2020, 2021], xs=[2, 3], ys=[10, 20, 30], name="temp", offset=2) b = _build_txy_data(ts=[2018, 2019, 2020, 2021], xs=[2, 3], ys=[10, 20, 30], name="prec", offset=4) udf_data = UdfData(datacube_list=[a, b]) result = run_udf_code(code=udf_code, data=udf_data) aa, bb = result.get_datacube_list() assert aa.id == "temp_mean" assert aa.array.name == "temp_mean" assert aa.array.shape == (2, 3) assert list( aa.array.values.ravel()) == [152.0, 153.0, 154.0, 162.0, 163.0, 164.0] assert bb.id == "prec_mean" assert bb.array.name == "prec_mean" assert bb.array.shape == (2, 3) assert list( bb.array.values.ravel()) == [154.0, 155.0, 156.0, 164.0, 165.0, 166.0]
def test_run_udf_code_statistics(): udf_code = _get_udf_code("statistics.py") xdc = _build_txy_data(ts=[2018, 2019], xs=[0, 1], ys=[0, 1, 2], name="temp", offset=2) udf_data = UdfData(datacube_list=[xdc]) result = run_udf_code(code=udf_code, data=udf_data) structured, = result.structured_data_list assert structured.to_dict() == { "data": { "temp": { "min": 2, "mean": 58, "max": 114, "sum": 696 } }, "type": "dict", "description": "Statistical data sum, min, max and mean for each raster collection cube as dict" }
def test_run_udf_code_ndvi(): udf_code = _get_udf_code("ndvi02.py") red = _build_txy_data(ts=[2018], xs=[0, 1], ys=[0, 1, 2], name="red", offset=2) nir = _build_txy_data(ts=[2018], xs=[0, 1], ys=[0, 1, 2], name="nir", offset=4) udf_data = UdfData(datacube_list=[red, nir]) result = run_udf_code(code=udf_code, data=udf_data) print(nir.array) print(red.array) n, = result.get_datacube_list() assert n.id == "NDVI" assert n.array.shape == (1, 2, 3) def ndvi(red, nir): return (nir - red) / (nir + red) assert list(n.array.values.ravel()) == [ ndvi(2, 4), ndvi(3, 5), ndvi(4, 6), ndvi(12, 14), ndvi(13, 15), ndvi(14, 16) ]
def test_run_udf_code_apply_timeseries_txy(): udf_code = textwrap.dedent(""" import pandas as pd def apply_timeseries(series: pd.Series, context: dict) -> pd.Series: return series - series.mean() """) a = _build_txy_data(ts=[2018, 2019, 2020, 2021], xs=[2, 3], ys=[10, 20, 30], name="temp", t_factor=2) b = _build_txy_data(ts=[2018, 2019, 2020, 2021], xs=[2, 3], ys=[10, 20, 30], name="prec", t_factor=10) udf_data = UdfData(datacube_list=[a, b]) result = run_udf_code(code=udf_code, data=udf_data) aa, bb = result.get_datacube_list() assert isinstance(aa, XarrayDataCube) expected_dims = [{ 'name': 't', 'coordinates': [2018, 2019, 2020, 2021] }, { 'name': 'x', 'coordinates': [2, 3] }, { 'name': 'y', 'coordinates': [10, 20, 30] }] assert aa.to_dict() == { 'id': 'temp', 'data': [ [[-3, -3, -3], [-3, -3, -3]], [[-1, -1, -1], [-1, -1, -1]], [[1, 1, 1], [1, 1, 1]], [[3, 3, 3], [3, 3, 3]], ], 'dimensions': expected_dims, } assert isinstance(bb, XarrayDataCube) assert bb.to_dict() == { 'id': 'prec', 'data': [[[-15, -15, -15], [-15, -15, -15]], [[-5, -5, -5], [-5, -5, -5]], [[5, 5, 5], [5, 5, 5]], [[15, 15, 15], [15, 15, 15]]], 'dimensions': expected_dims, }
def test_run_udf_code_map_fabs(): udf_code = _get_udf_code("map_fabs.py") xdc = _build_txy_data(ts=[2019, 2020, 2021], xs=[2, 3, 4], ys=[10, 20, 30], name="temp", x_factor=-10) udf_data = UdfData(datacube_list=[xdc]) result = run_udf_code(code=udf_code, data=udf_data) assert list(xdc.array[0, :2, :].values.ravel()) == [0, 1, 2, -10, -9, -8] output, = result.get_datacube_list() assert output.id == "temp_fabs" assert output.array.name == "temp_fabs" assert output.array.shape == (3, 3, 3) assert list(output.array[0, :2, :].values.ravel()) == [0, 1, 2, 10, 9, 8]
def test_run_udf_code_reduce_time_min_median_max(): udf_code = _get_udf_code("reduce_time_min_median_max.py") a = _build_txy_data(ts=[2018, 2019, 2020], xs=[2, 3], ys=[10, 20, 30], name="temp", offset=2) udf_data = UdfData(datacube_list=[a]) result = run_udf_code(code=udf_code, data=udf_data) x, y, z = result.get_datacube_list() assert x.id == "temp_min" assert x.array.shape == (2, 3) assert list(x.array.values.ravel()) == [2, 3, 4, 12, 13, 14] assert y.id == "temp_median" assert y.array.shape == (2, 3) assert list(y.array.values.ravel()) == [102., 103., 104., 112., 113., 114.] assert z.id == "temp_max" assert z.array.shape == (2, 3) assert list(z.array.values.ravel()) == [202., 203., 204., 212., 213., 214.]