コード例 #1
0
def chi2_map(minout,
             xname,
             yname,
             nx=11,
             ny=11,
             xrange=None,
             yrange=None,
             sigmas=None,
             **kws):
    """generate a confidence map for any two parameters for a fit

    Arguments
    ==========
       minout   output of minimize() fit (must be run first)
       xname    name of variable parameter for x-axis
       yname    name of variable parameter for y-axis
       nx       number of steps in x [11]
       ny       number of steps in y [11]
       xrange   range of calculations for x [x.best +/- 5*x.stderr]
       yrange   range of calculations for y [y.best +/- 5*y.stderr]

    Returns
    =======
        xpts, ypts, map
    """
    return conf_interval2d(minout,
                           xname,
                           yname,
                           nx=nx,
                           ny=ny,
                           sigmas=sigmas,
                           **kws)
コード例 #2
0
def test_confidence_2d(data, pars):
    """Test the 2D confidence interval calculation."""
    minimizer = lmfit.Minimizer(residual, pars, fcn_args=data)
    out = minimizer.minimize(method='leastsq')

    cx, cy, grid = lmfit.conf_interval2d(minimizer, out, 'a', 'b', 30, 20)
    assert len(cx.ravel()) == 30
    assert len(cy.ravel()) == 20
    assert grid.shape == (20, 30)
コード例 #3
0
ファイル: test_confidence.py プロジェクト: lmfit/lmfit-py
def test_confidence_2d(data, pars):
    """Test the 2D confidence interval calculation."""
    minimizer = lmfit.Minimizer(residual, pars, fcn_args=data)
    out = minimizer.minimize(method='leastsq')

    cx, cy, grid = lmfit.conf_interval2d(minimizer, out, 'a', 'b', 30, 20)
    assert len(cx.ravel()) == 30
    assert len(cy.ravel()) == 20
    assert grid.shape == (20, 30)
コード例 #4
0
ファイル: __init__.py プロジェクト: astrojuan/xraylarch
def chi2_map(fit_result,
             xname,
             yname,
             nx=11,
             ny=11,
             sigma=3,
             _larch=None,
             **kws):
    """generate a confidence map for any two parameters for a fit

    Arguments
    ==========
       minout   output of minimize() fit (must be run first)
       xname    name of variable parameter for x-axis
       yname    name of variable parameter for y-axis
       nx       number of steps in x [11]
       ny       number of steps in y [11]
       sigma    scale for uncertainty range [3]

    Returns
    =======
        xpts, ypts, map

    Notes
    =====
     1.  sigma sets the extent of values to explore:
              param.value +/- sigma * param.stderr
    """
    #
    fitter = getattr(fit_result, 'fitter', None)
    result = getattr(fit_result, 'fit_details', None)
    if fitter is None or result is None:
        raise ValueError("chi2_map needs valid fit result as first argument")

    c2_scale = fit_result.chi_square / result.chisqr

    def scaled_chisqr(ndata, nparas, new_chi, best_chi, nfix=1.):
        """return scaled chi-sqaure, instead of probability"""
        return new_chi * c2_scale

    x = result.params[xname]
    y = result.params[yname]
    xrange = (x.value + sigma * x.stderr, x.value - sigma * x.stderr)
    yrange = (y.value + sigma * y.stderr, y.value - sigma * y.stderr)

    return conf_interval2d(fitter,
                           result,
                           xname,
                           yname,
                           limits=(xrange, yrange),
                           prob_func=scaled_chisqr,
                           nx=nx,
                           ny=ny,
                           **kws)
コード例 #5
0
ファイル: test_confidence.py プロジェクト: lmfit/lmfit-py
def test_confidence_2d_limits(data, pars):
    """Test the 2D confidence interval calculation using limits."""
    minimizer = lmfit.Minimizer(residual, pars, fcn_args=data)
    out = minimizer.minimize(method='leastsq')

    lim = ((1.0e-6, 0.02), (1.0e-6, -4.0))
    cx, cy, grid = lmfit.conf_interval2d(minimizer, out, 'a', 'b', limits=lim)
    assert grid.shape == (10, 10)
    assert_allclose(min(cx.ravel()), 1.0e-6)
    assert_allclose(max(cx.ravel()), 0.02)
    assert_allclose(min(cy.ravel()), -4.0)
    assert_allclose(max(cy.ravel()), 1.0e-6)
コード例 #6
0
def test_confidence_2d_limits(data, pars):
    """Test the 2D confidence interval calculation using limits."""
    minimizer = lmfit.Minimizer(residual, pars, fcn_args=data)
    out = minimizer.minimize(method='leastsq')

    lim = ((1.0e-6, 0.02), (1.0e-6, -4.0))
    cx, cy, grid = lmfit.conf_interval2d(minimizer, out, 'a', 'b', limits=lim)
    assert grid.shape == (10, 10)
    assert_allclose(min(cx.ravel()), 1.0e-6)
    assert_allclose(max(cx.ravel()), 0.02)
    assert_allclose(min(cy.ravel()), -4.0)
    assert_allclose(max(cy.ravel()), 1.0e-6)
