NDArithmeticMixin

class astropy.nddata.NDArithmeticMixin[source] [edit on github]

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

  1. It is not tried to decompose the units, mainly due to the internal mechanics of Quantity, so the resulting data might have units like km/m if you divided for example 100km by 5m. So this Mixin has adopted this behaviour.

Examples

TODO: Just here because the examples fit more into the main documentation than in here.

For example:

>>> from astropy.nddata import *
>>> class NDDataWithMath(NDArithmeticMixin, NDData): pass
>>> nd = NDDataWithMath([1,2,3], unit='meter')
>>> nd_inv = nd.ic_division(1, nd)
>>> nd_inv.__class__.__name__
'NDDataWithMath'
>>> nd_inv.data
array([ 1.        ,  0.5       ,  0.33333333])
>>> nd_inv.unit
Unit("1 / m")

This method also allows that the result of unrelated objects is tried with what this class implements as arithmetics and give a result that is the same class as the class that called the method.

>>> nd2 = nd.ic_multiplication([1,2],3)
>>> nd2.__class__.__name__
'NDDataWithMath'
>>> nd2.data
array([3, 6])

Since this is a classmethod we don’t need an instance to use them:

>>> nd3 = NDDataWithMath.ic_subtraction(5, NDData([1,2,3]))
>>> nd3.__class__.__name__
'NDDataWithMath'
>>> nd3.data
array([4, 3, 2])

And it allows to handle arithmetics with different subclasses.

>>> class NDDataWithMathAndSlicing(NDSlicingMixin,NDArithmeticMixin, NDData): pass
>>> nd = NDDataWithMath([5,5,5])
>>> nd2 = NDDataWithMathAndSlicing([3,2,5])
>>> nd3 = nd2.ic_addition(nd, nd2)
>>> nd3.__class__.__name__
'NDDataWithMathAndSlicing'
>>> nd3.data
array([ 8,  7, 10])

Methods Summary

add(operand, **kwargs) Performs addition by evaluating self + operand.
divide(operand, **kwargs) Performs division by evaluating self / operand.
ic_addition(operand1, operand2, **kwargs) Like NDArithmeticMixin.add() you can add two operands.
ic_division(operand1, operand2, **kwargs) Like NDArithmeticMixin.divide() you can divide two operands.
ic_multiplication(operand1, operand2, **kwargs) Like NDArithmeticMixin.multiply() you can multiply two operands.
ic_subtraction(operand1, operand2, **kwargs) Like NDArithmeticMixin.subtract() you can subtract two operands.
multiply(operand, **kwargs) Performs multiplication by evaluating self * operand.
subtract(operand, **kwargs) Performs subtraction by evaluating self - operand.

Methods Documentation

add(operand, **kwargs)[source] [edit on github]

Performs addition by evaluating self + operand.

For the inverse operation or operations between different subclasses take a look at ic_addition() .

Parameters:

operand : NDData-like instance or convertible to one.

The second operand in the operation.

propagate_uncertainties : bool or None, optional

If None the result will have no uncertainty. If False the result will have a copied version of the first operand that has an uncertainty. If True the result will have a correctly propagated uncertainty from the uncertainties of the operands but this assumes that the uncertainties are NDUncertainty-like. Default is True.

handle_mask : callable, 'first_found' or None, 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 results mask and if necessary provide a copy. Default is numpy.logical_or.

handle_meta : callable, 'first_found' or None, 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 results meta and if necessary provide a copy. Default is None.

compare_wcs : callable, 'first_found' or None, 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 the wcs. The resulting wcs will be like if False was given otherwise it raises a ValueError if the comparison was not successful. Default is 'first_found'.

uncertainty_correlation : number 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.

kwargs :

Any other parameter that should be passed to the callables used.

Returns:

result : NDData-like

The resulting dataset

Notes

If a callable is used for mask, wcs or meta the callable must accept the corresponding attributes as first two parameters. If the callable also needs additional parameters these can be defined as kwargs and must start with "wcs_" (for wcs callable) or "meta_" (for meta callable). This startstring is removed before the callable is called.

divide(operand, **kwargs)[source] [edit on github]

Performs division by evaluating self / operand.

For the inverse operation or operations between different subclasses take a look at ic_division() .

Parameters:

operand : NDData-like instance or convertible to one.

The second operand in the operation.

propagate_uncertainties : bool or None, optional

If None the result will have no uncertainty. If False the result will have a copied version of the first operand that has an uncertainty. If True the result will have a correctly propagated uncertainty from the uncertainties of the operands but this assumes that the uncertainties are NDUncertainty-like. Default is True.

handle_mask : callable, 'first_found' or None, 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 results mask and if necessary provide a copy. Default is numpy.logical_or.

handle_meta : callable, 'first_found' or None, 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 results meta and if necessary provide a copy. Default is None.

compare_wcs : callable, 'first_found' or None, 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 the wcs. The resulting wcs will be like if False was given otherwise it raises a ValueError if the comparison was not successful. Default is 'first_found'.

uncertainty_correlation : number 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.

kwargs :

Any other parameter that should be passed to the callables used.

Returns:

result : NDData-like

The resulting dataset

Notes

If a callable is used for mask, wcs or meta the callable must accept the corresponding attributes as first two parameters. If the callable also needs additional parameters these can be defined as kwargs and must start with "wcs_" (for wcs callable) or "meta_" (for meta callable). This startstring is removed before the callable is called.

