Datasets:
File size: 29,772 Bytes
fe39cc9 | 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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | """
Flexible Multi-Task Head and Loss Function
Adapts to any biomarker configuration without hardcoded assumptions
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from typing import Dict, List, Tuple, Any, Union, Optional
import logging
from config.biomarker_config import FlexibleBiomarkerConfig, TensorLayout
from .single_target_strategies import (
SingleTargetStrategy,
create_feature_extractor,
get_strategy_from_name
)
logger = logging.getLogger(__name__)
class FlexibleMultiTaskHead(nn.Module):
"""Flexible multi-task head that adapts to biomarker configuration"""
def __init__(
self,
input_dim: int,
biomarker_config: FlexibleBiomarkerConfig,
dropout: float = 0.1,
single_target_strategy: Optional[Union[str, SingleTargetStrategy]] = None,
target_feature_dim: Optional[int] = None
):
super().__init__()
self.biomarker_config = biomarker_config
self.tensor_layout = biomarker_config.get_tensor_layout()
# Handle single-target strategy
self.single_target_strategy = None
self.feature_extractor = None
self.target_feature_dim = target_feature_dim
if single_target_strategy is not None:
if isinstance(single_target_strategy, str):
self.single_target_strategy = get_strategy_from_name(single_target_strategy)
else:
self.single_target_strategy = single_target_strategy
# Create appropriate feature extractor with target feature dimension
feature_dim = target_feature_dim if target_feature_dim is not None else input_dim
self.feature_extractor = create_feature_extractor(
self.single_target_strategy,
input_dim,
feature_dim=feature_dim,
dropout=dropout
)
# Use feature extractor output dimension
processed_input_dim = self.feature_extractor.output_dim
else:
# Default behavior - use original input dimension
processed_input_dim = input_dim
# Shared feature processing (only if no single-target strategy is used)
if self.single_target_strategy is None:
# Use target_feature_dim if provided, otherwise use 512 as default
output_dim = target_feature_dim if target_feature_dim is not None else 512
self.shared_layers = nn.Sequential(
nn.Linear(input_dim, output_dim),
nn.ReLU(inplace=True),
nn.Dropout(dropout),
nn.BatchNorm1d(output_dim)
)
processed_input_dim = output_dim
else:
# Skip shared layers when using single-target strategy
self.shared_layers = nn.Identity()
processed_input_dim = self.feature_extractor.output_dim
# Task-specific heads
self.task_heads = nn.ModuleDict()
# Binary classification heads (one per biomarker)
for biomarker in biomarker_config.binary_biomarkers:
self.task_heads[f"binary_{biomarker.name}"] = nn.Linear(processed_input_dim, 1)
# Multiclass classification heads
for biomarker in biomarker_config.multiclass_biomarkers:
num_classes = len(biomarker.classes)
self.task_heads[f"multiclass_{biomarker.name}"] = nn.Linear(processed_input_dim, num_classes)
# Regression heads (one per biomarker)
for biomarker in biomarker_config.continuous_biomarkers:
self.task_heads[f"continuous_{biomarker.name}"] = nn.Linear(processed_input_dim, 1)
def forward(self, x):
"""
Forward pass
Args:
x: Input features [batch_size, input_dim] or [batch_size, C, H, W] for spatial features
Returns:
Concatenated outputs [batch_size, total_output_size]
"""
# Apply single-target strategy feature extraction if specified
if self.single_target_strategy is not None and self.feature_extractor is not None:
shared_features = self.feature_extractor(x)
else:
shared_features = self.shared_layers(x)
outputs = []
# Binary outputs
for biomarker in self.biomarker_config.binary_biomarkers:
head_name = f"binary_{biomarker.name}"
binary_out = self.task_heads[head_name](shared_features) # [B, 1]
outputs.append(binary_out)
# Multiclass outputs
for biomarker in self.biomarker_config.multiclass_biomarkers:
head_name = f"multiclass_{biomarker.name}"
multiclass_out = self.task_heads[head_name](shared_features) # [B, num_classes]
outputs.append(multiclass_out)
# Regression outputs
for biomarker in self.biomarker_config.continuous_biomarkers:
head_name = f"continuous_{biomarker.name}"
regression_out = self.task_heads[head_name](shared_features) # [B, 1]
outputs.append(regression_out)
# Concatenate all outputs
return torch.cat(outputs, dim=1)
class LinearProbeMultiTaskHead(nn.Module):
"""
True linear probe head - direct mapping from backbone features to task outputs
No shared layers, minimal parameters, maximum interpretability
"""
def __init__(
self,
input_dim: int,
biomarker_config: FlexibleBiomarkerConfig,
dropout: float = 0.0,
single_target_strategy: Optional[Union[str, SingleTargetStrategy]] = None,
target_feature_dim: Optional[int] = None
):
super().__init__()
self.biomarker_config = biomarker_config
self.tensor_layout = biomarker_config.get_tensor_layout()
# Handle single-target strategy
self.single_target_strategy = None
self.feature_extractor = None
self.target_feature_dim = target_feature_dim
if single_target_strategy is not None:
if isinstance(single_target_strategy, str):
self.single_target_strategy = get_strategy_from_name(single_target_strategy)
else:
self.single_target_strategy = single_target_strategy
# Create appropriate feature extractor with target feature dimension
feature_dim = target_feature_dim if target_feature_dim is not None else input_dim
self.feature_extractor = create_feature_extractor(
self.single_target_strategy,
input_dim,
feature_dim=feature_dim,
dropout=0.0 # No dropout for linear probe
)
# Use feature extractor output dimension
processed_input_dim = self.feature_extractor.output_dim
else:
# Default behavior - use original input dimension
processed_input_dim = input_dim
# Optional minimal dropout (usually 0.0 for true linear probe)
self.dropout = nn.Dropout(dropout) if dropout > 0 else nn.Identity()
# Direct task-specific linear layers (no shared processing)
self.task_heads = nn.ModuleDict()
# Binary classification heads - direct from backbone
for biomarker in biomarker_config.binary_biomarkers:
self.task_heads[f"binary_{biomarker.name}"] = nn.Linear(processed_input_dim, 1)
# Multiclass classification heads - direct from backbone
for biomarker in biomarker_config.multiclass_biomarkers:
num_classes = len(biomarker.classes)
self.task_heads[f"multiclass_{biomarker.name}"] = nn.Linear(processed_input_dim, num_classes)
# Regression heads - direct from backbone
for biomarker in biomarker_config.continuous_biomarkers:
self.task_heads[f"continuous_{biomarker.name}"] = nn.Linear(processed_input_dim, 1)
# Initialize weights for better convergence
self._initialize_weights()
def _initialize_weights(self):
"""Initialize linear layer weights for stable training"""
for name, module in self.task_heads.items():
if isinstance(module, nn.Linear):
# Xavier/Glorot initialization for linear layers
nn.init.xavier_uniform_(module.weight)
nn.init.zeros_(module.bias)
def forward(self, x):
"""
Direct forward pass - no shared processing
Args:
x: Backbone features [batch_size, input_dim] or [batch_size, C, H, W] for spatial features
Returns:
Concatenated outputs [batch_size, total_output_size]
"""
# Apply single-target strategy feature extraction if specified
if self.single_target_strategy is not None and self.feature_extractor is not None:
features = self.feature_extractor(x)
else:
# Optional dropout on backbone features (usually disabled)
features = self.dropout(x) # [batch_size, input_dim]
outputs = []
# Binary outputs - direct linear transformation
for biomarker in self.biomarker_config.binary_biomarkers:
head_name = f"binary_{biomarker.name}"
binary_out = self.task_heads[head_name](features) # [B, 1]
outputs.append(binary_out)
# Multiclass outputs - direct linear transformation
for biomarker in self.biomarker_config.multiclass_biomarkers:
head_name = f"multiclass_{biomarker.name}"
multiclass_out = self.task_heads[head_name](features) # [B, num_classes]
outputs.append(multiclass_out)
# Regression outputs - direct linear transformation
for biomarker in self.biomarker_config.continuous_biomarkers:
head_name = f"continuous_{biomarker.name}"
regression_out = self.task_heads[head_name](features) # [B, 1]
outputs.append(regression_out)
# Concatenate all outputs
return torch.cat(outputs, dim=1) # [batch_size, total_output_size]
class FlexibleMultiTaskLoss(nn.Module):
"""Flexible multi-task loss function that adapts to biomarker configuration"""
def __init__(self, biomarker_config: FlexibleBiomarkerConfig, class_weights: Dict[str, float] = None):
super().__init__()
self.biomarker_config = biomarker_config
self.tensor_layout = biomarker_config.get_tensor_layout()
self.class_weights = class_weights or {}
# Create weighted BCE losses for binary tasks
self.binary_losses = nn.ModuleDict()
for biomarker in biomarker_config.binary_biomarkers:
weight = self.class_weights.get(biomarker.name, 1.0)
if weight != 1.0:
pos_weight = torch.tensor([weight], dtype=torch.float32)
self.binary_losses[biomarker.name] = nn.BCEWithLogitsLoss(pos_weight=pos_weight)
else:
self.binary_losses[biomarker.name] = nn.BCEWithLogitsLoss()
# Cross-entropy losses for multiclass tasks
self.multiclass_losses = nn.ModuleDict()
for biomarker in biomarker_config.multiclass_biomarkers:
self.multiclass_losses[biomarker.name] = nn.CrossEntropyLoss()
# MSE losses for regression tasks
self.regression_losses = nn.ModuleDict()
for biomarker in biomarker_config.continuous_biomarkers:
self.regression_losses[biomarker.name] = nn.MSELoss()
def forward(self, predictions: torch.Tensor, targets: torch.Tensor) -> Tuple[torch.Tensor, Dict[str, float]]:
"""
Calculate multi-task loss
Args:
predictions: [batch_size, total_output_size]
targets: [batch_size, total_output_size]
Returns:
total_loss: Combined loss
loss_dict: Dictionary with individual loss components
"""
device = predictions.device
# Move pos_weight tensors to correct device
for biomarker_name, loss_fn in self.binary_losses.items():
if hasattr(loss_fn, 'pos_weight') and loss_fn.pos_weight is not None:
loss_fn.pos_weight = loss_fn.pos_weight.to(device)
total_loss = 0.0
loss_components = {}
# Binary classification losses
binary_losses = []
for biomarker in self.biomarker_config.binary_biomarkers:
layout = self.tensor_layout[biomarker.name]
pred_slice = predictions[:, layout.start_idx:layout.end_idx].squeeze(-1) # [B]
target_slice = targets[:, layout.start_idx:layout.end_idx].squeeze(-1) # [B]
loss_fn = self.binary_losses[biomarker.name]
binary_loss = loss_fn(pred_slice, target_slice)
binary_losses.append(binary_loss)
loss_components[f'binary_{biomarker.name}'] = binary_loss.item()
if binary_losses:
avg_binary_loss = torch.mean(torch.stack(binary_losses))
total_loss += avg_binary_loss
loss_components['avg_binary_loss'] = avg_binary_loss.item()
# Multiclass classification losses
multiclass_losses = []
for biomarker in self.biomarker_config.multiclass_biomarkers:
layout = self.tensor_layout[biomarker.name]
pred_slice = predictions[:, layout.start_idx:layout.end_idx] # [B, num_classes]
target_slice = targets[:, layout.start_idx:layout.end_idx] # [B, num_classes]
# Convert one-hot targets to class indices
target_indices = torch.argmax(target_slice, dim=1) # [B]
loss_fn = self.multiclass_losses[biomarker.name]
multiclass_loss = loss_fn(pred_slice, target_indices)
multiclass_losses.append(multiclass_loss)
loss_components[f'multiclass_{biomarker.name}'] = multiclass_loss.item()
if multiclass_losses:
avg_multiclass_loss = torch.mean(torch.stack(multiclass_losses))
total_loss += avg_multiclass_loss
loss_components['avg_multiclass_loss'] = avg_multiclass_loss.item()
# Regression losses
regression_losses = []
for biomarker in self.biomarker_config.continuous_biomarkers:
layout = self.tensor_layout[biomarker.name]
pred_slice = predictions[:, layout.start_idx:layout.end_idx].squeeze(-1) # [B]
target_slice = targets[:, layout.start_idx:layout.end_idx].squeeze(-1) # [B]
loss_fn = self.regression_losses[biomarker.name]
regression_loss = loss_fn(pred_slice, target_slice)
regression_losses.append(regression_loss)
loss_components[f'regression_{biomarker.name}'] = regression_loss.item()
if regression_losses:
avg_regression_loss = torch.mean(torch.stack(regression_losses))
total_loss += avg_regression_loss
loss_components['avg_regression_loss'] = avg_regression_loss.item()
loss_components['total_loss'] = total_loss.item()
return total_loss, loss_components
class FlexibleMetricsCalculator:
"""Calculate comprehensive metrics for flexible multi-task learning"""
def __init__(self, biomarker_config: FlexibleBiomarkerConfig):
self.biomarker_config = biomarker_config
self.tensor_layout = biomarker_config.get_tensor_layout()
# Threshold optimization settings from config
validation_config = biomarker_config.validation
self.threshold_optimization = validation_config.get('threshold_optimization', False)
self.optimization_metric = validation_config.get('optimization_metric', 'f1_score')
self.per_biomarker_thresholds = validation_config.get('per_biomarker_thresholds', True)
self.threshold_range = validation_config.get('threshold_search_range', [0.1, 0.9])
self.threshold_steps = validation_config.get('threshold_search_steps', 81)
self.fallback_threshold = validation_config.get('fallback_threshold', 0.5)
# Store optimal thresholds per biomarker
self.optimal_thresholds = {}
def find_optimal_threshold(self, pred_probs: np.ndarray, true_labels: np.ndarray,
metric: str = 'f1_score') -> Tuple[float, float]:
"""
Find optimal threshold for a single biomarker based on specified metric
Args:
pred_probs: Predicted probabilities [N]
true_labels: True binary labels [N]
metric: Metric to optimize ('f1_score', 'sensitivity', 'specificity', etc.)
Returns:
best_threshold: Optimal threshold value
best_score: Best metric score achieved
"""
from sklearn.metrics import f1_score, precision_score, recall_score
# Create threshold search space
thresholds = np.linspace(self.threshold_range[0], self.threshold_range[1], self.threshold_steps)
best_threshold = self.fallback_threshold
best_score = 0.0
for threshold in thresholds:
pred_labels = (pred_probs > threshold).astype(int)
try:
if metric == 'f1_score':
score = f1_score(true_labels, pred_labels, zero_division=0.0)
elif metric == 'precision':
score = precision_score(true_labels, pred_labels, zero_division=0.0)
elif metric == 'recall' or metric == 'sensitivity':
score = recall_score(true_labels, pred_labels, zero_division=0.0)
elif metric == 'specificity':
# Specificity = TN / (TN + FP)
tn = np.sum((pred_labels == 0) & (true_labels == 0))
fp = np.sum((pred_labels == 1) & (true_labels == 0))
score = tn / (tn + fp + 1e-8)
elif metric == 'accuracy':
score = (pred_labels == true_labels).mean()
else:
# Default to f1_score
score = f1_score(true_labels, pred_labels, zero_division=0.0)
if score > best_score:
best_score = score
best_threshold = threshold
except (ValueError, ZeroDivisionError):
continue
return best_threshold, best_score
def optimize_thresholds(self, predictions: torch.Tensor, targets: torch.Tensor) -> Dict[str, float]:
"""
Find optimal thresholds for all binary biomarkers
Args:
predictions: Model predictions [batch_size, total_output_size]
targets: True targets [batch_size, total_output_size]
Returns:
Dictionary mapping biomarker names to optimal thresholds
"""
# Convert to numpy
if isinstance(predictions, torch.Tensor):
predictions = predictions.detach().cpu().numpy()
if isinstance(targets, torch.Tensor):
targets = targets.detach().cpu().numpy()
optimal_thresholds = {}
for biomarker in self.biomarker_config.binary_biomarkers:
layout = self.tensor_layout[biomarker.name]
pred_logits = predictions[:, layout.start_idx]
pred_probs = 1 / (1 + np.exp(-pred_logits)) # Sigmoid
true_labels = targets[:, layout.start_idx].astype(int)
# Skip if all labels are the same (no positive or negative examples)
if len(np.unique(true_labels)) < 2:
optimal_thresholds[biomarker.name] = self.fallback_threshold
continue
# Find optimal threshold
best_threshold, best_score = self.find_optimal_threshold(
pred_probs, true_labels, self.optimization_metric
)
optimal_thresholds[biomarker.name] = best_threshold
# Log the optimization result
logger.info(
" %s: threshold=%.3f, %s=%.3f",
biomarker.name,
best_threshold,
self.optimization_metric,
best_score,
)
return optimal_thresholds
def update_optimal_thresholds(self, predictions: torch.Tensor, targets: torch.Tensor):
"""Update optimal thresholds based on current predictions and targets"""
if self.threshold_optimization:
logger.info("Optimizing thresholds...")
self.optimal_thresholds = self.optimize_thresholds(predictions, targets)
else:
# Use fallback threshold for all biomarkers
for biomarker in self.biomarker_config.binary_biomarkers:
self.optimal_thresholds[biomarker.name] = self.fallback_threshold
def calculate_binary_metrics(self, predictions: torch.Tensor, targets: torch.Tensor,
use_optimal_thresholds: bool = True) -> Dict[str, Dict[str, float]]:
"""Calculate metrics for binary classification tasks"""
metrics = {}
# Convert to numpy
if isinstance(predictions, torch.Tensor):
predictions = predictions.detach().cpu().numpy()
if isinstance(targets, torch.Tensor):
targets = targets.detach().cpu().numpy()
for biomarker in self.biomarker_config.binary_biomarkers:
layout = self.tensor_layout[biomarker.name]
pred_logits = predictions[:, layout.start_idx]
pred_probs = 1 / (1 + np.exp(-pred_logits)) # Sigmoid
true_labels = targets[:, layout.start_idx].astype(int)
# AUROC (threshold-independent)
try:
from sklearn.metrics import roc_auc_score
auroc = roc_auc_score(true_labels, pred_probs)
except (ValueError, ImportError):
auroc = 0.0
# Get threshold for this biomarker
if use_optimal_thresholds and biomarker.name in self.optimal_thresholds:
threshold = self.optimal_thresholds[biomarker.name]
else:
threshold = self.fallback_threshold
# Predictions with threshold
pred_labels = (pred_probs > threshold).astype(int)
# Basic metrics
accuracy = (pred_labels == true_labels).mean()
# Confusion matrix components
true_positives = np.sum((pred_labels == 1) & (true_labels == 1))
true_negatives = np.sum((pred_labels == 0) & (true_labels == 0))
false_positives = np.sum((pred_labels == 1) & (true_labels == 0))
false_negatives = np.sum((pred_labels == 0) & (true_labels == 1))
# Sensitivity and Specificity
sensitivity = true_positives / (true_positives + false_negatives) if (true_positives + false_negatives) > 0 else 0.0
specificity = true_negatives / (true_negatives + false_positives) if (true_negatives + false_positives) > 0 else 0.0
# F1 Score
try:
from sklearn.metrics import f1_score
f1 = f1_score(true_labels, pred_labels, zero_division=0.0)
except ImportError:
precision = true_positives / (true_positives + false_positives) if (true_positives + false_positives) > 0 else 0.0
recall = sensitivity
f1 = 2 * (precision * recall) / (precision + recall + 1e-8)
metrics[biomarker.name] = {
'auroc': float(auroc),
'accuracy': float(accuracy),
'sensitivity': float(sensitivity),
'specificity': float(specificity),
'f1': float(f1),
'threshold': float(threshold), # Include threshold used
'true_positives': int(true_positives),
'true_negatives': int(true_negatives),
'false_positives': int(false_positives),
'false_negatives': int(false_negatives)
}
return metrics
def calculate_multiclass_metrics(self, predictions: torch.Tensor, targets: torch.Tensor) -> Dict[str, Dict[str, Any]]:
"""Calculate metrics for multiclass classification tasks"""
metrics = {}
# Convert to numpy
if isinstance(predictions, torch.Tensor):
predictions = predictions.detach().cpu().numpy()
if isinstance(targets, torch.Tensor):
targets = targets.detach().cpu().numpy()
for biomarker in self.biomarker_config.multiclass_biomarkers:
layout = self.tensor_layout[biomarker.name]
pred_logits = predictions[:, layout.start_idx:layout.end_idx]
# Numerically stable softmax
max_logits = np.max(pred_logits, axis=1, keepdims=True)
exp_logits = np.exp(pred_logits - max_logits)
pred_probs = exp_logits / (np.sum(exp_logits, axis=1, keepdims=True) + 1e-12)
target_one_hot = targets[:, layout.start_idx:layout.end_idx]
# Get predicted and true classes
pred_classes = np.argmax(pred_probs, axis=1)
true_classes = np.argmax(target_one_hot, axis=1)
# Overall accuracy
accuracy = (pred_classes == true_classes).mean()
# Multi-class AUROC
try:
from sklearn.metrics import roc_auc_score
auroc = roc_auc_score(target_one_hot, pred_probs, multi_class='ovr', average='macro')
except (ValueError, ImportError):
auroc = 0.0
metrics[biomarker.name] = {
'accuracy': float(accuracy),
'auroc': float(auroc)
}
return metrics
def calculate_regression_metrics(self, predictions: torch.Tensor, targets: torch.Tensor) -> Dict[str, Dict[str, float]]:
"""Calculate metrics for regression tasks"""
metrics = {}
# Convert to numpy
if isinstance(predictions, torch.Tensor):
predictions = predictions.detach().cpu().numpy()
if isinstance(targets, torch.Tensor):
targets = targets.detach().cpu().numpy()
for biomarker in self.biomarker_config.continuous_biomarkers:
layout = self.tensor_layout[biomarker.name]
pred_values = predictions[:, layout.start_idx]
true_values = targets[:, layout.start_idx]
# Denormalize if needed
if biomarker.normalization == "min_max":
pred_denorm = pred_values * (biomarker.max_value - biomarker.min_value) + biomarker.min_value
true_denorm = true_values * (biomarker.max_value - biomarker.min_value) + biomarker.min_value
else:
pred_denorm = pred_values
true_denorm = true_values
# Calculate metrics
mse = np.mean((pred_denorm - true_denorm) ** 2)
mae = np.mean(np.abs(pred_denorm - true_denorm))
# R² score
ss_res = np.sum((true_denorm - pred_denorm) ** 2)
ss_tot = np.sum((true_denorm - np.mean(true_denorm)) ** 2)
r2 = 1 - (ss_res / (ss_tot + 1e-8))
metrics[biomarker.name] = {
'mse': float(mse),
'mae': float(mae),
'r2': float(r2)
}
return metrics
def calculate_all_metrics(self, predictions: torch.Tensor, targets: torch.Tensor) -> Dict[str, Any]:
"""Calculate all metrics"""
all_metrics = {}
# Binary metrics
if self.biomarker_config.binary_biomarkers:
binary_metrics = self.calculate_binary_metrics(predictions, targets)
all_metrics.update(binary_metrics)
# Multiclass metrics
if self.biomarker_config.multiclass_biomarkers:
multiclass_metrics = self.calculate_multiclass_metrics(predictions, targets)
all_metrics.update(multiclass_metrics)
# Regression metrics
if self.biomarker_config.continuous_biomarkers:
regression_metrics = self.calculate_regression_metrics(predictions, targets)
all_metrics.update(regression_metrics)
# Calculate both average and median AUROC for comprehensive monitoring
auroc_values = []
for biomarker_name, metrics in all_metrics.items():
if isinstance(metrics, dict) and 'auroc' in metrics:
auroc_values.append(metrics['auroc'])
if auroc_values:
all_metrics['average_auroc'] = float(np.mean(auroc_values))
all_metrics['median_auroc'] = float(np.median(auroc_values))
else:
all_metrics['average_auroc'] = 0.0
all_metrics['median_auroc'] = 0.0
return all_metrics
|