Skip to content

Quantity created with copy=True doesn't owndata #7244

Open

Description

@duncanmmacleod

It seems that the copy keyword for creating Quantity doesn't do what I expect it to, namely ensuring that the returned Quantity actually owns the data:

>>> import numpy
>>> from astropy.units import Quantity
>>> a = numpy.arange(4)
>>> print(a.flags.owndata)
True
>>> b = Quantity(a)
>>> print(b.flags.owndata)
False
>>> c = Quantity(a, copy=True)
>>> print(c.flags.owndata)
False
>>> d = Quantity(a).copy()
>>> print(d.flags.owndata)
True

This is all because internal to Quantity.__new__ the .value property is set using ndarray.view.
If the copy keyword is supposed to guarantee data ownership, can Quantity.__new__ get modified to use Quantity.copy, otherwise can the documentation be expanded a wee bit to highlight this potential confusion?

Maybe I've just missed some documentation that already exists somewhere, if so, sorry for the spam.

Activity

  1. mhvk commented on Feb 28, 2018

    @mhvk
    Contributor

    @duncanmmacleod - I think a Quantity can, as currently written, never own its data as it is always constructed via a view of an ndarray (which itself, though, holds a new copy of the data). In principle, I guess this could be changed by doing something like

    self = super().__new__(cls, shape=..., dtype=...)
    

    and then copying the data to self.

    Since we're doing the copy anyway, maybe there is a way to do this more immediately -- worth investigating since it might also speed up initialization just a little bit...

  2. mhvk commented on Feb 28, 2018

    @mhvk
    Contributor

    p.s. The way I usually test data is a copy is via np.may_share_memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions