Skip to content

Commit 8eff1b5

Browse files
committed
Next commit on arithmetic docs.
1 parent 8ca5c5b commit 8eff1b5

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

docs/explaining_ndcube/arithmetic.rst

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ Arithmetic Operations
55
*********************
66

77
Arithmetic operations are a crucial tool in n-dimensional data analysis.
8-
Applications include subtracting a background from a 1-D timeseries or spectrum, scaling an image by a vignetting function, any many others.
8+
Applications include subtracting a background from a 1-D timeseries or spectrum, scaling an image by a vignetting function, and many others.
99
To aid with such workflows, `~ndcube.NDCube` supports addition, subtraction, multiplication, and division with numbers, arrays, `~astropy.units.Quantity`.
1010
Raising an `~ndcube.NDCube` to a power is also supported.
11-
These operations return a new `~ndcube.NDCube` with the data array (and where appropriate, the uncertainties) altered in accordance with the arithmetic operation.
11+
These operations return a new `~ndcube.NDCube` with the data array (and, where appropriate, the uncertainties) altered in accordance with the arithmetic operation.
1212
Other attributes of the `~ndcube.NDCube` remain unchanged.
1313

14-
In addition, combining `~ndcube.NDCube` with coordinate-less `~astropy.nddata.NDData` subclasses via these operations is important.
15-
Such operations can be more complicated. Hence see the :ref:`arithmetic_nddata` section below for a discussion separate, more detailed discussion.
14+
In addition, combining `~ndcube.NDCube` with coordinate-less `~astropy.nddata.NDData` subclasses via these operations is also supported.
15+
Such operations can be more complicated. See the section below on :ref:`arithmetic_nddata` for a discussion separate, more detailed discussion.
1616

1717
.. _arithmetic_standard:
1818

@@ -22,12 +22,12 @@ Standard Arithmetic Operations
2222
Addition and Subtraction with Numbers, Arrays and Quantities
2323
------------------------------------------------------------
2424

25-
Numbers, arrays and `~astropy.units.Quantity` can be added to and subtracted from an `~ndcube.NDCube` via the ``+`` and ``-`` operators.
26-
Note that addition and subtraction only changes the data values of the `~ndcube.NDCube`.
27-
Let's deomonstrate with an example `~ndcube.NDCube` called ``cube``
25+
Numbers, arrays and `~astropy.units.Quantity` can be added to and subtracted from `~ndcube.NDCube` via the ``+`` and ``-`` operators.
26+
Note that addition and subtraction only change the data values of the `~ndcube.NDCube`.
27+
Let's deomonstrate with an example `~ndcube.NDCube`, ``cube``
2828

2929
.. expanding-code-block:: python
30-
:summary: Expand to see my_cube instantiated.
30+
:summary: Expand to see cube instantiated.
3131

