Adding the metrics file.

This commit is contained in:
2026-04-17 17:00:17 +01:00
parent 993ed2284d
commit eafd64f150

51
src/facebias/metrics.py Normal file
View File

@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
from typing import Any
from facebias.estimators import Capability
def find_common_capabilities(
gt: dict[str, dict[Capability, Any]],
preds: dict[str, dict[Capability, Any]]
) -> list[str]:
"""Iterates on `preds` and `gt`, finding common model capabilities.
Some models predict different features of face images. Some predict sex,
age and skin color, while others may predict only one of these features, or
others beyond them. This function finds the common capabilities, returning
them as a list.
Parameters
----------
gt: dict[str, dict[Capability, Any]]
Ground-truth data indexed by element ID, and values are a
feature -> prediction dictionary.
preds: dict[str, dict[Capability, Any]]
Predictions data in the same format as `gt`.
Returns
-------
common_keys: list[Capability]
The common features between `gt` and `preds`. If no common features are
found, returns an empty list.
"""
# Find the first common element between the `gt` and `preds`.
it = iter(gt)
common_elem = ""
while True:
try:
common_elem = next(it)
except StopIteration:
break
else:
if common_elem in preds:
break
if not common_elem:
return []
gt_keys = set(gt[common_elem].keys())
preds_keys = set(preds[common_elem].keys())
return list(gt_keys & preds_keys)