From 5508730b4e7f60bae88ac3321013131d83489edd Mon Sep 17 00:00:00 2001 From: lorenzodagostinoradicalbit Date: Mon, 1 Jul 2024 16:58:38 +0200 Subject: [PATCH 1/3] fix: check that prediction_proba is not None before checking its type --- api/app/models/model_dto.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/api/app/models/model_dto.py b/api/app/models/model_dto.py index f76fdbd6..abe9eef1 100644 --- a/api/app/models/model_dto.py +++ b/api/app/models/model_dto.py @@ -99,7 +99,9 @@ def validate_outputs(self) -> Self: raise ValueError( f'prediction must be a number for a ModelType.BINARY, has been provided [{self.outputs.prediction}]' ) - if not is_optional_float(self.outputs.prediction_proba.type): + if not is_none(self.outputs.prediction_proba) and not is_optional_float( + self.outputs.prediction_proba.type + ): raise ValueError( f'prediction_proba must be an optional float for a ModelType.BINARY, has been provided [{self.outputs.prediction_proba}]' ) @@ -109,7 +111,9 @@ def validate_outputs(self) -> Self: raise ValueError( f'prediction must be a number or string for a ModelType.MULTI_CLASS, has been provided [{self.outputs.prediction}]' ) - if not is_optional_float(self.outputs.prediction_proba.type): + if not is_none(self.outputs.prediction_proba) and not is_optional_float( + self.outputs.prediction_proba.type + ): raise ValueError( f'prediction_proba must be an optional float for a ModelType.MULTI_CLASS, has been provided [{self.outputs.prediction_proba}]' ) @@ -119,7 +123,7 @@ def validate_outputs(self) -> Self: raise ValueError( f'prediction must be a number for a ModelType.REGRESSION, has been provided [{self.outputs.prediction}]' ) - if not is_none(self.outputs.prediction_proba.type): + if not is_none(self.outputs.prediction_proba) and not is_none(self.outputs.prediction_proba.type): raise ValueError( f'prediction_proba must be None for a ModelType.REGRESSION, has been provided [{self.outputs.prediction_proba}]' ) From e19826cd182f3dbe8c5dfde515bc2cfe2904d25c Mon Sep 17 00:00:00 2001 From: lorenzodagostinoradicalbit Date: Mon, 1 Jul 2024 17:10:53 +0200 Subject: [PATCH 2/3] fix: using matched value to populate ValueError text --- api/app/models/model_dto.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/api/app/models/model_dto.py b/api/app/models/model_dto.py index abe9eef1..61ff6052 100644 --- a/api/app/models/model_dto.py +++ b/api/app/models/model_dto.py @@ -72,19 +72,19 @@ def validate_target(self) -> Self: case ModelType.BINARY: if not is_number(self.target.type): raise ValueError( - f'target must be a number for a ModelType.BINARY, has been provided [{self.target}]' + f'target must be a number for a ModelType.{checked_model_type}, has been provided [{self.target}]' ) return self case ModelType.MULTI_CLASS: if not is_number_or_string(self.target.type): raise ValueError( - f'target must be a number or string for a ModelType.MULTI_CLASS, has been provided [{self.target}]' + f'target must be a number or string for a ModelType.{checked_model_type}, has been provided [{self.target}]' ) return self case ModelType.REGRESSION: if not is_number(self.target.type): raise ValueError( - f'target must be a number for a ModelType.REGRESSION, has been provided [{self.target}]' + f'target must be a number for a ModelType.{checked_model_type}, has been provided [{self.target}]' ) return self case _: @@ -97,35 +97,37 @@ def validate_outputs(self) -> Self: case ModelType.BINARY: if not is_number(self.outputs.prediction.type): raise ValueError( - f'prediction must be a number for a ModelType.BINARY, has been provided [{self.outputs.prediction}]' + f'prediction must be a number for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction}]' ) if not is_none(self.outputs.prediction_proba) and not is_optional_float( self.outputs.prediction_proba.type ): raise ValueError( - f'prediction_proba must be an optional float for a ModelType.BINARY, has been provided [{self.outputs.prediction_proba}]' + f'prediction_proba must be an optional float for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction_proba}]' ) return self case ModelType.MULTI_CLASS: if not is_number_or_string(self.outputs.prediction.type): raise ValueError( - f'prediction must be a number or string for a ModelType.MULTI_CLASS, has been provided [{self.outputs.prediction}]' + f'prediction must be a number or string for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction}]' ) if not is_none(self.outputs.prediction_proba) and not is_optional_float( self.outputs.prediction_proba.type ): raise ValueError( - f'prediction_proba must be an optional float for a ModelType.MULTI_CLASS, has been provided [{self.outputs.prediction_proba}]' + f'prediction_proba must be an optional float for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction_proba}]' ) return self case ModelType.REGRESSION: if not is_number(self.outputs.prediction.type): raise ValueError( - f'prediction must be a number for a ModelType.REGRESSION, has been provided [{self.outputs.prediction}]' + f'prediction must be a number for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction}]' ) - if not is_none(self.outputs.prediction_proba) and not is_none(self.outputs.prediction_proba.type): + if not is_none(self.outputs.prediction_proba) and not is_none( + self.outputs.prediction_proba.type + ): raise ValueError( - f'prediction_proba must be None for a ModelType.REGRESSION, has been provided [{self.outputs.prediction_proba}]' + f'prediction_proba must be None for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction_proba}]' ) return self case _: From 558519da54a161aa49f63eef5c16b3ab12788931 Mon Sep 17 00:00:00 2001 From: lorenzodagostinoradicalbit Date: Mon, 1 Jul 2024 17:17:45 +0200 Subject: [PATCH 3/3] fix: removed ModelType in ValueError text --- api/app/models/model_dto.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/api/app/models/model_dto.py b/api/app/models/model_dto.py index 61ff6052..60a9e441 100644 --- a/api/app/models/model_dto.py +++ b/api/app/models/model_dto.py @@ -72,19 +72,19 @@ def validate_target(self) -> Self: case ModelType.BINARY: if not is_number(self.target.type): raise ValueError( - f'target must be a number for a ModelType.{checked_model_type}, has been provided [{self.target}]' + f'target must be a number for a {checked_model_type}, has been provided [{self.target}]' ) return self case ModelType.MULTI_CLASS: if not is_number_or_string(self.target.type): raise ValueError( - f'target must be a number or string for a ModelType.{checked_model_type}, has been provided [{self.target}]' + f'target must be a number or string for a {checked_model_type}, has been provided [{self.target}]' ) return self case ModelType.REGRESSION: if not is_number(self.target.type): raise ValueError( - f'target must be a number for a ModelType.{checked_model_type}, has been provided [{self.target}]' + f'target must be a number for a {checked_model_type}, has been provided [{self.target}]' ) return self case _: @@ -97,37 +97,37 @@ def validate_outputs(self) -> Self: case ModelType.BINARY: if not is_number(self.outputs.prediction.type): raise ValueError( - f'prediction must be a number for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction}]' + f'prediction must be a number for a {checked_model_type}, has been provided [{self.outputs.prediction}]' ) if not is_none(self.outputs.prediction_proba) and not is_optional_float( self.outputs.prediction_proba.type ): raise ValueError( - f'prediction_proba must be an optional float for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction_proba}]' + f'prediction_proba must be an optional float for a {checked_model_type}, has been provided [{self.outputs.prediction_proba}]' ) return self case ModelType.MULTI_CLASS: if not is_number_or_string(self.outputs.prediction.type): raise ValueError( - f'prediction must be a number or string for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction}]' + f'prediction must be a number or string for a {checked_model_type}, has been provided [{self.outputs.prediction}]' ) if not is_none(self.outputs.prediction_proba) and not is_optional_float( self.outputs.prediction_proba.type ): raise ValueError( - f'prediction_proba must be an optional float for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction_proba}]' + f'prediction_proba must be an optional float for a {checked_model_type}, has been provided [{self.outputs.prediction_proba}]' ) return self case ModelType.REGRESSION: if not is_number(self.outputs.prediction.type): raise ValueError( - f'prediction must be a number for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction}]' + f'prediction must be a number for a {checked_model_type}, has been provided [{self.outputs.prediction}]' ) if not is_none(self.outputs.prediction_proba) and not is_none( self.outputs.prediction_proba.type ): raise ValueError( - f'prediction_proba must be None for a ModelType.{checked_model_type}, has been provided [{self.outputs.prediction_proba}]' + f'prediction_proba must be None for a {checked_model_type}, has been provided [{self.outputs.prediction_proba}]' ) return self case _: