Upload folder using huggingface_hub
Browse files- processing_swipe.py +102 -0
processing_swipe.py
CHANGED
|
@@ -261,3 +261,105 @@ class SwipeProcessor(ProcessorMixin):
|
|
| 261 |
Decoded string
|
| 262 |
"""
|
| 263 |
return self.tokenizer.decode(token_ids, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
Decoded string
|
| 262 |
"""
|
| 263 |
return self.tokenizer.decode(token_ids, **kwargs)
|
| 264 |
+
|
| 265 |
+
def normalize_coordinates(
|
| 266 |
+
self, data_points: list[dict], canvas_width: float = None, canvas_height: float = None
|
| 267 |
+
) -> list[dict]:
|
| 268 |
+
"""
|
| 269 |
+
Normalize swipe coordinates and timestamps.
|
| 270 |
+
|
| 271 |
+
Args:
|
| 272 |
+
data_points: List of dicts with 'x', 'y', 't' keys
|
| 273 |
+
canvas_width: Canvas width (not used - kept for compatibility)
|
| 274 |
+
canvas_height: Canvas height (not used - kept for compatibility)
|
| 275 |
+
|
| 276 |
+
Returns:
|
| 277 |
+
List of normalized coordinate dicts with x, y in [0,1] and t in [0,1]
|
| 278 |
+
|
| 279 |
+
Note:
|
| 280 |
+
For futo-org/swipe.futo.org dataset, x and y are already normalized to [0,1].
|
| 281 |
+
This function clamps them to ensure they stay in bounds and normalizes timestamps.
|
| 282 |
+
"""
|
| 283 |
+
if not data_points:
|
| 284 |
+
return []
|
| 285 |
+
|
| 286 |
+
# Extract timestamps for normalization
|
| 287 |
+
timestamps = [p["t"] for p in data_points]
|
| 288 |
+
t_min = min(timestamps)
|
| 289 |
+
t_max = max(timestamps)
|
| 290 |
+
t_range = t_max - t_min if t_max > t_min else 1.0
|
| 291 |
+
|
| 292 |
+
normalized = []
|
| 293 |
+
for point in data_points:
|
| 294 |
+
# x and y are already normalized to [0,1] in the dataset
|
| 295 |
+
# But sometimes they go slightly outside bounds, so clamp them
|
| 296 |
+
x_norm = max(0.0, min(1.0, point["x"]))
|
| 297 |
+
y_norm = max(0.0, min(1.0, point["y"]))
|
| 298 |
+
|
| 299 |
+
# Normalize timestamp to [0, 1]
|
| 300 |
+
t_norm = (point["t"] - t_min) / t_range
|
| 301 |
+
|
| 302 |
+
normalized.append({"x": x_norm, "y": y_norm, "t": t_norm})
|
| 303 |
+
|
| 304 |
+
return normalized
|
| 305 |
+
|
| 306 |
+
def sample_path_points(self, data_points: list[dict], max_len: int = None) -> tuple:
|
| 307 |
+
"""
|
| 308 |
+
Sample or pad path points to fixed length using linear interpolation.
|
| 309 |
+
|
| 310 |
+
Args:
|
| 311 |
+
data_points: List of coordinate dicts with 'x', 'y', 't' keys
|
| 312 |
+
max_len: Target length (defaults to self.max_path_len if not specified)
|
| 313 |
+
|
| 314 |
+
Returns:
|
| 315 |
+
Tuple of (sampled_points, mask) where:
|
| 316 |
+
- sampled_points: numpy array of shape [max_len, 3] with (x, y, t) coordinates
|
| 317 |
+
- mask: numpy array of shape [max_len] indicating valid (1) vs padding (0) points
|
| 318 |
+
|
| 319 |
+
Note:
|
| 320 |
+
- If path has fewer points than max_len, it's zero-padded
|
| 321 |
+
- If path has more points than max_len, it's downsampled using linear interpolation
|
| 322 |
+
- If path has exactly max_len points, it's returned as-is
|
| 323 |
+
"""
|
| 324 |
+
if max_len is None:
|
| 325 |
+
max_len = self.max_path_len
|
| 326 |
+
|
| 327 |
+
num_points = len(data_points)
|
| 328 |
+
|
| 329 |
+
if num_points == max_len:
|
| 330 |
+
points = data_points
|
| 331 |
+
mask = [1] * max_len
|
| 332 |
+
elif num_points < max_len:
|
| 333 |
+
# Pad with zeros
|
| 334 |
+
points = data_points + [{"x": 0.0, "y": 0.0, "t": 0.0}] * (max_len - num_points)
|
| 335 |
+
mask = [1] * num_points + [0] * (max_len - num_points)
|
| 336 |
+
else:
|
| 337 |
+
# Downsample using linear interpolation
|
| 338 |
+
# Extract coordinates as arrays
|
| 339 |
+
x_coords = np.array([p["x"] for p in data_points])
|
| 340 |
+
y_coords = np.array([p["y"] for p in data_points])
|
| 341 |
+
t_coords = np.array([p["t"] for p in data_points])
|
| 342 |
+
|
| 343 |
+
# Original indices (parameter for interpolation)
|
| 344 |
+
original_indices = np.arange(num_points)
|
| 345 |
+
|
| 346 |
+
# Target indices for interpolation (evenly spaced)
|
| 347 |
+
target_indices = np.linspace(0, num_points - 1, max_len)
|
| 348 |
+
|
| 349 |
+
# Interpolate each coordinate independently
|
| 350 |
+
x_interp = np.interp(target_indices, original_indices, x_coords)
|
| 351 |
+
y_interp = np.interp(target_indices, original_indices, y_coords)
|
| 352 |
+
t_interp = np.interp(target_indices, original_indices, t_coords)
|
| 353 |
+
|
| 354 |
+
# Reconstruct points
|
| 355 |
+
points = [
|
| 356 |
+
{"x": float(x), "y": float(y), "t": float(t)}
|
| 357 |
+
for x, y, t in zip(x_interp, y_interp, t_interp, strict=True)
|
| 358 |
+
]
|
| 359 |
+
mask = [1] * max_len
|
| 360 |
+
|
| 361 |
+
# Convert to numpy arrays
|
| 362 |
+
coords = np.array([[p["x"], p["y"], p["t"]] for p in points], dtype=np.float32)
|
| 363 |
+
mask = np.array(mask, dtype=np.int64)
|
| 364 |
+
|
| 365 |
+
return coords, mask
|