Skip to content

Commit

Permalink
Numpy2 fixes (#90)
Browse files Browse the repository at this point in the history
* Cope with numpy-2 change in comparing float32 and float64 values

* Cope with numpy-2 changes in how repr() handles numpy scalar values
  • Loading branch information
neilflood authored Jun 18, 2024
1 parent 7135564 commit aa516c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions rios/calcstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand All @@ -315,19 +315,19 @@ 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
gtmiddle = hist.cumsum() >= middlenum
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)))

Expand Down
4 changes: 4 additions & 0 deletions rios/riostests/testvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit aa516c8

Please sign in to comment.