File size: 7,382 Bytes
6e99b69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from sklearn.manifold import TSNE

class ResultVisualizer:
    @staticmethod
    def plot_misconception_distribution(predictions: pd.Series):
        """Plot distribution of misconceptions"""
        fig = px.pie(
            names=predictions.value_counts().index,
            values=predictions.value_counts().values,
            title="Distribution of Mathematical Misconceptions",
            hole=0.4  # Make it a donut chart
        )
        return fig

    @staticmethod
    def plot_confidence_scores(probabilities: np.ndarray, classes: list):
        """Plot confidence scores for predictions"""
        fig = go.Figure()

        for i, class_name in enumerate(classes):
            fig.add_trace(go.Box(
                y=probabilities[:, i],
                name=class_name,
                boxpoints='all',
                jitter=0.3,
                pointpos=-1.8
            ))

        fig.update_layout(
            title="Prediction Confidence Scores by Class",
            yaxis_title="Confidence Score",
            showlegend=True
        )
        return fig

    @staticmethod
    def plot_feature_importance(feature_importance: dict, top_n: int = 10):
        """Plot feature importance"""
        sorted_features = dict(sorted(
            feature_importance.items(),
            key=lambda x: x[1],
            reverse=True
        )[:top_n])

        fig = px.bar(
            x=list(sorted_features.keys()),
            y=list(sorted_features.values()),
            title=f"Top {top_n} Important Features",
            labels={'x': 'Features', 'y': 'Importance Score'}
        )
        fig.update_xaxes(tickangle=45)
        return fig

    @staticmethod
    def plot_topic_distribution(topic_distributions: pd.DataFrame, topic_keywords: list):
        """Plot topic distribution heatmap"""
        topic_cols = [col for col in topic_distributions.columns if col.startswith('topic_')]
        if not topic_cols:
            return None

        # Create topic labels with keywords
        labels = [f"Topic {i}\n({keywords})" for i, keywords in enumerate(topic_keywords)]

        # Create heatmap
        fig = go.Figure(data=go.Heatmap(
            z=topic_distributions[topic_cols].values.T,
            x=np.arange(len(topic_distributions)),
            y=labels,
            colorscale='Viridis',
            hoverongaps=False
        ))

        fig.update_layout(
            title="Topic Distribution Across Responses",
            xaxis_title="Response Index",
            yaxis_title="Topics",
            height=400
        )
        return fig

    @staticmethod
    def plot_embedding_clusters(embeddings: np.ndarray, labels: np.ndarray):
        """Plot word embeddings clusters using t-SNE"""
        if len(embeddings) < 4:
            return None  # Return None if not enough samples

        try:
            # Calculate perplexity based on dataset size
            # Perplexity should be smaller than the number of samples
            perplexity = min(30, len(embeddings) // 2)

            # Apply t-SNE for dimensionality reduction
            tsne = TSNE(
                n_components=2,
                random_state=42,
                perplexity=max(2, perplexity),  # Minimum perplexity of 2
                n_iter=1000,
                init='pca'  # Use PCA initialization for better stability
            )
            embeddings_2d = tsne.fit_transform(embeddings)

            # Create DataFrame for plotting
            df_plot = pd.DataFrame({
                'x': embeddings_2d[:, 0],
                'y': embeddings_2d[:, 1],
                'label': labels
            })

            # Create scatter plot
            fig = px.scatter(
                df_plot,
                x='x',
                y='y',
                color='label',
                title="Response Embeddings Clustering",
                labels={'label': 'Misconception Type'},
                hover_data=['label']
            )

            fig.update_layout(
                xaxis_title="t-SNE Dimension 1",
                yaxis_title="t-SNE Dimension 2"
            )
            return fig
        except Exception as e:
            print(f"Error in embedding visualization: {str(e)}")
            return None

    @staticmethod
    def plot_training_history(history: list):
        """Plot model training history"""
        if not history:
            return None

        # Extract metrics from history
        dates = [entry['timestamp'] for entry in history]
        samples = [entry['samples'] for entry in history]
        cv_scores = [entry['metrics'].get('cv_mean', None) for entry in history]
        cv_scores = [score for score in cv_scores if score is not None]

        # Create timeline figure
        fig = go.Figure()

        # Add sample size trace
        fig.add_trace(go.Scatter(
            x=dates,
            y=samples,
            name='Training Samples',
            line=dict(color='blue'),
            yaxis='y'
        ))

        # Add CV scores if available
        if cv_scores:
            fig.add_trace(go.Scatter(
                x=dates[:len(cv_scores)],
                y=cv_scores,
                name='CV Accuracy',
                line=dict(color='green'),
                yaxis='y2'
            ))

        # Update layout
        fig.update_layout(
            title="Model Training History",
            xaxis_title="Training Date",
            yaxis_title="Number of Samples",
            yaxis2=dict(
                title="CV Accuracy",
                overlaying='y',
                side='right'
            ),
            showlegend=True
        )

        return fig

    @staticmethod
    def plot_model_metrics(metrics: dict):
        """Plot model performance metrics"""
        figures = []

        # Cross-validation scores if available
        if 'cv_scores' in metrics and len(metrics['cv_scores']) > 0:
            cv_fig = go.Figure()
            cv_fig.add_trace(go.Box(
                y=metrics['cv_scores'],
                name='Cross-validation Scores',
                boxpoints='all'
            ))
            cv_fig.update_layout(
                title=f"Cross-validation Scores (Mean: {metrics['cv_mean']:.3f} ± {metrics['cv_std']:.3f})",
                yaxis_title="Accuracy",
                showlegend=False
            )
            figures.append(cv_fig)

        # Confusion matrix
        if 'confusion_matrix' in metrics:
            conf_matrix = np.array(metrics['confusion_matrix'])
            classes = list(metrics['classification_report'].keys())[:-3]  # Exclude 'accuracy', 'macro avg', 'weighted avg'

            heatmap = go.Figure(data=go.Heatmap(
                z=conf_matrix,
                x=classes,
                y=classes,
                colorscale='RdBu',
                text=conf_matrix.astype(str),
                texttemplate="%{text}",
                textfont={"size": 12},
                hoverongaps=False
            ))
            heatmap.update_layout(
                title="Confusion Matrix",
                xaxis_title="Predicted Label",
                yaxis_title="True Label"
            )
            figures.append(heatmap)

        return tuple(figures) if len(figures) > 1 else figures[0] if figures else None