3232
>>> import astropy.units as u
3333
>>> import astropy.wcs
@@ -63,6 +63,7 @@ Let's deomonstrate with an example `~ndcube.NDCube` called ``cube``
6363
>>> cube.data
6464
array([[10, 11, 12],
6565
[13, 14, 15]])
66+
6667
>>> new_cube = cube + 1
6768
>>> new_cube.data
6869
array([[11, 12, 13],
@@ -78,6 +79,7 @@ We can also add an array if we want to add a different number to each data eleme
7879
>>> arr
7980
array([[0, 1, 2],
8081
[3, 4, 5]])
82+
8183
>>> new_cube = cube + arr
8284
>>> new_cube.data
8385
array([[10, 12, 14],
@@ -91,39 +93,45 @@ Subtraction works in the same way.
9193
>>> new_cube.data
9294
array([[ 9, 10, 11],
9395
[12, 13, 14]])
96+
9497
>>> new_cube = cube - arr
9598
>>> new_cube.data
9699
array([[10, 10, 10],
97100
[10, 10, 10]])
98101
99-
Note that ``cube`` has no unit, which is why we are able to add and subtract numbers and arrays.
102+
Note that int he above examples, ``cube`` has no unit.
103+
This is why we are able to add and subtract numbers and arrays.
100104
If, however, we have an `~ndcube.NDCube` with a unit assigned,
101105

102106
.. code-block:: python
103107
104-
>>> cube_unitful = NDCube(cube, unit=u.ct)
108+
>>> cube_with_unit = NDCube(cube, unit=u.ct)
105109
106110
then adding or subtracting an array or unitless number will raise an error.
107111
In such cases, we must use a `~astropy.units.Quantity` with a compatible unit:
108112

109113
.. code-block:: python
110114
111-
>>> cube_unitful.data
115+
>>> cube_with_unit.data
112116
array([[10, 11, 12],
113117
[13, 14, 15]])
114-
>>> new_cube = cube_unitful + 1 * u.ct # Adding a scalar quantity
118+
119+
>>> new_cube = cube_with_unit + 1 * u.ct # Adding a scalar quantity
115120
>>> new_cube.data
116121
array([[11., 12., 13.],
117122
[14., 15., 16.]])
118-
>>> new_cube = cube_unitful - 1 * u.ct # Subtracting a scalar quantity
123+
124+
>>> new_cube = cube_with_unit - 1 * u.ct # Subtracting a scalar quantity
119125
>>> new_cube.data
120126
array([[ 9., 10., 11.],
121127
[12., 13., 14.]])
122-
>>> new_cube = cube_unitful + arr * u.ct # Adding an array-like quantity
128+
129+
>>> new_cube = cube_with_unit + arr * u.ct # Adding an array-like quantity
123130
>>> new_cube.data
124131
array([[10., 12., 14.],
125132
[16., 18., 20.]])
126-
>>> new_cube = cube_unitful - arr * u.ct # Subtracting an array-like quantity
133+
134+
>>> new_cube = cube_with_unit - arr * u.ct # Subtracting an array-like quantity
127135
>>> new_cube.data
128136
array([[10., 10., 10.],
129137
[10., 10., 10.]])
@@ -133,8 +141,10 @@ Multiplying and Dividing with Numbers, Arrays and Quantities
133141

134142
An `~ndcube.NDCube` can be multiplied and divided by numbers, arrays, and `~astropy.units.Quantity` via the ``*`` and ``-`` operators.
135143
These work similarly to addition and subtraction with a few minor differences:
144+
136145
- The uncertainties of the resulting `~ndcube.NDCube` are scaled by the same factor as the data.
137146
- Classes with different units can be combined.
147+
138148
* e.g. an `~ndcube.NDCube` with a unit of counts divided by an `~astropy.units.Quantity` with a unit is seconds will result in an `~ndcube.NDCube` with a unit of counts per second.
139149
* This also holds for cases were unitful and unitless classes can be combined. In such cases, the unit of the resulting `~ndcube.NDCube` will be the same as that of the unitful object.
140150

@@ -143,21 +153,22 @@ Below are some examples.
143153
.. code-block:: python
144154
145155
>>> # See attributes of original cube.
146-
>>> cube_unitful.data
156+
>>> cube_with_unit.data
147157
array([[10, 11, 12],
148158
[13, 14, 15]])
149-
>>> cube_unitful.unit
159+
>>> cube_with_unit.unit
150160
Unit("ct")
151-
>>> cube_unitful.uncertainty
161+
>>> cube_with_unit.uncertainty
152162
StdDevUncertainty([[1. , 1.1, 1.2],
153163
[1.3, 1.4, 1.5]])
154164
155165
>>> # Multiply by a unitless array.
156-
>>> arr = 1 + np.arange(cube_unitful.data.size).reshape(cube_unitful.data.shape)
166+
>>> arr = 1 + np.arange(cube_with_unit.data.size).reshape(cube_with_unit.data.shape)
157167
>>> arr
158168
array([[1, 2, 3],
159169
[4, 5, 6]])
160-
>>> new_cube = cube_unitful * arr
170+
171+
>>> new_cube = cube_with_unit * arr
161172
162173
>>> # Inspect attributes of resultant cube.
163174
>>> new_cube.data
@@ -170,7 +181,7 @@ Below are some examples.
170181
[5.2, 7. , 9. ]])
171182
172183
>>> # Divide by an astropy Quantity.
173-
>>> new_cube = cube_unitful / (2 * u.s)
184+
>>> new_cube = cube_with_unit / (2 * u.s)
174185
175186
>>> # Inspect attributes of resultant cube.
176187
>>> new_cube.data
@@ -182,20 +193,26 @@ Below are some examples.
182193
StdDevUncertainty([[0.5 , 0.55, 0.6 ],
183194
[0.65, 0.7 , 0.75]])
184195
196+
Note that when performing arithmetic operations with `~ndcube.NDCube` and array-like objects, their shapes only have to be broadcastable.
197+
For example:
198+
199+
Raising NDCube to a Power
200+
-------------------------
201+
185202

186203
.. _arithmetic_nddata:
187204

188-
Arithmetic Operations between Coordinate-less NDData
189-
====================================================
205+
Arithmetic Operations with Coordinate-less NDData
206+
=================================================
190207

191208
Sometimes more advanced arithmetic operations are required.
192209
For example, we may want to create a sequence of running difference images which highlight changes between frames, and propagate the uncertainties associated with each image.
193210
Alternatively, we may want to subtract one image from another, but exclude a certain region of the image with a mask.
194211
In such cases, numbers, arrays and `~astropy.units.Quantity` are insufficient, and we would like to subtract two `~ndcube.NDCube` objects.
195212
This is not directly supported, but can still be achieved in practice, as we shall see below.
196213

197-
Why Arithmetic Operations with Coordinate-aware NDData Instances Are Not Directly Supported, and How the Same Result Can Be Achieved
198-
------------------------------------------------------------------------------------------------------------------------------------
214+
Why Arithmetic Operations with Coordinate-aware NDData Are Not Directly Supported, and How the Same Result Can Be Achieved
215+
--------------------------------------------------------------------------------------------------------------------------
199216

200217
Arithmetic operations between two `~ndcube.NDCube` instances (or equivalently, an `~ndcube.NDCube` and another coordinate-aware object) are not supported because of the possibility of supporting non-sensical operations.
201218
For example, what does it mean to multiply a spectrum and an image in a coordinate-aware way?
@@ -211,8 +228,8 @@ In many cases, a simple solution would be to extract the data (an optionally the
211228
.. expanding-code-block:: python
212229
:summary: Expand to see definition of cube1 and cube2.
213230

214-
>>> cube1 = cube_unitful
215-
>>> cube2 = cube_unitful / 4
231+
>>> cube1 = cube_with_unit
232+
>>> cube2 = cube_with_unit / 4
216233

217234
.. code-block:: python
218235

0 commit comments

Comments
 (0)