Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nateanl committed Apr 5, 2023
1 parent e7907d8 commit 0be21fb
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 148 deletions.
171 changes: 78 additions & 93 deletions test/torchaudio_unittest/prototype/functional/functional_test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,19 +551,24 @@ def test_simulate_rir_ism_multi_band(self, channel):
[
(0.1, 0.2, (2, 1, 2500)), # both float
# Per-wall
(torch.rand(4), 0.2, (2, 1, 2500)),
(0.1, torch.rand(4), (2, 1, 2500)),
(torch.rand(4), torch.rand(4), (2, 1, 2500)),
(torch.rand(6), 0.2, (2, 1, 2500)),
(0.1, torch.rand(6), (2, 1, 2500)),
(torch.rand(6), torch.rand(6), (2, 1, 2500)),
# Per-band and per-wall
(torch.rand(6, 4), 0.2, (2, 6, 2500)),
(0.1, torch.rand(6, 4), (2, 6, 2500)),
(torch.rand(6, 4), torch.rand(6, 4), (2, 6, 2500)),
(torch.rand(4, 6), 0.2, (2, 4, 2500)),
(0.1, torch.rand(4, 6), (2, 4, 2500)),
(torch.rand(4, 6), torch.rand(4, 6), (2, 4, 2500)),
]
)
def test_ray_tracing_output_shape(self, absorption, scattering, expected_shape):
room_dim = torch.tensor([20, 25], dtype=self.dtype)
mic_array = torch.tensor([[2, 2], [8, 8]], dtype=self.dtype)
source = torch.tensor([7, 6], dtype=self.dtype)
room_dim = torch.tensor([20, 25, 30], dtype=self.dtype)
mic_array = torch.tensor([[2, 2, 2], [8, 8, 8]], dtype=self.dtype)
source = torch.tensor([7, 6, 5], dtype=self.dtype)
if isinstance(absorption, torch.Tensor):
absorption = absorption.to(self.dtype)
if isinstance(scattering, torch.Tensor):
scattering = scattering.to(self.dtype)

num_rays = 100

