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

Update record_reader.py to remove warnings #211

Merged
merged 37 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c17f0b0
Update record_reader.py to remove warnings
waltsims Sep 21, 2023
b810f1b
Update beamform.py
waltsims Sep 28, 2023
56adb93
Update simulation_options.py
waltsims Sep 28, 2023
5093b86
clean up zero division
waltsims Sep 28, 2023
9bcadc6
Merge branch 'master' into clean-up-pytest-warnings
waltsims Dec 19, 2023
ce938f6
clean up minor warnings
waltsims Dec 19, 2023
3c41b7a
add N assertion
waltsims Dec 19, 2023
62ac65e
fix element-wise string comparison warning
waltsims Dec 19, 2023
15b2423
fix num_radial
waltsims Dec 19, 2023
5e2bf1d
try something for get_win
waltsims Dec 19, 2023
1bd6547
add back d_radial
waltsims Dec 19, 2023
cd92948
comment out divide by zero assertation
waltsims Dec 19, 2023
a49e180
check for power exponent of 1
waltsims Dec 19, 2023
d6b15dd
test new assertion
waltsims Dec 19, 2023
29aa637
revert changes
waltsims Dec 19, 2023
c9f2d65
modify assertion
waltsims Dec 19, 2023
bea50bf
remove fixes that don't help
waltsims Dec 19, 2023
a1b9a41
assert Z is greater than 0
waltsims Dec 19, 2023
ee1022f
catch zero distance case
waltsims Dec 19, 2023
6ae7cff
attemt to remove warning with another assertion
waltsims Dec 19, 2023
192ca01
Revert fix. discuss matlab behaviour
waltsims Dec 19, 2023
573d025
treat Z as an array
waltsims Dec 19, 2023
3e3c93c
assert N > 1
waltsims Dec 19, 2023
ba7365f
Try to solve divide by zero warnings in signals
waltsims Dec 19, 2023
05106ad
Add TODO
waltsims Dec 19, 2023
341a112
revert assertion
waltsims Dec 20, 2023
f4a940b
Merge branch 'master' into clean-up-pytest-warnings
waltsims Jan 2, 2024
60f4269
Merge branch 'master' into clean-up-pytest-warnings
waltsims Jan 16, 2024
ef68882
Merge branch 'master' into clean-up-pytest-warnings
faridyagubbayli Jan 16, 2024
05a2ba6
update value checking
waltsims Jan 17, 2024
0f92312
Merge branch 'clean-up-pytest-warnings' of github.com:waltsims/k-wave…
waltsims Jan 17, 2024
092f4ee
update assertion errors
waltsims Jan 17, 2024
365d716
Merge branch 'master' into clean-up-pytest-warnings
waltsims Jan 17, 2024
c8fc470
Merge branch 'clean-up-pytest-warnings' of github.com:waltsims/k-wave…
waltsims Jan 17, 2024
df792f7
Merge branch 'master' into clean-up-pytest-warnings
waltsims Jan 18, 2024
9ad6801
change assertion the validation
waltsims Jan 18, 2024
7383e77
Merge branch 'clean-up-pytest-warnings' of github.com:waltsims/k-wave…
waltsims Jan 18, 2024
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
2 changes: 1 addition & 1 deletion kwave/options/simulation_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def option_factory(kgrid: "kWaveGrid", options: SimulationOptions):
options.plot_scale = [-1, 1]

