espnet3.contract.metrics.check_metric_contract
Less than 1 minute
espnet3.contract.metrics.check_metric_contract
espnet3.contract.metrics.check_metric_contract(metric: Any) → None
Raise unless metric declares a valid inputs/outputs contract.
BaseMetric.__init__ already calls this on construction, but a subclass that overrides __init__ without calling super().__init__() skips that - so measure() calls this again on every metric it instantiates, before trusting its declaration for anything. There is no undeclared fallback either way.
- Parameters:metric – A
BaseMetricinstance (or any object exposinginputs/outputsthe same way). - Returns: None.
- Raises:TypeError – Neither
inputsnoroutputsis declared, or the declaration is malformed (seecheck_declaration,check_metric_declaration).
Examples
>>> from espnet3.components.metrics.base_metric import BaseMetric
>>> class Undeclared(BaseMetric):
... def __init__(self):
... pass # declares nothing, and skips super().__init__()
... def update(self, **values):
... pass
... def compute(self):
... return {}
>>> metric = Undeclared() # construction alone does not catch it
>>> check_metric_contract(metric)
Traceback (most recent call last):
TypeError: Undeclared does not declare inputs/outputs; declare ...