Skip to content
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
66755ad
add available device to test_canberra_metric.py
BanzaiTokyo Apr 24, 2025
9229e3b
add _double_dtype ad dtype when transfrring errors to device
BanzaiTokyo Apr 24, 2025
2f6320a
available devices in test_fractional_absolute_error.py, test_fraction…
BanzaiTokyo Apr 24, 2025
557f549
when transferring to device use dtype
BanzaiTokyo Apr 24, 2025
0130773
add available device to tests
BanzaiTokyo Apr 24, 2025
94a002b
use self._double_dtype instead of torch.double
BanzaiTokyo Apr 24, 2025
2631377
use self._double_dtype when moving to device in epoch_metric.py
BanzaiTokyo Apr 24, 2025
d5b9e5a
removes unnecessary tests
BanzaiTokyo Apr 24, 2025
f99b643
rollbacks changes in epoch_metric.py
BanzaiTokyo Apr 24, 2025
e24ce01
redo test_integration
BanzaiTokyo Apr 24, 2025
3dbbe1e
redo test_integration
BanzaiTokyo Apr 24, 2025
1cf59fa
casting of eps in _update
BanzaiTokyo Apr 24, 2025
6f0599d
more conversions to torch
BanzaiTokyo Apr 24, 2025
35527d5
in _torch_median move output to cpu if mps (torch.kthvalue is not sup…
BanzaiTokyo Apr 25, 2025
c13837e
fixing test_degenerated_sample
BanzaiTokyo Apr 25, 2025
c85dab1
fixing test_degenerated_sample
BanzaiTokyo Apr 25, 2025
c662c44
rename upper case variables
BanzaiTokyo Apr 25, 2025
e471064
change range to 3
BanzaiTokyo Apr 25, 2025
37a0469
rewrite test_compute
BanzaiTokyo Apr 25, 2025
71af57e
rewrite test_fractional_bias
BanzaiTokyo Apr 25, 2025
d59cb6f
remove prints
BanzaiTokyo Apr 25, 2025
da2e75d
rollback eps in canberra_metric.py
BanzaiTokyo Apr 25, 2025
0a2f6d4
rollback test_epoch_metric.py because the changes are moved to a sepa…
BanzaiTokyo Apr 25, 2025
d1ef2d4
Merge branch 'master' into regression_tests_add_available_device
BanzaiTokyo Apr 25, 2025
667332d
set sum_of_errors as _double_dtype
BanzaiTokyo Apr 28, 2025
713aab9
Merge branch 'master' into regression_tests_add_available_device
BanzaiTokyo Apr 28, 2025
579d035
use torch instead of numpy where possible in test_canberra_metric.py
BanzaiTokyo Apr 28, 2025
cab29ca
Merge branch 'master' into regression_tests_add_available_device
BanzaiTokyo Apr 29, 2025
e6c96de
remove double_dtype from metrics
BanzaiTokyo Apr 29, 2025
346e0e1
takes into account PR comments
BanzaiTokyo May 2, 2025
ded98cf
refactor integration tests for fractional bias and fractional absolut…
BanzaiTokyo May 2, 2025
63baad6
remove modifications in test
BanzaiTokyo May 3, 2025
151f16b
Merge branch 'master' into regression_metrics_updates_mps
BanzaiTokyo May 3, 2025
7af547f
test_canberra_metric.py test_fractional_absolute_error.py test_fracti…
BanzaiTokyo May 3, 2025
6ca67bd
test_geometric_mean_relative_absolute_error.py test_kendall_correlati…
BanzaiTokyo May 3, 2025
d1c2504
Merge branch 'master' into 2_regression_tests_available_device
BanzaiTokyo May 4, 2025
42dde5a
revert "if torch.isnan(r)" check in pearson_correlation.py
BanzaiTokyo May 4, 2025
38d1573
in test_compute use torch instead of np to generate data
BanzaiTokyo May 5, 2025
8c61dd6
refactor test_integration_manhattan_distance
BanzaiTokyo May 5, 2025
b4921d2
refactor test_integration_kendall_rank_correlation
BanzaiTokyo May 5, 2025
f048bd0
Merge branch 'master' into 2_regression_tests_available_device
BanzaiTokyo May 5, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,28 @@ def test_wrong_input_shapes():
m.update((torch.rand(4, 1), torch.rand(4)))


def test_compute():
def test_compute(available_device):
size = 51
np_y_pred = np.random.rand(size)
np_y = np.random.rand(size)
np_gmrae = np.exp(np.log(np.abs(np_y - np_y_pred) / np.abs(np_y - np_y.mean())).mean())

m = GeometricMeanRelativeAbsoluteError()
y_pred = torch.from_numpy(np_y_pred)
y = torch.from_numpy(np_y)
m = GeometricMeanRelativeAbsoluteError(device=available_device)
assert m._device == torch.device(available_device)
y_pred = (
torch.from_numpy(np_y_pred).to(dtype=torch.float32)
if available_device == "mps"
else torch.from_numpy(np_y_pred)
)
y = torch.from_numpy(np_y).to(dtype=torch.float32) if available_device == "mps" else torch.from_numpy(np_y)

m.reset()
m.update((y_pred, y))

assert np_gmrae == pytest.approx(m.compute())


def test_integration():
def test_integration(available_device):
y_pred = torch.rand(size=(100,))
y = torch.rand(size=(100,))

Expand All @@ -59,7 +64,8 @@ def update_fn(engine, batch):

engine = Engine(update_fn)

m = GeometricMeanRelativeAbsoluteError()
m = GeometricMeanRelativeAbsoluteError(device=available_device)
assert m._device == torch.device(available_device)
m.attach(engine, "gmrae")

np_y = y.numpy().ravel()
Expand Down
12 changes: 8 additions & 4 deletions tests/ignite/metrics/regression/test_kendall_correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ def test_wrong_variant():


@pytest.mark.parametrize("variant", ["b", "c"])
def test_kendall_correlation(variant: str):
def test_kendall_correlation(variant: str, available_device):
a = np.random.randn(4).astype(np.float32)
b = np.random.randn(4).astype(np.float32)
c = np.random.randn(4).astype(np.float32)
d = np.random.randn(4).astype(np.float32)
ground_truth = np.random.randn(4).astype(np.float32)

m = KendallRankCorrelation(variant=variant)
m = KendallRankCorrelation(variant=variant, device=available_device)
assert m._device == torch.device(available_device)

m.update((torch.from_numpy(a), torch.from_numpy(ground_truth)))
np_ans = kendalltau(a, ground_truth, variant=variant).statistic
Expand Down Expand Up @@ -99,7 +100,9 @@ def test_case(request):

@pytest.mark.parametrize("n_times", range(5))
@pytest.mark.parametrize("variant", ["b", "c"])
def test_integration(n_times: int, variant: str, test_case: Tuple[Tensor, Tensor, int]):
def test_integration_kendall_rank_correlation(
n_times: int, variant: str, test_case: Tuple[Tensor, Tensor, int], available_device
):
y_pred, y, batch_size = test_case

np_y = y.numpy().ravel()
Expand All @@ -113,7 +116,8 @@ def update_fn(engine: Engine, batch):

engine = Engine(update_fn)

m = KendallRankCorrelation(variant=variant)
m = KendallRankCorrelation(variant=variant, device=available_device)
assert m._device == torch.device(available_device)
m.attach(engine, "kendall_tau")

data = list(range(y_pred.shape[0] // batch_size))
Expand Down
82 changes: 44 additions & 38 deletions tests/ignite/metrics/regression/test_manhattan_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,83 +20,89 @@ def test_wrong_input_shapes():
m.update((torch.rand(4, 1), torch.rand(4)))


def test_mahattan_distance():
def test_mahattan_distance(available_device):
a = np.random.randn(4)
b = np.random.randn(4)
c = np.random.randn(4)
d = np.random.randn(4)
ground_truth = np.random.randn(4)

m = ManhattanDistance()
m = ManhattanDistance(device=available_device)
assert m._device == torch.device(available_device)

manhattan = DistanceMetric.get_metric("manhattan")

m.update((torch.from_numpy(a), torch.from_numpy(ground_truth)))
torch_a = torch.from_numpy(a).to(dtype=torch.float32) if available_device == "mps" else torch.from_numpy(a)
torch_ground_truth = (
torch.from_numpy(ground_truth).to(dtype=torch.float32)
if available_device == "mps"
else torch.from_numpy(ground_truth)
)
m.update((torch_a, torch_ground_truth))
np_sum = np.abs(ground_truth - a).sum()
assert m.compute() == pytest.approx(np_sum)
assert manhattan.pairwise([a, ground_truth])[0][1] == pytest.approx(np_sum)

m.update((torch.from_numpy(b), torch.from_numpy(ground_truth)))
torch_b = torch.from_numpy(b).to(dtype=torch.float32) if available_device == "mps" else torch.from_numpy(b)
m.update((torch_b, torch_ground_truth))
np_sum += np.abs(ground_truth - b).sum()
assert m.compute() == pytest.approx(np_sum)
v1 = np.hstack([a, b])
v2 = np.hstack([ground_truth, ground_truth])
assert manhattan.pairwise([v1, v2])[0][1] == pytest.approx(np_sum)

m.update((torch.from_numpy(c), torch.from_numpy(ground_truth)))
torch_c = torch.from_numpy(c).to(dtype=torch.float32) if available_device == "mps" else torch.from_numpy(c)
m.update((torch_c, torch_ground_truth))
np_sum += np.abs(ground_truth - c).sum()
assert m.compute() == pytest.approx(np_sum)
v1 = np.hstack([v1, c])
v2 = np.hstack([v2, ground_truth])
assert manhattan.pairwise([v1, v2])[0][1] == pytest.approx(np_sum)

m.update((torch.from_numpy(d), torch.from_numpy(ground_truth)))
torch_d = torch.from_numpy(d).to(dtype=torch.float32) if available_device == "mps" else torch.from_numpy(d)
m.update((torch_d, torch_ground_truth))
np_sum += np.abs(ground_truth - d).sum()
assert m.compute() == pytest.approx(np_sum)
v1 = np.hstack([v1, d])
v2 = np.hstack([v2, ground_truth])
assert manhattan.pairwise([v1, v2])[0][1] == pytest.approx(np_sum)


def test_integration():
def _test(y_pred, y, batch_size):
def update_fn(engine, batch):
idx = (engine.state.iteration - 1) * batch_size
y_true_batch = np_y[idx : idx + batch_size]
y_pred_batch = np_y_pred[idx : idx + batch_size]
return torch.from_numpy(y_pred_batch), torch.from_numpy(y_true_batch)

engine = Engine(update_fn)
@pytest.mark.parametrize("n_times", range(3))
@pytest.mark.parametrize(
"test_case",
[
(torch.rand(size=(100,)), torch.rand(size=(100,)), 10),
(torch.rand(size=(100, 1)), torch.rand(size=(100, 1)), 20),
],
)
def test_integration(test_case, n_times, available_device):
y_pred, y, batch_size = test_case

m = ManhattanDistance()
m.attach(engine, "md")

np_y = y.numpy().ravel()
np_y_pred = y_pred.numpy().ravel()
np_y = y.numpy().ravel()
np_y_pred = y_pred.numpy().ravel()

manhattan = DistanceMetric.get_metric("manhattan")
def update_fn(engine, batch):
idx = (engine.state.iteration - 1) * batch_size
y_true_batch = np_y[idx : idx + batch_size]
y_pred_batch = np_y_pred[idx : idx + batch_size]
return torch.from_numpy(y_pred_batch), torch.from_numpy(y_true_batch)

data = list(range(y_pred.shape[0] // batch_size))
md = engine.run(data, max_epochs=1).metrics["md"]
engine = Engine(update_fn)

assert manhattan.pairwise([np_y_pred, np_y])[0][1] == pytest.approx(md)
metric = ManhattanDistance(device=available_device)
assert metric._device == torch.device(available_device)
metric.attach(engine, "md")

def get_test_cases():
test_cases = [
(torch.rand(size=(100,)), torch.rand(size=(100,)), 10),
(torch.rand(size=(100, 1)), torch.rand(size=(100, 1)), 20),
]
return test_cases
data = list(range(y_pred.shape[0] // batch_size))
result = engine.run(data, max_epochs=1).metrics["md"]

for _ in range(5):
# check multiple random inputs as random exact occurencies are rare
test_cases = get_test_cases()
for y_pred, y, batch_size in test_cases:
_test(y_pred, y, batch_size)
expected = DistanceMetric.get_metric("manhattan").pairwise([np_y_pred, np_y])[0][1]
assert expected == pytest.approx(result)


def test_error_is_not_nan():
m = ManhattanDistance()
def test_error_is_not_nan(available_device):
m = ManhattanDistance(device=available_device)
assert m._device == torch.device(available_device)
m.update((torch.zeros(4), torch.zeros(4)))
assert not (torch.isnan(m._sum_of_errors).any() or torch.isinf(m._sum_of_errors).any()), m._sum_of_errors

Expand Down
114 changes: 47 additions & 67 deletions tests/ignite/metrics/regression/test_maximum_absolute_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,73 +28,53 @@ def test_wrong_input_shapes():
m.update((torch.rand(4, 1), torch.rand(4)))


def test_maximum_absolute_error():
a = np.random.randn(4)
b = np.random.randn(4)
c = np.random.randn(4)
d = np.random.randn(4)
ground_truth = np.random.randn(4)

m = MaximumAbsoluteError()

np_ans = -1

m.update((torch.from_numpy(a), torch.from_numpy(ground_truth)))
np_max = np.max(np.abs((a - ground_truth)))
np_ans = np_max if np_max > np_ans else np_ans
assert m.compute() == pytest.approx(np_ans)

m.update((torch.from_numpy(b), torch.from_numpy(ground_truth)))
np_max = np.max(np.abs((b - ground_truth)))
np_ans = np_max if np_max > np_ans else np_ans
assert m.compute() == pytest.approx(np_ans)

m.update((torch.from_numpy(c), torch.from_numpy(ground_truth)))
np_max = np.max(np.abs((c - ground_truth)))
np_ans = np_max if np_max > np_ans else np_ans
assert m.compute() == pytest.approx(np_ans)

m.update((torch.from_numpy(d), torch.from_numpy(ground_truth)))
np_max = np.max(np.abs((d - ground_truth)))
np_ans = np_max if np_max > np_ans else np_ans
assert m.compute() == pytest.approx(np_ans)


def test_integration():
def _test(y_pred, y, batch_size):
def update_fn(engine, batch):
idx = (engine.state.iteration - 1) * batch_size
y_true_batch = np_y[idx : idx + batch_size]
y_pred_batch = np_y_pred[idx : idx + batch_size]
return torch.from_numpy(y_pred_batch), torch.from_numpy(y_true_batch)

engine = Engine(update_fn)

m = MaximumAbsoluteError()
m.attach(engine, "mae")

np_y = y.numpy().ravel()
np_y_pred = y_pred.numpy().ravel()

data = list(range(y_pred.shape[0] // batch_size))
mae = engine.run(data, max_epochs=1).metrics["mae"]

np_max = np.max(np.abs((np_y_pred - np_y)))

assert np_max == pytest.approx(mae)

def get_test_cases():
test_cases = [
(torch.rand(size=(100,)), torch.rand(size=(100,)), 10),
(torch.rand(size=(100, 1)), torch.rand(size=(100, 1)), 20),
]
return test_cases

for _ in range(5):
# check multiple random inputs as random exact occurencies are rare
test_cases = get_test_cases()
for y_pred, y, batch_size in test_cases:
_test(y_pred, y, batch_size)
def test_maximum_absolute_error(available_device):
a = torch.randn(4)
b = torch.randn(4)
c = torch.randn(4)
d = torch.randn(4)
ground_truth = torch.randn(4)

m = MaximumAbsoluteError(device=available_device)
assert m._device == torch.device(available_device)

max_error = -1.0

for pred in [a, b, c, d]:
m.update((pred, ground_truth))
current_max = torch.max(torch.abs(pred - ground_truth)).item()
max_error = max(current_max, max_error)
assert m.compute() == pytest.approx(max_error)


@pytest.mark.parametrize("n_times", range(5))
@pytest.mark.parametrize(
"test_cases",
[
(torch.rand(size=(100,)), torch.rand(size=(100,)), 10),
(torch.rand(size=(100, 1)), torch.rand(size=(100, 1)), 20),
],
)
def test_integration_maximum_absolute_error(n_times, test_cases, available_device):
y_pred, y, batch_size = test_cases

def update_fn(engine, batch):
idx = (engine.state.iteration - 1) * batch_size
y_true_batch = y[idx : idx + batch_size]
y_pred_batch = y_pred[idx : idx + batch_size]
return y_pred_batch, y_true_batch

engine = Engine(update_fn)

m = MaximumAbsoluteError(device=available_device)
assert m._device == torch.device(available_device)
m.attach(engine, "mae")

data = list(range(y_pred.shape[0] // batch_size))
mae = engine.run(data, max_epochs=1).metrics["mae"]

expected = torch.max(torch.abs(y_pred - y)).item()
assert mae == pytest.approx(expected)


def _test_distrib_compute(device):
Expand Down
Loading