Dev_plot / app.py
Shivraj8615's picture
Update app.py
3d3f1d9 verified
raw
history blame
3.38 kB
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from io import BytesIO
st.title("KPI Std. Deviation")
# KPI Dropdown with units
kpi_options = {
'CRF': '%',
'Feed Water Temp': '°C',
'S:F': '',
'SSC': 'kg/kg',
'SWC': 'kg/kg',
'Make up water': 'kL'
}
kpi_selected = st.selectbox("Select KPI", list(kpi_options.keys()))
unit = kpi_options[kpi_selected]
# Inputs for Min, Max, and Mean
col1, col2, col3 = st.columns(3)
with col1:
min_val = st.number_input(f"Enter Min Value ({unit})", value=60.0)
with col2:
mean_val = st.number_input(f"Enter Mean (Avg) Value ({unit})", value=100.0)
with col3:
max_val = st.number_input(f"Enter Max Value ({unit})", value=140.0)
# Inputs for Probability Densities
col4, col5, col6 = st.columns(3)
with col4:
min_pdf = st.number_input("Enter Min Value Probability Density", value=0.0)
with col5:
mean_pdf = st.number_input("Enter Mean Value Probability Density", value=1.0)
with col6:
max_pdf = st.number_input("Enter Max Value Probability Density", value=0.0)
# Validation
if max_val > mean_val > min_val:
# Generate X values (200 points evenly spaced)
x = np.linspace(min_val, max_val, 200)
# Custom quadratic bell shape through interpolation of given PDF values
A = np.array([
[min_val**2, min_val, 1],
[mean_val**2, mean_val, 1],
[max_val**2, max_val, 1]
])
B = np.array([min_pdf, mean_pdf, max_pdf])
coeffs = np.linalg.solve(A, B)
a, b, c = coeffs
# Compute Y values based on custom quadratic
y = a * x**2 + b * x + c
y = np.maximum(y, 0) # Ensure no negative values
df = pd.DataFrame({
f"{kpi_selected} Value ({unit})": x,
"Probability Density": y
})
# Plot
fig, ax = plt.subplots()
ax.plot(x, y, color='royalblue', linewidth=2, label='Deviation Curve')
# Markers for Min, Mean, Max
ax.axvline(min_val, color='blue', linestyle='--', linewidth=1)
ax.axvline(mean_val, color='green', linestyle='--', linewidth=1)
ax.axvline(max_val, color='red', linestyle='--', linewidth=1)
# Adjusted label positions for visibility
y_max = y.max()
offset = y_max * 0.05
ax.text(min_val, offset, f'Min\n{min_val} {unit}', color='blue', ha='right', va='bottom')
ax.text(mean_val, mean_pdf + offset, f'Mean\n{mean_val} {unit}', color='green', ha='center', va='bottom')
ax.text(max_val, offset, f'Max\n{max_val} {unit}', color='red', ha='left', va='bottom')
ax.set_title(f"{kpi_selected} - Std. Deviation Curve")
ax.set_xlabel(f"{kpi_selected} ({unit})")
ax.set_ylabel("Probability Density")
ax.grid(True, which='both', linestyle='--', linewidth=0.5, alpha=0.7)
st.pyplot(fig)
# Download data as CSV
csv = df.to_csv(index=False).encode('utf-8')
st.download_button(
label="Download Data as CSV",
data=csv,
file_name=f"{kpi_selected}_custom_bell_curve_data.csv",
mime='text/csv'
)
# Download plot as PNG
buf = BytesIO()
fig.savefig(buf, format="png")
st.download_button(
label="Download Plot as PNG",
data=buf.getvalue(),
file_name=f"{kpi_selected}_custom_bell_curve_plot.png",
mime="image/png"
)
else:
st.warning("Please ensure that: Min < Mean < Max to generate a valid bell curve.")