Skip to content

Commit 1a8b9cb

Browse files
committed
Add atol arg to all calls to np.testing.assert_allclose (defaults to 0 => bogus errors for values close to 0)
1 parent ca82e11 commit 1a8b9cb

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
lines changed

tests/influence/torch/test_influence_model.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,19 @@ def test_influences_lissa(
527527
influence_factors, x_train, y_train, mode=test_case.mode
528528
)
529529

530+
atol = 1e-5
531+
rtol = 1e-4
530532
assert torch.allclose(
531-
influences_from_factors, approx_influences, atol=1e-5, rtol=1e-4
533+
influences_from_factors, approx_influences, atol=atol, rtol=rtol
532534
)
533535

534536
approx_influences = approx_influences.cpu().numpy()
535537

536538
assert not np.any(np.isnan(approx_influences))
537539

538-
np.testing.assert_allclose(approx_influences, direct_influences, rtol=1e-1)
540+
np.testing.assert_allclose(
541+
approx_influences, direct_influences, atol=atol, rtol=rtol
542+
)
539543

540544
if test_case.mode == InfluenceMode.Up:
541545
assert approx_influences.shape == (
@@ -553,7 +557,9 @@ def test_influences_lissa(
553557
# check that influences are not all constant
554558
assert not np.all(approx_influences == approx_influences.item(0))
555559

556-
np.testing.assert_allclose(approx_influences, direct_influences, rtol=1e-1)
560+
np.testing.assert_allclose(
561+
approx_influences, direct_influences, atol=atol, rtol=rtol
562+
)
557563

558564

559565
@pytest.mark.parametrize(
@@ -674,6 +680,9 @@ def test_influences_ekfac(
674680
direct_sym_influences,
675681
device: torch.device,
676682
):
683+
atol = 1e-6
684+
rtol = 1e-4
685+
677686
model, loss, x_train, y_train, x_test, y_test = model_and_data
678687

679688
train_dataloader = DataLoader(
@@ -731,8 +740,12 @@ def test_influences_ekfac(
731740
.numpy()
732741
)
733742

734-
np.testing.assert_allclose(ekfac_influence_values, influence_from_factors)
735-
np.testing.assert_allclose(ekfac_influence_values, accumulated_inf_by_layer)
743+
np.testing.assert_allclose(
744+
ekfac_influence_values, influence_from_factors, atol=atol, rtol=rtol
745+
)
746+
np.testing.assert_allclose(
747+
ekfac_influence_values, accumulated_inf_by_layer, atol=atol, rtol=rtol
748+
)
736749
check_influence_correlations(
737750
direct_influences.numpy(), ekfac_influence_values, threshold=0.94
738751
)
@@ -832,7 +845,7 @@ def test_influences_cg(
832845
.numpy()
833846
)
834847
np.testing.assert_allclose(
835-
single_influence, direct_factors[0], atol=1e-6, rtol=1e-4
848+
single_influence[0], direct_factors[0], atol=1e-6, rtol=1e-4
836849
)
837850

838851

tests/utils/test_numeric.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def test_random_matrix_with_condition_number(n, cond, exception):
207207
else:
208208
mat = random_matrix_with_condition_number(n, cond)
209209
(
210-
np.testing.assert_allclose(np.linalg.cond(mat), cond),
210+
np.testing.assert_allclose(np.linalg.cond(mat), cond, atol=1e-5),
211211
"Condition number does not match",
212212
)
213213
assert np.array_equal(mat, mat.T), "Matrix is not symmetric"
@@ -272,8 +272,8 @@ def test_running_moments():
272272

273273
true_means = [np.mean(vv) for vv in values]
274274
true_variances = [np.var(vv) for vv in values]
275-
np.testing.assert_allclose(means, true_means)
276-
np.testing.assert_allclose(variances, true_variances)
275+
np.testing.assert_allclose(means, true_means, atol=1e-5)
276+
np.testing.assert_allclose(variances, true_variances, atol=1e-5)
277277

278278

279279
def test_running_moment_initialization():

tests/valuation/samplers/test_sampler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,14 @@ def test_antithetic_owen_sampler():
626626
# check that the sample sizes are close to expected sizes
627627
sizes = np.array([len(sample.subset) for sample in samples])
628628
avg_sizes = sizes.reshape(n_outer, -1).mean(axis=1)
629-
np.testing.assert_allclose(avg_sizes, len(indices) // 2, rtol=0.01)
629+
np.testing.assert_allclose(avg_sizes, len(indices) // 2, rtol=0.01, atol=1e-5)
630630

631631

632632
def _check_sample_sizes(samples, n_samples_outer, n_indices, probs):
633633
sizes = np.array([len(sample.subset) for sample in samples])
634634
avg_sizes = sizes.reshape(n_samples_outer, -1).mean(axis=1)
635635
expected_sizes = probs * n_indices # mean of Binomial(n_indices, probs)
636-
np.testing.assert_allclose(avg_sizes, expected_sizes, rtol=0.01)
636+
np.testing.assert_allclose(avg_sizes, expected_sizes, rtol=0.01, atol=1e-5)
637637

638638

639639
@pytest.mark.flaky(reruns=1)

tests/valuation/test_result.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def test_from_random_creation(size: int, total: float | None):
265265
assert result.status == Status.Converged
266266
assert result.algorithm == "random"
267267
if total is not None:
268-
np.testing.assert_allclose(np.sum(result.values), total)
268+
np.testing.assert_allclose(np.sum(result.values), total, atol=1e-5)
269269

270270

271271
def test_from_random_creation_errors():
@@ -300,8 +300,10 @@ def test_adding_random():
300300
true_means = values.mean(axis=1)
301301
true_variances = values.var(axis=1)
302302

303-
np.testing.assert_allclose(true_means[result.indices], result.values)
304-
np.testing.assert_allclose(true_variances[result.indices], result.variances)
303+
np.testing.assert_allclose(true_means[result.indices], result.values, atol=1e-5)
304+
np.testing.assert_allclose(
305+
true_variances[result.indices], result.variances, atol=1e-5
306+
)
305307

306308

307309
@pytest.mark.parametrize(
@@ -378,7 +380,7 @@ def test_adding_different_indices(
378380
v3 = v1 + v2
379381

380382
np.testing.assert_allclose(v3.indices, np.array(expected_indices))
381-
np.testing.assert_allclose(v3.values, np.array(expected_values))
383+
np.testing.assert_allclose(v3.values, np.array(expected_values), atol=1e-5)
382384
assert np.all(v3.names == expected_names)
383385

384386

tests/value/shapley/test_classwise.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,19 @@ def test_classwise_scorer_utility(dataset_left_right_margins):
209209
in_cls_acc_0, out_of_cls_acc_0 = scorer.estimate_in_class_and_out_of_class_score(
210210
model, x, y
211211
)
212-
np.testing.assert_allclose(in_cls_acc_0, target_in_cls_acc_0)
213-
np.testing.assert_allclose(out_of_cls_acc_0, target_out_of_cls_acc_0)
212+
np.testing.assert_allclose(in_cls_acc_0, target_in_cls_acc_0, atol=1e-5)
213+
np.testing.assert_allclose(out_of_cls_acc_0, target_out_of_cls_acc_0, atol=1e-5)
214214

215215
value = scorer(model, x, y)
216-
np.testing.assert_allclose(value, in_cls_acc_0 * np.exp(out_of_cls_acc_0))
216+
np.testing.assert_allclose(
217+
value, in_cls_acc_0 * np.exp(out_of_cls_acc_0), atol=1e-5
218+
)
217219

218220
scorer.label = 1
219221
value = scorer(model, x, y)
220-
np.testing.assert_allclose(value, out_of_cls_acc_0 * np.exp(in_cls_acc_0))
222+
np.testing.assert_allclose(
223+
value, out_of_cls_acc_0 * np.exp(in_cls_acc_0), atol=1e-5
224+
)
221225

222226

223227
@pytest.mark.parametrize("n_element, left_margin, right_margin", [(101, 0.3, 0.4)])
@@ -329,8 +333,8 @@ def test_classwise_scorer_accuracies_left_right_margins(dataset_left_right_margi
329333
in_cls_acc_0, out_of_cls_acc_0 = scorer.estimate_in_class_and_out_of_class_score(
330334
model, x, y
331335
)
332-
np.testing.assert_allclose(in_cls_acc_0, target_in_cls_acc_0)
333-
np.testing.assert_allclose(out_of_cls_acc_0, target_out_of_cls_acc_0)
336+
np.testing.assert_allclose(in_cls_acc_0, target_in_cls_acc_0, atol=1e-5)
337+
np.testing.assert_allclose(out_of_cls_acc_0, target_out_of_cls_acc_0, atol=1e-5)
334338

335339

336340
def test_closed_form_linear_classifier(

tests/value/test_results.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,10 @@ def test_adding_random():
296296
true_means = values.mean(axis=1)
297297
true_variances = values.var(axis=1)
298298

299-
np.testing.assert_allclose(true_means[result.indices], result.values)
300-
np.testing.assert_allclose(true_variances[result.indices], result.variances)
299+
np.testing.assert_allclose(true_means[result.indices], result.values, atol=1e-5)
300+
np.testing.assert_allclose(
301+
true_variances[result.indices], result.variances, atol=1e-5
302+
)
301303

302304

303305
@pytest.mark.parametrize(
@@ -374,7 +376,7 @@ def test_adding_different_indices(
374376
v3 = v1 + v2
375377

376378
np.testing.assert_allclose(v3.indices, np.array(expected_indices))
377-
np.testing.assert_allclose(v3.values, np.array(expected_values))
379+
np.testing.assert_allclose(v3.values, np.array(expected_values), atol=1e-5)
378380
assert np.all(v3.names == expected_names)
379381

380382

0 commit comments

Comments
 (0)