From b633adc30952479388bf5ec167eda2d52c0e81b1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 3 Oct 2026 23:30:24 +0000 Subject: [PATCH] i.oif: Sort NaN results last and fix pygrass doctest for numpy 2 Sorting (oif, bands) tuples that contain NaN has no well-defined order, so the output order of i.oif depended on the sort algorithm. Place NaN results last, in input order, and update the test to expect that. The get_raster_for_points doctest in grass.pygrass.utils expected `1`, but numpy 2 prints `np.int32(1)`. Match it with the ellipsis directive already present. Both showed up when running the full Ubuntu test configuration with the latest Python and numpy. Written with the help of Claude Code. --- python/grass/pygrass/utils.py | 4 ++-- scripts/i.oif/i.oif.py | 6 +++++- scripts/i.oif/testsuite/test_i_oif.py | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/python/grass/pygrass/utils.py b/python/grass/pygrass/utils.py index 344e4237ea4..b6cdfd47c89 100644 --- a/python/grass/pygrass/utils.py +++ b/python/grass/pygrass/utils.py @@ -278,9 +278,9 @@ def get_raster_for_points(poi_vector, raster, column=None, region=None): >>> l = get_raster_for_points(vect, ele, region=region) >>> l[0] # doctest: +ELLIPSIS - (1, 10.0, 6.0, 1) + (1, 10.0, 6.0, ...1...) >>> l[1] # doctest: +ELLIPSIS - (2, 12.0, 6.0, 1) + (2, 12.0, 6.0, ...1...) Add a new column and sample again diff --git a/scripts/i.oif/i.oif.py b/scripts/i.oif/i.oif.py index d158454bbe0..f323fcd77c9 100755 --- a/scripts/i.oif/i.oif.py +++ b/scripts/i.oif/i.oif.py @@ -130,7 +130,11 @@ def main(): oif = [] for p in perms(bands): oif.append((oifcalc(stddev, correlation, *p), p)) - oif.sort(reverse=True) + # NaN does not compare, so sorting the raw tuples gives an order which + # depends on the sort algorithm. Place NaN last, in input order. + oif.sort( + key=lambda item: (item[0] != item[0], -item[0] if item[0] == item[0] else 0) + ) grass.verbose( _("The Optimum Index Factor analysis result (best combination shown first):") diff --git a/scripts/i.oif/testsuite/test_i_oif.py b/scripts/i.oif/testsuite/test_i_oif.py index 313f96853bb..a0930f7e9cf 100644 --- a/scripts/i.oif/testsuite/test_i_oif.py +++ b/scripts/i.oif/testsuite/test_i_oif.py @@ -156,11 +156,11 @@ def test_constant_raster_yields_nan(self): expected_output = ( f"{self.band1}, {self.band2}, {self.band4}: 3.5301\n" f"{self.band1}, {self.band2}, {self.band3}: 2.6992\n" - f"{self.band1}, {self.band2}, {self.const_band}: nan\n" + f"{self.band2}, {self.band3}, {self.band4}: 2.4723\n" f"{self.band1}, {self.band3}, {self.band4}: 2.0387\n" + f"{self.band1}, {self.band2}, {self.const_band}: nan\n" f"{self.band1}, {self.band3}, {self.const_band}: nan\n" f"{self.band1}, {self.band4}, {self.const_band}: nan\n" - f"{self.band2}, {self.band3}, {self.band4}: 2.4723\n" f"{self.band2}, {self.band3}, {self.const_band}: nan\n" f"{self.band2}, {self.band4}, {self.const_band}: nan\n" f"{self.band3}, {self.band4}, {self.const_band}: nan\n"