import streamlit as st import plotly.express as px import plotly.graph_objects as go import pandas as pd import numpy as np from datetime import datetime, timedelta from app.services.extruder_service import ExtruderService from app.services.main_process_service import MainProcessService def show_metered_weight_correlation(): # 初始化服务 extruder_service = ExtruderService() main_process_service = MainProcessService() # 页面标题 st.title("米重相关性分析") # 初始化会话状态用于日期同步 if 'mc_start_date' not in st.session_state: st.session_state['mc_start_date'] = datetime.now().date() - timedelta(days=7) if 'mc_end_date' not in st.session_state: st.session_state['mc_end_date'] = datetime.now().date() if 'mc_quick_select' not in st.session_state: st.session_state['mc_quick_select'] = "最近7天" if 'mc_time_offset' not in st.session_state: st.session_state['mc_time_offset'] = 0.0 # 定义回调函数 def update_dates(qs): st.session_state['mc_quick_select'] = qs today = datetime.now().date() if qs == "今天": st.session_state['mc_start_date'] = today st.session_state['mc_end_date'] = today elif qs == "最近3天": st.session_state['mc_start_date'] = today - timedelta(days=3) st.session_state['mc_end_date'] = today elif qs == "最近7天": st.session_state['mc_start_date'] = today - timedelta(days=7) st.session_state['mc_end_date'] = today elif qs == "最近30天": st.session_state['mc_start_date'] = today - timedelta(days=30) st.session_state['mc_end_date'] = today def on_date_change(): st.session_state['mc_quick_select'] = "自定义" # 查询条件区域 with st.expander("🔍 查询配置", expanded=True): # 添加自定义 CSS 实现响应式换行 st.markdown(""" """, unsafe_allow_html=True) # 创建布局 cols = st.columns([1, 1, 1, 1, 1, 1.5, 1.5, 1]) options = ["今天", "最近3天", "最近7天", "最近30天", "自定义"] for i, option in enumerate(options): with cols[i]: # 根据当前选择状态决定按钮类型 button_type = "primary" if st.session_state['mc_quick_select'] == option else "secondary" if st.button(option, key=f"btn_mc_{option}", width='stretch', type=button_type): update_dates(option) st.rerun() with cols[5]: start_date = st.date_input( "开始日期", label_visibility="collapsed", key="mc_start_date", on_change=on_date_change ) with cols[6]: end_date = st.date_input( "结束日期", label_visibility="collapsed", key="mc_end_date", on_change=on_date_change ) with cols[7]: query_button = st.button("🚀 开始分析", key="mc_query", width='stretch') # 数据对齐调整 st.markdown("---") offset_cols = st.columns([2, 4, 2]) with offset_cols[0]: st.write("⏱️ **数据对齐调整**") with offset_cols[1]: time_offset = st.slider( "时间偏移 (分钟)", min_value=0.0, max_value=5.0, value=st.session_state['mc_time_offset'], step=0.1, help="调整主流程和温度数据的时间偏移,使其与挤出机米重数据对齐。" ) st.session_state['mc_time_offset'] = time_offset with offset_cols[2]: st.write(f"当前偏移: {time_offset} 分钟") # 转换为datetime对象 start_dt = datetime.combine(start_date, datetime.min.time()) end_dt = datetime.combine(end_date, datetime.max.time()) # 查询处理 - 仅获取数据并缓存到会话状态 if query_button: with st.spinner("正在获取数据..."): # 1. 获取完整的挤出机数据(包含所有时间点的螺杆转速和机头压力) df_extruder_full = extruder_service.get_extruder_data(start_dt, end_dt) # 2. 获取主流程控制数据 df_main_speed = main_process_service.get_cutting_setting_data(start_dt, end_dt) df_temp = main_process_service.get_temperature_control_data(start_dt, end_dt) # 检查是否有数据 has_data = any([ df_extruder_full is not None and not df_extruder_full.empty, df_main_speed is not None and not df_main_speed.empty, df_temp is not None and not df_temp.empty ]) if not has_data: st.warning("所选时间段内未找到任何数据,请尝试调整查询条件。") # 清除缓存数据 for key in ['cached_extruder_full', 'cached_main_speed', 'cached_temp', 'last_query_start', 'last_query_end']: if key in st.session_state: del st.session_state[key] return # 缓存数据到会话状态 st.session_state['cached_extruder_full'] = df_extruder_full st.session_state['cached_main_speed'] = df_main_speed st.session_state['cached_temp'] = df_temp st.session_state['last_query_start'] = start_dt st.session_state['last_query_end'] = end_dt # 数据处理和图表渲染 - 每次应用重新运行时执行(包括调整时间偏移时) if all(key in st.session_state for key in ['cached_extruder_full', 'cached_main_speed', 'cached_temp']): with st.spinner("正在分析数据相关性..."): # 获取缓存数据 df_extruder_full = st.session_state['cached_extruder_full'] df_main_speed = st.session_state['cached_main_speed'] df_temp = st.session_state['cached_temp'] # 获取当前时间偏移量 offset_delta = timedelta(minutes=st.session_state['mc_time_offset']) # 处理数据 if df_extruder_full is not None and not df_extruder_full.empty: # 过滤机头压力大于2的值 df_extruder_filtered = df_extruder_full[df_extruder_full['head_pressure'] <= 2] # 为米重数据创建偏移后的时间列(只对米重数据进行时间偏移) df_extruder_filtered['weight_time'] = df_extruder_filtered['time'] - offset_delta else: df_extruder_filtered = None # 检查是否有数据 has_data = any([ df_extruder_filtered is not None and not df_extruder_filtered.empty, df_main_speed is not None and not df_main_speed.empty, df_temp is not None and not df_temp.empty ]) if not has_data: st.warning("所选时间段内未找到任何数据,请尝试调整查询条件。") return # 数据整合与预处理 def integrate_data(df_extruder_filtered, df_main_speed, df_temp): # 确保挤出机数据存在 if df_extruder_filtered is None or df_extruder_filtered.empty: return None # 创建只包含米重和偏移时间的主数据集 df_weight = df_extruder_filtered[['weight_time', 'metered_weight']].copy() df_weight.rename(columns={'weight_time': 'time'}, inplace=True) # 将weight_time重命名为time作为基准时间 # 创建包含螺杆转速和原始时间的完整数据集 # 注意:这里使用完整的螺杆转速数据,而不仅仅是与米重对应的数据点 df_screw = df_extruder_filtered[['time', 'screw_speed_actual']].copy() # 创建包含机头压力和原始时间的完整数据集 # 注意:这里使用完整的机头压力数据,而不仅仅是与米重对应的数据点 df_pressure = df_extruder_filtered[['time', 'head_pressure']].copy() # 使用偏移后的米重时间整合螺杆转速数据 # 关键:使用merge_asof根据偏移后的米重时间查找最接近的螺杆转速数据 df_merged = pd.merge_asof( df_weight.sort_values('time'), df_screw.sort_values('time'), on='time', direction='nearest', tolerance=pd.Timedelta('1min') ) # 使用偏移后的米重时间整合机头压力数据 # 关键:使用merge_asof根据偏移后的米重时间查找最接近的机头压力数据 df_merged = pd.merge_asof( df_merged.sort_values('time'), df_pressure.sort_values('time'), on='time', direction='nearest', tolerance=pd.Timedelta('1min') ) # 整合主流程数据 if df_main_speed is not None and not df_main_speed.empty: df_main_speed = df_main_speed[['time', 'process_main_speed']] df_merged = pd.merge_asof( df_merged.sort_values('time'), df_main_speed.sort_values('time'), on='time', direction='nearest', tolerance=pd.Timedelta('1min') ) # 整合温度数据 if df_temp is not None and not df_temp.empty: temp_cols = ['time', 'nakata_extruder_screw_display_temp', 'nakata_extruder_rear_barrel_display_temp', 'nakata_extruder_front_barrel_display_temp', 'nakata_extruder_head_display_temp'] df_temp_subset = df_temp[temp_cols].copy() df_merged = pd.merge_asof( df_merged.sort_values('time'), df_temp_subset.sort_values('time'), on='time', direction='nearest', tolerance=pd.Timedelta('1min') ) # 重命名列以提高可读性 df_merged.rename(columns={ 'screw_speed_actual': '螺杆转速', 'head_pressure': '机头压力', 'process_main_speed': '流程主速', 'nakata_extruder_screw_display_temp': '螺杆温度', 'nakata_extruder_rear_barrel_display_temp': '后机筒温度', 'nakata_extruder_front_barrel_display_temp': '前机筒温度', 'nakata_extruder_head_display_temp': '机头温度' }, inplace=True) # 清理数据 df_merged.dropna(subset=['metered_weight'], inplace=True) return df_merged # 执行数据整合 df_analysis = integrate_data(df_extruder_filtered, df_main_speed, df_temp) if df_analysis is None or df_analysis.empty: st.warning("数据整合失败,请检查数据质量或调整时间范围。") return # 重命名米重列 df_analysis.rename(columns={'metered_weight': '米重'}, inplace=True) # --- 原始数据趋势图 --- st.subheader("📈 原始数据趋势图") # 创建趋势图 fig_trend = go.Figure() # 添加米重数据(使用偏移后的时间) if df_extruder_filtered is not None and not df_extruder_filtered.empty: fig_trend.add_trace(go.Scatter( x=df_extruder_filtered['weight_time'], # 使用偏移后的时间 y=df_extruder_filtered['metered_weight'], name='米重 (Kg/m) [已偏移]', mode='lines', line=dict(color='blue', width=2) )) # 添加螺杆转速(使用原始时间) fig_trend.add_trace(go.Scatter( x=df_extruder_filtered['time'], # 使用原始时间 y=df_extruder_filtered['screw_speed_actual'], name='螺杆转速 (RPM)', mode='lines', line=dict(color='green', width=1.5), yaxis='y2' )) # 添加机头压力(使用原始时间,已过滤大于2的值) fig_trend.add_trace(go.Scatter( x=df_extruder_filtered['time'], # 使用原始时间 y=df_extruder_filtered['head_pressure'], name='机头压力 (≤2)', mode='lines', line=dict(color='orange', width=1.5), yaxis='y3' )) # 添加流程主速 if df_main_speed is not None and not df_main_speed.empty: fig_trend.add_trace(go.Scatter( x=df_main_speed['time'], y=df_main_speed['process_main_speed'], name='流程主速 (M/Min)', mode='lines', line=dict(color='red', width=1.5), yaxis='y4' )) # 添加温度数据 if df_temp is not None and not df_temp.empty: # 螺杆温度 fig_trend.add_trace(go.Scatter( x=df_temp['time'], y=df_temp['nakata_extruder_screw_display_temp'], name='螺杆温度 (°C)', mode='lines', line=dict(color='purple', width=1), yaxis='y5' )) # 后机筒温度 fig_trend.add_trace(go.Scatter( x=df_temp['time'], y=df_temp['nakata_extruder_rear_barrel_display_temp'], name='后机筒温度 (°C)', mode='lines', line=dict(color='pink', width=1), yaxis='y5' )) # 前机筒温度 fig_trend.add_trace(go.Scatter( x=df_temp['time'], y=df_temp['nakata_extruder_front_barrel_display_temp'], name='前机筒温度 (°C)', mode='lines', line=dict(color='brown', width=1), yaxis='y5' )) # 机头温度 fig_trend.add_trace(go.Scatter( x=df_temp['time'], y=df_temp['nakata_extruder_head_display_temp'], name='机头温度 (°C)', mode='lines', line=dict(color='gray', width=1), yaxis='y5' )) # 配置趋势图布局 fig_trend.update_layout( title=f'原始数据趋势 (米重向前偏移 {st.session_state["mc_time_offset"]} 分钟)', xaxis=dict( title='时间', rangeslider=dict(visible=True), type='date' ), yaxis=dict( title='米重 (Kg/m)', title_font=dict(color='blue'), tickfont=dict(color='blue') ), yaxis2=dict( title='螺杆转速 (RPM)', title_font=dict(color='green'), tickfont=dict(color='green'), overlaying='y', side='right' ), yaxis3=dict( title='机头压力', title_font=dict(color='orange'), tickfont=dict(color='orange'), overlaying='y', side='right', anchor='free', position=0.85 ), yaxis4=dict( title='流程主速 (M/Min)', title_font=dict(color='red'), tickfont=dict(color='red'), overlaying='y', side='right', anchor='free', position=0.75 ), yaxis5=dict( title='温度 (°C)', title_font=dict(color='purple'), tickfont=dict(color='purple'), overlaying='y', side='left', anchor='free', position=0.15 ), legend=dict( orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1 ), height=600, margin=dict(l=100, r=200, t=100, b=100), hovermode='x unified', dragmode='select', ) # 显示趋势图 selection = st.plotly_chart(fig_trend, width='stretch', config={'scrollZoom': True}, on_select='rerun' ) # 调试输出 # st.write("原始 selection 对象:", selection) # 定义分析列 analysis_cols = ['米重', '螺杆转速', '机头压力', '流程主速', '螺杆温度', '后机筒温度', '前机筒温度', '机头温度'] # 定义要分析的参数 params = [ ('螺杆转速', 'RPM'), ('机头压力', ''), ('流程主速', 'M/Min'), ('螺杆温度', '°C'), ('后机筒温度', '°C'), ('前机筒温度', '°C'), ('机头温度', '°C') ] # 正确提取 selected_data = None if selection.selection and selection.selection.box: boxs = selection.selection.box # 获取选中框的x轴范围 x_range = boxs[0]['x'][0], boxs[0]['x'][1] st.write("x轴范围:", x_range) # 过滤出在x轴范围内的数据 # 注意:这里需要使用df_analysis的time列进行过滤 # 首先需要确保df_analysis有time列 if 'time' in df_analysis.columns: selected_data = df_analysis[ (df_analysis['time'] >= x_range[0]) & (df_analysis['time'] <= x_range[1]) ].copy() # 使用copy()避免切片警告 st.write(f"选中范围内的数据点数量: {len(selected_data)}") # 显示可用的列名,帮助调试 st.write("可用列名:", list(selected_data.columns)) else: st.warning("数据中缺少time列,无法进行范围过滤") else: st.info("请使用矩形框选工具选择时间范围(已自动启用选择模式)") # 添加细节分析按钮 if selected_data is not None and not selected_data.empty: if st.button("🔍 细节分析"): st.subheader("📊 框选范围细节分析") # 计算选中范围内的相关系数矩阵 selected_corr_matrix = selected_data[analysis_cols].corr() # 创建选中范围的热力图 selected_fig_heatmap = px.imshow( selected_corr_matrix, text_auto=True, aspect="auto", title="框选范围参数相关性矩阵", color_continuous_scale=["#0000FF", "#FFFFFF", "#FF0000"], color_continuous_midpoint=0, labels=dict(color="相关系数") ) # 自定义布局 selected_fig_heatmap.update_layout( height=400, margin=dict(l=80, r=80, t=80, b=80), xaxis=dict(tickangle=-45), yaxis=dict(tickangle=0) ) # 显示选中范围的热力图 st.plotly_chart(selected_fig_heatmap, width='stretch') # 显示选中范围的参数与米重散点图 st.subheader("📈 框选范围参数与米重散点图") # 创建选中范围的散点图 for i in range(0, len(params), 2): row_cols = st.columns(2) for j in range(2): if i + j < len(params): param_name, unit = params[i + j] with row_cols[j]: if param_name in selected_data.columns: # 计算相关系数(添加错误处理) try: # 过滤掉NaN值 valid_data = selected_data[[param_name, '米重']].dropna() if len(valid_data) >= 2: # 至少需要2个数据点 corr_coef = np.corrcoef(valid_data['米重'], valid_data[param_name])[0, 1] else: corr_coef = None except Exception as e: corr_coef = None # 创建散点图 fig_scatter = px.scatter( selected_data, x=param_name, y='米重', title=f"{param_name} vs 米重(框选范围)", labels={param_name: f"{param_name} ({unit})" if unit else param_name, '米重': '米重 (Kg/m)'} ) # 添加趋势线(添加错误处理) try: # 过滤掉NaN值 valid_data = selected_data[[param_name, '米重']].dropna() if len(valid_data) >= 2: # 至少需要2个数据点 trend_line = np.poly1d(np.polyfit(valid_data[param_name], valid_data['米重'], 1))(valid_data[param_name]) fig_scatter.add_trace(go.Scatter( x=valid_data[param_name], y=trend_line, mode='lines', name='趋势线', line=dict(color='red', width=2) )) except Exception as e: # 如果趋势线计算失败,跳过添加趋势线 pass # 添加相关系数注释(添加错误处理) if corr_coef is not None: fig_scatter.add_annotation( x=0.05, y=0.95, xref='paper', yref='paper', text=f"相关系数: {corr_coef:.4f}", showarrow=False, font=dict(size=12, color="black"), bgcolor="white", bordercolor="black", borderwidth=1 ) else: fig_scatter.add_annotation( x=0.05, y=0.95, xref='paper', yref='paper', text="相关系数: 无法计算", showarrow=False, font=dict(size=12, color="black"), bgcolor="white", bordercolor="black", borderwidth=1 ) # 显示散点图 st.plotly_chart(fig_scatter, use_container_width=True) else: st.warning(f"数据中缺少 {param_name} 列") # 显示选中范围的数据摘要 st.subheader("📊 框选范围数据摘要") selected_summary_cols = st.columns(4) with selected_summary_cols[0]: if '米重' in selected_data.columns: st.metric("平均米重", f"{selected_data['米重'].mean():.2f} Kg/m") with selected_summary_cols[1]: if '螺杆转速' in selected_data.columns: st.metric("平均螺杆转速", f"{selected_data['螺杆转速'].mean():.2f} RPM") with selected_summary_cols[2]: if '流程主速' in selected_data.columns: st.metric("平均流程主速", f"{selected_data['流程主速'].mean():.2f} M/Min") with selected_summary_cols[3]: if '机头压力' in selected_data.columns: st.metric("平均机头压力", f"{selected_data['机头压力'].mean():.2f}") # 显示选中范围的数据预览 st.subheader("🔍 框选范围数据预览") st.dataframe(selected_data[analysis_cols].head(10), use_container_width=True) # --- 相关性矩阵热力图 --- st.subheader("📊 相关性矩阵热力图") # 重命名米重列 df_analysis.rename(columns={'metered_weight': '米重'}, inplace=True) # 计算相关系数矩阵 corr_matrix = df_analysis[analysis_cols].corr() # 创建热力图 fig_heatmap = px.imshow( corr_matrix, text_auto=True, aspect="auto", title="参数相关性矩阵", color_continuous_scale=["#0000FF", "#FFFFFF", "#FF0000"], color_continuous_midpoint=0, labels=dict(color="相关系数") ) # 自定义布局 fig_heatmap.update_layout( height=500, margin=dict(l=100, r=100, t=100, b=100), xaxis=dict(tickangle=-45), yaxis=dict(tickangle=0) ) # 显示热力图 st.plotly_chart(fig_heatmap, width='stretch') # --- 参数与米重散点图 --- st.subheader("📈 参数与米重散点图") # 创建散点图 for i in range(0, len(params), 2): row_cols = st.columns(2) for j in range(2): if i + j < len(params): param_name, unit = params[i + j] with row_cols[j]: if param_name in df_analysis.columns: # 计算相关系数(添加错误处理) try: # 过滤掉NaN值 valid_data = df_analysis[[param_name, '米重']].dropna() if len(valid_data) >= 2: # 至少需要2个数据点 corr_coef = np.corrcoef(valid_data['米重'], valid_data[param_name])[0, 1] else: corr_coef = None except Exception as e: corr_coef = None # 创建散点图 fig_scatter = px.scatter( df_analysis, x=param_name, y='米重', title=f"{param_name} vs 米重", labels={param_name: f"{param_name} ({unit})" if unit else param_name, '米重': '米重 (Kg/m)'} ) # 添加趋势线(添加错误处理) try: # 过滤掉NaN值 valid_data = df_analysis[[param_name, '米重']].dropna() if len(valid_data) >= 2: # 至少需要2个数据点 trend_line = np.poly1d(np.polyfit(valid_data[param_name], valid_data['米重'], 1))(valid_data[param_name]) fig_scatter.add_trace(go.Scatter( x=valid_data[param_name], y=trend_line, mode='lines', name='趋势线', line=dict(color='red', width=2) )) except Exception as e: # 如果趋势线计算失败,跳过添加趋势线 pass # 添加相关系数注释(添加错误处理) if corr_coef is not None: fig_scatter.add_annotation( x=0.05, y=0.95, xref='paper', yref='paper', text=f"相关系数: {corr_coef:.4f}", showarrow=False, font=dict(size=12, color="black"), bgcolor="white", bordercolor="black", borderwidth=1 ) else: fig_scatter.add_annotation( x=0.05, y=0.95, xref='paper', yref='paper', text="相关系数: 无法计算", showarrow=False, font=dict(size=12, color="black"), bgcolor="white", bordercolor="black", borderwidth=1 ) # 显示散点图 st.plotly_chart(fig_scatter, use_container_width=True) else: st.warning(f"数据中缺少 {param_name} 列") # --- 相关性统计表格 --- st.subheader("📋 相关性统计") # 计算每个参数与米重的相关系数(添加错误处理) corr_stats = [] for param_name, _ in params: if param_name in df_analysis.columns: try: # 过滤掉NaN值 valid_data = df_analysis[[param_name, '米重']].dropna() if len(valid_data) >= 2: # 至少需要2个数据点 corr_coef = np.corrcoef(valid_data['米重'], valid_data[param_name])[0, 1] corr_stats.append({ '参数': param_name, '相关系数': corr_coef, '相关程度': '强' if abs(corr_coef) > 0.7 else '中等' if abs(corr_coef) > 0.3 else '弱' }) else: corr_stats.append({ '参数': param_name, '相关系数': None, '相关程度': '无法计算' }) except Exception as e: corr_stats.append({ '参数': param_name, '相关系数': None, '相关程度': '无法计算' }) # 创建统计表格 corr_df = pd.DataFrame(corr_stats) # 按相关系数绝对值排序(处理None值) try: # 计算相关系数绝对值,对于None值使用-1(这样会排在最后) corr_df['相关系数绝对值'] = corr_df['相关系数'].apply(lambda x: abs(x) if x is not None else -1) corr_df.sort_values('相关系数绝对值', ascending=False, inplace=True) corr_df.drop('相关系数绝对值', axis=1, inplace=True) except Exception as e: # 如果排序失败,保持原始顺序 pass # 显示表格 st.dataframe(corr_df, use_container_width=True) # --- 数据摘要 --- # st.subheader("📊 数据摘要") # summary_cols = st.columns(4) # with summary_cols[0]: # if '米重' in df_analysis.columns: # st.metric("平均米重", f"{df_analysis['米重'].mean():.2f} Kg/m") # with summary_cols[1]: # if '螺杆转速' in df_analysis.columns: # st.metric("平均螺杆转速", f"{df_analysis['螺杆转速'].mean():.2f} RPM") # with summary_cols[2]: # if '流程主速' in df_analysis.columns: # st.metric("平均流程主速", f"{df_analysis['流程主速'].mean():.2f} M/Min") # with summary_cols[3]: # if '机头压力' in df_analysis.columns: # st.metric("平均机头压力", f"{df_analysis['机头压力'].mean():.2f}") # --- 数据预览 --- st.subheader("🔍 数据预览") st.dataframe(df_analysis[analysis_cols].head(20), use_container_width=True) else: # 提示用户点击开始分析按钮 st.info("请选择时间范围并点击'开始分析'按钮获取数据。")