Spaces:
Runtime error
Runtime error
Download visualization.py from harrycool12/MISCONCEPTION: direct link, hf CLI and curl.
- Browser
- Download file 7.38 kB
-
https://huggingface.co/spaces/harrycool12/MISCONCEPTION/resolve/main/visualization.py
- Command line
-
hf download hf://spaces/harrycool12/MISCONCEPTION/visualization.py
-
curl -L -o visualization.py https://huggingface.co/spaces/harrycool12/MISCONCEPTION/resolve/main/visualization.py
7.38 kB
| 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: | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 |