コード例 #7
0
def trace_plotter(ci, trace, result, minimizer):
    import pylab as pl
    pl.figure(5).clf()
    cx, cy, grid = lmfit.conf_interval2d(minimizer, result, 'rinner', 'router',
                                         30, 30)
    ax = pl.subplot(3, 3, 4)
    ax.contourf(cx, cy, grid, np.linspace(0, 1, 11), cmap='gray')
    ax.xlabel('$R_{inner}$ [AU]')
    ax.ylabel('$R_{outer}$ [AU]')
    ax.colorbar()

    cx, cy, grid = lmfit.conf_interval2d(minimizer, result, 'rinner', 'mass',
                                         30, 30)

    ax = pl.subplot(3, 3, 7)
    ax.contourf(cx, cy, grid, np.linspace(0, 1, 11), cmap='gray')
    ax.xlabel('$R_{inner}$ [AU]')
    ax.ylabel('Mass [$M_\odot$]')
    ax.colorbar()

    cx, cy, grid = lmfit.conf_interval2d(minimizer, result, 'router', 'mass',
                                         30, 30)

    ax = pl.subplot(3, 3, 8)
    ax.contourf(cx, cy, grid, np.linspace(0, 1, 11), cmap='gray')
    ax.xlabel('$R_{outer}$ [AU]')
    ax.ylabel('Mass [$M_\odot$]')
    ax.colorbar()

    ax = pl.subplot(3, 3, 1)
    ax.hist(trace['rinner'], facecolor='none', bins=25)

    ax = pl.subplot(3, 3, 2)
    ax.hist(trace['router'], facecolor='none', bins=25)

    ax = pl.subplot(3, 3, 3)
    ax.hist(trace['mass'], facecolor='none', bins=25)
コード例 #8
0
def covar(mzr,nsigma=8,nx=15,figsize=(8,8)):
    params = mzr.params
    pnames = [x for x in params.keys() if x != 'A_phase']
    nparam = len(pnames)
    f = plt.figure(figsize=figsize)
    for p1 in range(nparam):
        for p2 in range(p1+1,nparam):
            print pnames[p1],pnames[p2]
            ax = f.add_subplot(nparam,nparam,nparam*p2+p1+1)
            x0 = params[pnames[p1]].value
            xerr = params[pnames[p1]].stderr*nsigma
            y0 = params[pnames[p2]].value
            yerr = params[pnames[p2]].stderr*nsigma
            x,y,im = lmfit.conf_interval2d(mzr,pnames[p1],pnames[p2],limits=((x0-xerr,x0+xerr),(y0-yerr,y0+yerr)),nx=nx,ny=nx)
            ax.contourf((x-x0)/abs(x0),(y-y0)/abs(y0),im,20)
            ax.set_xlabel(pnames[p1])
            ax.set_ylabel(pnames[p2])
コード例 #9
0
def chi2_map(fit_result, xname, yname, nx=11, ny=11, _larch=None, **kws):
    """generate a confidence map for any two parameters for a fit

    Arguments
    ==========
       minout   output of minimize() fit (must be run first)
       xname    name of variable parameter for x-axis
       yname    name of variable parameter for y-axis
       nx       number of steps in x [11]
       ny       number of steps in y [11]

    Returns
    =======
        xpts, ypts, map
    """
    fitter = getattr(fit_result, 'fitter', None)
    result = getattr(fit_result, 'fit_details', None)
    return conf_interval2d(fitter, result, xname, yname, nx=nx, ny=ny, **kws)
コード例 #10
0
ファイル: __init__.py プロジェクト: maurov/xraylarch
def chi2_map(fit_result, xname, yname, nx=11, ny=11, sigma=3, _larch=None, **kws):
    """generate a confidence map for any two parameters for a fit

    Arguments
    ==========
       minout   output of minimize() fit (must be run first)
       xname    name of variable parameter for x-axis
       yname    name of variable parameter for y-axis
       nx       number of steps in x [11]
       ny       number of steps in y [11]
       sigma    scale for uncertainty range [3]

    Returns
    =======
        xpts, ypts, map

    Notes
    =====
     1.  sigma sets the extent of values to explore:
              param.value +/- sigma * param.stderr
    """
    #
    fitter = getattr(fit_result, 'fitter', None)
    result = getattr(fit_result, 'fit_details', None)
    if fitter is None or result is None:
        raise ValueError("chi2_map needs valid fit result as first argument")

    c2_scale = fit_result.chi_square / result.chisqr

    def scaled_chisqr(ndata, nparas, new_chi, best_chi, nfix=1.):
        """return scaled chi-sqaure, instead of probability"""
        return new_chi * c2_scale

    x = result.params[xname]
    y = result.params[yname]
    xrange = (x.value + sigma * x.stderr, x.value - sigma * x.stderr)
    yrange = (y.value + sigma * y.stderr, y.value - sigma * y.stderr)

    return conf_interval2d(fitter, result, xname, yname,
                           limits=(xrange, yrange),
                           prob_func=scaled_chisqr,
                           nx=nx, ny=ny, **kws)
コード例 #11
0
    def confidence_interval2D(self, x_name, y_name, nx=10, ny=10):
        """Draws a 2D confidence intervals using matplotlib.

        Parameters
        ----------
        x_name : str
            Name of the variable that will be on the x axis.
        y_name : str
            Name of the variable that will be on the y axis.
        nx : int, optional
            Number of points in the x direction, default 10, the higher the value, better resolution, but slower.
        ny : int, optional
            Number of points in the y direction, default 10, the higher the value, better resolution, but slower.
        """
        cx, cy, grid = conf_interval2d(self.minimizer, self.result, x_name, y_name, nx, ny)
        plt.contourf(cx, cy, grid, np.linspace(0, 1, 21))
        plt.xlabel(x_name)
        plt.colorbar()
        plt.ylabel(y_name)
        plt.show()
コード例 #12
0
ファイル: fit_saw_resonator.py プロジェクト: edumur/s2p
    def plot_s21_conf_interval2d(self, a, b, title, file_name,
                                 a_nb_point=50, b_nb_point=50,
                                 cmap=plt.cm.jet, file_format='png',
                                 grid=True, show=False):


        mini = lmfit.Minimizer(self.residual_inverse_circle, self.result.params)

        fig, ax = plt.subplots(1, 1)

        cx, cy, data_grid = lmfit.conf_interval2d(mini, self.result,
                                             a.lower(), b.lower(),
                                             a_nb_point, b_nb_point)

        cax = plt.imshow(data_grid,
                         interpolation='none',
                         origin='bottom',
                         extent=[cx[0], cx[-1], cy[0], cy[-1]],
                         aspect='auto',
                         cmap=cmap)

        cb = fig.colorbar(cax)
        cb.solids.set_rasterized(True)
        cb.solids.set_edgecolor('face')
        cb.set_label('Probability')

        ax.set_ylabel(b)
        ax.set_xlabel(a)

        ax.ticklabel_format(style='scientific', scilimits=(0,0))

        if grid:
            ax.grid(which='both', color='w')

        fig.suptitle(title)

        if show:
            plt.show()
        else:
            plt.savefig('{0}.{1}'.format(file_name, file_format))
            plt.close(fig)
コード例 #13
0
def covar(mzr, nsigma=8, nx=15, figsize=(8, 8)):
    params = mzr.params
    pnames = [x for x in params.keys() if x != 'A_phase']
    nparam = len(pnames)
    f = plt.figure(figsize=figsize)
    for p1 in range(nparam):
        for p2 in range(p1 + 1, nparam):
            print pnames[p1], pnames[p2]
            ax = f.add_subplot(nparam, nparam, nparam * p2 + p1 + 1)
            x0 = params[pnames[p1]].value
            xerr = params[pnames[p1]].stderr * nsigma
            y0 = params[pnames[p2]].value
            yerr = params[pnames[p2]].stderr * nsigma
            x, y, im = lmfit.conf_interval2d(mzr,
                                             pnames[p1],
                                             pnames[p2],
                                             limits=((x0 - xerr, x0 + xerr),
                                                     (y0 - yerr, y0 + yerr)),
                                             nx=nx,
                                             ny=nx)
            ax.contourf((x - x0) / abs(x0), (y - y0) / abs(y0), im, 20)
            ax.set_xlabel(pnames[p1])
            ax.set_ylabel(pnames[p2])
コード例 #14
0
ファイル: 191217-mesoscale_eos.py プロジェクト: lanl/fiteos
def confidence_intervals(ci, mini, out2, fix_vo, bm2):
    plt.figure(4)
    results = out2.params.valuesdict()

    ko_unc = float(str(out2.params['ko']).split()[4].replace(',', ''))

    if fix_vo:
        kp_unc = out2.params['kp'].stderr
        fig, axes = plt.subplots(1, 1)
        cx, cy, grid = conf_interval2d(mini, out2, 'ko', 'kp', 80, 80)
        ctp = axes.contourf(cx, cy, grid, np.linspace(0, 1, 100))
        axes.errorbar(results['ko'],results['kp'], xerr=ko_unc, yerr=kp_unc,\
                        linestyle='None', marker='None', label=sample,color='white', capsize = 3.,elinewidth=1.5)
        #fig.colorbar(ctp, ax=axes[1])
        axes.set_xlabel('$\mathbf{K_0 (GPa)}$')
        axes.set_ylabel('$\mathbf{K_0}$\'')
        axes.tick_params(direction='in', bottom=1, top=1, left=1, right=1)

    elif bm2:
        vo_unc = out2.params['vo'].stderr
        fig, axes = plt.subplots(1, 1)
        cx, cy, grid = conf_interval2d(mini, out2, 'vo', 'ko', 80, 80)
        ctp = axes.contourf(cx, cy, grid, np.linspace(0, 1, 100))
        axes.errorbar(results['vo'],results['ko'], xerr=vo_unc, yerr=ko_unc,\
                        linestyle='None', marker='None', label=sample,color='white', capsize = 3.,elinewidth=1.5)
        axes.set_xlabel('$\mathbf{V_0 (\AA^3)}$')
        axes.set_ylabel('$\mathbf{K_0 (GPa)}$')
        axes.tick_params(direction='in', bottom=1, top=1, left=1, right=1)
    else:
        vo_unc = out2.params['vo'].stderr
        kp_unc = out2.params['kp'].stderr
        # plot confidence intervals
        fig, axes = plt.subplots(2, 2)
        cx, cy, grid = conf_interval2d(mini, out2, 'vo', 'kp', 80, 80)
        ctp = axes[1, 0].contourf(cx, cy, grid, np.linspace(0, 1, 100))
        axes[1,0].errorbar(results['vo'],results['kp'], xerr=vo_unc, yerr=kp_unc,\
                        linestyle='None', marker='None', label=sample,color='white', capsize = 3.,elinewidth=1.5)
        axes[1, 0].set_xlabel('$\mathbf{V_0 (\AA^3)}$')
        axes[1, 0].set_ylabel('$\mathbf{K_0}$\'')
        axes[1, 0].tick_params(direction='in',
                               bottom=1,
                               top=1,
                               left=1,
                               right=1)

        cx, cy, grid = conf_interval2d(mini, out2, 'vo', 'ko', 80, 80)
        ctp = axes[0, 0].contourf(cx, cy, grid, np.linspace(0, 1, 100))
        #axes[0,0].set_xlabel('$\mathbf{V_0 (\AA^3)}$')
        axes[0,0].errorbar(results['vo'],results['ko'], xerr=vo_unc, yerr=ko_unc,\
                        linestyle='None', marker='None', label=sample,color='white', capsize = 3.,elinewidth=1.5)
        axes[0, 0].set_ylabel('$\mathbf{K_0 (GPa)}$')
        axes[0, 0].tick_params(direction='in',
                               bottom=1,
                               top=1,
                               left=1,
                               right=1)

        cx, cy, grid = conf_interval2d(mini, out2, 'ko', 'kp', 80, 80)
        ctp = axes[1, 1].contourf(cx, cy, grid, np.linspace(0, 1, 100))
        #fig.colorbar(ctp, ax=axes[1])
        axes[1,1].errorbar(results['ko'],results['kp'], xerr=ko_unc, yerr=kp_unc,\
                        linestyle='None', marker='None', label=sample,color='white', capsize = 3.,elinewidth=1.5)
        axes[1, 1].set_xlabel('$\mathbf{K_0 (GPa)}$')
        #axes[1,1].set_ylabel('$\mathbf{K_0}$\'')
        axes[1, 1].tick_params(direction='in',
                               bottom=1,
                               top=1,
                               left=1,
                               right=1)

        axes[0, 1].set_axis_off()
    return
