This is a bug then.
The problem is in the way strides are computed (buffer.c in
rpy/rinterface), and a fix seems to be:
static void
sexp_strides(SEXP sexp, Py_intptr_t *strides, Py_ssize_t itemsize,
Py_intptr_t *shape, int nd)
{
/* Set the buffer 'strides', that is a vector or Py_intptr_t
* containing the offset (in bytes) when progressing along
* each dimension.
*/
int i;
Py_intptr_t cumul_strides = 1;
strides[0] = itemsize;
for (i = 1; i < nd; i++) {
strides[i] = shape[i-1] * strides[i-1];
}
}
I just committed that change to the branch 2.2.x. but I'd like to have
this tested a little before porting it back to 2.1.x.
Can you fill one on the bitbucket tracker ? (so others know about the issue)
Thanks,
L.
On 01/11/10 22:36, Denham Robert wrote:
> Yeah, sorry, this is the output:
> In [3]: import numpy
> In [4]: import rpy2
> In [5]: import rpy2.robjects as robjects
> In [7]: marr = robjects.r.array(robjects.IntVector(range(54)),dim=
> robjects.IntVector([9,2,3]))
>
> In [8]: marrpy = numpy.array(marr)
> In [9]: print marrpy.flatten()
> ------> print(marrpy.flatten())
> [ 0 2 4 9 11 13 1 3 5 10 12 14 2 4 6 11 13 15 3 5 7 12 14 16
> 4
> 6 8 13 15 17 5 7 9 14 16 18 6 8 10 15 17 19 7 9 11 16 18 20 8
> 10
> 12 17 19 21]
>
> In [10]: print robjects.r['as.vector'](marr)
> -------> print(robjects.r['as.vector'](marr))
> [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
> 22 23 24
> [26] 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
> 47 48 49
> [51] 50 51 52 53
>
>
> In [15]: print marrpy.flatten('F')
> -------> print(marrpy.flatten('F'))
> [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 2 3 4 5 6 7
> 8
> 9 10 11 12 13 14 15 16 17 18 19 4 5 6 7 8 9 10 11 12 13 14 15 16
> 17
> 18 19 20 21]
>
>
> I was prepared for differences due to column/row ordering, but this
> doesn't seem to be the problem.
>
> Works fine on matrices:
>
> In [11]: mat = robjects.r.matrix(robjects.IntVector(range(54)),nrow=9)
> In [12]: matpy = numpy.array(mat)
> In [13]: print mat
> -------> print(mat)
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 0 9 18 27 36 45
> [2,] 1 10 19 28 37 46
> [3,] 2 11 20 29 38 47
> [4,] 3 12 21 30 39 48
> [5,] 4 13 22 31 40 49
> [6,] 5 14 23 32 41 50
> [7,] 6 15 24 33 42 51
> [8,] 7 16 25 34 43 52
> [9,] 8 17 26 35 44 53
>
>
> In [14]: print matpy
> -------> print(matpy)
> [[ 0 9 18 27 36 45]
> [ 1 10 19 28 37 46]
> [ 2 11 20 29 38 47]
> [ 3 12 21 30 39 48]
> [ 4 13 22 31 40 49]
> [ 5 14 23 32 41 50]
> [ 6 15 24 33 42 51]
> [ 7 16 25 34 43 52]
> [ 8 17 26 35 44 53]]
>
>
>
> Regards, Robert
>
>
>
> -----Original Message-----
> From: Laurent Gautier [mailto:[email protected]]
> Sent: Monday, 1 November 2010 4:58 PM
> To: Denham Robert
> Cc: RPy help, support and design discussion list
> Subject: Re: [Rpy] converting r array to numpy array
>
> On 01/11/10 07:28, Denham Robert wrote:
>> I got caught out converting a 3d array in R to a numpy array. I did
>> something like:
>>
>> import numpy
>> import rpy2
>> import rpy2.robjects as robjects
>>
>> marr = robjects.r.array(robjects.IntVector(range(54)),dim=
>> robjects.IntVector([9,2,3]))
>> marrpy = numpy.array(marr)
>> print marrpy.flatten()
>> print robjects.r['as.vector'](marr)
>>
>> Which clearly doesn't work (at least for me, using rpy2.1.7, R-10.0 on
>> 64bit suse linux).
> Could you be more specific about "does not work" ?
> If you are after getting the same output for the two print calls, this
> is not happening because arrays are being stored "column-major" by R by
> while numpy has "row-major" as a default.
> Try:
>
> print marrpy.flatten('F')
>
>
>
>> The matrix version does work, eg
>>
>> mat = robjects.r.matrix(robjects.IntVector(range(54)),nrow=9)
>> matpy = numpy.array(mat)
>> print mat
>> print matpy
>>
>> I checked the documentation, and found examples for vectors and
>> matrices, but not for arrays, but also no warnings. So, I was just
>> wondering, is this supposed to work? If it is, a hint on why it
>> doesn't work for me would be good. If it isn't supposed to work, I was
>> wondering if it were possible to throw an error or something.
>
> It is supposed to work, but not claim about an absence of bugs is made.
>
>
> Best,
>
>
> Laurent
>
>
>> Regards,
>> Robert
>>
>>
>>
>> Robert Denham
>> Environmental Statistician, Remote Sensing Centre Environment and
>> Resource Sciences Phone 07 3896 9899 (ext 69899)
>> Email: [email protected]
>> www.derm.qld.gov.au<http://www.derm.qld.gov.au/>
>>
>> Department of Environment and Resource Management QCCE Building, 80
>> Meiers Rd, Indooroopilly QLD 4068
>>
>>
>> +----------------------------------------------------------------+
>> Think B4U Print
>> 1 ream of paper = 6% of a tree and 5.4kg CO2 in the atmosphere
>> 3 sheets of A4 paper = 1 litre of water
>> +----------------------------------------------------------------+
>>
>>
>> ----------------------------------------------------------------------
>> -------- Nokia and AT&T present the 2010 Calling All Innovators-North
>> America contest Create new apps& games for the Nokia N8 for consumers
>> in U.S. and Canada $10 million total in prizes - $4M cash, 500
>> devices, nearly $6M in marketing Develop with Nokia Qt SDK, Web
>> Runtime, or Java and Publish to Ovi Store
>> http://p.sf.net/sfu/nokia-dev2dev
>> _______________________________________________
>> rpy-list mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/rpy-list
------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
rpy-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rpy-list