Skip to content

Commit 68dd711

Browse files
authored
Merge pull request #988 from ojustino/update-subtraction
Implement NotImplemented case in Spectrum1D subtraction
2 parents 695e475 + 3bc6a37 commit 68dd711

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

docs/spectrum1d.rst

+15-15
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ create it explicitly from arrays or `~astropy.units.Quantity` objects:
3636

3737
.. note::
3838
The ``spectral_axis`` can be either ascending or descending, but must be monotonic
39-
in either case.
39+
in either case.
4040

4141
Reading from a File
4242
-------------------
@@ -97,7 +97,7 @@ objects will propagate uncertainties.
9797
9898
Uncertainties are a special subclass of :class:`~astropy.nddata.NDData`, and their
9999
propagation rules are implemented at the class level. Therefore, users must
100-
specify the uncertainty type at creation time
100+
specify the uncertainty type at creation time.
101101
102102
.. code-block:: python
103103
@@ -248,9 +248,9 @@ axis is always the last.
248248
Slicing
249249
-------
250250
251-
As seen above, `~specutils.Spectrum1D` supports slicing in the same way as any
252-
other array-like object. Additionally, a `~specutils.Spectrum1D` can be sliced
253-
along the spectral axis using world coordinates.
251+
As seen above, `~specutils.Spectrum1D` supports slicing in the same way as any
252+
other array-like object. Additionally, a `~specutils.Spectrum1D` can be sliced
253+
along the spectral axis using world coordinates.
254254
255255
.. code-block:: python
256256
@@ -261,7 +261,7 @@ along the spectral axis using world coordinates.
261261
>>> spec_slice.spectral_axis
262262
<SpectralAxis [5002., 5003., 5004., 5005.] Angstrom>
263263
264-
It is also possible to slice on other axes using simple array indices at the
264+
It is also possible to slice on other axes using simple array indices at the
265265
same time as slicing the spectral axis based on spectral values.
266266
267267
.. code-block:: python
@@ -273,7 +273,7 @@ same time as slicing the spectral axis based on spectral values.
273273
>>> spec_slice.shape
274274
(2, 4)
275275
276-
If the `specutils.Spectrum1D` was created with a WCS that included spatial
276+
If the `specutils.Spectrum1D` was created with a WCS that included spatial
277277
information, for example in case of a spectral cube with two spatial dimensions,
278278
the `specutils.Spectrum1D.crop` method can be used to subset the data based on
279279
the world coordinates. The inputs required are two sets up `astropy.coordinates`
@@ -282,7 +282,7 @@ one of the coordinates is decreasing along an axis, the higher world coordinate
282282
value will apply to the lower bound input.
283283
284284
.. code-block:: python
285-
285+
286286
>>> from astropy.coordinates import SpectralCoord, SkyCoord
287287
>>> import astropy.units as u
288288
@@ -293,9 +293,9 @@ value will apply to the lower bound input.
293293
Collapsing
294294
----------
295295
296-
`~specutils.Spectrum1D` has built-in convenience methods for collapsing the
296+
`~specutils.Spectrum1D` has built-in convenience methods for collapsing the
297297
flux array of the spectrum via various statistics. The available statistics are
298-
mean, median, sum, max, and min, and may be called either on a specific axis
298+
mean, median, sum, max, and min, and may be called either on a specific axis
299299
(or axes) or over the entire flux array. The collapse methods currently respect
300300
the ``mask`` attribute of the `~specutils.Spectrum1D`, but do not propagate
301301
any ``uncertainty`` attached to the spectrum.
@@ -307,18 +307,18 @@ any ``uncertainty`` attached to the spectrum.
307307
<Quantity 0.4572145 Jy>
308308
309309
The 'axis' argument of the collapse methods may either be an integer axis, or a
310-
string specifying either 'spectral', which will collapse along only the
310+
string specifying either 'spectral', which will collapse along only the
311311
spectral axis, or 'spatial', which will collapse along all non-spectral axes.
312312
313313
.. code-block:: python
314314
315315
>>> spec.mean(axis='spatial') # doctest: +IGNORE_OUTPUT
316-
<Spectrum1D(flux=<Quantity [0.39985669, ... 0.38041483] Jy>,
316+
<Spectrum1D(flux=<Quantity [0.39985669, ... 0.38041483] Jy>,
317317
spectral_axis=<SpectralAxis ... [5000., ... 5009.]>
318318
319-
Note that in this case, the result of the collapse operation is a
320-
`~specutils.Spectrum1D` rather than an `astropy.units.Quantity`, because the
321-
collapse operation left the spectral axis intact.
319+
Note that in this case, the result of the collapse operation is a
320+
`~specutils.Spectrum1D` rather than an `astropy.units.Quantity`, because the
321+
collapse operation left the spectral axis intact.
322322
323323
It is also possible to supply your own function for the collapse operation by
324324
calling `~specutils.Spectrum1D.collapse()` and providing a callable function

specutils/spectra/spectrum1d.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,19 @@ def radial_velocity(self, val):
673673

674674
def __add__(self, other):
675675
if not isinstance(other, (NDCube, u.Quantity)):
676-
other = u.Quantity(other, unit=self.unit)
676+
try:
677+
other = u.Quantity(other, unit=self.unit)
678+
except TypeError:
679+
return NotImplemented
677680

678681
return self.add(other)
679682

680683
def __sub__(self, other):
681684
if not isinstance(other, NDCube):
682-
other = u.Quantity(other, unit=self.unit)
685+
try:
686+
other = u.Quantity(other, unit=self.unit)
687+
except TypeError:
688+
return NotImplemented
683689

684690
return self.subtract(other)
685691

0 commit comments

Comments
 (0)