Delete preprocessing.py
Browse files- preprocessing.py +0 -108
preprocessing.py
DELETED
|
@@ -1,108 +0,0 @@
|
|
| 1 |
-
"""Standalone preprocessing utilities for swipe path data.
|
| 2 |
-
|
| 3 |
-
This module provides preprocessing functions for the SwipeALot model that are
|
| 4 |
-
completely standalone and don't require the full swipealot training package.
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import numpy as np
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def normalize_coordinates(
|
| 11 |
-
data_points: list[dict], canvas_width: float, canvas_height: float
|
| 12 |
-
) -> list[dict]:
|
| 13 |
-
"""
|
| 14 |
-
Normalize swipe coordinates and timestamps.
|
| 15 |
-
|
| 16 |
-
Args:
|
| 17 |
-
data_points: List of dicts with 'x', 'y', 't' keys
|
| 18 |
-
canvas_width: Canvas width (not used - kept for compatibility)
|
| 19 |
-
canvas_height: Canvas height (not used - kept for compatibility)
|
| 20 |
-
|
| 21 |
-
Returns:
|
| 22 |
-
List of normalized coordinate dicts with x, y in [0,1] and t in [0,1]
|
| 23 |
-
|
| 24 |
-
Note:
|
| 25 |
-
For futo-org/swipe.futo.org dataset, x and y are already normalized to [0,1].
|
| 26 |
-
This function clamps them to ensure they stay in bounds and normalizes timestamps.
|
| 27 |
-
"""
|
| 28 |
-
if not data_points:
|
| 29 |
-
return []
|
| 30 |
-
|
| 31 |
-
# Extract timestamps for normalization
|
| 32 |
-
timestamps = [p["t"] for p in data_points]
|
| 33 |
-
t_min = min(timestamps)
|
| 34 |
-
t_max = max(timestamps)
|
| 35 |
-
t_range = t_max - t_min if t_max > t_min else 1.0
|
| 36 |
-
|
| 37 |
-
normalized = []
|
| 38 |
-
for point in data_points:
|
| 39 |
-
# x and y are already normalized to [0,1] in the dataset
|
| 40 |
-
# But sometimes they go slightly outside bounds, so clamp them
|
| 41 |
-
x_norm = max(0.0, min(1.0, point["x"]))
|
| 42 |
-
y_norm = max(0.0, min(1.0, point["y"]))
|
| 43 |
-
|
| 44 |
-
# Normalize timestamp to [0, 1]
|
| 45 |
-
t_norm = (point["t"] - t_min) / t_range
|
| 46 |
-
|
| 47 |
-
normalized.append({"x": x_norm, "y": y_norm, "t": t_norm})
|
| 48 |
-
|
| 49 |
-
return normalized
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
def sample_path_points(data_points: list[dict], max_len: int) -> tuple:
|
| 53 |
-
"""
|
| 54 |
-
Sample or pad path points to fixed length using linear interpolation.
|
| 55 |
-
|
| 56 |
-
Args:
|
| 57 |
-
data_points: List of coordinate dicts with 'x', 'y', 't' keys
|
| 58 |
-
max_len: Target length (typically 128 for SwipeALot models)
|
| 59 |
-
|
| 60 |
-
Returns:
|
| 61 |
-
Tuple of (sampled_points, mask) where:
|
| 62 |
-
- sampled_points: numpy array of shape [max_len, 3] with (x, y, t) coordinates
|
| 63 |
-
- mask: numpy array of shape [max_len] indicating valid (1) vs padding (0) points
|
| 64 |
-
|
| 65 |
-
Note:
|
| 66 |
-
- If path has fewer points than max_len, it's zero-padded
|
| 67 |
-
- If path has more points than max_len, it's downsampled using linear interpolation
|
| 68 |
-
- If path has exactly max_len points, it's returned as-is
|
| 69 |
-
"""
|
| 70 |
-
num_points = len(data_points)
|
| 71 |
-
|
| 72 |
-
if num_points == max_len:
|
| 73 |
-
points = data_points
|
| 74 |
-
mask = [1] * max_len
|
| 75 |
-
elif num_points < max_len:
|
| 76 |
-
# Pad with zeros
|
| 77 |
-
points = data_points + [{"x": 0.0, "y": 0.0, "t": 0.0}] * (max_len - num_points)
|
| 78 |
-
mask = [1] * num_points + [0] * (max_len - num_points)
|
| 79 |
-
else:
|
| 80 |
-
# Downsample using linear interpolation
|
| 81 |
-
# Extract coordinates as arrays
|
| 82 |
-
x_coords = np.array([p["x"] for p in data_points])
|
| 83 |
-
y_coords = np.array([p["y"] for p in data_points])
|
| 84 |
-
t_coords = np.array([p["t"] for p in data_points])
|
| 85 |
-
|
| 86 |
-
# Original indices (parameter for interpolation)
|
| 87 |
-
original_indices = np.arange(num_points)
|
| 88 |
-
|
| 89 |
-
# Target indices for interpolation (evenly spaced)
|
| 90 |
-
target_indices = np.linspace(0, num_points - 1, max_len)
|
| 91 |
-
|
| 92 |
-
# Interpolate each coordinate independently
|
| 93 |
-
x_interp = np.interp(target_indices, original_indices, x_coords)
|
| 94 |
-
y_interp = np.interp(target_indices, original_indices, y_coords)
|
| 95 |
-
t_interp = np.interp(target_indices, original_indices, t_coords)
|
| 96 |
-
|
| 97 |
-
# Reconstruct points
|
| 98 |
-
points = [
|
| 99 |
-
{"x": float(x), "y": float(y), "t": float(t)}
|
| 100 |
-
for x, y, t in zip(x_interp, y_interp, t_interp, strict=True)
|
| 101 |
-
]
|
| 102 |
-
mask = [1] * max_len
|
| 103 |
-
|
| 104 |
-
# Convert to numpy arrays
|
| 105 |
-
coords = np.array([[p["x"], p["y"], p["t"]] for p in points], dtype=np.float32)
|
| 106 |
-
mask = np.array(mask, dtype=np.int64)
|
| 107 |
-
|
| 108 |
-
return coords, mask
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|