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
17 changes: 10 additions & 7 deletions movement/utils/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def convert_to_unit(data: xr.DataArray) -> xr.DataArray:
"""
if "space" in data.dims:
validate_dims_coords(data, {"space": ["x", "y"]})
return data / compute_norm(data)
norm = compute_norm(data)
unit = data / norm
unit = xr.where(norm == 0, np.nan, unit)
return unit
elif "space_pol" in data.dims:
validate_dims_coords(data, {"space_pol": ["rho", "phi"]})
# Set both rho and phi values to NaN at null vectors (where rho = 0)
Expand Down Expand Up @@ -144,8 +147,7 @@ def cart2pol(data: xr.DataArray) -> xr.DataArray:
# Make all zeros in phi positive zeros
# - where rho == 0, set phi to 0
# - where rho != 0, keep the phi value from atan2
phi = xr.where(np.isclose(rho.values, 0.0, atol=1e-9), 0.0, phi)

phi = xr.where(np.isclose(rho, 0.0, atol=1e-9), 0.0, phi)
# Replace space dim with space_pol
dims = list(data.dims)
dims[dims.index("space")] = "space_pol"
Expand Down Expand Up @@ -298,11 +300,12 @@ def compute_signed_angle_2d(
cross *= -1.0
dot = u_x * v_x + u_y * v_y

angles = np.arctan2(cross, dot)
assert isinstance(angles, xr.DataArray)
angles = xr.apply_ufunc(np.arctan2, cross, dot)

# arctan2 returns values in [-pi, pi].
# We need to map -pi angles to pi, to stay in the (-pi, pi] range
angles.values[angles <= -np.pi] = np.pi
# Map -pi to pi to stay in the (-pi, pi] range
angles = xr.where(angles <= -np.pi, np.pi, angles)

angles.name = "signed_angle"
return angles

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies = [
"numpy>=2.0.0",
"pandas",
"h5py",
"netCDF4<1.7.3",
"netCDF4>=1.7.2,<1.7.3",
"tables>=3.10.1",
"attrs",
"pooch",
Expand Down