classmethod ic_addition(operand1, operand2, **kwargs)[source] [edit on github]

Like NDArithmeticMixin.add() you can add two operands.

This method is avaiable as classmethod in order to allow arithmetic operations between arbitary objects as long as they are convertable to the class that called the method. Therefore the name prefix ic_ which stands for interclass operations.

Parameters:

operand1 : NDData-like or convertable to NDData

the first operand in the operation.

operand2 : NDData-like or convertable to NDData

the second operand in the operation.

kwargs :

Returns:

result : NDData-like

The result of the operation. The class of the result is the class from where the method was invoked.

classmethod ic_division(operand1, operand2, **kwargs)[source] [edit on github]

Like NDArithmeticMixin.divide() you can divide two operands.

This method is avaiable as classmethod in order to allow arithmetic operations between arbitary objects as long as they are convertable to the class that called the method. Therefore the name prefix ic_ which stands for interclass operations.

Parameters:

operand1 : NDData-like or convertable to NDData

the first operand in the operation.

operand2 : NDData-like or convertable to NDData

the second operand in the operation.

kwargs :

Returns:

result : NDData-like

The result of the operation. The class of the result is the class from where the method was invoked.

classmethod ic_multiplication(operand1, operand2, **kwargs)[source] [edit on github]

Like NDArithmeticMixin.multiply() you can multiply two operands.

This method is avaiable as classmethod in order to allow arithmetic operations between arbitary objects as long as they are convertable to the class that called the method. Therefore the name prefix ic_ which stands for interclass operations.

Parameters:

operand1 : NDData-like or convertable to NDData

the first operand in the operation.

operand2 : NDData-like or convertable to NDData

the second operand in the operation.

kwargs :

Returns:

result : NDData-like

The result of the operation. The class of the result is the class from where the method was invoked.

classmethod ic_subtraction(operand1, operand2, **kwargs)[source] [edit on github]

Like NDArithmeticMixin.subtract() you can subtract two operands.

This method is avaiable as classmethod in order to allow arithmetic operations between arbitary objects as long as they are convertable to the class that called the method. Therefore the name prefix ic_ which stands for interclass operations.

Parameters:

operand1 : NDData-like or convertable to NDData

the first operand in the operation.

operand2 : NDData-like or convertable to NDData

the second operand in the operation.

kwargs :

Returns:

result : NDData-like

The result of the operation. The class of the result is the class from where the method was invoked.

multiply(operand, **kwargs)[source] [edit on github]

Performs multiplication by evaluating self * operand.

For the inverse operation or operations between different subclasses take a look at ic_multiplication() .

Parameters:

operand : NDData-like instance or convertible to one.

The second operand in the operation.

propagate_uncertainties : bool or None, optional

If None the result will have no uncertainty. If False the result will have a copied version of the first operand that has an uncertainty. If True the result will have a correctly propagated uncertainty from the uncertainties of the operands but this assumes that the uncertainties are NDUncertainty-like. Default is True.

handle_mask : callable, 'first_found' or None, 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 results mask and if necessary provide a copy. Default is numpy.logical_or.

handle_meta : callable, 'first_found' or None, 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 results meta and if necessary provide a copy. Default is None.

compare_wcs : callable, 'first_found' or None, 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 the wcs. The resulting wcs will be like if False was given otherwise it raises a ValueError if the comparison was not successful. Default is 'first_found'.

uncertainty_correlation : number 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.

kwargs :

Any other parameter that should be passed to the callables used.

Returns:

result : NDData-like

The resulting dataset

Notes

If a callable is used for mask, wcs or meta the callable must accept the corresponding attributes as first two parameters. If the callable also needs additional parameters these can be defined as kwargs and must start with "wcs_" (for wcs callable) or "meta_" (for meta callable). This startstring is removed before the callable is called.

subtract(operand, **kwargs)[source] [edit on github]

Performs subtraction by evaluating self - operand.

For the inverse operation or operations between different subclasses take a look at ic_subtraction() .

Parameters:

operand : NDData-like instance or convertible to one.

The second operand in the operation.

propagate_uncertainties : bool or None, optional

If None the result will have no uncertainty. If False the result will have a copied version of the first operand that has an uncertainty. If True the result will have a correctly propagated uncertainty from the uncertainties of the operands but this assumes that the uncertainties are NDUncertainty-like. Default is True.

handle_mask : callable, 'first_found' or None, 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 results mask and if necessary provide a copy. Default is numpy.logical_or.

handle_meta : callable, 'first_found' or None, 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 results meta and if necessary provide a copy. Default is None.

compare_wcs : callable, 'first_found' or None, 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 the wcs. The resulting wcs will be like if False was given otherwise it raises a ValueError if the comparison was not successful. Default is 'first_found'.

uncertainty_correlation : number 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.

kwargs :

Any other parameter that should be passed to the callables used.

Returns:

result : NDData-like

The resulting dataset

Notes

If a callable is used for mask, wcs or meta the callable must accept the corresponding attributes as first two parameters. If the callable also needs additional parameters these can be defined as kwargs and must start with "wcs_" (for wcs callable) or "meta_" (for meta callable). This startstring is removed before the callable is called.