NDArithmeticMixin¶

class
astropy.nddata.
NDArithmeticMixin
[source]¶ Bases:
object
Mixin class to add arithmetic to an NDData object.
When subclassing, be sure to list the superclasses in the correct order so that the subclass sees NDData as the main superclass. See
NDDataArray
for an example.Notes
This class only aims at covering the most common cases so there are certain restrictions on the saved attributes:
 ``uncertainty`` : has to be something that has a `NDUncertainty`like interface for uncertainty propagation  ``mask`` : has to be something that can be used by a bitwise ``or`` operation.  ``wcs`` : has to implement a way of comparing with ``=`` to allow the operation.
But there is a workaround that allows to disable handling a specific attribute and to simply set the results attribute to
None
or to copy the existing attribute (and neglecting the other). For example for uncertainties not representing anNDUncertainty
like interface you can alter thepropagate_uncertainties
parameter inNDArithmeticMixin.add()
.None
means that the result will have no uncertainty,False
means it takes the uncertainty of the first operand (if this does not exist from the second operand) as the result’s uncertainty. This behavior is also explained in the docstring for the different arithmetic operations.Decomposing the units is not attempted, mainly due to the internal mechanics of
Quantity
, so the resulting data might have units likekm/m
if you divided for example 100km by 5m. So this Mixin has adopted this behavior.Examples
Using this Mixin with
NDData
:>>> from astropy.nddata import NDData, NDArithmeticMixin >>> class NDDataWithMath(NDArithmeticMixin, NDData): ... pass
Using it with one operand on an instance:
>>> ndd = NDDataWithMath(100) >>> ndd.add(20) NDDataWithMath(120)
Using it with two operand on an instance:
>>> ndd = NDDataWithMath(4) >>> ndd.divide(1, ndd) NDDataWithMath(0.25)
Using it as classmethod requires two operands:
>>> NDDataWithMath.subtract(5, 4) NDDataWithMath(1)
Methods Summary
add
(operand[, operand2])Performs addition by evaluating
self
+operand
.divide
(operand[, operand2])Performs division by evaluating
self
/operand
.multiply
(operand[, operand2])Performs multiplication by evaluating
self
*operand
.subtract
(operand[, operand2])Performs subtraction by evaluating
self
operand
.Methods Documentation

classmethod
add
(operand, operand2=None, **kwargs)[source]¶ Performs addition by evaluating
self
+operand
. Parameters
 operand, operand2
NDData
like instance or convertible to one. If
operand2
isNone
or not given it will perform the operationself
+operand
. Ifoperand2
is given it will performoperand
+operand2
. If the method was called on a class rather than on the instanceoperand2
must be given. propagate_uncertainties
bool
orNone
, optional If
None
the result will have no uncertainty. IfFalse
the result will have a copied version of the first operand that has an uncertainty. IfTrue
the result will have a correctly propagated uncertainty from the uncertainties of the operands but this assumes that the uncertainties areNDUncertainty
like. Default isTrue
.Changed in version 1.2: This parameter must be given as keywordparameter. Using it as positional parameter is deprecated.
None
was added as valid parameter value. handle_maskcallable,
'first_found'
orNone
, optional If
None
the result will have no mask. If'first_found'
the result will have a copied version of the first operand that has a mask). If it is a callable then the specified callable must create the resultsmask
and if necessary provide a copy. Default isnumpy.logical_or
.New in version 1.2.
 handle_metacallable,
'first_found'
orNone
, optional If
None
the result will have no meta. If'first_found'
the result will have a copied version of the first operand that has a (not empty) meta. If it is a callable then the specified callable must create the resultsmeta
and if necessary provide a copy. Default isNone
.New in version 1.2.
 compare_wcscallable,
'first_found'
orNone
, optional If
None
the result will have no wcs and no comparison between the wcs of the operands is made. If'first_found'
the result will have a copied version of the first operand that has a wcs. If it is a callable then the specified callable must compare thewcs
. The resultingwcs
will be like ifFalse
was given otherwise it raises aValueError
if the comparison was not successful. Default is'first_found'
.New in version 1.2.
 uncertainty_correlationnumber or
