-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparison_visualizer.py
More file actions
129 lines (107 loc) · 4.85 KB
/
Copy pathcomparison_visualizer.py
File metadata and controls
129 lines (107 loc) · 4.85 KB
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
"""
Visualization utilities for RQ3 information loss analysis.
"""
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from typing import Dict
class ComparisonVisualizer:
"""Visualize comparison results between full text and summary models."""
def __init__(self, results: Dict):
"""
Initialize visualizer.
Parameters:
-----------
results : Dict
Results from Information LossAnalyzer
"""
self.results = results
def plot_information_loss_comparison(self, save_path: str = None):
"""Plot information loss across methods and ratios."""
data = []
for method, ratio_results in self.results.items():
for ratio, result in ratio_results.items():
info_loss = result['comparison']['information_loss']
data.append({
'Method': method,
'Ratio': f"{ratio*100:.0f}%",
'Information Loss (%)': info_loss['information_loss'] * 100
})
df = pd.DataFrame(data)
plt.figure(figsize=(12, 6))
sns.barplot(data=df, x='Ratio', y='Information Loss (%)', hue='Method')
plt.title('Information Loss by Summarization Method and Ratio', fontsize=14, fontweight='bold')
plt.ylabel('Information Loss (%)', fontsize=12)
plt.xlabel('Compression Ratio', fontsize=12)
plt.legend(title='Method')
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.show()
def plot_metric_comparison(self, save_path: str = None):
"""Plot multiple metrics comparison."""
fig, axes = plt.subplots(2, 2, figsize=(15, 10))
axes = axes.flatten()
metrics = ['topic_count_preservation', 'topic_overlap_score',
'vocab_preservation', 'coherence_preservation']
titles = ['Topic Count Preservation', 'Topic Overlap',
'Vocabulary Preservation', 'Coherence Preservation']
for idx, (metric, title) in enumerate(zip(metrics, titles)):
data = []
for method, ratio_results in self.results.items():
for ratio, result in ratio_results.items():
info_loss = result['comparison']['information_loss']
data.append({
'Method': method,
'Ratio': f"{ratio*100:.0f}%",
'Value': info_loss[metric] * 100
})
df = pd.DataFrame(data)
ax = axes[idx]
sns.barplot(data=df, x='Ratio', y='Value', hue='Method', ax=ax)
ax.set_title(title, fontsize=11, fontweight='bold')
ax.set_ylabel('Preservation (%)')
ax.set_xlabel('Ratio')
if idx == 0:
ax.legend(title='Method')
else:
ax.legend().remove()
plt.suptitle('Preservation Metrics Comparison', fontsize=14, fontweight='bold', y=1.00)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.show()
def plot_emerging_topics_comparison(self, save_path: str = None):
"""Compare emerging topics detection."""
data = []
for method, ratio_results in self.results.items():
for ratio, result in ratio_results.items():
data.append({
'Method': method,
'Ratio': f"{ratio*100:.0f}%",
'Full Text': result['full_model']['emerging_topics'],
'Summary': result['summary_model']['emerging_topics']
})
df = pd.DataFrame(data)
# Skip visualization if no emerging topics were detected
if df['Full Text'].sum() == 0 and df['Summary'].sum() == 0:
print("⊘ Skipping emerging topics visualization (no emerging topics detected)")
return
# Create separate plots for Full Text and Summary
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))
# Plot Full Text
sns.barplot(data=df, x='Ratio', y='Full Text', hue='Method', ax=ax1, palette='Set2')
ax1.set_title('Full Text - Emerging Topics', fontsize=12, fontweight='bold')
ax1.set_ylabel('Number of Emerging Topics')
ax1.legend(title='Method')
# Plot Summary
sns.barplot(data=df, x='Ratio', y='Summary', hue='Method', ax=ax2, palette='Set2')
ax2.set_title('Summary - Emerging Topics', fontsize=12, fontweight='bold')
ax2.set_ylabel('Number of Emerging Topics')
ax2.legend(title='Method')
plt.suptitle('Emerging Topics Detection Comparison', fontsize=14, fontweight='bold', y=1.02)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.show()