hist = F.ray_tracing(
Expand All @@ -578,143 +583,125 @@ def test_ray_tracing_output_shape(self, absorption, scattering, expected_shape):
assert hist.shape == expected_shape

def test_ray_tracing_input_errors(self):
with self.assertRaisesRegex(ValueError, "room must be a 1D tensor"):
with self.assertRaisesRegex(ValueError, "room must be a 1D Tensor."):
F.ray_tracing(
room=torch.tensor([[4, 5]]), source=torch.tensor([0, 0]), mic_array=torch.tensor([[3, 4]]), num_rays=10
)
with self.assertRaisesRegex(ValueError, "room must be a 1D tensor"):
with self.assertRaisesRegex(ValueError, "The shape of room must be"):
F.ray_tracing(
room=torch.tensor([4, 5, 4, 5]),
source=torch.tensor([0, 0]),
mic_array=torch.tensor([[3, 4]]),
num_rays=10,
)
with self.assertRaisesRegex(ValueError, r"mic_array must be 1D tensor of shape \(D,\), or 2D tensor"):
with self.assertRaisesRegex(ValueError, "The second dimension of mic_array must be 3"):
F.ray_tracing(
room=torch.tensor([4, 5]), source=torch.tensor([0, 0]), mic_array=torch.tensor([[[3, 4]]]), num_rays=10
room=torch.tensor([4, 5, 6]),
source=torch.tensor([0, 0, 0]),
mic_array=torch.tensor([[3, 4]]),
num_rays=10,
)
with self.assertRaisesRegex(ValueError, "room must be of float32 or float64 dtype"):
F.ray_tracing(
room=torch.tensor([4, 5]).to(torch.int),
source=torch.tensor([0, 0]),
mic_array=torch.tensor([3, 4]),
room=torch.tensor([4, 5, 6]).to(torch.int),
source=torch.tensor([0, 0, 0]),
mic_array=torch.tensor([[3, 4, 5]]),
num_rays=10,
)
with self.assertRaisesRegex(ValueError, "dtype of room, source and mic_array must be the same"):
F.ray_tracing(
room=torch.tensor([4, 5]).to(torch.float64),
source=torch.tensor([0, 0]).to(torch.float32),
mic_array=torch.tensor([3, 4]),
num_rays=10,
)
with self.assertRaisesRegex(ValueError, "Room dimension D must match with source and mic_array"):
F.ray_tracing(
room=torch.tensor([4, 5, 10], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
num_rays=10,
)
with self.assertRaisesRegex(ValueError, "Room dimension D must match with source and mic_array"):
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
num_rays=10,
)
with self.assertRaisesRegex(ValueError, "Room dimension D must match with source and mic_array"):
F.ray_tracing(
room=torch.tensor([4, 5, 10], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6]).to(torch.float64),
source=torch.tensor([0, 0, 0]).to(torch.float32),
mic_array=torch.tensor([[3, 4, 5]]),
num_rays=10,
)
with self.assertRaisesRegex(ValueError, "time_thres=10 must be at least greater than hist_bin_size=11"):
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([[3, 4, 5]], dtype=torch.float),
num_rays=10,
time_thres=10,
hist_bin_size=11,
)
with self.assertRaisesRegex(ValueError, "The shape of absorption must be"):
with self.assertRaisesRegex(ValueError, "The shape of coefficient must be"):
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([[3, 4, 5]], dtype=torch.float),
num_rays=10,
absorption=torch.rand(5, dtype=torch.float),
)
with self.assertRaisesRegex(ValueError, "The shape of scattering must be"):
with self.assertRaisesRegex(ValueError, "The shape of coefficient must be"):
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([[3, 4, 5]], dtype=torch.float),
num_rays=10,
scattering=torch.rand(5, 5, dtype=torch.float),
)
with self.assertRaisesRegex(ValueError, "The shape of absorption must be"):
with self.assertRaisesRegex(ValueError, "The shape of coefficient must be"):
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([[3, 4, 5]], dtype=torch.float),
num_rays=10,
absorption=torch.rand(5, 5, dtype=torch.float),
)
with self.assertRaisesRegex(ValueError, "The shape of scattering must be"):
with self.assertRaisesRegex(ValueError, "The shape of coefficient must be"):
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([[3, 4, 5]], dtype=torch.float),
num_rays=10,
scattering=torch.rand(5, dtype=torch.float),
)
with self.assertRaisesRegex(
ValueError, "absorption and scattering must have the same number of bands and walls"
):
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([[3, 4, 5]], dtype=torch.float),
num_rays=10,
absorption=torch.rand(6, 4, dtype=torch.float),
scattering=torch.rand(5, 4, dtype=torch.float),
absorption=torch.rand(6, 6, dtype=torch.float),
scattering=torch.rand(5, 6, dtype=torch.float),
)

# Make sure passing different shapes for absorption or scattering doesn't raise an error
# float and tensor
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([[3, 4, 5]], dtype=torch.float),
num_rays=10,
absorption=0.1,
scattering=torch.rand(5, 4, dtype=torch.float),
scattering=torch.rand(5, 6, dtype=torch.float),
)
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([[3, 4, 5]], dtype=torch.float),
num_rays=10,
absorption=torch.rand(5, 4, dtype=torch.float),
absorption=torch.rand(5, 6, dtype=torch.float),
scattering=0.1,
)
# per-wall only and per-band + per-wall
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([[3, 4, 5]], dtype=torch.float),
num_rays=10,
absorption=torch.rand(4, dtype=torch.float),
scattering=torch.rand(6, 4, dtype=torch.float),
absorption=torch.rand(6, dtype=torch.float),
scattering=torch.rand(6, 6, dtype=torch.float),
)
F.ray_tracing(
room=torch.tensor([4, 5], dtype=torch.float),
source=torch.tensor([0, 0], dtype=torch.float),
mic_array=torch.tensor([3, 4], dtype=torch.float),
room=torch.tensor([4, 5, 6], dtype=torch.float),
source=torch.tensor([0, 0, 0], dtype=torch.float),
mic_array=torch.tensor([[3, 4, 5]], dtype=torch.float),
num_rays=10,
absorption=torch.rand(6, 4, dtype=torch.float),
scattering=torch.rand(4, dtype=torch.float),
absorption=torch.rand(6, 6, dtype=torch.float),
scattering=torch.rand(6, dtype=torch.float),
)

