-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add statistics for multiclassification + refactoring and improv…
…ements (#35) * fix: refactoring for statistics * feat: statistics multiclass and refactoring * fix: refactoring * refactor: cleaned code * feat: improved spark docker image * fix: readme * feat: add preload image * fix: regenerate poetry.lock * fix: change action
- Loading branch information
Showing
23 changed files
with
901 additions
and
855 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
from models.current_dataset import CurrentDataset | ||
from models.reference_dataset import ReferenceDataset | ||
import pyspark.sql.functions as F | ||
|
||
N_VARIABLES = "n_variables" | ||
N_OBSERVATION = "n_observations" | ||
MISSING_CELLS = "missing_cells" | ||
MISSING_CELLS_PERC = "missing_cells_perc" | ||
DUPLICATE_ROWS = "duplicate_rows" | ||
DUPLICATE_ROWS_PERC = "duplicate_rows_perc" | ||
NUMERIC = "numeric" | ||
CATEGORICAL = "categorical" | ||
DATETIME = "datetime" | ||
|
||
|
||
# FIXME use pydantic struct like data quality | ||
def calculate_statistics_reference( | ||
reference_dataset: ReferenceDataset, | ||
) -> dict[str, float]: | ||
number_of_variables = len(reference_dataset.get_all_variables()) | ||
number_of_observations = reference_dataset.reference_count | ||
number_of_numerical = len(reference_dataset.get_numerical_variables()) | ||
number_of_categorical = len(reference_dataset.get_categorical_variables()) | ||
number_of_datetime = len(reference_dataset.get_datetime_variables()) | ||
reference_columns = reference_dataset.reference.columns | ||
|
||
stats = ( | ||
reference_dataset.reference.select( | ||
[ | ||
F.count(F.when(F.isnan(c) | F.col(c).isNull(), c)).alias(c) | ||
if t not in ("datetime", "date", "timestamp", "bool", "boolean") | ||
else F.count(F.when(F.col(c).isNull(), c)).alias(c) | ||
for c, t in reference_dataset.reference.dtypes | ||
] | ||
) | ||
.withColumn(MISSING_CELLS, sum([F.col(c) for c in reference_columns])) | ||
.withColumn( | ||
MISSING_CELLS_PERC, | ||
(F.col(MISSING_CELLS) / (number_of_variables * number_of_observations)) | ||
* 100, | ||
) | ||
.withColumn( | ||
DUPLICATE_ROWS, | ||
F.lit( | ||
number_of_observations | ||
- reference_dataset.reference.dropDuplicates( | ||
[ | ||
c | ||
for c in reference_columns | ||
if c != reference_dataset.model.timestamp.name | ||
] | ||
).count() | ||
), | ||
) | ||
.withColumn( | ||
DUPLICATE_ROWS_PERC, | ||
(F.col(DUPLICATE_ROWS) / number_of_observations) * 100, | ||
) | ||
.withColumn(N_VARIABLES, F.lit(number_of_variables)) | ||
.withColumn(N_OBSERVATION, F.lit(number_of_observations)) | ||
.withColumn(NUMERIC, F.lit(number_of_numerical)) | ||
.withColumn(CATEGORICAL, F.lit(number_of_categorical)) | ||
.withColumn(DATETIME, F.lit(number_of_datetime)) | ||
.select( | ||
*[ | ||
MISSING_CELLS, | ||
MISSING_CELLS_PERC, | ||
DUPLICATE_ROWS, | ||
DUPLICATE_ROWS_PERC, | ||
N_VARIABLES, | ||
N_OBSERVATION, | ||
NUMERIC, | ||
CATEGORICAL, | ||
DATETIME, | ||
] | ||
) | ||
.toPandas() | ||
.to_dict(orient="records")[0] | ||
) | ||
|
||
return stats | ||
|
||
|
||
def calculate_statistics_current( | ||
current_dataset: CurrentDataset, | ||
) -> dict[str, float]: | ||
number_of_variables = len(current_dataset.get_all_variables()) | ||
number_of_observations = current_dataset.current_count | ||
number_of_numerical = len(current_dataset.get_numerical_variables()) | ||
number_of_categorical = len(current_dataset.get_categorical_variables()) | ||
number_of_datetime = len(current_dataset.get_datetime_variables()) | ||
reference_columns = current_dataset.current.columns | ||
|
||
stats = ( | ||
current_dataset.current.select( | ||
[ | ||
F.count(F.when(F.isnan(c) | F.col(c).isNull(), c)).alias(c) | ||
if t not in ("datetime", "date", "timestamp", "bool", "boolean") | ||
else F.count(F.when(F.col(c).isNull(), c)).alias(c) | ||
for c, t in current_dataset.current.dtypes | ||
] | ||
) | ||
.withColumn(MISSING_CELLS, sum([F.col(c) for c in reference_columns])) | ||
.withColumn( | ||
MISSING_CELLS_PERC, | ||
(F.col(MISSING_CELLS) / (number_of_variables * number_of_observations)) | ||
* 100, | ||
) | ||
.withColumn( | ||
DUPLICATE_ROWS, | ||
F.lit( | ||
number_of_observations | ||
- current_dataset.current.dropDuplicates( | ||
[ | ||
c | ||
for c in reference_columns | ||
if c != current_dataset.model.timestamp.name | ||
] | ||
).count() | ||
), | ||
) | ||
.withColumn( | ||
DUPLICATE_ROWS_PERC, | ||
(F.col(DUPLICATE_ROWS) / number_of_observations) * 100, | ||
) | ||
.withColumn(N_VARIABLES, F.lit(number_of_variables)) | ||
.withColumn(N_OBSERVATION, F.lit(number_of_observations)) | ||
.withColumn(NUMERIC, F.lit(number_of_numerical)) | ||
.withColumn(CATEGORICAL, F.lit(number_of_categorical)) | ||
.withColumn(DATETIME, F.lit(number_of_datetime)) | ||
.select( | ||
*[ | ||
MISSING_CELLS, | ||
MISSING_CELLS_PERC, | ||
DUPLICATE_ROWS, | ||
DUPLICATE_ROWS_PERC, | ||
N_VARIABLES, | ||
N_OBSERVATION, | ||
NUMERIC, | ||
CATEGORICAL, | ||
DATETIME, | ||
] | ||
) | ||
.toPandas() | ||
.to_dict(orient="records")[0] | ||
) | ||
|
||
return stats |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from typing import List | ||
|
||
from pyspark.sql import DataFrame | ||
from pyspark.sql.types import DoubleType, StructField, StructType | ||
|
||
from utils.models import ModelOut, ModelType, ColumnDefinition | ||
from utils.spark import apply_schema_to_dataframe | ||
|
||
|
||
class CurrentDataset: | ||
def __init__(self, model: ModelOut, raw_dataframe: DataFrame): | ||
current_schema = self.spark_schema(model) | ||
current_dataset = apply_schema_to_dataframe(raw_dataframe, current_schema) | ||
|
||
self.model = model | ||
self.current = current_dataset.select( | ||
*[c for c in current_schema.names if c in current_dataset.columns] | ||
) | ||
self.current_count = self.current.count() | ||
|
||
# FIXME this must exclude target when we will have separate current and ground truth | ||
@staticmethod | ||
def spark_schema(model: ModelOut): | ||
all_features = ( | ||
model.features + [model.target] + [model.timestamp] + model.outputs.output | ||
) | ||
if model.outputs.prediction_proba and model.model_type == ModelType.BINARY: | ||
enforce_float = [ | ||
model.target.name, | ||
model.outputs.prediction.name, | ||
model.outputs.prediction_proba.name, | ||
] | ||
elif model.model_type == ModelType.BINARY: | ||
enforce_float = [model.target.name, model.outputs.prediction.name] | ||
else: | ||
enforce_float = [] | ||
return StructType( | ||
[ | ||
StructField( | ||
name=feature.name, | ||
dataType=model.convert_types(feature.type), | ||
nullable=False, | ||
) | ||
if feature.name not in enforce_float | ||
else StructField( | ||
name=feature.name, | ||
dataType=DoubleType(), | ||
nullable=False, | ||
) | ||
for feature in all_features | ||
] | ||
) | ||
|
||
def get_numerical_features(self) -> List[ColumnDefinition]: | ||
return [feature for feature in self.model.features if feature.is_numerical()] | ||
|
||
def get_categorical_features(self) -> List[ColumnDefinition]: | ||
return [feature for feature in self.model.features if feature.is_categorical()] | ||
|
||
# FIXME this must exclude target when we will have separate current and ground truth | ||
def get_numerical_variables(self) -> List[ColumnDefinition]: | ||
all_features = ( | ||
self.model.features | ||
+ [self.model.target] | ||
+ [self.model.timestamp] | ||
+ self.model.outputs.output | ||
) | ||
return [feature for feature in all_features if feature.is_numerical()] | ||
|
||
# FIXME this must exclude target when we will have separate current and ground truth | ||
def get_categorical_variables(self) -> List[ColumnDefinition]: | ||
all_features = ( | ||
self.model.features | ||
+ [self.model.target] | ||
+ [self.model.timestamp] | ||
+ self.model.outputs.output | ||
) | ||
return [feature for feature in all_features if feature.is_categorical()] | ||
|
||
# FIXME this must exclude target when we will have separate current and ground truth | ||
def get_datetime_variables(self) -> List[ColumnDefinition]: | ||
all_features = ( | ||
self.model.features | ||
+ [self.model.target] | ||
+ [self.model.timestamp] | ||
+ self.model.outputs.output | ||
) | ||
return [feature for feature in all_features if feature.is_datetime()] | ||
|
||
# FIXME this must exclude target when we will have separate current and ground truth | ||
def get_all_variables(self) -> List[ColumnDefinition]: | ||
return ( | ||
self.model.features | ||
+ [self.model.target] | ||
+ [self.model.timestamp] | ||
+ self.model.outputs.output | ||
) |
Oops, something went wrong.