Skip to content

Commit

Permalink
Merge pull request #4100 from t20100/fix-deprecation
Browse files Browse the repository at this point in the history
Dependencies: Fixed numpy v2 deprecation warnings
  • Loading branch information
payno committed Apr 18, 2024
2 parents e3b2acb + f3ec1e4 commit 4e1bd4e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 26 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/items/_arc_roi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# /*##########################################################################
#
# Copyright (c) 2018-2023 European Synchrotron Radiation Facility
# Copyright (c) 2018-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 @@ -632,7 +632,12 @@ def _createGeometryFromControlPoints(self, start, mid, end, weight, closed=None)
center, start, end, radius, weight, startAngle, endAngle
)

elif numpy.linalg.norm(numpy.cross(mid - start, end - start)) < 1e-5:
elif (
numpy.linalg.norm(
numpy.cross(numpy.append(mid - start, 0), numpy.append(end - start, 0))
)
< 1e-5
):
# Degenerated arc, it's a rectangle
return _ArcGeometry.createRect(start, end, weight)
else:
Expand Down
15 changes: 5 additions & 10 deletions src/silx/gui/plot/items/scatter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# /*##########################################################################
#
# Copyright (c) 2017-2023 European Synchrotron Radiation Facility
# Copyright (c) 2017-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 @@ -713,7 +713,7 @@ def _addBackendRenderer(self, backend):
shape = shape[0], int(numpy.ceil(nbpoints / shape[0]))

if shape[0] < 2 or shape[1] < 2: # Single line, at least 2 points
points = numpy.ones((2, nbpoints, 2), dtype=numpy.float64)
points = numpy.zeros((2, nbpoints, 3), dtype=numpy.float64)
# Use row/column major depending on shape, not on info value
gridOrder = "row" if shape[0] == 1 else "column"

Expand All @@ -727,19 +727,14 @@ def _addBackendRenderer(self, backend):
# Add a second line that will be clipped in the end
points[1, :-1] = (
points[0, :-1]
+ numpy.cross(points[0, 1:] - points[0, :-1], (0.0, 0.0, 1.0))[
:, :2
]
+ numpy.cross(points[0, 1:] - points[0, :-1], (0.0, 0.0, 1.0))
)
points[1, -1] = (
points[0, -1]
+ numpy.cross(points[0, -1] - points[0, -2], (0.0, 0.0, 1.0))[
:2
]
+ numpy.cross(points[0, -1] - points[0, -2], (0.0, 0.0, 1.0))
)

points.shape = 2, nbpoints, 2 # Use same shape for both orders
coords, indices = _quadrilateral_grid_as_triangles(points)
points = points[:, :, :2]

elif gridOrder == "row": # row-major order
if nbpoints != numpy.prod(shape):
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
14 changes: 7 additions & 7 deletions src/silx/opencl/sift/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Project: Sift implementation in Python + OpenCL
# https://github.com/silx-kit/silx
#
# Copyright (C) 2013-2018 European Synchrotron Radiation Facility, Grenoble, France
# Copyright (C) 2013-2024 European Synchrotron Radiation Facility, Grenoble, France
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -324,8 +324,8 @@ def align(
[transform_matrix[5], transform_matrix[2]], dtype=numpy.float32
)
matrix = numpy.empty((2, 2), dtype=numpy.float32)
matrix[0, 0], matrix[0, 1] = transform_matrix[4], transform_matrix[3]
matrix[1, 0], matrix[1, 1] = transform_matrix[1], transform_matrix[0]
matrix[0, 0], matrix[0, 1] = transform_matrix[4, 0], transform_matrix[3, 0]
matrix[1, 0], matrix[1, 1] = transform_matrix[1, 0], transform_matrix[0, 0]
if double_check and (
len_match >= 3 * 6
): # and abs(matrix - numpy.identity(2)).max() > 0.1:
Expand All @@ -348,12 +348,12 @@ def align(
)
matrix = numpy.empty((2, 2), dtype=numpy.float32)
matrix[0, 0], matrix[0, 1] = (
transform_matrix[4],
transform_matrix[3],
transform_matrix[4, 0],
transform_matrix[3, 0],
)
matrix[1, 0], matrix[1, 1] = (
transform_matrix[1],
transform_matrix[0],
transform_matrix[1, 0],
transform_matrix[0, 0],
)
if relative: # update stable part to perform a relative alignment
self.ref_kp = kp
Expand Down

0 comments on commit 4e1bd4e

Please sign in to comment.