473,847 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Marlin Rowley wrote:

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'

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
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'],
>>b.shape=(2,4, 4)
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']]],
>>c = b.transpose((2, 0,1))
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']]],
>>d=c.copy() # To make it contiguous
d.shape = (32,)
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'],

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!

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:

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
snippet of self-contained code, the output that you got, and
explain the output
that you expected to get. Thank you.
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

E-mail for the greater good. Join the iím Initiative from
<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>


Jun 27 '08 #1
0 1736

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

Similar topics

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"
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
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...
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...
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
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
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()
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)
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...
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...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
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 presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
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.