import plotly.graph_objs as go import pandas as pd from sklearn.decomposition import PCA def create_feature_radar_chart(features): categories = ['振幅', '分布范围', '衰减速度', '反射次数'] values = [features[f'{cat}值'] for cat in categories] fig = go.Figure(data=go.Scatterpolar( r=values, theta=categories, fill='toself' )) fig.update_layout( polar=dict( radialaxis=dict(visible=True, range=[0, max(values)]) ), showlegend=False ) return fig.to_html(full_html=False) def create_pca_visualization(reports): features = ['振幅值', '分布范围值', '衰减速度值', '反射次数值'] X = pd.DataFrame([r.features for r in reports])[features] pca = PCA(n_components=2) pca_result = pca.fit_transform(X) df = pd.DataFrame(data=pca_result, columns=['PC1', 'PC2']) df['缺陷类型'] = [r.defect_type for r in reports] fig = go.Figure(data=go.Scatter( x=df['PC1'], y=df['PC2'], mode='markers', marker=dict( size=10, color=df['缺陷类型'].map({'空洞': 'red', '裂缝': 'blue'}), opacity=0.8 ), text=df['缺陷类型'] )) fig.update_layout( title='PCA of Radar Features', xaxis_title='Principal Component 1', yaxis_title='Principal Component 2' ) return fig.to_html(full_html=False)