All, I want to compare whether two arrays point to the same data. I've been using 'is' so far, but I'm wondering whether it's the right approach. If x is a plain ndarray, `x is x`, and `x is not x.view()`. I understand the second one (I think so...), `x` and `x.view `are two different Python objects. However, `x.__array_interface__ == x.view().__array_interface__`, which means that the underlying memory hasn't been modified at all, right ? In other terms, the data hasn't been copied, it's just being accessed slightly differently. So, when I'm using `x is y` to test whether some data has been copied, I should in fact compare the __array_interface__s, shouldn't I ? Sorry for the poor phrasing, and thanks a lot for your forthcoming inputs. P.
Pierre GM wrote:
All,
I want to compare whether two arrays point to the same data. I've been using 'is' so far, but I'm wondering whether it's the right approach.
It isn't. Your analysis is correct. -- 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
On Wednesday 07 February 2007 22:38:30 Robert Kern wrote:
Pierre GM wrote:
All,
I want to compare whether two arrays point to the same data. I've been using 'is' so far, but I'm wondering whether it's the right approach.
It isn't. Your analysis is correct.
So, there's no real point in using the Python 'id' function ? Do we need a shortcut to __array_interface__['data'] as id number ?
Pierre GM wrote:
On Wednesday 07 February 2007 22:38:30 Robert Kern wrote:
Pierre GM wrote:
All,
I want to compare whether two arrays point to the same data. I've been using 'is' so far, but I'm wondering whether it's the right approach. It isn't. Your analysis is correct.
So, there's no real point in using the Python 'id' function ?
Not for this purpose, no.
Do we need a shortcut to __array_interface__['data'] as id number ?
I'm not sure that would be useful. Different arrays may share the same starting pointer but have different strides, and arrays may also have different starting pointers but share some overlapping data. -- 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
Pierre GM wrote:
On Wednesday 07 February 2007 22:38:30 Robert Kern wrote:
Pierre GM wrote:
All,
I want to compare whether two arrays point to the same data. I've been using 'is' so far, but I'm wondering whether it's the right approach.
It isn't. Your analysis is correct.
So, there's no real point in using the Python 'id' function ? Do we need a shortcut to __array_interface__['data'] as id number ?
You have it (sort of a short-cut). .ctypes.data This works even if ctypes is not installed. -Travis
On Wednesday 07 February 2007 23:09:16 Travis Oliphant wrote:
So, there's no real point in using the Python 'id' function ? Do we need a shortcut to __array_interface__['data'] as id number ?
You have it (sort of a short-cut).
.ctypes.data
OK, great, thanks a lot On Wednesday 07 February 2007 23:05:46 Robert Kern wrote:
Do we need a shortcut to __array_interface__['data'] as id number ?
I'm not sure that would be useful. Different arrays may share the same starting pointer but have different strides, and arrays may also have different starting pointers but share some overlapping data.
So, in the first case, I can still compare the __array_interfaces__, right ? If they're equal, then the two arrays are strictly equivalent. In the second case, even if the arrays share some overlapping data, they're intrinsically different, so a test on whether the data has been copied/moved around would fail anyway. Well, thank y'all again.
Pierre GM wrote:
I want to compare whether two arrays point to the same data.
Travis just posted a note about a couple utility functions that may help: Travis Oliphant wrote:
In SVN there is a new function may_share_memory(a,b) which will return True if the memory foot-print of the two arrays over-lap.
may_share_memory(a, flipud(a)) True
This is based on another utility function byte_bounds that returns the byte-boundaries of any object exporting the Python side of the array interface.
Perhaps these utilities will help (I know they can be used to make the who function a bit more intelligent about how many bytes are being used).
-Travis
-- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (4)
-
Christopher Barker
-
Pierre GM
-
Robert Kern
-
Travis Oliphant