MCPcopy Create free account
hub / github.com/PAIR-code/lit / validate_dataset

Function validate_dataset

lit_nlp/lib/validation.py:11–79  ·  view source on GitHub ↗

Validate dataset entries against spec. Args: ds: The Dataset being validated. enforce_all_fields_required: If `True`, require that every field in the Dataset referenced by `ds` has `required=True`. report_all: If `True`, log all errors before raising the first error encoun

(
    ds: dataset.Dataset,
    enforce_all_fields_required: bool = False,
    report_all: bool = False,
)

Source from the content-addressed store, hash-verified

9
10
11def validate_dataset(
12 ds: dataset.Dataset,
13 enforce_all_fields_required: bool = False,
14 report_all: bool = False,
15) -> None:
16 """Validate dataset entries against spec.
17
18 Args:
19 ds: The Dataset being validated.
20 enforce_all_fields_required: If `True`, require that every field in the
21 Dataset referenced by `ds` has `required=True`.
22 report_all: If `True`, log all errors before raising the first error
23 encountered in the validation of the Dataset referenced by `ds`.
24
25 Raises:
26 ValueError: The first instance of one of the following conditions occurring
27 during validation:
28 * A field in the Dataset's Spec has `required=False` when enforcing all
29 fields are required.
30 * The value for a field in an example is `None` when that field is
31 required (either explicitly or when enforcing all fields are required).
32 * The value for a field fails valdiation via `LitType.validate_input()`.
33 """
34 # If report_all is True, first_error stores the first error encountered during
35 # the validation process, which is then raised at the end of processing.
36 first_error: Optional[ValueError] = None
37 # If report_all is True, first_error_origin stores the ValueError raised by
38 # LitType.validate_input() if a datapoint fails validation.
39 first_error_origin: Optional[ValueError] = None
40
41 def raise_or_log_error(
42 msg: str, origin: Optional[ValueError] = None
43 ) -> ValueError:
44 """Raise (if report_all=False) or log (and return) a validation error."""
45 if report_all:
46 logging.error(termcolor.colored(msg, 'red'))
47 return ValueError(msg)
48 else:
49 raise ValueError(msg) from origin
50
51 for key, entry in ds.spec().items():
52 if enforce_all_fields_required and not entry.required:
53 err = raise_or_log_error(
54 f'Encountered a field, "{key}", that has required=False while'
55 ' enforcing that all fields in the Dataset.spec must be requred.'
56 )
57 first_error = first_error or err
58
59 for example in ds.examples:
60 value = example.get(key)
61 if value is None:
62 if enforce_all_fields_required or entry.required:
63 err = raise_or_log_error(
64 f'Required dataset feature "{key}" missing from datapoint.'
65 )
66 first_error = first_error or err
67 else:
68 try:

Callers

nothing calls this directly

Calls 4

raise_or_log_errorFunction · 0.85
getMethod · 0.80
specMethod · 0.45
validate_inputMethod · 0.45

Tested by

no test coverage detected