question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

masked Quantity fails on astropy master with numpy 1.17.2

See original GitHub issue

Masked Quantity started failing with astropy master and numpy 1.17.2:

>>> import numpy as np
>>> import astropy.units as u

>>> masked_quantity = np.ma.array([1, 2, 3 ,4] * u.kg,
...                               mask=[True, False, True, False])
>>> print(masked_quantity)
Traceback (most recent call last):
  File "/Users/lbradley/Dropbox/softw/packages/dev/astropy/astropy/units/quantity.py", line 1319, in _to_own_unit
    _value = value.to_value(self.unit)
AttributeError: '_MaskedPrintOption' object has no attribute 'to_value'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./ma.py", line 8, in <module>
    print(masked_quantity)
  File "/Users/lbradley/local/conda/envs/dev/lib/python3.7/site-packages/numpy/ma/core.py", line 3870, in __str__
    return str(self._insert_masked_print())
  File "/Users/lbradley/local/conda/envs/dev/lib/python3.7/site-packages/numpy/ma/core.py", line 3864, in _insert_masked_print
    _recursive_printoption(res, mask, masked_print_option)
  File "/Users/lbradley/local/conda/envs/dev/lib/python3.7/site-packages/numpy/ma/core.py", line 2443, in _recursive_printoption
    np.copyto(result, printopt, where=mask)
  File "<__array_function__ internals>", line 6, in copyto
  File "/Users/lbradley/Dropbox/softw/packages/dev/astropy/astropy/units/quantity.py", line 1475, in __array_function__
    args, kwargs, unit, out = function_helper(*args, **kwargs)
  File "/Users/lbradley/Dropbox/softw/packages/dev/astropy/astropy/units/quantity_helper/function_helpers.py", line 270, in copyto
    return ((dst.view(np.ndarray), dst._to_own_unit(src)) + args,
  File "/Users/lbradley/Dropbox/softw/packages/dev/astropy/astropy/units/quantity.py", line 1324, in _to_own_unit
    as_quantity = Quantity(value)
  File "/Users/lbradley/Dropbox/softw/packages/dev/astropy/astropy/units/quantity.py", line 374, in __new__
    raise TypeError("The value must be a valid Python or "
TypeError: The value must be a valid Python or Numpy numeric type.

It works fine with astropy master and numpy <= 1.17.1. It also works fine with astropy 3.2.1 and numpy = 1.17.2:

>>> import numpy as np
>>> import astropy.units as u

>>> masked_quantity = np.ma.array([1, 2, 3 ,4] * u.kg,
...                               mask=[True, False, True, False])
>>> print(masked_quantity)
[-- 2.0 -- 4.0] kg

Ping: @mhvk

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
mhvkcommented, Oct 8, 2019

well, for one units do not propagate correctly… It is a long-standing wish list to have it work, but almost certainly we need to make our own masked type…

In [2]: q = np.ma.MaskedArray([1., 2., 3.]*u.m, mask=[True, False, True])

In [3]: q
Out[3]: 
masked_Quantity(data=[<Quantity -- m>, <Quantity 2. m>, <Quantity -- m>],
                mask=[ True, False,  True],
          fill_value=1e+20)

In [4]: q * q
Out[4]: 
masked_Quantity(data=[<Quantity -- m>, <Quantity 4. m>, <Quantity -- m>],
                mask=[ True, False,  True],
          fill_value=1e+20)
0reactions
mhvkcommented, Oct 8, 2019

Yes, there is, and that would certainly help (but might take a while…)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Masked Values (astropy.utils.masked) — Astropy v5.2
Astropy Masked instances behave like ndarray or subclasses such as Quantity but with a mask associated, which is propagated in operations such as...
Read more >
Release Notes — NumPy v1.17 Manual
This release contains a number of fixes for bugs reported against NumPy 1.17.0 along with a few documentation and build improvements. The Python...
Read more >
Release 1.1.dev0 Henry Leung
Calculate mean absolute error, ignoring the magic number ... mask (np.ndarray) – numpy boolean array as a mask for incoming tensor. Returns.
Read more >
xinyu7030/numpy - Gitiles
+* `#13950 <https://github.com/numpy/numpy/pull/13950>`__: Fixing failure on Python 2.7 on Windows 7 +* `#13952 ... REL: Update master after 1.17.2 release.
Read more >
Astropy complaining about Numpy version - python
I had a bad Numpy installation. The solution was to repeatedly uninstall Numpy, as regular user and root, until no more installations were ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found