[Numpy-discussion] storage for records

Travis Oliphant oliphant.travis at ieee.org
Thu Feb 16 13:07:02 EST 2006


Stefan van der Walt wrote:

>Is there any way to control the underlying storage for a record?
>
>I am trying to use Travis' earlier example of an image with named fields:
>
>dt = N.dtype('<f12', [('r','<f4'),('g','<f4'),('b','<f4')])
>img = N.array(N.empty((rows,columns)), dtype=dt)
>
>Using this, I can access the different bands of the image using
>
>img['r'], img['g'], img['b'] (but not img.r as mentioned in some of
>the posts).
>  
>
Attribute lookup (img.r) is the purpose of the record array subclass.

rimg= img.view(numpy.recarray)

rimg.r --- will now work.

>'img' itself is a matrix of similar dimension as img['r'], but
>contains the combined items of type '<f12'.
>  
>
Be-ware that the '<f12' data-type (based on the C long double) is not 
available on all platforms ---
and on some platforms long double is '<f16' (there is no '<f12').   It's 
implementation specific.

>However, I would like to store the image as a 3xMxN array, with the r,
>g and b bands being contained in
>
>img[0], img[1] and img[2]
>  
>
You don't need a record array to do that.  Just define your array as a 
3xMxN array of floats.
But, you could just re-define the data-type as

img2 = img.view(('f4',3))  -- if img is MxN then img2 is MxNx3.

or use rimg.field(0) --- field was recently added to record-arrays.

>Is there a way to construct the record so that this structure is used
>for storage?  Further, how do I specify the dtype above, i.e.
>
>N.dtype('<f12', [('r','<f4'),('g','<f4'),('b','<f4')])
>
>in the style
>
>N.dtype({'names' : ['r','g','b'], 'formats': ['f4','f4','f4']})
>
>  
>
>(how do I specify that the combined type is 'f12')?
>  
>
Use a tuple

N.dtype(('<f12', {'names' : ['r','g','b'], 'formats': ['f4','f4','f4']}))



-Travis





More information about the NumPy-Discussion mailing list