Data2D

class sherpa.data.Data2D(name: str, x0: Sequence[float] | ndarray | None, x1: Sequence[float] | ndarray | None, y: Sequence[float] | ndarray | None, shape: tuple[int, int] | None = None, staterror: Sequence[float] | ndarray | None = None, syserror: Sequence[float] | ndarray | None = None)[source] [edit on github]

Bases: Data

2D data set

This class represents a 2D data set. It is desigend to work with flattened arrays for coordinates and independent variables, which makes it easy to deal with filters and sparse or irregularly-placed grids.

Of course, the same structure can also be used for regularly-gridded data, it just has to be passed in as a flattened array. However, in this case the more specialized DataIMG class might be more appropriate.

Parameters:
namestr

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. If this is a numpy masked array, the mask will be used to initialize a mask.

shapetuple

Shape of the data grid for regularly gridded data (optional). This is used to return the data as an image e.g. for display, but it not needed for fitting and modelling. For irregularly gridded data, shape must be None.

staterrorarray_like

the statistical error associated with the data

syserrorarray_like

the systematic error associated with the data

Attributes:
dep

Left for compatibility with older versions

indep

The grid of the data space associated with this data set.

mask

Mask array for dependent variable

size

The number of elements in the data set.

staterror

The statistical error on the dependent axis, if set.

syserror

The systematic error on the dependent axis, if set.

x0

kept for compatibility

x1

kept for compatibility

y

The 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.

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_x0label()

Return label for first dimension in 2-D view of independent axis/axes.

get_x1label()

Return label for second dimension in 2-D view of independent axis/axes.

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.

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

get_filter

get_filter_expr

get_imgerr

get_x0

get_x1

ignore

notice

set_indep

to_contour

to_fit

to_guess

Examples

An irregularly-gridded 2D dataset, with points at (-200, -200), (-200, 0), (0, 0), (200, -100), and (200, 150) can be created with:

>>> irreg2d = Data2D("irregular2d",
...                  [-200, -200, 0, 200, 200],
...                  [-200, 0, 0, -100, 150],
...                  [12, 15, 23, 45, -2])

A regularly-gridded 2D dataset can be created, but the arguments must be flattened:

>>> import numpy as np
>>> x1, x0 = np.mgrid[20:30:2, 5:20:2]
>>> datashape = x0.shape
>>> y = np.sqrt((x0 - 10)**2 + (x1 - 31)**2)
>>> x0 = x0.flatten()
>>> x1 = x1.flatten()
>>> y = y.flatten()
>>> reg2d = Data2D("regular2d", x0, x1, y, shape=datashape)

Note

Sherpa provides the DataIMG class to handle regularly-gridded data more easily.

Changed in version 4.19.0: The get_axis method has been removed because it had inconsistent outputs. Use get_x0 and get_x1 or get_indep instead.

Attributes Summary

dep

Left for compatibility with older versions

indep

The grid of the data space associated with this data set.

mask

Mask array for dependent variable

ndim

The dimensionality of the dataset, if defined, or None.

size

The number of elements in the data set.

staterror

The statistical error on the dependent axis, if set.

syserror

The systematic error on the dependent axis, if set.

x0

kept for compatibility

x1

kept for compatibility

y

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.

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_filter()

get_filter_expr()

get_img([yfunc])

Return the dependent axis as a 2D array.

get_imgerr()

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_x0([filter])

get_x0label()

Return label for first dimension in 2-D view of independent axis/axes.

get_x1([filter])

get_x1label()

Return label for second dimension in 2-D view of independent axis/axes.

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])

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

dep

Left for compatibility with older versions

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 notice and ignore is now cleared when the independent axis is changed.

Returns:
tuple of array_like or None
mask

Mask array for dependent variable

Returns:
maskbool or numpy.ndarray
ndim: int | None = 2

The dimensionality of the dataset, if defined, or None.

size

The number of elements in the data set.

Returns:
sizeint or None

