|
5 | 5 | from autointent import Dataset |
6 | 6 | from autointent.configs import DataConfig |
7 | 7 | from autointent.context.data_handler import DataHandler |
| 8 | +from autointent.custom_types import Split |
8 | 9 | from autointent.schemas import Sample |
9 | 10 |
|
10 | 11 |
|
@@ -246,3 +247,66 @@ def test_few_shot_split(dataset): |
246 | 247 | assert Counter(dh.dataset[data_split][dh.dataset.label_feature]) == desired_specs[data_split], ( |
247 | 248 | f"Failed for {data_split}" |
248 | 249 | ) |
| 250 | + |
| 251 | + |
| 252 | +def _make_multiclass_mapping_with_oos(*, with_validation: bool) -> dict: |
| 253 | + # Ensure enough samples per class so stratified splitting doesn't fail. |
| 254 | + in_domain = [{"utterance": f"c0_{i}", "label": 0} for i in range(50)] + [ |
| 255 | + {"utterance": f"c1_{i}", "label": 1} for i in range(50) |
| 256 | + ] |
| 257 | + |
| 258 | + oos = [{"utterance": f"oos_{i}"} for i in range(20)] |
| 259 | + |
| 260 | + mapping: dict = { |
| 261 | + "train": [*in_domain, *oos], |
| 262 | + "intents": [{"id": 0}, {"id": 1}], |
| 263 | + } |
| 264 | + |
| 265 | + if with_validation: |
| 266 | + mapping["validation"] = [ |
| 267 | + {"utterance": "val_c0_0", "label": 0}, |
| 268 | + {"utterance": "val_c0_1", "label": 0}, |
| 269 | + {"utterance": "val_c1_0", "label": 1}, |
| 270 | + {"utterance": "val_c1_1", "label": 1}, |
| 271 | + {"utterance": "val_oos_0"}, |
| 272 | + {"utterance": "val_oos_1"}, |
| 273 | + ] |
| 274 | + |
| 275 | + return mapping |
| 276 | + |
| 277 | + |
| 278 | +def _split_has_oos_labels(dh: DataHandler, split_name: str) -> bool: |
| 279 | + return any(lab is None for lab in dh.dataset[split_name][dh.dataset.label_feature]) |
| 280 | + |
| 281 | + |
| 282 | +def test_ho_oos_without_separation_ratio_duplicates_and_filters_scoring_splits(): |
| 283 | + """If OOS exists and separation_ratio is None, scoring splits must be OOS-free.""" |
| 284 | + dataset = Dataset.from_dict(_make_multiclass_mapping_with_oos(with_validation=False)) |
| 285 | + dh = DataHandler(dataset, config=DataConfig(scheme="ho", separation_ratio=None), random_seed=42) |
| 286 | + |
| 287 | + assert "train_0" in dh.dataset |
| 288 | + assert "train_1" in dh.dataset |
| 289 | + assert "validation_0" in dh.dataset |
| 290 | + assert "validation_1" in dh.dataset |
| 291 | + assert Split.TRAIN not in dh.dataset |
| 292 | + assert Split.VALIDATION not in dh.dataset |
| 293 | + |
| 294 | + assert _split_has_oos_labels(dh, "train_0") is False |
| 295 | + assert _split_has_oos_labels(dh, "validation_0") is False |
| 296 | + assert _split_has_oos_labels(dh, "train_1") is True |
| 297 | + assert _split_has_oos_labels(dh, "validation_1") is True |
| 298 | + |
| 299 | + |
| 300 | +def test_ho_oos_with_user_validation_duplicates_validation_when_needed(): |
| 301 | + """If user provides validation with OOS, it should be duplicated and filtered for scoring.""" |
| 302 | + dataset = Dataset.from_dict(_make_multiclass_mapping_with_oos(with_validation=True)) |
| 303 | + dh = DataHandler(dataset, config=DataConfig(scheme="ho", separation_ratio=None), random_seed=42) |
| 304 | + |
| 305 | + assert "train_0" in dh.dataset |
| 306 | + assert "train_1" in dh.dataset |
| 307 | + assert "validation_0" in dh.dataset |
| 308 | + assert "validation_1" in dh.dataset |
| 309 | + assert Split.VALIDATION not in dh.dataset |
| 310 | + |
| 311 | + assert _split_has_oos_labels(dh, "validation_0") is False |
| 312 | + assert _split_has_oos_labels(dh, "validation_1") is True |
0 commit comments