NDAstroData

class astrodata.NDAstroData(data, uncertainty=None, mask=None, wcs=None, meta=None, unit=None, copy=False, variance=None)[source]

Bases: AstroDataMixin, NDArithmeticMixin, NDSlicingMixin, NDData

Primary data class for AstroData objects.

Implements NDData with all Mixins, plus some AstroData specifics.

This class implements an NDData-like container that supports reading and writing as implemented in the astropy.io.registry and also slicing (indexing) and simple arithmetics (add, subtract, divide and multiply).

A very important difference between NDAstroData and NDData is that the former attempts to load all its data lazily. There are also some important differences in the interface (eg. .data lets you reset its contents after initialization).

Documentation is provided where our class differs.

See also

NDData, NDArithmeticMixin, NDSlicingMixin

Examples

The mixins allow operation that are not possible with NDData or NDDataBase, i.e. simple arithmetics:

>>> from astropy.nddata import StdDevUncertainty
>>> import numpy as np
>>> data = np.ones((3,3), dtype=float)
>>> ndd1 = NDAstroData(data, uncertainty=StdDevUncertainty(data))
>>> ndd2 = NDAstroData(data, uncertainty=StdDevUncertainty(data))
>>> ndd3 = ndd1.add(ndd2)
>>> ndd3.data
array([[2., 2., 2.],
    [2., 2., 2.],
    [2., 2., 2.]])
>>> ndd3.uncertainty.array
array([[1.41421356, 1.41421356, 1.41421356],
    [1.41421356, 1.41421356, 1.41421356],
    [1.41421356, 1.41421356, 1.41421356]])

see NDArithmeticMixin for a complete list of all supported arithmetic operations.

But also slicing (indexing) is possible:

>>> ndd4 = ndd3[1,:]
>>> ndd4.data
array([2., 2., 2.])
>>> ndd4.uncertainty.array
array([1.41421356, 1.41421356, 1.41421356])

See NDSlicingMixin for a description how slicing works (which attributes) are sliced.

Initialize an NDAstroData instance.

Parameters:
  • data (array-like) – The actual data. This can be a numpy array, a memmap, or a fits.ImageHDU object.

  • uncertainty (NDUncertainty-like object, optional) – An object that represents the uncertainty of the data. If not specified, the uncertainty will be set to None.

  • mask (array-like, optional) – An array that represents the mask of the data. If not specified, the mask will be set to None.

  • wcs (gwcs.WCS object, optional) – The WCS of the data. If not specified, the WCS will be set to None.

  • meta (dict-like, optional) – A dictionary-like object that holds the meta data. If not specified, the meta data will be set to None.

  • unit (astropy.units.Unit object, optional) – The unit of the data. If not specified, the unit will be set to None.

  • copy (bool, optional) – If True, the data, uncertainty, mask, wcs, meta, and unit will be copied. Otherwise, they will be referenced. Default is False.

  • variance (array-like, optional) – An array that represents the variance of the data. If not specified, the variance will be set to None.

Raises:

ValueError – If uncertainty and variance are both specified.

Notes

The uncertainty and variance parameters are mutually exclusive.

Attributes Summary

T

Transpose the data.

data

Access the data stored in this instance.

mask

Get or set the mask of the data.

uncertainty

Get or set the uncertainty of the data.

variance

Get and aset the variance of the data.

window

Access a slice of the data.

Methods Summary

set_section(section, input_data)

Set a section of the data to the input data.

transpose()

Transpose the data.

Attributes Documentation

T

Transpose the data. This is not a copy of the data.

data

Access the data stored in this instance. It implements a setter.

mask

Get or set the mask of the data.

uncertainty

Get or set the uncertainty of the data.

variance

Get and aset the variance of the data.

A convenience property to access the contents of uncertainty, squared (as the uncertainty data is stored as standard deviation).

window

Access a slice of the data.

Interface to access a section of the data, using lazy access whenever possible.

Returns:

  • An instance of NDWindowing, which provides __getitem__,

  • to allow the use of square brackets when specifying the window.

  • Ultimately, an NDWindowingAstrodata instance is returned.

Examples

>>> ad[0].nddata.window[100:200, 100:200]  
<NDWindowingAstrodata .....>

Methods Documentation

set_section(section, input_data)[source]

Set a section of the data to the input data.

Sets only a section of the data. This method is meant to prevent fragmentation in the Python heap, by reusing the internal structures instead of replacing them with new ones.

Parameters:
  • section (slice) – The area that will be replaced

  • input_data (NDData-like instance) – This object needs to implement at least data, uncertainty, and mask. Their entire contents will replace the data in the area defined by section.

Examples

>>> def setup():
...     sec = NDData(np.zeros((100,100)))
...     ad[0].nddata.set_section(
...         (slice(None,100),slice(None,100)),
...         sec
...     )
...
>>> setup()  
transpose()[source]

Transpose the data. This is not a copy of the data.