Beispiel #1
0
def calc_wstat_sherpa(mu_sig, n_on, n_off, alpha):
    import sherpa.stats as ss
    from sherpa.astro.data import DataPHA
    from sherpa.models import Const1D
    wstat = ss.WStat()

    model = Const1D()
    model.c0 = mu_sig
    data = DataPHA(counts=np.atleast_1d(n_on),
                   name='dummy',
                   channel=np.atleast_1d(1),
                   backscal=1,
                   exposure=1)
    background = DataPHA(counts=np.atleast_1d(n_off),
                         name='dummy background',
                         channel=np.atleast_1d(1),
                         backscal=np.atleast_1d(1. / alpha),
                         exposure=1)

    data.set_background(background, 1)

    # Docstring for ``calc_stat``
    # https://github.com/sherpa/sherpa/blob/fe8508818662346cb6d9050ba676e23318e747dd/sherpa/stats/__init__.py#L219

    stat = wstat.calc_stat(model=model, data=data)
    print("Sherpa stat: {}".format(stat[0]))
    print("Sherpa fvec: {}".format(stat[1]))
def test_case(args):
    import sherpa.stats as ss

    mu_sig = float(args[1])
    n_on = float(args[2])
    n_off = float(args[3])
    alpha = float(args[4])

    model, data = get_data(mu_sig, n_on, n_off, alpha)
    wstat, cash, cstat = ss.WStat(), ss.Cash(), ss.CStat()

    print('wstat: {}'.format(wstat.calc_stat(model=model, data=data)[0]))
    print('cash: {}'.format(cash.calc_stat(model=model, data=data)[0]))
    print('cstat: {}'.format(cstat.calc_stat(model=model, data=data)[0]))
Beispiel #3
0
def calc_wstat_sherpa():
    data, model, staterr, off_vec, alpha = get_test_data()
    import sherpa.stats as ss
    wstat = ss.WStat()

    # We assume equal exposure
    bkg = dict(bkg=off_vec,
               exposure_time=[1, 1],
               backscale_ratio=1. / alpha,
               data_size=len(model))

    stat = wstat.calc_stat(data, model, staterror=staterr, bkg=bkg)
    print("Sherpa stat: {}".format(stat[0]))
    print("Sherpa fvec: {}".format(stat[1]))
Beispiel #4
0
    def statistic(self, stat):
        import sherpa.stats as s

        if isinstance(stat, six.string_types):
            if stat.lower() == 'cstat':
                stat = s.CStat()
            elif stat.lower() == 'wstat':
                stat = s.WStat()
            else:
                raise ValueError("Undefined stat string: {}".format(stat))

        if not isinstance(stat, s.Stat):
            raise ValueError("Only sherpa statistics are supported")

        self._stat = stat
Beispiel #5
0
def test_wstat():
    import sherpa.stats as ss
    sherpa_stat = ss.WStat()
    data, model, staterror, off_vec = get_test_data()
    alpha = np.ones(len(data)) * 0.2

    statsvec = gammapy_stats.wstat(n_on=data,
                                   mu_sig=model,
                                   n_off=off_vec,
                                   alpha=alpha,
                                   extra_terms=True)

    # This is how sherpa wants the background (found by trial and error)
    bkg = dict(bkg=off_vec,
               exposure_time=[1, 1],
               backscale_ratio=1. / alpha,
               data_size=len(data))

    # Check for one bin first
    test_bin = 0
    bkg_testbin = dict(bkg=off_vec[test_bin],
                       exposure_time=[1, 1],
                       backscale_ratio=1. / alpha[test_bin],
                       data_size=1)

    desired_testbin, fvec = sherpa_stat.calc_stat(
        data[test_bin],
        model[test_bin],
        staterror=staterror[test_bin],
        bkg=bkg_testbin)

    actual_testbin = statsvec[test_bin]
    #    assert_allclose(actual_testbin, desired_testbin)

    # Now check total stat for all bins
    desired, fvec = sherpa_stat.calc_stat(data,
                                          model,
                                          staterror=staterror,
                                          bkg=bkg)

    actual = np.sum(statsvec)
    print(fvec)
    assert_allclose(actual, desired)
Beispiel #6
0
    def statistic(self, stat):
        """Set Statistic to be used in the fit

        Parameters
        ----------
        stat : `~sherpa.stats.Stat`, str
            Statistic
        """
        import sherpa.stats as s

        if isinstance(stat, six.string_types):
            if stat == 'cash':
                stat = s.Cash()
            elif stat == 'wstat':
                stat = s.WStat()
            else:
                raise ValueError("Undefined stat string: {}".format(stat))

        if not isinstance(stat, s.Stat):
            raise ValueError("Only sherpa statistics are supported")

        self._stat = stat
# clear.
#
@requires_data
@requires_fits
@pytest.mark.parametrize("stat",
                         [None,
                          stats.Chi2(),
                          stats.Chi2ConstVar(),
                          stats.Chi2DataVar(),
                          stats.Chi2Gehrels(),
                          stats.Chi2ModVar(),
                          stats.Chi2XspecVar(),
                          stats.LeastSq(),
                          stats.Cash(),
                          stats.CStat(),
                          stats.WStat()])
def test_astro_data_plot_with_stat_simple(make_data_path, stat):

    from sherpa.astro import io

    infile = make_data_path('3c273.pi')
    pha = io.read_pha(infile)

    # tweak the data set so that we aren't using the default
    # options (it shouldn't matter for this test but just
    # in case).
    #
    # Note that background subtraction would normally be an issue
    # for some of the stats (e.g. WStat), but this shouldn't
    # trigger a problem here.
    #
Beispiel #8
0
# clear.
#
@requires_data
@requires_fits
@pytest.mark.parametrize("stat", [
    None,
    stats.Chi2(),
    stats.Chi2ConstVar(),
    stats.Chi2DataVar(),
    stats.Chi2Gehrels(),
    stats.Chi2ModVar(),
    stats.Chi2XspecVar(),
    stats.LeastSq(),
    stats.Cash(),
    stats.CStat(),
    stats.WStat(),
])
def test_astro_data_plot_with_stat_simple(make_data_path, stat):

    from sherpa.astro import io

    infile = make_data_path('3c273.pi')
    pha = io.read_pha(infile)

    # tweak the data set so that we aren't using the default
    # options (it shouldn't matter for this test but just
    # in case).
    #
    # Note that background subtraction would normally be an issue
    # for some of the stats (e.g. WStat), but this shouldn't
    # trigger a problem here.