amitaddi
feat: deploy bone tumor segmentation workstation
1bd13a5
Raw History Blame Contribute Delete
19.7 kB
"""
Publication Figure Generation.
Creates publication-ready figures:
- Box plots of Dice/IoU per model
- Radar charts across metrics
- Per-class performance heatmap
- Prompt sensitivity analysis
- Statistical significance indicators
Usage:
python -m src.analysis.figures --project_root . --results_dir results/runs
"""
import os
import json
import glob
import argparse
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import seaborn as sns
from pathlib import Path
from typing import Dict, List
# Publication style
plt.rcParams.update({
'font.family': 'sans-serif',
'font.sans-serif': ['Arial', 'DejaVu Sans'],
'font.size': 10,
'axes.titlesize': 12,
'axes.labelsize': 10,
'figure.dpi': 300,
'savefig.dpi': 300,
})
CLASS_NAMES = [
'GCT', 'M. Osteo.', 'Osteochondr.', 'Osteofibr.',
'Osteosarc.', 'Other BT', 'Other MT', 'Bone Cyst', 'Syn. Osteo.'
]
def plot_dice_boxplot(results_dict: Dict[str, List[float]],
save_path: str = None,
title: str = "Dice Score Distribution"):
"""
Box plot comparing Dice scores across models.
Args:
results_dict: Dict mapping model_name → list of per-sample Dice scores
save_path: Save path for figure
title: Figure title
"""
fig, ax = plt.subplots(figsize=(10, 6))
data = []
labels = []
for name, scores in results_dict.items():
data.append(scores)
labels.append(name)
# Color palette
colors = sns.color_palette("husl", len(data))
bp = ax.boxplot(data, labels=labels, patch_artist=True, showmeans=True,
meanprops=dict(marker='D', markerfacecolor='white', markersize=6))
for patch, color in zip(bp['boxes'], colors):
patch.set_facecolor(color)
patch.set_alpha(0.7)
ax.set_ylabel('Dice Score')
ax.set_title(title, fontweight='bold')
ax.set_ylim(0, 1.05)
ax.grid(axis='y', alpha=0.3)
# Add mean values as text
for i, (name, scores) in enumerate(results_dict.items()):
mean_val = np.mean(scores)
ax.text(i + 1, mean_val + 0.02, f'{mean_val:.3f}',
ha='center', fontsize=8, fontweight='bold')
plt.tight_layout()
if save_path:
os.makedirs(os.path.dirname(save_path), exist_ok=True)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def plot_perclass_heatmap(results_dict: Dict[str, Dict[int, float]],
save_path: str = None,
metric_name: str = "Dice"):
"""
Heatmap of per-class performance across models.
Args:
results_dict: Dict mapping model_name → {class_id: metric_value}
"""
models = list(results_dict.keys())
n_classes = len(CLASS_NAMES)
data = np.full((len(models), n_classes), np.nan)
for i, model in enumerate(models):
for cls_id, value in results_dict[model].items():
data[i, int(cls_id)] = value
fig, ax = plt.subplots(figsize=(12, max(4, len(models) * 0.8)))
sns.heatmap(data, annot=True, fmt='.3f', cmap='RdYlGn',
xticklabels=CLASS_NAMES, yticklabels=models,
vmin=0, vmax=1, ax=ax,
cbar_kws={'label': metric_name})
ax.set_title(f'Per-Class {metric_name} Score', fontweight='bold')
ax.set_xlabel('Tumor Type')
ax.set_ylabel('Model')
plt.tight_layout()
if save_path:
os.makedirs(os.path.dirname(save_path), exist_ok=True)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def plot_radar_chart(results_dict: Dict[str, Dict[str, float]],
metrics: List[str] = None,
save_path: str = None):
"""
Radar chart comparing models across multiple metrics.
Args:
results_dict: Dict mapping model_name → {metric_name: value}
metrics: List of metric names to include
"""
if metrics is None:
metrics = ['dice', 'iou', 'precision', 'recall']
angles = np.linspace(0, 2 * np.pi, len(metrics), endpoint=False).tolist()
angles += angles[:1]
fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(polar=True))
colors = sns.color_palette("husl", len(results_dict))
for (name, values), color in zip(results_dict.items(), colors):
data = [values.get(m, 0) for m in metrics]
data += data[:1]
ax.plot(angles, data, 'o-', linewidth=2, label=name, color=color)
ax.fill(angles, data, alpha=0.15, color=color)
ax.set_xticks(angles[:-1])
ax.set_xticklabels([m.upper() for m in metrics])
ax.set_ylim(0, 1)
ax.set_title('Model Comparison', fontweight='bold', pad=20)
ax.legend(loc='upper right', bbox_to_anchor=(1.3, 1.1))
plt.tight_layout()
if save_path:
os.makedirs(os.path.dirname(save_path), exist_ok=True)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def plot_prompt_sensitivity(box_expansions: List[float],
dice_scores: List[float],
save_path: str = None):
"""
Plot how bounding box expansion ratio affects SAM2 performance.
Args:
box_expansions: List of expansion ratios [0.0, 0.05, 0.1, 0.2]
dice_scores: Corresponding mean Dice scores
"""
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(box_expansions, dice_scores, 'o-', linewidth=2, markersize=8,
color='#2196F3', label='SAM2 (box prompt)')
ax.set_xlabel('Box Expansion Ratio')
ax.set_ylabel('Mean Dice Score')
ax.set_title('Prompt Sensitivity: Effect of Box Expansion', fontweight='bold')
ax.grid(True, alpha=0.3)
ax.legend()
# Annotate best
best_idx = np.argmax(dice_scores)
ax.annotate(f'Best: {dice_scores[best_idx]:.4f}',
xy=(box_expansions[best_idx], dice_scores[best_idx]),
xytext=(10, 10), textcoords='offset points',
fontsize=9, fontweight='bold',
arrowprops=dict(arrowstyle='->', color='red'))
plt.tight_layout()
if save_path:
os.makedirs(os.path.dirname(save_path), exist_ok=True)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def plot_fewshot_learning_curve(shots: List[int],
dice_scores: Dict[str, List[float]],
save_path: str = None):
"""
Plot few-shot learning curve: performance vs. number of shots.
Args:
shots: [1, 5, 10, ...]
dice_scores: Dict mapping model_name → list of Dice scores per shot count
"""
fig, ax = plt.subplots(figsize=(8, 5))
colors = sns.color_palette("husl", len(dice_scores))
for (name, scores), color in zip(dice_scores.items(), colors):
ax.plot(shots, scores, 'o-', linewidth=2, markersize=8,
label=name, color=color)
ax.set_xlabel('Number of Shots per Class')
ax.set_ylabel('Mean Dice Score')
ax.set_title('Few-Shot Learning Curve', fontweight='bold')
ax.set_xticks(shots)
ax.grid(True, alpha=0.3)
ax.legend()
plt.tight_layout()
if save_path:
os.makedirs(os.path.dirname(save_path), exist_ok=True)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def main():
parser = argparse.ArgumentParser(description='Generate publication figures')
parser.add_argument('--project_root', type=str, default='e:\\Amit Data\\BT')
parser.add_argument('--results_dir', type=str, default=None)
args = parser.parse_args()
if args.results_dir is None:
args.results_dir = os.path.join(args.project_root, 'results', 'runs')
figures_dir = os.path.join(args.project_root, 'results', 'figures')
os.makedirs(figures_dir, exist_ok=True)
# 1. Find all run directories
run_dirs = sorted(glob.glob(os.path.join(args.results_dir, '*')))
run_dirs = [d for d in run_dirs if os.path.isdir(d)]
if not run_dirs:
print(f"No runs found in {args.results_dir}")
return
# Chronologically filter run directories to keep only the latest of each type
from src.evaluate import filter_latest_runs, get_clean_model_name, load_run_results
run_dirs = filter_latest_runs(run_dirs)
print(f"Loading data from {len(run_dirs)} latest runs...")
# Load all results
all_runs_data = [load_run_results(d) for d in run_dirs]
# 2. Extract data for Dice boxplot
boxplot_data = {}
# Extract data for Heatmap
heatmap_data = {}
# Extract data for Radar Chart
radar_data = {}
# Extract few-shot data
fewshot_data = {}
for run_data in all_runs_data:
name = run_data['run_name']
if 'results' not in run_data:
continue
results = run_data['results']
if isinstance(results, dict):
# Check if it is a SAM2 zero-shot run
if any(k in results for k in ['box', 'center_point', 'random_3_points']):
for prompt_type, pt_results in results.items():
if isinstance(pt_results, dict) and 'per_sample' in pt_results:
clean_name = get_clean_model_name(name, prompt_type)
# Boxplot
dice_scores = [float(s['dice']) for s in pt_results['per_sample'] if 'dice' in s]
boxplot_data[clean_name] = dice_scores
# Radar
if 'overall' in pt_results:
overall = pt_results['overall']
radar_data[clean_name] = {
'dice': overall.get('dice', {}).get('mean', 0.0),
'iou': overall.get('iou', {}).get('mean', 0.0),
'precision': overall.get('precision', {}).get('mean', 0.0),
'recall': overall.get('recall', {}).get('mean', 0.0),
}
# Heatmap
if 'per_class' in pt_results:
heatmap_data[clean_name] = {}
for cls_id, cls_agg in pt_results['per_class'].items():
if 'dice' in cls_agg:
heatmap_data[clean_name][int(cls_id)] = cls_agg['dice']['mean']
# Check if it is a few-shot run
elif any(k.endswith('_shot') for k in results.keys()):
for shot_type, shot_results in results.items():
if isinstance(shot_results, dict) and 'per_sample' in shot_results:
clean_name = get_clean_model_name(name, shot_type)
# Boxplot
dice_scores = [float(s['dice']) for s in shot_results['per_sample'] if 'dice' in s]
boxplot_data[clean_name] = dice_scores
# Radar
if 'overall' in shot_results:
overall = shot_results['overall']
radar_data[clean_name] = {
'dice': overall.get('dice', {}).get('mean', 0.0),
'iou': overall.get('iou', {}).get('mean', 0.0),
'precision': overall.get('precision', {}).get('mean', 0.0),
'recall': overall.get('recall', {}).get('mean', 0.0),
}
# Save few-shot progression
shot_num = int(shot_type.replace('_shot', ''))
fewshot_data[shot_num] = overall.get('dice', {}).get('mean', 0.0)
# Heatmap
if 'per_class' in shot_results:
heatmap_data[clean_name] = {}
for cls_id, cls_agg in shot_results['per_class'].items():
if 'dice' in cls_agg:
heatmap_data[clean_name][int(cls_id)] = cls_agg['dice']['mean']
# Check if it is a SAM2 ablation or enhanced run
elif any(k in results for k in ['baseline_padded', 'iterative_3pass', 'iterative_postprocess', 'full_pipeline', 'enhanced']):
for config_name, config_results in results.items():
if isinstance(config_results, dict) and 'per_sample' in config_results:
clean_name = get_clean_model_name(name, config_name)
# Boxplot
dice_scores = [float(s['dice']) for s in config_results['per_sample'] if 'dice' in s]
boxplot_data[clean_name] = dice_scores
# Radar
if 'overall' in config_results:
overall = config_results['overall']
radar_data[clean_name] = {
'dice': overall.get('dice', {}).get('mean', 0.0),
'iou': overall.get('iou', {}).get('mean', 0.0),
'precision': overall.get('precision', {}).get('mean', 0.0),
'recall': overall.get('recall', {}).get('mean', 0.0),
}
# Heatmap
if 'per_class' in config_results:
heatmap_data[clean_name] = {}
for cls_id, cls_agg in config_results['per_class'].items():
if 'dice' in cls_agg:
heatmap_data[clean_name][int(cls_id)] = cls_agg['dice']['mean']
# Otherwise standard single results.json run (UNet, MedSAM, Fine-Tuned SAM2)
elif 'overall' in results:
clean_name = get_clean_model_name(name)
# Boxplot
if 'per_sample' in results:
dice_scores = [float(s['dice']) for s in results['per_sample'] if 'dice' in s]
boxplot_data[clean_name] = dice_scores
# Radar
overall = results['overall']
radar_data[clean_name] = {
'dice': overall.get('dice', {}).get('mean', 0.0),
'iou': overall.get('iou', {}).get('mean', 0.0),
'precision': overall.get('precision', {}).get('mean', 0.0),
'recall': overall.get('recall', {}).get('mean', 0.0),
}
# Heatmap
if 'per_class' in results:
heatmap_data[clean_name] = {}
for cls_id, cls_agg in results['per_class'].items():
if 'dice' in cls_agg:
heatmap_data[clean_name][int(cls_id)] = cls_agg['dice']['mean']
# 3. Generate figures
if boxplot_data:
preferred_order = [
'UNet Baseline',
'SAM2 (Zero-Shot Center Point)',
'SAM2 (Zero-Shot 3 Points)',
'SAM2 (Zero-Shot Box)',
'SAM2 (Padded Box)',
'SAM2 (Iterative Box)',
'SAM2 (Iterative + Morph Box)',
'SAM2 (Full Pipeline)',
'SAM2 (Enhanced Weak-Label)',
'MedSAM (Zero-Shot Box)',
'SAM2 (1-Shot Box)',
'SAM2 (5-Shot Box)',
'SAM2 (10-Shot Box)',
'SAM2 (Fine-Tuned Box)'
]
ordered_boxplot = {k: boxplot_data[k] for k in preferred_order if k in boxplot_data}
for k, v in boxplot_data.items():
if k not in ordered_boxplot:
ordered_boxplot[k] = v
plot_dice_boxplot(
ordered_boxplot,
save_path=os.path.join(figures_dir, 'dice_boxplot.png'),
title="BTXRD Bone Tumor Segmentation Dice Score Distribution"
)
print(f"Generated dice_boxplot.png")
if heatmap_data:
ordered_heatmap = {k: heatmap_data[k] for k in preferred_order if k in heatmap_data}
for k, v in heatmap_data.items():
if k not in ordered_heatmap:
ordered_heatmap[k] = v
plot_perclass_heatmap(
ordered_heatmap,
save_path=os.path.join(figures_dir, 'perclass_heatmap.png'),
metric_name="Dice"
)
print(f"Generated perclass_heatmap.png")
if radar_data:
radar_models = [
'UNet Baseline',
'SAM2 (Zero-Shot Box)',
'SAM2 (Full Pipeline)',
'SAM2 (Enhanced Weak-Label)',
'MedSAM (Zero-Shot Box)',
'SAM2 (Fine-Tuned Box)'
]
filtered_radar = {k: radar_data[k] for k in radar_models if k in radar_data}
if not filtered_radar:
filtered_radar = radar_data
plot_radar_chart(
filtered_radar,
metrics=['dice', 'iou', 'precision', 'recall'],
save_path=os.path.join(figures_dir, 'model_comparison_radar.png')
)
print(f"Generated model_comparison_radar.png")
if fewshot_data:
sorted_shots = sorted(fewshot_data.keys())
sorted_dice = [fewshot_data[s] for s in sorted_shots]
zero_shot_dice = radar_data.get('SAM2 (Zero-Shot Box)', {}).get('dice', None)
finetuned_dice = radar_data.get('SAM2 (Fine-Tuned Box)', {}).get('dice', None)
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(sorted_shots, sorted_dice, 'o-', linewidth=2.5, markersize=8, color='#4CAF50', label='Few-Shot SAM2 (box)')
if zero_shot_dice is not None:
ax.axhline(y=zero_shot_dice, color='#2196F3', linestyle='--', linewidth=1.5, label='Zero-Shot SAM2 (box)')
ax.plot([0] + sorted_shots, [zero_shot_dice] + sorted_dice, ':', color='#4CAF50', alpha=0.5)
if finetuned_dice is not None:
ax.axhline(y=finetuned_dice, color='#FF5722', linestyle='-.', linewidth=1.5, label='Fine-Tuned SAM2 (box)')
ax.set_xlabel('Number of Support Shots per Class')
ax.set_ylabel('Mean Dice Score')
ax.set_title('Few-Shot Learning Curve (SAM2 Hiera Tiny)', fontweight='bold')
ax.set_xticks([0] + sorted_shots)
ax.set_ylim(0.5, 0.8)
ax.grid(True, alpha=0.3)
ax.legend()
plt.tight_layout()
fewshot_path = os.path.join(figures_dir, 'fewshot_learning_curve.png')
plt.savefig(fewshot_path, dpi=300, bbox_inches='tight')
plt.close()
print(f"Generated fewshot_learning_curve.png")
print(f"\nAll publication figures successfully saved in: {figures_dir}")
if __name__ == '__main__':
main()