UCS2014 commited on
Commit
9c36cf3
·
verified ·
1 Parent(s): 105c522

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -51
app.py CHANGED
@@ -141,7 +141,8 @@ except AttributeError:
141
  return wrapper
142
  return deco
143
 
144
- def rmse(y_true, y_pred): return float(np.sqrt(mean_squared_error(y_true, y_pred)))
 
145
 
146
  @st.cache_resource(show_spinner=False)
147
  def load_model(model_path: str):
@@ -153,7 +154,8 @@ def parse_excel(data_bytes: bytes):
153
  xl = pd.ExcelFile(bio)
154
  return {sh: xl.parse(sh) for sh in xl.sheet_names}
155
 
156
- def read_book_bytes(b: bytes): return parse_excel(b) if b else {}
 
157
 
158
  def ensure_cols(df, cols):
159
  miss = [c for c in cols if c not in df.columns]
@@ -165,25 +167,23 @@ def ensure_cols(df, cols):
165
  def find_sheet(book, names):
166
  low2orig = {k.lower(): k for k in book.keys()}
167
  for nm in names:
168
- if nm.lower() in low2orig: return low2orig[nm.lower()]
 
169
  return None
170
 
171
  def _nice_tick0(xmin: float, step: int = 100) -> float:
172
- """Round xmin down to a sensible multiple so the first tick sits at the left edge."""
173
- if not np.isfinite(xmin):
174
- return xmin
175
- return step * math.floor(xmin / step)
176
 
177
- # ---------- Plot builders ----------
178
  def cross_plot(actual, pred):
179
  a = pd.Series(actual).astype(float)
180
  p = pd.Series(pred).astype(float)
181
 
182
  fixed_min = 6000
183
  fixed_max = 10000
184
- # Choose tick spacing and first tick
185
- tick_spacing = 500
186
- tick_start = 6000
187
  fig = go.Figure()
188
 
189
  # Scatter points
@@ -194,12 +194,12 @@ tick_start = 6000
194
  showlegend=False
195
  ))
196
 
197
- # 1:1 reference line from bottom-left to top-right
198
- fig.add_trace(go.Scatter(
199
- x=[x0, x1], y=[x0, x1], mode="lines",
200
- line=dict(color=COLORS["ref"], width=1.2, dash="dash"),
201
- hoverinfo="skip", showlegend=False
202
- ))
203
 
204
  fig.update_layout(
205
  width=CROSS_W, height=CROSS_H,
@@ -207,52 +207,48 @@ fig.add_trace(go.Scatter(
207
  margin=dict(l=64, r=18, t=10, b=48),
208
  hovermode="closest",
209
  font=dict(size=FONT_SZ),
210
- dragmode=False # disables zooming
211
  )
212
 
213
- fig.update_xaxes(
214
- title_text="<b>Actual UCS (psi)</b>",
215
- title_font=dict(size=18, family="Arial", color="#000"),
216
- range=[x0, x1],
217
- tick0=tick_start,
218
- dtick=tick_spacing,
219
- ticks="outside", tickformat=",.0f",
220
- showline=True, linewidth=1.2, linecolor="#444", mirror=True,
221
- showgrid=True, gridcolor="rgba(0,0,0,0.12)", automargin=True
222
- )
223
 
224
- fig.update_yaxes(
225
- title_text="<b>Predicted UCS (psi)</b>",
226
- title_font=dict(size=18, family="Arial", color="#000"),
227
- range=[x0, x1],
228
- tick0=tick_start,
229
- dtick=tick_spacing,
230
- ticks="outside", tickformat=",.0f",
231
- showline=True, linewidth=1.2, linecolor="#444", mirror=True,
232
- showgrid=True, gridcolor="rgba(0,0,0,0.12)",
233
- scaleanchor="x", scaleratio=1, automargin=True
234
- )
235
 
236
  return fig
237
 
238
-
239
  def track_plot(df, include_actual=True):
240
  depth_col = next((c for c in df.columns if 'depth' in str(c).lower()), None)
241
- if depth_col is not None:
242
  y = pd.Series(df[depth_col]).astype(float)
243
  ylab = depth_col
244
- y_min, y_max = float(y.min()), float(y.max())
245
- y_range = [y_max, y_min] # reversed for log profile style
246
  else:
247
  y = pd.Series(np.arange(1, len(df) + 1))
248
  ylab = "Point Index"
249
- y_min, y_max = float(y.min()), float(y.max())
250
- y_range = [y_max, y_min]
251
 
252
- # X (UCS) range & ticks
 
253
  x_series = pd.Series(df.get("UCS_Pred", pd.Series(dtype=float))).astype(float)
254
  if include_actual and TARGET in df.columns:
255
  x_series = pd.concat([x_series, pd.Series(df[TARGET]).astype(float)], ignore_index=True)
 
256
  x_lo, x_hi = float(x_series.min()), float(x_series.max())
257
  x_pad = 0.03 * (x_hi - x_lo if x_hi > x_lo else 1.0)
258
  xmin, xmax = x_lo - x_pad, x_hi + x_pad
@@ -292,8 +288,8 @@ def track_plot(df, include_actual=True):
292
  title_text="<b>UCS (psi)</b>",
293
  title_font=dict(size=18, family="Arial", color="#000"),
294
  side="top", range=[xmin, xmax],
295
- ticks="outside", tickformat=",.0f",
296
- tickmode="auto", tick0=tick0,
297
  showline=True, linewidth=1.2, linecolor="#444", mirror=True,
298
  showgrid=True, gridcolor="rgba(0,0,0,0.12)", automargin=True
299
  )
@@ -308,9 +304,6 @@ def track_plot(df, include_actual=True):
308
  )
