Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ActurialCapital committed Jul 2, 2024
1 parent cb11049 commit f11e978
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 17 deletions.
4 changes: 3 additions & 1 deletion blocks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,6 @@ def __post_init__(self):


class BaseDataLoader:
pass
@abstractmethod
def get(self, label: str) -> pd.DataFrame:
pass
2 changes: 2 additions & 0 deletions blocks/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ def __call__(self, X: pd.DataFrame, y=None) -> pd.DataFrame:


class Factor(TransformerMixin, MetaEstimatorMixin, BaseEstimator):
"""Work in progress"""

def __init__(self, template: BaseFactor):
self.template = template

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "python-blocks"
version = "0.1.7"
version = "0.1.8"
description = "Extra blocks for scikit-learn features."
authors = ["ActurialCapital"]
license = "BSD 3-Clause License"
Expand Down
61 changes: 61 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pytest
from dataclasses import dataclass, field
import blocks as bk


def test_base_sampler():

class MySampler(bk.BaseSampler):
pass

with pytest.raises(TypeError):
MySampler()


def test_base_transformer():

class MyTransformer(bk.BaseTransformer):
pass

with pytest.raises(TypeError):
MyTransformer()


def test_base_transformer_check_kwargs():

class MyTransformer(bk.BaseTransformer):
TRANSFORMERS = {'func1': 'a', 'func2': 'b'}

def __init__(self, select: str, **kwargs):
self.select = select
self.kwargs = kwargs

def __call__(cls):
pass

my_transformer = MyTransformer('func1')
assert my_transformer.check_kwargs("hello", "hello") == None
assert my_transformer.check_kwargs("func2", "b") == None
with pytest.raises(ValueError):
my_transformer.check_kwargs("func1", "a")

my_transformer = MyTransformer('func1', a='a')
assert my_transformer.check_kwargs("hello", "hello") == None
assert my_transformer.check_kwargs("func2", "b") == None
assert my_transformer.check_kwargs("func1", "a") == None


def test_base_factor():

@dataclass
class MyFactor(bk.BaseFactor):
tags: list = field(default_factory=lambda: [])
name: str = ""
X: str = ""
y: str = None
market_feature: str = ""
inputs: dict = field(default_factory=lambda: {})
outputs: dict = field(default_factory=lambda: {})
pipeline: tuple = field(default_factory=lambda: ())

assert MyFactor().__class__.__name__ == "MyFactor"
53 changes: 44 additions & 9 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import pandas as pd
from pandas.testing import assert_index_equal

from blocks.decorators import validate_select, register_feature_names, output_pandas_dataframe
from blocks.base import BaseTransformer
from sklearn.linear_model import LinearRegression

from blocks.decorators import (
validate_select,
register_feature_names,
output_pandas_dataframe
)

length = 50
n_paths = 10
Expand Down Expand Up @@ -34,35 +39,50 @@
columns=assets,
index=index,
)
df3 = pd.DataFrame(
np.random.normal(size=(length, n_paths)),
columns=assets,
index=index,
)


class MyClass(BaseTransformer):
class MyClass:
CHECK_SELECT = {'a': 'foo', 'b': 'bar'}

@validate_select(CHECK_SELECT)
def __init__(self, select: str):
self.select = select

@register_feature_names
def fit(self, X, y=None):
def fit(self, X, y):
self.model = LinearRegression().fit(X, y)
return self

@output_pandas_dataframe
def __call__(cls, X, y=None):
return X
def predict(self, X, y=None):
return self.model.predict(X)




def test_validate_select():
# Test valid selections
MyClass('a')
MyClass('b')
with pytest.raises(TypeError):
MyClass()
with pytest.raises(TypeError):
MyClass('hello')
with pytest.raises(TypeError):
MyClass(123)
with pytest.raises(TypeError):
MyClass(3.14)
with pytest.raises(TypeError):
MyClass(True)
with pytest.raises(TypeError):
MyClass(['a'])
with pytest.raises(TypeError):
MyClass({'a': 1})
with pytest.raises(TypeError):
MyClass(None)


Expand All @@ -76,8 +96,23 @@ def test_additional_valid_options():


def test_register_feature_names():
transformer = MyClass('a').fit(df1)
transformer = MyClass('a').fit(df1, df2)
assert_index_equal(transformer.columns_, df1.columns)

transformer = MyClass('a').fit(df2)
transformer = MyClass('a').fit(df2, df1)
assert_index_equal(transformer.columns_, df2.columns)

def test_output_pandas_dataframe():
arr = df3.to_numpy()
assert isinstance(arr, np.ndarray)

output = LinearRegression().fit(df1, df2).predict(df3)
assert isinstance(output, np.ndarray)

myclass = MyClass('a').fit(df1, df2)
pred = myclass.predict(df3)
assert isinstance(pred, pd.DataFrame)
assert_index_equal(myclass.columns_, df1.columns)



28 changes: 27 additions & 1 deletion tests/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import numpy as np
import pandas as pd
from datetime import datetime, timedelta
Expand Down Expand Up @@ -44,7 +46,9 @@

def test_vector_regression():
# Model based
pred = bk.VectorRegressor(LinearRegression).fit(X_train, y_train_).transform(y_test)
model = bk.VectorRegressor(LinearRegression)
model.fit(X_train, y_train_)
pred = model.transform(y_test)

# Iterating through assets (vector by vector)
predictions = []
Expand All @@ -58,6 +62,12 @@ def test_vector_regression():

# Assert
assert_frame_equal(pred, output)

# Test All NaNs
X_train_nans = pd.DataFrame(index=X_train.index, columns=X_train.columns)
with pytest.raises(ValueError):
model.fit(X_train_nans, y_train_)


def test_estimator_transformer():
model = LinearRegression()
Expand All @@ -71,4 +81,20 @@ def test_estimator_transformer():

# Assert
assert_frame_equal(pred, output)

# Test check_X_y
model = bk.EstimatorTransformer(LinearRegression(), check_input=True)
new_y_train = np.select([y_train > 0, y_train <= 0], [True, False], default=True)
model.fit(X_train, new_y_train)
output = model.transform(y_test)
assert isinstance(output, pd.DataFrame)

new_y_train = np.select([y_train > 0, y_train <= 0], ['foo', 'bar'], default='foo')
with pytest.raises(ValueError):
model.fit(X_train, new_y_train)


def test_factor():
pass


Loading

0 comments on commit f11e978

Please sign in to comment.