Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions spatialmath/spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,18 @@ def stochastic_downsample_interpolation(

return self.spline, interpolation_indices

def _check_fitted(self) -> None:
if self.spline is None:
raise RuntimeError(
"no spline has been fitted yet, call a fitting method such as "
"stochastic_downsample_interpolation() first"
)

def max_angular_error(self) -> float:
return np.max(self.angular_errors())

def angular_errors(self) -> List[float]:
self._check_fitted()
return [
pose.angdist(self.spline(t))
for pose, t in zip(self.pose_data, self.time_data)
Expand All @@ -240,6 +248,7 @@ def max_euclidean_error(self) -> float:
return np.max(self.euclidean_errors())

def euclidean_errors(self) -> List[float]:
self._check_fitted()
return [
np.linalg.norm(pose.t - self.spline(t).t)
for pose, t in zip(self.pose_data, self.time_data)
Expand Down
31 changes: 30 additions & 1 deletion tests/test_spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ def test_spline_fit(self):
assert len(spline.timepoints) == len(kept_indices)

assert fit.max_angular_error() < np.deg2rad(5.0)
assert fit.max_angular_error() < 0.1
# check_type="local" only bounds the error at each removed sample when
# it is removed, so the final Euclidean error is not bounded by
# epsilon_xyz; see test_spline_fit_global for the bounded case
# animate=True busy-waits on a timer callback that never fires
# under the non-interactive Agg backend
if plt.get_backend().lower() != "agg" and os.environ.get("CI") != "true":
Expand All @@ -126,3 +128,30 @@ def test_spline_fit(self):
animate=True,
repeat=False,
)

def test_spline_fit_global(self):
# global checks are O(n^2), so use a coarser sampling of the same helix
timestamps = self.timestamps[::5]
trajectory = self.trajectory[::5]
epsilon_xyz, epsilon_angle = 1e-3, 1e-2
np.random.seed(0)
fit = SplineFit(timestamps, trajectory)
spline, kept_indices = fit.stochastic_downsample_interpolation(
epsilon_xyz=epsilon_xyz,
epsilon_angle=epsilon_angle,
check_type="global",
normalize_time=False,
)

assert len(kept_indices) < len(timestamps)
assert fit.max_euclidean_error() <= epsilon_xyz
assert fit.max_angular_error() <= epsilon_angle

@pytest.mark.parametrize(
"method",
["angular_errors", "max_angular_error", "euclidean_errors", "max_euclidean_error"],
)
def test_errors_before_fit(self, method):
fit = SplineFit(self.timestamps, self.trajectory)
with pytest.raises(RuntimeError, match="no spline has been fitted"):
getattr(fit, method)()