コード例 #15
0
ファイル: example_ci2.py プロジェクト: Tillsten/lmfit-py
out.leastsq()
ci, trace = conf_interval(out, trace=True)


names=fit_params.keys()

if HASPYLAB:
    pylab.rcParams['font.size']=8
    pylab.plot(x,data)
    pylab.figure()
    cm=pylab.cm.coolwarm
    for i in range(4):
        for j in range(4):
            pylab.subplot(4,4,16-j*4-i)
            if i!=j:
                x,y,m = conf_interval2d(out,names[i],names[j],20,20)
                pylab.contourf(x,y,m,np.linspace(0,1,10),cmap=cm)
                pylab.xlabel(names[i])
                pylab.ylabel(names[j])

                x=trace[names[i]][names[i]]            
                y=trace[names[i]][names[j]]
                pr=trace[names[i]]['prob']
                s=np.argsort(x)
                pylab.scatter(x[s],y[s],c=pr[s],s=30,lw=1, cmap=cm)
            else:
                x=trace[names[i]][names[i]]            
                y=trace[names[i]]['prob']

                t,s=np.unique(x,True)                       
                f=interp1d(t,y[s],'slinear')
コード例 #16
0
# Nelder-Mead solution as a starting point
out2 = mini.minimize(method='leastsq', params=out1.params)

lmfit.report_fit(out2.params, min_correl=0.5)

ci, trace = lmfit.conf_interval(mini, out2, sigmas=[0.68,0.95],
                                trace=True, verbose=False)
lmfit.printfuncs.report_ci(ci)

plot_type = 2
if plot_type == 0:
    plt.plot(x, y)
    plt.plot(x, residual(out2.params)+y )

elif plot_type == 1:
    cx, cy, grid = lmfit.conf_interval2d(mini, out2, 'a2','t2',30,30)
    plt.contourf(cx, cy, grid, np.linspace(0,1,11))
    plt.xlabel('a2')
    plt.colorbar()
    plt.ylabel('t2')

elif plot_type == 2:
    cx, cy, grid = lmfit.conf_interval2d(mini, out2, 'a1','t2',30,30)
    plt.contourf(cx, cy, grid, np.linspace(0,1,11))
    plt.xlabel('a1')
    plt.colorbar()
    plt.ylabel('t2')


elif plot_type == 3:
    cx1, cy1, prob = trace['a1']['a1'], trace['a1']['t2'],trace['a1']['prob']
