From 6a8dc893f95ea3251ae788d2befef7a3ec81efad Mon Sep 17 00:00:00 2001 From: ValentinaHutter <85164505+ValentinaHutter@users.noreply.github.com> Date: Wed, 13 Apr 2022 10:21:53 +0200 Subject: [PATCH 1/3] Use stack and unstack inside prediction --- src/openeo_processes/cubes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/openeo_processes/cubes.py b/src/openeo_processes/cubes.py index c08db242..778f3511 100644 --- a/src/openeo_processes/cubes.py +++ b/src/openeo_processes/cubes.py @@ -2067,6 +2067,7 @@ def exec_xar(data, model, dimension, predictors_vars = None, client = None): predictor_cols = list(data.dims) if dimension in predictor_cols: predictor_cols.remove(dimension) + unstack = False if len(predictor_cols) == 1: stacked = data I = stacked[predictor_cols[0]].values @@ -2074,6 +2075,7 @@ def exec_xar(data, model, dimension, predictors_vars = None, client = None): stacked = data.stack(z=predictor_cols) I = stacked['z'].values predictor_cols = ['z'] + unstack = True X_hat = stacked.to_dataset(dim=dimension).to_dask_dataframe().drop(columns=predictor_cols) if 'spatial_ref' in X_hat: @@ -2095,6 +2097,8 @@ def exec_xar(data, model, dimension, predictors_vars = None, client = None): predictions_xr = xr.ones_like(p) * y_hat_da predictions_xr.attrs = data.attrs predictions_xr[dimension] = np.array(['prediction']) + if unstack: + predictions_xr = predictions_xr.unstack() return predictions_xr From 8dd5da3d70cb1105253e79f51b5b6ef570f17e5d Mon Sep 17 00:00:00 2001 From: ValentinaHutter <85164505+ValentinaHutter@users.noreply.github.com> Date: Tue, 19 Apr 2022 10:10:37 +0200 Subject: [PATCH 2/3] remove predictors_vars parameter --- src/openeo_processes/cubes.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/openeo_processes/cubes.py b/src/openeo_processes/cubes.py index deb5c704..2ad95842 100644 --- a/src/openeo_processes/cubes.py +++ b/src/openeo_processes/cubes.py @@ -2087,7 +2087,7 @@ def predict_random_forest(): class PredictRandomForest: @staticmethod - def exec_xar(data, model, dimension, predictors_vars = None, client = None): + def exec_xar(data, model, dimension, client = None): if dimension in ['time', 't', 'times']: # time dimension must be converted into values dimension = get_time_dimension_from_data(data, dimension) predictor_cols = list(data.dims) @@ -2104,12 +2104,11 @@ def exec_xar(data, model, dimension, predictors_vars = None, client = None): unstack = True X_hat = stacked.to_dataset(dim=dimension).to_dask_dataframe().drop(columns=predictor_cols) - if 'spatial_ref' in X_hat: - X_hat = X_hat.drop(columns=['spatial_ref']) - if predictors_vars is not None: - for c in X_hat.columns: - if c not in predictors_vars: - X_hat = X_hat.drop(columns=c) + + predictors_vars = data[dimension].values + for c in X_hat.columns: + if c not in predictors_vars: + X_hat = X_hat.drop(columns=c) # Make sure that feature names fit to those of the trained model X_hat = X_hat[model.feature_names] From c46b5d31713c81ec48b0821f025e128e3f93661d Mon Sep 17 00:00:00 2001 From: ValentinaHutter <85164505+ValentinaHutter@users.noreply.github.com> Date: Tue, 19 Apr 2022 16:15:44 +0200 Subject: [PATCH 3/3] load model in prediction --- src/openeo_processes/cubes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/openeo_processes/cubes.py b/src/openeo_processes/cubes.py index 2ad95842..22d5a001 100644 --- a/src/openeo_processes/cubes.py +++ b/src/openeo_processes/cubes.py @@ -2087,7 +2087,9 @@ def predict_random_forest(): class PredictRandomForest: @staticmethod - def exec_xar(data, model, dimension, client = None): + def exec_xar(data, model, dimension, client = None, input_filepath = None): + if isinstance(model, str): + model = load_ml_model(model, input_filepath = input_filepath) if dimension in ['time', 't', 'times']: # time dimension must be converted into values dimension = get_time_dimension_from_data(data, dimension) predictor_cols = list(data.dims)