Skip to content

declearn.data_info.DataInfoField

Abstract base class to implement 'data_info' fields specifications.

This class defines a small API to write specifications for expected metadata fields shared by clients in a federated learning process.

Subclasses are not meant to be instantiated, but act as a namespace, and must implement the following class attributes and class methods:

Attributes:

Name Type Description
field str

Name of the field that is being specified.

types tuple of types

Supported types for individual field values.

doc str

Short summary of the field's specification (accessible by using the get_data_info_fields_documentation function).

Methods

is_valid: any -> bool Check that a given value is valid for this field. If not overridden, run isinstance(value, cls.types). combine: *any -> any Combine multiple field values into a single one. This method needs extension or overridding. If super is called, run is_valid on each and every input.

Source code in declearn/data_info/_base.py
 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
class DataInfoField(metaclass=ABCMeta):
    """Abstract base class to implement 'data_info' fields specifications.

    This class defines a small API to write specifications for expected
    metadata fields shared by clients in a federated learning process.

    Subclasses are not meant to be instantiated, but act as a namespace,
    and must implement the following class attributes and class methods:

    Attributes
    ----------
    field: str
        Name of the field that is being specified.
    types: tuple of types
        Supported types for individual field values.
    doc: str
        Short summary of the field's specification (accessible by
        using the `get_data_info_fields_documentation` function).

    Methods
    -------
    is_valid: any -> bool
        Check that a given value is valid for this field.
        If not overridden, run isinstance(value, `cls.types`).
    combine: *any -> any
        Combine multiple field values into a single one.
        This method needs extension or overridding. If super
        is called, run `is_valid` on each and every input.
    """

    field: ClassVar[str] = NotImplemented
    types: ClassVar[Tuple[Type, ...]] = NotImplemented
    doc: ClassVar[str] = NotImplemented

    @classmethod
    def is_valid(
        cls,
        value: Any,
    ) -> bool:
        """Check that a given value may belong to this field."""
        # false-pos; pylint: disable=isinstance-second-argument-not-valid-type
        return isinstance(value, cls.types)

    @classmethod
    @abstractmethod
    def combine(
        cls,
        *values: Any,
    ) -> Any:
        """Combine multiple field values into a single one.

        Raise a ValueError if input values are invalid or incompatible.
        """
        if not all(cls.is_valid(val) for val in values):
            raise ValueError(
                f"Cannot combine '{cls.field}': invalid values encountered."
            )

combine(*values) abstractmethod classmethod

Combine multiple field values into a single one.

Raise a ValueError if input values are invalid or incompatible.

Source code in declearn/data_info/_base.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@classmethod
@abstractmethod
def combine(
    cls,
    *values: Any,
) -> Any:
    """Combine multiple field values into a single one.

    Raise a ValueError if input values are invalid or incompatible.
    """
    if not all(cls.is_valid(val) for val in values):
        raise ValueError(
            f"Cannot combine '{cls.field}': invalid values encountered."
        )

is_valid(value) classmethod

Check that a given value may belong to this field.

Source code in declearn/data_info/_base.py
 97
 98
 99
100
101
102
103
104
@classmethod
def is_valid(
    cls,
    value: Any,
) -> bool:
    """Check that a given value may belong to this field."""
    # false-pos; pylint: disable=isinstance-second-argument-not-valid-type
    return isinstance(value, cls.types)