From acd2b0cff6ff830a43e0258764928aaea245a310 Mon Sep 17 00:00:00 2001 From: Neil Flood Date: Tue, 18 Jun 2024 17:58:49 +1000 Subject: [PATCH 1/2] Cope with numpy-2 change in comparing float32 and float64 values --- rios/riostests/testvector.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rios/riostests/testvector.py b/rios/riostests/testvector.py index d69637c2..a2437c85 100644 --- a/rios/riostests/testvector.py +++ b/rios/riostests/testvector.py @@ -47,6 +47,10 @@ def run(): meanVal_rios = calcMeanWithRiosApplier(imgfile, vecfile) ok = True + # Since numpy-2, these have different precisions. The numpy-calculated + # value is numpy.float64, while the rios-calculated one is numpy.float32. + # So, cast the longer one to match the shorter. Sigh..... + meanVal_numpy = numpy.asarray(meanVal_numpy, dtype=meanVal_rios.dtype) if meanVal_numpy == meanVal_rios: riostestutils.report(TESTNAME, "Passed") else: From b986efa4918f83ad5ca0092bce0b33a6c35bcbfb Mon Sep 17 00:00:00 2001 From: Neil Flood Date: Tue, 18 Jun 2024 17:59:54 +1000 Subject: [PATCH 2/2] Cope with numpy-2 changes in how repr() handles numpy scalar values --- rios/calcstats.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rios/calcstats.py b/rios/calcstats.py index 501b5ccb..6db7e9ad 100644 --- a/rios/calcstats.py +++ b/rios/calcstats.py @@ -294,7 +294,7 @@ def addStatistics(ds, progress, ignore=None, approx_ok=False): modebin = numpy.argmax(hist) modeval = modebin * histstep + histmin if band.DataType == gdal.GDT_Float32 or band.DataType == gdal.GDT_Float64: - tmpmeta["STATISTICS_MODE"] = repr(modeval) + tmpmeta["STATISTICS_MODE"] = repr(float(modeval)) else: tmpmeta["STATISTICS_MODE"] = repr(int(round(modeval))) @@ -315,11 +315,11 @@ def addStatistics(ds, progress, ignore=None, approx_ok=False): else: # Use GDAL's original metadata interface, for drivers which # don't support the more modern approach - tmpmeta["STATISTICS_HISTOBINVALUES"] = '|'.join(map(repr, hist)) + '|' + tmpmeta["STATISTICS_HISTOBINVALUES"] = '|'.join(map(str, hist)) + '|' tmpmeta["STATISTICS_HISTOMIN"] = repr(histmin) tmpmeta["STATISTICS_HISTOMAX"] = repr(histmax) - tmpmeta["STATISTICS_HISTONUMBINS"] = repr(histnbins) + tmpmeta["STATISTICS_HISTONUMBINS"] = int(histnbins) # estimate the median - bin with the middle number middlenum = hist.sum() / 2 @@ -327,7 +327,7 @@ def addStatistics(ds, progress, ignore=None, approx_ok=False): medianbin = gtmiddle.nonzero()[0][0] medianval = medianbin * histstep + histmin if band.DataType == gdal.GDT_Float32 or band.DataType == gdal.GDT_Float64: - tmpmeta["STATISTICS_MEDIAN"] = repr(medianval) + tmpmeta["STATISTICS_MEDIAN"] = repr(float(medianval)) else: tmpmeta["STATISTICS_MEDIAN"] = repr(int(round(medianval)))