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.')
def test_data2d_int_wrong_y_array_size(array_sizes_fixture): x0, x1, dx, y = array_sizes_fixture with pytest.raises(TypeError): Data2DInt('name', x0.flatten(), x0.flatten(), x1.flatten(), x1.flatten(), y, staterror=numpy.sqrt(y).flatten())
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
def test_data2d_int_eval_model_to_fit(array_sizes_fixture): from sherpa.fit import Fit from sherpa.optmethods import LevMar from sherpa.stats import Chi2 from sherpa.models import Gauss2D x0, x1, dx, y = array_sizes_fixture data2 = Data2DInt('name', x0.flatten(), x0.flatten() + dx, x1.flatten(), x1.flatten() + dx, y.flatten(), staterror=numpy.sqrt(y).flatten()) model2 = Gauss2D() fitter = Fit(data2, model2, Chi2(), LevMar()) fitter.fit() # Failed in Sherpa 4.11.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.')
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