469,578 Members | 1,705 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

Re: numpy.frombuffer != unpack() ??

Marlin Rowley wrote:
All:

Say I have an array:

a = (['rrrrggggbbbbaaaa'],['rrrrggggbbbbaaaa'])

How do I make it so that I now have:

starting with first element (a[0])
new_arr[0] = 'r'
new_arr[1] = 'g'
new_arr[2] = 'b'
new_arr[3] = 'a'
new_arr[4] = 'r'
.....

continuing "through" a[1] with the same new_arr
new_arr[N] = 'r'
new_arr[N+1] = 'g'
....

-M
Numpy can do this for you. First, do you really mean the array to
contain lists of one string each? If so:
>>import numpy
a = (['rrrrggggbbbbaaaa'],['rrrrggggbbbbaaaa'])
b = numpy.frombuffer(''.join(sum(a,[])),dtype='S1') # Kind of a
kludge here
>>b
array(['r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'a',
'a', 'a', 'a', 'r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b',
'b', 'b', 'a', 'a', 'a', 'a'],
dtype='|S1')
>>b.shape=(2,4,4)
b
array([[['r', 'r', 'r', 'r'],
['g', 'g', 'g', 'g'],
['b', 'b', 'b', 'b'],
['a', 'a', 'a', 'a']],

[['r', 'r', 'r', 'r'],
['g', 'g', 'g', 'g'],
['b', 'b', 'b', 'b'],
['a', 'a', 'a', 'a']]],
dtype='|S1')
>>c = b.transpose((2,0,1))
c
array([[['r', 'g', 'b', 'a'],
['r', 'g', 'b', 'a']],

[['r', 'g', 'b', 'a'],
['r', 'g', 'b', 'a']],

[['r', 'g', 'b', 'a'],
['r', 'g', 'b', 'a']],

[['r', 'g', 'b', 'a'],
['r', 'g', 'b', 'a']]],
dtype='|S1')
>>d=c.copy() # To make it contiguous
d.shape = (32,)
d
array(['r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r',
'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g',
'b', 'a', 'r', 'g', 'b', 'a'],
dtype='|S1')

Done. Cool no?

Gary Herron
>

------------------------------------------------------------------------
From: ma***********@hotmail.com
To: ro*********@gmail.com; py*********@python.org
Subject: RE: numpy.frombuffer != unpack() ??
Date: Fri, 16 May 2008 17:31:30 -0500

Thank you! That solved it!

-M
------------------------------------------------------------------------
To: py*********@python.org
From: ro*********@gmail.com
Subject: Re: numpy.frombuffer != unpack() ??
Date: Fri, 16 May 2008 17:25:00 -0500
>
Marlin Rowley wrote:
All:

I'm getting different floating point values when I use numpy
vs. unpack().

frgba = numpy.frombuffer(<string of bytes>, dtype=float32)
buffer = unpack("!f", byte)

frgba[0] != buffer[0]

why? This is forcing me use the unpack() function since it's
giving me
the correct values. What am I doing wrong?
>
Endianness, perhaps? '!' specifies big-endian data (an alias for
'>'). Most
likely, you are on a little-endian platform. All of the dtypes
in numpy default
to the native-endianness unless specified. If you want to read
big-endian data
using numpy, do this:
>
frgba = numpy.frombuffer(<string of bytes>, dtype='>f')
>
If you have any more problems with numpy, please join us on the
numpy mailing
list. When reporting problems, please try to provide a small but
complete
snippet of self-contained code, the output that you got, and
explain the output
that you expected to get. Thank you.
>
http://www.scipy.org/Mailing_Lists
>
--
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
>
--
http://mail.python.org/mailman/listinfo/python-list


------------------------------------------------------------------------
E-mail for the greater good. Join the iím Initiative from
Microsoft.
<http://im.live.com/Messenger/IM/Join/Default.aspx?source=EML_WL_%20GreaterGood>

------------------------------------------------------------------------
E-mail for the greater good. Join the iím Initiative from Microsoft.
<http://im.live.com/Messenger/IM/Join/Default.aspx?source=EML_WL_%20GreaterGood>

------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #1
0 1459

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Gary Wessle | last post: by
3 posts views Thread by Iljya | last post: by
2 posts views Thread by Chris Smith | last post: by
1 post views Thread by Gerdus van Zyl | last post: by
reply views Thread by Robert Kern | last post: by
3 posts views Thread by Sean Davis | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.