Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ __pycache__/
# C extensions
*.so

# Python notebooks
*.ipynb

# Distribution / packaging
.Python
build/
Expand Down
84 changes: 78 additions & 6 deletions src/skprometheus/preprocessing.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
from tkinter import Y
from functools import wraps

from sklearn import preprocessing
from skprometheus.metrics import MetricRegistry
from skprometheus.utils import get_feature_names


def feature_category_count(X, categories):

features = get_feature_names(X)

for idx, row in enumerate(categories.T):
for category in row:
if category is None:
category = "missing"

MetricRegistry.model_categorical(feature=str(features[idx]), category=str(category)).inc()


def label_count(labels):

for label in labels:
if label[0] is None:
label[0] = "missing"
MetricRegistry.label_categorical(Y=str(label[0])).inc()


class OneHotEncoder(preprocessing.OneHotEncoder):
"""
OneHotEncoder that adds metrics to the prometheus metric registry.
Expand All @@ -24,15 +45,66 @@ def transform(self, X):
metric registry.
"""
transformed_X = super().transform(X)
features = get_feature_names(X)

# Use inverse method on transformed_X to get all missing values back as 'None'
categories = self.inverse_transform(transformed_X)

for idx, row in enumerate(categories.T):
for category in row:
if not category:
category = "missing"
MetricRegistry.model_categorical(feature=str(features[idx]), category=str(category)).inc()
feature_category_count(X, categories)

return transformed_X


class OrdinalEncoder(preprocessing.OrdinalEncoder):
"""
OrdinalEncoder that adds metrics to the prometheus metric registry.
"""
@wraps(preprocessing.OrdinalEncoder.__init__, assigned=["__signature__"])
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
MetricRegistry.add_counter(
"model_categorical",
"Counts category occurrence for each categorical feature.",
additional_labels=("feature", "category"),
)

def transform(self, X):
"""
Transform method that adds the count for each category in each feature to the prometheus
metric registry.
"""
transformed_X = super().transform(X)

# Use inverse method on transformed_X to get all missing values back as 'None'
categories = self.inverse_transform(transformed_X)

feature_category_count(X, categories)

return transformed_X


class LabelEncoder(preprocessing.OrdinalEncoder):
"""
LabelEncoder that adds metrics to the prometheus metric registry.
"""
@wraps(preprocessing.LabelEncoder.__init__, assigned=["__signature__"])
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
MetricRegistry.add_counter(
"label_categorical",
"Counts category occurrence for each target label.",
additional_labels=tuple("Y"),
)

def transform(self, Y):
"""
Transform method that adds the count for each label to the prometheus
metric registry.
"""
transformed_Y = super().transform(Y)

# Use inverse method on transformed_X to get all missing values back as 'None'
labels = self.inverse_transform(transformed_Y)

label_count(labels)

return transformed_Y
2 changes: 1 addition & 1 deletion src/skprometheus/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ def get_feature_names(X):
if isinstance(X, pd.DataFrame):
return X.columns
else:
X = check_array(X, force_all_finite=False)
X = check_array(X, dtype=None, force_all_finite=False)
return list(range(X.shape[1]))
91 changes: 89 additions & 2 deletions tests/test_preprocessing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from skprometheus.preprocessing import OneHotEncoder
from skprometheus.preprocessing import OneHotEncoder, OrdinalEncoder, LabelEncoder
import numpy as np
from prometheus_client import REGISTRY
import pandas as pd
Expand All @@ -16,11 +16,25 @@
exclude=["check_fit2d_predict1d"],
)
)
def test_standard_checks(test_fn):

def test_standard_checks_OneHot(test_fn):
trf = OneHotEncoder()
test_fn(OneHotEncoder.__name__, trf)


@pytest.mark.parametrize(
"test_func",
select_tests(
flatten([general_checks, transformer_checks]),
exclude=["check_fit2d_predict1d"],
)
)

