Spaces:
Sleeping
Sleeping
HHH
Browse files- src/elo.py +47 -7
- src/galaxy_profiles.py +9 -0
src/elo.py
CHANGED
|
@@ -19,6 +19,14 @@ from src.config import (
|
|
| 19 |
HF_TOKEN,
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
STATE_DIR = Path("state")
|
|
@@ -45,24 +53,44 @@ class EloState:
|
|
| 45 |
self.dataset_id = dataset_id
|
| 46 |
|
| 47 |
def to_dict(self) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
rankings = sorted(
|
| 49 |
-
[{"galaxy_id":
|
| 50 |
key=lambda x: x["elo"],
|
| 51 |
reverse=True,
|
| 52 |
)
|
| 53 |
return {
|
| 54 |
-
"pool": self.pool,
|
| 55 |
-
"elo_ratings":
|
| 56 |
"total_comparisons": self.total_comparisons,
|
| 57 |
"dataset_id": self.dataset_id,
|
| 58 |
"rankings": rankings,
|
| 59 |
}
|
| 60 |
|
| 61 |
@classmethod
|
| 62 |
-
def from_dict(cls, d: dict) -> EloState:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
return cls(
|
| 64 |
-
pool=
|
| 65 |
-
elo_ratings=
|
| 66 |
total_comparisons=d.get("total_comparisons", 0),
|
| 67 |
dataset_id=d.get("dataset_id", ""),
|
| 68 |
)
|
|
@@ -145,8 +173,20 @@ def load_elo_state() -> bool:
|
|
| 145 |
logger.info("Saved state is old format β starting fresh")
|
| 146 |
return False
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
with _lock:
|
| 149 |
-
_state = EloState.from_dict(raw)
|
| 150 |
_init_scheduler()
|
| 151 |
_save_state()
|
| 152 |
logger.info("Restored ELO state: %d galaxies, %d comparisons",
|
|
|
|
| 19 |
HF_TOKEN,
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Imported lazily to avoid circular import at module load time
|
| 23 |
+
def _get_display_name(row_index: int) -> str:
|
| 24 |
+
try:
|
| 25 |
+
from src.galaxy_profiles import get_display_name
|
| 26 |
+
return get_display_name(row_index)
|
| 27 |
+
except Exception:
|
| 28 |
+
return str(row_index)
|
| 29 |
+
|
| 30 |
logger = logging.getLogger(__name__)
|
| 31 |
|
| 32 |
STATE_DIR = Path("state")
|
|
|
|
| 53 |
self.dataset_id = dataset_id
|
| 54 |
|
| 55 |
def to_dict(self) -> dict:
|
| 56 |
+
named_elo = {
|
| 57 |
+
_get_display_name(idx): self.elo_ratings.get(idx, DEFAULT_ELO)
|
| 58 |
+
for idx in self.pool
|
| 59 |
+
}
|
| 60 |
rankings = sorted(
|
| 61 |
+
[{"galaxy_id": gid, "elo": elo} for gid, elo in named_elo.items()],
|
| 62 |
key=lambda x: x["elo"],
|
| 63 |
reverse=True,
|
| 64 |
)
|
| 65 |
return {
|
| 66 |
+
"pool": [_get_display_name(idx) for idx in self.pool],
|
| 67 |
+
"elo_ratings": named_elo,
|
| 68 |
"total_comparisons": self.total_comparisons,
|
| 69 |
"dataset_id": self.dataset_id,
|
| 70 |
"rankings": rankings,
|
| 71 |
}
|
| 72 |
|
| 73 |
@classmethod
|
| 74 |
+
def from_dict(cls, d: dict, id_to_row: dict[str, int] | None = None) -> EloState:
|
| 75 |
+
"""Restore from a saved dict.
|
| 76 |
+
|
| 77 |
+
If *id_to_row* is provided (display-name β row-index map), pool entries
|
| 78 |
+
and elo_ratings keys are treated as display names and converted back to
|
| 79 |
+
row indices. Entries that have no mapping are silently dropped.
|
| 80 |
+
"""
|
| 81 |
+
if id_to_row is not None:
|
| 82 |
+
pool = [id_to_row[gid] for gid in d["pool"] if gid in id_to_row]
|
| 83 |
+
elo_ratings = {
|
| 84 |
+
id_to_row[gid]: v
|
| 85 |
+
for gid, v in d["elo_ratings"].items()
|
| 86 |
+
if gid in id_to_row
|
| 87 |
+
}
|
| 88 |
+
else:
|
| 89 |
+
pool = d["pool"]
|
| 90 |
+
elo_ratings = {int(k): v for k, v in d["elo_ratings"].items()}
|
| 91 |
return cls(
|
| 92 |
+
pool=pool,
|
| 93 |
+
elo_ratings=elo_ratings,
|
| 94 |
total_comparisons=d.get("total_comparisons", 0),
|
| 95 |
dataset_id=d.get("dataset_id", ""),
|
| 96 |
)
|
|
|
|
| 173 |
logger.info("Saved state is old format β starting fresh")
|
| 174 |
return False
|
| 175 |
|
| 176 |
+
# Build reverse map: display name β row index (requires metadata to be loaded first)
|
| 177 |
+
id_to_row: dict[str, int] | None = None
|
| 178 |
+
pool_sample = raw["pool"]
|
| 179 |
+
if pool_sample and isinstance(pool_sample[0], str):
|
| 180 |
+
# New format: pool contains display names β reverse-map via metadata cache
|
| 181 |
+
from src.galaxy_profiles import get_row_index_by_id
|
| 182 |
+
id_to_row = {}
|
| 183 |
+
for gid in raw["pool"]:
|
| 184 |
+
row = get_row_index_by_id(gid)
|
| 185 |
+
if row is not None:
|
| 186 |
+
id_to_row[gid] = row
|
| 187 |
+
|
| 188 |
with _lock:
|
| 189 |
+
_state = EloState.from_dict(raw, id_to_row=id_to_row)
|
| 190 |
_init_scheduler()
|
| 191 |
_save_state()
|
| 192 |
logger.info("Restored ELO state: %d galaxies, %d comparisons",
|
src/galaxy_profiles.py
CHANGED
|
@@ -35,6 +35,15 @@ def register_metadata(metadata_map: dict[int, dict]):
|
|
| 35 |
_metadata_cache.update(metadata_map)
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def _get_metadata(row_index: int) -> dict | None:
|
| 39 |
with _lock:
|
| 40 |
return _metadata_cache.get(row_index)
|
|
|
|
| 35 |
_metadata_cache.update(metadata_map)
|
| 36 |
|
| 37 |
|
| 38 |
+
def get_row_index_by_id(display_name: str) -> int | None:
|
| 39 |
+
"""Reverse-lookup: return the row index for a given display name, or None."""
|
| 40 |
+
with _lock:
|
| 41 |
+
for row_index, meta in _metadata_cache.items():
|
| 42 |
+
if meta is not None and str(meta.get(ID_COLUMN)) == display_name:
|
| 43 |
+
return row_index
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
|
| 47 |
def _get_metadata(row_index: int) -> dict | None:
|
| 48 |
with _lock:
|
| 49 |
return _metadata_cache.get(row_index)
|