Skip to content

Commit

Permalink
Fix numpy warnings (#510)
Browse files Browse the repository at this point in the history
* update linalg import

* explicitly cast to intp

* remove @typechecker

* cast to int16 to replicate legacy behavior

* update types for larger values

* try uint16

* convert to uint16

* revert change

* test two step

* convert uint to unsigned int to allow negative indexing instead of wraparound with unsinged ints

* fix type
  • Loading branch information
waltsims authored Nov 19, 2024
1 parent 0ab1e87 commit 85be199
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion kwave/ktransducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.uint16)

@property
def elevation_beamforming_delays(self):
Expand Down
2 changes: 1 addition & 1 deletion kwave/utils/kwave_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions kwave/utils/matlab.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Tuple, Union, Optional, List

from beartype import beartype as typechecker
import numpy as np


Expand Down Expand Up @@ -75,6 +75,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.
Expand All @@ -89,10 +90,14 @@ def matlab_mask(arr: np.ndarray, mask: np.ndarray, diff: Optional[int] = None) -
"""

if mask.dtype == "uint8":
mask = mask.astype("int8")

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], ...]:
Expand Down

0 comments on commit 85be199

Please sign in to comment.