-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_nlp.py
183 lines (157 loc) · 5.44 KB
/
test_nlp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import uuid
import pandas as pd
import pytest
from mlflow.tracking import MlflowClient
import pycaret.datasets
import pycaret.nlp
@pytest.fixture(scope="module")
def kiva_dataframe():
# loading dataset
data = pycaret.datasets.get_data("kiva")
data = data.head(1000)
assert isinstance(data, pd.DataFrame)
return pycaret.datasets.get_data("kiva")
def test_nlp(kiva_dataframe):
data = kiva_dataframe.head(1000)
assert isinstance(data, pd.DataFrame)
# init setup
nlp1 = pycaret.nlp.setup(
data=data,
target="en",
log_experiment=True,
html=False,
session_id=123,
)
assert isinstance(nlp1, tuple)
assert isinstance(nlp1[0], list)
assert isinstance(nlp1[1], pd.DataFrame)
assert isinstance(nlp1[2], list)
assert isinstance(nlp1[4], int)
assert isinstance(nlp1[5], str)
assert isinstance(nlp1[6], list)
assert isinstance(nlp1[7], str)
assert isinstance(nlp1[8], bool)
assert isinstance(nlp1[9], bool)
# create model
lda = pycaret.nlp.create_model("lda")
# assign model
lda_results = pycaret.nlp.assign_model(lda)
assert isinstance(lda_results, pd.DataFrame)
# evaluate model
pycaret.nlp.evaluate_model(lda)
# save model
pycaret.nlp.save_model(lda, "lda_model_23122019")
# load model
pycaret.nlp.load_model("lda_model_23122019")
# returns table of models
all_models = pycaret.nlp.models()
assert isinstance(all_models, pd.DataFrame)
# get config
text = pycaret.nlp.get_config("text")
assert isinstance(text, list)
# set config
pycaret.nlp.set_config("seed", 124)
seed = pycaret.nlp.get_config("seed")
assert seed == 124
assert 1 == 1
class TestNLPExperimentCustomTags:
def test_nlp_setup_fails_with_experiment_custom_tags(self, kiva_dataframe):
with pytest.raises(TypeError):
# init setup
_ = pycaret.nlp.setup(
data=kiva_dataframe,
target="en",
log_experiment=True,
html=False,
session_id=123,
experiment_name=uuid.uuid4().hex,
experiment_custom_tags="custom_tag",
)
def test_nlp_create_model_fails_with_experiment_custom_tags(self, kiva_dataframe):
with pytest.raises(TypeError):
# init setup
_ = pycaret.nlp.setup(
data=kiva_dataframe,
target="en",
log_experiment=True,
html=False,
session_id=123,
experiment_name=uuid.uuid4().hex,
)
_ = pycaret.nlp.create_model(
"lda", experiment_custom_tags=("pytest", "testing")
)
@pytest.mark.parametrize("custom_tag", [1, ("pytest", "True"), True, 1000.0])
def test_nlp_setup_fails_with_experiment_custom_multiples_inputs(self, custom_tag):
with pytest.raises(TypeError):
# init setup
_ = pycaret.nlp.setup(
data=kiva_dataframe,
target="en",
log_experiment=True,
html=False,
session_id=123,
experiment_name=uuid.uuid4().hex,
experiment_custom_tags=custom_tag,
)
def test_nlp_setup_with_experiment_custom_tags(self, kiva_dataframe):
experiment_name = uuid.uuid4().hex
# init setup
_ = pycaret.nlp.setup(
data=kiva_dataframe,
target="en",
log_experiment=True,
html=False,
session_id=123,
experiment_name=experiment_name,
experiment_custom_tags={"pytest": "testing"},
)
# get experiment data
tracking_api = MlflowClient()
experiment = [
e for e in tracking_api.list_experiments() if e.name == experiment_name
][0]
experiment_id = experiment.experiment_id
# get run's info
experiment_run = tracking_api.list_run_infos(experiment_id)[0]
# get run id
run_id = experiment_run.run_id
# get run data
run_data = tracking_api.get_run(run_id)
# assert that custom tag was inserted
assert "testing" == run_data.to_dictionary().get("data").get("tags").get(
"pytest"
)
def test_nlp_create_models_with_experiment_custom_tags(self, kiva_dataframe):
experiment_name = uuid.uuid4().hex
# init setup
_ = pycaret.nlp.setup(
data=kiva_dataframe,
target="en",
log_experiment=True,
html=False,
session_id=123,
experiment_name=experiment_name,
)
_ = pycaret.nlp.create_model(
"lda", experiment_custom_tags={"pytest": "testing"}
)
# get experiment data
tracking_api = MlflowClient()
experiment = [
e for e in tracking_api.list_experiments() if e.name == experiment_name
][0]
experiment_id = experiment.experiment_id
# get run's info
experiment_run = tracking_api.list_run_infos(experiment_id)[0]
# get run id
run_id = experiment_run.run_id
# get run data
run_data = tracking_api.get_run(run_id)
# assert that custom tag was inserted
assert "testing" == run_data.to_dictionary().get("data").get("tags").get(
"pytest"
)
if __name__ == "__main__":
test_nlp()
TestNLPExperimentCustomTags()