From 29775ea5743c3e36c721cad9b1596c712354aed2 Mon Sep 17 00:00:00 2001 From: pascalau Date: Thu, 18 Nov 2021 14:52:50 +0100 Subject: [PATCH] first prototype for detecting invalid features --- .../src/optimizers/linear_regressor.py | 1 + .../src/optimizers/optimizer_base_template.py | 11 +++++++++++ Tools/parametric_model/src/optimizers/qp_optimizer.py | 1 + 3 files changed, 13 insertions(+) diff --git a/Tools/parametric_model/src/optimizers/linear_regressor.py b/Tools/parametric_model/src/optimizers/linear_regressor.py index 36421017..cdcae56b 100644 --- a/Tools/parametric_model/src/optimizers/linear_regressor.py +++ b/Tools/parametric_model/src/optimizers/linear_regressor.py @@ -54,6 +54,7 @@ def estimate_parameters(self, X, y): # Estimate parameters c such that X * c = y self.X = X self.y = y + self.check_features() self.reg.fit(self.X, self.y) self.estimation_completed = True diff --git a/Tools/parametric_model/src/optimizers/optimizer_base_template.py b/Tools/parametric_model/src/optimizers/optimizer_base_template.py index b5236dd0..e39ba106 100644 --- a/Tools/parametric_model/src/optimizers/optimizer_base_template.py +++ b/Tools/parametric_model/src/optimizers/optimizer_base_template.py @@ -38,6 +38,8 @@ from abc import ABC, abstractmethod from typing import Dict, List +import warnings +import numpy as np class ParametersNotEstimatedError(Exception): @@ -67,6 +69,15 @@ def check_estimation_completed(self): else: raise self.parametersNotEstimatedError + def check_features(self): + for i in range(self.X.shape[1]): + if np.count_nonzero(self.X[:,i]) == 0: + warnings.warn("Feature detected that is only zero. " + \ + "Parameter {} is probably wrong.".format(self.param_name_list[i]), + RuntimeWarning + ) + return + @abstractmethod def estimate_parameters(self) -> None: pass diff --git a/Tools/parametric_model/src/optimizers/qp_optimizer.py b/Tools/parametric_model/src/optimizers/qp_optimizer.py index 14c2dfeb..8778ad8e 100644 --- a/Tools/parametric_model/src/optimizers/qp_optimizer.py +++ b/Tools/parametric_model/src/optimizers/qp_optimizer.py @@ -158,6 +158,7 @@ def estimate_parameters(self, X, y): # remove fixed coefficients from problem formulation self.X = X self.y = y + self.check_features() self.X_reduced, self.y_reduced = self.remove_fixed_coef_features(X, y) self.y = y c = cvxpy.Variable(self.n_opt_coef)