Skip to content

Serialization

feature_to_oml_dict(feat)

Source code in src/features/serialization.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def feature_to_oml_dict(feat: Feature) -> dict:
    result: dict[str, object] = {
        "oml:index": str(feat.index),
        "oml:name": feat.name,
        "oml:data_type": feat.data_type,
    }

    if feat.nominal_values:
        result["oml:nominal_value"] = feat.nominal_values

    for attr in OML_BOOL_FIELDS:
        v = getattr(feat, attr)
        if v is not None:
            result[f"oml:{attr}"] = str(v).lower()

    for attr in (*OML_INT_FIELDS, *OML_FLOAT_FIELDS):
        v = getattr(feat, attr)
        if v is not None:
            result[f"oml:{attr}"] = str(v)

    if feat.class_distribution:
        result["oml:class_distribution"] = feat.class_distribution

    return result

features_to_oml_dict(data_feature)

Source code in src/features/serialization.py
38
39
40
41
42
43
44
def features_to_oml_dict(data_feature: DataFeature) -> dict:
    return {
        "oml:data_features": {
            "@xmlns:oml": "http://openml.org/openml",
            "oml:feature": [feature_to_oml_dict(f) for f in data_feature.features],
        }
    }

features_to_xml(data_feature, *, pretty=True)

Source code in src/features/serialization.py
47
48
49
50
51
52
53
54
55
def features_to_xml(
    data_feature: DataFeature,
    *,
    pretty: bool = True,
) -> str:
    return xmltodict.unparse(
        features_to_oml_dict(data_feature),
        pretty=pretty,
    )

parse_features_xml(xml_str)

Source code in src/features/serialization.py
58
59
60
61
62
def parse_features_xml(xml_str: str) -> dict:
    return xmltodict.parse(
        xml_str,
        force_list=("oml:feature", "oml:nominal_value"),
    )