Пример #1
0
def test_psf2d_data_show():
    """What happens when the kernel is a Data2D instance?"""

    y, x = np.mgrid[1:4, 1:3]
    x = x.flatten()
    y = y.flatten()
    z = np.ones(x.size)
    data2 = Data2D('data2', x, y, z)

    m = PSFModel("pdata2", data2)

    yy, xx = np.mgrid[1:10, 1:9]
    xx = xx.flatten()
    yy = yy.flatten()
    zz = np.arange(xx.size)
    dfold = Data2D('fold', xx, yy, zz)
    m.fold(dfold)

    out = str(m).split("\n")
    assert len(out) == 8
    assert out[0] == "pdata2"
    assert out[
        1] == "   Param        Type          Value          Min          Max      Units"
    assert out[
        2] == "   -----        ----          -----          ---          ---      -----"
    assert out[3] == "   pdata2.kernel frozen        data2"
    assert out[
        4] == "   pdata2.size  frozen       (6, 6)       (6, 6)       (6, 6)"
    assert out[
        5] == "   pdata2.center frozen       (3, 3)       (3, 3)       (3, 3)"
    assert out[
        6] == "   pdata2.radial frozen            0            0            1           "
    assert out[
        7] == "   pdata2.norm  frozen            1            0            1           "
Пример #2
0
def test_psf2d_model_show():
    """What happens when the kernel is a model?"""

    m = PSFModel("pmodel2", make_2d_model())

    yy, xx = np.mgrid[1:10, 1:9]
    xx = xx.flatten()
    yy = yy.flatten()
    zz = np.arange(xx.size)
    dfold = Data2D('fold', xx, yy, zz)
    m.fold(dfold)

    print(m)
    out = str(m).split("\n")
    assert len(out) == 8
    assert out[0] == "pmodel2"
    assert out[
        1] == "   Param        Type          Value          Min          Max      Units"
    assert out[
        2] == "   -----        ----          -----          ---          ---      -----"
    assert out[3] == "   pmodel2.kernel frozen         box2"
    assert out[
        4] == "   pmodel2.size frozen     (72, 72)     (72, 72)     (72, 72)"
    assert out[
        5] == "   pmodel2.center frozen     (36, 36)     (36, 36)     (36, 36)"
    assert out[
        6] == "   pmodel2.radial frozen            0            0            1           "
    assert out[
        7] == "   pmodel2.norm frozen            1            0            1           "
Пример #3
0
    def to_sherpa_data2d(self, dstype='Data2D'):
        """
        Convert image to `~sherpa.data.Data2D` or `~sherpa.data.Data2DInt` class.

        Parameters
        ----------
        dstype : {'Data2D', 'Data2DInt'}
            Sherpa data type.
        """
        from sherpa.data import Data2D, Data2DInt

        if dstype == 'Data2D':
            coordinates = self.coordinates(mode='center')
            x = coordinates.data.lon.degree
            y = coordinates.data.lat.degree
            return Data2D(self.name, x.ravel(), y.ravel(), self.data.ravel(),
                          self.data.shape)
        elif dstype == 'Data2DInt':
            coordinates = self.coordinates(mode='edges')
            x = coordinates.data.lon
            y = coordinates.data.lat
            xlo, xhi = x[:-1], x[1:]
            ylo, yhi = y[:-1], y[1:]
            return Data2DInt(self.name, xlo.ravel(), xhi.ravel(), ylo.ravel(),
                             yhi.ravel(), self.data.ravel(), self.data.shape)
        else:
            raise ValueError('Invalid sherpa data type.')
Пример #4
0
def make_2d_data():
    """We want to create a new object each call."""

    y, x = np.mgrid[1:3, 1:4]
    x = x.flatten()
    y = y.flatten()
    z = np.ones(x.size)
    return Data2D('twod', x, y, z)
Пример #5
0
def test_data2d_wrong_y_array_size(array_sizes_fixture):
    x0, x1, dx, y = array_sizes_fixture

    with pytest.raises(TypeError):
        Data2D('name',
               x0.flatten(),
               x1.flatten(),
               y,
               staterror=numpy.sqrt(y).flatten())
