Spaces:
Sleeping
Sleeping
add augment py
Browse files- augment_dataset.py +416 -183
augment_dataset.py
CHANGED
|
@@ -22,222 +22,376 @@ def require_env(var_name: str) -> str:
|
|
| 22 |
return value
|
| 23 |
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
"""๊ฐ์ ๋
ธ์ด์ฆ ์ถ๊ฐ"""
|
| 27 |
if value is None:
|
| 28 |
return None
|
| 29 |
-
return
|
| 30 |
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
"""๊ฐ
|
| 34 |
if value is None:
|
| 35 |
return None
|
| 36 |
-
return
|
| 37 |
|
| 38 |
|
| 39 |
-
def
|
| 40 |
-
"""
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
if "timestamp_utc" in
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
except:
|
| 50 |
pass
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
if "window_id" in
|
| 54 |
-
|
| 55 |
-
if "window_start_ms" in
|
| 56 |
-
|
| 57 |
-
if "window_end_ms" in
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
if
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
#
|
| 79 |
-
if
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
if "linacc_z_mean" in augmented and augmented["linacc_z_mean"] is not None:
|
| 84 |
-
augmented["linacc_z_mean"] = add_noise(augmented["linacc_z_mean"], abs(augmented["linacc_z_mean"]) * 0.1 + 0.01)
|
| 85 |
-
|
| 86 |
-
# ์ค๋ ฅ ๋ฒกํฐ ์ฆํญ (๋ฌผ๋ฆฌ์ ์ ์ฝ: ํฌ๊ธฐ๊ฐ ์ฝ 9.8์ ๊ฐ๊น์์ผ ํจ)
|
| 87 |
-
if all(f in augmented and augmented[f] is not None for f in ["gravity_x_mean", "gravity_y_mean", "gravity_z_mean"]):
|
| 88 |
-
gx = augmented["gravity_x_mean"] + random.uniform(-0.01, 0.01)
|
| 89 |
-
gy = augmented["gravity_y_mean"] + random.uniform(-0.01, 0.01)
|
| 90 |
-
gz = augmented["gravity_z_mean"] + random.uniform(-0.02, 0.02)
|
| 91 |
-
# ์ค๋ ฅ ๋ฒกํฐ ํฌ๊ธฐ ์ ๊ทํ (์ฝ 9.8 ์ ์ง)
|
| 92 |
g_mag = np.sqrt(gx**2 + gy**2 + gz**2)
|
| 93 |
if g_mag > 0:
|
| 94 |
scale = 9.8 / g_mag
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
"
|
| 102 |
-
|
| 103 |
-
]
|
| 104 |
-
for field in sensor_std_fields:
|
| 105 |
-
if field in augmented and augmented[field] is not None:
|
| 106 |
-
augmented[field] = bounded(add_noise(augmented[field], augmented[field] * 0.1), 0.01, 1.0)
|
| 107 |
-
|
| 108 |
-
# RMS ๊ฐ ์ฆํญ (์ผ์ ํ๊ท ๊ฐ๊ณผ ์ผ๊ด์ฑ ์ ์ง)
|
| 109 |
-
if "rms_acc" in augmented and augmented["rms_acc"] is not None:
|
| 110 |
-
# RMS๋ ๊ฐ์๋ ํ๊ท ๊ฐ์ ํฌ๊ธฐ์ ๊ด๋ จ
|
| 111 |
-
acc_mag = np.sqrt(
|
| 112 |
-
(augmented.get("acc_x_mean", 0) or 0)**2 +
|
| 113 |
-
(augmented.get("acc_y_mean", 0) or 0)**2 +
|
| 114 |
-
(augmented.get("acc_z_mean", 0) or 0)**2
|
| 115 |
)
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
(augmented.get("gyro_x_mean", 0) or 0)**2 +
|
| 123 |
-
(augmented.get("gyro_y_mean", 0) or 0)**2 +
|
| 124 |
-
(augmented.get("gyro_z_mean", 0) or 0)**2
|
| 125 |
)
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
#
|
| 130 |
-
if "
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
if "
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
if "
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
if "fatigue_prev" in augmented and augmented["fatigue_prev"] is not None:
|
| 169 |
-
if "fatigue" in augmented and augmented["fatigue"] is not None:
|
| 170 |
-
# ์ด์ ํผ๋ก๋๋ ํ์ฌ ํผ๋ก๋๋ณด๋ค ์ฝ๊ฐ ๋ฎ๊ฑฐ๋ ๋น์ท
|
| 171 |
-
augmented["fatigue_prev"] = bounded(augmented["fatigue"] - random.uniform(0, 0.1), 0.05, 0.95)
|
| 172 |
else:
|
| 173 |
-
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
try:
|
| 179 |
-
emb_list = json.loads(augmented["user_emb"])
|
| 180 |
-
except:
|
| 181 |
-
emb_list = augmented["user_emb"]
|
| 182 |
else:
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
-
if
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
if random.random() < 0.05: # 5% ํ๋ฅ ๋ก ๋ณ๊ฒฝ
|
| 195 |
-
|
|
|
|
|
|
|
| 196 |
|
| 197 |
# session_id ์ฝ๊ฐ ๋ณํ
|
| 198 |
-
if "session_id" in
|
| 199 |
-
parts =
|
| 200 |
if len(parts) > 1:
|
| 201 |
try:
|
| 202 |
session_num = int(parts[-1])
|
| 203 |
-
|
| 204 |
except:
|
| 205 |
-
|
|
|
|
|
|
|
| 206 |
|
| 207 |
-
return
|
| 208 |
|
| 209 |
|
| 210 |
-
def augment_user_data(df: pd.DataFrame, target_count: int) -> pd.DataFrame:
|
| 211 |
-
"""
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
if current_count >= target_count:
|
| 217 |
-
# ์ด๋ฏธ ์ถฉ๋ถํ๋ฉด ๊ทธ๋๋ก ๋ฐํ
|
| 218 |
return df.head(target_count)
|
| 219 |
|
| 220 |
-
|
| 221 |
-
needed = target_count - current_count
|
| 222 |
|
| 223 |
-
# ๊ธฐ์กด ๋ฐ์ดํฐ
|
| 224 |
-
|
| 225 |
-
for _ in range(needed):
|
| 226 |
-
# ๋๋คํ๊ฒ ์๋ณธ ๋ ์ฝ๋ ์ ํ
|
| 227 |
-
original_idx = random.randint(0, current_count - 1)
|
| 228 |
-
original = df.iloc[original_idx].to_dict()
|
| 229 |
-
|
| 230 |
-
# ์ฆํญ (๋
ธ์ด์ฆ ์ค์ผ์ผ์ ํ๋์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ)
|
| 231 |
-
noise_scale = random.uniform(0.05, 0.15)
|
| 232 |
-
augmented = augment_record(original, noise_scale)
|
| 233 |
-
augmented_records.append(augmented)
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
|
| 243 |
def main():
|
|
@@ -270,6 +424,11 @@ def main():
|
|
| 270 |
else:
|
| 271 |
user_id = filename_no_ext
|
| 272 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
# ๊ฐ๋ณ ํ์ผ์ pandas๋ก ์ง์ ๋ก๋
|
| 274 |
from huggingface_hub import hf_hub_download
|
| 275 |
import tempfile
|
|
@@ -296,9 +455,13 @@ def main():
|
|
| 296 |
print(f"โ ๋ฐ์ดํฐ์
๋ก๋ ์์ ์คํจ: {e3}")
|
| 297 |
return
|
| 298 |
|
| 299 |
-
# ์ ํจํ ์ฌ์ฉ์๋ง ํํฐ๋ง (๋ฐ์ดํฐ๊ฐ ์๋ ์ฌ์ฉ์๋ง)
|
| 300 |
valid_users = {}
|
| 301 |
for user_id in existing.keys():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
try:
|
| 303 |
user_data = existing[user_id]
|
| 304 |
if len(user_data) > 0:
|
|
@@ -346,10 +509,18 @@ def main():
|
|
| 346 |
continue
|
| 347 |
|
| 348 |
try:
|
| 349 |
-
# ์ฐธ์กฐ ๋ฐ์ดํฐ๋ฅผ ์ฆํญํ์ฌ ์๋ก์ด ์ฌ์ฉ์ ๋ฐ์ดํฐ ์์ฑ
|
| 350 |
-
new_user_df = augment_user_data(reference_df, RECORDS_PER_USER)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
new_user_datasets[new_user_id] = Dataset.from_pandas(new_user_df, preserve_index=False)
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
| 353 |
except Exception as e:
|
| 354 |
print(f"โ {new_user_id}: ์์ฑ ์คํจ ({e}), ๊ฑด๋๋")
|
| 355 |
continue
|
|
@@ -358,6 +529,38 @@ def main():
|
|
| 358 |
print("โ ์๋ก์ด ์ฌ์ฉ์ ๋ฐ์ดํฐ๊ฐ ์์ฑ๋์ง ์์์ต๋๋ค.")
|
| 359 |
return
|
| 360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
# ๊ธฐ์กด ๋ฐ์ดํฐ์
์ ์๋ก์ด ์ฌ์ฉ์ ๋ฐ์ดํฐ ์ถ๊ฐ
|
| 362 |
final_datasets = {}
|
| 363 |
# ๊ธฐ์กด ์ฌ์ฉ์ ๋ฐ์ดํฐ ์ ์ง
|
|
@@ -374,6 +577,36 @@ def main():
|
|
| 374 |
print(f"๐ ์ ์ฒด ๋ฐ์ดํฐ์
์ด ๋ ์ฝ๋ ์: {total_records}")
|
| 375 |
print(f"๐ ์๋ก์ด parquet ํ์ผ ์: {len(new_user_datasets)}๊ฐ")
|
| 376 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
print(f"๐ค Hugging Face Hub์ ์
๋ก๋ ์ค: {repo_id}")
|
| 378 |
final_dict.push_to_hub(repo_id, token=token, private=True)
|
| 379 |
print("โ
์
๋ก๋ ์๋ฃ")
|
|
|
|
| 22 |
return value
|
| 23 |
|
| 24 |
|
| 25 |
+
def jitter(value: float, scale: float = 0.02) -> float:
|
| 26 |
+
"""๊ฐ์ ยฑscale ๋น์จ์ ๋
ธ์ด์ฆ๋ฅผ ์ถ๊ฐ"""
|
| 27 |
if value is None:
|
| 28 |
return None
|
| 29 |
+
return value * (1 + random.uniform(-scale, scale))
|
| 30 |
|
| 31 |
|
| 32 |
+
def jitter_abs(value: float, amount: float) -> float:
|
| 33 |
+
"""์ ๋๊ฐ ๊ธฐ์ค ๋
ธ์ด์ฆ ์ถ๊ฐ"""
|
| 34 |
if value is None:
|
| 35 |
return None
|
| 36 |
+
return value + random.uniform(-amount, amount)
|
| 37 |
|
| 38 |
|
| 39 |
+
def augment_sensor_vector(x: float, y: float, z: float, noise: float = 0.02) -> tuple:
|
| 40 |
+
"""
|
| 41 |
+
3์ถ ์ผ์ ๋ฐ์ดํฐ๋ฅผ ๋ฌผ๋ฆฌ์ ์ผ๋ก ์์ฐ์ค๋ฝ๊ฒ ์ฆํญ
|
| 42 |
+
โ 3์ถ์ ๋์ผํ ๋น์จ๋ก scaling + ๊ฐ๋ณ ์์ ๋
ธ์ด์ฆ
|
| 43 |
+
"""
|
| 44 |
+
if x is None or y is None or z is None:
|
| 45 |
+
return (x, y, z)
|
| 46 |
+
scale = 1 + random.uniform(-noise, noise)
|
| 47 |
+
return (
|
| 48 |
+
round(x * scale + random.uniform(-0.01, 0.01), 4),
|
| 49 |
+
round(y * scale + random.uniform(-0.01, 0.01), 4),
|
| 50 |
+
round(z * scale + random.uniform(-0.01, 0.01), 4),
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def compute_rms(x: float, y: float, z: float, base_noise: float = 0.02) -> float:
|
| 55 |
+
"""3์ถ mean ๊ธฐ๋ฐ์ผ๋ก RMS ์ฌ๊ณ์ฐ"""
|
| 56 |
+
if x is None or y is None or z is None:
|
| 57 |
+
return None
|
| 58 |
+
base = np.sqrt(x**2 + y**2 + z**2)
|
| 59 |
+
return round(base * (1 + random.uniform(-base_noise, base_noise)), 4)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def augment_record_strict(row: dict) -> dict:
|
| 63 |
+
"""๋ฌผ๋ฆฌ์ ์ ์ฝ์ ์งํค๋ฉด์ ์ผ์ ๋ฐ์ดํฐ๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ์ฆํญ"""
|
| 64 |
+
new = row.copy()
|
| 65 |
|
| 66 |
+
# timestamp jitter
|
| 67 |
+
if "timestamp_utc" in row and isinstance(row["timestamp_utc"], str):
|
| 68 |
try:
|
| 69 |
+
t = datetime.fromisoformat(row["timestamp_utc"].replace("Z", "+00:00"))
|
| 70 |
+
t = t + timedelta(milliseconds=random.randint(-150, 150))
|
| 71 |
+
new["timestamp_utc"] = t.isoformat()
|
| 72 |
except:
|
| 73 |
pass
|
| 74 |
|
| 75 |
+
# window jitter
|
| 76 |
+
if "window_id" in row and row["window_id"] is not None:
|
| 77 |
+
new["window_id"] = int(row["window_id"] + random.randint(-1, 1))
|
| 78 |
+
if "window_start_ms" in row and row["window_start_ms"] is not None:
|
| 79 |
+
new["window_start_ms"] = row["window_start_ms"] + random.randint(-50, 50)
|
| 80 |
+
if "window_end_ms" in row and row["window_end_ms"] is not None:
|
| 81 |
+
new["window_end_ms"] = new["window_start_ms"] + 2000 # window_size_ms์ ์ผ์น
|
| 82 |
+
|
| 83 |
+
# --- Accelerometer mean ---
|
| 84 |
+
if all(f in row and row[f] is not None for f in ["acc_x_mean", "acc_y_mean", "acc_z_mean"]):
|
| 85 |
+
new["acc_x_mean"], new["acc_y_mean"], new["acc_z_mean"] = augment_sensor_vector(
|
| 86 |
+
row["acc_x_mean"], row["acc_y_mean"], row["acc_z_mean"], noise=0.03
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# --- Gyro mean ---
|
| 90 |
+
if all(f in row and row[f] is not None for f in ["gyro_x_mean", "gyro_y_mean", "gyro_z_mean"]):
|
| 91 |
+
new["gyro_x_mean"], new["gyro_y_mean"], new["gyro_z_mean"] = augment_sensor_vector(
|
| 92 |
+
row["gyro_x_mean"], row["gyro_y_mean"], row["gyro_z_mean"], noise=0.03
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# --- Linear accel mean ---
|
| 96 |
+
if all(f in row and row[f] is not None for f in ["linacc_x_mean", "linacc_y_mean", "linacc_z_mean"]):
|
| 97 |
+
new["linacc_x_mean"], new["linacc_y_mean"], new["linacc_z_mean"] = augment_sensor_vector(
|
| 98 |
+
row["linacc_x_mean"], row["linacc_y_mean"], row["linacc_z_mean"], noise=0.03
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# --- Gravity vector (๋ฌผ๋ฆฌ์ ์ ์ฝ: ํฌ๊ธฐ๊ฐ ์ฝ 9.8) ---
|
| 102 |
+
if all(f in row and row[f] is not None for f in ["gravity_x_mean", "gravity_y_mean", "gravity_z_mean"]):
|
| 103 |
+
gx, gy, gz = augment_sensor_vector(
|
| 104 |
+
row["gravity_x_mean"], row["gravity_y_mean"], row["gravity_z_mean"], noise=0.01
|
| 105 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
g_mag = np.sqrt(gx**2 + gy**2 + gz**2)
|
| 107 |
if g_mag > 0:
|
| 108 |
scale = 9.8 / g_mag
|
| 109 |
+
new["gravity_x_mean"] = round(gx * scale, 4)
|
| 110 |
+
new["gravity_y_mean"] = round(gy * scale, 4)
|
| 111 |
+
new["gravity_z_mean"] = round(gz * scale, 4)
|
| 112 |
+
|
| 113 |
+
# --- Recompute RMS from sensor means ---
|
| 114 |
+
if all(f in new and new[f] is not None for f in ["acc_x_mean", "acc_y_mean", "acc_z_mean"]):
|
| 115 |
+
new["rms_acc"] = compute_rms(
|
| 116 |
+
new["acc_x_mean"], new["acc_y_mean"], new["acc_z_mean"], base_noise=0.03
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
)
|
| 118 |
+
elif "rms_acc" in row and row["rms_acc"] is not None:
|
| 119 |
+
new["rms_acc"] = jitter(row["rms_acc"], 0.03)
|
| 120 |
+
|
| 121 |
+
if all(f in new and new[f] is not None for f in ["gyro_x_mean", "gyro_y_mean", "gyro_z_mean"]):
|
| 122 |
+
new["rms_gyro"] = compute_rms(
|
| 123 |
+
new["gyro_x_mean"], new["gyro_y_mean"], new["gyro_z_mean"], base_noise=0.03
|
|
|
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
+
elif "rms_gyro" in row and row["rms_gyro"] is not None:
|
| 126 |
+
new["rms_gyro"] = jitter(row["rms_gyro"], 0.03)
|
| 127 |
+
|
| 128 |
+
# --- std values scale with RMS ---
|
| 129 |
+
if "rms_acc" in new and new["rms_acc"] is not None and "rms_acc" in row and row["rms_acc"] is not None and row["rms_acc"] > 0:
|
| 130 |
+
rms_ratio = new["rms_acc"] / row["rms_acc"]
|
| 131 |
+
for col in ["acc_x_std", "acc_y_std", "acc_z_std"]:
|
| 132 |
+
if col in row and row[col] is not None:
|
| 133 |
+
new[col] = max(0.01, row[col] * rms_ratio * jitter(1, 0.1))
|
| 134 |
+
|
| 135 |
+
if "rms_gyro" in new and new["rms_gyro"] is not None and "rms_gyro" in row and row["rms_gyro"] is not None and row["rms_gyro"] > 0:
|
| 136 |
+
rms_ratio = new["rms_gyro"] / row["rms_gyro"]
|
| 137 |
+
for col in ["gyro_x_std", "gyro_y_std", "gyro_z_std"]:
|
| 138 |
+
if col in row and row[col] is not None:
|
| 139 |
+
new[col] = max(0.001, row[col] * rms_ratio * jitter(1, 0.1))
|
| 140 |
+
|
| 141 |
+
# --- frequency (weak positive correlation with RMS) ---
|
| 142 |
+
if "mean_freq_acc" in row and row["mean_freq_acc"] is not None and "rms_acc" in new and new["rms_acc"] is not None:
|
| 143 |
+
new["mean_freq_acc"] = round(jitter_abs(row["mean_freq_acc"], new["rms_acc"] * 0.3), 2)
|
| 144 |
+
elif "mean_freq_acc" in row and row["mean_freq_acc"] is not None:
|
| 145 |
+
new["mean_freq_acc"] = round(jitter(row["mean_freq_acc"], 0.02), 2)
|
| 146 |
+
|
| 147 |
+
if "mean_freq_gyro" in row and row["mean_freq_gyro"] is not None and "rms_gyro" in new and new["rms_gyro"] is not None:
|
| 148 |
+
new["mean_freq_gyro"] = round(jitter_abs(row["mean_freq_gyro"], new["rms_gyro"] * 0.3), 2)
|
| 149 |
+
elif "mean_freq_gyro" in row and row["mean_freq_gyro"] is not None:
|
| 150 |
+
new["mean_freq_gyro"] = round(jitter(row["mean_freq_gyro"], 0.02), 2)
|
| 151 |
+
|
| 152 |
+
# --- entropy: increases when RMS increases ---
|
| 153 |
+
if "entropy_acc" in row and row["entropy_acc"] is not None and "rms_acc" in new and new["rms_acc"] is not None and "rms_acc" in row and row["rms_acc"] is not None and row["rms_acc"] > 0:
|
| 154 |
+
new["entropy_acc"] = min(1.0, max(0.05, row["entropy_acc"] * (new["rms_acc"] / row["rms_acc"]) * jitter(1, 0.1)))
|
| 155 |
+
elif "entropy_acc" in row and row["entropy_acc"] is not None:
|
| 156 |
+
new["entropy_acc"] = min(1.0, max(0.05, jitter(row["entropy_acc"], 0.02)))
|
| 157 |
+
|
| 158 |
+
if "entropy_gyro" in row and row["entropy_gyro"] is not None and "rms_gyro" in new and new["rms_gyro"] is not None and "rms_gyro" in row and row["rms_gyro"] is not None and row["rms_gyro"] > 0:
|
| 159 |
+
new["entropy_gyro"] = min(1.0, max(0.05, row["entropy_gyro"] * (new["rms_gyro"] / row["rms_gyro"]) * jitter(1, 0.1)))
|
| 160 |
+
elif "entropy_gyro" in row and row["entropy_gyro"] is not None:
|
| 161 |
+
new["entropy_gyro"] = min(1.0, max(0.05, jitter(row["entropy_gyro"], 0.02)))
|
| 162 |
+
|
| 163 |
+
# --- jerk: depends on std and RMS ---
|
| 164 |
+
if "jerk_mean" in row and row["jerk_mean"] is not None:
|
| 165 |
+
if "acc_x_std" in row and row["acc_x_std"] is not None:
|
| 166 |
+
new["jerk_mean"] = round(jitter_abs(row["jerk_mean"], row["acc_x_std"] * 0.3), 4)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
else:
|
| 168 |
+
new["jerk_mean"] = round(jitter(row["jerk_mean"], 0.02), 4)
|
| 169 |
|
| 170 |
+
if "jerk_std" in row and row["jerk_std"] is not None:
|
| 171 |
+
if "acc_x_std" in row and row["acc_x_std"] is not None:
|
| 172 |
+
new["jerk_std"] = max(0.001, round(jitter_abs(row["jerk_std"], row["acc_x_std"] * 0.1), 4))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
else:
|
| 174 |
+
new["jerk_std"] = max(0.001, round(jitter(row["jerk_std"], 0.01), 4))
|
| 175 |
+
|
| 176 |
+
# --- stability index (inverse to entropy) ---
|
| 177 |
+
entropy_avg = 0.5
|
| 178 |
+
if "entropy_acc" in new and new["entropy_acc"] is not None and "entropy_gyro" in new and new["entropy_gyro"] is not None:
|
| 179 |
+
entropy_avg = (new["entropy_acc"] + new["entropy_gyro"]) / 2
|
| 180 |
+
elif "entropy_acc" in new and new["entropy_acc"] is not None:
|
| 181 |
+
entropy_avg = new["entropy_acc"]
|
| 182 |
+
elif "entropy_gyro" in new and new["entropy_gyro"] is not None:
|
| 183 |
+
entropy_avg = new["entropy_gyro"]
|
| 184 |
+
|
| 185 |
+
new["stability_index"] = round(max(0.4, min(0.99, 1 - entropy_avg * 0.3)), 4)
|
| 186 |
+
|
| 187 |
+
# --- fatigue model (RMS, ์ฃผํ์ ๊ธฐ๋ฐ) ---
|
| 188 |
+
# fatigue๋ augment_user_data์์ ์๊ฐ์ ์ฐ์์ฑ์ ๊ณ ๋ คํ์ฌ ๊ณ์ฐ
|
| 189 |
+
# ์ฌ๊ธฐ์๋ ๊ธฐ๋ณธ๊ฐ๋ง ์ค์ (๋์ค์ ๋ฎ์ด์์์ง)
|
| 190 |
+
if "fatigue" in row and row["fatigue"] is not None:
|
| 191 |
+
# ๊ธฐ๋ณธ์ ์ผ๋ก RMS์ ์ฃผํ์ ๊ธฐ๋ฐ์ผ๋ก ์ฝ๊ฐ ์กฐ์
|
| 192 |
+
if "rms_acc" in new and new["rms_acc"] is not None and "rms_acc" in row and row["rms_acc"] is not None and row["rms_acc"] > 0.1:
|
| 193 |
+
rms_factor = new["rms_acc"] / row["rms_acc"]
|
| 194 |
+
else:
|
| 195 |
+
rms_factor = 1.0
|
| 196 |
|
| 197 |
+
if "mean_freq_acc" in new and new["mean_freq_acc"] is not None and "mean_freq_acc" in row and row["mean_freq_acc"] is not None and row["mean_freq_acc"] > 1:
|
| 198 |
+
freq_factor = row["mean_freq_acc"] / new["mean_freq_acc"]
|
| 199 |
+
else:
|
| 200 |
+
freq_factor = 1.0
|
| 201 |
+
|
| 202 |
+
fatigue_delta = rms_factor * 0.05 - freq_factor * 0.03
|
| 203 |
+
new["fatigue"] = min(0.95, max(0.05, row["fatigue"] + fatigue_delta + random.uniform(-0.02, 0.02)))
|
| 204 |
+
new["fatigue_level"] = 0 if new["fatigue"] < 0.3 else 1 if new["fatigue"] < 0.6 else 2
|
| 205 |
+
else:
|
| 206 |
+
# fatigue๊ฐ ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ ์ค์
|
| 207 |
+
new["fatigue"] = 0.1
|
| 208 |
+
new["fatigue_level"] = 0
|
| 209 |
+
|
| 210 |
+
# fatigue_prev๋ augment_user_data์์ ์ค์ ๋จ
|
| 211 |
+
if "fatigue_prev" in row and row["fatigue_prev"] is not None:
|
| 212 |
+
new["fatigue_prev"] = row["fatigue_prev"]
|
| 213 |
+
else:
|
| 214 |
+
new["fatigue_prev"] = 0.05
|
| 215 |
+
|
| 216 |
+
# --- baseline values (preserve) ---
|
| 217 |
+
if "rms_base" in row:
|
| 218 |
+
new["rms_base"] = row["rms_base"]
|
| 219 |
+
if "freq_base" in row:
|
| 220 |
+
new["freq_base"] = row["freq_base"]
|
| 221 |
+
|
| 222 |
+
# --- user_emb: NEVER change ---
|
| 223 |
+
if "user_emb" in row:
|
| 224 |
+
new["user_emb"] = row["user_emb"]
|
| 225 |
+
|
| 226 |
+
# --- other fields ---
|
| 227 |
+
if "overlap_rate" in row and row["overlap_rate"] is not None:
|
| 228 |
+
new["overlap_rate"] = max(0.3, min(0.7, jitter(row["overlap_rate"], 0.02)))
|
| 229 |
+
|
| 230 |
+
if "window_size_ms" in row:
|
| 231 |
+
new["window_size_ms"] = row.get("window_size_ms", 2000)
|
| 232 |
+
|
| 233 |
+
if "quality_flag" in row:
|
| 234 |
if random.random() < 0.05: # 5% ํ๋ฅ ๋ก ๋ณ๊ฒฝ
|
| 235 |
+
new["quality_flag"] = 0 if row["quality_flag"] == 1 else 1
|
| 236 |
+
else:
|
| 237 |
+
new["quality_flag"] = row["quality_flag"]
|
| 238 |
|
| 239 |
# session_id ์ฝ๊ฐ ๋ณํ
|
| 240 |
+
if "session_id" in row and row["session_id"]:
|
| 241 |
+
parts = str(row["session_id"]).split("_")
|
| 242 |
if len(parts) > 1:
|
| 243 |
try:
|
| 244 |
session_num = int(parts[-1])
|
| 245 |
+
new["session_id"] = "_".join(parts[:-1]) + "_" + str(session_num + random.randint(-5, 5))
|
| 246 |
except:
|
| 247 |
+
new["session_id"] = row["session_id"]
|
| 248 |
+
else:
|
| 249 |
+
new["session_id"] = row["session_id"]
|
| 250 |
|
| 251 |
+
return new
|
| 252 |
|
| 253 |
|
| 254 |
+
def augment_user_data(df: pd.DataFrame, target_count: int, new_user_id: str = None) -> pd.DataFrame:
|
| 255 |
+
"""
|
| 256 |
+
์ฌ์ฉ์๋ณ ๋ฐ์ดํฐ๋ฅผ ์ฆํญํ์ฌ ๋ชฉํ ๊ฐ์๋งํผ ์์ฑ
|
| 257 |
+
์๋ก์ด ์ฌ์ฉ์์ธ ๊ฒฝ์ฐ ์๊ฐ์ ์ฐ์์ฑ์ ์ ์ง
|
| 258 |
+
"""
|
| 259 |
+
if len(df) >= target_count:
|
|
|
|
|
|
|
| 260 |
return df.head(target_count)
|
| 261 |
|
| 262 |
+
need = target_count - len(df)
|
|
|
|
| 263 |
|
| 264 |
+
# ์๋ก์ด ์ฌ์ฉ์์ธ ๊ฒฝ์ฐ (๊ธฐ์กด ๋ฐ์ดํฐ๊ฐ ์๊ฑฐ๋ ์ ์ฌ์ฉ์ ID๊ฐ ์ ๊ณต๋ ๊ฒฝ์ฐ)
|
| 265 |
+
is_new_user = new_user_id is not None or len(df) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
+
if is_new_user and len(df) > 0:
|
| 268 |
+
# ์๋ก์ด ์ฌ์ฉ์๋ ํญ์ target_count๋งํผ ์์ฑ (์ฐธ์กฐ ๋ฐ์ดํฐ ๊ธธ์ด์ ๋ฌด๊ด)
|
| 269 |
+
base_row = df.iloc[0].to_dict()
|
| 270 |
+
new_rows = []
|
| 271 |
+
|
| 272 |
+
# ์๊ฐ ๊ธฐ๋ฐ ์ด๊ธฐ๊ฐ ์ค์
|
| 273 |
+
if "timestamp_utc" in base_row and base_row["timestamp_utc"]:
|
| 274 |
+
try:
|
| 275 |
+
base_time = datetime.fromisoformat(str(base_row["timestamp_utc"]).replace("Z", "+00:00"))
|
| 276 |
+
except:
|
| 277 |
+
base_time = datetime.now(timezone.utc)
|
| 278 |
+
else:
|
| 279 |
+
base_time = datetime.now(timezone.utc)
|
| 280 |
+
|
| 281 |
+
base_window_id = 1 # ์ ์ฌ์ฉ์๋ window_id๋ฅผ 1๋ถํฐ ์์
|
| 282 |
+
base_window_start = 0 # ์ ์ฌ์ฉ์๋ window_start_ms๋ฅผ 0๋ถํฐ ์์
|
| 283 |
+
prev_fatigue = base_row.get("fatigue", 0.1) if base_row.get("fatigue") is not None else 0.1
|
| 284 |
+
|
| 285 |
+
# ์๋ก์ด ์ฌ์ฉ์๋ ํญ์ target_count๋งํผ ์์ฑ
|
| 286 |
+
for i in range(target_count):
|
| 287 |
+
# ์ํ ๋ ์ฝ๋ ์ ํ
|
| 288 |
+
sample_idx = random.randint(0, len(df) - 1)
|
| 289 |
+
sample = df.iloc[sample_idx].to_dict()
|
| 290 |
+
|
| 291 |
+
# ์๋ก์ด ๋ ์ฝ๋ ์์ฑ
|
| 292 |
+
new_row = augment_record_strict(sample)
|
| 293 |
+
|
| 294 |
+
# ์๋ก์ด ์ฌ์ฉ์ ID ์ค์
|
| 295 |
+
if new_user_id:
|
| 296 |
+
new_row["user_id"] = new_user_id
|
| 297 |
+
|
| 298 |
+
# ์๊ฐ์ ์ฐ์์ฑ ์ ์ง
|
| 299 |
+
window_interval = 2000 # window_size_ms
|
| 300 |
+
new_row["window_id"] = base_window_id + i
|
| 301 |
+
new_row["window_start_ms"] = base_window_start + i * window_interval
|
| 302 |
+
new_row["window_end_ms"] = new_row["window_start_ms"] + window_interval
|
| 303 |
+
|
| 304 |
+
# timestamp ์ฐ์์ฑ ์ ์ง
|
| 305 |
+
new_row["timestamp_utc"] = (base_time + timedelta(milliseconds=i * window_interval)).isoformat()
|
| 306 |
+
|
| 307 |
+
# ํผ๋ก๋ ์ฐ์์ฑ ์ ์ง (์ด์ ํผ๋ก๋๋ ์ง์ ๋ ์ฝ๋์ ํผ๋ก๋)
|
| 308 |
+
if i > 0:
|
| 309 |
+
new_row["fatigue_prev"] = prev_fatigue
|
| 310 |
+
else:
|
| 311 |
+
# ์ฒซ ๋ ์ฝ๋๋ ์ฐธ์กฐ ๋ฐ์ดํฐ์ ํผ๋ก๋์์ ์ฝ๊ฐ ๋ฎ๊ฒ ์์
|
| 312 |
+
new_row["fatigue_prev"] = max(0.05, prev_fatigue - random.uniform(0, 0.05))
|
| 313 |
+
|
| 314 |
+
# ํ์ฌ ํผ๋ก๋๋ ์ด์ ํผ๋ก๋ ๊ธฐ๋ฐ์ผ๋ก ์ฝ๊ฐ ์ฆ๊ฐํ๋ ๊ฒฝํฅ (์ค์ ์ธก์ ๊ณผ ์ ์ฌ)
|
| 315 |
+
if "fatigue" in new_row and new_row["fatigue"] is not None:
|
| 316 |
+
# ํผ๋ก๋๋ ์๊ฐ์ ๋ฐ๋ผ ์ ์ง์ ์ผ๋ก ์ฆ๊ฐํ๋ ๊ฒฝํฅ
|
| 317 |
+
fatigue_base = new_row["fatigue_prev"] if "fatigue_prev" in new_row else prev_fatigue
|
| 318 |
+
# ์ฝ๊ฐ์ ์ฆ๊ฐ + ๋
ธ์ด์ฆ
|
| 319 |
+
fatigue_increase = random.uniform(0, 0.02) # ์๊ฐ์ ๋ฐ๋ฅธ ์ ์ง์ ์ฆ๏ฟฝ๏ฟฝ
|
| 320 |
+
new_row["fatigue"] = min(0.95, max(0.05, fatigue_base + fatigue_increase + random.uniform(-0.01, 0.01)))
|
| 321 |
+
new_row["fatigue_level"] = 0 if new_row["fatigue"] < 0.3 else 1 if new_row["fatigue"] < 0.6 else 2
|
| 322 |
+
prev_fatigue = new_row["fatigue"]
|
| 323 |
+
|
| 324 |
+
# ์ธ์
ID ์์ฑ (์ ์ฌ์ฉ์์ด๋ฏ๋ก ์๋ก์ด ์ธ์
)
|
| 325 |
+
if "session_id" in new_row:
|
| 326 |
+
new_row["session_id"] = f"session_{i // 10 + 1:03d}" # 10๊ฐ ๋ ์ฝ๋๋น ์ธ์
|
| 327 |
+
|
| 328 |
+
# measure_date๋ ๊ธฐ์กด ๋ฐ์ดํฐ์ ์๋ ๊ฒฝ์ฐ์๋ง ์ค์
|
| 329 |
+
if "measure_date" in sample:
|
| 330 |
+
try:
|
| 331 |
+
measure_time = datetime.fromisoformat(new_row["timestamp_utc"].replace("Z", "+00:00"))
|
| 332 |
+
new_row["measure_date"] = measure_time.strftime("%Y-%m-%d")
|
| 333 |
+
except:
|
| 334 |
+
new_row["measure_date"] = base_time.strftime("%Y-%m-%d")
|
| 335 |
+
|
| 336 |
+
new_rows.append(new_row)
|
| 337 |
+
|
| 338 |
+
return pd.DataFrame(new_rows)
|
| 339 |
|
| 340 |
+
else:
|
| 341 |
+
# ๊ธฐ์กด ์ฌ์ฉ์ ๋ฐ์ดํฐ ์ฆํญ (์๊ฐ์ ์ฐ์์ฑ ์ ์ง)
|
| 342 |
+
new_rows = []
|
| 343 |
+
last_row = df.iloc[-1].to_dict()
|
| 344 |
+
|
| 345 |
+
# ๋ง์ง๋ง ๋ ์ฝ๋์ ์๊ฐ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
|
| 346 |
+
if "timestamp_utc" in last_row and last_row["timestamp_utc"]:
|
| 347 |
+
try:
|
| 348 |
+
last_time = datetime.fromisoformat(str(last_row["timestamp_utc"]).replace("Z", "+00:00"))
|
| 349 |
+
except:
|
| 350 |
+
last_time = datetime.now(timezone.utc)
|
| 351 |
+
else:
|
| 352 |
+
last_time = datetime.now(timezone.utc)
|
| 353 |
+
|
| 354 |
+
last_window_id = last_row.get("window_id", 0) if last_row.get("window_id") is not None else 0
|
| 355 |
+
last_window_start = last_row.get("window_end_ms", 0) if last_row.get("window_end_ms") is not None else 0
|
| 356 |
+
prev_fatigue = last_row.get("fatigue", 0.1) if last_row.get("fatigue") is not None else 0.1
|
| 357 |
+
|
| 358 |
+
for i in range(need):
|
| 359 |
+
# ์ํ ๋ ์ฝ๋ ์ ํ
|
| 360 |
+
sample_idx = random.randint(0, len(df) - 1)
|
| 361 |
+
sample = df.iloc[sample_idx].to_dict()
|
| 362 |
+
|
| 363 |
+
# ์๋ก์ด ๋ ์ฝ๋ ์์ฑ
|
| 364 |
+
new_row = augment_record_strict(sample)
|
| 365 |
+
|
| 366 |
+
# ์๊ฐ์ ์ฐ์์ฑ ์ ์ง
|
| 367 |
+
window_interval = 2000
|
| 368 |
+
new_row["window_id"] = last_window_id + i + 1
|
| 369 |
+
new_row["window_start_ms"] = last_window_start + i * window_interval
|
| 370 |
+
new_row["window_end_ms"] = new_row["window_start_ms"] + window_interval
|
| 371 |
+
|
| 372 |
+
# timestamp ์ฐ์์ฑ ์ ์ง
|
| 373 |
+
new_row["timestamp_utc"] = (last_time + timedelta(milliseconds=(i + 1) * window_interval)).isoformat()
|
| 374 |
+
|
| 375 |
+
# ํผ๋ก๋ ์ฐ์์ฑ ์ ์ง
|
| 376 |
+
new_row["fatigue_prev"] = prev_fatigue
|
| 377 |
+
if "fatigue" in new_row and new_row["fatigue"] is not None:
|
| 378 |
+
# ํผ๋ก๋๋ ์๊ฐ์ ๋ฐ๋ผ ์ ์ง์ ์ผ๋ก ์ฆ๊ฐํ๋ ๊ฒฝํฅ
|
| 379 |
+
fatigue_increase = random.uniform(0, 0.02) # ์๊ฐ์ ๋ฐ๋ฅธ ์ ์ง์ ์ฆ๊ฐ
|
| 380 |
+
new_row["fatigue"] = min(0.95, max(0.05, prev_fatigue + fatigue_increase + random.uniform(-0.01, 0.01)))
|
| 381 |
+
new_row["fatigue_level"] = 0 if new_row["fatigue"] < 0.3 else 1 if new_row["fatigue"] < 0.6 else 2
|
| 382 |
+
prev_fatigue = new_row["fatigue"]
|
| 383 |
+
|
| 384 |
+
# measure_date๋ ๊ธฐ์กด ๋ฐ์ดํฐ์ ์๋ ๊ฒฝ์ฐ์๋ง ์ค์
|
| 385 |
+
if "measure_date" in sample:
|
| 386 |
+
try:
|
| 387 |
+
measure_time = datetime.fromisoformat(new_row["timestamp_utc"].replace("Z", "+00:00"))
|
| 388 |
+
new_row["measure_date"] = measure_time.strftime("%Y-%m-%d")
|
| 389 |
+
except:
|
| 390 |
+
new_row["measure_date"] = last_time.strftime("%Y-%m-%d")
|
| 391 |
+
|
| 392 |
+
new_rows.append(new_row)
|
| 393 |
+
|
| 394 |
+
return pd.concat([df, pd.DataFrame(new_rows)], ignore_index=True)
|
| 395 |
|
| 396 |
|
| 397 |
def main():
|
|
|
|
| 424 |
else:
|
| 425 |
user_id = filename_no_ext
|
| 426 |
|
| 427 |
+
# local_user๋ก ์์ํ๋ ํ์ผ์ ์ ์ธ
|
| 428 |
+
if user_id.startswith("local_user"):
|
| 429 |
+
print(f"โญ๏ธ {user_id}: local_user๋ก ์์ํ๋ ํ์ผ์ ์ ์ธ")
|
| 430 |
+
continue
|
| 431 |
+
|
| 432 |
# ๊ฐ๋ณ ํ์ผ์ pandas๋ก ์ง์ ๋ก๋
|
| 433 |
from huggingface_hub import hf_hub_download
|
| 434 |
import tempfile
|
|
|
|
| 455 |
print(f"โ ๋ฐ์ดํฐ์
๋ก๋ ์์ ์คํจ: {e3}")
|
| 456 |
return
|
| 457 |
|
| 458 |
+
# ์ ํจํ ์ฌ์ฉ์๋ง ํํฐ๋ง (๋ฐ์ดํฐ๊ฐ ์๋ ์ฌ์ฉ์๋ง, local_user ์ ์ธ)
|
| 459 |
valid_users = {}
|
| 460 |
for user_id in existing.keys():
|
| 461 |
+
# local_user๋ก ์์ํ๋ ์ฌ์ฉ์๋ ์ ์ธ
|
| 462 |
+
if user_id.startswith("local_user"):
|
| 463 |
+
print(f"โญ๏ธ {user_id}: local_user๋ก ์์ํ๋ ์ฌ์ฉ์๋ ์ ์ธ")
|
| 464 |
+
continue
|
| 465 |
try:
|
| 466 |
user_data = existing[user_id]
|
| 467 |
if len(user_data) > 0:
|
|
|
|
| 509 |
continue
|
| 510 |
|
| 511 |
try:
|
| 512 |
+
# ์ฐธ์กฐ ๋ฐ์ดํฐ๋ฅผ ์ฆํญํ์ฌ ์๋ก์ด ์ฌ์ฉ์ ๋ฐ์ดํฐ ์์ฑ (์ ์ฌ์ฉ์ ID ์ ๋ฌ)
|
| 513 |
+
new_user_df = augment_user_data(reference_df, RECORDS_PER_USER, new_user_id=new_user_id)
|
| 514 |
+
# user_id ์ปฌ๋ผ์ด ์์ผ๋ฉด ์ถ๊ฐ
|
| 515 |
+
if "user_id" not in new_user_df.columns:
|
| 516 |
+
new_user_df["user_id"] = new_user_id
|
| 517 |
+
else:
|
| 518 |
+
new_user_df["user_id"] = new_user_id
|
| 519 |
new_user_datasets[new_user_id] = Dataset.from_pandas(new_user_df, preserve_index=False)
|
| 520 |
+
actual_count = len(new_user_df)
|
| 521 |
+
print(f"๐ {new_user_id}: {actual_count} ๋ ์ฝ๋ ์์ฑ (์ฐธ์กฐ: {reference_user_id}, ๋ชฉํ: {RECORDS_PER_USER})")
|
| 522 |
+
if actual_count != RECORDS_PER_USER:
|
| 523 |
+
print(f" โ ๏ธ ๊ฒฝ๊ณ : ์์ฑ๋ ๋ ์ฝ๋ ์({actual_count})๊ฐ ๋ชฉํ({RECORDS_PER_USER})์ ๋ค๋ฆ
๋๋ค!")
|
| 524 |
except Exception as e:
|
| 525 |
print(f"โ {new_user_id}: ์์ฑ ์คํจ ({e}), ๊ฑด๋๋")
|
| 526 |
continue
|
|
|
|
| 529 |
print("โ ์๋ก์ด ์ฌ์ฉ์ ๋ฐ์ดํฐ๊ฐ ์์ฑ๋์ง ์์์ต๋๋ค.")
|
| 530 |
return
|
| 531 |
|
| 532 |
+
# ๊ธฐ์กด ๋ฐ์ดํฐ์ ์คํค๋ง ํ์ธ (์ฒซ ๋ฒ์งธ ์ฌ์ฉ์ ๋ฐ์ดํฐ ๊ธฐ์ค)
|
| 533 |
+
print("๐ง ๊ธฐ์กด ๋ฐ์ดํฐ ์คํค๋ง ํ์ธ ์ค...")
|
| 534 |
+
reference_user_id = list(valid_users.keys())[0]
|
| 535 |
+
reference_df = valid_users[reference_user_id].to_pandas()
|
| 536 |
+
existing_columns = set(reference_df.columns)
|
| 537 |
+
print(f" ๐ ๊ธฐ์กด ๋ฐ์ดํฐ ์ปฌ๋ผ ์: {len(existing_columns)}")
|
| 538 |
+
print(f" ๐ ๊ธฐ์กด ๋ฐ์ดํฐ ์ปฌ๋ผ: {sorted(existing_columns)}")
|
| 539 |
+
|
| 540 |
+
# ์๋ก์ด ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ์กด ์คํค๋ง์ ๋ง์ถค
|
| 541 |
+
print("๐ง ์๋ก์ด ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ์กด ์คํค๋ง์ ๋ง์ถ๋ ์ค...")
|
| 542 |
+
for user_id in new_user_datasets.keys():
|
| 543 |
+
df = new_user_datasets[user_id].to_pandas()
|
| 544 |
+
|
| 545 |
+
# ๊ธฐ์กด์ ์๋ ์ปฌ๋ผ ์ ๊ฑฐ
|
| 546 |
+
columns_to_remove = set(df.columns) - existing_columns
|
| 547 |
+
if columns_to_remove:
|
| 548 |
+
df = df.drop(columns=list(columns_to_remove))
|
| 549 |
+
print(f" โ ๏ธ {user_id}: ๋ถํ์ํ ์ปฌ๋ผ ์ ๊ฑฐ: {columns_to_remove}")
|
| 550 |
+
|
| 551 |
+
# ๊ธฐ์กด์ ์๋๋ฐ ์๋ ์ปฌ๋ผ ์ถ๊ฐ (None์ผ๋ก)
|
| 552 |
+
columns_to_add = existing_columns - set(df.columns)
|
| 553 |
+
if columns_to_add:
|
| 554 |
+
for col in columns_to_add:
|
| 555 |
+
df[col] = None
|
| 556 |
+
print(f" โ {user_id}: ๋๋ฝ๋ ์ปฌ๋ผ ์ถ๊ฐ: {columns_to_add}")
|
| 557 |
+
|
| 558 |
+
# ์ปฌ๋ผ ์์๋ฅผ ๊ธฐ์กด ๋ฐ์ดํฐ์ ๋์ผํ๊ฒ ๋ง์ถค
|
| 559 |
+
df = df[list(reference_df.columns)]
|
| 560 |
+
|
| 561 |
+
new_user_datasets[user_id] = Dataset.from_pandas(df, preserve_index=False)
|
| 562 |
+
print(f" โ
{user_id}: ์คํค๋ง ์ ๊ทํ ์๋ฃ")
|
| 563 |
+
|
| 564 |
# ๊ธฐ์กด ๋ฐ์ดํฐ์
์ ์๋ก์ด ์ฌ์ฉ์ ๋ฐ์ดํฐ ์ถ๊ฐ
|
| 565 |
final_datasets = {}
|
| 566 |
# ๊ธฐ์กด ์ฌ์ฉ์ ๋ฐ์ดํฐ ์ ์ง
|
|
|
|
| 577 |
print(f"๐ ์ ์ฒด ๋ฐ์ดํฐ์
์ด ๋ ์ฝ๋ ์: {total_records}")
|
| 578 |
print(f"๐ ์๋ก์ด parquet ํ์ผ ์: {len(new_user_datasets)}๊ฐ")
|
| 579 |
|
| 580 |
+
# local_user๋ก ์์ํ๋ ํ์ผ ์ญ์
|
| 581 |
+
print("๐๏ธ local_user๋ก ์์ํ๋ ํ์ผ ์ญ์ ์ค...")
|
| 582 |
+
try:
|
| 583 |
+
files_to_delete = []
|
| 584 |
+
for file_path in parquet_files:
|
| 585 |
+
filename = file_path.split("/")[-1] if "/" in file_path else file_path
|
| 586 |
+
filename_no_ext = filename.replace(".parquet", "")
|
| 587 |
+
# -00000-of-00001 ๋ถ๋ถ์ด ์์ผ๋ฉด ์ ๊ฑฐ
|
| 588 |
+
if "-" in filename_no_ext:
|
| 589 |
+
user_id = filename_no_ext.split("-")[0]
|
| 590 |
+
else:
|
| 591 |
+
user_id = filename_no_ext
|
| 592 |
+
|
| 593 |
+
if user_id.startswith("local_user"):
|
| 594 |
+
files_to_delete.append(file_path)
|
| 595 |
+
|
| 596 |
+
for file_path in files_to_delete:
|
| 597 |
+
try:
|
| 598 |
+
api.delete_file(path_in_repo=file_path, repo_id=repo_id, repo_type="dataset", token=token)
|
| 599 |
+
print(f" โ
์ญ์ : {file_path}")
|
| 600 |
+
except Exception as e:
|
| 601 |
+
print(f" โ ๏ธ ์ญ์ ์คํจ ({file_path}): {str(e)[:100]}")
|
| 602 |
+
|
| 603 |
+
if files_to_delete:
|
| 604 |
+
print(f"๐๏ธ {len(files_to_delete)}๊ฐ ํ์ผ ์ญ์ ์๋ฃ")
|
| 605 |
+
else:
|
| 606 |
+
print("โน๏ธ ์ญ์ ํ local_user ํ์ผ์ด ์์ต๋๋ค")
|
| 607 |
+
except Exception as e:
|
| 608 |
+
print(f"โ ๏ธ ํ์ผ ์ญ์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)[:100]}")
|
| 609 |
+
|
| 610 |
print(f"๐ค Hugging Face Hub์ ์
๋ก๋ ์ค: {repo_id}")
|
| 611 |
final_dict.push_to_hub(repo_id, token=token, private=True)
|
| 612 |
print("โ
์
๋ก๋ ์๋ฃ")
|