File size: 18,461 Bytes
653040f | 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | """
mjm_2a/b/c: three variants that inject brain-specific spatial/morphological context
into the mjm_1 hierarchical encoder-decoder.
All variants preserve the EXACT mjm_1 hierarchical logic:
X -> E1->h1 -> E2->h2 -> E3->H
D1->C1 D2->C2+C1 D3->C3+C2 + ReconDecoder
mjm_2a: Laminar Depth Injection
- DepthMLP(depth_imputed[B,1]) -> depth_feat[B,dec_hidden_dim]
- Injected as residual into C2 (Subclass decoder), after C1 residual
- Auxiliary depth regression head on h2
mjm_2b: Morpho-Spatial Fusion
- Input = concat(X[B,140], cell_volume_norm[B,1], x_tiled[B,1], y_tiled[B,1]) -> [B,143]
- Linear(143->140) projects back to gene-expression space before E1
- Auxiliary volume regression head on H (latent)
mjm_2c: CPS-Conditioned FiLM Modulation
- ContextMLP([cps, x_tiled, y_tiled] -> [B,3]) -> gamma, beta [B, enc_hidden_dim]
- FiLM modulates h1 (E1 output): h1_mod = gamma * h1 + beta
- Everything downstream (E2, E3, D1/D2/D3) sees disease-space-conditioned features
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
# ── Re-use building blocks from mjm.py ─────────────────────────────────────
from src.models.mjm import RMSNorm, SwiGLU, FFN, _make_dec_block
# ────────────────────────────────────────────────────────────────────────────
# Plan A: Laminar Depth Injection
# ────────────────────────────────────────────────────────────────────────────
class mjm_2a(nn.Module):
"""
mjm_1 architecture + depth context injected at the Subclass decoder level.
depth_imputed [B,1] -> DepthMLP -> depth_feat [B, dec_hidden_dim]
C2 = D2(h2) + C1 + depth_feat (depth residual at Subclass level)
C3 = D3(H) + C2 (Supertype still gets depth via C2)
Auxiliary head: depth_head(h2) -> scalar (only supervised on has_depth cells)
"""
def __init__(self,
input_dim=140,
latent_dim=20,
e_layers=3,
d_layers=1,
enc_hidden_dim=256,
dec_hidden_dim=128,
depth_mlp_dim=64,
expansion_factor=2.67,
dropout=0.3,
output_num=[3, 24, 137],
residual_mode='feature',
):
super().__init__()
assert residual_mode == 'feature', "mjm_2a only supports feature residual mode"
self.residual_mode = residual_mode
# ── Encoders (identical to mjm_1) ───────────────────────────────────
self.E1 = nn.Sequential(
nn.Linear(input_dim, enc_hidden_dim),
FFN(n_layers=e_layers, model_dim=enc_hidden_dim,
expansion_factor=expansion_factor, dropout=dropout),
)
self.E2 = FFN(n_layers=e_layers, model_dim=enc_hidden_dim,
expansion_factor=expansion_factor, dropout=dropout)
self.E3 = nn.Sequential(
FFN(n_layers=e_layers, model_dim=enc_hidden_dim,
expansion_factor=expansion_factor, dropout=dropout),
nn.Linear(enc_hidden_dim, latent_dim),
)
# ── Decoders (identical to mjm_1) ───────────────────────────────────
self.D1 = _make_dec_block(enc_hidden_dim, dec_hidden_dim,
d_layers, expansion_factor, dropout)
self.D2 = _make_dec_block(enc_hidden_dim, dec_hidden_dim,
d_layers, expansion_factor, dropout)
self.D3 = _make_dec_block(latent_dim, dec_hidden_dim,
d_layers, expansion_factor, dropout)
# ── Classification heads (identical to mjm_1) ───────────────────────
self.head1 = nn.Linear(dec_hidden_dim, output_num[0])
self.head2 = nn.Linear(dec_hidden_dim, output_num[1])
self.head3 = nn.Linear(dec_hidden_dim, output_num[2])
# ── Reconstruction decoder (identical to mjm_1) ─────────────────────
self.recon_decoder = SwiGLU(latent_dim, input_dim, dropout=dropout)
# ── NEW: Depth context branch ────────────────────────────────────────
self.depth_mlp = nn.Sequential(
nn.Linear(1, depth_mlp_dim),
nn.SiLU(),
nn.Linear(depth_mlp_dim, dec_hidden_dim),
)
# Auxiliary depth regression head (applied on h2)
self.depth_head = nn.Linear(enc_hidden_dim, 1)
def forward(self, x, depth_imputed):
"""
Args:
x: [B, input_dim] gene expression (log1p normalized)
depth_imputed: [B, 1] imputed normalized depth from pia
Returns:
recon, [logits1, logits2, logits3], H, depth_pred
"""
# ── Encoding ────────────────────────────────────────────────────────
h1 = self.E1(x) # [B, enc_hidden_dim]
h2 = self.E2(h1) # [B, enc_hidden_dim]
H = self.E3(h2) # [B, latent_dim]
# ── Depth context ────────────────────────────────────────────────────
depth_feat = self.depth_mlp(depth_imputed) # [B, dec_hidden_dim]
# ── Hierarchical decoding + classification ───────────────────────────
C1 = self.D1(h1) # [B, dec_hidden_dim]
logits1 = self.head1(C1) # [B, n1]
# C2: mjm_1 feature residual + depth injection at Subclass level
C2 = self.D2(h2) + C1 + depth_feat # [B, dec_hidden_dim]
logits2 = self.head2(C2) # [B, n2]
# C3: standard feature residual (depth flows through C2)
C3 = self.D3(H) + C2 # [B, dec_hidden_dim]
logits3 = self.head3(C3) # [B, n3]
# ── Reconstruction ───────────────────────────────────────────────────
recon = self.recon_decoder(H) # [B, input_dim]
# ── Auxiliary depth prediction ───────────────────────────────────────
depth_pred = self.depth_head(h2) # [B, 1]
return recon, [logits1, logits2, logits3], H, depth_pred
# ────────────────────────────────────────────────────────────────────────────
# Plan B: Morpho-Spatial Fusion
# ────────────────────────────────────────────────────────────────────────────
class mjm_2b(nn.Module):
"""
mjm_1 + cell morphology (volume) and spatial tiled coords fused at input.
Input fusion:
[X(140), cell_volume_norm(1), x_tiled_norm(1), y_tiled_norm(1)] -> [B,143]
Linear(143->140) -> X_proj [B,140]
-> E1->E2->E3 (identical to mjm_1)
Auxiliary volume regression head on H (latent):
volume_head(H) -> [B,1], only supervised on has_volume cells
has_volume mask ensures 80.5% cells without volume still train normally
through the spatial tiled coordinates.
"""
def __init__(self,
input_dim=140,
latent_dim=20,
e_layers=3,
d_layers=1,
enc_hidden_dim=256,
dec_hidden_dim=128,
expansion_factor=2.67,
dropout=0.3,
output_num=[3, 24, 137],
residual_mode='feature',
):
super().__init__()
assert residual_mode == 'feature'
self.residual_mode = residual_mode
# ── NEW: Input fusion projection ─────────────────────────────────────
# X(140) + volume(1) + x_tiled(1) + y_tiled(1) = 143
self.input_proj = nn.Linear(input_dim + 3, input_dim)
# ── Encoders (identical to mjm_1) ───────────────────────────────────
self.E1 = nn.Sequential(
nn.Linear(input_dim, enc_hidden_dim),
FFN(n_layers=e_layers, model_dim=enc_hidden_dim,
expansion_factor=expansion_factor, dropout=dropout),
)
self.E2 = FFN(n_layers=e_layers, model_dim=enc_hidden_dim,
expansion_factor=expansion_factor, dropout=dropout)
self.E3 = nn.Sequential(
FFN(n_layers=e_layers, model_dim=enc_hidden_dim,
expansion_factor=expansion_factor, dropout=dropout),
nn.Linear(enc_hidden_dim, latent_dim),
)
# ── Decoders (identical to mjm_1) ───────────────────────────────────
self.D1 = _make_dec_block(enc_hidden_dim, dec_hidden_dim,
d_layers, expansion_factor, dropout)
self.D2 = _make_dec_block(enc_hidden_dim, dec_hidden_dim,
d_layers, expansion_factor, dropout)
self.D3 = _make_dec_block(latent_dim, dec_hidden_dim,
d_layers, expansion_factor, dropout)
# ── Classification heads (identical to mjm_1) ───────────────────────
self.head1 = nn.Linear(dec_hidden_dim, output_num[0])
self.head2 = nn.Linear(dec_hidden_dim, output_num[1])
self.head3 = nn.Linear(dec_hidden_dim, output_num[2])
# ── Reconstruction decoder (identical to mjm_1) ─────────────────────
self.recon_decoder = SwiGLU(latent_dim, input_dim, dropout=dropout)
# ── NEW: Auxiliary volume regression head ────────────────────────────
self.volume_head = nn.Linear(latent_dim, 1)
def forward(self, x, cell_volume_norm, spatial_tiled_norm):
"""
Args:
x: [B, 140] gene expression (log1p)
cell_volume_norm: [B, 1] z-score normalized volume (0 if missing)
spatial_tiled_norm: [B, 2] normalized tiled spatial coords
Returns:
recon, [logits1, logits2, logits3], H, vol_pred
"""
# ── Input fusion ────────────────────────────────────────────────────
x_aug = torch.cat([x, cell_volume_norm, spatial_tiled_norm], dim=-1) # [B,143]
x_proj = self.input_proj(x_aug) # [B,140]
# ── Encoding ────────────────────────────────────────────────────────
h1 = self.E1(x_proj) # [B, enc_hidden_dim]
h2 = self.E2(h1) # [B, enc_hidden_dim]
H = self.E3(h2) # [B, latent_dim]
# ── Hierarchical decoding (identical to mjm_1 feature mode) ─────────
C1 = self.D1(h1)
logits1 = self.head1(C1)
C2 = self.D2(h2) + C1
logits2 = self.head2(C2)
C3 = self.D3(H) + C2
logits3 = self.head3(C3)
recon = self.recon_decoder(H)
# ── Auxiliary volume prediction ──────────────────────────────────────
vol_pred = self.volume_head(H) # [B, 1]
return recon, [logits1, logits2, logits3], H, vol_pred
# ────────────────────────────────────────────────────────────────────────────
# Plan C: CPS-Conditioned FiLM Modulation
# ────────────────────────────────────────────────────────────────────────────
class mjm_2c(nn.Module):
"""
mjm_1 + FiLM modulation of h1 conditioned on CPS and spatial_tiled.
Context = [cps(1), x_tiled_norm(1), y_tiled_norm(1)] -> [B,3]
ContextMLP(3->film_hidden->enc_hidden_dim*2) -> gamma[B,d], beta[B,d]
h1_mod = gamma * h1 + beta
Everything downstream (E2->E3->D1/D2/D3) sees disease-and-space-conditioned
features. CPS is available for ALL 1.9M cells, so no cells are wasted.
No auxiliary loss needed — FiLM supervision comes from the classification
and reconstruction losses backpropagating through h1_mod.
"""
def __init__(self,
input_dim=140,
latent_dim=20,
e_layers=3,
d_layers=1,
enc_hidden_dim=256,
dec_hidden_dim=128,
film_hidden=64,
expansion_factor=2.67,
dropout=0.3,
output_num=[3, 24, 137],
residual_mode='feature',
):
super().__init__()
assert residual_mode == 'feature'
self.residual_mode = residual_mode
self.enc_hidden_dim = enc_hidden_dim
# ── Encoders (identical to mjm_1) ───────────────────────────────────
self.E1 = nn.Sequential(
nn.Linear(input_dim, enc_hidden_dim),
FFN(n_layers=e_layers, model_dim=enc_hidden_dim,
expansion_factor=expansion_factor, dropout=dropout),
)
self.E2 = FFN(n_layers=e_layers, model_dim=enc_hidden_dim,
expansion_factor=expansion_factor, dropout=dropout)
self.E3 = nn.Sequential(
FFN(n_layers=e_layers, model_dim=enc_hidden_dim,
expansion_factor=expansion_factor, dropout=dropout),
nn.Linear(enc_hidden_dim, latent_dim),
)
# ── Decoders (identical to mjm_1) ───────────────────────────────────
self.D1 = _make_dec_block(enc_hidden_dim, dec_hidden_dim,
d_layers, expansion_factor, dropout)
self.D2 = _make_dec_block(enc_hidden_dim, dec_hidden_dim,
d_layers, expansion_factor, dropout)
self.D3 = _make_dec_block(latent_dim, dec_hidden_dim,
d_layers, expansion_factor, dropout)
# ── Classification heads (identical to mjm_1) ───────────────────────
self.head1 = nn.Linear(dec_hidden_dim, output_num[0])
self.head2 = nn.Linear(dec_hidden_dim, output_num[1])
self.head3 = nn.Linear(dec_hidden_dim, output_num[2])
# ── Reconstruction decoder (identical to mjm_1) ─────────────────────
self.recon_decoder = SwiGLU(latent_dim, input_dim, dropout=dropout)
# ── NEW: FiLM context network ────────────────────────────────────────
# Input: [cps, x_tiled_norm, y_tiled_norm] -> 3 dims
self.film_net = nn.Sequential(
nn.Linear(3, film_hidden),
nn.SiLU(),
nn.Linear(film_hidden, enc_hidden_dim * 2), # -> gamma + beta
)
def forward(self, x, cps, spatial_tiled_norm):
"""
Args:
x: [B, 140] gene expression (log1p)
cps: [B, 1] continuous pseudo-progression score
spatial_tiled_norm: [B, 2] normalized tiled spatial coords
Returns:
recon, [logits1, logits2, logits3], H
"""
# ── E1: initial encoding ─────────────────────────────────────────────
h1 = self.E1(x) # [B, enc_hidden_dim]
# ── FiLM modulation on h1 ────────────────────────────────────────────
context = torch.cat([cps, spatial_tiled_norm], dim=-1) # [B, 3]
film_params = self.film_net(context) # [B, enc_hidden_dim*2]
gamma, beta = film_params.chunk(2, dim=-1) # each [B, enc_hidden_dim]
# scale gamma around 1 (like layer norm scale), beta is bias
h1_mod = (1.0 + gamma) * h1 + beta # [B, enc_hidden_dim]
# ── E2, E3 ───────────────────────────────────────────────────────────
h2 = self.E2(h1_mod) # [B, enc_hidden_dim]
H = self.E3(h2) # [B, latent_dim]
# ── Hierarchical decoding (identical to mjm_1 feature mode) ─────────
C1 = self.D1(h1_mod)
logits1 = self.head1(C1)
C2 = self.D2(h2) + C1
logits2 = self.head2(C2)
C3 = self.D3(H) + C2
logits3 = self.head3(C3)
recon = self.recon_decoder(H)
return recon, [logits1, logits2, logits3], H
|