Skip to content

Make cart2pol and pol2cart refuse 3D data, and test kinematics in 3D - #1147

Open
Mudassiruddin7 wants to merge 1 commit into
neuroinformatics-unit:mainfrom
Mudassiruddin7:fix/cart2pol-2d-only
Open

Mudassiruddin7 wants to merge 1 commit into
neuroinformatics-unit:mainfrom
Mudassiruddin7:fix/cart2pol-2d-only

Conversation

@Mudassiruddin7

@Mudassiruddin7 Mudassiruddin7 commented Oct 8, 2026 •

Copy link
Copy Markdown

Description

cart2pol accepted 3D data and returned wrong polar coordinates, and the 3D behaviour of the rest of the kinematics functions was untested. This addresses #363.

The bug

cart2pol called validate_dims_coords(data, {"space": ["x", "y"]}), which only checks that x and y are present. With an extra z coordinate it went on: rho = compute_norm(data) is then the full 3D norm, while phi = arctan2(y, x) is the angle of the x-y projection. For example, for a helix (cos t, sin t, 0.5 t) it returns rho = sqrt(1 + 0.25 t^2) instead of 1, and pol2cart does not give the input back.

It now requires exactly x and y, the same as compute_signed_angle_2d does with exact_coords=True, and pol2cart requires exactly rho and phi. Both docstrings say so. The error message is the existing one from validate_dims_coords.

3D tests

For #363 I ran each function on a straight line (1, 2, 2) and a helix in x-y-z and added test_kinematics_3d.py:

  • velocity, speed, acceleration, path length, path straightness, forward/backward displacement, compute_norm and convert_to_unit give the closed-form result in 3D (they needed no change);
  • compute_turning_angle, compute_directional_change, compute_path_sinuosity, compute_path_emax, compute_forward_vector and compute_signed_angle_2d raise a ValueError for 3D data (also unchanged).

test_vector.py gets a 3D case for cart2pol and an extra-coordinate case for pol2cart. Those two cases fail on main and pass now; the other new tests pass on both, and are there to keep the 3D behaviour from changing.

What is not done

#363 also suggests a 3D variant of valid_poses_dataset_uniform_linear_motion and parametrising the existing tests with it. I wrote separate 3D tests instead, because the 2D-only functions would need to be excluded in every parametrised test. Happy to switch if you prefer the fixture.

How has this been tested?

The full unit suite passes (1506 passed, 2 xfailed), and the pre-commit hooks (ruff, ruff format, mypy, codespell, check-manifest) pass on the changed files.

`cart2pol` only checked that `space` contained `x` and `y`. For 3D data it
passed the check, and `rho` was then the full 3D norm while `phi` was the 2D
angle of the x-y projection, so the polar coordinates did not describe the
point and did not round-trip through `pol2cart`. Require that `space` is
exactly `x` and `y` (and `space_pol` exactly `rho` and `phi` in `pol2cart`),
like `compute_signed_angle_2d` already does.

Add tests for the behaviour on x-y-z data that neuroinformatics-unit#363 asked to be checked:
velocity, speed, acceleration, path length, straightness, displacements, norm
and unit vectors give the right answer, and the 2D-only metrics raise a
ValueError.
Copilot AI balanced review requested due to automatic review settings October 8, 2026 17:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@sonarqubecloud

sonarqubecloud Bot commented Oct 8, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants