473,472 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1695

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Gary Wessle | last post by:
Hi I am trying to install NumPy in my debian/testing linux 2.6.15-1-686. with no numpy for debian/testing, I am left alone, since the experimental version available by debian will result in a...
3
by: Iljya | last post by:
Hello, I need to pickle the type numpy.float32 but encounter an error when I try to do so. I am able to pickle the array itself, it is specifically the type that I cannot pickle. I am using:...
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
5
by: robert | last post by:
Turning algs for old NumPy modules into numpy code I suffer from this: Upon further processing of returns of numpy calculations, lots of data in an apps object tree will become elementary numpy...
1
by: Gerdus van Zyl | last post by:
I have the following, that is used to convert pixel data and thus should be as fast as possible: b = numpy.ndarray (shape=(w,h,4), dtype=numpy.uint8) a = numpy.frombuffer(buf, numpy.uint8)...
0
by: Robert Kern | last post by:
Marlin Rowley wrote: 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...
3
by: Sean Davis | last post by:
I have a set of numpy arrays which I would like to save to a gzip file. Here is an example without gzip: b=numpy.ones(1000000,dtype=numpy.uint8) a=numpy.zeros(1000000,dtype=numpy.uint8) fd =...
6
by: John [H2O] | last post by:
I have a script: from numpy import float OutD= v= OutD.append(]) On linux: Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12)
2
by: Travis Oliphant | last post by:
I wanted to point anybody interested to a blog post that describes a useful pattern for having a NumPy array that points to the memory created by a different memory manager than the standard one...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.