DataIMG
- class sherpa.astro.data.DataIMG(name, x0, x1, y, shape=None, staterror=None, syserror=None, sky=None, eqpos=None, coord='logical', header=None)[source] [edit on github]
Bases:
Data2DImage data set
This class builds on
sherpa.data.Data2Dto add support for region filters and the ability to describe the data in different coordinate systems, such as the “logical” coordinates (pixels of the image), the “physical” coordinates (e.g. detector coordinates), and the “world” coordinates (e.g. Ra/Dec on the sky).Note
In principle, this class can deal with any shape of data, even with sparse data. However, the added functionality over the base class is most useful for regularly gridded images. In that case, that data should be ordered as in the FITS image conventions, i.e. [column, row]; in other words
x0is the x-axis of the image andx1is the y-axis in an x-y-plot. (Sherpa usesyfor the dependent variable and calls the axesx0andx1.)Only with this convention will the routines that return a 2D array (e.g. for plotting) work correctly. Fitting is done on the flattened 1D arrays and will work for any ordering.
- Parameters:
- name
str name of this dataset
- x0array_like
Independent coordinate values for the first dimension
- x1array_like
Independent coordinate values for the second dimension
- yarray_like
The values of the dependent observable.
- shape
tuple Shape of the data grid (optional). This is used return the data as an image e.g. for display, but it not needed for fitting and modelling.
- staterrorarray_like
the statistical error associated with the data
- syserrorarray_like
the systematic error associated with the data
- sky
sherpa.astro.io.wcs.WCS The optional WCS object that converts to the physical coordinate system.
- eqpos
sherpa.astro.io.wcs.WCS The optional WCS object that converts to the world coordinate system.
- coord
str The coordinate system of the data. This can be one of ‘logical’, ‘physical’, or ‘world’. ‘image’ is an alias for ‘logical’ and ‘wcs’ is an alias for ‘world’.
- header
dict The FITS header associated with the data to store meta data.
- name
- Attributes:
coordReturn the coordinate setting.
depLeft for compatibility with older versions
- eqpos
indepThe grid of the data space associated with this data set.
maskMask array for dependent variable
sizeThe number of elements in the data set.
- sky
staterrorThe statistical error on the dependent axis, if set.
syserrorThe systematic error on the dependent axis, if set.
x0kept for compatibility
x1kept for compatibility
yThe dependent axis.
Methods
eval_model(modelfunc)Evaluate the model on the independent axis.
eval_model_to_fit(modelfunc)Evaluate the model on the independent axis after filtering.
from_2d_array(name, *, y[, x0, x1, ...])Create a
DataIMGinstance from a 2-dimensional array.from_2d_array_with_wcs(name, *, y[, ...])Create a DataIMG instance from 2D data and WCS information.
get_dep([filter])Return the dependent axis of a data set.
get_dims([filter])Return the dimensions of this data space as a tuple of tuples.
get_error([filter, staterrfunc])Return the total error on the dependent variable.
get_img([yfunc])Return the dependent axis as a 2D array.
get_indep([filter])Return the independent axes of a data set.
get_max_pos([dep])Return the coordinates of the maximum value.
get_staterror([filter, staterrfunc])Return the statistical error on the dependent axis of a data set.
get_syserror([filter])Return the systematic error on the dependent axis of a data set.
Return the label for the first independent axis.
Return the label for the second independent axis.
get_y([filter, yfunc, use_evaluation_space])Return dependent axis in N-D view of dependent variable
get_yerr([filter, staterrfunc])Return errors in dependent axis in N-D view of dependent variable.
get_ylabel([yfunc])Return label for dependent axis in N-D view of dependent variable.
notice2d([val, ignore])Apply a 2D filter.
set_coord(coord)Change the
coordattribute.set_dep(val)Set the dependent variable values.
set_x0label(label)Set the label for the first independent axis.
set_x1label(label)Set the label for the second independent axis.
set_ylabel(label)Set the label for the dependent axis.
apply_filter
filter_region
get_bounding_mask
get_filter
get_filter_expr
get_image
get_imgerr
get_logical
get_physical
get_wcs
get_world
get_x0
get_x1
ignore
notice
set_indep
to_contour
to_fit
to_guess
Examples
In this example, we create an image data set, but do not specify the coordinate system. This means that the coordinates are expressed in the logical coordinates of the image, i.e. in pixels:
>>> from sherpa.astro.data import DataIMG >>> import numpy as np >>> # Note ordering of x1, x0 here >>> x1, x0 = np.mgrid[20:30, 5:20] >>> datashape = x0.shape >>> y = np.sqrt((x0 - 10)**2 + (x1 - 31)**2) >>> x0flat = x0.flatten() >>> x1flat = x1.flatten() >>> yflat = y.flatten() >>> image = DataIMG("bimage", x0=x0flat, x1=x1flat, y=yflat, shape=datashape)
In this example, we end up with a “logical” coordinate system in
imageand no WCS system to convert it to anything else. On the other hand, in FITS standard terminology, the “logical” coordinate system is the “image”, counting pixels starting at 1, while here thex0lo`andx1loactually start at 20 and 5, respectively. This behavior works for now, but might be revisited.For the common case of creating an image data set from a 2D array, the
from_2d_arrayclass method can be used. In this case, the length ofx0andx1must match the shape of the 2D numpy array and Sherpa will take care of transforming it from numpy [row, column] ordering to the [column, row] ordering:>>> x0 = np.arange(20, 30) >>> x1 = np.arange(5, 20) >>> print(y.shape, x0.shape, x1.shape) (10, 15) (10,) (15,) >>> image = DataIMG.from_2d_array("bimage", y=y, x0=x0, x1=x1)
It’s even easier if no particular coordinate system is needed for
x0andx1and the coordinates are simply pixel indices, which can be generated automatically (starting at 1):>>> image = DataIMG.from_2d_array("bimage", y=y)
Attributes Summary
Return the coordinate setting.
Left for compatibility with older versions
The optional WCS object that converts to the world coordinate system.
The grid of the data space associated with this data set.
Mask array for dependent variable
The dimensionality of the dataset, if defined, or None.
The number of elements in the data set.
The optional WCS object that converts to the physical coordinate system.
The statistical error on the dependent axis, if set.
The systematic error on the dependent axis, if set.
kept for compatibility
kept for compatibility
The dependent axis.
Methods Summary
apply_filter(data)eval_model(modelfunc)Evaluate the model on the independent axis.
eval_model_to_fit(modelfunc)Evaluate the model on the independent axis after filtering.
filter_region(data)from_2d_array(name, *, y[, x0, x1, ...])Create a
DataIMGinstance from a 2-dimensional array.from_2d_array_with_wcs(name, *, y[, ...])Create a DataIMG instance from 2D data and WCS information.
get_dep([filter])Return the dependent axis of a data set.
get_dims([filter])Return the dimensions of this data space as a tuple of tuples.
get_error([filter, staterrfunc])Return the total error on the dependent variable.
get_img([yfunc])Return the dependent axis as a 2D array.
get_indep([filter])Return the independent axes of a data set.
get_max_pos([dep])Return the coordinates of the maximum value.
get_staterror([filter, staterrfunc])Return the statistical error on the dependent axis of a data set.
get_syserror([filter])Return the systematic error on the dependent axis of a data set.
get_wcs()get_x0([filter])Return the label for the first independent axis.
get_x1([filter])Return the label for the second independent axis.
get_y([filter, yfunc, use_evaluation_space])Return dependent axis in N-D view of dependent variable
get_yerr([filter, staterrfunc])Return errors in dependent axis in N-D view of dependent variable.
get_ylabel([yfunc])Return label for dependent axis in N-D view of dependent variable.
ignore(*args, **kwargs)notice([x0lo, x0hi, x1lo, x1hi, ignore])notice2d([val, ignore])Apply a 2D filter.
set_coord(coord)Change the
coordattribute.set_dep(val)Set the dependent variable values.
set_indep(val)set_x0label(label)Set the label for the first independent axis.
set_x1label(label)Set the label for the second independent axis.
set_ylabel(label)Set the label for the dependent axis.
to_contour([yfunc])to_fit([staterrfunc])to_guess()Attributes Documentation
- coord
Return the coordinate setting.
The attribute is one of ‘logical’, ‘physical’, or ‘world’. Use
set_coordto change the setting.
- dep
Left for compatibility with older versions
- eqpos = None
The optional WCS object that converts to the world coordinate system.
- indep
The grid of the data space associated with this data set.
When set, the field must be set to a tuple, even for a one-dimensional data set. The “related” fields such as the dependent axis and the error fields are set to None if their size does not match.
Changed in version 4.14.1: The filter created by
noticeandignoreis now cleared when the independent axis is changed.- Returns:
tupleofarray_like orNone
- mask
Mask array for dependent variable
- Returns:
- maskbool or
numpy.ndarray
- maskbool or
- size
The number of elements in the data set.
- sky = None
The optional WCS object that converts to the physical coordinate system.
- staterror
The statistical error on the dependent axis, if set.
This must match the size of the independent axis.
- syserror
The systematic error on the dependent axis, if set.
This must match the size of the independent axis.
- x0
kept for compatibility
- x1
kept for compatibility
- y
The dependent axis.
If set, it must match the size of the independent axes.
Methods Documentation
- apply_filter(data) [edit on github]
- eval_model(modelfunc: Callable[[...], Sequence[float] | ndarray]) Sequence[float] | ndarray [edit on github]
Evaluate the model on the independent axis.
- eval_model_to_fit(modelfunc: Callable[[...], Sequence[float] | ndarray]) Sequence[float] | ndarray [edit on github]
Evaluate the model on the independent axis after filtering.
- filter_region(data)[source] [edit on github]
- classmethod from_2d_array(name: str, *, y: NDArray[floating], x0: NDArray[floating] | None = None, x1: NDArray[floating] | None = None, staterror: NDArray[floating] | None = None, syserror: NDArray[floating] | None = None, header: Mapping[str, Any] | None = None) DataIMG[source] [edit on github]
Create a
DataIMGinstance from a 2-dimensional array.- Parameters:
- name
str The name of the dataset.
- y
ndarraywith2dimensions The dependent variable array.
- x0, x1: ndarray, optional
The independent variable arrays; the length of x0 must match y’s first dimension, and the length of x1 must match y’s second dimension. If not provided, they will be generated, starting at 1.
Note
This does not follow the usual Python conventions to start enumerations at 0, instead, it follows the fits conventions where the first pixel has the coordinate (1, 1).
- staterror
ndarraywith2dimensions,optional The statistical error array.
- syserror
ndarraywith2dimensions,optional The systematic error array.
- header
dict,optional The header information for the data.
- name
- Returns:
- dataimg
DataIMG A DataIMG instance.
- dataimg
Examples
Create a DataIMG instance from 2D data. Here, we first create a 2-D array of values y, but in practice this might be read in from a file, from the output of a simulation, or as a result of some other computation.
>>> import numpy as np >>> from sherpa.astro.data import DataIMG >>> x1 = np.arange(20, 30, 2) >>> x0 = np.arange(5, 20, 2) >>> y = np.sqrt((x0[:, np.newaxis] - 10)**2 + (x1[np.newaxis, :] - 31)**2) >>> reg2d = DataIMG.from_2d_array("regular2d", x0=x0, x1=x1, y=y)
- classmethod from_2d_array_with_wcs(name: str, *, y: npt.NDArray[np.floating], staterror: npt.NDArray[np.floating] | None = None, syserror: npt.NDArray[np.floating] | None = None, sky: WCS, eqpos: WCS, coord: Literal['logical', 'image', 'physical', 'world', 'wcs'] = 'logical', header: Mapping[str, Any] | None = None) DataIMG[source] [edit on github]
Create a DataIMG instance from 2D data and WCS information.
- get_bounding_mask()[source] [edit on github]
- get_dep(filter: bool = False) ndarray | None [edit on github]
Return the dependent axis of a data set.
- Parameters:
- Returns:
- axis:
array The dependent axis values for the data set. This gives the value of each point in the data set.
- axis:
See also
get_indepReturn the independent axis of a data set.
get_errorReturn the errors on the dependent axis of a data set.
get_staterrorReturn the statistical errors on the dependent axis of a data set.
get_syserrorReturn the systematic errors on the dependent axis of a data set.
- get_dims(filter: bool = False) tuple[int, ...] [edit on github]
Return the dimensions of this data space as a tuple of tuples. The first element in the tuple is a tuple with the dimensions of the data space, while the second element provides the size of the dependent array.
- Returns:
- get_error(filter=False, staterrfunc=None) [edit on github]
Return the total error on the dependent variable.
- Parameters:
- Returns:
See also
get_depReturn the independent axis of a data set.
get_staterrorReturn the statistical errors on the dependent axis of a data set.
get_syserrorReturn the systematic errors on the dependent axis of a data set.
- get_filter() [edit on github]
- get_filter_expr()[source] [edit on github]
- get_image() [edit on github]
- get_img(yfunc=None)[source] [edit on github]
Return the dependent axis as a 2D array.
The data is not filtered.
- Parameters:
- yfunc
sherpa.models.model.ModelinstanceorNone,optional If set then it is a model that is evaluated on the data grid and returned along with the dependent axis.
- yfunc
- Returns:
- get_imgerr() [edit on github]
- get_indep(filter: bool = False) tuple[ndarray, ...] | tuple[None, ...] [edit on github]
Return the independent axes of a data set.
- Parameters:
- Returns:
- axis:
tupleofarrays The independent axis values for the data set. This gives the coordinates of each point in the data set.
- axis:
See also
get_depReturn the dependent axis of a data set.
- get_logical()[source] [edit on github]
- get_max_pos(dep: ndarray | None = None) tuple[float, float] | list[tuple[float, float]] [edit on github]
Return the coordinates of the maximum value.
- Parameters:
- Returns:
- get_physical()[source] [edit on github]
- get_staterror(filter: bool = False, staterrfunc: Callable[[Sequence[float] | ndarray], Sequence[float] | ndarray] | None = None) Sequence[float] | ndarray | None [edit on github]
Return the statistical error on the dependent axis of a data set.
- Parameters:
- Returns:
See also
get_errorReturn the errors on the dependent axis of a data set.
get_indepReturn the independent axis of a data set.
get_syserrorReturn the systematic errors on the dependent axis of a data set.
- get_syserror(filter: bool = False) ndarray | None [edit on github]
Return the systematic error on the dependent axis of a data set.
- Parameters:
- Returns:
See also
get_errorReturn the errors on the dependent axis of a data set.
get_indepReturn the independent axis of a data set.
get_staterrorReturn the statistical errors on the dependent axis of a data set.
- get_wcs() [edit on github]
- get_world()[source] [edit on github]
- get_x0(filter: bool = False) ndarray | None [edit on github]
- get_x0label() str[source] [edit on github]
Return the label for the first independent axis.
If set_x0label has been called then the label used in that call will be used, otherwise the label will change depending on the coordinate setting.
Changed in version 4.17.0: The return value depends on if set_x0label has been called.
See also
- get_x1(filter: bool = False) ndarray | None [edit on github]
- get_x1label() str[source] [edit on github]
Return the label for the second independent axis.
If set_x1label has been called then the label used in that call will be used, otherwise the label will change depending on the coordinate setting.
Changed in version 4.17.0: The return value depends on if set_x1label has been called.
See also
- get_y(filter=False, yfunc=None, use_evaluation_space=False) [edit on github]
Return dependent axis in N-D view of dependent variable
- get_yerr(filter=False, staterrfunc=None) [edit on github]
Return errors in dependent axis in N-D view of dependent variable.
- Parameters:
- filter
- staterrfunc
- Returns:
- get_ylabel(yfunc=None) str [edit on github]
Return label for dependent axis in N-D view of dependent variable.
- Parameters:
- yfunc
Unused.
- Returns:
- label:
str The label.
- label:
See also
- ignore(*args, **kwargs) None [edit on github]
- notice(x0lo: float | None = None, x0hi: float | None = None, x1lo: float | None = None, x1hi: float | None = None, ignore: bool = False) None [edit on github]
- notice2d(val=None, ignore=False)[source] [edit on github]
Apply a 2D filter.
- set_coord(coord)[source] [edit on github]
Change the
coordattribute.Changed in version 4.14.1: The filter created by
notice2dis now cleared when the coordinate system is changed.- Parameters:
- coord{‘logical’, ‘image’, ‘physical’, ‘world’, ‘wcs’}
The coordinate system to use. Note that “image” is a synonym for “logical” and “wcs” is a synomyn for “world”.
- set_dep(val: Sequence[float] | ndarray | float) None [edit on github]
Set the dependent variable values.
- Parameters:
- valsequence or
number If a number then it is used for each element.
- valsequence or
- set_x0label(label: str) None [edit on github]
Set the label for the first independent axis.
Added in version 4.17.0.
- Parameters:
- label: str
See also
- set_x1label(label: str) None [edit on github]
Set the label for the second independent axis.
Added in version 4.17.0.
- Parameters:
- label: str
See also
- set_ylabel(label: str) None [edit on github]
Set the label for the dependent axis.
Added in version 4.17.0.
- Parameters:
- label: str
The new label.
See also
- to_contour(yfunc=None)[source] [edit on github]
- to_fit(staterrfunc: Callable[[Sequence[float] | ndarray], Sequence[float] | ndarray] | None = None) tuple[ndarray | None, Sequence[float] | ndarray | None, ndarray | None] [edit on github]
- to_guess() tuple[ndarray | None, ...] [edit on github]