Skip to content

Commit 1592c6a

Browse files
refactor: improve label functions
1 parent e5a704b commit 1592c6a

2 files changed

Lines changed: 56 additions & 47 deletions

File tree

src/analyzer/data_loader.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
from typing import List, Tuple
1+
from collections import defaultdict
2+
from typing import DefaultDict, List, Tuple, TypedDict, cast
23

34
from chess_utils import fen_to_tensor
45

5-
from .labels import label_to_vector
6+
from .labels import label_to_vector, vector_to_label
67

8+
FenVec = list[float]
79

8-
def load_chessfile_predict(filepath: str) -> List[str]:
10+
11+
def load_chessfile_predict(filepath: str) -> list[str]:
912
with open(filepath, "r", encoding="utf-8") as f:
1013
return [line.strip() for line in f if line.strip()]
1114

1215

1316
def load_chessfile_train(
1417
filepath: str, encoding: str = "simple"
15-
) -> List[Tuple[List[float], List[float]]]:
18+
) -> list[tuple[FenVec, list[float]]]:
1619

17-
dataset: List[Tuple[List[float], List[float]]] = []
20+
dataset: list[tuple[FenVec, list[float]]] = []
1821

1922
with open(filepath, "r", encoding="utf-8") as f:
2023
for line_num, line in enumerate(f, 1):
@@ -37,3 +40,21 @@ def load_chessfile_train(
3740
continue
3841

3942
return dataset
43+
44+
45+
class Dataset(TypedDict):
46+
nothing: list[FenVec]
47+
checkmate_white: list[FenVec]
48+
checkmate_black: list[FenVec]
49+
check_white: list[FenVec]
50+
check_black: list[FenVec]
51+
52+
53+
def sort_dataset(ds: list[tuple[FenVec, list[float]]]) -> Dataset:
54+
out: DefaultDict[str, list[FenVec]] = defaultdict(list)
55+
56+
for fen, expected in ds:
57+
key = vector_to_label(expected).lower().replace(" ", "_")
58+
out[key].append(fen)
59+
60+
return cast(Dataset, out)

src/analyzer/labels.py

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,30 @@
1-
from typing import List
2-
3-
4-
def label_to_vector(label: str) -> List[float]:
5-
label = label.strip()
6-
7-
if label == "Nothing":
8-
return [1.0, 0.0, 0.0, 0.0, 0.0]
9-
elif label in ("Check White", "CheckWhite"):
10-
return [0.0, 1.0, 0.0, 0.0, 0.0]
11-
elif label in ("Check Black", "CheckBlack"):
12-
return [0.0, 0.0, 1.0, 0.0, 0.0]
13-
elif label in ("Checkmate White", "CheckmateWhite"):
14-
return [0.0, 0.0, 0.0, 1.0, 0.0]
15-
elif label in ("Checkmate Black", "CheckmateBlack"):
16-
return [0.0, 0.0, 0.0, 0.0, 1.0]
17-
elif label == "Check":
18-
return [0.0, 1.0, 0.0]
19-
elif label == "Checkmate":
20-
return [0.0, 0.0, 1.0]
21-
else:
22-
raise ValueError(f"Unknown label: {label}")
23-
24-
25-
def vector_to_label(vec: List[float], mode: str = "auto") -> str:
26-
if mode == "auto":
27-
mode = "detailed" if len(vec) == 5 else "basic"
28-
29-
pred_idx = max(range(len(vec)), key=lambda i: vec[i])
30-
31-
if mode == "detailed" and len(vec) == 5:
32-
labels = [
33-
"Nothing",
34-
"Check White",
35-
"Check Black",
36-
"Checkmate White",
37-
"Checkmate Black",
38-
]
39-
return labels[pred_idx]
40-
else:
41-
labels = ["Nothing", "Check", "Checkmate"]
42-
return labels[min(pred_idx, 2)]
1+
from typing import List, Literal
2+
3+
Label = Literal[
4+
"Nothing",
5+
"Check White",
6+
"Check Black",
7+
"Checkmate White",
8+
"Checkmate Black",
9+
]
10+
11+
LABELS: list[Label] = [
12+
"Nothing",
13+
"Check White",
14+
"Check Black",
15+
"Checkmate White",
16+
"Checkmate Black",
17+
]
18+
19+
20+
def label_to_vector(label: Label) -> List[float]:
21+
vec = [0.0] * 5
22+
vec[LABELS.index(label)] = 1.0
23+
24+
return vec
25+
26+
27+
def vector_to_label(vec: List[float]) -> Label:
28+
int_vec = [e > 0.5 for e in vec]
29+
30+
return LABELS[int_vec.index(True)]

0 commit comments

Comments
 (0)