From 618d9e9617950b85f34f5468cfc5013096571b68 Mon Sep 17 00:00:00 2001 From: Thomas VINCENT Date: Thu, 4 Apr 2024 09:42:17 +0200 Subject: [PATCH] Fix deprecated trapz in numpy2, renamed trapezoid --- examples/plotStats.py | 9 +++++++-- src/silx/gui/plot/CurvesROIWidget.py | 12 +++++++++--- src/silx/gui/plot/test/testCurvesROIWidget.py | 9 +++++++-- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/examples/plotStats.py b/examples/plotStats.py index dd6e4f8d2e..b9cf197681 100644 --- a/examples/plotStats.py +++ b/examples/plotStats.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # /*########################################################################## # -# Copyright (c) 2016-2021 European Synchrotron Radiation Facility +# Copyright (c) 2016-2024 European Synchrotron Radiation Facility # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -51,6 +51,11 @@ import numpy import time +try: + from numpy import trapezoid +except ImportError: # numpy v1 compatibility + from numpy import trapz as trapezoid + class UpdateThread(threading.Thread): """Thread updating the curve of a :class:`~silx.gui.plot.Plot1D` @@ -96,7 +101,7 @@ def __init__(self): def calculate(self, context): xData, yData = context.data - return numpy.trapz(x=xData, y=yData) + return trapezoid(x=xData, y=yData) class COM(StatBase): diff --git a/src/silx/gui/plot/CurvesROIWidget.py b/src/silx/gui/plot/CurvesROIWidget.py index bd47da0b38..7b4469926f 100644 --- a/src/silx/gui/plot/CurvesROIWidget.py +++ b/src/silx/gui/plot/CurvesROIWidget.py @@ -1,6 +1,6 @@ # /*########################################################################## # -# Copyright (c) 2004-2023 European Synchrotron Radiation Facility +# Copyright (c) 2004-2024 European Synchrotron Radiation Facility # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -37,6 +37,12 @@ import sys import functools import numpy + +try: + from numpy import trapezoid +except ImportError: # numpy v1 compatibility + from numpy import trapz as trapezoid + from silx.io import dictdump from silx.utils.weakref import WeakMethodProxy from silx.utils.proxy import docstring @@ -1240,13 +1246,13 @@ def computeRawAndNetArea(self, curve): if x.size == 0: return 0.0, 0.0 - rawArea = numpy.trapz(y, x=x) + rawArea = trapezoid(y, x=x) # to speed up and avoid an intersection calculation we are taking the # closest index to the ROI closestXLeftIndex = (numpy.abs(x - self.getFrom())).argmin() closestXRightIndex = (numpy.abs(x - self.getTo())).argmin() yBackground = y[closestXLeftIndex], y[closestXRightIndex] - background = numpy.trapz(yBackground, x=x) + background = trapezoid(yBackground, x=x) netArea = rawArea - background return rawArea, netArea diff --git a/src/silx/gui/plot/test/testCurvesROIWidget.py b/src/silx/gui/plot/test/testCurvesROIWidget.py index 05acd366b2..c8f2b94707 100644 --- a/src/silx/gui/plot/test/testCurvesROIWidget.py +++ b/src/silx/gui/plot/test/testCurvesROIWidget.py @@ -1,6 +1,6 @@ # /*########################################################################## # -# Copyright (c) 2016-2023 European Synchrotron Radiation Facility +# Copyright (c) 2016-2024 European Synchrotron Radiation Facility # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -32,6 +32,11 @@ import os.path import numpy +try: + from numpy import trapezoid +except ImportError: # numpy v1 compatibility + from numpy import trapz as trapezoid + from silx.gui import qt from silx.gui.plot import items from silx.gui.plot import Plot1D @@ -171,7 +176,7 @@ def testAreaCalculation(self): self.assertEqual( roi_pos.computeRawAndNetArea(posCurve), - (numpy.trapz(y=[10, 20], x=[10, 20]), 0.0), + (trapezoid(y=[10, 20], x=[10, 20]), 0.0), ) self.assertEqual(roi_pos.computeRawAndNetArea(negCurve), (0.0, 0.0)) self.assertEqual(roi_neg.computeRawAndNetArea(posCurve), ((0.0), 0.0))