-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualization.py
More file actions
235 lines (187 loc) · 8.73 KB
/
Copy pathvisualization.py
File metadata and controls
235 lines (187 loc) · 8.73 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
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
import matplotlib.pyplot as plt
import matplotlib
from typing import List, Dict, Optional
import numpy as np
# Cấu hình font hỗ trợ tiếng Việt
matplotlib.rcParams['font.family'] = ['DejaVu Sans', 'Arial', 'sans-serif']
plt.rcParams['axes.unicode_minus'] = False
def plot_entropy_comparison(sources: List[Dict], figsize: tuple = (10, 6)) -> plt.Figure:
if not sources:
return None
fig, ax = plt.subplots(figsize=figsize)
names = [src['name'] for src in sources]
entropies = [src['entropy'] for src in sources]
max_entropies = [src['max_entropy'] for src in sources]
x = np.arange(len(names))
width = 0.35
# Vẽ cột entropy thực tế và entropy max
bars1 = ax.bar(x - width/2, entropies, width, label='Entropy thực tế',
color='#3498db', edgecolor='white', linewidth=1)
bars2 = ax.bar(x + width/2, max_entropies, width, label='Entropy tối đa',
color='#e74c3c', alpha=0.7, edgecolor='white', linewidth=1)
# Thêm giá trị lên đầu cột
for bar, val in zip(bars1, entropies):
ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.1,
f'{val:.3f}', ha='center', va='bottom', fontsize=10, fontweight='bold')
for bar, val in zip(bars2, max_entropies):
ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.1,
f'{val:.3f}', ha='center', va='bottom', fontsize=10)
ax.set_xlabel('Nguồn tin', fontsize=12)
ax.set_ylabel('Entropy (bit/ký hiệu)', fontsize=12)
ax.set_title('So sánh Entropy giữa các nguồn tin', fontsize=14, fontweight='bold')
ax.set_xticks(x)
ax.set_xticklabels(names, rotation=15, ha='right')
ax.legend(loc='upper right')
ax.grid(axis='y', alpha=0.3)
# Đặt giới hạn y để có khoảng trống cho labels
max_val = max(max_entropies) if max_entropies else 1
ax.set_ylim(0, max_val * 1.2)
plt.tight_layout()
return fig
def plot_efficiency_comparison(sources: List[Dict], figsize: tuple = (10, 6)) -> plt.Figure:
if not sources:
return None
fig, ax = plt.subplots(figsize=figsize)
names = [src['name'] for src in sources]
efficiencies = [src['efficiency'] for src in sources]
redundancies = [src['redundancy'] for src in sources]
x = np.arange(len(names))
# Stacked bar chart
bars1 = ax.bar(x, efficiencies, label='Hiệu suất (%)',
color='#2ecc71', edgecolor='white', linewidth=1)
bars2 = ax.bar(x, redundancies, bottom=efficiencies,
label='Độ dư tương đối (%)', color='#e67e22', alpha=0.7,
edgecolor='white', linewidth=1)
# Thêm labels
for bar, val in zip(bars1, efficiencies):
if val > 5: # Chỉ hiển thị nếu đủ lớn
ax.text(bar.get_x() + bar.get_width()/2, val/2,
f'{val:.1f}%', ha='center', va='center',
fontsize=10, fontweight='bold', color='white')
ax.set_xlabel('Nguồn tin', fontsize=12)
ax.set_ylabel('Phần trăm (%)', fontsize=12)
ax.set_title('Hiệu suất sử dụng Entropy', fontsize=14, fontweight='bold')
ax.set_xticks(x)
ax.set_xticklabels(names, rotation=15, ha='right')
ax.legend(loc='upper right')
ax.set_ylim(0, 110)
ax.axhline(y=100, color='gray', linestyle='--', alpha=0.5)
plt.tight_layout()
return fig
def plot_probability_distribution(source: Dict, top_n: int = 20,
figsize: tuple = (12, 6)) -> plt.Figure:
if not source or not source.get('probabilities'):
return None
fig, ax = plt.subplots(figsize=figsize)
probs = source['probabilities']
# Lấy top N ký tự có xác suất cao nhất
sorted_items = sorted(probs.items(), key=lambda x: x[1], reverse=True)[:top_n]
chars = [item[0] for item in sorted_items]
values = [item[1] for item in sorted_items]
# Tạo màu gradient
colors = plt.cm.Blues(np.linspace(0.4, 0.9, len(chars)))
bars = ax.bar(range(len(chars)), values, color=colors, edgecolor='white', linewidth=1)
# Format labels cho ký tự đặc biệt
display_chars = []
for c in chars:
if c == ' ':
display_chars.append('[space]')
elif c == '\n':
display_chars.append('[newline]')
elif c == '\t':
display_chars.append('[tab]')
else:
display_chars.append(c)
ax.set_xlabel('Ký tự', fontsize=12)
ax.set_ylabel('Xác suất', fontsize=12)
ax.set_title(f'Phân phối xác suất - {source["name"]}', fontsize=14, fontweight='bold')
ax.set_xticks(range(len(chars)))
ax.set_xticklabels(display_chars, rotation=45, ha='right', fontsize=9)
ax.grid(axis='y', alpha=0.3)
# Thêm giá trị lên đầu cột
for bar, val in zip(bars, values):
ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.005,
f'{val:.3f}', ha='center', va='bottom', fontsize=8, rotation=45)
plt.tight_layout()
return fig
def plot_all_comparisons(sources: List[Dict], figsize: tuple = (14, 10)) -> plt.Figure:
if not sources:
return None
fig, axes = plt.subplots(2, 2, figsize=figsize)
# Biểu đồ 1: So sánh entropy
names = [src['name'] for src in sources]
entropies = [src['entropy'] for src in sources]
max_entropies = [src['max_entropy'] for src in sources]
x = np.arange(len(names))
width = 0.35
axes[0, 0].bar(x - width/2, entropies, width, label='Entropy', color='#3498db')
axes[0, 0].bar(x + width/2, max_entropies, width, label='H_max', color='#e74c3c', alpha=0.7)
axes[0, 0].set_title('So sánh Entropy', fontweight='bold')
axes[0, 0].set_xticks(x)
axes[0, 0].set_xticklabels(names, rotation=15, ha='right', fontsize=9)
axes[0, 0].legend()
axes[0, 0].grid(axis='y', alpha=0.3)
# Biểu đồ 2: Hiệu suất
efficiencies = [src['efficiency'] for src in sources]
colors = ['#2ecc71' if e >= 80 else '#f39c12' if e >= 50 else '#e74c3c'
for e in efficiencies]
axes[0, 1].bar(x, efficiencies, color=colors, edgecolor='white')
axes[0, 1].axhline(y=100, color='gray', linestyle='--', alpha=0.5)
axes[0, 1].set_title('Hiệu suất sử dụng (%)', fontweight='bold')
axes[0, 1].set_xticks(x)
axes[0, 1].set_xticklabels(names, rotation=15, ha='right', fontsize=9)
axes[0, 1].set_ylim(0, 110)
axes[0, 1].grid(axis='y', alpha=0.3)
# Biểu đồ 3: Số ký hiệu
num_symbols = [src['num_symbols'] for src in sources]
axes[1, 0].bar(x, num_symbols, color='#9b59b6', edgecolor='white')
axes[1, 0].set_title('Số ký hiệu khác nhau', fontweight='bold')
axes[1, 0].set_xticks(x)
axes[1, 0].set_xticklabels(names, rotation=15, ha='right', fontsize=9)
axes[1, 0].grid(axis='y', alpha=0.3)
# Biểu đồ 4: Độ dư thừa
redundancies = [src['redundancy'] for src in sources]
axes[1, 1].bar(x, redundancies, color='#e67e22', edgecolor='white')
axes[1, 1].set_title('Độ dư tương đối (%)', fontweight='bold')
axes[1, 1].set_xticks(x)
axes[1, 1].set_xticklabels(names, rotation=15, ha='right', fontsize=9)
axes[1, 1].set_ylim(0, max(redundancies) * 1.2 if redundancies else 100)
axes[1, 1].grid(axis='y', alpha=0.3)
plt.suptitle('Phân tích Entropy các nguồn tin', fontsize=16, fontweight='bold')
plt.tight_layout()
return fig
# Demo
if __name__ == "__main__":
# Dữ liệu mẫu để test
sample_sources = [
{
"name": "Tiếng Việt",
"entropy": 4.2,
"max_entropy": 5.0,
"efficiency": 84.0,
"redundancy": 16.0,
"num_symbols": 32,
"probabilities": {"a": 0.12, "n": 0.08, "t": 0.07, "e": 0.06}
},
{
"name": "Tiếng Anh",
"entropy": 4.0,
"max_entropy": 4.7,
"efficiency": 85.1,
"redundancy": 14.9,
"num_symbols": 26,
"probabilities": {"e": 0.13, "t": 0.09, "a": 0.08, "o": 0.07}
},
{
"name": "Nhị phân",
"entropy": 7.8,
"max_entropy": 8.0,
"efficiency": 97.5,
"redundancy": 2.5,
"num_symbols": 256,
"probabilities": {}
}
]
# Test vẽ biểu đồ
fig = plot_all_comparisons(sample_sources)
plt.show()