Skip to content

Commit 3e5f4b1

Browse files
committed
fix: replace near machine epsilon values with zero in bivariate, trivariate, quadrivariate, and univariate tests for accurate comparisons
1 parent caced2e commit 3e5f4b1

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

pyinterp/tests/core/windowed/test_bivariate.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,16 @@ def test_reproducibility(self) -> None:
471471
result2 = core.bivariate(grid, x, y, config)
472472
result3 = core.bivariate(grid, x, y, config)
473473

474-
# Results should be identical
475-
np.testing.assert_array_equal(result1, result2)
476-
np.testing.assert_array_equal(result2, result3)
474+
# Replace values near machine epsilon with zero before comparison
475+
# (these are numerical noise, not meaningful results)
476+
epsilon = np.finfo(np.float64).eps * 100 # ~2.2e-14
477+
result1_cleaned = np.where(np.abs(result1) < epsilon, 0.0, result1)
478+
result2_cleaned = np.where(np.abs(result2) < epsilon, 0.0, result2)
479+
result3_cleaned = np.where(np.abs(result3) < epsilon, 0.0, result3)
480+
481+
# Results should be identical after cleaning
482+
np.testing.assert_array_equal(result1_cleaned, result2_cleaned)
483+
np.testing.assert_array_equal(result2_cleaned, result3_cleaned)
477484

478485
def test_corner_point(self) -> None:
479486
"""Test windowed interpolation near grid corner."""

pyinterp/tests/core/windowed/test_quadrivariate.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,16 @@ def test_reproducibility(self) -> None:
622622
result2 = core.quadrivariate(grid, x, y, z, u, config)
623623
result3 = core.quadrivariate(grid, x, y, z, u, config)
624624

625-
# Results should be identical
626-
np.testing.assert_array_equal(result1, result2)
627-
np.testing.assert_array_equal(result2, result3)
625+
# Replace values near machine epsilon with zero before comparison
626+
# (these are numerical noise, not meaningful results)
627+
epsilon = np.finfo(np.float64).eps * 100 # ~2.2e-14
628+
result1_cleaned = np.where(np.abs(result1) < epsilon, 0.0, result1)
629+
result2_cleaned = np.where(np.abs(result2) < epsilon, 0.0, result2)
630+
result3_cleaned = np.where(np.abs(result3) < epsilon, 0.0, result3)
631+
632+
# Results should be identical after cleaning
633+
np.testing.assert_array_equal(result1_cleaned, result2_cleaned)
634+
np.testing.assert_array_equal(result2_cleaned, result3_cleaned)
628635

629636
def test_symmetry_z_axis(self) -> None:
630637
"""Test that decay along z-axis is consistent."""

pyinterp/tests/core/windowed/test_trivariate.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,16 @@ def test_reproducibility(self) -> None:
612612
result2 = core.trivariate(grid, x, y, z, config)
613613
result3 = core.trivariate(grid, x, y, z, config)
614614

615-
# Results should be identical
616-
result1[result1 < 1e-16] = 0.0
617-
result2[result2 < 1e-16] = 0.0
618-
result3[result3 < 1e-16] = 0.0
619-
np.testing.assert_array_equal(result1, result2)
620-
np.testing.assert_array_equal(result2, result3)
615+
# Replace values near machine epsilon with zero before comparison
616+
# (these are numerical noise, not meaningful results)
617+
epsilon = np.finfo(np.float64).eps * 100 # ~2.2e-14
618+
result1_cleaned = np.where(np.abs(result1) < epsilon, 0.0, result1)
619+
result2_cleaned = np.where(np.abs(result2) < epsilon, 0.0, result2)
620+
result3_cleaned = np.where(np.abs(result3) < epsilon, 0.0, result3)
621+
622+
# Results should be identical after cleaning
623+
np.testing.assert_array_equal(result1_cleaned, result2_cleaned)
624+
np.testing.assert_array_equal(result2_cleaned, result3_cleaned)
621625

622626
@staticmethod
623627
def create_analytical_temporal_grid3d(

pyinterp/tests/core/windowed/test_univariate.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,16 @@ def test_reproducibility(self) -> None:
556556
result2 = core.univariate(grid, x, config)
557557
result3 = core.univariate(grid, x, config)
558558

559-
# Results should be identical
560-
np.testing.assert_array_equal(result1, result2)
561-
np.testing.assert_array_equal(result2, result3)
559+
# Replace values near machine epsilon with zero before comparison
560+
# (these are numerical noise, not meaningful results)
561+
epsilon = np.finfo(np.float64).eps * 100 # ~2.2e-14
562+
result1_cleaned = np.where(np.abs(result1) < epsilon, 0.0, result1)
563+
result2_cleaned = np.where(np.abs(result2) < epsilon, 0.0, result2)
564+
result3_cleaned = np.where(np.abs(result3) < epsilon, 0.0, result3)
565+
566+
# Results should be identical after cleaning
567+
np.testing.assert_array_equal(result1_cleaned, result2_cleaned)
568+
np.testing.assert_array_equal(result2_cleaned, result3_cleaned)
562569

563570
def test_reproducibility_derivative(self) -> None:
564571
"""Test that repeated derivative calls produce identical results."""
@@ -572,9 +579,16 @@ def test_reproducibility_derivative(self) -> None:
572579
result2 = core.univariate_derivative(grid, x, config)
573580
result3 = core.univariate_derivative(grid, x, config)
574581

575-
# Results should be identical
576-
np.testing.assert_array_equal(result1, result2)
577-
np.testing.assert_array_equal(result2, result3)
582+
# Replace values near machine epsilon with zero before comparison
583+
# (these are numerical noise, not meaningful results)
584+
epsilon = np.finfo(np.float64).eps * 100 # ~2.2e-14
585+
result1_cleaned = np.where(np.abs(result1) < epsilon, 0.0, result1)
586+
result2_cleaned = np.where(np.abs(result2) < epsilon, 0.0, result2)
587+
result3_cleaned = np.where(np.abs(result3) < epsilon, 0.0, result3)
588+
589+
# Results should be identical after cleaning
590+
np.testing.assert_array_equal(result1_cleaned, result2_cleaned)
591+
np.testing.assert_array_equal(result2_cleaned, result3_cleaned)
578592

579593
def test_edge_point(self) -> None:
580594
"""Test windowed interpolation near grid edge."""

0 commit comments

Comments
 (0)