Skip to content
Merged
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
7 changes: 6 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -e ".[dev,all_models]"
python -m pip install -e ".[dev]"
python -m pip install xgboost
python -m pip install spacy
python -m pip install torch
python -m pip install statsmodels
Copy link
Contributor Author

@isabelizimm isabelizimm Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is effectively the same as python -m pip install -e ".[dev,all_models]", but Python 3.7 will not complete installation, ending in an OSError saying there is not enough space 😩 this is likely due to some pretty intense dependency resolution.

I'll likely want to rethink this strategy, but that can come in a separate PR.

python -m pip install typing_extensions==4.7.1
Copy link
Contributor Author

@isabelizimm isabelizimm Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pinned due to fastapi requirements. fastapi/fastapi#9808

- name: Run Tests
run: |
pytest -m 'not rsc_test and not docker' --cov --cov-report xml
Expand Down
17 changes: 14 additions & 3 deletions vetiver/prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _(data: pd.DataFrame):
>>> another_prototype.schema() == prototype.schema()
True
"""
dict_data = data.iloc[0, :].to_dict()
dict_data = _to_field(data.iloc[0, :].to_dict())
prototype = create_prototype(**dict_data)
return prototype

Expand Down Expand Up @@ -156,7 +156,11 @@ def _item(value):

dict_data = dict(enumerate(data[0], 0))
# pydantic requires strings as indicies
dict_data = {f"{key}": _item(value) for key, value in dict_data.items()}
# if its a numpy type, we have to take the Python type due to Pydantic

dict_data = {
f"{key}": (type(value.item()), _item(value)) for key, value in dict_data.items()
}
prototype = create_prototype(**dict_data)
return prototype

Expand All @@ -171,7 +175,7 @@ def _(data: dict):
data : dict
Dictionary
"""
return create_prototype(**data)
return create_prototype(**_to_field(data))


@vetiver_create_prototype.register
Expand All @@ -198,3 +202,10 @@ def _(data: NoneType):
None
"""
return None


def _to_field(data):
basemodel_input = dict()
for key, value in data.items():
basemodel_input[key] = (type(value), value)
return basemodel_input
14 changes: 11 additions & 3 deletions vetiver/tests/test_build_vetiver_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import pandas as pd
import pydantic
import pins
import numpy as np

np.random.seed(50)

# Load data, model
X_df, y = get_mock_data()
Expand All @@ -33,8 +36,9 @@ def test_vetiver_model_array_prototype():

assert vt1.model == model
assert issubclass(vt1.prototype, vt.Prototype)
# change to model_construct for pydantic v3
assert isinstance(vt1.prototype.construct(), pydantic.BaseModel)
assert list(vt1.prototype.__fields__.values())[0].type_ == int
assert vt1.prototype.construct().__dict__ == {"0": 96, "1": 11, "2": 33}


def test_vetiver_model_df_prototype():
Expand All @@ -48,8 +52,9 @@ def test_vetiver_model_df_prototype():
)

assert vt2.model == model
# change to model_construct for pydantic v3
assert isinstance(vt2.prototype.construct(), pydantic.BaseModel)
assert list(vt2.prototype.__fields__.values())[0].type_ == int
assert vt2.prototype.construct().B == 96


def test_vetiver_model_dict_prototype():
Expand All @@ -64,8 +69,9 @@ def test_vetiver_model_dict_prototype():
)

assert vt3.model == model
# change to model_construct for pydantic v3
assert isinstance(vt3.prototype.construct(), pydantic.BaseModel)
assert list(vt3.prototype.__fields__.values())[0].type_ == int
assert vt3.prototype.construct().B == 0


def test_vetiver_model_basemodel_prototype():
Expand Down Expand Up @@ -135,6 +141,7 @@ def test_vetiver_model_from_pin():

assert isinstance(v2, vt.VetiverModel)
assert isinstance(v2.model, sklearn.base.BaseEstimator)
# change to model_construct for pydantic v3
assert isinstance(v2.prototype.construct(), pydantic.BaseModel)
assert v2.metadata.user == {"test": 123}
assert v2.metadata.version is not None
Expand Down Expand Up @@ -170,6 +177,7 @@ def test_vetiver_model_from_pin_user_metadata():

assert isinstance(v2, vt.VetiverModel)
assert isinstance(v2.model, sklearn.base.BaseEstimator)
# change to model_construct for pydantic v3
assert isinstance(v2.prototype.construct(), pydantic.BaseModel)
assert v2.metadata.user == custom_meta
assert v2.metadata.version is not None
Expand Down
2 changes: 2 additions & 0 deletions vetiver/tests/test_custom_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test_custom_vetiver_model():
assert v.description == "A DummyRegressor model"
assert not v.metadata.required_pkgs
assert isinstance(v.model, sklearn.dummy.DummyRegressor)
# change to model_construct for pydantic v3
assert isinstance(v.prototype.construct(), pydantic.BaseModel)


Expand All @@ -58,4 +59,5 @@ def test_custom_vetiver_model_no_ptype():

assert v.description == "A regression model for testing purposes"
assert isinstance(v.model, sklearn.dummy.DummyRegressor)
# change to model_construct for pydantic v3
assert isinstance(v.prototype.construct(), pydantic.BaseModel)
8 changes: 3 additions & 5 deletions vetiver/tests/test_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,10 @@ def test_predict_sklearn_no_ptype(data, length, vetiver_client_check_ptype_false

@pytest.mark.parametrize("data", [(0), 0, 0.0, "0"])
def test_predict_sklearn_type_error(data, vetiver_client):
import re

msg = re.sub(
r"\n",
": ",
"1 validation error for Request\nbody\n value is not a valid list \(type=type_error.list\)", # noqa
msg = str(
"[{'type': 'list_type', 'loc': ('body',), 'msg': 'Input should be a valid list', \
'input': '0', 'url': 'https://errors.pydantic.dev/2.0.3/v/list_type'}]"
)

with pytest.raises(TypeError, match=msg):
Expand Down