Upload ProDiff/Experiments/trajectory_a40_temporal_optimized_TKY_temporal_len3_ddpm_20250724-101534/code_snapshot/metric.py with huggingface_hub
Browse files
ProDiff/Experiments/trajectory_a40_temporal_optimized_TKY_temporal_len3_ddpm_20250724-101534/code_snapshot/metric.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from .utils import haversine
|
| 3 |
+
|
| 4 |
+
def mean_absolute_error_per_point(pred, true):
|
| 5 |
+
"""
|
| 6 |
+
Calculates the Mean Absolute Error Per Point (MAEPP) for a batch.
|
| 7 |
+
:param pred: Predicted time, shape (batch_size, traj_length)
|
| 8 |
+
:param true: Ground truth time, shape (batch_size, traj_length)
|
| 9 |
+
:return: Mean Absolute Error Per Point (MAEPP) for the batch.
|
| 10 |
+
"""
|
| 11 |
+
maepp = np.abs(pred - true).mean()
|
| 12 |
+
return maepp
|
| 13 |
+
|
| 14 |
+
def mean_absolute_error_per_sample(pred, true):
|
| 15 |
+
"""
|
| 16 |
+
Calculates the Mean Absolute Error Per Sample (MAEPS) for a batch.
|
| 17 |
+
:param pred: Predicted time, shape (batch_size, traj_length)
|
| 18 |
+
:param true: Ground truth time, shape (batch_size, traj_length)
|
| 19 |
+
:return: Mean Absolute Error Per Sample (MAEPS) for the batch.
|
| 20 |
+
"""
|
| 21 |
+
mae_per_sample = np.abs(pred - true).mean(axis=1)
|
| 22 |
+
maeps = mae_per_sample.mean()
|
| 23 |
+
return maeps
|
| 24 |
+
|
| 25 |
+
def mean_trajectory_deviation(pred, true):
|
| 26 |
+
"""
|
| 27 |
+
Calculates the Mean Trajectory Deviation (MTD) for a batch.
|
| 28 |
+
:param pred: Predicted trajectories, shape (batch_size, 2, traj_length) or (batch_size, traj_length, 2/3)
|
| 29 |
+
:param true: Ground truth trajectories, shape (batch_size, 2, traj_length) or (batch_size, traj_length, 2/3)
|
| 30 |
+
:return: Mean Trajectory Deviation (MTD) for the batch.
|
| 31 |
+
"""
|
| 32 |
+
batch_size, traj_length, _ = pred.shape # Assuming pred shape is (batch_size, traj_length, num_features)
|
| 33 |
+
deviations = []
|
| 34 |
+
for i in range(batch_size):
|
| 35 |
+
# Assuming lat is at index 1 and lon is at index 2 if num_features is 3,
|
| 36 |
+
# or lat is index 0 and lon is index 1 if num_features is 2 (after potential permute)
|
| 37 |
+
# The original code used pred[i, :, 1] and pred[i, :, 2] which might imply features are [time, lat, lon]
|
| 38 |
+
# and slicing was done after permuting to (batch_size, num_coords, traj_length).
|
| 39 |
+
# For (batch_size, traj_length, num_features), access directly.
|
| 40 |
+
pred_lat, pred_lon = pred[i, :, 1], pred[i, :, 2] # Adapt if lat/lon indices are different
|
| 41 |
+
true_lat, true_lon = true[i, :, 1], true[i, :, 2] # Adapt if lat/lon indices are different
|
| 42 |
+
deviation = np.array([haversine(pred_lat[j], pred_lon[j], true_lat[j], true_lon[j]) for j in range(traj_length)])
|
| 43 |
+
deviations.append(np.mean(deviation))
|
| 44 |
+
mtd = np.mean(deviations)
|
| 45 |
+
return mtd
|
| 46 |
+
|
| 47 |
+
def mean_point_to_point_error(pred, true):
|
| 48 |
+
"""
|
| 49 |
+
Calculates the Mean Point-to-Point Error (MPPE) for a batch.
|
| 50 |
+
:param pred: Predicted trajectories, shape (batch_size, traj_length, 2) or (batch_size, traj_length, 3)
|
| 51 |
+
:param true: Ground truth trajectories, shape (batch_size, traj_length, 2) or (batch_size, traj_length, 3)
|
| 52 |
+
:return: Mean Point-to-Point Error (MPPE) for the batch.
|
| 53 |
+
"""
|
| 54 |
+
batch_size, traj_length, _ = pred.shape
|
| 55 |
+
total_error = 0
|
| 56 |
+
for i in range(batch_size):
|
| 57 |
+
for j in range(traj_length):
|
| 58 |
+
pred_lat, pred_lon = pred[i, j, 1], pred[i, j, 2] # Adapt if lat/lon indices are different
|
| 59 |
+
true_lat, true_lon = true[i, j, 1], true[i, j, 2] # Adapt if lat/lon indices are different
|
| 60 |
+
point_error = haversine(pred_lat, pred_lon, true_lat, true_lon)
|
| 61 |
+
total_error += point_error
|
| 62 |
+
mppe = total_error / (batch_size * traj_length)
|
| 63 |
+
return mppe
|
| 64 |
+
|
| 65 |
+
def trajectory_coverage(pred, true, thresholds):
|
| 66 |
+
"""
|
| 67 |
+
Calculates Trajectory Coverage (TC) for each sample at multiple thresholds.
|
| 68 |
+
:param pred: Predicted trajectories, shape (batch_size, 2, traj_length) or (batch_size, traj_length, 2/3)
|
| 69 |
+
:param true: Ground truth trajectories, shape (batch_size, 2, traj_length) or (batch_size, traj_length, 2/3)
|
| 70 |
+
:param thresholds: List of deviation thresholds.
|
| 71 |
+
:return: A dictionary of trajectory coverage for each sample at various thresholds,
|
| 72 |
+
and the average trajectory coverage (APTC).
|
| 73 |
+
"""
|
| 74 |
+
batch_size, traj_length, _ = pred.shape
|
| 75 |
+
tc_dict = {f'TC@{threshold}': [] for threshold in thresholds}
|
| 76 |
+
for i in range(batch_size):
|
| 77 |
+
pred_lat, pred_lon = pred[i, :, 1], pred[i, :, 2] # Adapt if lat/lon indices are different
|
| 78 |
+
true_lat, true_lon = true[i, :, 1], true[i, :, 2] # Adapt if lat/lon indices are different
|
| 79 |
+
deviations = np.array([haversine(pred_lat[j], pred_lon[j], true_lat[j], true_lon[j]) for j in range(traj_length)])
|
| 80 |
+
for threshold in thresholds:
|
| 81 |
+
tc = (deviations <= threshold).mean() # Original comment: tc = deviations.mean() <= threshold, this seems more standard.
|
| 82 |
+
tc_dict[f'TC@{threshold}'].append(tc)
|
| 83 |
+
aptc = {k: np.mean(v) for k, v in tc_dict.items()}
|
| 84 |
+
avg_aptc = np.mean(list(aptc.values()))
|
| 85 |
+
return aptc, avg_aptc
|
| 86 |
+
|
| 87 |
+
def max_trajectory_deviation(pred, true):
|
| 88 |
+
"""
|
| 89 |
+
Calculates the Maximum Trajectory Deviation (MaxTD) for each sample in a batch.
|
| 90 |
+
:param pred: Predicted trajectories, shape (batch_size, 2, traj_length) or (batch_size, traj_length, 2/3)
|
| 91 |
+
:param true: Ground truth trajectories, shape (batch_size, 2, traj_length) or (batch_size, traj_length, 2/3)
|
| 92 |
+
:return: Maximum Trajectory Deviation (MaxTD) for the batch.
|
| 93 |
+
"""
|
| 94 |
+
batch_size, traj_length, _ = pred.shape
|
| 95 |
+
max_deviations = []
|
| 96 |
+
for i in range(batch_size):
|
| 97 |
+
pred_lat, pred_lon = pred[i, :, 1], pred[i, :, 2] # Adapt if lat/lon indices are different
|
| 98 |
+
true_lat, true_lon = true[i, :, 1], true[i, :, 2] # Adapt if lat/lon indices are different
|
| 99 |
+
deviation = np.array([haversine(pred_lat[j], pred_lon[j], true_lat[j], true_lon[j]) for j in range(traj_length)])
|
| 100 |
+
max_deviations.append(np.max(deviation))
|
| 101 |
+
max_td = np.max(max_deviations)
|
| 102 |
+
return max_td
|