<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style>
</head>
<body class='hmmessage'>Actually in my traversal of the never-ending maze of understanding arrays in python, I stumbled that the data in the wrong sequence.<BR>
 <BR>
Let's get back to this transpose() deal, say we have these values as integers (representing rgba again):<BR> <BR>[[[0,1,2,3]<BR>[4,5,6,7]<BR>[8,9,10,11]<BR>[12,13,14,15]]<BR> <BR>[[16,17,18,19]<BR>[20,21,22,23]<BR>[24,25,26,27]<BR>[28,29,30,31]]]<BR> <BR>Now if I do this: transpose((2,0,1)), I get this:<BR> <BR>[[[0,4,8,12] [16,20,24,28]]<BR>[[1,5,9,13] [17,21,25,29]]<BR>[[2,6,10,14][18,22,26,30]]<BR>[[3,7,11,15][19,23,27,31]]]<BR> <BR>This is NOT what I want.  I want the new array to be:<BR> <BR>[0,4,8,12][1,5,9,13]<BR>[2,6,10,14][3,7,11,15]<BR>[16,20,24,28][17,21,25,29]<BR>[18,22,26,30][19,23,27,31]<BR> <BR>How do I do this?<BR> <BR>-M<BR><BR><BR><BR><BR><BR>

<HR id=stopSpelling>
<BR>
> Date: Sat, 17 May 2008 08:58:08 -0700<BR>> From: gherron@islandtraining.com<BR>> To: marlin_rowley@hotmail.com<BR>> CC: python-list@python.org<BR>> Subject: Re: numpy.frombuffer != unpack() ??<BR>> <BR>> Marlin Rowley wrote:<BR>> > <BR>> > Very cool.<BR>> > <BR>> > > > a = (['rrrrggggbbbbaaaa'],['rrrrggggbbbbaaaa'])<BR>> > a represents a tile with height of 2 and width of 4 with 4 bits/pixel <BR>> > for each color.<BR>> > <BR>> > > >>> b = numpy.frombuffer(''.join(sum(a,[])),dtype='S1')<BR>> > this seperates the stream into individual values - Check<BR>> > <BR>> > > >>> b.shape=(2,4,4)<BR>> ><BR>> > This reshapes the array so that b.shape=(height,width,#bits/pixel) - Check<BR>> > <BR>> > >>> c = b.transpose((2,0,1))<BR>> > <BR>> > What does the (2,0,1) represent in terms of width and height and <BR>> > number of bytes?<BR>> <BR>> The (2,0,1) tells how to exchange the axes. For instance in a 2D array, <BR>> a normal transpose exchanges rows and columns. It will change a (a by <BR>> b) sized array into a (b by a) sized array. This would be equivalent to <BR>> the more saying interchange axes 0,1 to the new order of 1,0. <BR>> <BR>> In numpy with higher dimension arrays, the default transpose just <BR>> exchanges the first two axes, and the full transpose allows you to <BR>> specify exactly the new ordering of the exes. <BR>> <BR>> So transpose((2,0,1)) means take axes (0,1,2) to the new order <BR>> (2,1,0). In terms of sizes, an (a by b by c) sized array will end being <BR>> of size (c by a by b) in size. <BR>> <BR>> In terms of implementation, there may not be *any* data re-arrangement <BR>> in a transpose. The only thing that needs changing is how the indices <BR>> are converted to an actual machine address of an indexed item. The <BR>> documentation notes this by saying transpose returns a "new view" of the <BR>> array. This explains why I copied the array before extracting bytes <BR>> out of it -- you really do need the elements in the new order for the <BR>> next operation.<BR>> <BR>> Gary Herron<BR>> <BR>> ><BR>> ><BR>> ><BR>> ><BR>> > <BR>> > ------------------------------------------------------------------------<BR>> ><BR>> > > Date: Fri, 16 May 2008 17:08:20 -0700<BR>> > > From: gherron@islandtraining.com<BR>> > > To: marlin_rowley@hotmail.com; python-list@python.org<BR>> > > Subject: Re: numpy.frombuffer != unpack() ??<BR>> > ><BR>> > > Marlin Rowley wrote:<BR>> > > > All:<BR>> > > ><BR>> > > > Say I have an array:<BR>> > > ><BR>> > > > a = (['rrrrggggbbbbaaaa'],['rrrrggggbbbbaaaa'])<BR>> > > ><BR>> > > > How do I make it so that I now have:<BR>> > > ><BR>> > > > starting with first element (a[0])<BR>> > > > new_arr[0] = 'r'<BR>> > > > new_arr[1] = 'g'<BR>> > > > new_arr[2] = 'b'<BR>> > > > new_arr[3] = 'a'<BR>> > > > new_arr[4] = 'r'<BR>> > > > .....<BR>> > > ><BR>> > > > continuing "through" a[1] with the same new_arr<BR>> > > > new_arr[N] = 'r'<BR>> > > > new_arr[N+1] = 'g'<BR>> > > > ....<BR>> > > ><BR>> > > > -M<BR>> > ><BR>> > > Numpy can do this for you. First, do you really mean the array to<BR>> > > contain lists of one string each? If so:<BR>> > ><BR>> > > >>> import numpy<BR>> > > >>> a = (['rrrrggggbbbbaaaa'],['rrrrggggbbbbaaaa'])<BR>> > > >>> b = numpy.frombuffer(''.join(sum(a,[])),dtype='S1') # Kind of a<BR>> > > kludge here<BR>> > > >>> b<BR>> > > array(['r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'a',<BR>> > > 'a', 'a', 'a', 'r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b',<BR>> > > 'b', 'b', 'a', 'a', 'a', 'a'],<BR>> > > dtype='|S1')<BR>> > > >>> b.shape=(2,4,4)<BR>> > > >>> b<BR>> > > array([[['r', 'r', 'r', 'r'],<BR>> > > ['g', 'g', 'g', 'g'],<BR>> > > ['b', 'b', 'b', 'b'],<BR>> > > ['a', 'a', 'a', 'a']],<BR>> > ><BR>> > > [['r', 'r', 'r', 'r'],<BR>> > > ['g', 'g', 'g', 'g'],<BR>> > > ['b', 'b', 'b', 'b'],<BR>> > > ['a', 'a', 'a', 'a']]],<BR>> > > dtype='|S1')<BR>> > > >>> c = b.transpose((2,0,1))<BR>> > > >>> c<BR>> > > array([[['r', 'g', 'b', 'a'],<BR>> > > ['r', 'g', 'b', 'a']],<BR>> > ><BR>> > > [['r', 'g', 'b', 'a'],<BR>> > > ['r', 'g', 'b', 'a']],<BR>> > ><BR>> > > [['r', 'g', 'b', 'a'],<BR>> > > ['r', 'g', 'b', 'a']],<BR>> > ><BR>> > > [['r', 'g', 'b', 'a'],<BR>> > > ['r', 'g', 'b', 'a']]],<BR>> > > dtype='|S1')<BR>> > > >>> d=c.copy() # To make it contiguous<BR>> > > >>> d.shape = (32,)<BR>> > > >>> d<BR>> > > array(['r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r',<BR>> > > 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g',<BR>> > > 'b', 'a', 'r', 'g', 'b', 'a'],<BR>> > > dtype='|S1')<BR>> > ><BR>> > > Done. Cool no?<BR>> > ><BR>> > > Gary Herron<BR>> > ><BR>> > > ><BR>> > > ><BR>> > > ><BR>> > > ><BR>> > > > <BR>> > ------------------------------------------------------------------------<BR>> > > > From: marlin_rowley@hotmail.com<BR>> > > > To: robert.kern@gmail.com; python-list@python.org<BR>> > > > Subject: RE: numpy.frombuffer != unpack() ??<BR>> > > > Date: Fri, 16 May 2008 17:31:30 -0500<BR>> > > ><BR>> > > > Thank you! That solved it!<BR>> > > ><BR>> > > > -M<BR>> > > ><BR>> > > ><BR>> > > > <BR>> > ------------------------------------------------------------------------<BR>> > > ><BR>> > > > > To: python-list@python.org<BR>> > > > > From: robert.kern@gmail.com<BR>> > > > > Subject: Re: numpy.frombuffer != unpack() ??<BR>> > > > > Date: Fri, 16 May 2008 17:25:00 -0500<BR>> > > > ><BR>> > > > > Marlin Rowley wrote:<BR>> > > > > > All:<BR>> > > > > ><BR>> > > > > > I'm getting different floating point values when I use numpy<BR>> > > > vs. unpack().<BR>> > > > > ><BR>> > > > > > frgba = numpy.frombuffer(<string of bytes>, dtype=float32)<BR>> > > > > > buffer = unpack("!f", byte)<BR>> > > > > ><BR>> > > > > > frgba[0] != buffer[0]<BR>> > > > > ><BR>> > > > > > why? This is forcing me use the unpack() function since it's<BR>> > > > giving me<BR>> > > > > > the correct values. What am I doing wrong?<BR>> > > > ><BR>> > > > > Endianness, perhaps? '!' specifies big-endian data (an alias for<BR>> > > > '>'). Most<BR>> > > > > likely, you are on a little-endian platform. All of the dtypes<BR>> > > > in numpy default<BR>> > > > > to the native-endianness unless specified. If you want to read<BR>> > > > big-endian data<BR>> > > > > using numpy, do this:<BR>> > > > ><BR>> > > > > frgba = numpy.frombuffer(<string of bytes>, dtype='>f')<BR>> > > > ><BR>> > > > > If you have any more problems with numpy, please join us on the<BR>> > > > numpy mailing<BR>> > > > > list. When reporting problems, please try to provide a small but<BR>> > > > complete<BR>> > > > > snippet of self-contained code, the output that you got, and<BR>> > > > explain the output<BR>> > > > > that you expected to get. Thank you.<BR>> > > > ><BR>> > > > > http://www.scipy.org/Mailing_Lists<BR>> > > > ><BR>> > > > > --<BR>> > > > > Robert Kern<BR>> > > > ><BR>> > > > > "I have come to believe that the whole world is an enigma, a<BR>> > > > harmless enigma<BR>> > > > > that is made terrible by our own mad attempt to interpret it as<BR>> > > > though it had<BR>> > > > > an underlying truth."<BR>> > > > > -- Umberto Eco<BR>> > > > ><BR>> > > > > --<BR>> > > > > http://mail.python.org/mailman/listinfo/python-list<BR>> > > ><BR>> > > ><BR>> > > > <BR>> > ------------------------------------------------------------------------<BR>> > > > E-mail for the greater good. Join the i’m Initiative from<BR>> > > > Microsoft.<BR>> > > > <BR>> > <http://im.live.com/Messenger/IM/Join/Default.aspx?source=EML_WL_%20GreaterGood><BR>> > > ><BR>> > > ><BR>> > > ><BR>> > > > <BR>> > ------------------------------------------------------------------------<BR>> > > > E-mail for the greater good. Join the i’m Initiative from Microsoft.<BR>> > > > <BR>> > <http://im.live.com/Messenger/IM/Join/Default.aspx?source=EML_WL_%20GreaterGood> <BR>> ><BR>> > > ><BR>> > > > <BR>> > ------------------------------------------------------------------------<BR>> > > ><BR>> > > > --<BR>> > > > http://mail.python.org/mailman/listinfo/python-list<BR>> > ><BR>> ><BR>> ><BR>> > ------------------------------------------------------------------------<BR>> > Keep your kids safer online with Windows Live Family Safety. Help <BR>> > protect your kids. <BR>> > <http://www.windowslive.com/family_safety/overview.html?ocid=TXT_TAGLM_WL_Refresh_family_safety_052008> <BR>> ><BR>> > ------------------------------------------------------------------------<BR>> ><BR>> > --<BR>> > http://mail.python.org/mailman/listinfo/python-list<BR>> <BR><BR><br /><hr />Keep your kids safer online with Windows Live Family Safety. <a href='http://www.windowslive.com/family_safety/overview.html?ocid=TXT_TAGLM_WL_Refresh_family_safety_052008' target='_new'>Help protect your kids.</a></body>
</html>