def test_ray_tracing_per_band_per_wall_absorption(self):
Expand All @@ -725,14 +712,14 @@ def test_ray_tracing_per_band_per_wall_absorption(self):
(D,) tensor.
"""

room_dim = torch.tensor([20, 25], dtype=self.dtype)
mic_array = torch.tensor([[2, 2], [8, 8]], dtype=self.dtype)
source = torch.tensor([7, 6], dtype=self.dtype)
room_dim = torch.tensor([20, 25, 30], dtype=self.dtype)
mic_array = torch.tensor([[2, 2, 2], [8, 8, 8]], dtype=self.dtype)
source = torch.tensor([7, 6, 5], dtype=self.dtype)
num_rays = 1_000
ABS, SCAT = 0.1, 0.2

absorption = torch.full(fill_value=ABS, size=(6, 4), dtype=self.dtype)
scattering = torch.full(fill_value=SCAT, size=(6, 4), dtype=self.dtype)
absorption = torch.full(fill_value=ABS, size=(4, 6), dtype=self.dtype)
scattering = torch.full(fill_value=SCAT, size=(4, 6), dtype=self.dtype)
hist_per_band_per_wall = F.ray_tracing(
room=room_dim,
source=source,
Expand All @@ -741,8 +728,8 @@ def test_ray_tracing_per_band_per_wall_absorption(self):
absorption=absorption,
scattering=scattering,
)
absorption = torch.full(fill_value=ABS, size=(4,), dtype=self.dtype)
scattering = torch.full(fill_value=SCAT, size=(4,), dtype=self.dtype)
absorption = torch.full(fill_value=ABS, size=(6,), dtype=self.dtype)
scattering = torch.full(fill_value=SCAT, size=(6,), dtype=self.dtype)
hist_per_wall = F.ray_tracing(
room=room_dim,
source=source,
Expand All @@ -762,22 +749,20 @@ def test_ray_tracing_per_band_per_wall_absorption(self):
absorption=absorption,
scattering=scattering,
)
assert hist_per_band_per_wall.shape == (2, 6, 2500)
assert hist_per_band_per_wall.shape == (2, 4, 2500)
assert hist_per_wall.shape == (2, 1, 2500)
assert hist_single.shape == (2, 1, 2500)
torch.testing.assert_close(hist_single, hist_per_wall)

hist_single = hist_single.expand(2, 6, 2500)
hist_single = hist_single.expand(2, 4, 2500)
torch.testing.assert_close(hist_single, hist_per_band_per_wall)

@parameterized.expand(
[
([20, 25], [2, 2], [[8, 8], [7, 6]], 10_000), # 2D with 2 mics
([20, 25, 30], [1, 10, 5], [[8, 8, 22]], 1_000), # 3D with 1 mic
]
)
def test_ray_tracing_same_results_as_pyroomacoustics(self, room_dim, source, mic_array, num_rays):

walls = ["west", "east", "south", "north"]
if len(room_dim) == 3:
walls += ["floor", "ceiling"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,15 @@ def test_simulate_rir_ism_multi_band(self, channel):

@parameterized.expand(
[
([20, 25], [2, 2], [[8, 8], [7, 6]], 1_000), # 2D with 2 mics
([20, 25, 30], [1, 10, 5], [[8, 8, 22]], 500), # 3D with 1 mic
]
)
def test_ray_tracing(self, room_dim, source, mic_array, num_rays):
num_walls = 4 if len(room_dim) == 2 else 6
num_walls = 6
num_bands = 3

absorption = torch.rand(num_bands, num_walls, dtype=torch.float32)
scattering = torch.rand(num_bands, num_walls, dtype=torch.float32)
absorption = torch.rand(num_bands, num_walls, dtype=self.dtype)
scattering = torch.rand(num_bands, num_walls, dtype=self.dtype)

energy_thres = 1e-7
time_thres = 10.0
Expand Down
Loading

0 comments on commit 0be21fb

Please sign in to comment.