def lmfit_powerlaw(x, y, yerr=None, xmin=(- np.inf), xmax=np.inf, init=None, maxiter=1000000):

    @custom_model
    def model(x, beta=init[0], kappa=init[1]):
        return np.log((kappa * (np.exp(x) ** beta)))
    keep = ((np.isfinite((1.0 / y)) & (x >= xmin)) & (x <= xmax))
    if (yerr is not None):
        keep = (keep & np.isfinite((1.0 / yerr)))
    m_init = model()
    fit = LevMarLSQFitter()
    m = fit(m_init, np.log(x[keep]), np.log(y[keep]), maxiter=maxiter)
    return (m, fit)
def lmfit_powerlaw(x, y, yerr=None, xmin=(- np.inf), xmax=np.inf, init=None, maxiter=1000000):

    @custom_model
    def model(x, beta=init[0], kappa=init[1]):
        return np.log((kappa * (np.exp(x) ** beta)))
    keep = ((np.isfinite((1.0 / y)) & (x >= xmin)) & (x <= xmax))
    if (yerr is not None):
        keep = (keep & np.isfinite((1.0 / yerr)))
    m_init = model()
    fit = LevMarLSQFitter()
    m = fit(m_init, np.log(x[keep]), np.log(y[keep]), maxiter=maxiter)
    return (m, fit)