-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
200 lines (184 loc) · 7.23 KB
/
Copy pathapp.py
File metadata and controls
200 lines (184 loc) · 7.23 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""Streamlit interface for the educational cooling-loop dashboard."""
from pathlib import Path
import pandas as pd
import streamlit as st
from cooling_loop_dashboard.analysis import (
DataValidationError,
Thresholds,
build_warning_table,
data_quality_summary,
load_sensor_csv,
process_data,
)
from cooling_loop_dashboard.report import build_html_report
REPOSITORY_ROOT = Path(__file__).parent
SAMPLES = {
"Normal operation": REPOSITORY_ROOT / "data" / "normal.csv",
"Restricted flow": REPOSITORY_ROOT / "data" / "restricted_flow.csv",
"Pump failure": REPOSITORY_ROOT / "data" / "pump_failure.csv",
}
st.set_page_config(page_title="Cooling Loop Dashboard", page_icon="💧", layout="wide")
st.title("Cooling Loop Dashboard")
st.caption(
"Educational analysis of synthetic cooling-loop data. This tool is not "
"intended for control or safety decisions in real facilities."
)
with st.sidebar:
st.header("Data and assumptions")
input_mode = st.radio("Data source", ["Included sample", "Upload CSV"])
source: object | None
source_name: str
if input_mode == "Included sample":
selected_sample = st.selectbox("Sample dataset", list(SAMPLES))
source = SAMPLES[selected_sample]
source_name = selected_sample
else:
uploaded_file = st.file_uploader("Upload simulator-compatible CSV", type="csv")
source = uploaded_file
source_name = uploaded_file.name if uploaded_file else "Uploaded CSV"
specific_heat = st.number_input(
"Specific heat (J/kg·K)", min_value=1.0, value=4180.0, step=10.0
)
low_flow = st.number_input(
"Low-flow warning (kg/s)", min_value=0.0, value=0.2, step=0.01
)
high_pressure_drop = st.number_input(
"High pressure-drop warning (kPa)", min_value=0.0, value=50.0, step=1.0
)
high_temperature_difference = st.number_input(
"High temperature-difference warning (°C)",
min_value=0.0,
value=12.0,
step=0.5,
)
if source is None:
st.info("Upload a CSV file to begin.")
st.stop()
try:
loaded = load_sensor_csv(source)
processed = process_data(loaded, float(specific_heat))
except (DataValidationError, ValueError) as error:
st.error(f"Unable to analyze this file: {error}")
st.stop()
thresholds = Thresholds(
low_flow_kg_s=float(low_flow),
high_pressure_drop_kpa=float(high_pressure_drop),
high_temperature_difference_c=float(high_temperature_difference),
)
warnings = build_warning_table(processed, thresholds)
quality = data_quality_summary(processed)
complete_rows = processed.dropna(
subset=["flow_rate_kg_s", "delta_temperature_c", "estimated_heat_removal_w"]
)
if complete_rows.empty:
st.error(
"Unable to analyze this file: no rows contain complete flow and "
"temperature data."
)
st.stop()
latest = complete_rows.iloc[-1]
has_synthetic_heat_load = "synthetic_heat_load_w" in processed.columns
balance_rows = (
processed.dropna(subset=["synthetic_heat_load_w", "heat_balance_residual_w"])
if has_synthetic_heat_load
else pd.DataFrame()
)
latest_balance = None if balance_rows.empty else balance_rows.iloc[-1]
st.header("Current operating summary")
metric_columns = st.columns(4)
metric_columns[0].metric("Flow", f"{latest['flow_rate_kg_s']:.3f} kg/s")
metric_columns[1].metric(
"Temperature difference", f"{latest['delta_temperature_c']:.2f} °C"
)
metric_columns[2].metric("Pressure drop", f"{latest['pressure_drop_kpa']:.2f} kPa")
metric_columns[3].metric(
"Estimated heat removal", f"{latest['estimated_heat_removal_w'] / 1000:.2f} kW"
)
if has_synthetic_heat_load:
balance_metric_columns = st.columns(2)
if latest_balance is None:
synthetic_heat_load = "Unavailable"
heat_balance_residual = "Unavailable"
else:
synthetic_heat_load = f"{latest_balance['synthetic_heat_load_w'] / 1000:.2f} kW"
residual_kw = latest_balance["heat_balance_residual_w"] / 1000
if abs(residual_kw) < 0.005:
residual_kw = 0.0
heat_balance_residual = f"{residual_kw:.2f} kW"
balance_metric_columns[0].metric("Synthetic heat load", synthetic_heat_load)
balance_metric_columns[1].metric(
"Educational heat-balance residual", heat_balance_residual
)
st.caption(
"The synthetic heat load comes from the simulator. The residual is a "
"simplified educational comparison, not a validated physical energy balance."
)
chart_frame = processed.dropna(subset=["timestamp"]).set_index("timestamp")
left, right = st.columns(2)
with left:
st.subheader("Inlet and outlet temperatures")
st.line_chart(chart_frame[["inlet_temperature_c", "outlet_temperature_c"]])
st.subheader("Flow rate")
st.line_chart(chart_frame[["flow_rate_kg_s"]])
st.subheader("Pump speed")
st.line_chart(chart_frame[["pump_speed_percent"]])
with right:
st.subheader("Temperature difference")
st.line_chart(chart_frame[["delta_temperature_c"]])
st.subheader("Pressure drop")
st.line_chart(chart_frame[["pressure_drop_kpa"]])
st.subheader("Calculated heat removal")
st.line_chart(chart_frame[["estimated_heat_removal_w"]] / 1000)
if has_synthetic_heat_load:
st.subheader("Synthetic heat load and educational residual")
balance_chart = (
chart_frame[["synthetic_heat_load_w", "heat_balance_residual_w"]].rename(
columns={
"synthetic_heat_load_w": "Synthetic heat load (kW)",
"heat_balance_residual_w": "Heat-balance residual (kW)",
}
)
/ 1000
)
st.line_chart(balance_chart)
st.header("Warnings and anomalies")
if warnings.empty:
st.success("No warnings detected with the selected thresholds.")
else:
st.warning(f"{len(warnings)} warnings detected with the selected thresholds.")
st.dataframe(warnings, width="stretch", hide_index=True)
st.header("Data quality summary")
quality_table = pd.DataFrame({"Metric": list(quality), "Value": list(quality.values())})
st.dataframe(quality_table, width="stretch", hide_index=True)
with st.expander("Formulas and assumptions"):
heat_balance_assumption = (
"- Educational heat-balance residual: `estimated heat removal - synthetic "
"heat load`\n"
if has_synthetic_heat_load
else ""
)
st.markdown(
f"""
- Temperature difference: `outlet temperature - inlet temperature`
- Estimated heat removal: `mass flow × specific heat × temperature difference`
{heat_balance_assumption}- Specific heat in this view: `{specific_heat:.1f} J/(kg·K)`
- Input files are expected to contain synthetic measurements from
`sensor-stream-simulator` or columns with the same names.
- Calculations use simplified, constant-property assumptions and are educational.
"""
)
processed_csv = processed.to_csv(index=False).encode("utf-8")
html_report = build_html_report(processed, warnings, source_name, float(specific_heat))
download_columns = st.columns(2)
download_columns[0].download_button(
"Download processed CSV",
processed_csv,
file_name="processed_cooling_loop.csv",
mime="text/csv",
)
download_columns[1].download_button(
"Download HTML summary",
html_report,
file_name="cooling_loop_summary.html",
mime="text/html",
)