From 385093e3f2f5066b7f4e5343cef6e6fcf74e819c Mon Sep 17 00:00:00 2001 From: Hugo Karas Date: Tue, 26 Nov 2024 11:08:10 +0100 Subject: [PATCH] Rename trapz to trapezoid --- deerlab/bg_models.py | 4 ++-- deerlab/classes.py | 8 ++++---- deerlab/dd_models.py | 4 ++-- deerlab/dipolarmodel.py | 4 ++-- deerlab/diststats.py | 8 ++++---- deerlab/solvers.py | 2 +- examples/advanced/ex_forcefield_fit.py | 2 +- examples/advanced/ex_global_twostates_parametric.py | 2 +- examples/advanced/ex_multigauss_fitting_4pdeer.py | 4 ++-- examples/advanced/ex_pseudotitration_parameter_free.py | 2 +- examples/basic/ex_fitting_4pdeer_gauss.py | 2 +- examples/basic/ex_restraints_4pdeer.py | 6 +++--- test/test_ddmodels.py | 2 +- test/test_dipolarmodel.py | 2 +- test/test_model_penalty.py | 4 ++-- test/test_snlls.py | 4 ++-- 16 files changed, 30 insertions(+), 30 deletions(-) diff --git a/deerlab/bg_models.py b/deerlab/bg_models.py index 037f61dcd..c0a11c8f4 100644 --- a/deerlab/bg_models.py +++ b/deerlab/bg_models.py @@ -188,7 +188,7 @@ def _hom3dex(t,conc,rex,lam): # Averaging integral z = np.linspace(0,1,1000)[np.newaxis,:] Dt = D*t[:,np.newaxis]*1e-6 - Is = 4*np.pi/3*np.trapz(Dt*(1-3*z**2)*sici((Dt*(1-3*z**2))/((rex*1e-9)**3))[0],z,axis=1) + Is = 4*np.pi/3*np.trapezoid(Dt*(1-3*z**2)*sici((Dt*(1-3*z**2))/((rex*1e-9)**3))[0],z,axis=1) # Background function C_k = -Vex + Is + np.squeeze(Vex*(dipolarkernel(t,rex,integralop=False))) @@ -242,7 +242,7 @@ def _hom3dex_phase(t,conc,rex,lam): ξ = 8*pi**2/9/np.sqrt(3)*(np.sqrt(3)+np.log(2-np.sqrt(3)))/np.pi*D z = np.linspace(0,1,1000)[np.newaxis,:] Dt = D*t[:,np.newaxis]*1e-6 - Ic = -ξ*(t*1e-6) + 4*np.pi/3*np.trapz(Dt*(1-3*z**2)*sici((Dt*np.abs(1-3*z**2))/((rex*1e-9)**3))[1],z,axis=1) + Ic = -ξ*(t*1e-6) + 4*np.pi/3*np.trapezoid(Dt*(1-3*z**2)*sici((Dt*np.abs(1-3*z**2))/((rex*1e-9)**3))[1],z,axis=1) # Background function C_k = - Ic - np.squeeze(Vex*(dipolarkernel(t,rex,integralop=False,complex=True)).imag) diff --git a/deerlab/classes.py b/deerlab/classes.py index 39063cbf4..7c16d29ce 100644 --- a/deerlab/classes.py +++ b/deerlab/classes.py @@ -151,8 +151,8 @@ def __init__(self,uqtype,data=None,covmat=None,lb=None,ub=None,threshold=None,pr elif uqtype == 'profile': xs = [self.pardist(n)[0] for n in range(nParam)] pardists = [self.pardist(n)[1] for n in range(nParam)] - means = [np.trapz(pardist*x,x) for x,pardist in zip(xs,pardists)] - std = [np.sqrt(np.trapz(pardist*(x-mean)**2,x)) for x,pardist,mean in zip(xs,pardists,means)] + means = [np.trapezoid(pardist*x,x) for x,pardist in zip(xs,pardists)] + std = [np.sqrt(np.trapezoid(pardist*(x-mean)**2,x)) for x,pardist,mean in zip(xs,pardists,means)] self.mean = means self.median = self.percentile(50) self.std = std @@ -342,8 +342,8 @@ def pardist(self,n=0): # Ensure normalization of the probability density function (if not a Dirac delta function) if not isdelta: - if np.trapz(pdf, x)!=0: - pdf = pdf/np.trapz(pdf, x) + if np.trapezoid(pdf, x)!=0: + pdf = pdf/np.trapezoid(pdf, x) return x, pdf #-------------------------------------------------------------------------------- diff --git a/deerlab/dd_models.py b/deerlab/dd_models.py index 1c4d6d3ff..2293f0905 100644 --- a/deerlab/dd_models.py +++ b/deerlab/dd_models.py @@ -99,7 +99,7 @@ def docstr_example(fcnstr): # ================================================================= def _normalize(r,P): if not all(P==0): - P = P/np.trapz(P,r) + P = P/np.trapezoid(P,r) return P # ================================================================= @@ -129,7 +129,7 @@ def _multirice3dfun(r,nu,sig): P[P<0] = 0 # Normalization - P = np.squeeze(P)/np.sum([np.trapz(c,np.squeeze(r)) for c in P.T]) + P = np.squeeze(P)/np.sum([np.trapezoid(c,np.squeeze(r)) for c in P.T]) return P # ================================================================= diff --git a/deerlab/dipolarmodel.py b/deerlab/dipolarmodel.py index 80035e5a6..1f3f0a79e 100644 --- a/deerlab/dipolarmodel.py +++ b/deerlab/dipolarmodel.py @@ -642,8 +642,8 @@ def dipolarpenalty(Pmodel, r, type, selection=None): def compactness_penalty(*args): P = Pmodel(*[r]*Nconstants,*args) if not np.all(P==0): - P = P/np.trapz(P,r) - return np.sqrt(P*(r - np.trapz(P*r,r))**2*np.mean(np.diff(r))) + P = P/np.trapezoid(P,r) + return np.sqrt(P*(r - np.trapezoid(P*r,r))**2*np.mean(np.diff(r))) # Add the penalty to the Pmodel penalty = Penalty(compactness_penalty,selection, signature = Pmodel._parameter_list(), diff --git a/deerlab/diststats.py b/deerlab/diststats.py index 22d5d0229..23ca2744c 100644 --- a/deerlab/diststats.py +++ b/deerlab/diststats.py @@ -103,7 +103,7 @@ def analyze_rmode(V): # Auxiliary functions # ------------------- - int = np.trapz(P,r) if not np.all(P==0) else 1 + int = np.trapezoid(P,r) if not np.all(P==0) else 1 def normalize(P): return P/int # Percentile function @@ -114,7 +114,7 @@ def pctile(r,P,p): return rpctile # Expectation operator function def E(x,P,r): - return np.trapz(x*normalize(P),r) + return np.trapezoid(x*normalize(P),r) # Location estimators # ------------------- @@ -125,7 +125,7 @@ def E(x,P,r): # Interquartile mean def iqmfcn(P): IQrange = (r>pctile(r,P,25)) & (r1: label=None plt.plot(r,2*n + Pfit,'k',label='Total contribution' if n<1 else None) plt.fill(r,2*n + xA*results.P_1,color=green,alpha=0.5,label='State A (natural)' if n<1 else None) diff --git a/examples/basic/ex_fitting_4pdeer_gauss.py b/examples/basic/ex_fitting_4pdeer_gauss.py index 225827c22..e110613e9 100644 --- a/examples/basic/ex_fitting_4pdeer_gauss.py +++ b/examples/basic/ex_fitting_4pdeer_gauss.py @@ -52,7 +52,7 @@ # Extract fitted distance distribution Pfit = results.evaluate(Pmodel,r) -scale = np.trapz(Pfit,r) +scale = np.trapezoid(Pfit,r) Puncert = results.propagate(Pmodel,r,lb=np.zeros_like(r)) Pfit = Pfit/scale Pci95 = Puncert.ci(95)/scale diff --git a/examples/basic/ex_restraints_4pdeer.py b/examples/basic/ex_restraints_4pdeer.py index d58675f37..d3c71a103 100644 --- a/examples/basic/ex_restraints_4pdeer.py +++ b/examples/basic/ex_restraints_4pdeer.py @@ -70,9 +70,9 @@ # Plot distribution and confidence bands violet = '#4550e6' -Pci95 = fit.PUncert.ci(95)/np.trapz(fit.P,r) -Pci50 = fit.PUncert.ci(50)/np.trapz(fit.P,r) -plt.plot(r,fit.P/np.trapz(fit.P,r),linewidth=2,color=violet,label='Distance distribution fit') +Pci95 = fit.PUncert.ci(95)/np.trapezoid(fit.P,r) +Pci50 = fit.PUncert.ci(50)/np.trapezoid(fit.P,r) +plt.plot(r,fit.P/np.trapezoid(fit.P,r),linewidth=2,color=violet,label='Distance distribution fit') plt.fill_between(r,Pci95[:,0],Pci95[:,1],color=violet,alpha=0.3) plt.fill_between(r,Pci50[:,0],Pci50[:,1],color=violet,alpha=0.4) diff --git a/test/test_ddmodels.py b/test/test_ddmodels.py index d7cba5482..a3bfd125e 100644 --- a/test/test_ddmodels.py +++ b/test/test_ddmodels.py @@ -30,7 +30,7 @@ def assert_ddmodel(model): assert all(P1 >= 0) assert all(P1 >= 0) and all(P2 >= 0) assert all(~np.isnan(P1)) and all(~np.isnan(P2)) and all(~np.isnan(P3)) and all(~np.isnan(P4)) - assert np.round(np.trapz(P5,rnus),2) == 1 + assert np.round(np.trapezoid(P5,rnus),2) == 1 assert len(lower)==nParam assert len(upper)==nParam assert len(meta['names'])==nParam diff --git a/test/test_dipolarmodel.py b/test/test_dipolarmodel.py index 05fe75645..f7b987e03 100644 --- a/test/test_dipolarmodel.py +++ b/test/test_dipolarmodel.py @@ -320,7 +320,7 @@ def test_fit_Pnonparametric_normalization(V1path): result = fit(Vmodel,V1path,ftol=1e-5) - assert np.isclose(np.trapz(result.P,r),1) + assert np.isclose(np.trapezoid(result.P,r),1) # ====================================================================== # Fixtures diff --git a/test/test_model_penalty.py b/test/test_model_penalty.py index 3020843e2..7919e89cc 100644 --- a/test/test_model_penalty.py +++ b/test/test_model_penalty.py @@ -24,8 +24,8 @@ def mock_data(): def penalty_fcn(): def _penalty_fcn(mean,std): P = dd_gauss(x,mean,std) - P = P/np.trapz(P,x) - return np.sqrt(P*(x - np.trapz(P*x,x))**2*np.mean(np.diff(x))) + P = P/np.trapezoid(P,x) + return np.sqrt(P*(x - np.trapezoid(P*x,x))**2*np.mean(np.diff(x))) return _penalty_fcn # ----------------------------------------------------------------------- diff --git a/test/test_snlls.py b/test/test_snlls.py index 29cc45e08..39799218d 100644 --- a/test/test_snlls.py +++ b/test/test_snlls.py @@ -207,7 +207,7 @@ def test_SNLLS_cost_value(mock_data,mock_Amodel): def test_SNLLS_fit_with_extra_penalty(mock_data,mock_Amodel): "Check that an additional penalty can be passed correctly to the SNLLS functional" beta = 0.05 - compactness_penalty = lambda _,plin: beta*np.sqrt(plin*(r - np.trapz(plin*r,r))**2*dr) + compactness_penalty = lambda _,plin: beta*np.sqrt(plin*(r - np.trapezoid(plin*r,r))**2*dr) fit = snlls(mock_data,mock_Amodel,nlpar0,lb,ub,lbl,extrapenalty=compactness_penalty) assert np.all(abs(lin_param - fit.lin) < 1e-1) and np.all(abs(nonlin_param - fit.nonlin[0]) < 1e-1) # ====================================================================== @@ -216,7 +216,7 @@ def test_SNLLS_fit_with_multiple_extra_penalties(mock_data,mock_Amodel): # ====================================================================== "Check that multiple additional penaltyies can be passed correctly to the SNLLS functional" beta = 0.05 - compactness_penalty = lambda _,plin: beta*np.sqrt(plin*(r - np.trapz(plin*r,r))**2*dr) + compactness_penalty = lambda _,plin: beta*np.sqrt(plin*(r - np.trapezoid(plin*r,r))**2*dr) fit = snlls(mock_data,mock_Amodel,nlpar0,lb,ub,lbl,extrapenalty=[compactness_penalty]) R = 0.5 radial_penalty = lambda pnonlin,_: 1/R**2*(np.linalg.norm((pnonlin-nonlin_param)/nonlin_param-R))**2