-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from matching
[WIP] Hooks on matching pairs
- Loading branch information
Showing
19 changed files
with
1,268 additions
and
1,061 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,23 +2,23 @@ | |
line-length = 120 | ||
src = ["src", "tests"] | ||
|
||
select = [ | ||
lint.select = [ | ||
"E", # pycodestyle | ||
"F", # pyflakes | ||
"UP", # pyupgrade | ||
"D", # pydocstyle | ||
] | ||
ignore = [ | ||
lint.ignore = [ | ||
"D102", # Missing docstring in public method | ||
"D107", # Missing docstring in `__init__` | ||
] | ||
|
||
fixable = ["ALL"] | ||
lint.fixable = ["ALL"] | ||
|
||
[tool.ruff.pydocstyle] | ||
[tool.ruff.lint.pydocstyle] | ||
convention = "google" | ||
|
||
[tool.ruff.isort] | ||
[tool.ruff.lint.isort] | ||
lines-after-imports = 2 | ||
|
||
[tool.black] | ||
|
@@ -33,7 +33,7 @@ testpaths = ["tests"] | |
|
||
[tool.poetry] | ||
name = "metametric" | ||
version = "0.1.2" | ||
version = "0.2.0" | ||
description = "A Unified View of Evaluation Metrics for Structured Prediction" | ||
authors = ["Tongfei Chen <[email protected]>", "Yunmo Chen <[email protected]>", "Will Gantt <[email protected]>"] | ||
readme = "README.md" | ||
|
@@ -42,26 +42,29 @@ repository = "https://github.com/wanmok/metametric" | |
packages = [{ include = "metametric", from = "src" }] | ||
|
||
[tool.poetry.dependencies] | ||
python = ">=3.9" | ||
scipy = "^1.11.2" | ||
python = ">=3.10" | ||
scipy = "^1.14.0" | ||
numpy = "^1.25.2" | ||
torchmetrics = { version = "^1.1.2", optional = true } | ||
|
||
[tool.poetry.group.dev] | ||
optional = true | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
pytest = "^7.4.2" | ||
pyright = "^1.1.327" | ||
ruff = "^0.0.290" | ||
pyright = "^1.1.385" | ||
networkx = "^3.1" | ||
mkdocs = "^1.5.3" | ||
mkdocs-material = "^9.4.2" | ||
mkdocstrings = {extras = ["python"], version = "^0.23.0"} | ||
mkdocstrings = {extras = ["python"], version = "^0.26.2"} | ||
ruff = "^0.7.0" | ||
griffe = "^1.5.1" | ||
|
||
[tool.poetry.extras] | ||
torchmetrics = ["torchmetrics"] | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.ruff.lint.per-file-ignores] | ||
"tests/*" = ["D100", "D103", "F841"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from abc import abstractmethod | ||
from typing import Sequence, TypeVar, Generic, Tuple, Collection | ||
|
||
import numpy as np | ||
import scipy.optimize as spo | ||
|
||
from metametric.core.constraint import MatchingConstraint | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class MatchingProblem(Generic[T]): | ||
"""A matching between two collections of objects.""" | ||
|
||
def __init__(self, x: Sequence[T], y: Sequence[T], gram_matrix: np.ndarray): | ||
self.x = x | ||
self.y = y | ||
self.gram_matrix = gram_matrix | ||
|
||
@abstractmethod | ||
def solve(self) -> Tuple[float, Collection[Tuple[int, int, float]]]: | ||
"""Solves the matching problem.""" | ||
raise NotImplementedError | ||
|
||
|
||
class AssignmentProblem(MatchingProblem[T]): | ||
def __init__(self, x: Sequence[T], y: Sequence[T], gram_matrix: np.ndarray, constraint: MatchingConstraint): | ||
super().__init__(x, y, gram_matrix) | ||
self.constraint = constraint | ||
|
||
def solve(self) -> Tuple[float, Collection[Tuple[int, int, float]]]: | ||
m = self.gram_matrix | ||
if self.constraint == MatchingConstraint.ONE_TO_ONE: | ||
row_idx, col_idx = spo.linear_sum_assignment( | ||
cost_matrix=m, | ||
maximize=True, | ||
) | ||
total = m[row_idx, col_idx].sum() | ||
matching = [(i.item(), j.item(), m[i, j].item()) for i, j in zip(row_idx, col_idx)] | ||
return total, matching | ||
if self.constraint == MatchingConstraint.ONE_TO_MANY: | ||
total = m.max(axis=0).sum().item() | ||
selected_x = m.argmax(axis=0) | ||
matching = [(selected_x[j].item(), j, m[selected_x[j], j].item()) for j in range(m.shape[1])] | ||
return total, matching | ||
if self.constraint == MatchingConstraint.MANY_TO_ONE: | ||
total = m.max(axis=1).sum().item() | ||
selected_y = m.argmax(axis=1) | ||
matching = [(i, selected_y[i].item(), m[i, selected_y[i]].item()) for i in range(m.shape[0])] | ||
return total, matching | ||
if self.constraint == MatchingConstraint.MANY_TO_MANY: | ||
total = m.sum().item() | ||
matching = [(i, j, m[i, j].item()) for i in range(m.shape[0]) for j in range(m.shape[1])] | ||
return total, matching | ||
raise ValueError(f"Invalid constraint: {self.constraint}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.