Best Python code snippet using autotest_python
kernelexpand.py
Source:kernelexpand.py  
...101        if not len(new_patches):102            new_patches.append(component[-1])103        new_components.append(new_patches)104    return new_components105def expand_classic(kernel, mirrors):106    components = decompose_kernel(kernel)107    if mirrors:108        components = mirror_kernel_components(mirrors, components)109    components = select_kernel_components(components)110    patches = []111    for component in components:112        patches.append(component[0])113    return patches114if __name__ == '__main__':115    from optparse import OptionParser116    parser = OptionParser()117    parser.add_option("-m", "--mirror", type="string", dest="mirror",118            action="append", nargs=2, help="mirror prefix")119    parser.add_option("-v", "--no-validate", dest="validate",...nelder_mead.py
Source:nelder_mead.py  
1import numpy as np2EXPAND_MINIMIZE, EXPAND_CLASSIC = 0, 13class NelderMead:    4    #static5    def init_simplex(self, N, x0=None, simplex_edge=1):6        if x0 is None:7            x0 = np.zeros(N)8        S = [x0]9        for i in range(N):10            S.append(x0 + simplex_edge * np.array([int(j == i) for j in range(N)]))11        return np.array(S)12            13    def __init__(self, α=1, β=0.5, γ=2, δ=0.5, expand=EXPAND_MINIMIZE, eps_x=1e-5, eps_f=1e-5, timeout=100):14        """15        EXPAND_MINIMIZE: x_new = argmin{f_e, f_r};16        EXPAND_CLASSIC: x_new = x_e if f_e < f_best17        """18        self.expand = expand19        self.α, self.β, self.γ, self.δ = α, β, γ, δ20        self.eps_x, self.eps_f = eps_x, eps_f21        self.timeout = timeout22    def _eval_vertices(self):23        """Return sorted list of (v_i, f(v_i))"""24        return sorted(enumerate(self.f(self.S[:,0], self.S[:,1])), key=lambda x:x[1])25    def _centroid(self):26        # sum everything except the worst point27        c = self.S.sum(axis=0) - self.S[self.fS[-1][0]]28        return c / self.N  # mean of (N+1)-1 vertices29    30    def _step(self):31        self.steps += 132        (best_i, best_f), (worst_i, worst_f), (worst2_i, worst2_f) = self.fS[0], self.fS[-1], self.fS[-2]33        c = self._centroid()34        35        action = 'reflected'36        37        # reflect38        x_r = c + self.α * (c - self.S[worst_i])39        f_r = self.f(*x_r)40        41        if f_r < best_f:42            # expand43            x_e = c + self.γ * (x_r - c)44            f_e = self.f(*x_e)45            46            if self.expand == EXPAND_MINIMIZE:47                if f_e < f_r:48                    self.S[worst_i] = x_e49                    action = 'expanded_m'50                else:51                    self.S[worst_i] = x_r52                    action = 'reflected'53            elif self.expand == EXPAND_CLASSIC:54                if f_e < best_f:55                    self.S[worst_i] = x_e56                    action = 'expanded_c'57                else:58                    self.S[worst_i] = x_r59                    action = 'reflected'60        elif f_r > worst2_f:61            # contract62            if f_r < worst_f:63                # contract outside - from reflected to centroid64                x_c = c + self.β * (x_r - c)65                action = 'contracted inside'66            else:67                # contract inside - from worst to centroid 68                x_c = c + self.β * (self.S[worst_i] - c)69                action = 'contracted outside'70            f_c = self.f(*x_c)71            if f_c <= f_r:72                self.S[worst_i] = x_c73            else:74                # shrink75                action = 'shrink'76                for i in range(self.N):77                    x_i = self.fS[i+1][0]78                    self.S[x_i] = self.S[best_i] + self.δ * (self.S[x_i] - self.S[best_i])79        else:80            # best_f <= self.f(xr) < worst2_f81            # enhanced a bit. save new vertex82            self.S[worst_i] = x_r83            action = 'reflected'84        85        self.fS = self._eval_vertices()86        self.c = c87        self.last_action = action88        89    def _term_condition(self):90        if self.steps == 0: 91            return False92        93        # a) vertex are close94        if ((self.S - self.c) < self.eps_x).all():95            self.term_condition = 'x close'96            return True97        98        # b) vertex values are close99        fc = self.f(*self.c)100        if (np.abs([self.f(*x) - fc for x in self.S]) < self.eps_f).all():101            self.term_condition = 'f close'102            return True103        104        # c) timeout105        if self.steps > self.timeout:106            self.term_condition = 'timeout'107            return True108        109        return False110    111    def optimize(self, f, N, G, S0=None, init_only=False):112        self.lims = G.min(axis=0), G.max(axis=0)113        self.N = N114        self.f = f115        self.G = G116        self.steps = 0117        self.last_action = None118        self.term_condition = None119        120        if S0 is None:121            self.S = self.init_simplex(self.N)122        else:123            self.S = S0124        self.fS = self._eval_vertices()125        126        if init_only:127            return128        129        while not self._term_condition():130            self._step()131        132        c = self.S.mean(axis=0)133        return {134            'x': c,135            'f': f(*c),136            'steps': self.steps,137            'message': 'stopped because '+self.term_condition138        }...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