Пример #6
0
def test_psf2d_kernel_data(caplog):
    """Access the kernel data: no subkernel"""

    x1, x0 = np.mgrid[-5:-3, 10:13]
    s = x0.shape

    k = np.ones(s)
    k[1, 1] = 2

    x0 = x1.flatten()
    x1 = x1.flatten()
    k = k.flatten()
    d = Data2D('my-kernel', x0, x1, k, shape=s)

    m = PSFModel('x', kernel=d)

    d1, d0 = np.mgrid[5:15, -5:5]
    ds = d0.shape
    z = np.zeros(ds)
    dfold = Data2D('fold', d0.flatten(), d1.flatten(), z.flatten(), shape=ds)

    with caplog.at_level(logging.INFO, logger='sherpa'):
        ans = m.get_kernel(dfold)

    assert len(caplog.records) == 1
    r = caplog.record_tuples[0]
    assert r[0] == 'sherpa.instrument'
    assert r[1] == logging.INFO
    assert r[2] == "PSF frac: 1.0"

    assert isinstance(ans, Data2D)
    assert ans.name == 'kernel'
    assert (ans.shape == [2, 3]).all()

    # integers, so treat as exact
    assert (ans.x0 == x0).all()
    assert (ans.x1 == x1).all()

    assert ans.y == pytest.approx(k / k.sum())

    assert ans.staterror is None
    assert ans.syserror is None
Пример #7
0
def make_data(data_class):
    """Create a test data object of the given class.

    Using a string means it is easier to support the various PHA
    "types" - eg basic, grouping, grouping+quality.

    """

    x0 = np.asarray([1, 3, 7, 12])
    y = np.asarray([2, 3, 4, 5])
    if data_class == "1d":
        return Data1D('x1', x0, y)

    if data_class == "1dint":
        return Data1DInt('xint1', x0, np.asarray([3, 5, 8, 15]), y)

    chans = np.arange(1, 5)
    if data_class == "pha":
        return DataPHA('pha', chans, y)

    # We want to provide PHA tests that check out the grouping and
    # quality handling (but it is not worth trying all different
    # variants), so we have "grp" for grouping and no quality [*], and
    # "qual" for grouping and quality.
    #
    # [*] by which I mean we have not called ignore_bad, not that
    # there is no quality array.
    #
    grp = np.asarray([1, -1, 1, 1])
    qual = np.asarray([0, 0, 2, 0])
    pha = DataPHA('pha', chans, y, grouping=grp, quality=qual)
    if data_class == "grp":
        return pha

    if data_class == "qual":
        pha.ignore_bad()
        return pha

    x0 = np.asarray([1, 2, 3] * 2)
    x1 = np.asarray([1, 1, 1, 2, 2, 2])
    y = np.asarray([2, 3, 4, 5, 6, 7])
    if data_class == "2d":
        return Data2D('x2', x0, x1, y, shape=(2, 3))

    if data_class == "2dint":
        return Data2DInt('xint2', x0, x1, x0 + 1, x1 + 1, y, shape=(2, 3))

    if data_class == "img":
        return DataIMG('img', x0, x1, y, shape=(2, 3))

    if data_class == "imgint":
        return DataIMGInt('imgi', x0, x1, x0 + 1, x1 + 1, y, shape=(2, 3))

    assert False
Пример #8
0
def example_data():
    """Create an example data set."""

    x1, x0 = np.mgrid[-4:5, 6:15]

    shp = x1.shape

    x0 = x0.flatten()
    x1 = x1.flatten()
    y = 100 / (1 + np.sqrt((x0 - 11)**2 + (x1 + 2)**2))

    return Data2D('example', x0, x1, y, shape=shp)
Пример #9
0
    def get_kernel(self, data, subkernel=True):

        indep, dep, kshape, lo, hi = self._get_kernel_data(data, subkernel)

        ndim = len(kshape)
        if ndim == 1:
            dataset = Data1D('kernel', indep[0], dep)
        elif ndim == 2:
            dataset = Data2D('kernel', indep[0], indep[1], dep, kshape[::-1])
        else:
            raise PSFErr('ndim')

        return dataset
Пример #10
0
def test_datacontour(override_plot_backend):
    p = plot.DataContour()
    r = p._repr_html_()
    check_empty(r, 'DataContour', nsummary=7)

    x0 = np.asarray([[10, 20, 30], [10, 20, 30]])
    x1 = np.asarray([[5, 5, 5], [15, 15, 15]])
    y = np.arange(x0.size)
    d = Data2D('n n', x0.flatten(), x1.flatten(), y.flatten())

    p.prepare(d)
    r = p._repr_html_()
    check_full(r, 'DataContour', 'x1', 'n n', nsummary=7)
