Esempio n. 1
0
def get_parameter_info(id=None):
    """Returns the parameter information needed for calling mht.
    
    This routine will call covariance() if needed, but not
    fit().

    For now only works with a single dataset, and requires
    the Cash statistic.
    """

    if id == None:
        idval = ui.get_default_id()
    else:
        idval = id

    # Employ a lot of safety checks
    #
    fr = ui.get_fit_results()
    if fr == None:
        raise RuntimeError, "No fit results available!"
    if len(fr.datasets) != 1:
        raise RuntimeError, "Fit is for multiple datasets (%s) which we do not support!" % fr.datasets
    if fr.datasets[0] != idval:
        raise RuntimeError, "Fit results are for dataset %s, not %s" % (fr.datasets[0], idval)
    if fr.statname != "cash":
        raise RuntimeError, "Fit was run using statistic=%s rather than cash!" % fr.statname
    if not fr.succeeded:
        # Should use standard sherpa logging
        print "Warning: fit to dataset %s did not complete successfully:\n%s" % (idval, fr.message)

    cr = ui.get_covar_results()
    if cr == None or len(cr.datasets) != 1 or cr.datasets[0] != idval:
        # Should use standard sherpa logging
        print "Running covariance for dataset %s" % idval
        ui.covariance(idval)
        cr = ui.get_covar_results()

    if cr.statname != "cash":
        raise RuntimeError, "Covariance was run using statistic=%s rather than cash!" % cr.statname

    if len(fr.parnames) != len(cr.parnames):
        raise RuntimeError, "Number of parameters used in fit and covariance analysis do not agree!\n  fit=%s\n  covar=%s\n" % (
            fr.parnames,
            cr.parnames,
        )
    for (p1, p2) in zip(fr.parnames, cr.parnames):
        if p1 != p2:
            raise RuntimeError, "Order of fit/covariance parameters does not match: %s vs %s" % (p1, p2)
    for (pname, v1, v2) in zip(fr.parnames, fr.parvals, cr.parvals):
        if v1 != v2:
            raise RuntimeError, "Value of fit/covariance parameters does not match for parameter %s: %g vs %g" % (
                pname,
                v1,
                v2,
            )

    if not hasattr(cr, "extra_output") or cr.extra_output == None:
        raise RuntimeError, "get_covar_results has no .extra_output or it is None; is this CIAOX?"

    # Store the information, we explicitly copy all items to avoid
    # problems if fit/covariance are run again. This is done by converting
    # all tuples to numpy arrays, even for strings, and is actually
    # not needed.
    #
    out = {
        "dataset": idval,
        "npars": len(fr.parnames),
        "parnames": np.asarray(fr.parnames),
        "parvals": np.asarray(fr.parvals),
        "parmins": np.asarray(cr.parmins),
        "parmaxes": np.asarray(cr.parmaxes),
        "sigma": cr.sigma,
        "covar": cr.extra_output.copy(),
        "statval": fr.statval,
    }
    return out
Esempio n. 2
0
    b1, b2 = pars
    return BoxBOD.model(x, b1, b2)

ui.load_user_model(boxbod_func, "boxbod")
ui.add_user_pars("boxbod", ["b1", "b2"])
bb = boxbod
ui.set_model(bb)
bb.b1, bb.b2 = BoxBOD.p0[0], BoxBOD.p0[1]

# Perform fit
ui.set_stat('chi2datavar')
#ui.set_method('levmar') #  ['levmar', 'moncar', 'neldermead', 'simplex']
ui.set_method_opt('xtol', 1e-10)
ui.fit() # Compute best-fit parameters
ui.set_covar_opt('eps', 1e-5) # @todo: Why does this parameter have no effect
ui.covariance() # Compute covariance matrix (i.e. errors)
#ui.conf() # Compute profile errors
#ui.show_all() # Print a very nice summary of your session to less

# Report results
fr = ui.get_fit_results()
cr = ui.get_covar_results()

# Report results (we have to apply the s factor ourselves)
popt = np.array(fr.parvals)
chi2 = fr.statval
s_factor = np.sqrt(chi2 / fr.dof)
perr = s_factor * np.array(cr.parmaxes)
report_results('sherpa', popt, perr, chi2)

Esempio n. 3
0
def get_parameter_info(id=None):
    """Returns the parameter information needed for calling mht.
    
    This routine will call covariance() if needed, but not
    fit().

    For now only works with a single dataset, and requires
    the Cash statistic.
    """

    if id == None:
        idval = ui.get_default_id()
    else:
        idval = id

    # Employ a lot of safety checks
    #
    fr = ui.get_fit_results()
    if fr == None:
        raise RuntimeError, "No fit results available!"
    if len(fr.datasets) != 1:
        raise RuntimeError, "Fit is for multiple datasets (%s) which we do not support!" % fr.datasets
    if fr.datasets[0] != idval:
        raise RuntimeError, "Fit results are for dataset %s, not %s" % (
            fr.datasets[0], idval)
    if fr.statname != "cash":
        raise RuntimeError, "Fit was run using statistic=%s rather than cash!" % fr.statname
    if not fr.succeeded:
        # Should use standard sherpa logging
        print "Warning: fit to dataset %s did not complete successfully:\n%s" % (
            idval, fr.message)

    cr = ui.get_covar_results()
    if cr == None or len(cr.datasets) != 1 or cr.datasets[0] != idval:
        # Should use standard sherpa logging
        print "Running covariance for dataset %s" % idval
        ui.covariance(idval)
        cr = ui.get_covar_results()

    if cr.statname != "cash":
        raise RuntimeError, "Covariance was run using statistic=%s rather than cash!" % cr.statname

    if len(fr.parnames) != len(cr.parnames):
        raise RuntimeError, "Number of parameters used in fit and covariance analysis do not agree!\n  fit=%s\n  covar=%s\n" % (
            fr.parnames, cr.parnames)
    for (p1, p2) in zip(fr.parnames, cr.parnames):
        if p1 != p2:
            raise RuntimeError, "Order of fit/covariance parameters does not match: %s vs %s" % (
                p1, p2)
    for (pname, v1, v2) in zip(fr.parnames, fr.parvals, cr.parvals):
        if v1 != v2:
            raise RuntimeError, "Value of fit/covariance parameters does not match for parameter %s: %g vs %g" % (
                pname, v1, v2)

    if not hasattr(cr, "extra_output") or cr.extra_output == None:
        raise RuntimeError, "get_covar_results has no .extra_output or it is None; is this CIAOX?"

    # Store the information, we explicitly copy all items to avoid
    # problems if fit/covariance are run again. This is done by converting
    # all tuples to numpy arrays, even for strings, and is actually
    # not needed.
    #
    out = {
        "dataset": idval,
        "npars": len(fr.parnames),
        "parnames": np.asarray(fr.parnames),
        "parvals": np.asarray(fr.parvals),
        "parmins": np.asarray(cr.parmins),
        "parmaxes": np.asarray(cr.parmaxes),
        "sigma": cr.sigma,
        "covar": cr.extra_output.copy(),
        "statval": fr.statval
    }
    return out