If the size has not been set then None is returned.

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: Sequence[float] | ndarray | None) ndarray | None [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.

get_dep(filter: bool = False) ndarray | None [edit on github]

Return the dependent axis of a data set.

Parameters:
filterbool, optional

Should the filter attached to the data set be applied to the return value or not. The default is False.

Returns:
axis: array

The dependent axis values for the data set. This gives the value of each point in the data set.

See also

get_indep

Return the independent axis of a data set.

get_error

Return the errors on the dependent axis of a data set.

get_staterror

Return the statistical errors on the dependent axis of a data set.

get_syserror

Return the systematic errors on the dependent axis of a data set.

get_dims(filter: bool = False) tuple[int, ...][source] [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:
tuple
get_error(filter=False, staterrfunc=None) [edit on github]

Return the total error on the dependent variable.

Parameters:
filterbool, optional

Should the filter attached to the data set be applied to the return value or not. The default is False.

staterrfuncfunction

If no statistical error has been set, the errors will be calculated by applying this function to the dependent axis of the data set.

Returns:
axisarray or None

The error for each data point, formed by adding the statistical and systematic errors in quadrature.

See also

get_dep

Return the independent axis of a data set.

get_staterror

Return the statistical errors on the dependent axis of a data set.

get_syserror

Return the systematic errors on the dependent axis of a data set.

get_filter() str[source] [edit on github]
get_filter_expr() str[source] [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:
yfuncsherpa.models.model.Model instance or None, optional

If set then it is a model that is evaluated on the data grid and returned along with the dependent axis.

Returns:
imgndarray or (ndarray, ndarray)

The data as a 2D array or a pair of 2D arrays when yfunc is set.

get_imgerr()[source] [edit on github]
get_indep(filter: bool = False) tuple[ndarray, ...] | tuple[None, ...] [edit on github]

Return the independent axes of a data set.

Parameters:
filterbool, optional

Should the filter attached to the data set be applied to the return value or not. The default is False.

Returns:
axis: tuple of arrays

The independent axis values for the data set. This gives the coordinates of each point in the data set.

See also

get_dep

Return the dependent axis of a data set.

get_max_pos(dep: ndarray | None = None) tuple[float, float] | list[tuple[float, float]][source] [edit on github]

Return the coordinates of the maximum value.

Parameters:
depndarray or None, optional

The data to search and it must match the current data filter. If not given then the dependent axis is used.

Returns:
coordspair or list of pairs

The coordinates of the maximum location. The data values match the values returned by get_x0 and get_x1. If there is only one maximum pixel then a pair is returned otherwise a list of pairs is returned.

See also

get_dep, get_x0, get_x1
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:
filterbool, optional

Should the filter attached to the data set be applied to the return value or not. The default is False.

staterrfuncfunction

If no statistical error has been set, the errors will be calculated by applying this function to the dependent axis of the data set.

Returns:
axisarray or None

The statistical error for each data point. A value of None is returned if the data set has no statistical error array and staterrfunc is None.

See also

get_error

Return the errors on the dependent axis of a data set.

get_indep

Return the independent axis of a data set.

get_syserror

Return 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:
filterbool, optional

Should the filter attached to the data set be applied to the return value or not. The default is False.

Returns:
axisarray or None

The systematic error for each data point. A value of None is returned if the data set has no systematic errors.

See also

get_error

Return the errors on the dependent axis of a data set.

get_indep

Return the independent axis of a data set.

get_staterror

Return the statistical errors on the dependent axis of a data set.

get_x0(filter: bool = False) ndarray | None[source] [edit on github]
get_x0label() str[source] [edit on github]

Return label for first dimension in 2-D view of independent axis/axes.

Returns:
label: str
get_x1(filter: bool = False) ndarray | None[source] [edit on github]
get_x1label() str[source] [edit on github]

Return label for second dimension in 2-D view of independent axis/axes. Returns ——- label: str

get_y(filter=False, yfunc=None, use_evaluation_space=False) [edit on github]

Return dependent axis in N-D view of dependent variable

Parameters:
filter
yfunc
use_evaluation_space
Returns:
y: array or (array, array) or None

If yfunc is not None and the dependent axis is set then the return value is (y, y2) where y2 is yfunc evaluated on the independent axis.

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.

See also

set_ylabel
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[source] [edit on github]
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.

set_indep(val: tuple[Sequence[float] | ndarray, ...] | tuple[None, ...]) None [edit on github]
set_x0label(label: str) None[source] [edit on github]

Set the label for the first independent axis.

Added in version 4.17.0.

Parameters:
label: str
set_x1label(label: str) None[source] [edit on github]

Set the label for the second independent axis.

Added in version 4.17.0.

Parameters:
label: str
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

get_ylabel
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]