def test_make_func_code(): fc = util.make_func_code(["a", "b"]) assert fc.co_varnames == ("a", "b") assert fc.co_argcount == 2 fc = util.make_func_code(("x",)) assert fc.co_varnames == ("x",) assert fc.co_argcount == 1
def __init__(self, model, x, y, dx, dy): self.model = model # model predicts y value for given x value self.x = np.array(x) # the x values self.y = np.array(y) # the y values self.dx = np.array(dx) # the x-axis uncertainties self.dy = np.array(dy) # the y-axis uncertainties self.func_code = make_func_code(describe(self.model)[1:])
def __init__(self, f, data, weights=None, bound=None, badvalue=-100000, extended=False, extended_bound=None, extended_nint=100): if bound is not None: data = np.array(data) mask = (data >= bound[0]) & (data <= bound[1]) data = data[mask] if (weights is not None): weights = weights[mask] self.f = f # model predicts PDF for given x self.data = np.array(data) self.weights = set_var_if_None(weights, self.data) self.bad_value = badvalue self.extended = extended self.extended_bound = extended_bound self.extended_nint = extended_nint if extended and extended_bound is None: self.extended_bound = (np.min(data), np.max(data)) self.func_code = make_func_code(describe(self.f)[1:])
def kg(self, x, A, B, Δ, ν): ''' Gaussian Kubo Toyabe in longitudinal field, static or dynamic x [mus], A, B [T], Δ [mus-1], ν (MHz) x need not be self.x (e.g. in plot) ''' N = x.shape[0] w = 2 * pi * B * self._gamma_Mu_MHzper_mT if ν == 0: # static f = self._kg(x, w, Δ) # normalized to 1. In this case t = x else: # dynamic # P=[w Δ]; f = self._kgdyn(x, w, Δ, ν) # function generated from t=0, shift result nshift=data(1,1)/dt bins backward dt = x[1] - x[0] nshift = x[0] / dt Ns = N + ceil(nshift) if Ns % 2: # odd Np = Ns // 2 Nm = -Np else: # even Np = Ns // 2 - 1 Nm = -Ns // 2 n = hstack((inspace(0, Np, Np + 1), linspace(Nm, -1., -Nm))) f = fft.ifft(fft.fft(f) * exp(nshift * 1j * 2 * pi * n / Ns)) # shift back # multiply by amplitude f = A * real(f[0:N]) return f kg.func_code = make_func_code(["A", "B", "Δ", "ν"])
def bs(self, x, A, λ, β): ''' fit component for a stretched decay, x [mus], A, λ [mus-1], β (>0) x need not be self.x (e.g. in plot) ''' return A * exp(-(x * λ)**β) bs.func_code = make_func_code(["A", "λ", "β"])
def bg(self, x, A, σ): ''' fit component for a Gaussian decay, x [mus], A, σ [mus-1] x need not be self.x (e.g. in plot) ''' return A * exp(-0.5 * (x * σ)**2) bg.func_code = make_func_code(["A", "σ"])
def bl(self, x, A, λ): ''' fit component for a Lorentzian decay, x [mus], A, λ [mus-1] x need not be self.x (e.g. in plot) ''' return A * exp(x * λ) bl.func_code = make_func_code(["A", "λ"])
def __init__(self, f, data): self.f = f self.data = data f_sig = describe(f) # this is how you fake function # signature dynamically self.func_code = make_func_code(f_sig[1:]) # docking off independent variable self.func_defaults = None # this keeps np.vectorize happy
def __init__(self, model, x, y, cov_inv, regulator=0, regulator_val=1): self.model = model self.regulator = regulator self.regulator_val = regulator_val self.x = np.array(x) self.y = np.array(y) self.cov_inv = np.array(cov_inv) self.func_code = make_func_code(describe(self.model)[1:])
def __init__(self, f, x, y, sy=None, weights=None): self.f = f # model predicts y for given x self.x = np.array(x) self.y = np.array(y) self.sy = set_var_if_None(sy, self.x) self.weights = set_var_if_None(weights, self.x) self.func_code = make_func_code(describe(self.f)[1:])
def __init__(self,f,x,y): self.f = f self.x = x self.y = y f_sig = describe(f) #this is how you fake function #signature dynamically self.func_code = make_func_code(f_sig[1:])#docking off independent variable self.func_defaults = None #this keeps np.vectorize happy
def jg(self, x, A, B, φ, σ): ''' fit component for a Bessel j0 precessing muon with Gaussian decay, x [mus], A, B [mT], φ [degrees], σ [mus-1] x need not be self.x (e.g. in plot) ''' return A * j0(2 * pi * self._gamma_Mu_MHzper_mT * B * x + φ * self._radeg_) * exp(-0.5 * (x * σ)**2) jg.func_code = make_func_code(["A", "B", "φ", "σ"])
def jl(self, x, A, B, φ, λ): ''' fit component for a Bessel j0 precessing muon with Lorentzian decay, x [mus], A, B [mT], φ [degrees], λ [mus-1] x need not be self.x (e.g. in plot) ''' return A * j0(2 * pi * self._gamma_Mu_MHzper_mT * B * x + φ * self._radeg_) * exp(-x * λ) jl.func_code = make_func_code(["A", "B", "φ", "λ"])
def ms(self, x, A, B, φ, λ, β): ''' fit component for a precessing muon with stretched decay, x [mus], A, B [mT], φ [degrees], λ [mus-1], β (>0) x need not be self.x (e.g. in plot) ''' return A * cos(2 * pi * self._gamma_Mu_MHzper_mT * B * x + φ * self._radeg_) * exp(-(x * λ)**beta) ms.func_code = make_func_code(["A", "B", "φ", "λ", "β"])
def __init__(self, f, data, bins=40, weights=None, weighterrors=None, bound=None, badvalue=1000000, extended=False, use_w2=False, nint_subdiv=1): if bound is not None: data = np.array(data) mask = (data >= bound[0]) & (data <= bound[1]) data = data[mask] if (weights is not None): weights = weights[mask] if (weighterrors is not None): weighterrors = weighterrors[mask] self.weights = set_var_if_None(weights, data) self.f = f self.use_w2 = use_w2 self.extended = extended if bound is None: bound = (np.min(data), np.max(data)) self.mymin, self.mymax = bound h, self.edges = np.histogram(data, bins, range=bound, weights=weights) self.bins = bins self.h = h self.N = np.sum(self.h) if weights is not None: if weighterrors is None: self.w2, _ = np.histogram(data, bins, range=bound, weights=weights**2) else: self.w2, _ = np.histogram(data, bins, range=bound, weights=weighterrors**2) else: self.w2, _ = np.histogram(data, bins, range=bound, weights=None) self.badvalue = badvalue self.nint_subdiv = nint_subdiv self.func_code = make_func_code(describe(self.f)[1:]) self.ndof = np.sum(self.h > 0) - (self.func_code.co_argcount - 1)
def __init__(self, f, n_params): self.f = f varnames = [] for i in range(n_params): varnames.append('p{:d}'.format(i)) self.func_code = make_func_code(varnames) # fake signature self.func_defaults = None # this keeps np.vectorize happy
def ml(self, x, A, B, φ, λ): ''' fit component for a precessing muon with Lorentzian decay, x [mus], A, B [mT], φ [degrees], λ [mus-1] x need not be self.x (e.g. in plot) ''' # print('a={}, B={}, ph={}, lb={}'.format(asymmetry,field,phase,Lor_rate)) return A * cos(2 * pi * self._gamma_Mu_MHzper_mT * B * x + φ * self._radeg_) * exp(-x * λ) ml.func_code = make_func_code(["A", "B", "φ", "λ"])
def __init__(self, model, x, y, dx, dy): self.model = model # model predicts y value for given x value self.x = np.array(x) # the x values self.y = np.array(y) # the y values self.dx = np.array(dx) # the x-axis uncertainties self.dy = np.array(dy) # the y-axis uncertainties self.func_code = make_func_code(describe(self.model)[1:]) self.h = ( x[-1] - x[0] ) / 10000 # this is the step size for the numerical calculation of the df/dx = last value in x (x[-1]) - first value in x (x[0])/10000
def al(self, x, α): ''' fit component for alpha calibration x [mus], dα x for compatibility, here it is dummy anyway ''' # empty method (could remove x from argument list ?) # print('al = {}'.format(α)) return [] al.func_code = make_func_code(["α"])
def __init__(self, data, S, d): self.data = data self.S = S self.d = d self.func_code = make_func_code(list(d.keys())) self.N_flavors, self.N_bins, _ = S.shape if not (self.N_flavors == 2 or self.N_flavors == 3): raise IllegalArgumentError('Number of flavors should be 2 or 3')
def da(self, x, dα): ''' fit component for linearized alpha correction x [mus], dα x for compatibility, here it is dummy anyway ''' # the returned value will not be used, correction in _add_ # print('dα = {}'.format(dα)) return [ ] # zeros(self.x.shape[0]) # alternatively, return [], remove x from argument list da.func_code = make_func_code(["dα"])
def useMinuit(self, b=True): # For iminuit's parameter introspection # Gets screwed up if parameters are changed between fixed and floating # Make sure this is propagated (somehow?) # Also screws up pickling (required for ThreadPool) if b: self.func_code = make_func_code(self.floatingParameterNames) else: self.func_code = None
def __init__(self, f, t, y, yerr): # def __init__(self,f,wvl,y): self.f = f self.t = t self.y = y self.yerr = yerr f_sig = describe(f) # this is how you fake function # signature dynamically # docking off independent variable self.func_code = make_func_code(f_sig[1:]) # this keeps np.vectorize happy self.func_defaults = None
def fm(self, x, A, B, λ): ''' fit component for FmuF (powder average) x [mus], A, B [mT], λ [mus-1] x need not be self.x (e.g. in plot) ''' return A / 6.0 * ( 3. + cos(2 * pi * self._gamma_Mu_MHzper_mT * B * sqrt(3.) * x) + (1. - 1. / sqrt(3.)) * cos(pi * self._gamma_Mu_MHzper_mT * Bd * (3. - sqrt(3.)) * x) + (1. + 1. / sqrt(3.)) * cos(pi * self._gamma_Mu_MHzper_mT * Bd * (3. + sqrt(3.)) * x)) * exp(-x * λ) fm.func_code = make_func_code(["A", "B", "λ"])
def __init__(self, f, data, weights=None, badvalue=-100000, extended=False, extended_bound=None, extended_nint=100): self.f = f # model predicts PDF for given x self.data = np.array(data) self.weights = set_var_if_None(weights, self.data) self.bad_value = badvalue self.extended = extended self.extended_bound = extended_bound self.extended_nint = extended_nint if extended and extended_bound is None: self.extended_bound = (np.min(data), np.max(data)) self.func_code = make_func_code(describe(self.f)[1:])
def __init__(self, f, x, y, sy=None, weights=None, bound=None): if bound is not None: x = np.array(x) y = np.array(y) sy = np.array(sy) mask = (x >= bound[0]) & (x <= bound[1]) x = x[mask] y = y[mask] sy = sy[mask] self.f = f # model predicts y for given x self.x = np.array(x) self.y = np.array(y) self.sy = set_var_if_None(sy, self.x) self.weights = set_var_if_None(weights, self.x) self.func_code = make_func_code(describe(self.f)[1:])
def __init__(self, model, data=None, bins=40, weights=None, weighterrors=None, bound=None, badvalue=1000000, extended=False, use_w2=False, nint_subdiv=1, minimiser='minuit'): if isinstance(model, HistiModel): self.pdf = model.pdf self.binedges = model.binedges self.func_code = FakeFuncCode(self.pdf, dock=True) self.parameters = model.Parameters() else: print("ERROR model should be an instance of HistiModels") self.minimiser = minimiser if hasattr(model, "data"): self.data = model.data if self.data is None: print("error: data is None, please feed the model with data") else: self.h = np.asarray(self.data) self.binned_data = data self.N = self.h.sum() else: print("error: model has no attribute data") self.use_w2 = use_w2 self.extended = extended if bound is None: self.bound = (self.data[0], self.data[-1]) else: self.bound = bound self.mymin, self.mymax = self.bound pdf_sig = describe(self.pdf) self.func_code = make_func_code(pdf_sig[1:]) self.func_defaults = None
def __init__(self, I, x=None, verbose=True): self.I = I.copy() if x is not None: self.x = x.copy() else: self.x = np.arange(len(I)) self.sy = np.sqrt(I) self.verbose = verbose self.model = exponential self.fit_kwargs = { "I_0": self.I[0], "R_eff": 1, "limit_R_eff": (0, None), "T": 4.7, "fix_T": True, } self.func_code = make_func_code(describe(self.model)[1:]) self.N_fit_parameters = len(describe(self.model)[1:]) self.N = len(I) self.df = self.N - self.N_fit_parameters
def __init__(self, model, x, y): self.model = model # model predicts y for given x self.x = np.array(x) self.y = np.array(y) self.y_err = custom_errors(self.y) self.func_code = make_func_code(describe(self.model)[1:])
def __init__(self, f, data): self.f = f # model predicts PDF for given x self.data = np.array(data) self.func_code = make_func_code(describe( self.f)[1:]) #Function signature for the minimizer
def __init__(self, f, s_args): self.func = f self.s_args = s_args self.func_code = make_func_code(s_args)
def __init__(self, f, names): from iminuit.util import make_func_code self.f = f self.func_code = make_func_code(names) self.func_defaults = None
def __init__(self, f, names): self.f = f self.func_code = make_func_code(names) self.func_defaults = None
def __init__(self): self.func_code = make_func_code(['x', 'y'])