Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding basic MetricSignature functionality #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion pose_evaluation/metrics/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,70 @@
# pylint: disable=undefined-variable
from tqdm import tqdm
from typing import Any, Callable

class Signature:
"""Represents reproducibility signatures for metrics. Inspired by sacreBLEU
"""
def __init__(self, name:str, args: dict):

self._abbreviated = {
"name":"n",
"higher_is_better":"hb"
}

self.signature_info = {
"name": name,
"higher_is_better": args.get("higher_is_better", None)
}

def update(self, key: str, value: Any):
self.signature_info[key] = value

def update_signature_and_abbr(self, key:str, abbr:str, args:dict):
self._abbreviated.update({
key: abbr
})

self.signature_info.update({
key: args.get(key, None)
})

def format(self, short: bool = False) -> str:
pairs = []
keys = list(self.signature_info.keys())
for name in keys:
value = self.signature_info[name]
if value is not None:
# Check for nested signature objects
if hasattr(value, "get_signature"):

# Wrap nested signatures in brackets
nested_signature = value.get_signature()
if isinstance(nested_signature, Signature):
nested_signature = nested_signature.format(short=short)
value = f"{{{nested_signature}}}"
if isinstance(value, bool):
# Replace True/False with yes/no
value = "yes" if value else "no"
if isinstance(value, Callable):
value = value.__name__
final_name = self._abbreviated[name] if short else name
pairs.append(f"{final_name}:{value}")

return "|".join(pairs)

def __str__(self):
return self.format()

def __repr__(self):
return self.format()

class BaseMetric[T]:
"""Base class for all metrics."""
# Each metric should define its Signature class' name here
_SIGNATURE_TYPE = Signature

def __init__(self, name: str, higher_is_better: bool = True):
def __init__(self, name: str, higher_is_better: bool = False):
cleong110 marked this conversation as resolved.
Show resolved Hide resolved
self.name = name
self.higher_is_better = higher_is_better

Expand Down Expand Up @@ -38,3 +97,7 @@ def score_all(self, hypotheses: list[T], references: list[T], progress_bar=True)

def __str__(self):
return self.name

def get_signature(self) -> Signature:
return self._SIGNATURE_TYPE(self.__dict__)
cleong110 marked this conversation as resolved.
Show resolved Hide resolved

Loading