Skip to content

Commit bfa832b

Browse files
committed
waltsims#460: moved element testing to it's own file in the tests dir.
1 parent a89bfb4 commit bfa832b

3 files changed

Lines changed: 51 additions & 54 deletions

File tree

tests/matlab_test_data_collectors/python_testers/element_test.py

Whitespace-only changes.

tests/matlab_test_data_collectors/python_testers/kWaveArray_test.py

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,11 @@
66
from kwave.data import Vector
77
from kwave.kgrid import kWaveGrid
88

9-
from kwave.utils.kwave_array import Element, kWaveArray
9+
from kwave.utils.kwave_array import kWaveArray
1010
from tests.matlab_test_data_collectors.python_testers.utils.check_equality import check_kwave_array_equality
1111
from tests.matlab_test_data_collectors.python_testers.utils.record_reader import TestRecordReader
1212

1313

14-
def test_element_is_approximately_equal():
15-
base_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1])
16-
17-
# Test cases
18-
assert base_element.is_close(Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1]))
19-
assert base_element.is_close(Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1.000001]))
20-
assert not base_element.is_close(Element(group_id=1, type="rect", dim=2, active=True, measure=2, position=[1, 1, 1]))
21-
assert not base_element.is_close(Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[2, 2, 2]))
22-
assert not base_element.is_close(Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, float('nan')]))
23-
assert not base_element.is_close(Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, float('inf')]))
24-
assert not base_element.is_close(Element(group_id=0, type="annular", dim=2, active=True, measure=1, position=[1, 1, 1]))
25-
26-
def test_element_is_approximately_equal_nan():
27-
nan_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, float('nan')])
28-
assert nan_element.is_close(nan_element, equal_nan=True)
29-
assert not nan_element.is_close(nan_element, equal_nan=False)
30-
31-
def test_element_is_approximately_equal_boundary():
32-
base_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1])
33-
boundary_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1 + 1.1e-5])
34-
assert not base_element.is_close(boundary_element)
35-
assert base_element.is_close(boundary_element, rtol=1.2e-5)
36-
37-
def test_element_is_approximately_equal_consistency():
38-
base_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1])
39-
other_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1.000001])
40-
assert base_element.is_close(other_element) == np.allclose(base_element.position, other_element.position)
41-
assert base_element.is_close(other_element) == base_element.is_close(other_element, rtol=1e-05, atol=1e-08, equal_nan=False)
42-
43-
def test_element_is_approximately_equal_type_error():
44-
element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1])
45-
46-
with pytest.raises(TypeError, match=r"not an element with <class 'str'> is not of type Element"):
47-
element.is_close("not an element")
48-
49-
def test_element_equality():
50-
element1 = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1])
51-
element2 = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1.000001])
52-
element3 = Element(group_id=1, type="rect", dim=2, active=True, measure=2, position=[1, 1, 1])
53-
54-
assert element1 == element1
55-
assert element1 != element2
56-
assert element1.is_approximately_equal(element2)
57-
assert element1 != element3
58-
assert not element1.is_approximately_equal(element3)
59-
assert element1 != element4
60-
assert not element1.is_approximately_equal(element4)
61-
with pytest.raises(TypeError):
62-
element1 != element5
63-
with pytest.raises(TypeError):
64-
element1.is_approximately_equal(element5)
65-
66-
6714
def test_kwave_array():
6815
test_record_path = os.path.join(Path(__file__).parent, "collectedValues/kWaveArray.mat")
6916
reader = TestRecordReader(test_record_path)

tests/test_element.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import numpy as np
2+
import pytest
3+
4+
from kwave.utils.kwave_array import Element
5+
6+
7+
def test_element_is_approximately_equal():
8+
base_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1])
9+
10+
# Test cases
11+
assert base_element.is_approximately_equal(Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1]))
12+
assert base_element.is_approximately_equal(Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1.000001]))
13+
assert not base_element.is_approximately_equal(Element(group_id=1, type="rect", dim=2, active=True, measure=2, position=[1, 1, 1]))
14+
assert not base_element.is_approximately_equal(Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[2, 2, 2]))
15+
assert not base_element.is_approximately_equal(
16+
Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, float("nan")])
17+
)
18+
assert not base_element.is_approximately_equal(
19+
Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, float("inf")])
20+
)
21+
assert not base_element.is_approximately_equal(Element(group_id=0, type="annular", dim=2, active=True, measure=1, position=[1, 1, 1]))
22+
23+
24+
def test_element_is_approximately_equal_nan():
25+
nan_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, float("nan")])
26+
assert nan_element.is_approximately_equal(nan_element, equal_nan=True)
27+
assert not nan_element.is_approximately_equal(nan_element, equal_nan=False)
28+
29+
30+
def test_element_is_approximately_equal_boundary():
31+
base_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1])
32+
boundary_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1 + 1.1e-5])
33+
assert not base_element.is_approximately_equal(boundary_element)
34+
assert base_element.is_approximately_equal(boundary_element, rtol=1.2e-5)
35+
36+
37+
def test_element_is_approximately_equal_consistency():
38+
base_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1])
39+
other_element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1.000001])
40+
assert base_element.is_approximately_equal(other_element) == np.allclose(base_element.position, other_element.position)
41+
assert base_element.is_approximately_equal(other_element) == base_element.is_approximately_equal(
42+
other_element, rtol=1e-05, atol=1e-08, equal_nan=False
43+
)
44+
45+
46+
def test_element_is_approximately_equal_type_error():
47+
element = Element(group_id=0, type="rect", dim=2, active=True, measure=1, position=[1, 1, 1])
48+
49+
with pytest.raises(TypeError, match=r"not an element with <class 'str'> is not of type Element"):
50+
element.is_approximately_equal("not an element")

0 commit comments

Comments
 (0)