コード例 #17
0
ファイル: sb_fitting_utils.py プロジェクト: rsuhada/code
def fit_v06_model_joint(r, sb_src, sb_src_err, instruments, theta, energy, results_pickle=None):
    """
    Fit a v06 x psf model to any combination of instruments via joint
    likelihood

    Arguments:
    """

    # settings
    APPLY_PSF = True
    DO_ZERO_PAD = True
    DO_FIT = True
    FIT_METHOD = 'simplex'
    # FIT_METHOD = 'leastsq'     # 'leastsq' - Levemberg-Markquardt,
                               # 'simplex' - simplex
    CALC_1D_CI = False         # in most cases standard error is good
                               # enough, this is not needed then
    CALC_2D_CI = False
    PLOT_PROFILE = True
    PRINT_FIT_DIAGNOSTICS = True

    ######################################################################
    # modelling is done in 2D and then projected - setup here the 2D
    # parameters

    size = 2.0 * r.max()
    xsize = size
    ysize = xsize
    xcen = xsize/2
    ycen = ysize/2
    # imsize = input_im.shape         # FIXME: is this necessary? I could just use it inside the model
    imsize = (size, size)         # FIXME: is this necessary? I could just use it inside the model

    xsize_obj = xsize # 100             # if running t1.fits set to 100 else xsize
    ysize_obj = xsize_obj
    xcen_obj = xsize_obj / 2
    ycen_obj = ysize_obj / 2
    r_aper = xsize_obj  / 2        # aperture for the fitting

    # pre-calculate distmatrix for speedup - it is same for all
    # instruments
    distmatrix = distance_matrix(zeros((imsize[0]-2, imsize[1]-2)), xcen_obj, ycen_obj).astype(int) # need int for bincount

    # set the ancilarry parameters
    # +1 bc of the central divergence
    data = empty(imsize)
    distmatrix_input = distance_matrix(data, xcen_obj, ycen_obj).astype('int') + 1
    bgrid = unique(distmatrix_input.flat)

    # r contains the start of the innermost bin for integration, but not needed for plotting
    rplt = r[1:]

    ######################################################################
    # scale the data

    # scale_sb_src = {}
    # scale_sb_src_err = {}
    psf_dict = {}
    ndata = 0

    for instrument in instruments:
        ndata += len(sb_src[instrument])
        psf_dict[instrument] = make_2d_king(distmatrix_input, instrument, theta[instrument], energy)

    ######################################################################
    # init beta model

    n0 = 1e+0
    rc = 20.0
    beta = 4.0/3.0
    rs = 20.0
    alpha = 1.5
    gamma = 3.0
    epsilon = 1.5
    r500_pix = r.max()

    # v06 pars lmfit structure
    pars = lm.Parameters()
    pars.add('rc'      , value=rc, vary=True, min=0.05, max=r.max())
    pars.add('beta'    , value=beta, vary=True, min=0.05, max=2.0)
    pars.add('rs'      , value=rs, vary=True, min=0.05, max=2*r.max())
    pars.add('alpha'   , value=alpha, vary=True, min=0.01, max=3.0)
    pars.add('epsilon' , value=epsilon, vary=True, min=0.0, max=5.0)
    pars.add('gamma'   , value=gamma, vary=False)

    # FIXME: reasonable initial value and bounds!
    for instrument in instruments:
        pars.add('n0_'+instrument, value=n0, #value=mean(sb_src[instrument]),
                 vary=True, min=1.0e-9, max=1.0e3)

    # non-fit arguments
    nonfit_args = (distmatrix_input, bgrid, r500_pix, instruments, psf_dict,
                   xcen_obj, ycen_obj, r, sb_src, sb_src_err)

    # fit stop criteria
    if FIT_METHOD == 'leastsq':
        # leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfev': 1.0e+0} # debug set; quickest
        # leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfev': 1.0e+4} # debug set; some evol
        leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfev': 1.0e+7}
        # leastsq_kws={'xtol': 1.0e-8, 'ftol': 1.0e-8, 'maxfev': 1.0e+9}

    if FIT_METHOD == 'simplex':
        # leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfun': 1.0e+1} # debug set; quickest
        # leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfun': 1.0e+4} # debug set; some evol
        leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfun': 1.0e+7}
        # leastsq_kws={'xtol': 1.0e-8, 'ftol': 1.0e-8, 'maxfun': 1.0e+9}

    ######################################################################
    # do the fit: beta

    if DO_FIT:
        print "starting v06 fit with method :: ", FIT_METHOD
        t1 = time.clock()

        result = lm.minimize(v06_psf_2d_lmfit_profile_joint,
                             pars,
                             args=nonfit_args,
                             method=FIT_METHOD,
                             **leastsq_kws)

        t2 = time.clock()

        print
        print
        print "fitting took: ", t2-t1, " s"
        print
        print

        ######################################################################
        # get the output model

        (r_model, profile_norm_model) = \
            v06_psf_2d_lmfit_profile_joint(pars, distmatrix_input, bgrid,
                                           r500_pix, instruments, psf_dict,
                                           xcen_obj, ycen_obj)

        ######################################################################
        # save structures

        if results_pickle:
            outstrct = lmfit_result_to_dict(result, pars)

            with open(results_pickle, 'wb') as output:
                pickle.dump(outstrct, output, pickle.HIGHEST_PROTOCOL)

                print "results written to:: ", results_pickle

        ######################################################################
        # output

        if PRINT_FIT_DIAGNOSTICS:
            print_fit_diagnostics(result, t2-t1, ndata, leastsq_kws)

        # print_result_tab(pars_true, pars)
        lm.printfuncs.report_errors(result.params)

        with open(results_pickle+'.txt', 'w') as f:
            sys.stdout = f

            if PRINT_FIT_DIAGNOSTICS:
                print_fit_diagnostics(result, t2-t1, ndata, leastsq_kws)

            print
            print
            lm.printfuncs.report_errors(result.params)
            print
            print

            sys.stdout = sys.__stdout__

        print
        print "fitting subroutine done!"

    ######################################################################
    # plot beta fit and data profiles

    if DO_FIT and PLOT_PROFILE:
        for instrument in instruments:
            output_figure = results_pickle+'.'+instrument+'.beta_psf.png'

            print "result plot :: ", output_figure

            # FIXME: implement plotter for joint fits
            plot_data_model_resid(rplt, sb_src[instrument],
                              r_model, profile_norm_model[instrument],
                              output_figure, sb_src_err[instrument])

    ######################################################################
    # calculate confidence intervals

    if DO_FIT and CALC_1D_CI:
        print "Calculating 1D confidence intervals"
        sigmas = [0.682689492137, 0.954499736104, 0.997300203937]
        # sigmas = [0.682689492137, 0.954499736104]
        # sigmas = [0.997300203937]
        # sigmas = [0.954499736104]
        # sigmas = [0.682689492137]
        # ci_pars = ['rc', 'beta']
        # ci_pars = ['rc']
        # ci_pars = ['n0_pn', 'rc']
        ci_pars = ['n0_'+instruments[0]]

        t1 = time.clock()
        ci, trace = lm.conf_interval(result, p_names=ci_pars, sigmas=sigmas,
                                     trace=True, verbose=True, maxiter=1e3)

        t2 = time.clock()

        # save to file
        with open(results_pickle+'.ci', 'wb') as output:
            pickle.dump(ci, output, pickle.HIGHEST_PROTOCOL)

        print
        print "Confidence interval calculation took : ", t2 - t1

        lm.printfuncs.report_ci(ci)

    ######################################################################
    # FIXME: not done yet: Calculate 2D confidence intervals

    if DO_FIT and  CALC_2D_CI:
        output_figure = results_pickle+'.2d_like_beta_psf.png'
        from timer import Timer

        with Timer() as t:
            print "Calculating 2D confidence intervals"
            x, y, likelihood = lm.conf_interval2d(result,'rcore','beta', 10, 10)
            plt_like_surface(x, y, likelihood, output_figure, 'rcore', 'beta')

        print "elasped time:", t.secs, " s"

    return 0
コード例 #18
0
    # Note that the limits are not symmetric
    print('Computed 95 percent percentiles from the Monte Carlo run:')
    for i in range(nParameters):
        print(toFit[i] + ': ', mean[i], "+/- ", plus[i], minus[i])

    print('Time for monte carlo = ', time.time() - to)
    print('Number of simulations = ', nsims)