ndarray
, optional The correlation between the two operands is used for correct error propagation for correlated data as given in: https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas Default is 0.
New in version 1.2.
 kwargs :
Any other parameter that should be passed to the callables used.
 operand, operand2
 Returns
 result
NDData
like The resulting dataset
 result
Notes
If a
callable
is used formask
,wcs
ormeta
the callable must accept the corresponding attributes as first two parameters. If the callable also needs additional parameters these can be defined askwargs
and must start with"wcs_"
(for wcs callable) or"meta_"
(for meta callable). This startstring is removed before the callable is called."first_found"
can also be abbreviated with"ff"
.

classmethod
divide
(operand, operand2=None, **kwargs)[source]¶ Performs division by evaluating
self
/operand
. Parameters
 operand, operand2
NDData
like instance or convertible to one. If
operand2
isNone
or not given it will perform the operationself
/operand
. Ifoperand2
is given it will performoperand
/operand2
. If the method was called on a class rather than on the instanceoperand2
must be given. propagate_uncertainties
bool
orNone
, optional If
None
the result will have no uncertainty. IfFalse
the result will have a copied version of the first operand that has an uncertainty. IfTrue
the result will have a correctly propagated uncertainty from the uncertainties of the operands but this assumes that the uncertainties areNDUncertainty
like. Default isTrue
.Changed in version 1.2: This parameter must be given as keywordparameter. Using it as positional parameter is deprecated.
None
was added as valid parameter value. handle_maskcallable,
'first_found'
orNone
, optional If
None
the result will have no mask. If'first_found'
the result will have a copied version of the first operand that has a mask). If it is a callable then the specified callable must create the resultsmask
and if necessary provide a copy. Default isnumpy.logical_or
.New in version 1.2.
 handle_metacallable,
'first_found'
orNone
, optional If
None
the result will have no meta. If'first_found'
the result will have a copied version of the first operand that has a (not empty) meta. If it is a callable then the specified callable must create the resultsmeta
and if necessary provide a copy. Default isNone
.New in version 1.2.
 compare_wcscallable,
'first_found'
orNone
, optional If
None
the result will have no wcs and no comparison between the wcs of the operands is made. If'first_found'
the result will have a copied version of the first operand that has a wcs. If it is a callable then the specified callable must compare thewcs
. The resultingwcs
will be like ifFalse
was given otherwise it raises aValueError
if the comparison was not successful. Default is'first_found'
.New in version 1.2.
 uncertainty_correlationnumber or
ndarray
, optional The correlation between the two operands is used for correct error propagation for correlated data as given in: https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas Default is 0.
New in version 1.2.
 kwargs :
Any other parameter that should be passed to the callables used.
 operand, operand2
 Returns
 result
NDData
like The resulting dataset
 result
Notes
If a
callable
is used formask
,wcs
ormeta
the callable must accept the corresponding attributes as first two parameters. If the callable also needs additional parameters these can be defined askwargs
and must start with"wcs_"
(for wcs callable) or"meta_"
(for meta callable). This startstring is removed before the callable is called."first_found"
can also be abbreviated with"ff"
.

classmethod
multiply
(operand, operand2=None, **kwargs)[source]¶ Performs multiplication by evaluating
self
*operand
. Parameters
 operand, operand2
NDData
like instance or convertible to one. If
operand2
isNone
or not given it will perform the operationself
*operand
. Ifoperand2
is given it will performoperand
*operand2
. If the method was called on a class rather than on the instanceoperand2
must be given. propagate_uncertainties
bool
orNone
, optional If
None
the result will have no uncertainty. IfFalse
the result will have a copied version of the first operand that has an uncertainty. IfTrue
the result will have a correctly propagated uncertainty from the uncertainties of the operands but this assumes that the uncertainties areNDUncertainty
like. Default isTrue
.Changed in version 1.2: This parameter must be given as keywordparameter. Using it as positional parameter is deprecated.
None
was added as valid parameter value. handle_maskcallable,
'first_found'
orNone
, optional If
None
the result will have no mask. If'first_found'
the result will have a copied version of the first operand that has a mask). If it is a callable then the specified callable must create the resultsmask
and if necessary provide a copy. Default isnumpy.logical_or
.New in version 1.2.
 handle_metacallable,
