Skip to content

declearn.data_info.NbFeaturesField

Bases: DataInfoField

Deprecated specifications for 'n_features' data_info field.

Source code in declearn/data_info/_fields.py
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
@register_data_info_field
class NbFeaturesField(DataInfoField):
    """Deprecated specifications for 'n_features' data_info field."""

    field = "n_features"
    types = (int,)
    doc = "Number of input features, checked to be equal."

    @classmethod
    def is_valid(
        cls,
        value: Any,
    ) -> bool:
        return isinstance(value, int) and (value > 0)

    @classmethod
    def combine(
        cls,
        *values: Any,
    ) -> int:
        # Warn about this class being deprecated.
        warnings.warn(
            "'NbFeaturesField has been deprecated as of declearn v2.2,"
            " and will be removed in v2.4 and/or v3.0."
            " Please use 'SingleInputShapeField' instead.",
            DeprecationWarning,
            stacklevel=3,
        )
        # Perform the values' combination.
        unique = list(set(values))
        if len(unique) != 1:
            raise ValueError(
                f"Cannot combine '{cls.field}': non-unique inputs."
            )
        if not cls.is_valid(unique[0]):
            raise ValueError(
                f"Cannot combine '{cls.field}': invalid unique value."
            )
        return unique[0]