Пример #11
0
def test_psf2d_kernel_model(caplog):
    """Access the kernel data: no subkernel"""

    k = Box2D()
    k.xlow = -1
    k.xhi = 1
    k.ylow = 10
    k.yhi = 12
    m = PSFModel('x', kernel=k)

    d1, d0 = np.mgrid[5:15, -5:4]
    ds = d0.shape

    d0 = d0.flatten()
    d1 = d1.flatten()
    z = np.zeros(ds).flatten()

    dfold = Data2D('fold', d0, d1, z, shape=ds)

    with caplog.at_level(logging.INFO, logger='sherpa'):
        ans = m.get_kernel(dfold)

    assert len(caplog.records) == 1
    r = caplog.record_tuples[0]
    assert r[0] == 'sherpa.instrument'
    assert r[1] == logging.INFO
    assert r[2] == "PSF frac: 1.0"

    assert isinstance(ans, Data2D)
    assert ans.name == 'kernel'
    assert (ans.shape == [10, 9]).all()

    # integers, so treat as exact
    assert (ans.x0 == d0).all()
    assert (ans.x1 == d1).all()

    # This is the old check, converted to what I assume is meant to be
    # the correct form - see #1428 - but ans.y is all zeros except for
    # a single 1 whilst k is not (it has 9 True values and the rest
    # False). I do not have time to identify the truth here.
    #
    k = (ans.x0 >= -1) & (ans.x0 <= 1) & (ans.x1 >= 10) & (ans.x1 <= 12)
    assert ans.y == pytest.approx(k / k.sum())

    assert ans.staterror is None
    assert ans.syserror is None
Пример #12
0
def test_psf2d_kernel_model(caplog):
    """Access the kernel data: no subkernel"""

    k = Box2D()
    k.xlow = -1
    k.xhi = 1
    k.ylow = 10
    k.yhi = 12
    m = PSFModel('x', kernel=k)

    d1, d0 = np.mgrid[5:15, -5:4]
    ds = d0.shape

    d0 = d0.flatten()
    d1 = d1.flatten()
    z = np.zeros(ds).flatten()

    dfold = Data2D('fold', d0, d1, z, shape=ds)

    with caplog.at_level(logging.INFO, logger='sherpa'):
        ans = m.get_kernel(dfold)

    assert len(caplog.records) == 1
    r = caplog.record_tuples[0]
    assert r[0] == 'sherpa.instrument'
    assert r[1] == logging.INFO
    assert r[2] == "PSF frac: 1.0"

    assert isinstance(ans, Data2D)
    assert ans.name == 'kernel'
    assert (ans.shape == [10, 9]).all()

    # integers, so treat as exact
    assert (ans.x0 == d0).all()
    assert (ans.x1 == d1).all()

    k = (ans.x0 >= -1) & (ans.x0 <= 1) & (ans.x1 >= 10) & (ans.x1 <= 12)
    assert pytest.approx(k / k.sum(), ans.y)

    assert ans.staterror is None
    assert ans.syserror is None
Пример #13
0
def test_modelcontour(override_plot_backend):
    p = plot.ModelContour()
    r = p._repr_html_()
    check_empty(r, 'ModelContour', nsummary=7)

    x0 = np.asarray([[10, 20, 30], [10, 20, 30]])
    x1 = np.asarray([[5, 5, 5], [15, 15, 15]])
    y = np.arange(x0.size)
    d = Data2D('n n', x0.flatten(), x1.flatten(), y.flatten())

    # need somethig we can contour
    m = Gauss2D()
    m.xpos = 21
    m.ypos = 9
    m.fwhm = 5

    # we need a stat argument (as of 4.12.1) but it isn't used so
    # send in None
    p.prepare(d, m, stat=None)
    r = p._repr_html_()
    check_full(r, 'ModelContour', 'x1', 'Model', nsummary=7)
Пример #14
0
def setup_single_2d(stat, sys):
    """Return a single data set and model.

    Parameters
    ----------
    stat, sys : bool
        Should statistical and systematic errors be explicitly set
        (True) or taken from the statistic (False)?

    Returns
    -------
    data, model
        Data2D and Model objects.

    """

    x0 = np.asarray([110, 150])
    x1 = np.asarray([200, 300])
    y = np.asarray([1000, 1100])

    if stat:
        # pick values close to sqrt(y)
        staterr = np.asarray([35, 30])
    else:
        staterr = None
    if sys:
        syserr = 0.05 * y
    else:
        syserr = None

    data = Data2D('tst2d', x0, x1, y, staterror=staterr, syserror=syserr)

    mdl = Lorentz2D('mdl2d')
    mdl.fwhm = 200
    mdl.xpos = 130
    mdl.ypos = 260
    mdl.ampl = 1350

    return data, mdl
