File size: 8,440 Bytes
fe5a903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""
Classifier head for protein localization from precomputed embeddings.
"""

from __future__ import annotations

import json
from pathlib import Path
from typing import Any, Dict, List, Mapping, Sequence

import torch
from torch import Tensor, nn

ROOT = Path(__file__).resolve().parent.parent.parent
DEFAULT_LABEL_COLUMNS_JSON = ROOT / "data" / "processed" / "embeddings" / "esm2_t33_650M" / "label_columns.json"

FALLBACK_LABEL_NAMES: List[str] = [
    "Membrane",
    "Cytoplasm",
    "Nucleus",
    "Extracellular",
    "Cell membrane",
    "Mitochondrion",
    "Plastid",
    "Endoplasmic reticulum",
    "Lysosome/Vacuole",
    "Golgi apparatus",
    "Peroxisome",
]


def _load_label_names_from_json(path: Path) -> List[str] | None:
    if not path.is_file():
        return None
    with path.open("r", encoding="utf-8") as f:
        payload: Any = json.load(f)
    if isinstance(payload, dict) and isinstance(payload.get("label_columns"), list):
        names = [str(x) for x in payload["label_columns"]]
        if names:
            return names
    return None


class ProteinLocalizationClassifier(nn.Module):
    def __init__(
        self,
        embedding_dim: int,
        num_labels: int | None = None,
        dropout_rates: Sequence[float] = (0.3, 0.3, 0.2),
        hidden_dims: Sequence[int] = (512, 256, 128),
        label_names: Sequence[str] | None = None,
        label_columns_path: str | Path | None = None,
    ) -> None:
        super().__init__()

        if len(dropout_rates) != 3:
            raise ValueError(f"Expected 3 dropout rates, got {len(dropout_rates)}")
        if len(hidden_dims) != 3:
            raise ValueError(f"Expected 3 hidden dims, got {len(hidden_dims)}")
        if embedding_dim <= 0:
            raise ValueError("embedding_dim must be > 0")

        if label_names is None:
            if label_columns_path is None:
                label_columns_file = DEFAULT_LABEL_COLUMNS_JSON
            else:
                label_columns_file = Path(label_columns_path).expanduser().resolve()
            resolved = _load_label_names_from_json(label_columns_file)
            label_names = resolved if resolved is not None else FALLBACK_LABEL_NAMES

        inferred_num_labels = len(label_names)
        if num_labels is None:
            self.num_labels = inferred_num_labels
        else:
            if num_labels <= 0:
                raise ValueError("num_labels must be > 0")
            self.num_labels = int(num_labels)
            if self.num_labels != inferred_num_labels:
                raise ValueError(
                    f"num_labels={self.num_labels} must match len(label_names)={inferred_num_labels}"
                )
        self.label_names = list(label_names)

        h1, h2, h3 = [int(h) for h in hidden_dims]
        d1, d2, d3 = [float(d) for d in dropout_rates]

        self.net = nn.Sequential(
            nn.Linear(embedding_dim, h1),
            nn.BatchNorm1d(h1),
            nn.ReLU(inplace=True),
            nn.Dropout(d1),
            nn.Linear(h1, h2),
            nn.BatchNorm1d(h2),
            nn.ReLU(inplace=True),
            nn.Dropout(d2),
            nn.Linear(h2, h3),
            nn.BatchNorm1d(h3),
            nn.ReLU(inplace=True),
            nn.Dropout(d3),
            nn.Linear(h3, self.num_labels),
        )

        self._init_weights()

    def _init_weights(self) -> None:
        for module in self.modules():
            if isinstance(module, nn.Linear):
                nn.init.kaiming_normal_(module.weight, mode="fan_in", nonlinearity="relu")
                if module.bias is not None:
                    nn.init.zeros_(module.bias)

    def forward(self, x: Tensor) -> Tensor:
        # No sigmoid here; use BCEWithLogitsLoss during training.
        return self.net(x)

    def _ensure_batch(self, embedding: Tensor) -> tuple[Tensor, bool]:
        if embedding.dim() == 1:
            return embedding.unsqueeze(0), True
        if embedding.dim() == 2:
            return embedding, False
        raise ValueError(f"Expected tensor with dim 1 or 2, got shape {tuple(embedding.shape)}")

    def predict_proba(self, embedding: Tensor) -> Dict[str, float] | List[Dict[str, float]]:
        was_training = self.training
        self.eval()
        with torch.no_grad():
            x, single = self._ensure_batch(embedding)
            probs = torch.sigmoid(self.forward(x))
            probs_cpu = probs.detach().cpu().tolist()
        if was_training:
            self.train()

        output = [
            {name: float(row[i]) for i, name in enumerate(self.label_names)}
            for row in probs_cpu
        ]
        return output[0] if single else output

    def predict(
        self,
        embedding: Tensor,
        thresholds: Dict[str, float] | Tensor | None = None,
    ) -> Dict[str, int] | List[Dict[str, int]]:
        was_training = self.training
        self.eval()
        with torch.no_grad():
            x, single = self._ensure_batch(embedding)
            probs = torch.sigmoid(self.forward(x))

            if thresholds is None:
                th = torch.full((self.num_labels,), 0.5, dtype=probs.dtype, device=probs.device)
            elif isinstance(thresholds, dict):
                th_vals = [float(thresholds.get(name, 0.5)) for name in self.label_names]
                th = torch.tensor(th_vals, dtype=probs.dtype, device=probs.device)
            elif isinstance(thresholds, Tensor):
                if thresholds.numel() != self.num_labels:
                    raise ValueError(
                        f"threshold tensor must have {self.num_labels} values, got {thresholds.numel()}"
                    )
                th = thresholds.to(device=probs.device, dtype=probs.dtype).reshape(-1)
            else:
                raise TypeError("thresholds must be None, dict, or torch.Tensor")

            binary = (probs >= th.unsqueeze(0)).to(torch.int64).detach().cpu().tolist()
        if was_training:
            self.train()

        output = [
            {name: int(row[i]) for i, name in enumerate(self.label_names)}
            for row in binary
        ]
        return output[0] if single else output


def count_parameters(model: nn.Module) -> None:
    total = sum(p.numel() for p in model.parameters())
    trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
    print(f"Total parameters: {total:,}")
    print(f"Trainable parameters: {trainable:,}")


def load_model(
    path: str | Path,
    embedding_dim: int,
    num_labels: int | None,
    device: torch.device | str,
) -> ProteinLocalizationClassifier:
    device = torch.device(device)
    ckpt_path = Path(path).expanduser().resolve()
    checkpoint = torch.load(ckpt_path, map_location=device)

    label_names: Sequence[str] | None = None
    if isinstance(checkpoint, dict) and "label_names" in checkpoint:
        label_names = checkpoint["label_names"]

    if isinstance(checkpoint, dict) and "state_dict" in checkpoint:
        state_dict = checkpoint["state_dict"]
    elif isinstance(checkpoint, Mapping):
        state_dict = checkpoint
    else:
        raise ValueError("Unsupported checkpoint format: expected dict or dict with 'state_dict'.")

    if num_labels is None:
        if label_names is not None:
            num_labels = len(label_names)
        else:
            classifier_weight = state_dict.get("net.12.weight")
            if classifier_weight is None:
                raise ValueError("Could not infer num_labels from checkpoint; pass num_labels explicitly.")
            num_labels = int(classifier_weight.shape[0])

    dropout_rates: Sequence[float] | None = None
    hidden_dims: Sequence[int] | None = None
    if isinstance(checkpoint, dict):
        if "dropout_rates" in checkpoint:
            dropout_rates = tuple(checkpoint["dropout_rates"])  # type: ignore[assignment]
        if "hidden_dims" in checkpoint:
            hidden_dims = tuple(int(x) for x in checkpoint["hidden_dims"])  # type: ignore[assignment]

    model = ProteinLocalizationClassifier(
        embedding_dim=embedding_dim,
        num_labels=num_labels,
        label_names=label_names,
        dropout_rates=dropout_rates if dropout_rates is not None else (0.3, 0.3, 0.2),
        hidden_dims=hidden_dims if hidden_dims is not None else (512, 256, 128),
    )

    model.load_state_dict(state_dict)
    model.to(device)
    model.eval()
    return model