LiamKhoaLe commited on
Commit
c25dbe3
·
1 Parent(s): 8f5cf0c

Upd rolling features

Browse files
Files changed (2) hide show
  1. app.py +6 -4
  2. utils/dbehavior_labeler.py +2 -2
app.py CHANGED
@@ -153,7 +153,7 @@ def ingest(entry: OBDEntry, background_tasks: BackgroundTasks):
153
  if entry.status == "end":
154
  background_tasks.add_task(process_data, norm_ts)
155
  return {"status": "processed"}
156
- # Normal row append
157
  try:
158
  df = pd.read_csv(RAW_CSV)
159
  row = {"timestamp": norm_ts, "driving_style": entry.driving_style}
@@ -477,8 +477,8 @@ def _process_and_save(df, norm_ts):
477
  import traceback
478
  logger.error(f"❌ UL labeling traceback: {traceback.format_exc()}")
479
  # Fallback: provide default labels
480
- logger.warning("⚠️ Using fallback: default 'Normal' labels")
481
- DRIVE_STYLE = ["Normal"] * len(df)
482
  df_labeled = df.copy()
483
  df_labeled["driving_style"] = DRIVE_STYLE
484
  labeled_path = os.path.join(CLEANED_DIR, f"cleaned_{norm_ts}_labeled.csv")
@@ -492,7 +492,9 @@ def _process_and_save(df, norm_ts):
492
  efficiency_labeler = EfficiencyLabeler.get()
493
  efficiency_preds = efficiency_labeler.predict_df(df)
494
  # update global FUEL_EFFICIENCY (overwrite if already exists)
495
- FUEL_EFFICIENCY = [float(p) for p in efficiency_preds]
 
 
496
  # write efficiency CSV (fuel_efficiency column)
497
  df_efficiency = df_for_persist.copy()
498
  df_efficiency["fuel_efficiency"] = FUEL_EFFICIENCY
 
153
  if entry.status == "end":
154
  background_tasks.add_task(process_data, norm_ts)
155
  return {"status": "processed"}
156
+ # Moderate row append
157
  try:
158
  df = pd.read_csv(RAW_CSV)
159
  row = {"timestamp": norm_ts, "driving_style": entry.driving_style}
 
477
  import traceback
478
  logger.error(f"❌ UL labeling traceback: {traceback.format_exc()}")
479
  # Fallback: provide default labels
480
+ logger.warning("⚠️ Using fallback: default 'Moderate' labels")
481
+ DRIVE_STYLE = ["Moderate"] * len(df)
482
  df_labeled = df.copy()
483
  df_labeled["driving_style"] = DRIVE_STYLE
484
  labeled_path = os.path.join(CLEANED_DIR, f"cleaned_{norm_ts}_labeled.csv")
 
492
  efficiency_labeler = EfficiencyLabeler.get()
493
  efficiency_preds = efficiency_labeler.predict_df(df)
494
  # update global FUEL_EFFICIENCY (overwrite if already exists)
495
+ # Efficiency is predicted per drive, so replicate the single score for all rows
496
+ efficiency_score = float(efficiency_preds[0]) if efficiency_preds else 0.0
497
+ FUEL_EFFICIENCY = [efficiency_score] * len(df_for_persist)
498
  # write efficiency CSV (fuel_efficiency column)
499
  df_efficiency = df_for_persist.copy()
500
  df_efficiency["fuel_efficiency"] = FUEL_EFFICIENCY
utils/dbehavior_labeler.py CHANGED
@@ -128,8 +128,8 @@ def engineer_features(df):
128
  def add_roll(col):
129
  if col not in fe.columns:
130
  return
131
- # Safety check to prevent infinite recursion
132
- if col.startswith(('SPEED_', 'RPM_', 'THROTTLE_', 'MAF_', 'ENGINE_', 'ACCEL_', 'JERK_')):
133
  return # Skip if already a rolling feature
134
  for w, tag in [(W1, "w1"), (W2, "w2"), (W5, "w5"), (W8, "w8")]:
135
  fe[f"{col}_mean_{tag}"] = fe[col].rolling(w, min_periods=1, center=True).mean()
 
128
  def add_roll(col):
129
  if col not in fe.columns:
130
  return
131
+ # Safety check to prevent infinite recursion - only skip if it already has rolling suffix
132
+ if any(col.endswith(suffix) for suffix in ['_mean_w1', '_std_w1', '_mean_w2', '_std_w2', '_mean_w5', '_std_w5', '_mean_w8', '_std_w8', '_min_w1', '_max_w1']):
133
  return # Skip if already a rolling feature
134
  for w, tag in [(W1, "w1"), (W2, "w2"), (W5, "w5"), (W8, "w8")]:
135
  fe[f"{col}_mean_{tag}"] = fe[col].rolling(w, min_periods=1, center=True).mean()