473,805 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: numpy.frombuffe r != unpack() ??

Marlin Rowley wrote:
All:

Say I have an array:

a = (['rrrrggggbbbbaa aa'],['rrrrggggbbbbaa aa'])

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 = (['rrrrggggbbbbaa aa'],['rrrrggggbbbbaa aa'])
b = numpy.frombuffe r(''.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***********@h otmail.com
To: ro*********@gma il.com; py*********@pyt hon.org
Subject: RE: numpy.frombuffe r != unpack() ??
Date: Fri, 16 May 2008 17:31:30 -0500

Thank you! That solved it!

-M
------------------------------------------------------------------------
To: py*********@pyt hon.org
From: ro*********@gma il.com
Subject: Re: numpy.frombuffe r != 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.frombuffe r(<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.frombuffe r(<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?so urce=EML_WL_%20 GreaterGood>

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

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

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

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

Similar topics

13
3935
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 dependency nightmares, so after unpacking the downloaded file "numpy-0.9.6.tar.gz"
3
3617
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: Numpy version: 0.9.4 Python version: 2.4.3 Windows XP
2
3974
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 had a problem, however we have a problem set where we are creating a large matrix and finding it's inverse to solve the problem. To invert the matrix I've tried using numpy.numarray.linear_algebra.inverse and...
5
4871
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 types. First there is some inefficiency in calculations. And then you get data inflation and questionable dependencies - e.g. with pickle,ZODB,mpi's ... : 0.0...
1
1557
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) a.shape = (w, h, 3) b = a b = a
0
188
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 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
3
5969
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 = file('test.dat','wb') a.tofile(fd) b.tofile(fd) fd.close()
6
2622
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
2791
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 used by NumPy. The pattern shows how to create a NumPy array that points to previously allocated memory and then shows how to construct an object that allows the correct deallocator to be called when the NumPy array is freed. This may be...
0
9718
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6876
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
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 we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.