Skip to content

Commit

Permalink
Esured compatibility of np.trapz for newer versions
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericjs committed Dec 9, 2024
1 parent 2cdb836 commit 326a37d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
14 changes: 7 additions & 7 deletions surfalize/abbottfirestone.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import matplotlib.pyplot as plt

from .mathutils import argclosest, interp1d
from .mathutils import argclosest, interp1d, trapezoid
from .cache import CachedInstance, cache

class AbbottFirestoneCurve(CachedInstance):
Expand Down Expand Up @@ -176,7 +176,7 @@ def Spk(self):

# Area enclosed above yupper between y-axis (at x=0) and abbott-firestone curve
idx = argclosest(self._yupper, self._height)
A1 = np.abs(np.trapz(self._material_ratio[:idx], x=self._height[:idx]))
A1 = np.abs(trapezoid(self._material_ratio[:idx], x=self._height[:idx]))
Spk = 2 * A1 / self.Smr1()
return Spk

Expand All @@ -191,29 +191,29 @@ def Svk(self):
"""
# Area enclosed below ylower between y-axis (at x=100) and abbott-firestone curve
idx = argclosest(self._ylower, self._height)
A2 = np.abs(np.trapz(100 - self._material_ratio[idx:], x=self._height[idx:]))
A2 = np.abs(trapezoid(100 - self._material_ratio[idx:], x=self._height[idx:]))
Svk = 2 * A2 / (100 - self.Smr2())
return Svk

@cache
def Vmp(self, p=10):
idx = argclosest(self.Smc(p), self._height)
return np.abs(np.trapz(self._material_ratio[:idx], x=self._height[:idx]) / 100)
return np.abs(trapezoid(self._material_ratio[:idx], x=self._height[:idx]) / 100)

@cache
def Vmc(self, p=10, q=80):
idx = argclosest(self.Smc(q), self._height)
return np.abs(np.trapz(self._material_ratio[:idx], x=self._height[:idx])) / 100 - self.Vmp(p)
return np.abs(trapezoid(self._material_ratio[:idx], x=self._height[:idx])) / 100 - self.Vmp(p)

@cache
def Vvv(self, q=80):
idx = argclosest(self.Smc(80), self._height)
return np.abs(np.trapz(100 - self._material_ratio[idx:], x=self._height[idx:])) / 100
return np.abs(trapezoid(100 - self._material_ratio[idx:], x=self._height[idx:])) / 100

@cache
def Vvc(self, p=10, q=80):
idx = argclosest(self.Smc(10), self._height)
return np.abs(np.trapz(100 - self._material_ratio[idx:], x=self._height[idx:])) / 100 - self.Vvv(q)
return np.abs(trapezoid(100 - self._material_ratio[idx:], x=self._height[idx:])) / 100 - self.Vvv(q)

def plot(self, nbars=20, ax=None):
if ax is None:
Expand Down
5 changes: 5 additions & 0 deletions surfalize/mathutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from scipy.signal import find_peaks
from .exceptions import FittingError

# Ensure compatibility with differnt numpy versions
if int(np.__version__.split('.')[0]) < 2:
trapezoid = np.trapz
else:
trapezoid = np.trapezoid

def interp1d(xdata, ydata, assume_sorted=False):
"""
Expand Down
4 changes: 2 additions & 2 deletions surfalize/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .file import load_file, write_file
from .utils import is_list_like, register_returnlabels
from .cache import CachedInstance, cache
from .mathutils import Sinusoid, argclosest, interp1d
from .mathutils import Sinusoid, argclosest, trapezoid
from .autocorrelation import AutocorrelationFunction
from .abbottfirestone import AbbottFirestoneCurve
from .profile import Profile
Expand Down Expand Up @@ -1648,7 +1648,7 @@ def homogeneity(self, parameters: tuple[str] = ('Sa', 'Sku', 'Sdr'), period: flo
lorenz[1:] = np.cumsum(results[i]) / np.sum(results[i])
x, step = np.linspace(0, 1, lorenz.size, retstep=True)
y = lorenz.min() + (lorenz.max() - lorenz.min()) * x
B = np.trapz(lorenz, dx=step)
B = trapezoid(lorenz, dx=step)
A = 0.5 - B
gini = A / 0.5
h.append(1 - gini)
Expand Down

0 comments on commit 326a37d

Please sign in to comment.