309
 
310
  return fig
311
-
312
-
313
-
314
  # ---------- Preview modal (matplotlib) ----------
315
  def preview_tracks(df: pd.DataFrame, cols: list[str]):
316
  cols = [c for c in cols if c in df.columns]
 
141
  return wrapper
142
  return deco
143
 
144
+ def rmse(y_true, y_pred):
145
+ return float(np.sqrt(mean_squared_error(y_true, y_pred)))
146
 
147
  @st.cache_resource(show_spinner=False)
148
  def load_model(model_path: str):
 
154
  xl = pd.ExcelFile(bio)
155
  return {sh: xl.parse(sh) for sh in xl.sheet_names}
156
 
157
+ def read_book_bytes(b: bytes):
158
+ return parse_excel(b) if b else {}
159
 
160
  def ensure_cols(df, cols):
161
  miss = [c for c in cols if c not in df.columns]
 
167
  def find_sheet(book, names):
168
  low2orig = {k.lower(): k for k in book.keys()}
169
  for nm in names:
170
+ if nm.lower() in low2orig:
171
+ return low2orig[nm.lower()]
172
  return None
173
 
174
  def _nice_tick0(xmin: float, step: int = 100) -> float:
175
+ return step * math.floor(xmin / step) if np.isfinite(xmin) else xmin
 
 
 
176
 
177
+ # ---------- cross_plot ----------
178
  def cross_plot(actual, pred):
179
  a = pd.Series(actual).astype(float)
180
  p = pd.Series(pred).astype(float)
181
 
182
  fixed_min = 6000
183
  fixed_max = 10000
184
+ tick_spacing = 500
185
+ tick_start = 6000
186
+
187
  fig = go.Figure()
188
 
189
  # Scatter points
 
194
  showlegend=False
195
  ))
196
 
197
+ # 1:1 diagonal line
198
+ fig.add_trace(go.Scatter(
199
+ x=[fixed_min, fixed_max], y=[fixed_min, fixed_max], mode="lines",
200
+ line=dict(color=COLORS["ref"], width=1.2, dash="dash"),
201
+ hoverinfo="skip", showlegend=False
202
+ ))
203
 
204
  fig.update_layout(
205
  width=CROSS_W, height=CROSS_H,
 
207
  margin=dict(l=64, r=18, t=10, b=48),
208
  hovermode="closest",
209
  font=dict(size=FONT_SZ),
210
+ dragmode=False
211
  )
212
 
213
+ fig.update_xaxes(
214
+ title_text="<b>Actual UCS (psi)</b>",
215
+ title_font=dict(size=18, family="Arial", color="#000"),
216
+ range=[fixed_min, fixed_max],
217
+ tick0=tick_start, dtick=tick_spacing,
218
+ ticks="outside", tickformat=",.0f",
219
+ showline=True, linewidth=1.2, linecolor="#444", mirror=True,
220
+ showgrid=True, gridcolor="rgba(0,0,0,0.12)", automargin=True
221
+ )
 
222
 
223
+ fig.update_yaxes(
224
+ title_text="<b>Predicted UCS (psi)</b>",
225
+ title_font=dict(size=18, family="Arial", color="#000"),
226
+ range=[fixed_min, fixed_max],
227
+ tick0=tick_start, dtick=tick_spacing,
228
+ ticks="outside", tickformat=",.0f",
229
+ showline=True, linewidth=1.2, linecolor="#444", mirror=True,
230
+ showgrid=True, gridcolor="rgba(0,0,0,0.12)",
231
+ scaleanchor="x", scaleratio=1, automargin=True
232
+ )
 
233
 
234
  return fig
235
 
236
+ # ---------- track_plot ----------
237
  def track_plot(df, include_actual=True):
238
  depth_col = next((c for c in df.columns if 'depth' in str(c).lower()), None)
239
+ if depth_col:
240
  y = pd.Series(df[depth_col]).astype(float)
241
  ylab = depth_col
 
 
242
  else:
243
  y = pd.Series(np.arange(1, len(df) + 1))
244
  ylab = "Point Index"
 
 
245
 
246
+ y_range = [float(y.max()), float(y.min())]
247
+
248
  x_series = pd.Series(df.get("UCS_Pred", pd.Series(dtype=float))).astype(float)
249
  if include_actual and TARGET in df.columns:
250
  x_series = pd.concat([x_series, pd.Series(df[TARGET]).astype(float)], ignore_index=True)
251
+
252
  x_lo, x_hi = float(x_series.min()), float(x_series.max())
253
  x_pad = 0.03 * (x_hi - x_lo if x_hi > x_lo else 1.0)
254
  xmin, xmax = x_lo - x_pad, x_hi + x_pad
 
288
  title_text="<b>UCS (psi)</b>",
289
  title_font=dict(size=18, family="Arial", color="#000"),
290
  side="top", range=[xmin, xmax],
291
+ tick0=tick0, tickmode="auto", tickformat=",.0f",
292
+ ticks="outside",
293
  showline=True, linewidth=1.2, linecolor="#444", mirror=True,
294
  showgrid=True, gridcolor="rgba(0,0,0,0.12)", automargin=True
295
  )
 
304
  )
305
 
306
  return fig
 
 
 
307
  # ---------- Preview modal (matplotlib) ----------
308
  def preview_tracks(df: pd.DataFrame, cols: list[str]):
309
  cols = [c for c in cols if c in df.columns]