Spaces:
Sleeping
Sleeping
| """Parameter visualization components""" | |
| import streamlit as st | |
| import plotly.graph_objects as go | |
| import plotly.express as px | |
| import pandas as pd | |
| import numpy as np | |
| from typing import Dict, List, Optional | |
| def plot_parameters(parameters: Dict[str, float], title: str = "Seven Parameters"): | |
| """Create parameter bar chart""" | |
| params = list(parameters.keys()) | |
| values = list(parameters.values()) | |
| # Define colors based on thresholds | |
| colors = [] | |
| for param, value in parameters.items(): | |
| if param.lower() == 'wcc': | |
| colors.append('red' if value > 1.58 else 'orange' if value > 1.35 else 'green') | |
| elif param.lower() == 'kpr': | |
| colors.append('red' if value > 2.0 else 'orange' if value > 1.6 else 'green') | |
| elif param.lower() == 'hfsi': | |
| colors.append('red' if value < 0.4 else 'orange' if value < 0.6 else 'green') | |
| elif param.lower() == 'becf': | |
| colors.append('red' if value > 6.0 else 'orange' if value > 4.0 else 'green') | |
| else: | |
| colors.append('blue') | |
| fig = go.Figure(data=[ | |
| go.Bar(name='Parameters', x=params, y=values, marker_color=colors) | |
| ]) | |
| fig.update_layout( | |
| title=title, | |
| xaxis_title="Parameter", | |
| yaxis_title="Value", | |
| height=400 | |
| ) | |
| st.plotly_chart(fig, use_container_width=True) | |
| def plot_parameter_timeseries(parameter: str, history: pd.DataFrame): | |
| """Plot parameter time series""" | |
| fig = px.line( | |
| history, | |
| x='timestamp', | |
| y='value', | |
| title=f"{parameter} Time Series" | |
| ) | |
| # Add threshold lines | |
| thresholds = { | |
| 'wcc': {'safe': 1.35, 'critical': 1.58}, | |
| 'kpr': {'safe': 1.6, 'critical': 2.0}, | |
| 'hfsi': {'safe': 0.6, 'critical': 0.4}, | |
| 'becf': {'safe': 4.0, 'critical': 6.0}, | |
| 'sdb': {'safe': 2.5, 'critical': 1.0}, | |
| 'sbsp': {'safe': 0.7, 'critical': 1.2}, | |
| 'smvi': {'safe': 0.4, 'critical': 0.6} | |
| } | |
| if parameter.lower() in thresholds: | |
| t = thresholds[parameter.lower()] | |
| fig.add_hline(y=t['safe'], line_dash="dash", line_color="orange") | |
| fig.add_hline(y=t['critical'], line_dash="dash", line_color="red") | |
| st.plotly_chart(fig, use_container_width=True) | |
| def plot_parameter_comparison(param_data: Dict[str, List[float]], | |
| zones: List[str], | |
| parameter: str): | |
| """Compare parameter across zones""" | |
| df = pd.DataFrame(param_data, index=zones) | |
| fig = px.bar( | |
| df.T, | |
| title=f"{parameter} Comparison Across Zones", | |
| barmode='group' | |
| ) | |
| st.plotly_chart(fig, use_container_width=True) | |
| def plot_correlation_matrix(parameters_df: pd.DataFrame): | |
| """Plot correlation matrix of parameters""" | |
| corr = parameters_df.corr() | |
| fig = px.imshow( | |
| corr, | |
| text_auto=True, | |
| aspect="auto", | |
| title="Parameter Correlation Matrix", | |
| color_continuous_scale='RdBu_r' | |
| ) | |
| st.plotly_chart(fig, use_container_width=True) | |
| def plot_radar_chart(parameters: Dict[str, float], | |
| normalized: bool = True, | |
| title: str = "Parameter Radar Chart"): | |
| """Create radar chart of parameters""" | |
| # Normalize if requested | |
| if normalized: | |
| norm_params = {} | |
| for param, value in parameters.items(): | |
| if param.lower() == 'wcc': | |
| norm_params[param] = min(value / 1.58, 1.0) | |
| elif param.lower() == 'kpr': | |
| norm_params[param] = min(value / 2.0, 1.0) | |
| elif param.lower() == 'hfsi': | |
| norm_params[param] = 1 - min(value / 1.0, 1.0) # Invert | |
| elif param.lower() == 'becf': | |
| norm_params[param] = min(value / 6.0, 1.0) | |
| else: | |
| norm_params[param] = min(value / 1.0, 1.0) | |
| else: | |
| norm_params = parameters | |
| fig = go.Figure(data=go.Scatterpolar( | |
| r=list(norm_params.values()), | |
| theta=list(norm_params.keys()), | |
| fill='toself' | |
| )) | |
| fig.update_layout( | |
| polar=dict( | |
| radialaxis=dict( | |
| visible=True, | |
| range=[0, 1] | |
| )), | |
| showlegend=False, | |
| title=title | |
| ) | |
| st.plotly_chart(fig, use_container_width=True) | |