File size: 4,062 Bytes
d5f2660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""UI helper functions for Gradio components"""

import gradio as gr
from typing import Tuple, List, Any


def hide_all_results() -> Tuple:
    """Hide all result display components"""
    return (
        gr.update(visible=False),  # result_display
        gr.update(visible=False),  # html_display
        gr.update(visible=False),  # result_info
        gr.update(visible=False),  # seg_nav_controls
        gr.update(visible=False),  # action_buttons
    )


def hide_viewer_components() -> Tuple:
    """Hide image viewer components"""
    return (
        gr.update(visible=False),  # image_filename
        gr.update(visible=False),  # viewer_image
        gr.update(visible=False),  # nav_controls
    )


def show_viewer_with_image(filename: str, image_path: str, counter: str) -> Tuple:
    """Show viewer with image loaded"""
    return (
        gr.update(value=f"📄 {filename}", visible=True),  # image_filename
        gr.update(value=image_path, visible=True),        # viewer_image
        gr.update(visible=True),                          # nav_controls
        counter,                                          # image_counter
    )


def create_empty_state() -> Tuple:
    """Create empty state for all components"""
    return (
        "",  # dataset_info
        *hide_viewer_components(),
        "",  # image_counter
        gr.update(visible=False),  # action_btn
        *hide_all_results(),
        [],  # current_images
        0,   # current_image_idx
        "",  # current_dataset
        "",  # current_category
        None,  # current_result_image
        "",  # current_result_text
    )


def format_classification_result(result: dict) -> str:
    """Format classification result as text"""
    if not result or not result.get('top_prediction'):
        return "❌ Classification failed"
    
    top_pred = result['top_prediction']
    info_text = (
        f"🎯 CLASSIFICATION RESULT\n\n"
        f"Prediction: {top_pred['class_name']}\n"
        f"Confidence: {top_pred['confidence']*100:.2f}%\n\n"
        f"All Predictions:\n"
    )
    info_text += "\n".join(
        f"  • {pred['class_name']}: {pred['confidence']*100:.2f}%" 
        for pred in result['all_predictions']
    )
    
    if result.get('note'):
        info_text += f"\n\n⚠️ {result['note']}"
    
    return info_text


def format_detection_result(result: dict) -> str:
    """Format detection result as text"""
    if not result or not result.get('annotated_image'):
        return "❌ Detection failed"
    
    total = result['total_detections']
    info_text = f"🎯 DETECTION RESULT\n\nTotal Detections: {total}\n\n"
    
    if result['predictions']:
        info_text += "Detected Objects:\n"
        info_text += "\n".join(
            f"  {i}. {pred['class_name']}: {pred['confidence']*100:.1f}%" 
            for i, pred in enumerate(result['predictions'], 1)
        )
    
    return info_text


def format_segmentation_result(result: dict) -> str:
    """Format segmentation result as text"""
    dice_scores = result['dice_scores']
    avg_dice = result['average_dice']
    vol_analysis = result.get('volumetric_analysis', {})
    spatial = result.get('spatial_analysis', {})
    
    info_text = f"🎯 SEGMENTATION RESULT\n\n"
    info_text += f"📊 Dice Scores:\n  Average: {avg_dice:.4f}\n"
    for k, v in dice_scores.items():
        info_text += f"  • {k}: {v:.4f}\n"
    
    if vol_analysis:
        info_text += f"\n📐 Volumetric Analysis:\n"
        info_text += f"  Whole Tumor: {vol_analysis['whole_tumor_volume_cm3']:.2f} cm³\n"
        info_text += f"  Tumor Core: {vol_analysis['tumor_core_volume_cm3']:.2f} cm³\n"
        info_text += f"  Enhancing Tumor: {vol_analysis['enhancing_tumor_volume_cm3']:.2f} cm³\n"
    
    if spatial and spatial.get('center_of_mass'):
        com = spatial['center_of_mass']
        info_text += f"\n📍 Tumor Location:\n"
        info_text += f"  Sagittal: {com['sagittal']:.1f}, Coronal: {com['coronal']:.1f}, Axial: {com['axial']:.1f}\n"
    
    return info_text