[Numpy-discussion] first recarray steps
Wed May 21 02:33:23 CDT 2008
Robert Kern wrote:
> On Wed, May 21, 2008 at 2:03 AM, Vincent Schut <email@example.com> wrote:
>> Robert Kern wrote:
>>> On Wed, May 21, 2008 at 1:48 AM, Vincent Schut <firstname.lastname@example.org> wrote:
>>>> Christopher Barker wrote:
>>>>> Also, if you image data is rgb, usually, that's a (width, height, 3)
>>>>> array: rgbrgbrgbrgb... in memory. If you have a (3, width, height)
>>>>> array, then that's rrrrrrr....gggggggg......bbbbbbbb. Some image libs
>>>>> may give you that, I'm not sure.
>>>> My data is. In fact, this is a simplification of my situation; I'm
>>>> processing satellite data, which usually has more (and other) bands than
>>>> just rgb. But the data is definitely in shape (bands, y, x).
>>> I don't think record arrays will help you much, then. Individual
>>> records need to be contiguous (bar padding). You can't interleave
>> Hmm, that was just what I was wondering about, when reading Stefan's
>> reply. So in fact, recarrays aren't just another way to view some data,
>> no matter in what shape it is.
>> So his solution:
>> x.T.reshape((-1,x.shape)).view(dt).reshape(x.shape[1:]).T won't work,
>> than. Or, at least, won't give me a view on my original dat, but would
>> give me a recarray with a copy of my data.
>> I guess I was misled by this text on the recarray wiki page:
>> "We would like to represent a small colour image. The image is two
>> pixels high and two pixels wide. Each pixel has a red, green and blue
>> colour component, which is represented by a 32-bit floating point number
>> between 0 and 1.
>> Intuitively, we could represent the image as a 3x2x2 array, where the
>> first dimension represents the color, and the last two the pixel
>> positions, i.e. "
>> Note the "3x2x2", which suggested imho that this would work with an
>> image with (bands,y,x) shape, not with (x,y,bands) shape.
> Yes, the tutorial goes on to use record arrays as a view onto an
> (x,y,bands) array and also make a (bands,x,y) view from that, too.
> That is, in fact, quite a confusing presentation of the subject.
> Now, there is a way to use record arrays here; it's a bit ugly but can
> be quite useful when parsing data formats. Each item in the record can
> also be an array. So let's pretend we have a (3,nx,ny) RGB array.
> nbands, nx, ny = a.shape
> dtype = numpy.dtype([
> ('r', a.dtype, [nx, ny]),
> ('g', a.dtype, [nx, ny]),
> ('b', a.dtype, [nx, ny]),
> # The flatten() is necessary to pre-empt numpy from
> # trying to do too much interpretation of a's shape.
> rec = a.flatten().view(dtype)
> print rec['r']
> print rec['g']
> print rec['b']
Ah, now that is clarifying! Thanks a lot. I'll do some experiments to
see whether this way of viewing my data is useful to me (in a sense that
making may code more readable is already very useful).
More information about the Numpy-discussion