'first_found'
orNone
, optional If
None
the result will have no meta. If'first_found'
the result will have a copied version of the first operand that has a (not empty) meta. If it is a callable then the specified callable must create the resultsmeta
and if necessary provide a copy. Default isNone
.New in version 1.2.
 compare_wcscallable,
'first_found'
orNone
, optional If
None
the result will have no wcs and no comparison between the wcs of the operands is made. If'first_found'
the result will have a copied version of the first operand that has a wcs. If it is a callable then the specified callable must compare thewcs
. The resultingwcs
will be like ifFalse
was given otherwise it raises aValueError
if the comparison was not successful. Default is'first_found'
.New in version 1.2.
 uncertainty_correlationnumber or
ndarray
, optional The correlation between the two operands is used for correct error propagation for correlated data as given in: https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas Default is 0.
New in version 1.2.
 kwargs :
Any other parameter that should be passed to the callables used.
 operand, operand2
 Returns
 result
NDData
like The resulting dataset
 result
Notes
If a
callable
is used formask
,wcs
ormeta
the callable must accept the corresponding attributes as first two parameters. If the callable also needs additional parameters these can be defined askwargs
and must start with"wcs_"
(for wcs callable) or"meta_"
(for meta callable). This startstring is removed before the callable is called."first_found"
can also be abbreviated with"ff"
.

classmethod
subtract
(operand, operand2=None, **kwargs)[source]¶ Performs subtraction by evaluating
self
operand
. Parameters
 operand, operand2
NDData
like instance or convertible to one. If
operand2
isNone
or not given it will perform the operationself
operand
. Ifoperand2
is given it will performoperand
operand2
. If the method was called on a class rather than on the instanceoperand2
must be given. propagate_uncertainties
bool
orNone
, optional If
None
the result will have no uncertainty. IfFalse
the result will have a copied version of the first operand that has an uncertainty. IfTrue
the result will have a correctly propagated uncertainty from the uncertainties of the operands but this assumes that the uncertainties areNDUncertainty
like. Default isTrue
.Changed in version 1.2: This parameter must be given as keywordparameter. Using it as positional parameter is deprecated.
None
was added as valid parameter value. handle_maskcallable,
'first_found'
orNone
, optional If
None
the result will have no mask. If'first_found'
the result will have a copied version of the first operand that has a mask). If it is a callable then the specified callable must create the resultsmask
and if necessary provide a copy. Default isnumpy.logical_or
.New in version 1.2.
 handle_metacallable,
'first_found'
orNone
, optional If
None
the result will have no meta. If'first_found'
the result will have a copied version of the first operand that has a (not empty) meta. If it is a callable then the specified callable must create the resultsmeta
and if necessary provide a copy. Default isNone
.New in version 1.2.
 compare_wcscallable,
'first_found'
orNone
, optional If
None
the result will have no wcs and no comparison between the wcs of the operands is made. If'first_found'
the result will have a copied version of the first operand that has a wcs. If it is a callable then the specified callable must compare thewcs
. The resultingwcs
will be like ifFalse
was given otherwise it raises aValueError
if the comparison was not successful. Default is'first_found'
.New in version 1.2.
 uncertainty_correlationnumber or
ndarray
, optional The correlation between the two operands is used for correct error propagation for correlated data as given in: https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas Default is 0.
New in version 1.2.
 kwargs :
Any other parameter that should be passed to the callables used.
 operand, operand2
 Returns
 result
NDData
like The resulting dataset
 result
Notes
If a
callable
is used formask
,wcs
ormeta
the callable must accept the corresponding attributes as first two parameters. If the callable also needs additional parameters these can be defined askwargs
and must start with"wcs_"
(for wcs callable) or"meta_"
(for meta callable). This startstring is removed before the callable is called."first_found"
can also be abbreviated with"ff"
.

classmethod