if False:
    to = time.time()
    ci = lmfit.conf_interval(minimizer, result)
    print('Time for conf Interval = ', time.time() - to)
    lmfit.printfuncs.report_ci(ci)
    #
    cx, cy, grid = lmfit.conf_interval2d(minimizer, result, toFit[0], toFit[1],
                                         100, 100)
    plt.contourf(cx, cy, grid, np.linspace(0, 1, 11))
    plt.xlabel(toFit[0])
    plt.colorbar()
    plt.ylabel(toFit[1])

if False:
    np.set_printoptions(precision=4, linewidth=150)

    print(result.covar)

    to = time.time()
    res = lmfit.minimize(residuals,
                         method='emcee',
                         nan_policy='omit',
                         burn=500,
コード例 #19
0
    plt.plot (k1Sample, k2Sample, '.', color='cornflowerblue')
    plt.xlabel ('k1'); plt.ylabel ('k2')
    plt.title ('Scatter plot of k1 against k2')
    plt.savefig('k1_k2_scatter.pdf')
    plt.show()
print ('Time for monte carlo = ', time.time() - to)


# Chi Square Analysis
if False:
    to = time.time()
    ci = lmfit.conf_interval(minimizer, result)
    print ('Time for conf Interval = ', time.time() - to)
    lmfit.printfuncs.report_ci(ci)
    #
    cx, cy, grid = lmfit.conf_interval2d(minimizer, result, 'k1', 'k2', 100, 100)
    plt.contourf(cx, cy, grid, np.linspace(0, 1, 11))
    plt.xlabel('k1')
    plt.colorbar()
    plt.ylabel('k2')

# MCMC Analysis
if False:
    np.set_printoptions(precision=4, linewidth=150)
    
    print (result.covar)  
    
    from tqdm import tqdm
    to = time.time()
    res = minimizer.emcee(params=params,burn=500, steps=5000, thin=20,is_weighted=False,progress=True)
    #res = lmfit.minimize(residuals, method='emcee', nan_policy='omit', burn=500, steps=5000, thin=20, progress=True,
コード例 #20
0
ファイル: test_lmfit.py プロジェクト: rsuhada/code
def plot_conf(result):
    x, y, grid=lm.conf_interval2d(result,'a','b',30,30)
    plt.contourf(x,y,grid,np.linspace(0,1,11))
    plt.xlabel('a');
    plt.colorbar();
    plt.ylabel('b');
コード例 #21
0
ファイル: doc_confidence2.py プロジェクト: yoavram/lmfit-py
lmfit.printfuncs.report_fit(mi.params, min_correl=0.5)

ci, trace = lmfit.conf_interval(mi,
                                sigmas=[0.68, 0.95],
                                trace=True,
                                verbose=False)
lmfit.printfuncs.report_ci(ci)

plot_type = 3
if plot_type == 0:
    plt.plot(x, y)
    plt.plot(x, residual(p) + y)

elif plot_type == 1:
    cx, cy, grid = lmfit.conf_interval2d(mi, 'a2', 't2', 30, 30)
    plt.contourf(cx, cy, grid, np.linspace(0, 1, 11))
    plt.xlabel('a2')
    plt.colorbar()
    plt.ylabel('t2')

elif plot_type == 2:
    cx, cy, grid = lmfit.conf_interval2d(mi, 'a1', 't2', 30, 30)
    plt.contourf(cx, cy, grid, np.linspace(0, 1, 11))
    plt.xlabel('a1')
    plt.colorbar()
    plt.ylabel('t2')

elif plot_type == 3:
    cx1, cy1, prob = trace['a1']['a1'], trace['a1']['t2'], trace['a1']['prob']
    cx2, cy2, prob2 = trace['t2']['t2'], trace['t2']['a1'], trace['t2']['prob']
コード例 #22
0
def fit_plots(x, y, minimizer, minimizer_result, residual,
              model, title='',
              contour_level=0.6827, cmap=plt.cm.coolwarm,
              xlabel='x', ylabel='y', xlim=None, ylim=None):

    params = minimizer_result.params

    ci, trace = lmfit.conf_interval(minimizer, minimizer_result, trace=True)
    param_names = list(params.keys())

    figs = []

    ##################################
    # Figure 1: Fit Parameters as text
    ##################################

    fig = plt.figure()

    s = '%s\n\n' % model
    if title:
        s += title + '\n'
    for ndx, k in enumerate(param_names):
        s += '%s: %.3f ± %.3f' % (k, minimizer_result.params[k].value, minimizer_result.params[k].stderr)
        s += '\n'

    plt.text(0.5, 0.5, s, fontsize=12, ha='center', va='center')
    plt.axis('off')

    figs.append(fig)

    #############################
    # Figure 2: Fit and residuals
    #############################

    fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, gridspec_kw={'height_ratios': [3, 1]})

    ax1.plot(x, y, '.')
    xs = np.linspace(x[0], x[-1])
    ax1.plot(xs, residual(minimizer_result.params, xs), '-')
    ax1.set_ylabel(ylabel)
    if ylim:
        ax1.set_ylim(ylim)

    r = residual(minimizer_result.params, x, data=y)
    ax2.plot(x, r)
    ax2.axhline(y=0, color='k', linestyle=':')
    mx = np.max(np.abs(r))
    ax2.set_ylim([-mx, mx])
    ax2.set_ylabel('R')
    ax2.set_xlabel(xlabel)

    if xlim:
        ax2.set_xlim(xlim)

    figs.append(fig)

    #############################
    # Figure 3: Probability plots
    #############################

    contours = {}

    fig, axs = plt.subplots(len(param_names), len(param_names))

    for ndx1 in range(len(param_names)):
        for ndx2 in range(len(param_names)):
            ax = axs[ndx2][ndx1]

            if ndx1 > ndx2:
                ax.set_axis_off()
                continue

            if ndx1 == ndx2:
                x = trace[param_names[ndx1]][param_names[ndx1]]
                y = trace[param_names[ndx1]]['prob']

                t, s = np.unique(x, True)
                f = interp1d(t, y[s], 'slinear')
                xn = np.linspace(x.min(), x.max(), 50)
                ax.plot(xn, f(xn), 'g', lw=1)

                contours[ndx1] = (x, y)

            else:
                x, y, m = lmfit.conf_interval2d(minimizer, minimizer_result, param_names[ndx1], param_names[ndx2], 20, 20)
                ax.contourf(x, y, m, np.linspace(0, 1, 10), cmap=cmap)

                ch = QuadContourGenerator.from_rectilinear(x, y, m, numpy_formatter)

                contours[(ndx1, ndx2)] = ch.contour(contour_level)

            if ndx1 == 0:
                if ndx2 > 0:
                    ax.set_ylabel(param_names[ndx2])
                else:
                    ax.set_ylabel('prob')
            else:
                ax.set_yticks([])

            if ndx2 == len(param_names) - 1:
                ax.set_xlabel(param_names[ndx1])
            else:
                ax.set_xticks([])

    figs.append(fig)

    return figs, contours
