From 618d9e9617950b85f34f5468cfc5013096571b68 Mon Sep 17 00:00:00 2001 From: Thomas VINCENT Date: Thu, 4 Apr 2024 09:42:17 +0200 Subject: [PATCH 1/3] 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)) From 1b7338d139e94212cb78d75a9329dc42241a0b90 Mon Sep 17 00:00:00 2001 From: Thomas VINCENT Date: Thu, 4 Apr 2024 11:11:19 +0200 Subject: [PATCH 2/3] Fix numpy.cross 2d vector deprecation warning --- src/silx/gui/plot/items/_arc_roi.py | 9 +++++++-- src/silx/gui/plot/items/scatter.py | 15 +++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/silx/gui/plot/items/_arc_roi.py b/src/silx/gui/plot/items/_arc_roi.py index 658573a7ee..22f967a735 100644 --- a/src/silx/gui/plot/items/_arc_roi.py +++ b/src/silx/gui/plot/items/_arc_roi.py @@ -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 @@ -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: diff --git a/src/silx/gui/plot/items/scatter.py b/src/silx/gui/plot/items/scatter.py index dda3e5ad6e..1b0ba03cb0 100644 --- a/src/silx/gui/plot/items/scatter.py +++ b/src/silx/gui/plot/items/scatter.py @@ -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 @@ -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" @@ -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): From f3ec1e477fb33ef1479431524b8791641896cc0f Mon Sep 17 00:00:00 2001 From: Thomas VINCENT Date: Thu, 4 Apr 2024 11:22:54 +0200 Subject: [PATCH 3/3] Fix deprecation warning Conversion of an array with ndim > 0 to a scalar --- src/silx/opencl/sift/alignment.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/silx/opencl/sift/alignment.py b/src/silx/opencl/sift/alignment.py index a8d9dee819..dbce1c713b 100644 --- a/src/silx/opencl/sift/alignment.py +++ b/src/silx/opencl/sift/alignment.py @@ -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 @@ -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: @@ -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