in src/dfcx_scrapi/tools/metrics.py [0:0]
def safe_check(
reference: Union[str, float],
prediction: Union[str, float]) -> Union[Tuple[str, str], float]:
"""
Checks if either reference or prediction is np.nan or an empty string.
Args:
reference: Can be a string or np.nan.
prediction: Can be a string or np.nan.
Returns:
np.nan if either input is np.nan or an empty string,
otherwise returns a tuple of (reference, prediction) with
non-string inputs converted to strings.
"""
if (isinstance(reference, float) and np.isnan(reference)) or \
(isinstance(prediction, float) and np.isnan(prediction)) or \
(isinstance(reference, str) and not reference) or \
(isinstance(prediction, str) and not prediction):
return np.nan
else:
return str(reference), str(prediction)