Skip to content

Commit

Permalink
🎨 move test to njab, remove unused class
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry committed May 2, 2024
1 parent 7c4e031 commit cb34b6a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 48 deletions.
11 changes: 1 addition & 10 deletions tests/test_transfrom.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import numpy as np
import numpy.testing as npt
import pandas as pd
import pytest
import sklearn
from sklearn import impute, preprocessing

from vaep.io.datasets import to_tensor
from vaep.transform import StandardScaler, VaepPipeline


def test_StandardScaler():
X = pd.DataFrame(np.array([[2, None], [3, 2], [4, 6]]))
npt.assert_almost_equal(
preprocessing.StandardScaler().fit(X).transform(X),
StandardScaler().fit(X).transform(X).to_numpy()
)
from vaep.transform import VaepPipeline


def test_Vaep_Pipeline():
Expand Down
59 changes: 21 additions & 38 deletions vaep/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,7 @@
logger = logging.getLogger(__name__)


# ? Can this be a MixIn class?
class StandardScaler(preprocessing.StandardScaler):
def transform(self, X, copy=None):
res = super().transform(X, copy)
if isinstance(X, pd.DataFrame):
return pd.DataFrame(res, columns=X.columns, index=X.index)
return res

def inverse_transform(self, X, copy=None):
res = super().inverse_transform(X, copy)
if isinstance(X, pd.DataFrame):
return pd.DataFrame(res, columns=X.columns, index=X.index)
return res


msg_return_docstring = """
Returns
-------
Y: array-like
If X is a pandas DataFrame, Y will be a DataFrame with the initial
Indix and column Index objects.
"""

StandardScaler.transform.__doc__ = preprocessing.StandardScaler.transform.__doc__ + \
msg_return_docstring
StandardScaler.inverse_transform.__doc__ = preprocessing.StandardScaler.inverse_transform.__doc__ + msg_return_docstring

# # this could be a class method

# @make_pandas_compatible
# class MinMaxScaler(preprocessing.MinMaxScaler):
# pass

# # look at fastcore to see if **kwargs could be replaced with original
# # arguments, see https://fastcore.fast.ai/meta.html#Metaprogramming
# # decorate()

# ! general transform and inverse_transform needs to move somewhere else

def transform(self, X, **kwargs):
res = super(self.__class__, self).transform(X, **kwargs)
Expand All @@ -63,6 +26,16 @@ def inverse_transform(self, X, **kwargs):
return res


msg_return_docstring = """
Returns
-------
Y: array-like
If X is a pandas DataFrame, Y will be a DataFrame with the initial
Indix and column Index objects.
"""


def make_pandas_compatible(cls):
"""Patch transform and inverse_transform."""
# ? could become factory function, build args dictionary
Expand All @@ -77,6 +50,16 @@ def make_pandas_compatible(cls):
new_class.inverse_transform.__doc__ = cls.inverse_transform.__doc__ + msg_return_docstring
return new_class

# ? Can this be a MixIn class?
# # this could be a class method
# @make_pandas_compatible
# class MinMaxScaler(preprocessing.MinMaxScaler):
# pass

# # look at fastcore to see if **kwargs could be replaced with original
# # arguments, see https://fastcore.fast.ai/meta.html#Metaprogramming
# # decorate()


MinMaxScaler = make_pandas_compatible(preprocessing.MinMaxScaler)

Expand Down

0 comments on commit cb34b6a

Please sign in to comment.