Update helpers.py
Browse files- helpers.py +176 -72
helpers.py
CHANGED
@@ -1,72 +1,176 @@
|
|
1 |
-
"""Utility functions shared by all modules."""
|
2 |
-
|
3 |
-
import
|
4 |
-
import
|
5 |
-
from
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Utility functions shared by all modules."""
|
2 |
+
|
3 |
+
import io
|
4 |
+
import re
|
5 |
+
from typing import Tuple
|
6 |
+
|
7 |
+
import numpy as np
|
8 |
+
import pandas as pd
|
9 |
+
import plotly.graph_objects as go
|
10 |
+
import streamlit as st
|
11 |
+
|
12 |
+
from config import FREQUENCIES, TOTAL_DOTS, AI_BANDS
|
13 |
+
|
14 |
+
|
15 |
+
# ββ misc helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
16 |
+
def slugify(txt: str) -> str:
|
17 |
+
"""Return a filesystemβ / urlβsafe identifier."""
|
18 |
+
return re.sub(r"[^0-9a-zA-Z_]+", "_", txt)
|
19 |
+
|
20 |
+
|
21 |
+
def standardise_freq_cols(df: pd.DataFrame) -> pd.DataFrame:
|
22 |
+
"""
|
23 |
+
Renames common textual frequency headings to plain numbers and returns a
|
24 |
+
**new** DataFrame so the callerβs original is left untouched.
|
25 |
+
"""
|
26 |
+
df = df.copy() # defensive copy
|
27 |
+
mapping = {
|
28 |
+
"125Hz": "125",
|
29 |
+
"250Hz": "250",
|
30 |
+
"500Hz": "500",
|
31 |
+
"1000Hz": "1000",
|
32 |
+
"1KHz": "1000",
|
33 |
+
"2KHz": "2000",
|
34 |
+
"4KHz": "4000",
|
35 |
+
}
|
36 |
+
df.columns = [
|
37 |
+
mapping.get(
|
38 |
+
str(c).replace(" Hz", "").replace("KHz", "000").strip(), str(c).strip()
|
39 |
+
)
|
40 |
+
for c in df.columns
|
41 |
+
]
|
42 |
+
df.columns = pd.to_numeric(df.columns, errors="ignore")
|
43 |
+
return df
|
44 |
+
|
45 |
+
|
46 |
+
def validate_numeric(df: pd.DataFrame) -> bool:
|
47 |
+
"""True iff every element of *df* is numeric."""
|
48 |
+
return not df.empty and df.applymap(np.isreal).all().all()
|
49 |
+
|
50 |
+
|
51 |
+
def read_upload(
|
52 |
+
upload, *, header: int | None = 0, index_col: int | None = None
|
53 |
+
) -> pd.DataFrame:
|
54 |
+
"""
|
55 |
+
Read an uploaded CSV or Excel file into a fresh DataFrame.
|
56 |
+
|
57 |
+
No caching is used so that every Streamlit session receives its own
|
58 |
+
independent object which can be mutated freely without leaking state.
|
59 |
+
"""
|
60 |
+
raw: bytes = upload.getvalue()
|
61 |
+
if upload.name.lower().endswith(".csv"):
|
62 |
+
return pd.read_csv(io.BytesIO(raw), header=header, index_col=index_col)
|
63 |
+
return pd.read_excel(io.BytesIO(raw), header=header, index_col=index_col)
|
64 |
+
|
65 |
+
|
66 |
+
def calc_abs_area(volume_m3: float, rt_s: float) -> float:
|
67 |
+
"""Sabine: absorption area required to achieve *rt_s* in a room of *volume_m3*."""
|
68 |
+
return float("inf") if rt_s == 0 else 0.16 * volume_m3 / rt_s
|
69 |
+
|
70 |
+
|
71 |
+
# ββ plotting helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
72 |
+
def _base_layout(title: str, x_title: str, y_title: str) -> dict:
|
73 |
+
"""Common Plotly layout options."""
|
74 |
+
return dict(
|
75 |
+
template="plotly_white",
|
76 |
+
title=title,
|
77 |
+
xaxis_title=x_title,
|
78 |
+
yaxis_title=y_title,
|
79 |
+
legend=dict(orientation="h", y=-0.2),
|
80 |
+
)
|
81 |
+
|
82 |
+
|
83 |
+
def plot_rt_band(
|
84 |
+
y_cur: list[float], y_min: list[float], y_max: list[float], title: str
|
85 |
+
) -> go.Figure:
|
86 |
+
"""RT60 band plot."""
|
87 |
+
fig = go.Figure()
|
88 |
+
fig.add_trace(
|
89 |
+
go.Scatter(
|
90 |
+
x=FREQUENCIES,
|
91 |
+
y=y_cur,
|
92 |
+
mode="lines+markers",
|
93 |
+
name="Current",
|
94 |
+
marker_color="#1f77b4",
|
95 |
+
)
|
96 |
+
)
|
97 |
+
fig.add_trace(
|
98 |
+
go.Scatter(
|
99 |
+
x=FREQUENCIES,
|
100 |
+
y=y_max,
|
101 |
+
mode="lines",
|
102 |
+
name="Max Std",
|
103 |
+
line=dict(dash="dash", color="#ff7f0e"),
|
104 |
+
)
|
105 |
+
)
|
106 |
+
fig.add_trace(
|
107 |
+
go.Scatter(
|
108 |
+
x=FREQUENCIES,
|
109 |
+
y=y_min,
|
110 |
+
mode="lines",
|
111 |
+
name="Min Std",
|
112 |
+
line=dict(dash="dash", color="#2ca02c"),
|
113 |
+
fill="tonexty",
|
114 |
+
fillcolor="rgba(44,160,44,0.15)",
|
115 |
+
)
|
116 |
+
)
|
117 |
+
fig.update_layout(**_base_layout(title, "Frequencyβ―(Hz)", "Reverberation Timeβ―(s)"))
|
118 |
+
return fig
|
119 |
+
|
120 |
+
|
121 |
+
def plot_bn_band(
|
122 |
+
x: pd.Series,
|
123 |
+
y_meas: pd.Series,
|
124 |
+
y_min: float,
|
125 |
+
y_max: float,
|
126 |
+
title: str,
|
127 |
+
) -> go.Figure:
|
128 |
+
"""Backgroundβnoise bar plot with standard band overlay."""
|
129 |
+
fig = go.Figure()
|
130 |
+
fig.add_trace(
|
131 |
+
go.Bar(x=x, y=y_meas, name="Measured", marker_color="#1f77b4", opacity=0.6)
|
132 |
+
)
|
133 |
+
|
134 |
+
# standard band
|
135 |
+
fig.add_shape(
|
136 |
+
type="rect",
|
137 |
+
x0=-0.5,
|
138 |
+
x1=len(x) - 0.5,
|
139 |
+
y0=y_min,
|
140 |
+
y1=y_max,
|
141 |
+
fillcolor="rgba(255,0,0,0.15)",
|
142 |
+
line=dict(width=0),
|
143 |
+
layer="below",
|
144 |
+
)
|
145 |
+
for y, label in [(y_max, "Max Std"), (y_min, "Min Std")]:
|
146 |
+
fig.add_shape(
|
147 |
+
type="line",
|
148 |
+
x0=-0.5,
|
149 |
+
x1=len(x) - 0.5,
|
150 |
+
y0=y,
|
151 |
+
y1=y,
|
152 |
+
line=dict(color="#ff0000", dash="dash"),
|
153 |
+
)
|
154 |
+
fig.add_trace(
|
155 |
+
go.Scatter(
|
156 |
+
x=[None],
|
157 |
+
y=[None],
|
158 |
+
mode="lines",
|
159 |
+
line=dict(color="#ff0000", dash="dash"),
|
160 |
+
showlegend=True,
|
161 |
+
name=label,
|
162 |
+
)
|
163 |
+
)
|
164 |
+
|
165 |
+
fig.update_layout(**_base_layout(title, "Location", "Sound Levelβ―(dBA)"))
|
166 |
+
return fig
|
167 |
+
|
168 |
+
|
169 |
+
# ββ speechβintelligibility helpers ββββββββββββββββββββββββββββββββββββββββ
|
170 |
+
def articulation_index(dots: int) -> Tuple[float, str]:
|
171 |
+
"""Return (AI value, interpretation label) given dotsβaboveβcurve count."""
|
172 |
+
ai = dots / TOTAL_DOTS
|
173 |
+
for (lo, hi), lbl in AI_BANDS.items():
|
174 |
+
if lo <= ai <= hi:
|
175 |
+
return ai, lbl
|
176 |
+
return ai, "Out of range"
|