Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor affine transformations and add deprecation utils #550

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
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
35 changes: 35 additions & 0 deletions kwave/utils/deprecation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Utilities for handling deprecation warnings in k-Wave."""

import functools
import warnings
from typing import Callable, TypeVar, Any

F = TypeVar("F", bound=Callable[..., Any])


def deprecated(message: str, target_version: str = "2.0.0") -> Callable[[F], F]:
"""Decorator to mark functions as deprecated.

Args:
message: Message explaining what to use instead
target_version: Version in which the function will be removed

Example:
@deprecated("Use new_function() instead", "2.0.0")
def old_function():
pass
"""

def decorator(func: F) -> F:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Usually these type of calls happen once per application lifecycle. If we display warning every time the function is called, it could be annoying. Perhaps move the warning outside of the wrapper but keep it still inside of the decorator? Since each function gets decorated only once (not sure how this will if the file is imported multiple times), a single log should be printed.

@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
warnings.warn(
f"{func.__name__} is deprecated and will be removed in version {target_version}. {message}",
DeprecationWarning,
stacklevel=2,
)
return func(*args, **kwargs)

return wrapper

return decorator
11 changes: 8 additions & 3 deletions kwave/utils/kwave_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from kwave.utils.conversion import tol_star
from kwave.utils.interp import get_delta_bli
from kwave.utils.mapgen import trim_cart_points, make_cart_rect, make_cart_arc, make_cart_bowl, make_cart_disc, make_cart_spherical_segment
from kwave.utils.math import sinc, get_affine_matrix
from kwave.utils.math import sinc, make_affine
from kwave.utils.matlab import matlab_assign, matlab_mask, matlab_find


Expand Down Expand Up @@ -680,8 +680,13 @@ def combine_sensor_data(self, kgrid, sensor_data):
return combined_sensor_data

def set_array_position(self, translation, rotation):
# get affine matrix and store
self.array_transformation = get_affine_matrix(translation, rotation)
"""Set the array position using translation and rotation.

Args:
translation: Vector of translation values [dx, dy] or [dx, dy, dz]
rotation: Single angle (in degrees) for 2D or [x_angle, y_angle, z_angle] for 3D
"""
self.array_transformation = make_affine(translation, rotation)

def set_affine_transform(self, affine_transform):
# check array has elements
Expand Down
8 changes: 5 additions & 3 deletions kwave/utils/mapgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
from beartype import beartype as typechecker
from beartype.typing import Union, List, Tuple, cast, Optional
from jaxtyping import Float, Complex, Int, Real, Integer
from scipy.spatial.transform import Rotation

from .conversion import db2neper, neper2db
from .data import scale_SI
from .math import cosd, sind, Rz, Ry, Rx, compute_linear_transform
from .math import cosd, sind
from .matlab import matlab_assign, matlab_find, ind2sub, sub2ind
from .matrix import max_nd
from .tictoc import TicToc
from ..data import Vector

import kwave.utils.typing as kt
from kwave.utils.math import compute_linear_transform, compute_rotation_between_vectors

# GLOBALS
# define literals (ref: http://www.wolframalpha.com/input/?i=golden+angle)
Expand Down Expand Up @@ -2625,8 +2627,8 @@ def make_cart_rect(
# No rotation
R = np.eye(3)
else:
# Using intrinsic rotations chain from right to left (z-y'-z'' rotations)
R = np.dot(Rz(theta[2]), np.dot(Ry(theta[1]), Rx(theta[0])))
# Using intrinsic rotations chain from right to left (xyz rotations)
R = Rotation.from_euler("xyz", theta, degrees=True).as_matrix()

# Combine scaling and rotation matrices
A = np.dot(R, S)
Expand Down
Loading
Loading