skbase.validate.check_sequence_named_objects#

skbase.validate.check_sequence_named_objects(seq_to_check: Sequence[Tuple[str, BaseObject]] | Dict[str, BaseObject], allow_dict: bool = True, require_unique_names=False, object_type: type | Tuple[type] | None = None, sequence_name: str | None = None) Sequence[Tuple[str, BaseObject]] | Dict[str, BaseObject][source]#
skbase.validate.check_sequence_named_objects(seq_to_check: Sequence[Tuple[str, BaseObject]], allow_dict: bool, require_unique_names=False, object_type: type | Tuple[type] | None = None, sequence_name: str | None = None) Sequence[Tuple[str, BaseObject]]
skbase.validate.check_sequence_named_objects(seq_to_check: Sequence[Tuple[str, BaseObject]] | Dict[str, BaseObject], allow_dict: bool = True, require_unique_names=False, object_type: type | Tuple[type] | None = None, sequence_name: str | None = None) Sequence[Tuple[str, BaseObject]] | Dict[str, BaseObject]

Check if input is a sequence of named BaseObject instances.

seq_to_check is returned unchanged when it follows the allowed named BaseObject convention. The allowed format includes a sequence of (str, BaseObject instance) tuples. A dictionary with string names as keys and BaseObject instances as values is also allowed if allow_dict is True.

Parameters:
seq_to_checkSequence((str, BaseObject)) or Dict[str, BaseObject]

The input to check for conformance with the named object interface. Conforming input are:

  • Sequence that contains (str, BaseObject instance) tuples

  • Dictionary with string names as keys and BaseObject instances as values if allow_dict=True

allow_dictbool, default=True

Whether a dictionary of named objects is allowed as conforming named object type.

  • If True, then a dictionary with string keys and BaseObject instances is allowed format for providing a sequence of named objects.

  • If False, then only sequences that contain (str, BaseObject instance) tuples are considered conforming with the named object parameter API.

require_unique_namesbool, default=False

Whether names used in the sequence of named BaseObject instances must be unique.

  • If True and the names are not unique, then False is always returned.

  • If False, then whether or not the function returns True or False depends on whether seq_to_check follows sequence of named BaseObject format.

object_typeclass or tuple[class], default=None

The class type(s) that is used to ensure that all elements of named objects match the expected type.

sequence_namestr, default=None

Optional name used to refer to the input seq_to_check when raising any errors. Ignored raise_error=False.

Returns:
Sequence((str, BaseObject)) or Dict[str, BaseObject]

The seq_to_check is returned if it is a conforming named object type.

  • If allow_dict=True then return type is Sequence((str, BaseObject)) or Dict[str, BaseObject]

  • If allow_dict=False then return type is Sequence((str, BaseObject))

Raises:
ValueError

If seq_to_check does not conform to the named BaseObject API.

See also

is_named_object_tuple

Indicate (True/False) if input follows the named object API format for a single named object (e.g., tuple[str, expected class type]).

is_sequence_named_objects

Indicate (True/False) if an input sequence follows the named object API.

Examples

>>> from skbase.base import BaseObject, BaseEstimator
>>> from skbase.validate import check_sequence_named_objects
>>> named_objects = [("Step 1", BaseObject()), ("Step 2", BaseObject())]
>>> check_sequence_named_objects(named_objects)
[('Step 1', BaseObject()), ('Step 2', BaseObject())]

Dictionaries are optionally allowed as sequences of named BaseObjects

>>> named_objects = {"Step 1": BaseObject(), "Step 2": BaseObject()}
>>> check_sequence_named_objects(named_objects)
{'Step 1': BaseObject(), 'Step 2': BaseObject()}

Raises error since dictionaries are not allowed when allow_dict is False

>>> check_sequence_named_objects(named_objects, allow_dict=False) 

Raises error due to invalid format due to object names not being strings

>>> incorrectly_named_objects = [(1, BaseObject()), (2, BaseObject())]
>>> check_sequence_named_objects(incorrectly_named_objects)  

Raises error due to invalid format since named items are not BaseObject instances

>>> named_items = [("1", 7), ("2", 42)]
>>> check_sequence_named_objects(named_items)  

The validation can require the object elements to be a certain class type

>>> named_objects = [("Step 1", BaseObject()), ("Step 2", BaseObject())]
>>> check_sequence_named_objects(     named_objects, object_type=BaseEstimator) 
>>> named_objects = [("Step 1", BaseEstimator()), ("Step 2", BaseEstimator())]
>>> check_sequence_named_objects(named_objects, object_type=BaseEstimator)
[('Step 1', BaseEstimator()), ('Step 2', BaseEstimator())]