コード例 #23
0
ファイル: sb_fitting_utils.py プロジェクト: rsuhada/code
def fit_v06_model(r, sb_src, sb_src_err, instrument, theta, energy, results_pickle=None):
    """
    Fit of v06 model with psf convolution
    """
    # settings
    APPLY_PSF = True
    DO_ZERO_PAD = True
    DO_FIT = True
    FIT_METHOD = 'simplex'
    # FIT_METHOD = 'leastsq'     # 'leastsq' - Levemberg-Markquardt,
                                 # 'simplex' - simplex
    CALC_1D_CI = True           # in most cases standard error is good
                                # enough, this is not needed then
    CALC_2D_CI = False
    PLOT_PROFILE = True
    PRINT_FIT_DIAGNOSTICS = True

    ######################################################################
    # modelling is done in 2D and then projected - setup here the 2D
    # parameters

    size = 2.0 * r.max()
    xsize = size
    ysize = xsize
    xcen = xsize/2
    ycen = ysize/2
    # imsize = input_im.shape     # FIXME: is this necessary? I could just use it inside the model
    imsize = (size, size)         # FIXME: is this necessary? I could just use it inside the model

    xsize_obj = xsize # 100             # if running t1.fits set to 100 else xsize
    ysize_obj = xsize_obj
    xcen_obj = xsize_obj / 2
    ycen_obj = ysize_obj / 2
    r_aper = xsize_obj  / 2        # aperture for the fitting

    ######################################################################
    # init model

    n0 = 1.0
    rc = 20.0
    beta = 4.0/3.0
    rs = 20.0
    alpha = 1.5
    gamma = 3.0
    epsilon = 1.5

    # rmax = 2*r500_pix
    r500_pix = r.max()
    ndata = len(sb_src)

    # v06 pars lmfit structure
    pars = lm.Parameters()
    pars.add('n0_'+instrument, value=n0, vary=True, min=1.0e-9, max=1.0e3)
    pars.add('rc'      , value=rc, vary=True, min=0.05, max=r.max())
    pars.add('beta'    , value=beta, vary=True, min=0.05, max=2.0)
    pars.add('rs'      , value=rs, vary=True, min=0.05, max=2*r.max())
    pars.add('alpha'   , value=alpha, vary=True, min=0.01, max=3.0)
    pars.add('epsilon' , value=epsilon, vary=True, min=0.0, max=5.0)
    pars.add('gamma'   , value=gamma, vary=False)

    # set the ancilarry parameters
    # +1 bc of the central divergence
    data = empty(imsize)
    distmatrix_input = distance_matrix(data, xcen_obj, ycen_obj).astype('int') + 1
    bgrid = unique(distmatrix_input.flat)

    ######################################################################
    # do the fit: v06

    nonfit_args = (distmatrix_input, bgrid, r500_pix, instrument, theta, energy,
                   xcen_obj, ycen_obj, r, sb_src, sb_src_err)

    # fit stop criteria
    if FIT_METHOD == 'leastsq':
        leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfev': 1.0e+0} # debug set; quickest
        # leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfev': 1.0e+4} # debug set; some evol
        # leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfev': 1.0e+7}
        # leastsq_kws={'xtol': 1.0e-8, 'ftol': 1.0e-8, 'maxfev': 1.0e+9}

    if FIT_METHOD == 'simplex':
        # leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfun': 1.0e+0} # debug set; quickest
        # leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfun': 1.0e+4} # debug set; some evol
        leastsq_kws={'xtol': 1.0e-7, 'ftol': 1.0e-7, 'maxfun': 1.0e+7}
        # leastsq_kws={'xtol': 1.0e-8, 'ftol': 1.0e-8, 'maxfun': 1.0e+9}

    ######################################################################
    # do the actual fitting

    if DO_FIT:
        print "starting v06 fit with method :: ", FIT_METHOD
        t1 = time.clock()

        result = lm.minimize(v06_psf_2d_lmfit_profile,
                             pars,
                             args=nonfit_args,
                             method=FIT_METHOD,
                             **leastsq_kws)

        t2 = time.clock()
        print "fitting took: ", t2-t1, " s"

        # get the output model
        (r_model, profile_norm_model) = v06_psf_2d_lmfit_profile(pars,
                                                                 distmatrix_input,
                                                                 bgrid,
                                                                 r500_pix,
                                                                 instrument, theta, energy,
                                                                 xcen_obj,
                                                                 ycen_obj)

        ######################################################################
        # save structures

        if results_pickle:
            outstrct = lmfit_result_to_dict(result, pars)

            with open(results_pickle, 'wb') as output:
                pickle.dump(outstrct, output, pickle.HIGHEST_PROTOCOL)

                print "results written to:: ", results_pickle

        ######################################################################
        # output

        if PRINT_FIT_DIAGNOSTICS:
            print_fit_diagnostics(result, t2-t1, ndata, leastsq_kws)

        # print_result_tab(pars_true, pars)
        lm.printfuncs.report_errors(result.params)

        with open(results_pickle+'.txt', 'w') as f:
            sys.stdout = f

            if PRINT_FIT_DIAGNOSTICS:
                print_fit_diagnostics(result, t2-t1, ndata, leastsq_kws)

            print
            print
            lm.printfuncs.report_errors(result.params)
            print
            print

            sys.stdout = sys.__stdout__

        print
        print "fitting subroutine done!"

    ######################################################################
    # plot v06 fit and data profiles

    if DO_FIT and PLOT_PROFILE:
        output_figure = results_pickle+'.'+instrument+'.v06_psf.png'

        print "result plot :: ", output_figure

        # FIXME: implement plotter for joint fits
        plot_data_model_resid(r, sb_src,
                              r_model, profile_norm_model,
                              output_figure, sb_src_err)

    ######################################################################
    # FIXME: not yet ported, confidence intervals

    if DO_FIT and CALC_1D_CI:
        print "Calculating 1D confidence intervals"
        # sigmas = [0.682689492137, 0.954499736104, 0.997300203937]
        sigmas = [0.682689492137, 0.954499736104]
        # just an example
        ci_pars = ['rc', 'beta']

        ci, trace = lm.conf_interval(result, p_names=ci_pars, sigmas=sigmas,
                              trace=True, verbose=True, maxiter=1e3)

        lm.printfuncs.report_ci(ci)

    # FIXME: this seems to fail for beta and rc (i think problem is
    # due to parameter degeneracy no code issue)
    if DO_FIT and  CALC_2D_CI:
        output_figure = results_pickle+'.2d_like_v06_psf.png'
        from timer import Timer

        with Timer() as t:
            print "Calculating 2D confidence intervals"
            x, y, likelihood = lm.conf_interval2d(result,'rc','beta', 10, 10)
            plt_like_surface(x, y, likelihood, output_figure, 'rc', 'beta')

        print "elasped time:", t.secs, " s"


    # import IPython
    # IPython.embed()

    return 0
