From 03712b70a4c2eddf99b4bf0e0abf54abe5c4d01d Mon Sep 17 00:00:00 2001 From: BorisIvanovic Date: Sat, 28 Aug 2021 18:08:32 -0700 Subject: [PATCH] Fixed the gradient calculations, going to make another big update to the codebase soon but wanted to get this out first. Fixes #40 and #26. --- trajectron/environment/data_utils.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/trajectron/environment/data_utils.py b/trajectron/environment/data_utils.py index 675f198..72c7ec8 100644 --- a/trajectron/environment/data_utils.py +++ b/trajectron/environment/data_utils.py @@ -20,10 +20,14 @@ def derivative_of(x, dt=1, radian=False): if radian: x = make_continuous_copy(x) - if x[~np.isnan(x)].shape[-1] < 2: + not_nan_mask = ~np.isnan(x) + masked_x = x[not_nan_mask] + + if masked_x.shape[-1] < 2: return np.zeros_like(x) dx = np.full_like(x, np.nan) - dx[~np.isnan(x)] = np.gradient(x[~np.isnan(x)], dt) + dx[not_nan_mask] = np.ediff1d(masked_x, to_begin=(masked_x[1] - masked_x[0])) / dt + + return dx - return dx \ No newline at end of file