File size: 3,323 Bytes
902a364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a12864
902a364
 
 
 
4a12864
 
 
987a502
 
 
4a12864
987a502
 
 
4a12864
 
902a364
 
4a12864
902a364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a12864
3d3f1d9
987a502
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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.plot(min_val, min_pdf, 'o', color='blue')
    ax.plot(mean_val, mean_pdf, 'o', color='green')
    ax.plot(max_val, max_pdf, 'o', color='red')

    ax.annotate(f'{min_val} {unit}', (min_val, min_pdf), textcoords="offset points", xytext=(-10,10), ha='center', color='blue')
    ax.annotate(f'{mean_val} {unit}', (mean_val, mean_pdf), textcoords="offset points", xytext=(0,10), ha='center', color='green')
    ax.annotate(f'{max_val} {unit}', (max_val, max_pdf), textcoords="offset points", xytext=(10,10), ha='center', color='red')

    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.")