-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.py
More file actions
1266 lines (1031 loc) · 49.2 KB
/
visualizer.py
File metadata and controls
1266 lines (1031 loc) · 49.2 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
可视化器(Visualizer)
本模块实现了贝叶斯超参数优化结果的可视化功能,包括:
- 收敛曲线可视化
- 参数分布和性能热力图
- 多目标优化的帕累托前沿可视化
- 参数重要性图表
- 优化过程动态可视化
"""
from typing import Dict, List, Any, Optional, Tuple, Union
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.patches import Patch
# 配置中文字体支持和禁用字体警告
import warnings
import platform
# 禁用字体相关的警告
warnings.filterwarnings('ignore', category=UserWarning, message='.*Glyph.*missing from font.*')
warnings.filterwarnings('ignore', category=UserWarning, message='.*findfont.*')
# 强制配置中文字体
def setup_chinese_font():
"""强制设置中文字体"""
try:
import matplotlib.font_manager as fm
# 清除matplotlib字体缓存
try:
fm._rebuild()
except:
pass
system = platform.system()
# 根据系统设置字体
if system == 'Windows':
# Windows系统优先使用微软雅黑
font_candidates = ['Microsoft YaHei', 'SimHei', 'SimSun', 'Arial Unicode MS']
elif system == 'Darwin': # macOS
font_candidates = ['PingFang SC', 'Heiti SC', 'STHeiti', 'Arial Unicode MS']
else: # Linux
font_candidates = ['WenQuanYi Micro Hei', 'DejaVu Sans', 'Arial Unicode MS']
# 检查哪些字体可用
available_fonts = [f.name for f in fm.fontManager.ttflist]
working_font = None
for font in font_candidates:
if font in available_fonts:
working_font = font
break
if working_font:
# 强制设置字体
plt.rcParams['font.sans-serif'] = [working_font] + font_candidates
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['axes.unicode_minus'] = False
print(f"字体配置成功: {working_font}")
else:
print("警告: 未找到合适的中文字体,可能显示为方框")
plt.rcParams['font.sans-serif'] = font_candidates
plt.rcParams['axes.unicode_minus'] = False
except Exception as e:
print(f"字体配置失败: {e}")
# 备用配置
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False
# 立即执行字体配置
setup_chinese_font()
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
from scipy.spatial.distance import pdist, squareform
from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import warnings
from datetime import datetime
import os
from pathlib import Path
from autodl_core import OptimizationHistory, OptimizationResult, ParameterSpace
from result_analyzer import ResultAnalyzer, ParameterSensitivityResult
class Visualizer:
"""
贝叶斯优化结果可视化器
提供多种可视化功能,包括收敛曲线、参数分布、性能热力图、
帕累托前沿和参数重要性分析等。
"""
def __init__(self, optimization_history: OptimizationHistory,
parameter_space: Optional[ParameterSpace] = None,
result_analyzer: Optional[ResultAnalyzer] = None):
"""
初始化可视化器
Args:
optimization_history: 优化历史记录
parameter_space: 参数空间定义(可选)
result_analyzer: 结果分析器(可选,如果不提供会自动创建)
"""
self.history = optimization_history
self.parameter_space = parameter_space
if result_analyzer is None:
self.analyzer = ResultAnalyzer(optimization_history, parameter_space)
else:
self.analyzer = result_analyzer
# 设置中文字体和绘图风格
self._setup_plotting_style()
# 创建结果DataFrame
self.results_df = self._create_results_dataframe()
def _setup_plotting_style(self):
"""设置绘图风格和中文字体"""
# 测试中文字体是否可用
self._test_chinese_font()
# 设置seaborn风格
sns.set_style("whitegrid")
sns.set_context("talk")
# 设置高质量绘图参数
plt.rcParams.update({
"savefig.dpi": 300,
"figure.dpi": 120,
"lines.antialiased": True,
"patch.antialiased": True,
"axes.linewidth": 1.2,
"lines.linewidth": 2.0,
"legend.frameon": True,
"legend.framealpha": 0.85,
"pdf.fonttype": 42,
"ps.fonttype": 42
})
def _test_chinese_font(self):
"""测试中文字体是否正确配置"""
try:
import matplotlib.font_manager as fm
# 获取当前字体
current_font = plt.rcParams['font.sans-serif'][0]
# 检查字体是否存在
font_list = [f.name for f in fm.fontManager.ttflist]
if current_font not in font_list:
print(f"警告: 字体 '{current_font}' 未找到,可能导致中文显示为方框")
print("可用的中文字体:")
chinese_fonts = [f for f in font_list if any(keyword in f for keyword in ['YaHei', 'SimHei', 'SimSun', 'PingFang', 'Heiti'])]
for font in chinese_fonts[:5]: # 只显示前5个
print(f" - {font}")
else:
print(f"字体配置成功: {current_font}")
except Exception as e:
print(f"字体测试失败: {e}")
def _create_results_dataframe(self) -> pd.DataFrame:
"""创建结果DataFrame用于可视化"""
if not self.history.results:
return pd.DataFrame()
data = []
for result in self.history.results:
row = {
'iteration': result.iteration,
'objective_value': result.objective_value,
'evaluation_time': result.evaluation_time,
'timestamp': result.timestamp,
'has_error': result.error_info is not None
}
# 添加参数值
for param_name, param_value in result.parameters.items():
row[f'param_{param_name}'] = param_value
# 添加其他指标
if result.metrics:
for metric_name, metric_value in result.metrics.items():
row[f'metric_{metric_name}'] = metric_value
data.append(row)
return pd.DataFrame(data)
def plot_convergence_curve(self, save_path: Optional[str] = None,
show_confidence_interval: bool = True,
smooth: bool = False, window_size: int = 5) -> None:
"""
绘制收敛曲线
Args:
save_path: 保存路径
show_confidence_interval: 是否显示置信区间
smooth: 是否平滑曲线
window_size: 平滑窗口大小
"""
if len(self.results_df) == 0:
warnings.warn("没有优化结果数据,无法绘制收敛曲线")
return
# 获取收敛曲线数据
convergence_curve = self.history.get_convergence_curve()
iterations = list(range(1, len(convergence_curve) + 1))
# 创建图形
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 10))
# 上图:收敛曲线
if smooth and len(convergence_curve) > window_size:
# 应用滑动平均平滑
smoothed_curve = pd.Series(convergence_curve).rolling(
window=window_size, center=True, min_periods=1
).mean().values
ax1.plot(iterations, smoothed_curve, 'b-', linewidth=2.5,
label=f'Smoothed Convergence (window={window_size})', alpha=0.8)
ax1.plot(iterations, convergence_curve, 'b-', linewidth=1,
alpha=0.3, label='Original Convergence')
else:
ax1.plot(iterations, convergence_curve, 'b-', linewidth=2.5,
label='Convergence Curve', marker='o', markersize=3)
# 标记最佳点
best_iteration = self.history.best_result.iteration if self.history.best_result else 1
best_value = self.history.get_best_objective_value() or 0
ax1.scatter([best_iteration], [best_value], color='red', s=100,
zorder=5, label=f'Best Result (Iter {best_iteration})')
# 添加置信区间(基于历史方差)
if show_confidence_interval and len(convergence_curve) > 10:
# 计算滑动标准差
obj_values = self.results_df['objective_value'].values
rolling_std = pd.Series(obj_values).rolling(
window=min(10, len(obj_values)), min_periods=1
).std().values
upper_bound = np.array(convergence_curve) + rolling_std * 0.5
lower_bound = np.array(convergence_curve) - rolling_std * 0.5
ax1.fill_between(iterations, lower_bound, upper_bound,
alpha=0.2, color='blue', label='Confidence Interval')
ax1.set_xlabel('Iteration')
ax1.set_ylabel('Objective Value (AUROC)')
ax1.set_title(f'Bayesian Optimization Convergence - {self.history.task_type} Task')
ax1.legend()
ax1.grid(True, alpha=0.3)
# 下图:每次迭代的目标函数值(散点图)
colors = ['red' if error else 'blue' for error in self.results_df['has_error']]
ax2.scatter(self.results_df['iteration'], self.results_df['objective_value'],
c=colors, alpha=0.6, s=50)
# 添加趋势线
z = np.polyfit(self.results_df['iteration'], self.results_df['objective_value'], 1)
p = np.poly1d(z)
ax2.plot(iterations, p(iterations), "r--", alpha=0.8, linewidth=1.5,
label=f'Trend Line (slope={z[0]:.4f})')
ax2.set_xlabel('Iteration')
ax2.set_ylabel('Objective Value')
ax2.set_title('Objective Value per Iteration')
ax2.legend()
ax2.grid(True, alpha=0.3)
# 添加统计信息文本框
stats_text = f"""Statistics:
Total Iterations: {len(convergence_curve)}
Best Value: {best_value:.4f}
Final Value: {convergence_curve[-1]:.4f}
Improvement: {convergence_curve[-1] - convergence_curve[0]:.4f}
Success Rate: {(1 - self.results_df['has_error'].mean()):.2%}"""
ax1.text(0.02, 0.98, stats_text, transform=ax1.transAxes,
verticalalignment='top', bbox=dict(boxstyle='round',
facecolor='wheat', alpha=0.8), fontsize=10)
plt.tight_layout()
if save_path:
self._ensure_dir(save_path)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def plot_parameter_distributions(self, save_path: Optional[str] = None,
max_params: int = 12) -> None:
"""
绘制参数分布图
Args:
save_path: 保存路径
max_params: 最大显示参数数量
"""
param_columns = [col for col in self.results_df.columns if col.startswith('param_')]
if len(param_columns) == 0:
warnings.warn("没有参数数据,无法绘制参数分布")
return
# 限制参数数量
if len(param_columns) > max_params:
# 根据参数重要性排序
importance_ranking = self.analyzer.get_parameter_importance_ranking()
important_params = [f'param_{name}' for name, _ in importance_ranking[:max_params]]
param_columns = [col for col in param_columns if col in important_params]
# 计算子图布局
n_params = len(param_columns)
n_cols = min(4, n_params)
n_rows = (n_params + n_cols - 1) // n_cols
fig, axes = plt.subplots(n_rows, n_cols, figsize=(4*n_cols, 3*n_rows))
if n_params == 1:
axes = [axes]
elif n_rows == 1:
axes = axes.reshape(1, -1)
# 获取目标函数值用于颜色映射
obj_values = self.results_df['objective_value'].values
for i, param_col in enumerate(param_columns):
row = i // n_cols
col = i % n_cols
ax = axes[row, col] if n_rows > 1 else axes[col]
param_name = param_col.replace('param_', '')
param_values = self.results_df[param_col].values
# 判断参数类型
if self.parameter_space and param_name in self.parameter_space.parameters:
param_config = self.parameter_space.parameters[param_name]
is_categorical = param_config.param_type.value == 'categorical'
else:
# 自动判断
is_categorical = (self.results_df[param_col].dtype == 'object' or
self.results_df[param_col].nunique() <= 10)
if is_categorical:
# 分类参数:箱线图
df_plot = pd.DataFrame({
'param': param_values,
'objective': obj_values
})
sns.boxplot(data=df_plot, x='param', y='objective', ax=ax)
# 修复警告:先设置刻度位置,再设置标签旋转
ax.tick_params(axis='x', rotation=45)
else:
# 连续参数:散点图
scatter = ax.scatter(param_values, obj_values, c=obj_values,
cmap='viridis', alpha=0.6, s=50)
# 添加趋势线
try:
z = np.polyfit(param_values, obj_values, 1)
p = np.poly1d(z)
x_trend = np.linspace(param_values.min(), param_values.max(), 100)
ax.plot(x_trend, p(x_trend), "r--", alpha=0.8, linewidth=1.5)
except:
pass
ax.set_xlabel(param_name)
ax.set_ylabel('Objective Value')
ax.set_title(f'{param_name} Distribution')
ax.grid(True, alpha=0.3)
# 隐藏多余的子图
for i in range(n_params, n_rows * n_cols):
row = i // n_cols
col = i % n_cols
if n_rows > 1:
axes[row, col].set_visible(False)
else:
axes[col].set_visible(False)
plt.suptitle(f'Parameter Distribution Analysis - {self.history.task_type} Task', fontsize=16)
plt.tight_layout()
if save_path:
self._ensure_dir(save_path)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def plot_parameter_correlation_heatmap(self, save_path: Optional[str] = None,
method: str = 'pearson') -> None:
"""
绘制参数相关性热力图
Args:
save_path: 保存路径
method: 相关性计算方法 ('pearson', 'spearman', 'kendall')
"""
correlation_matrix = self.analyzer.analyze_parameter_correlations()
if correlation_matrix.empty:
warnings.warn("没有足够的参数数据,无法绘制相关性热力图")
return
# 创建图形
fig, ax = plt.subplots(figsize=(12, 10))
# 创建自定义颜色映射
colors = ['#d73027', '#f46d43', '#fdae61', '#fee08b', '#ffffff',
'#e6f598', '#abdda4', '#66c2a5', '#3288bd']
n_bins = 100
cmap = LinearSegmentedColormap.from_list('custom', colors, N=n_bins)
# 绘制热力图
mask = np.triu(np.ones_like(correlation_matrix, dtype=bool)) # 只显示下三角
sns.heatmap(correlation_matrix, mask=mask, annot=True, cmap=cmap, center=0,
square=True, linewidths=0.5, cbar_kws={"shrink": 0.8}, ax=ax,
fmt='.3f', annot_kws={'size': 8})
ax.set_title(f'Parameter Correlation Heatmap ({method.capitalize()})', fontsize=16)
ax.set_xlabel('Parameters')
ax.set_ylabel('Parameters')
plt.tight_layout()
if save_path:
self._ensure_dir(save_path)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def plot_parameter_importance(self, save_path: Optional[str] = None,
top_k: int = 15) -> None:
"""
绘制参数重要性图表
Args:
save_path: 保存路径
top_k: 显示前k个重要参数
"""
sensitivity_results = self.analyzer.analyze_parameter_sensitivity()
if not sensitivity_results:
warnings.warn("没有参数敏感性分析结果,无法绘制重要性图表")
return
# 取前k个重要参数
top_results = sensitivity_results[:min(top_k, len(sensitivity_results))]
# 准备数据
param_names = [result.parameter_name for result in top_results]
sensitivity_scores = [result.sensitivity_score for result in top_results]
correlation_coeffs = [abs(result.correlation_coefficient) for result in top_results]
mutual_info_scores = [result.mutual_information for result in top_results]
# 创建子图
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))
# 左图:参数重要性条形图
y_pos = np.arange(len(param_names))
bars = ax1.barh(y_pos, sensitivity_scores, alpha=0.8,
color=plt.cm.viridis(np.linspace(0, 1, len(param_names))))
ax1.set_yticks(y_pos)
ax1.set_yticklabels(param_names)
ax1.set_xlabel('Sensitivity Score')
ax1.set_title('Parameter Importance Ranking')
ax1.grid(True, alpha=0.3, axis='x')
# 添加数值标签
for i, (bar, score) in enumerate(zip(bars, sensitivity_scores)):
ax1.text(score + 0.01, bar.get_y() + bar.get_height()/2,
f'{score:.3f}', va='center', fontsize=9)
# 右图:多指标对比雷达图
if len(top_results) <= 8: # 只有参数不太多时才绘制雷达图
# 准备雷达图数据
angles = np.linspace(0, 2 * np.pi, len(param_names), endpoint=False).tolist()
angles += angles[:1] # 闭合
# 归一化数据
sensitivity_norm = np.array(sensitivity_scores) / max(sensitivity_scores) if max(sensitivity_scores) > 0 else np.zeros_like(sensitivity_scores)
correlation_norm = np.array(correlation_coeffs) / max(correlation_coeffs) if max(correlation_coeffs) > 0 else np.zeros_like(correlation_coeffs)
mutual_info_norm = np.array(mutual_info_scores) / max(mutual_info_scores) if max(mutual_info_scores) > 0 else np.zeros_like(mutual_info_scores)
# 闭合数据
sensitivity_norm = np.concatenate([sensitivity_norm, [sensitivity_norm[0]]])
correlation_norm = np.concatenate([correlation_norm, [correlation_norm[0]]])
mutual_info_norm = np.concatenate([mutual_info_norm, [mutual_info_norm[0]]])
ax2 = plt.subplot(122, projection='polar')
ax2.plot(angles, sensitivity_norm, 'o-', linewidth=2, label='Sensitivity Score', alpha=0.8)
ax2.fill(angles, sensitivity_norm, alpha=0.25)
ax2.plot(angles, correlation_norm, 's-', linewidth=2, label='Correlation Coeff', alpha=0.8)
ax2.plot(angles, mutual_info_norm, '^-', linewidth=2, label='Mutual Information', alpha=0.8)
ax2.set_xticks(angles[:-1])
ax2.set_xticklabels(param_names, fontsize=10)
ax2.set_ylim(0, 1)
ax2.set_title('Multi-metric Parameter Importance Comparison', pad=20)
ax2.legend(loc='upper right', bbox_to_anchor=(1.3, 1.0))
ax2.grid(True)
else:
# 参数太多时,绘制多指标条形图
x = np.arange(len(param_names))
width = 0.25
ax2.bar(x - width, sensitivity_scores, width, label='Sensitivity Score', alpha=0.8)
ax2.bar(x, correlation_coeffs, width, label='Correlation Coeff', alpha=0.8)
ax2.bar(x + width, mutual_info_scores, width, label='Mutual Information', alpha=0.8)
ax2.set_xlabel('Parameters')
ax2.set_ylabel('Score')
ax2.set_title('Multi-metric Parameter Importance Comparison')
ax2.set_xticks(x)
ax2.set_xticklabels(param_names, rotation=45, ha='right')
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.tight_layout()
if save_path:
self._ensure_dir(save_path)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def plot_performance_heatmap(self, save_path: Optional[str] = None,
param1: Optional[str] = None,
param2: Optional[str] = None) -> None:
"""
绘制参数性能热力图
Args:
save_path: 保存路径
param1: 第一个参数名(如果不指定,自动选择最重要的参数)
param2: 第二个参数名(如果不指定,自动选择第二重要的参数)
"""
# 自动选择参数
if param1 is None or param2 is None:
importance_ranking = self.analyzer.get_parameter_importance_ranking()
if len(importance_ranking) < 2:
warnings.warn("参数数量不足,无法绘制二维热力图")
return
if param1 is None:
param1 = importance_ranking[0][0]
if param2 is None:
param2 = importance_ranking[1][0]
# 检查参数是否存在
param1_col = f'param_{param1}'
param2_col = f'param_{param2}'
if param1_col not in self.results_df.columns or param2_col not in self.results_df.columns:
warnings.warn(f"参数 {param1} 或 {param2} 不存在")
return
# 获取数据
x_values = self.results_df[param1_col].values
y_values = self.results_df[param2_col].values
z_values = self.results_df['objective_value'].values
# 判断参数类型
param1_is_categorical = (self.results_df[param1_col].dtype == 'object' or
self.results_df[param1_col].nunique() <= 10)
param2_is_categorical = (self.results_df[param2_col].dtype == 'object' or
self.results_df[param2_col].nunique() <= 10)
fig, ax = plt.subplots(figsize=(12, 8))
if param1_is_categorical and param2_is_categorical:
# 两个都是分类参数:创建分组热力图
df_pivot = self.results_df.pivot_table(
values='objective_value',
index=param2_col,
columns=param1_col,
aggfunc='mean'
)
sns.heatmap(df_pivot, annot=True, cmap='viridis', ax=ax,
fmt='.3f', cbar_kws={'label': 'Average Objective Value'})
elif not param1_is_categorical and not param2_is_categorical:
# 两个都是连续参数:插值热力图
from scipy.interpolate import griddata
# 创建网格
xi = np.linspace(x_values.min(), x_values.max(), 50)
yi = np.linspace(y_values.min(), y_values.max(), 50)
xi, yi = np.meshgrid(xi, yi)
# 插值
zi = griddata((x_values, y_values), z_values, (xi, yi), method='cubic')
# 绘制热力图
im = ax.contourf(xi, yi, zi, levels=20, cmap='viridis', alpha=0.8)
# 添加原始数据点
scatter = ax.scatter(x_values, y_values, c=z_values, cmap='viridis',
s=50, edgecolors='white', linewidth=0.5)
# 添加颜色条
cbar = plt.colorbar(im, ax=ax)
cbar.set_label('Objective Value')
else:
# 一个分类一个连续:分组散点图
if param1_is_categorical:
cat_param, cont_param = param1, param2
cat_values, cont_values = x_values, y_values
else:
cat_param, cont_param = param2, param1
cat_values, cont_values = y_values, x_values
# 为每个类别绘制散点
unique_cats = np.unique(cat_values)
colors = plt.cm.viridis(np.linspace(0, 1, len(unique_cats)))
for i, cat in enumerate(unique_cats):
mask = cat_values == cat
ax.scatter(cont_values[mask], z_values[mask],
c=[colors[i]], label=f'{cat_param}={cat}',
alpha=0.7, s=50)
ax.set_xlabel(cont_param)
ax.set_ylabel('Objective Value')
ax.legend()
ax.set_xlabel(param1)
ax.set_ylabel(param2)
ax.set_title(f'Parameter Performance Heatmap: {param1} vs {param2}')
plt.tight_layout()
if save_path:
self._ensure_dir(save_path)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def plot_pareto_frontier(self, save_path: Optional[str] = None,
objective1: str = 'AUROC',
objective2: str = 'AUPRC') -> None:
"""
绘制帕累托前沿(多目标优化)
Args:
save_path: 保存路径
objective1: 第一个目标函数名
objective2: 第二个目标函数名
"""
# 检查是否有多目标数据
obj1_col = f'metric_{objective1}'
obj2_col = f'metric_{objective2}'
if obj1_col not in self.results_df.columns or obj2_col not in self.results_df.columns:
warnings.warn(f"缺少目标函数数据: {objective1} 或 {objective2}")
return
# 获取目标函数值
obj1_values = self.results_df[obj1_col].values
obj2_values = self.results_df[obj2_col].values
# 计算帕累托前沿
pareto_indices = self._find_pareto_frontier(obj1_values, obj2_values)
# 创建图形
fig, ax = plt.subplots(figsize=(10, 8))
# 绘制所有点
ax.scatter(obj1_values, obj2_values, alpha=0.6, s=50,
color='lightblue', label='All Solutions')
# 绘制帕累托前沿点
pareto_obj1 = obj1_values[pareto_indices]
pareto_obj2 = obj2_values[pareto_indices]
ax.scatter(pareto_obj1, pareto_obj2, alpha=0.8, s=100,
color='red', label='Pareto Front', zorder=5)
# 连接帕累托前沿点
sorted_indices = np.argsort(pareto_obj1)
sorted_pareto_obj1 = pareto_obj1[sorted_indices]
sorted_pareto_obj2 = pareto_obj2[sorted_indices]
ax.plot(sorted_pareto_obj1, sorted_pareto_obj2, 'r--',
alpha=0.7, linewidth=2, zorder=4)
# 标记最佳点
best_idx = self.history.best_result.iteration - 1 if self.history.best_result else 0
if best_idx < len(obj1_values):
ax.scatter([obj1_values[best_idx]], [obj2_values[best_idx]],
color='gold', s=150, marker='*',
label='Best Solution', zorder=6, edgecolors='black')
ax.set_xlabel(f'{objective1}')
ax.set_ylabel(f'{objective2}')
ax.set_title(f'Pareto Front Analysis: {objective1} vs {objective2}')
ax.legend()
ax.grid(True, alpha=0.3)
# 添加统计信息
stats_text = f"""Pareto Front Statistics:
Front Solutions: {len(pareto_indices)}
Total Solutions: {len(obj1_values)}
Front Ratio: {len(pareto_indices)/len(obj1_values):.2%}
{objective1} Range: [{pareto_obj1.min():.3f}, {pareto_obj1.max():.3f}]
{objective2} Range: [{pareto_obj2.min():.3f}, {pareto_obj2.max():.3f}]"""
ax.text(0.02, 0.98, stats_text, transform=ax.transAxes,
verticalalignment='top', bbox=dict(boxstyle='round',
facecolor='wheat', alpha=0.8), fontsize=10)
plt.tight_layout()
if save_path:
self._ensure_dir(save_path)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def _find_pareto_frontier(self, obj1: np.ndarray, obj2: np.ndarray,
maximize: bool = True) -> np.ndarray:
"""
找到帕累托前沿点的索引
Args:
obj1: 第一个目标函数值数组
obj2: 第二个目标函数值数组
maximize: 是否最大化目标函数
Returns:
帕累托前沿点的索引数组
"""
# 组合目标函数值
objectives = np.column_stack([obj1, obj2])
if not maximize:
objectives = -objectives
# 找到帕累托前沿
pareto_indices = []
n_points = len(objectives)
for i in range(n_points):
is_pareto = True
for j in range(n_points):
if i != j:
# 检查点j是否支配点i
if (objectives[j] >= objectives[i]).all() and (objectives[j] > objectives[i]).any():
is_pareto = False
break
if is_pareto:
pareto_indices.append(i)
return np.array(pareto_indices)
def plot_optimization_landscape_3d(self, save_path: Optional[str] = None,
param1: Optional[str] = None,
param2: Optional[str] = None) -> None:
"""
绘制3D优化景观图
Args:
save_path: 保存路径
param1: 第一个参数名
param2: 第二个参数名
"""
# 自动选择参数
if param1 is None or param2 is None:
importance_ranking = self.analyzer.get_parameter_importance_ranking()
if len(importance_ranking) < 2:
warnings.warn("参数数量不足,无法绘制3D景观图")
return
if param1 is None:
param1 = importance_ranking[0][0]
if param2 is None:
param2 = importance_ranking[1][0]
# 检查参数是否存在且为连续型
param1_col = f'param_{param1}'
param2_col = f'param_{param2}'
if param1_col not in self.results_df.columns or param2_col not in self.results_df.columns:
warnings.warn(f"参数 {param1} 或 {param2} 不存在")
return
# 检查是否为连续型参数
if (self.results_df[param1_col].dtype == 'object' or
self.results_df[param2_col].dtype == 'object'):
warnings.warn("3D景观图只支持连续型参数")
return
# 获取数据
x_values = self.results_df[param1_col].values
y_values = self.results_df[param2_col].values
z_values = self.results_df['objective_value'].values
# 创建3D图形
fig = plt.figure(figsize=(14, 10))
ax = fig.add_subplot(111, projection='3d')
# 创建网格进行插值
from scipy.interpolate import griddata
xi = np.linspace(x_values.min(), x_values.max(), 30)
yi = np.linspace(y_values.min(), y_values.max(), 30)
xi, yi = np.meshgrid(xi, yi)
# 插值
zi = griddata((x_values, y_values), z_values, (xi, yi), method='cubic')
# 绘制表面
surf = ax.plot_surface(xi, yi, zi, cmap='viridis', alpha=0.7,
linewidth=0, antialiased=True)
# 添加原始数据点
ax.scatter(x_values, y_values, z_values, c=z_values, cmap='viridis',
s=50, alpha=0.8, edgecolors='white', linewidth=0.5)
# 标记最佳点
if self.history.best_result:
best_params = self.history.best_result.parameters
if param1 in best_params and param2 in best_params:
best_x = best_params[param1]
best_y = best_params[param2]
best_z = self.history.best_result.objective_value
ax.scatter([best_x], [best_y], [best_z], color='red', s=200,
marker='*', label='Best Solution')
# 设置标签和标题
ax.set_xlabel(param1)
ax.set_ylabel(param2)
ax.set_zlabel('Objective Value')
ax.set_title(f'3D Optimization Landscape: {param1} vs {param2}')
# 添加颜色条
fig.colorbar(surf, ax=ax, shrink=0.5, aspect=20, label='Objective Value')
if save_path:
self._ensure_dir(save_path)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def plot_parameter_evolution(self, save_path: Optional[str] = None,
params: Optional[List[str]] = None,
max_params: int = 6) -> None:
"""
绘制参数演化图(参数值随迭代次数的变化)
Args:
save_path: 保存路径
params: 要显示的参数列表
max_params: 最大显示参数数量
"""
if params is None:
# 自动选择重要参数
importance_ranking = self.analyzer.get_parameter_importance_ranking()
params = [name for name, _ in importance_ranking[:max_params]]
if not params:
warnings.warn("没有可显示的参数")
return
# 计算子图布局
n_params = len(params)
n_cols = min(3, n_params)
n_rows = (n_params + n_cols - 1) // n_cols
fig, axes = plt.subplots(n_rows, n_cols, figsize=(5*n_cols, 4*n_rows))
if n_params == 1:
axes = [axes]
elif n_rows == 1:
axes = axes.reshape(1, -1)
iterations = self.results_df['iteration'].values
obj_values = self.results_df['objective_value'].values
for i, param_name in enumerate(params):
row = i // n_cols
col = i % n_cols
ax = axes[row, col] if n_rows > 1 else axes[col]
param_col = f'param_{param_name}'
if param_col not in self.results_df.columns:
continue
param_values = self.results_df[param_col].values
# 判断参数类型
is_categorical = (self.results_df[param_col].dtype == 'object' or
self.results_df[param_col].nunique() <= 10)
if is_categorical:
# 分类参数:使用不同颜色表示不同类别
unique_values = self.results_df[param_col].unique()
colors = plt.cm.tab10(np.linspace(0, 1, len(unique_values)))
for j, value in enumerate(unique_values):
mask = param_values == value
ax.scatter(iterations[mask], obj_values[mask],
c=[colors[j]], label=str(value), alpha=0.7, s=30)
ax.legend(title=param_name, bbox_to_anchor=(1.05, 1), loc='upper left')
ax.set_ylabel('Objective Value')
else:
# 连续参数:颜色映射表示参数值
scatter = ax.scatter(iterations, obj_values, c=param_values,
cmap='viridis', alpha=0.7, s=30)
# 添加颜色条
cbar = plt.colorbar(scatter, ax=ax)
cbar.set_label(param_name)
ax.set_ylabel('Objective Value')
ax.set_xlabel('Iteration')
ax.set_title(f'{param_name} Evolution')
ax.grid(True, alpha=0.3)
# 隐藏多余的子图
for i in range(n_params, n_rows * n_cols):
row = i // n_cols
col = i % n_cols
if n_rows > 1:
axes[row, col].set_visible(False)
else:
axes[col].set_visible(False)
plt.suptitle(f'Parameter Evolution Analysis - {self.history.task_type} Task', fontsize=16)
plt.tight_layout()
if save_path:
self._ensure_dir(save_path)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
plt.close()
else:
plt.show()
def create_interactive_dashboard(self, save_path: Optional[str] = None) -> None:
"""
创建交互式仪表板(使用Plotly)
Args:
save_path: 保存路径(HTML文件)
"""
try:
# 创建子图
fig = make_subplots(
rows=2, cols=2,
subplot_titles=('Convergence Curve', 'Parameter Importance', 'Parameter Distribution', 'Performance Scatter'),
specs=[[{"secondary_y": False}, {"type": "bar"}],
[{"type": "scatter"}, {"type": "scatter"}]]
)
# 1. 收敛曲线
convergence_curve = self.history.get_convergence_curve()
iterations = list(range(1, len(convergence_curve) + 1))
fig.add_trace(
go.Scatter(x=iterations, y=convergence_curve, mode='lines+markers',
name='Convergence Curve', line=dict(color='blue', width=2)),
row=1, col=1
)
# 2. 参数重要性
importance_ranking = self.analyzer.get_parameter_importance_ranking()
if importance_ranking:
top_params = importance_ranking[:10] # 前10个
param_names = [name for name, _ in top_params]
importance_scores = [score for _, score in top_params]
fig.add_trace(
go.Bar(x=param_names, y=importance_scores, name='参数重要性',
marker_color='viridis'),
row=1, col=2
)