-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocessors.py
298 lines (245 loc) · 9.28 KB
/
processors.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""Processors read data from files."""
import json
import logging
import dataclasses
from dataclasses import dataclass
from collections import Counter
from typing import List, Optional, Dict
import pandas as pd
from transformers import DataProcessor # type: ignore
logger = logging.getLogger(__name__)
@dataclass
class MBExample:
"""A single training/test example for multilabel sequence classification.
Args:
guid: Unique id for the example.
text: string. The untokenized text of the sequence.
labels: (Optional) List[string]. The labels of the example.
label_list: (Optional) List[string]. The list of possible labels.
"""
guid: str
text: str
labels: Optional[List[str]] = None
label_list: Optional[List[str]] = None
split: Optional[str] = None
def to_json_string(self):
"""Serialize this instance to a JSON string."""
return json.dumps(dataclasses.asdict(self))
@dataclass
class SoftExample:
"""A single training/test example for multilabel sequence classification.
Args:
guid: Unique id for the example.
text: string. The untokenized text of the sequence.
labels: (Optional) List[float]. The labels of the example.
label_list: (Optional) List[string]. The corresponding list of labels.
"""
guid: str
text: str
scores: Dict[str, float]
split: Optional[str] = None
def to_json_string(self):
"""Serialize this instance to a JSON string."""
return json.dumps(dataclasses.asdict(self))
class MultiLabelTSVProcessor(DataProcessor):
"""Processor for Multilabel classification datasets stored as a TSV file.
Args:
data_file: string. The TSV file containing data.
text_col: The column containing the text.
ignore_cols: List[string]. A list of columns to ignore.
index_col: string. The column of the file containing the id strings.
If None, uses the row as index.
random_state: int. The random state to use for the split.
"""
label_list: List[str]
def __init__(
self,
data_file,
text_col: str = "text",
lang: Optional[str] = None,
lang_col: str = "lang",
split: Optional[str] = None,
split_col: Optional[str] = None,
random_state: int = 1984,
print_counts: bool = False,
):
"""See class."""
super(MultiLabelTSVProcessor, self).__init__()
self.data_file = data_file
self.text_col = text_col
self.random_state = random_state
self.lang = lang
self.lang_col = lang_col
self.split_col = split_col
self.split = split
self.print_counts = print_counts
self.not_labels = [self.text_col, self.lang_col, self.split_col]
self.not_labels = [c for c in self.not_labels if c is not None]
self.not_labels += ["tid", "id", "split", "og_split", "text", "lang"]
def _df_has_neutral(self, df):
"""Check that the dataframe has a neutral examples."""
label_list = self._get_labels_in_data(df)
df = df.astype({c: int for c in label_list})
df = df[label_list].sum(axis=1)
if any(df == 0):
print(f"Found {sum(df==0)} neutral examples")
return True
return False
def _get_labels_in_data(self, df):
"""Get the labels present in the dataframe."""
label_list = df.columns.tolist()
label_list = [
lbl
for lbl in label_list
if lbl not in self.not_labels
]
return label_list
def _read_tsv(self) -> List[MBExample]:
"""Read data file."""
logger.info(
f"Reading from file={self.data_file}"
)
counter = Counter()
df = pd.read_csv(
self.data_file,
sep="\t", # type: ignore
) # type: ignore
cols = df.columns.tolist() # type: ignore
cols = [c for c in cols if c not in self.not_labels]
# filter by language
if self.lang is not None:
logger.info(f"Filtering by language: {self.lang}")
df = df[df[self.lang_col] == self.lang]
ids = df.index.tolist() # type: ignore
# get the labels present in the dataset file
label_list = self._get_labels_in_data(df)
if self._df_has_neutral(df):
self.neutral = True
self.label_list = label_list
# logger.info(f"{self.__class__.__name__} Label list: {label_list}")
# switch the data format to a list of dictionaries
df = df.to_dict("records") # type: ignore
# convert to a list of MultilabelExample
examples = []
for guid, row in zip(ids, df):
if guid is None:
raise ValueError("No found")
guid = str(guid)
keys = [k for k in row.keys() if k not in self.not_labels]
labels: List[str] = [str(k) for k in keys if row[k] == 1]
# count labels
count_labels = [k for k in labels]
# in case no label is present, this is a neutral example
if not count_labels:
count_labels = ["neutral"]
counter.update(count_labels)
split = row[self.split_col] if self.split_col else None
# mask
labels_in_example = [k for k in label_list if row[k] >= 0]
# create the MBExample
ex = MBExample(
guid=guid,
text=row[self.text_col],
labels=labels,
label_list=labels_in_example,
split=split,
)
# add it to the list
examples.append(ex)
logger.info(f"Read: {len(examples)} examples")
if self.print_counts:
for k in sorted(counter.keys()):
logger.info(f"\t{k}:\t{counter[k]}")
return examples
def get_examples(self) -> List[MBExample]:
"""See base class."""
return self._read_tsv()
def get_labels(self) -> List[str]:
"""Return the list of labels."""
return self.label_list
class SoftMultiLabelTSVProcessor(DataProcessor):
"""Processor for Multilabel classification datasets stored as a TSV file.
Labels are expected to be soft labels (i.e. probabilities)
Args:
data_file: string. The TSV file containing data.
text_col: The column containing the text.
use_cols: List[string]. A list of columns to use.
index_col: string. The column of the file containing the id strings.
If None, uses the row as index.
"""
labels: List[str]
def __init__(
self,
data_file,
text_col: str = "text",
index_col: str = "tid",
split_col: Optional[str] = None,
use_cols: Optional[List[str]] = None,
):
"""See class."""
super(SoftMultiLabelTSVProcessor, self).__init__()
self.data_file = data_file
self.text_col = text_col
self.split_col = split_col
self.use_cols = use_cols
self.index_col = index_col
if self.use_cols and split_col:
self.use_cols = [
self.index_col,
self.text_col,
split_col,
] + self.use_cols
elif self.use_cols and not split_col:
self.use_cols = [self.index_col, self.text_col] + self.use_cols
def get_labels_in_df(self, df):
"""Get the labels present in the dataframe."""
label_list = df.columns.tolist()
label_list = [
lbl
for lbl in label_list
if lbl != self.text_col
and lbl != self.index_col
and lbl != self.split_col
]
self.labels = sorted(label_list)
return self.labels
def _read_tsv(self) -> List[SoftExample]:
"""Read data file."""
logger.info(
f"Reading from file={self.data_file} index_col={self.index_col} use_cols={self.use_cols}"
)
counter = Counter()
df = pd.read_csv(
self.data_file,
sep="\t", # type: ignore
index_col=self.index_col,
usecols=self.use_cols,
) # type: ignore
ids = df.index.tolist() # type: ignore
# get the labels present in the dataset file
label_list = self.get_labels_in_df(df)
logger.info(f"Processor: Labels in File: {label_list}")
# switch the data format to a list of dictionaries
df = df.to_dict("records") # type: ignore
# convert to a list of MultilabelExample
examples = []
for guid, row in zip(ids, df):
if guid is None:
raise ValueError("Missing id in row")
guid = str(guid)
scores = {k: row[k] for k in label_list}
# create the MBExample
split = row[self.split_col] if self.split_col else None
ex = SoftExample(
guid=guid, text=row[self.text_col], scores=scores, split=split,
)
# add it to the list
examples.append(ex)
logger.info(f"Read: {counter}")
return examples
def get_examples(self) -> List[SoftExample]:
"""See base class."""
return self._read_tsv()
def get_labels(self) -> List[str]:
"""Return the list of labels."""
return self.labels