Skip to content

Commit

Permalink
Fix deprecated trapz in numpy2, renamed trapezoid
Browse files Browse the repository at this point in the history
  • Loading branch information
t20100 committed Apr 17, 2024
1 parent 1675965 commit 618d9e9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
9 changes: 7 additions & 2 deletions examples/plotStats.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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):
Expand Down
12 changes: 9 additions & 3 deletions src/silx/gui/plot/CurvesROIWidget.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions src/silx/gui/plot/test/testCurvesROIWidget.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 618d9e9

Please sign in to comment.