Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that quantities are converted to Magnitude instances if needed. #5183

Merged
merged 1 commit into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions astropy/units/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,15 @@ def __quantity_subclass__(self, unit):

def _new_view(self, obj=None, unit=None):
"""
Create a Quantity view of obj, and set the unit
Create a Quantity view of some array-like input, and set the unit

By default, return a view of ``obj`` of the same class as ``self``
and with the unit passed on, or that of ``self``. Subclasses can
override the type of class used with ``__quantity_subclass__``, and
can ensure other properties of ``self`` are copied using
`__array_finalize__`.
By default, return a view of ``obj`` of the same class as ``self`` and
with the same unit. Subclasses can override the type of class for a
given unit using ``__quantity_subclass__``, and can ensure properties
other than the unit are copied using ``__array_finalize__``.

If the given unit defines a ``_quantity_class`` of which ``self``
is not an instance, a view using this class is taken.

Parameters
----------
Expand All @@ -624,9 +626,11 @@ def _new_view(self, obj=None, unit=None):
quantity_subclass = self.__class__
else:
unit = Unit(unit)
quantity_subclass, subok = self.__quantity_subclass__(unit)
if subok:
quantity_subclass = self.__class__
quantity_subclass = getattr(unit, '_quantity_class', Quantity)
if isinstance(self, quantity_subclass):
quantity_subclass, subok = self.__quantity_subclass__(unit)
if subok:
quantity_subclass = self.__class__

# We only want to propagate information from ``self`` to our new view,
# so obj should be a regular array. By using ``np.array``, we also
Expand Down
17 changes: 17 additions & 0 deletions astropy/units/tests/test_logarithmic.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,23 @@ def test_indirect_creation(self, unit):
assert (q2._function_view / u.mag).to(1).value == -5.


def test_conversion_to_and_from_physical_quantities():
"""Ensures we can convert from regular quantities."""
mst = [10., 12., 14.] * u.STmag
flux_lambda = mst.physical
mst_roundtrip = flux_lambda.to(u.STmag)
# check we return a logquantity; see #5178.
assert isinstance(mst_roundtrip, u.Magnitude)
assert mst_roundtrip.unit == mst.unit
assert_allclose(mst_roundtrip.value, mst.value)
wave = [4956.8, 4959.55, 4962.3] * u.AA
flux_nu = mst.to(u.Jy, equivalencies=u.spectral_density(wave))
mst_roundtrip2 = flux_nu.to(u.STmag, u.spectral_density(wave))
assert isinstance(mst_roundtrip2, u.Magnitude)
assert mst_roundtrip2.unit == mst.unit
assert_allclose(mst_roundtrip2.value, mst.value)


class TestLogQuantityViews(object):
def setup(self):
self.lq = u.Magnitude(np.arange(10.) * u.Jy)
Expand Down