Args: keys: keys of the corresponding items to be stack and execute ensemble. if only 1 key provided, suppose it's a PyTorch Tensor with data stacked on dimension `E`. output_key: the key to store ensemble result in the dictionary. ensembl
(
self,
keys: KeysCollection,
ensemble: Callable[[Sequence[NdarrayOrTensor] | NdarrayOrTensor], NdarrayOrTensor],
output_key: str | None = None,
allow_missing_keys: bool = False,
)
| 414 | backend = list(set(VoteEnsemble.backend) & set(MeanEnsemble.backend)) |
| 415 | |
| 416 | def __init__( |
| 417 | self, |
| 418 | keys: KeysCollection, |
| 419 | ensemble: Callable[[Sequence[NdarrayOrTensor] | NdarrayOrTensor], NdarrayOrTensor], |
| 420 | output_key: str | None = None, |
| 421 | allow_missing_keys: bool = False, |
| 422 | ) -> None: |
| 423 | """ |
| 424 | Args: |
| 425 | keys: keys of the corresponding items to be stack and execute ensemble. |
| 426 | if only 1 key provided, suppose it's a PyTorch Tensor with data stacked on dimension `E`. |
| 427 | output_key: the key to store ensemble result in the dictionary. |
| 428 | ensemble: callable method to execute ensemble on specified data. |
| 429 | if only 1 key provided in `keys`, `output_key` can be None and use `keys` as default. |
| 430 | allow_missing_keys: don't raise exception if key is missing. |
| 431 | |
| 432 | Raises: |
| 433 | TypeError: When ``ensemble`` is not ``callable``. |
| 434 | ValueError: When ``len(keys) > 1`` and ``output_key=None``. Incompatible values. |
| 435 | |
| 436 | """ |
| 437 | super().__init__(keys, allow_missing_keys) |
| 438 | if not callable(ensemble): |
| 439 | raise TypeError(f"ensemble must be callable but is {type(ensemble).__name__}.") |
| 440 | self.ensemble = ensemble |
| 441 | if len(self.keys) > 1 and output_key is None: |
| 442 | raise ValueError("Incompatible values: len(self.keys) > 1 and output_key=None.") |
| 443 | self.output_key = output_key if output_key is not None else self.keys[0] |
| 444 | |
| 445 | def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]: |
| 446 | d = dict(data) |