-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add statistics for multiclassification + refactoring and improvements #35
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7bce847
fix: refactoring for statistics
rivamarco 3021225
feat: statistics multiclass and refactoring
rivamarco a6b75fd
fix: refactoring
rivamarco 0124af6
refactor: cleaned code
rivamarco a97e9ea
feat: merge branch 'main' into feature/ROS-279-stats-multiclass
rivamarco 7cf6b71
feat: improved spark docker image
rivamarco 668b0a7
fix: readme
rivamarco b8561b7
feat: add preload image
rivamarco 3b805ee
fix: regenerate poetry.lock
rivamarco dc83dbc
fix: change action
rivamarco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How to use the local spark image |
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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be squashed probably in only one method in future refactoring |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will use pyproject and poetry lock from
spark
project instead of manually putting dependencies as it was before