File size: 3,738 Bytes
ee594f0 | 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 | import re
import os
with open('Train_CMTLNet_Step2.py', 'r') as f:
code = f.read()
target = """ # Forward pass (now receives risk_scores)
(out_cls_list_UFE, out_cls_labels_UFE,
out_cls_list_UCFC, out_cls_labels_UCFC, risk_scores) = model(combined_input, label_list, condition)
# --- CALCULATE COX LOSS ---
# Mask out patients who don't have survival data (time == 0)
valid_mask = combined_times > 0
if valid_mask.sum() >= 2: # Cox loss needs at least a pair to rank
loss_surv = cox_ph_loss(risk_scores[valid_mask], combined_times[valid_mask], combined_events[valid_mask])
else:
loss_surv = torch.tensor(0.0, device=risk_scores.device, requires_grad=True)
# Classification losses
loss_cls_U1 = cross_entropy_loss(out_cls_list_UFE[0], out_cls_labels_UFE[0])
loss_cls_U2 = cross_entropy_loss(out_cls_list_UFE[1], out_cls_labels_UFE[1])
loss_cls_U3 = cross_entropy_loss(out_cls_list_UFE[2], out_cls_labels_UFE[2])
loss_cls_1 = cross_entropy_loss(out_cls_list_UCFC[0], out_cls_labels_UCFC[0])
loss_cls_2 = cross_entropy_loss(out_cls_list_UCFC[1], out_cls_labels_UCFC[1])
loss_cls_3 = cross_entropy_loss(out_cls_list_UCFC[2], out_cls_labels_UCFC[2])
# Total loss (weighing survival by 0.5 so it doesn't overpower classification)
total_loss = loss_cls_U1 + loss_cls_U2 + loss_cls_U3 + loss_cls_1 + loss_cls_2 + loss_cls_3 + (0.5 * loss_surv)
# Backward pass
optimizer.zero_grad()
total_loss.backward()
optimizer.step()"""
replacement = """ # Forward pass with AMP
with autocast():
(out_cls_list_UFE, out_cls_labels_UFE,
out_cls_list_UCFC, out_cls_labels_UCFC, risk_scores) = model(combined_input, label_list, condition)
# --- CALCULATE COX LOSS ---
valid_mask = combined_times > 0
if valid_mask.sum() >= 2:
loss_surv = cox_ph_loss(risk_scores[valid_mask], combined_times[valid_mask], combined_events[valid_mask])
else:
loss_surv = torch.tensor(0.0, device=risk_scores.device, requires_grad=True)
loss_cls_U1 = cross_entropy_loss(out_cls_list_UFE[0], out_cls_labels_UFE[0])
loss_cls_U2 = cross_entropy_loss(out_cls_list_UFE[1], out_cls_labels_UFE[1])
loss_cls_U3 = cross_entropy_loss(out_cls_list_UFE[2], out_cls_labels_UFE[2])
loss_cls_1 = cross_entropy_loss(out_cls_list_UCFC[0], out_cls_labels_UCFC[0])
loss_cls_2 = cross_entropy_loss(out_cls_list_UCFC[1], out_cls_labels_UCFC[1])
loss_cls_3 = cross_entropy_loss(out_cls_list_UCFC[2], out_cls_labels_UCFC[2])
total_loss = loss_cls_U1 + loss_cls_U2 + loss_cls_U3 + loss_cls_1 + loss_cls_2 + loss_cls_3 + (0.5 * loss_surv)
# Backward pass with AMP
optimizer.zero_grad()
scaler.scale(total_loss).backward()
scaler.step(optimizer)
scaler.update()"""
if target in code:
code = code.replace(target, replacement)
else:
print("Target 3 not found.")
target2 = """ # Define save path
save_path = os.path.join(config.data_dir, 'results', str(save_name), f'Fold_{fold}')
if not os.path.exists(save_path):
os.makedirs(save_path)"""
if target2 in code:
code = code.replace(target2, "")
else:
print("Target 4 not found.")
with open('Train_CMTLNet_Step2.py', 'w') as f:
f.write(code)
print("Part 3 replacements done.")
|