# replace defaults with user defined values if provided and check inputs
if (val := options.pml_alpha) is not None and options.pml_alpha != 'auto':
if (val := options.pml_alpha) is not None and not isinstance(options.pml_alpha, str):
waltsims marked this conversation as resolved.
Show resolved Hide resolved
# check input is correct size
val = np.atleast_1d(val)
if val.size > kgrid.dim:
Expand Down
3 changes: 2 additions & 1 deletion kwave/reconstruction/beamform.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def focus(kgrid, input_signal, source_mask, focus_position, sound_speed):
input_signal_mat: matrix of time series following the source points
"""

assert kgrid.t_array != 'auto', "kgrid.t_array must be defined."
assert not isinstance(kgrid.t_array, str), "kgrid.t_array must be a numeric array."

if isinstance(sound_speed, int):
sound_speed = float(sound_speed)

Expand Down
19 changes: 14 additions & 5 deletions kwave/utils/atten_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def atten_comp(
dt: time step [s]
c: sound speed [m/s]
alpha_0: power law absorption prefactor [dB/(MHz^y cm)]
y: power law absorption exponent [0 < y < 3, y ~= 1]
y: power law absorption exponent [0 < y < 3, y != 1]
display_updates: Boolean controlling whether command line updates
% and compute time are printed to the command line
distribution: default TF distribution
Expand Down Expand Up @@ -67,6 +67,8 @@ def atten_comp(
# extract signal characteristics
N, num_signals = signal.shape

assert y != 1, "A power exponent [y] of 1 is not valid."
faridyagubbayli marked this conversation as resolved.
Show resolved Hide resolved

# convert absorption coefficient to nepers
alpha_0 = db2neper(alpha_0, y)

Expand Down Expand Up @@ -243,15 +245,22 @@ def findClosest(arr, value):
dist_vec = c * dt * (np.arange(N) - t0)
dist_vec[dist_vec < 0] = 0

# create the time variant filter
# Check if f_array and dist_vec are valid
assert f_array is not None and len(f_array) > 0, "f_array must be valid."
assert dist_vec is not None and len(dist_vec) > 0, "dist_vec must be valid."
faridyagubbayli marked this conversation as resolved.
Show resolved Hide resolved

# Create the time variant filter
f_mat, dist_mat = np.meshgrid(f_array, dist_vec)

assert y != 1, "A power exponent [y] of 1 is not valid." # this is a duplicate assertion to attempt to remove a warning

# Add conditionals or use np.where to manage zero and NaN
part_1 = (2 * np.pi * np.abs(f_mat)) ** y
part_2 = 1j * np.tan(np.pi * y / 2)
part_3 = (2 * np.pi * f_mat)
part_4 = (2 * np.pi * np.abs(f_mat)) ** (y - 1)
tv_filter = alpha_0 * dist_mat * (
part_1 - part_2 * part_3 * part_4
)

tv_filter = alpha_0 * dist_mat * (part_1 - part_2 * part_3 * part_4)

# convert cutoff frequency to a window size
N_win_array = np.floor((cutoff_freq_array / f_array[-1]) * N) - 1
Expand Down
12 changes: 10 additions & 2 deletions kwave/utils/mapgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ def make_spiral_circle_points(num_points: int, radius: float) -> np.ndarray:
return p0

def make_concentric_circle_points(num_points: int, radius: float) -> Tuple[np.ndarray, int]:

assert num_points >= 1, "The number of points must be greater or equal to 1."

num_radial = int(np.ceil(np.sqrt(num_points / np.pi)))

try:
d_radial = radius / (num_radial - 1)
except ZeroDivisionError:
Expand Down Expand Up @@ -205,7 +209,7 @@ def make_cart_bowl(bowl_pos: np.ndarray, radius: float, diameter: float, focus_p
bowl = R @ p0 + b

# plot results
if plot_bowl:
if plot_bowl is True:
# select suitable axis scaling factor
_, scale, prefix, unit = scale_SI(np.max(bowl))

Expand Down Expand Up @@ -558,6 +562,8 @@ def make_cart_sphere(radius: float, num_points: int, center_pos: Vector = Vector
r = np.sqrt(1 - (y ** 2))
phi = k * inc

assert num_points > 0, "num_points must be greater than 0"

# create the sphere
sphere = radius * np.concatenate([np.cos(phi) * r[np.newaxis, :], y[np.newaxis, :], np.sin(phi) * r[np.newaxis, :]])

Expand Down Expand Up @@ -2720,6 +2726,8 @@ def calculate_axial_pressure() -> float:
def calculate_lateral_pressure() -> float:
# calculate magnitude of the lateral pressure at the geometric focus
Z = k * lateral_positions * diameter / (2 * radius)
# TODO: this should work
# assert np.all(Z) > 0, 'Z must be greater than 0'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not to enable this check?

Copy link
Owner Author

Choose a reason for hiding this comment

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

this fails the CI.

lateral_pressure = 2. * density * sound_speed * velocity * k * h * scipy.special.jv(1, Z) / Z

# replace origin with limit
Expand Down Expand Up @@ -2968,7 +2976,7 @@ def make_cart_spherical_segment(bowl_pos: np.ndarray, radius: float, inner_diame
segment = R @ p0 + b

# plot results
if plot_bowl:
if plot_bowl is True:
_, scale, prefix, unit = scale_SI(np.max(segment))

# create the figure
Expand Down
10 changes: 9 additions & 1 deletion kwave/utils/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,16 @@ def compute_linear_transform(pos1, pos2, offset=None):
# Compute vector pointing from pos1 to pos2
beam_vec = pos2 - pos1

magnitude = np.linalg.norm(beam_vec)

# TODO: it seems matlab behaviour is to return nans when positions are the same.
# we should open an issue and change our behaviour once matlab is fixed.
# if magnitude == 0:
# # "pos1 and pos2 are the same"
# return np.eye(3), 0 if offset is None else offset

# Normalise to give unit beam vector
beam_vec = beam_vec / np.linalg.norm(beam_vec)
beam_vec = beam_vec / magnitude

# Canonical normalised beam_vec (canonical pos1 is [0, 0, 1])
beam_vec0 = np.array([0, 0, -1])
Expand Down
4 changes: 4 additions & 0 deletions kwave/utils/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ def cosine_series(n: int, N: int, coeffs: List[float]) -> np.ndarray:

# create the window
if N.size == 1:
# TODO: what should this behaviour be if N is a list of ints? make windows of multiple lengths?
waltsims marked this conversation as resolved.
Show resolved Hide resolved
n = np.arange(0, N)

# TODO: find failure cases in test suite when N is zero.
# assert np.all(N) > 1, 'Signal length N must be greater than 1'

if type_ == 'Bartlett':
win = (2 / (N - 1) * ((N - 1) / 2 - abs(n - (N - 1) / 2))).T
elif type_ == 'Bartlett-Hanning':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import numpy as np

from scipy.io import loadmat


class TestRecordReader(object):

class TestRecordReader(object):
__test__ = False
waltsims marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, record_filename):
recorded_data = loadmat(record_filename, simplify_cells=True)
self._records = recorded_data
Expand Down
Loading