70 lines
2.8 KiB
Python
70 lines
2.8 KiB
Python
from typing import Callable
|
|
import collections
|
|
import dataclasses
|
|
import statistics
|
|
|
|
from metric_bench.places import Place
|
|
from metric_bench.python_units import Unit
|
|
|
|
METRICS: dict[str, Callable[[Unit, dict[str, int]], int]] = {
|
|
"cognitive complexity": lambda unit, spread: unit.cognitive,
|
|
"length": lambda unit, spread: unit.length,
|
|
"shared state": lambda unit, spread: unit.shared_state,
|
|
"change spread": lambda unit, spread: spread.get(unit.path, 0),
|
|
}
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Score:
|
|
auc: float
|
|
top_k: float
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class RunScores:
|
|
units: int
|
|
named: int
|
|
unmeasured: int
|
|
scores: dict[str, Score]
|
|
|
|
|
|
def co_changed(log: str) -> dict[str, int]:
|
|
"""For each file, how many other files changed in a commit with it; log is `git log --format=format:@@ --name-only`."""
|
|
partners: dict[str, set[str]] = collections.defaultdict(set)
|
|
for commit in log.split("@@"):
|
|
files = set(commit.split())
|
|
for path in files:
|
|
partners[path] |= files - {path}
|
|
return {path: len(others) for path, others in partners.items()}
|
|
|
|
|
|
def bench(units: list[Unit], places: list[Place], spread: dict[str, int]) -> RunScores | None:
|
|
"""How well each metric ranks the units the seats named above the rest; None when no named place is a measured unit."""
|
|
names = {(place.path, place.name) for place in places}
|
|
named = [unit for unit in units if (unit.path, unit.name) in names]
|
|
if not named or len(named) == len(units):
|
|
return None
|
|
rest = [unit for unit in units if (unit.path, unit.name) not in names]
|
|
measured = {(unit.path, unit.name) for unit in named}
|
|
unmeasured = len({place for place in places if (place.path, place.name) not in measured})
|
|
scores = {}
|
|
for metric, value in METRICS.items():
|
|
scores[metric] = Score(auc([value(unit, spread) for unit in named], [value(unit, spread) for unit in rest]), top_k(units, named, lambda unit: value(unit, spread)))
|
|
return RunScores(len(units), len(named), unmeasured, scores)
|
|
|
|
|
|
def auc(named: list[int], rest: list[int]) -> float:
|
|
"""The chance a named unit outranks one not named, a tie counting half: 0.5 is a coin toss, 1.0 a perfect ranking."""
|
|
wins = sum((a > b) + 0.5 * (a == b) for a in named for b in rest)
|
|
return wins / (len(named) * len(rest))
|
|
|
|
|
|
def top_k(units: list[Unit], named: list[Unit], value: Callable[[Unit], int]) -> float:
|
|
"""The share of the metric's top k units the seats named, k being how many they named; a tie at the cut counts in order of the units."""
|
|
top = sorted(units, key=value, reverse=True)[:len(named)]
|
|
return sum(unit in named for unit in top) / len(named)
|
|
|
|
|
|
def medians(runs: list[RunScores]) -> dict[str, Score]:
|
|
return {metric: Score(statistics.median(run.scores[metric].auc for run in runs), statistics.median(run.scores[metric].top_k for run in runs)) for metric in METRICS}
|