diff --git a/src/facebias/metrics.py b/src/facebias/metrics.py new file mode 100644 index 0000000..f0b5e9c --- /dev/null +++ b/src/facebias/metrics.py @@ -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)