File size: 6,062 Bytes
82a3b34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Heatmap Component



Gradio component for displaying attack vulnerability heatmaps.

"""

import logging
from typing import Any, List, Optional

import gradio as gr
import plotly.graph_objects as go

from dashboard.schemas import HeatmapData
from dashboard.utils import log_dashboard_event

logger = logging.getLogger(__name__)


def create_heatmap_chart() -> gr.Plot:
    """

    Create heatmap chart component.

    

    Returns:

        Plot component

    """
    plot = gr.Plot(
        label="Attack Vulnerability Heatmap",
    )
    
    return plot


def update_heatmap_chart(

    heatmap_data: Optional[HeatmapData],

) -> Any:
    """

    Update heatmap chart with data.

    

    Args:

        heatmap_data: Heatmap data

        

    Returns:

        Plotly figure

    """
    if heatmap_data is None or not heatmap_data.attack_types:
        # Return empty chart
        fig = go.Figure()
        fig.update_layout(
            title="No Data Available",
            xaxis=dict(title="Metrics"),
            yaxis=dict(title="Attack Types"),
        )
        return fig
    
    log_dashboard_event("DASHBOARD_VIEW_HEATMAP", run_id=heatmap_data.run_id)
    
    # Create heatmap
    fig = go.Figure(
        data=go.Heatmap(
            z=heatmap_data.values,
            x=heatmap_data.metrics,
            y=heatmap_data.attack_types,
            colorscale="RdYlGn_r",  # Red (high) to Green (low) - reversed
            zmin=0,
            zmax=1,
            colorbar=dict(
                title=dict(text="Metric Value", side="right"),
            ),
            hovertemplate=(
                "<b>Attack:</b> %{y}<br>"
                "<b>Metric:</b> %{x}<br>"
                "<b>Value:</b> %{z:.3f}<extra></extra>"
            ),
        )
    )
    
    fig.update_layout(
        title="Attack Vulnerability Heatmap",
        xaxis=dict(title="Metrics"),
        yaxis=dict(
            title="Attack Types",
            autorange="reversed",  # Top to bottom
        ),
        height=500,
        width=700,
    )
    
    return fig


def create_comparison_heatmap(

    heatmap_data_list: List[HeatmapData],

    model_names: List[str],

) -> Any:
    """

    Create comparison heatmap across multiple runs.

    

    Args:

        heatmap_data_list: List of heatmap data

        model_names: List of model names

        

    Returns:

        Plotly figure

    """
    if not heatmap_data_list:
        # Return empty chart
        fig = go.Figure()
        fig.update_layout(title="No Data Available")
        return fig
    
    # For comparison, we'll show the average vulnerability
    import numpy as np
    
    all_attack_types = set()
    for hd in heatmap_data_list:
        all_attack_types.update(hd.attack_types)
    
    attack_types = sorted(list(all_attack_types))
    metrics = heatmap_data_list[0].metrics if heatmap_data_list else []
    
    # Calculate average values
    avg_values = []
    for attack_type in attack_types:
        row = []
        for metric in metrics:
            values = []
            for hd in heatmap_data_list:
                if attack_type in hd.attack_types and metric in hd.metrics:
                    idx = hd.attack_types.index(attack_type)
                    m_idx = hd.metrics.index(metric)
                    values.append(hd.values[idx][m_idx])
            
            if values:
                row.append(np.mean(values))
            else:
                row.append(0.0)
        avg_values.append(row)
    
    fig = go.Figure(
        data=go.Heatmap(
            z=avg_values,
            x=metrics,
            y=attack_types,
            colorscale="RdYlGn_r",
            zmin=0,
            zmax=1,
            colorbar=dict(
                title=dict(text="Avg Value", side="right"),
            ),
        )
    )
    
    fig.update_layout(
        title="Average Attack Vulnerability Across Models",
        xaxis=dict(title="Metrics"),
        yaxis=dict(
            title="Attack Types",
            autorange="reversed",
        ),
        height=500,
        width=700,
    )
    
    return fig


class HeatmapChart:
    """

    Heatmap chart component with state management.

    """
    
    def __init__(self):
        """Initialize heatmap chart."""
        self._current_data: Optional[HeatmapData] = None
    
    def set_data(self, data: HeatmapData) -> None:
        """

        Set heatmap data.

        

        Args:

            data: Heatmap data

        """
        self._current_data = data
    
    def get_data(self) -> Optional[HeatmapData]:
        """

        Get current heatmap data.

        

        Returns:

            Current heatmap data or None

        """
        return self._current_data
    
    def get_figure(self) -> Any:
        """

        Get Plotly figure.

        

        Returns:

            Plotly figure

        """
        return update_heatmap_chart(self._current_data)
    
    @staticmethod
    def create_empty() -> Any:
        """

        Create empty heatmap chart.

        

        Returns:

            Empty Plotly figure

        """
        fig = go.Figure()
        fig.update_layout(
            title="Select a run to view vulnerability heatmap",
            xaxis=dict(title="Metrics"),
            yaxis=dict(title="Attack Types"),
            height=400,
            width=600,
        )
        return fig


def get_heatmap_tooltip() -> str:
    """

    Get heatmap tooltip explanation.

    

    Returns:

        Tooltip string

    """
    return (
        "Heatmap shows mean metric values for each attack type.\n\n"
        "Color Scale:\n"
        "• Red (1.0) = High vulnerability (bad)\n"
        "• Yellow (0.5) = Medium vulnerability\n"
        "• Green (0.0) = Low vulnerability (good)\n\n"
        "Formula: M_ij = mean(metric_j | attack_i)"
    )