/
test_log_for_text_classification.py
144 lines (112 loc) · 4.16 KB
/
test_log_for_text_classification.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
import pytest
import rubrix as rb
from rubrix.client.sdk.commons.errors import BadRequestApiError, ValidationApiError
from rubrix.server.apis.v0.settings.server import settings
def test_log_records_with_multi_and_single_label_task(mocked_client):
dataset = "test_log_records_with_multi_and_single_label_task"
expected_inputs = ["This is a text"]
rb.delete(dataset)
records = [
rb.TextClassificationRecord(
id=0,
inputs=expected_inputs,
multi_label=False,
),
rb.TextClassificationRecord(
id=1,
inputs=expected_inputs,
multi_label=True,
),
]
with pytest.raises(ValidationApiError):
rb.log(
records,
name=dataset,
)
rb.log(records[0], name=dataset)
with pytest.raises(Exception):
rb.log(records[1], name=dataset)
def test_delete_and_create_for_different_task(mocked_client):
dataset = "test_delete_and_create_for_different_task"
text = "This is a text"
rb.delete(dataset)
rb.log(rb.TextClassificationRecord(id=0, inputs=text), name=dataset)
rb.load(dataset)
rb.delete(dataset)
rb.log(
rb.TokenClassificationRecord(id=0, text=text, tokens=text.split(" ")),
name=dataset,
)
rb.load(dataset)
def test_search_keywords(mocked_client):
dataset = "test_search_keywords"
from datasets import load_dataset
dataset_ds = load_dataset("Recognai/sentiment-banking", split="train")
dataset_rb = rb.read_datasets(dataset_ds, task="TextClassification")
rb.delete(dataset)
rb.log(name=dataset, records=dataset_rb)
ds = rb.load(dataset, query="lim*")
df = ds.to_pandas()
assert not df.empty
assert "search_keywords" in df.columns
top_keywords = set(
[
keyword
for keywords in df.search_keywords.value_counts(sort=True, ascending=False)
.index[:3]
.tolist()
for keyword in keywords
]
)
assert top_keywords == {"limits", "limited", "limit"}, top_keywords
def test_log_records_with_empty_metadata_list(mocked_client):
dataset = "test_log_records_with_empty_metadata_list"
rb.delete(dataset)
expected_records = [
rb.TextClassificationRecord(text="The input text", metadata={"emptyList": []}),
rb.TextClassificationRecord(text="The input text", metadata={"emptyTuple": ()}),
rb.TextClassificationRecord(text="The input text", metadata={"emptyDict": {}}),
rb.TextClassificationRecord(text="The input text", metadata={"none": None}),
]
rb.log(expected_records, name=dataset)
df = rb.load(dataset)
df = df.to_pandas()
assert len(df) == len(expected_records)
for meta in df.metadata.values.tolist():
assert meta == {}
def test_logging_with_metadata_limits_exceeded(mocked_client):
dataset = "test_logging_with_metadata_limits_exceeded"
rb.delete(dataset)
expected_record = rb.TextClassificationRecord(
text="The input text",
metadata={k: k for k in range(0, settings.metadata_fields_limit + 1)},
)
with pytest.raises(BadRequestApiError):
rb.log(expected_record, name=dataset)
expected_record.metadata = {k: k for k in range(0, settings.metadata_fields_limit)}
rb.log(expected_record, name=dataset)
expected_record.metadata["new_key"] = "value"
with pytest.raises(BadRequestApiError):
rb.log(expected_record, name=dataset)
def test_log_with_other_task(mocked_client):
dataset = "test_log_with_other_task"
rb.delete(dataset)
record = rb.TextClassificationRecord(
text="The input text",
)
rb.log(record, name=dataset)
with pytest.raises(BadRequestApiError):
rb.log(
rb.TokenClassificationRecord(text="The text", tokens=["The", "text"]),
name=dataset,
)
def test_dynamics_metadata(mocked_client):
dataset = "test_dynamics_metadata"
rb.log(
rb.TextClassificationRecord(text="This is a text", metadata={"a": "value"}),
name=dataset,
)
rb.log(
rb.TextClassificationRecord(text="Another text", metadata={"b": "value"}),
name=dataset,
)