File size: 1,550 Bytes
c289d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import annotations

from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Any, Dict, List

import json


@dataclass
class QueueItem:
    ligand_id: str
    status: str = "active"
    priority: float = 0.0
    hypercluster_id: int = -1
    cluster_id: int = -1
    times_selected: int = 0
    last_score: float | None = None


@dataclass
class SchedulerState:
    queue: List[QueueItem] = field(default_factory=list)
    batch_history: List[Dict[str, Any]] = field(default_factory=list)
    round_idx: int = 0

    def to_dict(self) -> Dict[str, Any]:
        return {
            "queue": [asdict(item) for item in self.queue],
            "batch_history": self.batch_history,
            "round_idx": self.round_idx,
        }

    @classmethod
    def from_dict(cls, payload: Dict[str, Any]) -> "SchedulerState":
        queue = [QueueItem(**item) for item in payload.get("queue", [])]
        return cls(queue=queue, batch_history=payload.get("batch_history", []), round_idx=int(payload.get("round_idx", 0)))

    def save(self, path: str | Path) -> Path:
        target = Path(path)
        target.parent.mkdir(parents=True, exist_ok=True)
        with target.open("w", encoding="utf-8") as handle:
            json.dump(self.to_dict(), handle, indent=2)
        return target

    @classmethod
    def load(cls, path: str | Path) -> "SchedulerState":
        source = Path(path)
        with source.open("r", encoding="utf-8") as handle:
            return cls.from_dict(json.load(handle))