-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path_data_handler.py
More file actions
335 lines (274 loc) · 13.6 KB
/
_data_handler.py
File metadata and controls
335 lines (274 loc) · 13.6 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
"""Data Handler file."""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING, cast
from datasets import concatenate_datasets
from autointent.configs import DataConfig
from autointent.custom_types import Split
from ._stratification import create_few_shot_split, split_dataset
if TYPE_CHECKING:
from collections.abc import Generator
from autointent import Dataset
from autointent.custom_types import FloatFromZeroToOne, ListOfGenericLabels, ListOfLabels
from autointent.schemas import Tag
logger = logging.getLogger(__name__)
class DataHandler:
"""Convenient wrapper for :py:class:`autointent.Dataset`.
Performs splitting of the wrapped dataset when instantiated.
"""
dataset: Dataset
"""Wrapped dataset."""
config: DataConfig
"""Configuration used for instantiation."""
def __init__(
self,
dataset: Dataset,
config: DataConfig | None = None,
random_seed: int | None = 0,
) -> None:
"""Initialize the data handler.
Args:
dataset: Training dataset.
config: Configuration object
random_seed: Seed for random number generation.
"""
self._seed = random_seed
self.dataset = dataset
self.config = config if config is not None else DataConfig()
self._n_classes = self.dataset.n_classes
if self.config.scheme == "ho":
self._split_ho(
self.config.separation_ratio,
self.config.validation_size,
self.config.is_few_shot_train,
self.config.examples_per_intent,
)
elif self.config.scheme == "cv":
self._split_cv(self.config.is_few_shot_train, self.config.examples_per_intent)
self._logger = logger
@property
def intent_descriptions(self) -> list[str | None]:
"""String descriptions for all intents."""
return [intent.description for intent in self.dataset.intents]
@property
def tags(self) -> list[Tag]:
"""Tags associated with intents.
Tagging is an experimental feature that is not guaranteed to work.
"""
return self.dataset.get_tags()
@property
def multilabel(self) -> bool:
"""Check if the dataset is multilabel."""
return self.dataset.multilabel
def _choose_split(self, split_name: str, idx: int | None = None) -> str:
if idx is not None:
split = f"{split_name}_{idx}"
if split not in self.dataset:
split = split_name
else:
split = split_name
return split
def train_utterances(self, idx: int | None = None) -> list[str]:
"""Retrieve training utterances from the dataset.
If a specific training split index is provided, retrieves utterances
from the indexed training split. Otherwise, retrieves utterances from
the primary training split.
Args:
idx: Optional index for a specific training split.
"""
split = self._choose_split(Split.TRAIN, idx)
return cast("list[str]", self.dataset[split][self.dataset.utterance_feature])
def train_labels(self, idx: int | None = None) -> ListOfGenericLabels:
"""Retrieve training labels from the dataset.
If a specific training split index is provided, retrieves labels
from the indexed training split. Otherwise, retrieves labels from
the primary training split.
Args:
idx: Optional index for a specific training split.
"""
split = self._choose_split(Split.TRAIN, idx)
return cast("ListOfGenericLabels", self.dataset[split][self.dataset.label_feature])
def train_labels_folded(self) -> list[ListOfGenericLabels]:
"""Retrieve train labels fold by fold."""
return [self.train_labels(j) for j in range(self.config.n_folds)]
def validation_utterances(self, idx: int | None = None) -> list[str]:
"""Retrieve validation utterances from the dataset.
If a specific validation split index is provided, retrieves utterances
from the indexed validation split. Otherwise, retrieves utterances from
the primary validation split.
Args:
idx: Optional index for a specific validation split.
"""
split = self._choose_split(Split.VALIDATION, idx)
return cast("list[str]", self.dataset[split][self.dataset.utterance_feature])
def validation_labels(self, idx: int | None = None) -> ListOfGenericLabels:
"""Retrieve validation labels from the dataset.
If a specific validation split index is provided, retrieves labels
from the indexed validation split. Otherwise, retrieves labels from
the primary validation split.
Args:
idx: Optional index for a specific validation split.
"""
split = self._choose_split(Split.VALIDATION, idx)
return cast("ListOfGenericLabels", self.dataset[split][self.dataset.label_feature])
def test_utterances(self) -> list[str] | None:
"""Retrieve test utterances from the dataset."""
if Split.TEST not in self.dataset:
return None
return cast("list[str]", self.dataset[Split.TEST][self.dataset.utterance_feature])
def test_labels(self) -> ListOfGenericLabels:
"""Retrieve test labels from the dataset."""
return cast("ListOfGenericLabels", self.dataset[Split.TEST][self.dataset.label_feature])
def validation_iterator(self) -> Generator[tuple[list[str], ListOfLabels, list[str], ListOfLabels]]:
"""Yield folds for cross-validation."""
if self.config.scheme != "cv":
msg = f"Cannot call cross-validation on {self.config.scheme} DataHandler"
raise RuntimeError(msg)
for j in range(self.config.n_folds):
val_utterances = self.train_utterances(j)
val_labels = self.train_labels(j)
train_folds = [i for i in range(self.config.n_folds) if i != j]
train_utterances = [ut for i_fold in train_folds for ut in self.train_utterances(i_fold)]
train_labels = [lab for i_fold in train_folds for lab in self.train_labels(i_fold)]
# filter out all OOS samples from train
train_utterances = [ut for ut, lab in zip(train_utterances, train_labels, strict=True) if lab is not None]
train_labels = [lab for lab in train_labels if lab is not None]
yield train_utterances, train_labels, val_utterances, val_labels # type: ignore[misc]
def _has_oos_samples(self, split_name: str) -> bool:
"""Return True if the given split contains OOS (label is None) samples."""
if split_name not in self.dataset:
return False
hf_split = self.dataset[split_name]
label_feature = self.dataset.label_feature
oos_samples = hf_split.filter(lambda sample: sample[label_feature] is None)
return len(oos_samples) > 0
def _duplicate_split_for_scoring_and_decision(self, split_name: str) -> None:
"""Duplicate split into _0/_1 where _0 is in-domain only.
Intended for hold-out mode when OOS is present but separation_ratio is not set:
- scoring uses `{split_name}_0` (no OOS)
- decision uses `{split_name}_1` (full, may include OOS)
"""
if split_name not in self.dataset:
return
hf_split = self.dataset[split_name]
label_feature = self.dataset.label_feature
in_domain = hf_split.filter(lambda sample: sample[label_feature] is not None)
if len(in_domain) == 0:
msg = f"Split '{split_name}' contains only OOS samples; cannot prepare scoring split."
raise ValueError(msg)
self.dataset[f"{split_name}_0"] = in_domain
self.dataset[f"{split_name}_1"] = hf_split
self.dataset.pop(split_name)
def _split_ho(
self,
separation_ratio: FloatFromZeroToOne | None,
validation_size: FloatFromZeroToOne,
is_few_shot: bool,
examples_per_intent: int,
) -> None:
has_validation_split = any(split.startswith(Split.VALIDATION) for split in self.dataset)
if Split.TRAIN in self.dataset:
if separation_ratio is not None:
self._split_train(separation_ratio)
elif self._has_oos_samples(Split.TRAIN):
# When OOS exists and separation_ratio is not set, keep the same in-domain pool
# for scoring and decision, but exclude OOS from scoring split.
self._duplicate_split_for_scoring_and_decision(Split.TRAIN)
# If user provided a single validation split containing OOS, make scoring validation OOS-free.
if Split.VALIDATION in self.dataset and self._has_oos_samples(Split.VALIDATION):
self._duplicate_split_for_scoring_and_decision(Split.VALIDATION)
if not has_validation_split:
self._split_validation_from_train(validation_size, is_few_shot, examples_per_intent)
elif is_few_shot:
self._split_few_shot(examples_per_intent)
for split in self.dataset:
n_classes_in_split = self.dataset.get_n_classes(split)
if n_classes_in_split != self._n_classes:
message = (
f"{n_classes_in_split=} for '{split=}' doesn't match initial number of classes ({self._n_classes})"
)
raise ValueError(message)
def _split_few_shot(self, examples_per_intent: int) -> None:
if Split.TRAIN in self.dataset:
self.dataset[Split.TRAIN], self.dataset[Split.VALIDATION] = create_few_shot_split(
self.dataset[Split.TRAIN],
self.dataset[Split.VALIDATION],
multilabel=self.dataset.multilabel,
label_column=self.dataset.label_feature,
random_seed=self._seed,
examples_per_label=examples_per_intent,
)
else:
for idx in range(2):
self.dataset[f"{Split.TRAIN}_{idx}"], self.dataset[f"{Split.VALIDATION}_{idx}"] = create_few_shot_split(
self.dataset[f"{Split.TRAIN}_{idx}"],
self.dataset[f"{Split.VALIDATION}_{idx}"],
multilabel=self.dataset.multilabel,
label_column=self.dataset.label_feature,
random_seed=self._seed,
examples_per_label=examples_per_intent,
)
def _split_train(self, ratio: FloatFromZeroToOne) -> None:
"""Split on two sets.
One is for scoring node optimizaton, one is for decision node.
Args:
ratio: Split ratio
"""
self.dataset[f"{Split.TRAIN}_0"], self.dataset[f"{Split.TRAIN}_1"] = split_dataset(
self.dataset,
split=Split.TRAIN,
test_size=ratio,
random_seed=self._seed,
allow_oos_in_train=False, # only train data for decision node should contain OOS
)
self.dataset.pop(Split.TRAIN)
def _split_cv(self, is_few_shot: bool, examples_per_intent: int) -> None:
extra_splits = [split_name for split_name in self.dataset if split_name != Split.TEST]
self.dataset[Split.TRAIN] = concatenate_datasets([self.dataset.pop(split_name) for split_name in extra_splits])
for j in range(self.config.n_folds - 1):
self.dataset[Split.TRAIN], self.dataset[f"{Split.TRAIN}_{j}"] = split_dataset(
self.dataset,
split=Split.TRAIN,
test_size=1 / (self.config.n_folds - j),
random_seed=self._seed,
is_few_shot=is_few_shot,
examples_per_intent=examples_per_intent,
allow_oos_in_train=True,
)
self.dataset[f"{Split.TRAIN}_{self.config.n_folds - 1}"] = self.dataset.pop(Split.TRAIN)
def _split_validation_from_train(self, size: float, is_few_shot: bool, examples_per_intent: int) -> None:
if Split.TRAIN in self.dataset:
self.dataset[Split.TRAIN], self.dataset[Split.VALIDATION] = split_dataset(
self.dataset,
split=Split.TRAIN,
test_size=size,
random_seed=self._seed,
is_few_shot=is_few_shot,
examples_per_intent=examples_per_intent,
allow_oos_in_train=True,
)
else:
for idx in range(2):
self.dataset[f"{Split.TRAIN}_{idx}"], self.dataset[f"{Split.VALIDATION}_{idx}"] = split_dataset(
self.dataset,
split=f"{Split.TRAIN}_{idx}",
test_size=size,
random_seed=self._seed,
is_few_shot=is_few_shot,
examples_per_intent=examples_per_intent,
allow_oos_in_train=idx == 1, # for decision node it's ok to have oos in train
)
def prepare_for_refit(self) -> None:
"""Merge all training folds into one in order to retrain configured optimal pipeline on it."""
if self.config.scheme == "ho":
return
train_folds = [split_name for split_name in self.dataset if split_name.startswith(Split.TRAIN)]
self.dataset[Split.TRAIN] = concatenate_datasets([self.dataset.pop(name) for name in train_folds])
self.dataset[f"{Split.TRAIN}_0"], self.dataset[f"{Split.TRAIN}_1"] = split_dataset(
self.dataset,
split=Split.TRAIN,
test_size=self.config.separation_ratio or 0.5,
random_seed=self._seed,
allow_oos_in_train=False,
)
self.dataset.pop(Split.TRAIN)