Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from scipy.stats import norm
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
st.title("KPI Std. Deviation")
|
| 9 |
+
|
| 10 |
+
# KPI Dropdown with units
|
| 11 |
+
kpi_options = {
|
| 12 |
+
'CRF': '%',
|
| 13 |
+
'Feed Water Temp': '°C',
|
| 14 |
+
'S:F': '',
|
| 15 |
+
'SSC': 'kg/kg',
|
| 16 |
+
'SWC': 'kg/kg',
|
| 17 |
+
'Make up water': 'kL'
|
| 18 |
+
}
|
| 19 |
+
kpi_selected = st.selectbox("Select KPI", list(kpi_options.keys()))
|
| 20 |
+
unit = kpi_options[kpi_selected]
|
| 21 |
+
|
| 22 |
+
# Inputs for Min, Max, and Mean
|
| 23 |
+
col1, col2, col3 = st.columns(3)
|
| 24 |
+
with col1:
|
| 25 |
+
min_val = st.number_input(f"Enter Min Value ({unit})", value=60.0)
|
| 26 |
+
with col2:
|
| 27 |
+
mean_val = st.number_input(f"Enter Mean (Avg) Value ({unit})", value=100.0)
|
| 28 |
+
with col3:
|
| 29 |
+
max_val = st.number_input(f"Enter Max Value ({unit})", value=140.0)
|
| 30 |
+
|
| 31 |
+
# Inputs for Probability Densities
|
| 32 |
+
col4, col5, col6 = st.columns(3)
|
| 33 |
+
with col4:
|
| 34 |
+
min_pdf = st.number_input("Enter Min Value Probability Density", value=0.0)
|
| 35 |
+
with col5:
|
| 36 |
+
mean_pdf = st.number_input("Enter Mean Value Probability Density", value=1.0)
|
| 37 |
+
with col6:
|
| 38 |
+
max_pdf = st.number_input("Enter Max Value Probability Density", value=0.0)
|
| 39 |
+
|
| 40 |
+
# Validation
|
| 41 |
+
if max_val > mean_val > min_val:
|
| 42 |
+
# Generate X values (200 points evenly spaced)
|
| 43 |
+
x = np.linspace(min_val, max_val, 200)
|
| 44 |
+
|
| 45 |
+
# Custom quadratic bell shape through interpolation of given PDF values
|
| 46 |
+
A = np.array([
|
| 47 |
+
[min_val**2, min_val, 1],
|
| 48 |
+
[mean_val**2, mean_val, 1],
|
| 49 |
+
[max_val**2, max_val, 1]
|
| 50 |
+
])
|
| 51 |
+
B = np.array([min_pdf, mean_pdf, max_pdf])
|
| 52 |
+
coeffs = np.linalg.solve(A, B)
|
| 53 |
+
a, b, c = coeffs
|
| 54 |
+
|
| 55 |
+
# Compute Y values based on custom quadratic
|
| 56 |
+
y = a * x**2 + b * x + c
|
| 57 |
+
y = np.maximum(y, 0) # Ensure no negative values
|
| 58 |
+
|
| 59 |
+
df = pd.DataFrame({
|
| 60 |
+
f"{kpi_selected} Value ({unit})": x,
|
| 61 |
+
f"Probability Density": y
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
# Plot
|
| 65 |
+
fig, ax = plt.subplots()
|
| 66 |
+
ax.plot(x, y, color='royalblue', linewidth=2)
|
| 67 |
+
ax.set_title(f"{kpi_selected} - Custom Bell Curve")
|
| 68 |
+
ax.set_xlabel(f"{kpi_selected} ({unit})")
|
| 69 |
+
ax.set_ylabel("Probability Density")
|
| 70 |
+
ax.grid(True, which='both', linestyle='--', linewidth=0.5, alpha=0.7) # Fine grid
|
| 71 |
+
st.pyplot(fig)
|
| 72 |
+
|
| 73 |
+
# Download data as CSV
|
| 74 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
| 75 |
+
st.download_button(
|
| 76 |
+
label="Download Data as CSV",
|
| 77 |
+
data=csv,
|
| 78 |
+
file_name=f"{kpi_selected}_custom_bell_curve_data.csv",
|
| 79 |
+
mime='text/csv'
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Download plot as PNG
|
| 83 |
+
buf = BytesIO()
|
| 84 |
+
fig.savefig(buf, format="png")
|
| 85 |
+
st.download_button(
|
| 86 |
+
label="Download Plot as PNG",
|
| 87 |
+
data=buf.getvalue(),
|
| 88 |
+
file_name=f"{kpi_selected}_custom_bell_curve_plot.png",
|
| 89 |
+
mime="image/png"
|
| 90 |
+
)
|
| 91 |
+
else:
|
| 92 |
+
st.warning("Please ensure that: Min < Mean < Max to generate a valid bell curve.")
|