|
| 1 | +import pytest |
| 2 | + |
| 3 | +spacy = pytest.importorskip("spacy", reason="spacy library not installed") |
| 4 | + |
| 5 | +import numpy as np # noqa |
| 6 | +import pandas as pd # noqa |
| 7 | +from fastapi.testclient import TestClient # noqa |
| 8 | +from numpy import nan # noqa |
| 9 | +import vetiver # noqa |
| 10 | + |
| 11 | + |
| 12 | +@spacy.language.Language.component("animals") |
| 13 | +def animal_component_function(doc): |
| 14 | + matches = matcher(doc) # noqa |
| 15 | + spans = [ |
| 16 | + spacy.tokens.Span(doc, start, end, label="ANIMAL") |
| 17 | + for match_id, start, end in matches |
| 18 | + ] |
| 19 | + doc.ents = spans |
| 20 | + return doc |
| 21 | + |
| 22 | + |
| 23 | +nlp = spacy.blank("en") |
| 24 | +animals = list(nlp.pipe(["dog", "cat", "turtle"])) |
| 25 | +matcher = spacy.matcher.PhraseMatcher(nlp.vocab) |
| 26 | +matcher.add("ANIMAL", animals) |
| 27 | +nlp.add_pipe("animals") |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def spacy_model(): |
| 32 | + return nlp |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture() |
| 36 | +def vetiver_client_with_prototype(spacy_model): # With check_prototype=True |
| 37 | + df = pd.DataFrame({"new_column": ["one", "two", "three"]}) |
| 38 | + v = vetiver.VetiverModel(spacy_model, "animals", prototype_data=df) |
| 39 | + app = vetiver.VetiverAPI(v, check_prototype=True) |
| 40 | + app.app.root_path = "/predict" |
| 41 | + client = TestClient(app.app) |
| 42 | + |
| 43 | + return client |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(scope="function") |
| 47 | +def vetiver_client_with_prototype_series(spacy_model): # With check_prototype=True |
| 48 | + df = pd.Series({"new_column": ["one", "two", "three"]}) |
| 49 | + v = vetiver.VetiverModel(spacy_model, "animals", prototype_data=df) |
| 50 | + app = vetiver.VetiverAPI(v, check_prototype=True) |
| 51 | + app.app.root_path = "/predict" |
| 52 | + client = TestClient(app.app) |
| 53 | + return client |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture |
| 57 | +def vetiver_client_no_prototype(spacy_model): # With check_prototype=False |
| 58 | + v = vetiver.VetiverModel(spacy_model, "animals") |
| 59 | + app = vetiver.VetiverAPI(v, check_prototype=False) |
| 60 | + app.app.root_path = "/predict" |
| 61 | + client = TestClient(app.app) |
| 62 | + |
| 63 | + return client |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("data", ["a", 1, [1, 2, 3]]) |
| 67 | +def test_bad_prototype_data(data, spacy_model): |
| 68 | + with pytest.raises(TypeError): |
| 69 | + vetiver.VetiverModel(spacy_model, "animals", prototype_data=data) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize( |
| 73 | + "data", |
| 74 | + [ |
| 75 | + {"col": ["1", "2"], "col2": [1, 2]}, |
| 76 | + pd.DataFrame({"col": ["1", "2"], "col2": [1, 2]}), |
| 77 | + ], |
| 78 | +) |
| 79 | +def test_bad_prototype_shape(data, spacy_model): |
| 80 | + with pytest.raises(ValueError): |
| 81 | + vetiver.VetiverModel(spacy_model, "animals", prototype_data=data) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize("data", [{"col": "1"}, pd.DataFrame({"col": ["1"]})]) |
| 85 | +def test_good_prototype_shape(data, spacy_model): |
| 86 | + v = vetiver.VetiverModel(spacy_model, "animals", prototype_data=data) |
| 87 | + |
| 88 | + assert v.prototype.construct().dict() == {"col": "1"} |
| 89 | + |
| 90 | + |
| 91 | +def test_vetiver_predict_with_prototype(vetiver_client_with_prototype): |
| 92 | + df = pd.DataFrame({"new_column": ["turtles", "i have a dog"]}) |
| 93 | + |
| 94 | + response = vetiver.predict(endpoint=vetiver_client_with_prototype, data=df) |
| 95 | + |
| 96 | + assert isinstance(response, pd.DataFrame), response |
| 97 | + assert response.to_dict() == { |
| 98 | + "0": { |
| 99 | + "text": "turtles", |
| 100 | + "ents": [], |
| 101 | + "sents": [{"start": 0, "end": 7}], |
| 102 | + "tokens": [{"id": 0, "start": 0, "end": 7}], |
| 103 | + }, |
| 104 | + "1": { |
| 105 | + "text": "i have a dog", |
| 106 | + "ents": [{"start": 9, "end": 12, "label": "ANIMAL"}], |
| 107 | + "sents": nan, |
| 108 | + "tokens": [ |
| 109 | + {"id": 0, "start": 0, "end": 1}, |
| 110 | + {"id": 1, "start": 2, "end": 6}, |
| 111 | + {"id": 2, "start": 7, "end": 8}, |
| 112 | + {"id": 3, "start": 9, "end": 12}, |
| 113 | + ], |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | +def test_vetiver_predict_no_prototype(vetiver_client_no_prototype): |
| 119 | + df = pd.DataFrame({"uhhh": ["turtles", "i have a dog"]}) |
| 120 | + |
| 121 | + response = vetiver.predict(endpoint=vetiver_client_no_prototype, data=df) |
| 122 | + |
| 123 | + assert isinstance(response, pd.DataFrame), response |
| 124 | + assert response.to_dict() == { |
| 125 | + "0": { |
| 126 | + "text": "turtles", |
| 127 | + "ents": [], |
| 128 | + "sents": [{"start": 0, "end": 7}], |
| 129 | + "tokens": [{"id": 0, "start": 0, "end": 7}], |
| 130 | + }, |
| 131 | + "1": { |
| 132 | + "text": "i have a dog", |
| 133 | + "ents": [{"start": 9, "end": 12, "label": "ANIMAL"}], |
| 134 | + "sents": nan, |
| 135 | + "tokens": [ |
| 136 | + {"id": 0, "start": 0, "end": 1}, |
| 137 | + {"id": 1, "start": 2, "end": 6}, |
| 138 | + {"id": 2, "start": 7, "end": 8}, |
| 139 | + {"id": 3, "start": 9, "end": 12}, |
| 140 | + ], |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | +def test_serialize_no_prototype(spacy_model): |
| 146 | + import pins |
| 147 | + |
| 148 | + board = pins.board_temp(allow_pickle_read=True) |
| 149 | + v = vetiver.VetiverModel(spacy_model, "animals") |
| 150 | + vetiver.vetiver_pin_write(board=board, model=v) |
| 151 | + v2 = vetiver.VetiverModel.from_pin(board, "animals") |
| 152 | + assert isinstance( |
| 153 | + v2.model, |
| 154 | + spacy.lang.en.English, |
| 155 | + ) |
| 156 | + |
| 157 | + |
| 158 | +def test_serialize_prototype(spacy_model): |
| 159 | + import pins |
| 160 | + |
| 161 | + board = pins.board_temp(allow_pickle_read=True) |
| 162 | + v = vetiver.VetiverModel( |
| 163 | + spacy_model, "animals", prototype_data=pd.DataFrame({"text": ["text"]}) |
| 164 | + ) |
| 165 | + vetiver.vetiver_pin_write(board=board, model=v) |
| 166 | + v2 = vetiver.VetiverModel.from_pin(board, "animals") |
| 167 | + assert isinstance( |
| 168 | + v2.model, |
| 169 | + spacy.lang.en.English, |
| 170 | + ) |
0 commit comments