From 2e138d12c6531ecbe5eee46b7409aeb5d241bd30 Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 12:05:15 -0800 Subject: [PATCH 01/11] update linalg import --- kwave/utils/kwave_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kwave/utils/kwave_array.py b/kwave/utils/kwave_array.py index 9996e055..3d666dbb 100644 --- a/kwave/utils/kwave_array.py +++ b/kwave/utils/kwave_array.py @@ -6,7 +6,7 @@ import numpy as np from numpy import arcsin, pi, cos, size, array -from numpy.linalg import linalg +import numpy.linalg as linalg from kwave.data import Vector from kwave.kgrid import kWaveGrid From 4437fe2012c7fb8f03b58bb9f7e01f26a9d336db Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 12:18:59 -0800 Subject: [PATCH 02/11] explicitly cast to intp --- kwave/utils/matlab.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kwave/utils/matlab.py b/kwave/utils/matlab.py index a589380c..1c46cd15 100644 --- a/kwave/utils/matlab.py +++ b/kwave/utils/matlab.py @@ -1,5 +1,5 @@ from typing import Tuple, Union, Optional, List - +from beartype import beartype as typechecker import numpy as np @@ -52,6 +52,7 @@ def matlab_assign(matrix: np.ndarray, indices: Union[int, np.ndarray], values: U return matrix.reshape(original_shape, order="F") +@typechecker def matlab_find(arr: Union[List[int], np.ndarray], val: int = 0, mode: str = "neq") -> np.ndarray: """ Finds the indices of elements in an array that satisfy a given condition. @@ -75,6 +76,7 @@ def matlab_find(arr: Union[List[int], np.ndarray], val: int = 0, mode: str = "ne return np.expand_dims(arr, -1) # compatibility, n => [n, 1] +@typechecker def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) -> np.ndarray: """ Applies a mask to an array and returns the masked elements. @@ -89,6 +91,8 @@ def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) - """ + # mask is np.intp type + mask = mask.astype(np.intp) if diff is None: return np.expand_dims(arr.ravel(order="F")[mask.ravel(order="F")], axis=-1) # compatibility, n => [n, 1] else: From a073420d105983658e0b09581f0105f4e5412bd6 Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 12:29:55 -0800 Subject: [PATCH 03/11] remove @typechecker --- kwave/utils/matlab.py | 1 - 1 file changed, 1 deletion(-) diff --git a/kwave/utils/matlab.py b/kwave/utils/matlab.py index 1c46cd15..818ce7f9 100644 --- a/kwave/utils/matlab.py +++ b/kwave/utils/matlab.py @@ -52,7 +52,6 @@ def matlab_assign(matrix: np.ndarray, indices: Union[int, np.ndarray], values: U return matrix.reshape(original_shape, order="F") -@typechecker def matlab_find(arr: Union[List[int], np.ndarray], val: int = 0, mode: str = "neq") -> np.ndarray: """ Finds the indices of elements in an array that satisfy a given condition. From d54606e2318bc6c66292fb5a2c4250c28a228234 Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 13:53:56 -0800 Subject: [PATCH 04/11] cast to int16 to replicate legacy behavior --- kwave/utils/matlab.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kwave/utils/matlab.py b/kwave/utils/matlab.py index 818ce7f9..bfefce97 100644 --- a/kwave/utils/matlab.py +++ b/kwave/utils/matlab.py @@ -90,8 +90,9 @@ def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) - """ - # mask is np.intp type - mask = mask.astype(np.intp) + if mask.dtype == "uint8": + mask = mask.astype(np.int16) + if diff is None: return np.expand_dims(arr.ravel(order="F")[mask.ravel(order="F")], axis=-1) # compatibility, n => [n, 1] else: From 950c04b04ffdd07f4f3fae60dfa530c9e60c7309 Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 14:08:31 -0800 Subject: [PATCH 05/11] update types for larger values --- kwave/ktransducer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kwave/ktransducer.py b/kwave/ktransducer.py index 176e3f05..27cc6db8 100644 --- a/kwave/ktransducer.py +++ b/kwave/ktransducer.py @@ -644,7 +644,7 @@ def delay_mask(self, mode=None): ].min() # -1s compatibility else: mask[unflatten_matlab_mask(mask, active_elements_index - 1)] += self.stored_beamforming_delays_offset # -1s compatibility - return mask.astype(np.uint8) + return mask.astype(np.int16) @property def elevation_beamforming_delays(self): From 3ac8846d61b8049f7fccfd9d01f6c40b24e9d557 Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 14:27:57 -0800 Subject: [PATCH 06/11] try uint16 --- kwave/ktransducer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kwave/ktransducer.py b/kwave/ktransducer.py index 27cc6db8..0f0f3315 100644 --- a/kwave/ktransducer.py +++ b/kwave/ktransducer.py @@ -644,7 +644,7 @@ def delay_mask(self, mode=None): ].min() # -1s compatibility else: mask[unflatten_matlab_mask(mask, active_elements_index - 1)] += self.stored_beamforming_delays_offset # -1s compatibility - return mask.astype(np.int16) + return mask.astype(np.uint16) @property def elevation_beamforming_delays(self): From e785e17c199eaaa3aa29da2e2528d53bcb729554 Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 14:38:14 -0800 Subject: [PATCH 07/11] convert to uint16 --- kwave/utils/matlab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kwave/utils/matlab.py b/kwave/utils/matlab.py index bfefce97..4a10cc4f 100644 --- a/kwave/utils/matlab.py +++ b/kwave/utils/matlab.py @@ -91,7 +91,7 @@ def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) - """ if mask.dtype == "uint8": - mask = mask.astype(np.int16) + mask = mask.astype(np.uint16) if diff is None: return np.expand_dims(arr.ravel(order="F")[mask.ravel(order="F")], axis=-1) # compatibility, n => [n, 1] From 87d5d632428f13728af2c38ff80f1b9b5125b36f Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 14:44:22 -0800 Subject: [PATCH 08/11] revert change --- kwave/utils/matlab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kwave/utils/matlab.py b/kwave/utils/matlab.py index 4a10cc4f..bfefce97 100644 --- a/kwave/utils/matlab.py +++ b/kwave/utils/matlab.py @@ -91,7 +91,7 @@ def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) - """ if mask.dtype == "uint8": - mask = mask.astype(np.uint16) + mask = mask.astype(np.int16) if diff is None: return np.expand_dims(arr.ravel(order="F")[mask.ravel(order="F")], axis=-1) # compatibility, n => [n, 1] From 4ba0095e0d0d91df0418ea64067c911401bebce5 Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 15:00:40 -0800 Subject: [PATCH 09/11] test two step --- kwave/utils/matlab.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/kwave/utils/matlab.py b/kwave/utils/matlab.py index bfefce97..2a426f40 100644 --- a/kwave/utils/matlab.py +++ b/kwave/utils/matlab.py @@ -90,13 +90,11 @@ def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) - """ - if mask.dtype == "uint8": - mask = mask.astype(np.int16) - if diff is None: - return np.expand_dims(arr.ravel(order="F")[mask.ravel(order="F")], axis=-1) # compatibility, n => [n, 1] + flat_mask = mask.ravel(order="F") else: - return np.expand_dims(arr.ravel(order="F")[mask.ravel(order="F") + diff], axis=-1) # compatibility, n => [n, 1] + flat_mask = mask.ravel(order="F") + diff + return np.expand_dims(arr.ravel(order="F")[flat_mask], axis=-1) # compatibility, n => [n, 1] def unflatten_matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) -> Tuple[Union[int, np.ndarray], ...]: From 30880699be91f0abf32e169b860192dbdf352e24 Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 15:16:40 -0800 Subject: [PATCH 10/11] convert uint to unsigned int to allow negative indexing instead of wraparound with unsinged ints --- kwave/utils/matlab.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kwave/utils/matlab.py b/kwave/utils/matlab.py index 2a426f40..dd44ca51 100644 --- a/kwave/utils/matlab.py +++ b/kwave/utils/matlab.py @@ -90,6 +90,9 @@ def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) - """ + if mask.dtype == "uint8": + mask = mask.dtype("int8") + if diff is None: flat_mask = mask.ravel(order="F") else: From bca3a27815ab62215fafcd2f762cb8460231b8eb Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Tue, 19 Nov 2024 15:20:21 -0800 Subject: [PATCH 11/11] fix type --- kwave/utils/matlab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kwave/utils/matlab.py b/kwave/utils/matlab.py index dd44ca51..9c0e43c5 100644 --- a/kwave/utils/matlab.py +++ b/kwave/utils/matlab.py @@ -91,7 +91,7 @@ def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) - """ if mask.dtype == "uint8": - mask = mask.dtype("int8") + mask = mask.astype("int8") if diff is None: flat_mask = mask.ravel(order="F")