Пример #15
0
    def to_sherpa_data2d(self, dstype='Data2D'):
        """
        Convert sky map to `~sherpa.data.Data2D` or `~sherpa.data.Data2DInt` class.

        Parameter
        ---------
        dstype : {'Data2D', 'Data2DInt'}
            Sherpa data type.
        """
        from sherpa.data import Data2D, Data2DInt

        if dstype == 'Data2D':
            x, y = self.coordinates('galactic', mode='center')
            return Data2D(self.name, x.ravel(), y.ravel(), self.data.ravel(),
                          self.data.shape)
        elif dstype == 'Data2DInt':
            x, y = self.coordinates('galactic', mode='edges')
            xlo, xhi = x[:-1], x[1:]
            ylo, yhi = y[:-1], y[1:]
            return Data2DInt(self.name, xlo.ravel(), xhi.ravel(), ylo.ravel(),
                             yhi.ravel(), self.data.ravel(), self.data.shape)
        else:
            raise ValueError('Invalid sherpa data type.')
Пример #16
0
def test_fitcontour(override_plot_backend):
    p = plot.FitContour()
    r = p._repr_html_()
    assert r is None  # note: always None

    x0 = np.asarray([[10, 20, 30], [10, 20, 30]])
    x1 = np.asarray([[5, 5, 5], [15, 15, 15]])
    y = np.arange(x0.size)
    d = Data2D('n n', x0.flatten(), x1.flatten(), y.flatten())

    # need somethig we can contour
    m = Gauss2D()
    m.xpos = 21
    m.ypos = 9
    m.fwhm = 5

    dplot = plot.DataContour()
    dplot.prepare(d)

    mplot = plot.ModelContour()
    mplot.prepare(d, m, stat=None)

    p.prepare(dplot, mplot)
    r = p._repr_html_()

    # different to previous checks
    assert r is not None

    if plot.backend.name == 'pylab':
        assert '<summary>FitContour</summary>' in r
        assert '<svg ' in r
        return

    assert '<summary>DataContour (' in r
    assert '<summary>ModelContour (' in r
    assert '<div class="dataval">n n</div>' in r
    assert '<div class="dataval">Model</div>' in r
Пример #17
0
import numpy as np

from sherpa import models
from sherpa.data import Data2D

x_small = np.linspace(-4, 3, 50)
x_large = np.linspace(-4, 300, 5000000)
twod_small = Data2D('data', x_small, x_small, x_small)
twod_large = Data2D('data', x_large, x_large, x_large)

twod_small.ignore(x0low=-3, x0hi=-2, x1lo=-3, x1hi=-2)

# Gauss is a common model, so we pick that as an example here.
# We could do the same benchmarks for all models in Sherpa
# but that might be more than we want ot look at.
# Much of the models shera a common machinary (i.e. they derive
# from the same base class), so a test for a few representative
# models here might be enough as a start.

gauss1d = models.Gauss1D("name")
gauss2d = models.Gauss2D("name")


def time_gauss1d_init():
    g = models.Gauss1d('name')

    
def time_direct_gauss1d_small():
    y = gauss1d(x_small)

    
