Skip to content

Commit

Permalink
Metrics modules
Browse files Browse the repository at this point in the history
  • Loading branch information
manfreddiaz committed Mar 12, 2024
1 parent 6f8861e commit 4ff4108
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from distutils.core import setup


setup(
name='poprank',
version='0.1',
version='1.0.0',
description='Rating Mechanisms',
author='Manfred Diaz, Aurelien Buck-Kaeffer',
author_email='',
url='https://www.python.org/sigs/distutils-sig/',
package_dir={'': 'src'},
packages=['poprank'],
install_requires=[
'popcore @ git+ssh://git@github.com/poprl/popcore@v1.0.0-beta#egg=popcore',
]
)
38 changes: 38 additions & 0 deletions src/poprank/metrics/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from abc import ABC, abstractmethod
from typing import Any

import numpy as np


class Metric(ABC):

def __init__(self, name: str) -> None:
super().__init__()
self.name = name

@abstractmethod
def _compute(
self, x: np.ndarray, y: np.ndarray, *args, **kwargs
) -> float:
raise NotImplementedError()

@abstractmethod
def _max(self, n: int) -> int:
raise NotImplementedError()

def __call__(
self, x: np.ndarray, y: np.ndarray, *args: Any, **kwds: Any
) -> float:
return self._compute(x, y, *args, **kwds)

def max(self, n: int) -> int:
"""
Computes the upper bound value of the metric
on the space of n-ranked permutations.
:param n: number of alternatives.
:type n: int
:return: metric upper bound.
:rtype: int
"""
return self._max(n)
16 changes: 16 additions & 0 deletions src/poprank/metrics/hamming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import numpy as np
from ..functional.metrics import hamming
from ._base import Metric


class Hamming(Metric):
def __init__(self) -> None:
super().__init__("hamming")

def _compute(
self, x: np.ndarray, y: np.ndarray, *args, **kwargs
) -> float:
return hamming(x, y, *args, **kwargs)

def _max(self, n: int) -> int:
return n
18 changes: 18 additions & 0 deletions src/poprank/metrics/kendall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np

from ..functional.metrics import kendall
from ._base import Metric


class Kendall(Metric):

def __init__(self) -> None:
super().__init__("kendall")

def _compute(
self, x: np.ndarray, y: np.ndarray, *args, **kwargs
) -> float:
return kendall(x, y, *args, **kwargs)

def _max(self, n: int) -> int:
return n * (n - 1) / 2

0 comments on commit 4ff4108

Please sign in to comment.