def test_standard_checks_Ordinal(test_func):
trf = OrdinalEncoder()
test_func(OrdinalEncoder.__name__, trf)


def test_OneHotEncoder():
one_hot = OneHotEncoder()
X = np.array([
Expand Down Expand Up @@ -77,3 +91,76 @@ def test_OneHotEncoder_missing():
one_hot.transform(X_test)

assert REGISTRY.get_sample_value('skprom_model_categorical_total', {'feature': '1', 'category': 'missing'}) == 2


def test_OrdinalEncoder():
ordinal = OrdinalEncoder(handle_unknown="use_encoded_value", unknown_value=np.nan)
x = np.array([['ndhbfg', 'akshf'],
['abhvg', 'likrghfb'],
['ndhbfg', 'lsbvjl']], dtype=np.str_)

ordinal.fit(x)
ordinal.transform(x)

assert 'skprom_model_categorical' in [m.name for m in REGISTRY.collect()]

assert REGISTRY.get_sample_value('skprom_model_categorical_total', {'feature': '0', 'category': 'ndhbfg'}) == 2
assert REGISTRY.get_sample_value('skprom_model_categorical_total', {'feature': '1', 'category': 'likrghfb'}) == 1


def test_OrdinalEncoder_pandas():
ordinal_pd = OrdinalEncoder(handle_unknown="use_encoded_value", unknown_value=np.nan)
x = np.array([['ndhbfg', 'akshf'],
['abhvg', 'likrghfb'],
['ndhbfg', 'lsbvjl']], dtype=np.str_)

df = pd.DataFrame.from_records(x, columns=['X', 'Y'])
ordinal_pd.fit(df)
ordinal_pd.transform(df)

assert REGISTRY.get_sample_value('skprom_model_categorical_total', {'feature': 'X', 'category': 'ndhbfg'}) == 2


def test_OrdinalEncoder_missing():
ordinal = OrdinalEncoder(handle_unknown="use_encoded_value", unknown_value=np.nan)
x = np.array([['ndhbfg', 'akshf'],
['abhvg', 'likrghfb'],
['ndhbfg', 'lsbvjl']], dtype=np.str_)

ordinal.fit(x)

x_test = np.array([['aaaa', 'bbbvbg'],
['abhvg', 'likrghfb'],
['ndhbfg', 'lsbvjl']], dtype=np.str_)

ordinal.transform(x_test)

assert REGISTRY.get_sample_value('skprom_model_categorical_total', {'feature': '0', 'category': 'missing'}) == 1
assert REGISTRY.get_sample_value('skprom_model_categorical_total', {'feature': '1', 'category': 'missing'}) == 1

def test_LabelEncoder():
label_enc = LabelEncoder()
Y = np.array(['A', 'B', 'C', 'B', 'E', 'D', 'E', 'E'], dtype = np.str_). reshape((-1, 1))

label_enc.fit(Y)
label_enc.transform(Y)

assert 'skprom_label_categorical' in [m.name for m in REGISTRY.collect()]

assert REGISTRY.get_sample_value('skprom_label_categorical_total', {'Y': 'A'}) == 1
assert REGISTRY.get_sample_value('skprom_label_categorical_total', {'Y': 'E'}) == 3


def test_LabelEncoder_missing():
label_enc = LabelEncoder(handle_unknown="use_encoded_value", unknown_value=np.nan)
Y = np.array(['A', 'B', 'C', 'B', 'E', 'D', 'E', 'E'], dtype = np.str_). reshape((-1, 1))

Y_test = np.array(['A', 'B', 'C', 'B', 'E', 'D', 'F', 'E'], dtype = np.str_). reshape((-1, 1))

label_enc.fit(Y)
label_enc.transform(Y_test)

assert 'skprom_label_categorical' in [m.name for m in REGISTRY.collect()]

assert REGISTRY.get_sample_value('skprom_label_categorical_total', {'Y': 'A'}) == 1
assert REGISTRY.get_sample_value('skprom_label_categorical_total', {'Y': 'missing'}) == 1