Re: [Numpy-discussion] who owns the data?
On Wed, Nov 30, 2011 at 4:00 PM, Robert Kern wrote: > On Wed, Nov 30, 2011 at 20:30, wrote: >> just a basic question (since I haven't looked at this in some time) >> >> I'm creating a structured array in a function. However, I want to >> return the array with just a simple dtype >> >> uni = uni.view(dt).reshape(-1, ncols) >> return uni >> >> the returned uni has owndata=False. Who owns the data, since the >> underlying, original array went out of scope? > > Every time you make a view through .view(), slicing, .T, certain > restricted .reshape() calls , etc. a reference to the original object > is stored on the view. Consequently, the original object does not get > garbage collected until all of the views go away too. Making view of a > view just adds another link in the chain. In your example, the > original object that was assigned to `uni` before that last assignment > statement was executed maintains ownership of the memory. The new > ndarray object that gets assigned to `uni` for the return statement > refers to the temporary ndarray returned by .view() which in turn > refers to the original `uni` array which owns the actual memory. Thanks for the explanation. There where cases on the mailing list where views created problem, so I just thought of trying to own the data, but I don't think it's really relevant. > >> 2) >> uni.dtype = dt >> uni.reshape(-1, ncols) >> return uni >> >> this works and uni owns the data. > > uni.reshape() doesn't reshape `uni` inplace, though. It is possible > that your `uni` array wasn't contiguous to begin with. In all of the > cases that your first example would have owndata=False, this one > should too. this bug happened to me a few times now. I found it but only checked the flags before fixing it. Since reshape again creates a view, the next step is to assign to shape uni.shape = (uni.size//ncols, ncols) but that starts to look like too much inplace modifications just to avoid a view Thanks, Josef > >> I'm only worried whether assigning >> to dtype directly is not a dangerous thing to do. > > It's no worse than .view(dt). The same kind of checking goes on in both > places. > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless > enigma that is made terrible by our own mad attempt to interpret it as > though it had an underlying truth." > -- Umberto Eco > ___ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] who owns the data?
On Wed, Nov 30, 2011 at 20:30, wrote: > just a basic question (since I haven't looked at this in some time) > > I'm creating a structured array in a function. However, I want to > return the array with just a simple dtype > > uni = uni.view(dt).reshape(-1, ncols) > return uni > > the returned uni has owndata=False. Who owns the data, since the > underlying, original array went out of scope? Every time you make a view through .view(), slicing, .T, certain restricted .reshape() calls , etc. a reference to the original object is stored on the view. Consequently, the original object does not get garbage collected until all of the views go away too. Making view of a view just adds another link in the chain. In your example, the original object that was assigned to `uni` before that last assignment statement was executed maintains ownership of the memory. The new ndarray object that gets assigned to `uni` for the return statement refers to the temporary ndarray returned by .view() which in turn refers to the original `uni` array which owns the actual memory. > 2) > uni.dtype = dt > uni.reshape(-1, ncols) > return uni > > this works and uni owns the data. uni.reshape() doesn't reshape `uni` inplace, though. It is possible that your `uni` array wasn't contiguous to begin with. In all of the cases that your first example would have owndata=False, this one should too. > I'm only worried whether assigning > to dtype directly is not a dangerous thing to do. It's no worse than .view(dt). The same kind of checking goes on in both places. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] who owns the data?
just a basic question (since I haven't looked at this in some time) I'm creating a structured array in a function. However, I want to return the array with just a simple dtype uni = uni.view(dt).reshape(-1, ncols) return uni the returned uni has owndata=False. Who owns the data, since the underlying, original array went out of scope? alternatives 1) uni = np.asarray(uni, dt).reshape(-1, ncols) return uni looks obvious but raises exception 2) uni.dtype = dt uni.reshape(-1, ncols) return uni this works and uni owns the data. I'm only worried whether assigning to dtype directly is not a dangerous thing to do. >>> u array([0, 0, 0, 1, 1, 0, 1, 1]) >>> u.dtype = np.dtype("float") >>> u array([ 0.e+000, 2.12199579e-314, 4.94065646e-324, 2.12199579e-314]) adding a safety check: for t in uni.dtype.fields.values(): assert (t[0] == dt) maybe I shouldn't care if nobody owns the data. Thanks, Josef ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion