From fbf6bb958b8aa434e1baa3fc6627cbd7557b682a Mon Sep 17 00:00:00 2001 From: Luis Fabregas Date: Thu, 18 Mar 2021 09:11:53 +0100 Subject: [PATCH 1/2] fitsignal: merge uq and uqanalysis keywords --- deerlab/fitsignal.py | 11 ++++++---- test/test_fitsignal.py | 46 +++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/deerlab/fitsignal.py b/deerlab/fitsignal.py index 61466142f..eb24ffadb 100644 --- a/deerlab/fitsignal.py +++ b/deerlab/fitsignal.py @@ -249,15 +249,18 @@ def fitsignal(Vexp, t, r, dd_model='P', bg_model=bg_hom3d, ex_model=ex_4pdeer, # Default bootstrap samples bootsamples = 1000 - if isinstance(uq, str): + if isinstance(uq, str) or uq==None: uq = [uq] - if uq[0]!='bootstrap' and uq[0]!='covariance': - raise KeyError("Uncertainty quantification must be either 'covariance' or 'bootstrap'.") - + if uq[0]!='bootstrap' and uq[0]!='covariance'and uq[0]!=None: + raise KeyError("Uncertainty quantification must be either 'covariance', 'bootstrap', or None.") if uq[0]=='bootstrap': # OVerride default if user has specified bootstraped samples if len(uq)>1: bootsamples = uq[1] uq = uq[0] + if uq is None: + uqanalysis = False + else: + uqanalysis = True # Combine input boundary and start conditions par0 = [[] if par0_i is None else par0_i for par0_i in [dd_par0,bg_par0,ex_par0]] diff --git a/test/test_fitsignal.py b/test/test_fitsignal.py index a5101f1a0..4a2a73c33 100644 --- a/test/test_fitsignal.py +++ b/test/test_fitsignal.py @@ -25,7 +25,7 @@ def assert_experiment_model(model): K = dipolarkernel(t,r,pathways,Bmodel) V = K@P - fit = fitsignal(V,t,r,'P',bg_exp,model,uqanalysis=False) + fit = fitsignal(V,t,r,'P',bg_exp,model,uq=None) assert ovl(P,fit.P) > 0.90 # -------------------------------------------------------------------- @@ -88,7 +88,7 @@ def test_dipevo_function(): P = dd_gauss(r,[4.5, 0.25]) K = dipolarkernel(t,r) V = K@P - fit = fitsignal(V,t,r,'P',None,None,uqanalysis=False) + fit = fitsignal(V,t,r,'P',None,None,uq=None) assert ovl(P,fit.P) > 0.90 # ====================================================================== @@ -101,7 +101,7 @@ def test_form_factor(): P = dd_gauss(r,[4.5, 0.25]) K = dipolarkernel(t,r,0.3) V = K@P - fit = fitsignal(V,t,r,'P',None,ex_4pdeer,uqanalysis=False) + fit = fitsignal(V,t,r,'P',None,ex_4pdeer,uq=None) assert ovl(P,fit.P) > 0.90 # ====================================================================== @@ -117,7 +117,7 @@ def test_full_parametric(): B = bg_exp(t,lam*kappa) V = dipolarkernel(t,r,lam,B)@P - fit = fitsignal(V,t,r,dd_gauss,bg_exp,ex_4pdeer,uqanalysis=False) + fit = fitsignal(V,t,r,dd_gauss,bg_exp,ex_4pdeer,uq=None) assert ovl(P,fit.P) > 0.99 # ====================================================================== @@ -131,7 +131,7 @@ def test_no_foreground(): k = 0.2 B = bg_exp(t,k) - fit = fitsignal(B,t,r,None,bg_exp,None,uqanalysis=False) + fit = fitsignal(B,t,r,None,bg_exp,None,uq=None) assert abs(k-fit.bgparam) < 1 # ====================================================================== @@ -149,7 +149,7 @@ def test_start_values(): K = dipolarkernel(t,r,lam,Bmodel) V = K@P - fit = fitsignal(V,t,r,'P',bg_exp,ex_4pdeer,bg_par0=0.5,ex_par0=0.5,uqanalysis=False) + fit = fitsignal(V,t,r,'P',bg_exp,ex_4pdeer,bg_par0=0.5,ex_par0=0.5,uq=None) assert ovl(P,fit.P) > 0.95 # ====================================================================== @@ -167,7 +167,7 @@ def test_boundaries(): K = dipolarkernel(t,r,lam,Bmodel) V = K@P - fit = fitsignal(V,t,r,'P',bg_exp,ex_4pdeer, uqanalysis=False, + fit = fitsignal(V,t,r,'P',bg_exp,ex_4pdeer, uq=None, bg_par0=0.4, bg_lb=0.2, bg_ub=0.5, ex_par0=0.4, ex_lb=0.2, ex_ub=0.5) @@ -187,7 +187,7 @@ def test_boundaries_adjust_bg(): K = dipolarkernel(t,r,lam,Bmodel) V = K@P - fit = fitsignal(V,t,r,'P',bg_hom3d,ex_4pdeer, uqanalysis=False, bg_lb=70, bg_ub=90) + fit = fitsignal(V,t,r,'P',bg_hom3d,ex_4pdeer, uq=None, bg_lb=70, bg_ub=90) assert ovl(P,fit.P) > 0.95 # ====================================================================== @@ -205,7 +205,7 @@ def test_boundaries_adjust_ex(): K = dipolarkernel(t,r,lam,Bmodel) V = K@P - fit = fitsignal(V,t,r,'P',bg_hom3d,ex_4pdeer, uqanalysis=False, ex_lb=0.55, ex_ub=0.65) + fit = fitsignal(V,t,r,'P',bg_hom3d,ex_4pdeer, uq=None, ex_lb=0.55, ex_ub=0.65) assert ovl(P,fit.P) > 0.95 # ====================================================================== @@ -223,7 +223,7 @@ def test_boundaries_adjust_dd(): K = dipolarkernel(t,r,lam,Bmodel) V = K@P - fit = fitsignal(V,t,r,dd_gauss,bg_hom3d,ex_4pdeer, uqanalysis=False, dd_lb=[4,0.3],dd_ub=[6,0.5]) + fit = fitsignal(V,t,r,dd_gauss,bg_hom3d,ex_4pdeer, uq=None, dd_lb=[4,0.3],dd_ub=[6,0.5]) assert ovl(P,fit.P) > 0.95 # ====================================================================== @@ -248,7 +248,7 @@ def test_global_4pdeer(): t2 = np.linspace(0,5,250) V2 = dipolarkernel(t2,r,pathways,Bmodel)@P - fit = fitsignal([V1,V2],[t1,t2],r,'P',bg_exp,ex_4pdeer,uqanalysis=False) + fit = fitsignal([V1,V2],[t1,t2],r,'P',bg_exp,ex_4pdeer,uq=None) assert ovl(P,fit.P) > 0.90 # ====================================================================== @@ -267,7 +267,7 @@ def test_global_full_parametric(): V1 = dipolarkernel(t1,r,lam,B)@P V2 = dipolarkernel(t2,r,lam,B)@P - fit = fitsignal([V1,V2],[t1,t2],r,dd_gauss,bg_exp,ex_4pdeer,uqanalysis=False) + fit = fitsignal([V1,V2],[t1,t2],r,dd_gauss,bg_exp,ex_4pdeer,uq=None) assert ovl(P,fit.P) > 0.99 # ====================================================================== @@ -286,7 +286,7 @@ def test_global_mixed_backgrounds(): V1 = dipolarkernel(t1,r,lam,B)@P V2 = dipolarkernel(t2,r,lam)@P - fit = fitsignal([V1,V2],[t1,t2],r,dd_gauss,[bg_exp,None],ex_4pdeer,uqanalysis=False) + fit = fitsignal([V1,V2],[t1,t2],r,dd_gauss,[bg_exp,None],ex_4pdeer,uq=None) assert ovl(P,fit.P) > 0.90 # ====================================================================== @@ -303,7 +303,7 @@ def test_global_mixed_experiments(): V1 = dipolarkernel(t1,r,lam)@P V2 = dipolarkernel(t2,r)@P - fit = fitsignal([V1,V2],[t1,t2],r,dd_gauss,None,[ex_4pdeer,None],uqanalysis=False) + fit = fitsignal([V1,V2],[t1,t2],r,dd_gauss,None,[ex_4pdeer,None],uq=None) assert ovl(P,fit.P) > 0.9 # ====================================================================== @@ -542,7 +542,7 @@ def test_global_scale_4pdeer(): t2 = np.linspace(0,5,250) V2 = scales[1]*dipolarkernel(t2,r,pathways,Bmodel)@P - fit = fitsignal([V1,V2],[t1,t2],r,'P',bg_exp,ex_4pdeer,uqanalysis=False) + fit = fitsignal([V1,V2],[t1,t2],r,'P',bg_exp,ex_4pdeer,uq=None) assert max(abs(np.asarray(scales)/np.asarray(fit.scale) - 1)) < 1e-2 # ====================================================================== @@ -559,7 +559,7 @@ def test_V_scale_parametric(): scale = 1.54e6 V = scale*(K@P) - fit = fitsignal(V,t,r,dd_gauss,bg_exp,ex_4pdeer,uqanalysis=False) + fit = fitsignal(V,t,r,dd_gauss,bg_exp,ex_4pdeer,uq=None) assert max(abs(1 - V/fit.V)) < 1e-4 # ====================================================================== @@ -575,7 +575,7 @@ def test_V_scale(): scale =1.54e6 V = scale*(K@P) - fit = fitsignal(V,t,r,'P',bg_exp,ex_4pdeer,uqanalysis=False) + fit = fitsignal(V,t,r,'P',bg_exp,ex_4pdeer,uq=None) assert max(abs(1 - V/fit.V)) < 1e-4 # ====================================================================== @@ -590,7 +590,7 @@ def test_V_scale_regularized(): scale = 1.54e6 V = scale*(K@P) - fit = fitsignal(V,t,r,'P',None,None,uqanalysis=False) + fit = fitsignal(V,t,r,'P',None,None,uq=None) assert max(abs(1 - V/fit.V)) < 1e-4 # ====================================================================== @@ -606,7 +606,7 @@ def test_plot(): K = dipolarkernel(t,r,0.4,Bmodel) V = K@P - fit = fitsignal(V,t,r,'P',bg_exp,ex_4pdeer,uqanalysis=False) + fit = fitsignal(V,t,r,'P',bg_exp,ex_4pdeer,uq=None) fig = fit.plot(show=False) assert str(fig.__class__)=="" @@ -625,7 +625,7 @@ def test_physical_bg_model(): V = K@P V = V0*V - fit = dl.fitsignal(V,t,r,'P',dl.bg_hom3d,dl.ex_4pdeer,uqanalysis=False) + fit = dl.fitsignal(V,t,r,'P',dl.bg_hom3d,dl.ex_4pdeer,uq=None) assert abs(fit.bgparam - 50)<1e-1 and abs(fit.exparam - 0.4)<1e-1 # ====================================================================== @@ -642,7 +642,7 @@ def test_phenomenological_bg_model(): V = K@P V = V0*V - fit = dl.fitsignal(V,t,r,'P',dl.bg_exp,dl.ex_4pdeer,uqanalysis=False) + fit = dl.fitsignal(V,t,r,'P',dl.bg_exp,dl.ex_4pdeer,uq=None) assert abs(fit.bgparam - 0.3)<1e-1 and abs(fit.exparam - 0.4)<1e-1 # ====================================================================== @@ -660,7 +660,7 @@ def test_Vunmod(): Bscaled = (1-lam)*dl.bg_hom3d(t,50,lam) V = K@P - fit = dl.fitsignal(V,t,r,'P',dl.bg_exp,dl.ex_4pdeer,uqanalysis=False) + fit = dl.fitsignal(V,t,r,'P',dl.bg_exp,dl.ex_4pdeer,uq=None) assert max(abs(Bscaled - fit.Vunmod))<1e-4 # ====================================================================== @@ -677,7 +677,7 @@ def test_cost_value(): K = dipolarkernel(t,r,0.4,Bmodel) V = K@P - fit = fitsignal(V,t,r,'P',bg_exp,ex_4pdeer,uqanalysis=False) + fit = fitsignal(V,t,r,'P',bg_exp,ex_4pdeer,uq=None) assert isinstance(fit.cost,float) and np.round(fit.cost/np.sum(fit.residuals**2),5)==1 # ====================================================================== From ffac445dbeaf4a490cf3f287054b021b491ff63b Mon Sep 17 00:00:00 2001 From: Luis Fabregas Date: Thu, 18 Mar 2021 09:14:53 +0100 Subject: [PATCH 2/2] fitsignal: update uq option docstring --- deerlab/fitsignal.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/deerlab/fitsignal.py b/deerlab/fitsignal.py index eb24ffadb..7cf53130a 100644 --- a/deerlab/fitsignal.py +++ b/deerlab/fitsignal.py @@ -88,6 +88,9 @@ def fitsignal(Vexp, t, r, dd_model='P', bg_model=bg_hom3d, ex_model=ex_4pdeer, * ``'covariance'`` - Covariance-based uncertainty quantification. Fast, but approximate. * ``'bootstrap'`` - Bootstrapped uncertainty quantification. Slow, but accurate. By default, 1000 bootstrap samples are used. Alternatively, a different number can be specified as follows ``uq=['bootstrap',Nsamples]``. + * ``None`` - Disable the uncertainty quantification analysis. + + The default is ``'covariance'``. weights : array_like, optional Array of weighting coefficients for the individual signals in global fitting, @@ -124,10 +127,6 @@ def fitsignal(Vexp, t, r, dd_model='P', bg_model=bg_hom3d, ex_model=ex_4pdeer, verbose : boolean, optional Enable/disable printing a table of fit results, by default is disabled - - uqanalysis : boolean, optional - Enable/disable the uncertainty quantification analysis, by default it is enabled. - Returns