Пример #18
0
Файл: main.py Проект: pllim/saba
    def _make_dataset(n_dim, x, y, z=None, xbinsize=None, ybinsize=None, err=None, bkg=None, bkg_scale=1, n=0):
        """
        Parameters
        ----------
        n_dim: int
            Used to veirfy required number of dimentions.
        x : array
            input coordinates
        y : array
            input coordinates
        z : array (optional)
            input coordinatesbkg
        xbinsize : array (optional)
            an array of errors in x
        ybinsize : array (optional)
            an array of errors in y
        err : array (optional)
            an array of errors in z
        n  : int
            used in error reporting

        Returns
        -------
        _data: a sherpa dataset
        """

        if (z is None and n_dim > 1) or (z is not None and n_dim == 1):
            raise ValueError("Model and data dimentions don't match in dataset %i" % n)

        if z is None:
            assert x.shape == y.shape, "shape of x and y don't match in dataset %i" % n
        else:
            z = np.asarray(z)
            assert x.shape == y.shape == z.shape, "shapes x,y and z don't match in dataset %i" % n

        if xbinsize is not None:
            xbinsize = np.array(xbinsize)
            assert x.shape == xbinsize.shape, "x's and xbinsize's shapes do not match in dataset %i" % n

        if z is not None and err is not None:
            err = np.array(err)
            assert z.shape == err.shape, "z's and err's shapes do not match in dataset %i" % n

            if ybinsize is not None:
                ybinsize = np.array(ybinsize)
                assert y.shape == ybinsize.shape, "y's and ybinsize's shapes do not match in dataset %i" % n

        else:
            if err is not None:
                err = np.array(err)
                assert y.shape == err.shape, "y's and err's shapes do not match in dataset %i" % n

        if xbinsize is not None:
            bs = xbinsize / 2.0

        if z is None:
            if xbinsize is None:
                if err is None:
                    if bkg is None:
                        data = Data1D("wrapped_data", x=x, y=y)
                    else:
                        data = Data1DBkg("wrapped_data", x=x, y=y, bkg=bkg, bkg_scale=bkg_scale)
                else:
                    if bkg is None:
                        data = Data1D("wrapped_data", x=x, y=y, staterror=err)
                    else:
                        data = Data1DBkg("wrapped_data", x=x, y=y, staterror=err, bkg=bkg, bkg_scale=bkg_scale)
            else:
                if err is None:
                    if bkg is None:

                        data = Data1DInt("wrapped_data", xlo=x - bs, xhi=x + bs, y=y)
                    else:
                        data = Data1DIntBkg("wrapped_data", xlo=x - bs, xhi=x + bs, y=y, bkg=bkg, bkg_scale=bkg_scale)
                else:
                    if bkg is None:
                        data = Data1DInt("wrapped_data", xlo=x - bs, xhi=x + bs, y=y, staterror=err)
                    else:
                        data = Data1DIntBkg("wrapped_data", xlo=x - bs, xhi=x + bs, y=y, staterror=err, bkg=bkg, bkg_scale=bkg_scale)
        else:
            if xbinsize is None and ybinsize is None:
                if err is None:
                    if bkg is None:
                        data = Data2D("wrapped_data", x0=x, x1=y, y=z)
                    else:
                        data = Data2DBkg("wrapped_data", x0=x, x1=y, y=z, bkg=bkg, bkg_scale=bkg_scale)
                else:
                    if bkg is None:
                        data = Data2D("wrapped_data", x0=x, x1=y, y=z, staterror=err)
                    else:
                        data = Data2DBkg("wrapped_data", x0=x, x1=y, y=z, staterror=err, bkg=bkg, bkg_scale=bkg_scale)
            elif xbinsize is not None and ybinsize is not None:
                ys = ybinsize / 2.0
                if err is None:
                    if bkg is None:
                        data = Data2DInt("wrapped_data", x0lo=x - bs, x0hi=x + bs, x1lo=y - ys, x1hi=y + ys, y=z)
                    else:
                        data = Data2DIntBkg("wrapped_data", x0lo=x - bs, x0hi=x + bs, x1lo=y - ys, x1hi=y + ys, y=z, bkg=bkg, bkg_scale=bkg_scale)
                else:
                    if bkg is None:
                        data = Data2DInt("wrapped_data", x0lo=x - bs, x0hi=x + bs, x1lo=y - ys, x1hi=y + ys, y=z, staterror=err)
                    else:
                        data = Data2DIntBkg("wrapped_data", x0lo=x - bs, x0hi=x + bs, x1lo=y - ys, x1hi=y + ys, y=z, staterror=err, bkg=bkg, bkg_scale=bkg_scale)
            else:
                raise ValueError("Set xbinsize and ybinsize, or set neither!")

        return data
Пример #19
0
from matplotlib import pyplot as plt
import numpy as np

from sherpa.models.basic import Box2D
from sherpa.instrument import PSFModel
from sherpa.data import Data2D

k = np.asarray([[0, 1, 0], [1, 0, 1], [0, 1, 0]])
yg, xg = np.mgrid[:3, :3]
kernel = Data2D('kdata', xg.flatten(), yg.flatten(), k.flatten(),
                shape=k.shape)
psf = PSFModel(kernel=kernel)

print("# print(psf)")
print(psf)

pt = Box2D('pt')
box = Box2D('box')
unconvolved_mdl = pt + box

pt.xlow = 1.5
pt.xhi = 2.5
pt.ylow = 2.5
pt.yhi = 3.5
pt.ampl = 10

box.xlow = 4
box.xhi = 10
box.ylow = 6.5
box.yhi = 7.5
box.ampl = 10