Spaces:
Sleeping
Sleeping
Upload environment.py with huggingface_hub
Browse files- environment.py +10 -53
environment.py
CHANGED
|
@@ -51,38 +51,13 @@ class DataCleaningEnv:
|
|
| 51 |
|
| 52 |
def _load_datasets(self):
|
| 53 |
mapping = {
|
| 54 |
-
"easy_dedup_rename":
|
| 55 |
-
"medium_missing_dtype":
|
| 56 |
-
"hard_full_pipeline":
|
| 57 |
-
"expert_sales_pipeline": "expert"
|
| 58 |
}
|
| 59 |
folder = mapping.get(self.task_id, "easy")
|
| 60 |
self.current_df = pd.read_csv(f"datasets/{folder}/dirty.csv")
|
| 61 |
self.gold_df = pd.read_csv(f"datasets/{folder}/gold.csv")
|
| 62 |
-
# Apply random variations so agent cannot memorize
|
| 63 |
-
self.current_df = self._add_random_variation(self.current_df)
|
| 64 |
-
|
| 65 |
-
def _add_random_variation(self, df: pd.DataFrame) -> pd.DataFrame:
|
| 66 |
-
"""Add small random variations to prevent memorization."""
|
| 67 |
-
df = df.copy()
|
| 68 |
-
rng = np.random.default_rng()
|
| 69 |
-
|
| 70 |
-
for col in df.columns:
|
| 71 |
-
# Randomly shuffle some numeric values slightly
|
| 72 |
-
num_series = pd.to_numeric(df[col], errors="coerce")
|
| 73 |
-
if num_series.notna().sum() > 2:
|
| 74 |
-
# Add tiny noise to 1-2 numeric cells
|
| 75 |
-
idx = df.index[num_series.notna()].tolist()
|
| 76 |
-
if len(idx) >= 2:
|
| 77 |
-
pick = rng.choice(idx, size=min(2, len(idx)), replace=False)
|
| 78 |
-
for i in pick:
|
| 79 |
-
orig = num_series[i]
|
| 80 |
-
noise = rng.integers(-3, 4)
|
| 81 |
-
df.at[i, col] = str(int(orig + noise)) if isinstance(df.at[i, col], str) else orig + noise
|
| 82 |
-
|
| 83 |
-
# Randomly shuffle row order (keeps same data, different order)
|
| 84 |
-
df = df.sample(frac=1, random_state=rng.integers(0, 9999)).reset_index(drop=True)
|
| 85 |
-
return df
|
| 86 |
|
| 87 |
def _get_observation(self, message: str = "") -> Observation:
|
| 88 |
df = self.current_df
|
|
@@ -138,27 +113,12 @@ class DataCleaningEnv:
|
|
| 138 |
|
| 139 |
# ββ Missing value score ββββββββββββββββββββββββββββββββββββββ
|
| 140 |
if "missing_score" in scoring:
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
"expert_sales_pipeline": "expert"
|
| 148 |
-
}
|
| 149 |
-
folder = mapping.get(self.task_id, "easy")
|
| 150 |
-
orig_df = pd.read_csv(f"datasets/{folder}/dirty.csv")
|
| 151 |
-
orig_missing = int(orig_df.isnull().sum().sum())
|
| 152 |
-
curr_missing = int(df.isnull().sum().sum())
|
| 153 |
-
gold_missing = int(gold.isnull().sum().sum())
|
| 154 |
-
|
| 155 |
-
if orig_missing <= gold_missing:
|
| 156 |
-
missing_score = 1.0
|
| 157 |
-
else:
|
| 158 |
-
filled_needed = orig_missing - gold_missing
|
| 159 |
-
filled_done = orig_missing - curr_missing
|
| 160 |
-
filled_done = max(0, filled_done)
|
| 161 |
-
missing_score = filled_done / filled_needed
|
| 162 |
missing_score = max(0.0, min(1.0, missing_score))
|
| 163 |
|
| 164 |
# ββ Dtype score ββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -259,10 +219,7 @@ class DataCleaningEnv:
|
|
| 259 |
fill_val = numeric.median()
|
| 260 |
self.current_df[c] = numeric.fillna(fill_val)
|
| 261 |
elif strategy == "mode":
|
| 262 |
-
|
| 263 |
-
if len(mode_vals) == 0:
|
| 264 |
-
continue
|
| 265 |
-
fill_val = mode_vals.iloc[0]
|
| 266 |
self.current_df[c] = self.current_df[c].fillna(fill_val)
|
| 267 |
elif strategy == "ffill":
|
| 268 |
self.current_df[c] = self.current_df[c].ffill()
|
|
|
|
| 51 |
|
| 52 |
def _load_datasets(self):
|
| 53 |
mapping = {
|
| 54 |
+
"easy_dedup_rename": "easy",
|
| 55 |
+
"medium_missing_dtype": "medium",
|
| 56 |
+
"hard_full_pipeline": "hard"
|
|
|
|
| 57 |
}
|
| 58 |
folder = mapping.get(self.task_id, "easy")
|
| 59 |
self.current_df = pd.read_csv(f"datasets/{folder}/dirty.csv")
|
| 60 |
self.gold_df = pd.read_csv(f"datasets/{folder}/gold.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def _get_observation(self, message: str = "") -> Observation:
|
| 63 |
df = self.current_df
|
|
|
|
| 113 |
|
| 114 |
# ββ Missing value score ββββββββββββββββββββββββββββββββββββββ
|
| 115 |
if "missing_score" in scoring:
|
| 116 |
+
total_cells = df.shape[0] * df.shape[1]
|
| 117 |
+
missing_curr = int(df.isnull().sum().sum())
|
| 118 |
+
missing_gold = int(gold.isnull().sum().sum())
|
| 119 |
+
if total_cells > 0:
|
| 120 |
+
filled = max(0, missing_curr - missing_gold)
|
| 121 |
+
missing_score = 1.0 - (filled / total_cells)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
missing_score = max(0.0, min(1.0, missing_score))
|
| 123 |
|
| 124 |
# ββ Dtype score ββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 219 |
fill_val = numeric.median()
|
| 220 |
self.current_df[c] = numeric.fillna(fill_val)
|
| 221 |
elif strategy == "mode":
|
| 222 |
+
fill_val = self.current_df[c].mode()[0]
|
|
|
|
|
|
|
|
|
|
| 223 |
self.current_df[c] = self.current_df[c].fillna(fill_val)
|
| 224 |
elif strategy == "ffill":
|
| 225 |
self.current_df[c] = self.current_df[c].ffill()
|