コード例 #24
0
        ax.ticklabel_format(style='sci', scilimits=(-2, 2), axis='y')

        # set-up labels and tick marks
        ax.tick_params(labelleft=False, labelbottom=False)
        if indx in (2, 5, 9, 13):
            plt.ylabel(names[j])
            ax.tick_params(labelleft=True)
        if indx == 1:
            ax.tick_params(labelleft=True)
        if indx in (13, 14, 15, 16):
            plt.xlabel(names[i])
            ax.tick_params(labelbottom=True)
            [label.set_rotation(45) for label in ax.get_xticklabels()]

        if i != j:
            x, y, m = conf_interval2d(mini, out, names[i], names[j], 20, 20)
            plt.contourf(x, y, m, linspace(0, 1, 10))

            x = tr[names[i]][names[i]]
            y = tr[names[i]][names[j]]
            pr = tr[names[i]]['prob']
            s = argsort(x)
            plt.scatter(x[s], y[s], c=pr[s], s=30, lw=1)

        else:
            x = tr[names[i]][names[i]]
            y = tr[names[i]]['prob']

            t, s = unique(x, True)
            f = interp1d(t, y[s], 'slinear')
            xn = linspace(x.min(), x.max(), 50)
コード例 #25
0
plt.title('M8')
levels = [0.6827, 0.9545, 0.9973]
colors = ["g", "y", "r"]

for ax, [xvar, yvar] in zip(axes.flat, [
    ["s0", "noise"],
    ["r0", "m"],
    ["m", "s0"],
    ["r0", "s0"],
]):
    cx, cy, grid = lmfit.conf_interval2d(
        result2,
        result2,
        xvar,
        yvar,
        30,
        30,
        limits=[plot_limits[xvar], plot_limits[yvar]],
    )
    ctp = ax.contour(cx, cy, grid, levels, colors=colors)
    ax.set_xlabel(xvar)
    ax.set_ylabel(yvar)

fig.tight_layout()
# -

x = r
y = B - 2 * sig2
tck = interpolate.splrep(x, y, s=0)
grid = np.linspace(x.min(), x.max(), num=len(x))
コード例 #26
0
# %%% Contours %%%%

ci, trace = lmfit.conf_interval(mini,
                                out2,
                                sigmas=[0.68, 0.95],
                                trace=True,
                                verbose=False)

lmfit.printfuncs.report_ci(ci)

# %%% b versus a %%%

cx, cy, grid = lmfit.conf_interval2d(mini,
                                     out2,
                                     'a',
                                     'b',
                                     nx=60,
                                     ny=60,
                                     limits=((5.2e-5, 6.8e-5), (0.0908,
                                                                0.0882)))

plt.contourf(1.e5 * cx,
             1.e2 * cy,
             grid, [0., 0.68, 0.95, 0.99],
             cmap='inferno')

plt.xlabel(r'$\alpha \times 10^5 \,\, (\mathrm{year}^{-1})$')
plt.colorbar()
plt.ylabel(r'$\beta \times 10^2 \,\, (\mathrm{year}^{-1})$')

plt.tight_layout()
plt.savefig('./plots/plot_contours_alpha_beta.pdf')