-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathloess_viewer.py
More file actions
267 lines (209 loc) · 7.86 KB
/
loess_viewer.py
File metadata and controls
267 lines (209 loc) · 7.86 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# streamlit run loessviewer.py
import io
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import streamlit as st
try:
from skmisc.loess import loess
SKMISC_AVAILABLE = True
except Exception:
SKMISC_AVAILABLE = False
st.set_page_config(page_title="LOESS Viewer", layout="wide")
st.title("LOESS Viewer")
def read_uploaded_file(uploaded_file):
name = uploaded_file.name.lower()
if name.endswith(".csv"):
for sep in [",", ";", "\t"]:
uploaded_file.seek(0)
try:
df = pd.read_csv(uploaded_file, sep=sep)
if df.shape[1] > 1:
return df
except Exception:
pass
uploaded_file.seek(0)
return pd.read_csv(uploaded_file)
if name.endswith(".xlsx") or name.endswith(".xls"):
xls = pd.ExcelFile(uploaded_file)
if len(xls.sheet_names) == 1:
return pd.read_excel(uploaded_file, sheet_name=xls.sheet_names[0])
sheet = st.selectbox("Sheet", xls.sheet_names)
return pd.read_excel(uploaded_file, sheet_name=sheet)
raise ValueError("Unsupported file type")
def to_numeric_time(series):
s = series.copy()
if np.issubdtype(s.dtype, np.datetime64):
t = (s - s.min()).dt.total_seconds() / 86400.0
return t.to_numpy(dtype=float), pd.to_datetime(s), True
s_num = pd.to_numeric(s, errors="coerce")
return s_num.to_numpy(dtype=float), s, False
def loess_smooth(x,y,span):
if 1==1:
l = loess(x,y)
# MODEL and CONTROL. Essential for replicating the results from the R script.
#
# https://has2k1.github.io/scikit-misc/stable/generated/skmisc.loess.loess_model.html#skmisc.loess.loess_model
# https://has2k1.github.io/scikit-misc/stable/generated/skmisc.loess.loess_control.html#skmisc.loess.loess_control
l.model.span = span
l.model.degree = 1
l.control.iterations = 1 #it # must be 1 for replicating the R-script
l.control.surface = "direct"
if len(y)<100:
l.control.statistics = "exact"
else:
l.control.statistics = "approximate" # or "none" if supported in your version
l.fit()
pred = l.predict(x, stderror=False)
lowess = pred.values
else:
lowess=y
return lowess
def loess_smooth_(x, y, span):
if SKMISC_AVAILABLE:
model = loess(x, y, span=span, degree=1)
model.fit()
return model.predict(x).values
# fallback
n = len(x)
r = max(2, int(np.ceil(span * n)))
yest = np.zeros(n)
for i in range(n):
distances = np.abs(x - x[i])
idx = np.argsort(distances)[:r]
dmax = distances[idx[-1]]
if dmax == 0:
yest[i] = y[i]
continue
w = (1 - (distances[idx] / dmax) ** 3) ** 3
X = np.vstack([np.ones(len(idx)), x[idx]]).T
W = np.diag(w)
beta = np.linalg.pinv(X.T @ W @ X) @ (X.T @ W @ y[idx])
yest[i] = beta[0] + beta[1] * x[i]
return yest
def main():
uploaded_file = st.file_uploader("Upload CSV or Excel", type=["csv", "xlsx", "xls"])
if uploaded_file is None:
st.stop()
try:
df = read_uploaded_file(uploaded_file)
except Exception as e:
st.error(f"Could not read file: {e}")
st.stop()
if df.empty:
st.error("File is empty")
st.stop()
with st.expander("Data preview"):
st.dataframe(df.head(20), width='stretch')
all_columns = df.columns.tolist()
col1, col2, col3,col4 = st.columns(4)
with col1:
filter_col = st.selectbox("Filter column", ["None"] + all_columns)
with col2:
if filter_col != "None":
unique_vals = df[filter_col].dropna().astype(str).unique().tolist()
unique_vals = sorted(unique_vals)
filter_val = st.selectbox("Filter value", ["All"] + unique_vals)
else:
filter_val = "All"
st.selectbox("Filter value", ["All"], disabled=True)
with col3:
x_col = st.selectbox("X axis", all_columns)
with col4:
factor = st.number_input("Factor",1.417)
df_plot = df.copy()
if filter_col != "None" and filter_val != "All":
df_plot = df_plot[df_plot[filter_col].astype(str) == str(filter_val)].copy()
if df_plot.empty:
st.warning("No rows left after filtering")
st.stop()
numeric_cols = df_plot.select_dtypes(include=[np.number]).columns.tolist()
numeric_cols = [c for c in numeric_cols if c != x_col]
col1, col2, col3,col4 = st.columns(4)
with col1:
y_col = st.selectbox(
"Columns for LOESS",
numeric_cols,
help="Choose columns to apply LOESS smoothing to. The raw data will also be plotted for comparison.",
)
if not y_col:
st.warning("Choose at least 1 column")
st.stop()
with col2:
window_1 = st.number_input("Window 1", min_value=1, value=7, step=1)
with col3:
window_2 = st.number_input("Window 2 (0 for None)", min_value=0, value=365, step=1)
with col4:
window_3 = st.number_input("Window 3 (0 for None)", min_value=0, value=0, step=1)
windows=[window_1,window_2,window_3]
df_plot = df_plot.sort_values(by=x_col).copy()
x_numeric, x_display, is_datetime = to_numeric_time(df_plot[x_col])
valid_x = ~np.isnan(x_numeric)
df_plot = df_plot.loc[valid_x].copy()
x_numeric = x_numeric[valid_x]
x_display = x_display.loc[valid_x] if hasattr(x_display, "loc") else x_display[valid_x]
first = True
fig = go.Figure()
for window in windows:
if window !=0:
print (f"Processing window {window}")
y = pd.to_numeric(df_plot[y_col], errors="coerce").to_numpy(dtype=float)
valid = ~np.isnan(y) & ~np.isnan(x_numeric)
if valid.sum() < 3:
st.warning(f"Too few valid rows for {y_col}")
continue
x_use = x_numeric[valid]
y_use = y[valid]
x_show = x_display.loc[valid] if hasattr(x_display, "loc") else x_display[valid]
span = factor * window / len(x_use)
span = min(max(span, 0.001), 1.0)
y_loess = loess_smooth(x_use, y_use, span)
#y_sma= pd.Series(y_use).rolling(window=window, center=True).mean().to_numpy()
y_sma= pd.Series(y_use).rolling(window=window, min_periods=1, center=True).mean().to_numpy() #values at edges
print (y_loess)
if first:
fig.add_trace(
go.Scatter(
x=x_show,
y=y_use,
mode="markers",
name=f"{y_col} raw",
marker=dict(size=1),
opacity=0.85,
)
)
first=False
fig.add_trace(
go.Scatter(
x=x_show,
y=y_loess,
mode="lines",
name=f"{y_col} loess {window}",
line=dict(width=1),
)
)
fig.add_trace(
go.Scatter(
x=x_show,
y=y_sma,
mode="lines",
name=f"{y_col} sma {window}",
line=dict(width=1),
)
)
st.caption(
f"Span formula: {factor} × window / len(t)"
+ (" | Using skmisc.loess" if SKMISC_AVAILABLE else " | Using fallback LOESS")
)
fig.update_layout(
height=700,
template="plotly_white",
hovermode="x unified",
xaxis_title=x_col,
yaxis_title="Value",
legend_title="Series",
)
st.plotly_chart(fig, width='stretch')
if __name__=="__main__":
print("Start")
main()