Skip to content

Commit

Permalink
Merge branch 'waltsims:master' into auto-chunking
Browse files Browse the repository at this point in the history
  • Loading branch information
djps authored Nov 2, 2023
2 parents 3ee0bee + d2176e0 commit 08cc73b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ RUN apt-get update
RUN apt-get install -y gfortran libopenblas-dev liblapack-dev libgl1-mesa-glx libglib2.0-0 libgl1-mesa-glx libglib2.0-0 git
RUN pip install --upgrade pip
COPY pyproject.toml .
COPY README.md .
#COPY README.md .
COPY docs/ docs
COPY LICENSE .
COPY kwave/ kwave
RUN pip install '.[test]'
42 changes: 41 additions & 1 deletion kwave/utils/kwave_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Element:
active: bool
measure: float

label: Optional[str] = None
group_type: Optional[str] = None
element_number: Optional[int] = None

Expand All @@ -36,10 +37,13 @@ class Element:
radius_of_curvature: Optional[float] = None
position: Optional[np.ndarray] = None
focus_position: Optional[np.ndarray] = None

# custom element
integration_points: Optional[np.ndarray] = None

length: Optional[float] = None
width: Optional[float] = None
orientation: Optional = None
orientation: Optional[np.ndarray] = None

start_point: Optional[np.ndarray] = None
end_point: Optional[np.ndarray] = None
Expand Down Expand Up @@ -220,7 +224,43 @@ def add_bowl_element(self, position, radius, diameter, focus_pos):
active=True,
measure=area
))

def add_custom_element(self, integration_points, measure, element_dim, label):

assert isinstance(integration_points, (np.ndarray)), "'integration_points' must be a numpy array"
assert isinstance(measure, (int, float)), "'measure' must be an integer or float"
assert isinstance(element_dim, (int)) and element_dim in [1, 2, 3], "'element_dim' must be an integer and either 1, 2 or 3"
assert isinstance(label, (str)), "'label' must be a string"

# check the dimensionality of the integration points
input_dim = integration_points.shape[0]
if (input_dim < 1) or (input_dim > 3):
raise ValueError("Input integration_points must be a 1 x N (in 1D), 2 x N (in 2D), or 3 x N (in 3D) array.")

# check if this is the first element, and set the dimension
if self.number_elements == 0:
self.dim = input_dim

# check that the element is being added to an array with the
# correct dimensions
if self.dim != input_dim:
raise ValueError(f"{input_dim}D custom element cannot be added to an array with {self.dim}D elements.")

self.number_elements += 1

self.elements.append(
Element(
group_id=0,
type='custom',
dim=element_dim,
label=label,
integration_points=integration_points,
active=True,
measure=measure
)
)


def add_rect_element(self, position, Lx, Ly, theta):

assert isinstance(position, (list, tuple)), "'position' must be a list or tuple"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
recorder.recordObject('kwave_array', kwave_array);
recorder.increment();

% Useful for testing addCustomElement in 3D
kwave_array.addCustomElement(single([[1, 1, 1, 2, 2, 2, 3, 3, 3]; [1, 2, 3, 1, 2, 3, 1, 2, 3]; [0, 0, 0, 0, 0, 0, 0, 0, 0]]), 9, 2, 'custom_3d');
recorder.recordObject('kwave_array', kwave_array);
recorder.increment();

% Useful for testing addRectElement in 3D
kwave_array.addRectElement([12, -8, 0.3], 3, 4, [2, 4, 5]);
recorder.recordObject('kwave_array', kwave_array);
Expand All @@ -61,6 +66,11 @@
recorder.recordObject('kwave_array', kwave_array);
recorder.increment();

% Useful for testing addCustomElement in 2D
kwave_array.addCustomElement(single([[1, 1, 1, 2, 2, 2, 3, 3, 3]; [1, 2, 3, 1, 2, 3, 1, 2, 3]]), 9, 1, 'custom_2d');
recorder.recordObject('kwave_array', kwave_array);
recorder.increment();

% Useful for testing addRectElement in 2D
kwave_array.addRectElement([12, -8], 3, 4, 2);
recorder.recordObject('kwave_array', kwave_array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
output_file = 'collectedValues/resize.mat';
recorder = utils.TestRecorder(output_file);

% TODO: test also for nearest
% interp_methods = {'nearest', 'linear'};
interp_methods = {'linear'};
interp_methods = {'nearest', 'linear'};

% Generate a random 3D volume with dimensions Nx x Ny x Nz
Nx = randi([10,20]);
Expand All @@ -30,4 +28,3 @@

end
recorder.saveRecordsToDisk();

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

import numpy as np
import pytest
from kwave.kgrid import kWaveGrid

from kwave.utils.kwave_array import kWaveArray
Expand Down Expand Up @@ -44,6 +45,22 @@ def test_kwave_array():
check_kwave_array_equality(kwave_array, reader.expected_value_of('kwave_array'))
reader.increment()

kwave_array.add_custom_element(
integration_points=np.array([[1, 1, 1, 2, 2, 2, 3, 3, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3], [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.float32),
measure=9,
element_dim=2, label='custom_3d'
)
check_kwave_array_equality(kwave_array, reader.expected_value_of('kwave_array'))

with pytest.raises(ValueError):
kwave_array.add_custom_element(
integration_points=np.array([[1, 1, 1, 2, 2, 2, 3, 3, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3]], dtype=np.float32),
measure=9,
element_dim=2, label='custom_3d'
)

reader.increment()

kwave_array.add_rect_element([12, -8, 0.3], 3, 4, [2, 4, 5])
check_kwave_array_equality(kwave_array, reader.expected_value_of('kwave_array'))
reader.increment()
Expand All @@ -61,6 +78,13 @@ def test_kwave_array():
check_kwave_array_equality(kwave_array, reader.expected_value_of('kwave_array'))
reader.increment()

kwave_array.add_custom_element(
np.array([[1, 1, 1, 2, 2, 2, 3, 3, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3]], dtype=np.float32),
9, 1, label='custom_2d'
)
check_kwave_array_equality(kwave_array, reader.expected_value_of('kwave_array'))
reader.increment()

# Useful for testing addRectElement in 2D
kwave_array.add_rect_element([12, -8], 3, 4, 2)
check_kwave_array_equality(kwave_array, reader.expected_value_of('kwave_array'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ def test_resize():

assert np.allclose(expected_resized_volume,
resized_volume), f"Results do not match for {i + 1} dimensional case."
reader.increment()

logging.log(logging.INFO, 'revolve2d(..) works as expected!')
logging.log(logging.INFO, 'resize(..) works as expected!')

0 comments on commit 08cc73b

Please sign in to comment.