Revision: 496 http://rpy.svn.sourceforge.net/rpy/?rev=496&view=rev Author: lgautier Date: 2008-04-19 07:43:10 -0700 (Sat, 19 Apr 2008)
Log Message: ----------- Working R-to-Numeric casting. Use with caution while details such as the handling of missing values is not fully tested. Modified Paths: -------------- branches/rpy_nextgen/rpy/rinterface/rinterface.c branches/rpy_nextgen/setup.py Added Paths: ----------- branches/rpy_nextgen/rpy/rinterface/array.h branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVectorNumeric.py Added: branches/rpy_nextgen/rpy/rinterface/array.h =================================================================== --- branches/rpy_nextgen/rpy/rinterface/array.h (rev 0) +++ branches/rpy_nextgen/rpy/rinterface/array.h 2008-04-19 14:43:10 UTC (rev 496) @@ -0,0 +1,11 @@ + +#ifndef RPY_AR_H +#define RPY_AR_H + +#include <R.h> +#include <Python.h> + +static PyObject* +array_struct_get(SexpObject *self); + +#endif /* !RPY_AR_H */ Property changes on: branches/rpy_nextgen/rpy/rinterface/array.h ___________________________________________________________________ Name: svn:eol-style + native Modified: branches/rpy_nextgen/rpy/rinterface/rinterface.c =================================================================== --- branches/rpy_nextgen/rpy/rinterface/rinterface.c 2008-04-18 20:56:30 UTC (rev 495) +++ branches/rpy_nextgen/rpy/rinterface/rinterface.c 2008-04-19 14:43:10 UTC (rev 496) @@ -67,6 +67,10 @@ #include <signal.h> +#include "rinterface.h" +#include "array.h" + + /* Back-compatibility with Python 2.4 */ #if (PY_VERSION_HEX < 0x02050000) typedef int Py_ssize_t; @@ -105,14 +109,6 @@ "); -/* Representation of R objects (instances) as instances in Python. - */ -typedef struct { - PyObject_HEAD - SEXP sexp; -} SexpObject; - - static SexpObject* globalEnv; static SexpObject* baseNameSpaceEnv; @@ -824,11 +820,8 @@ (lenfunc)VectorSexp_len, /* sq_length */ 0, /* sq_concat */ 0, /* sq_repeat */ - //FIXME: implement (ssizeargfunc)VectorSexp_item, /* sq_item */ - //FIXME: implement 0, //(ssizessizeargfunc)VectorSexp_slice, /* sq_slice */ - //FIXME: implement (ssizeobjargproc)VectorSexp_ass_item, /* sq_ass_item */ 0, /* sq_ass_slice */ 0, /* sq_contains */ @@ -836,6 +829,16 @@ 0 /* sq_inplace_repeat */ }; + +static PyGetSetDef VectorSexp_getsets[] = { + {"__array_struct__", + (getter)array_struct_get, + (setter)0, + "Array protocol: struct"}, + {NULL, NULL, NULL, NULL} /* sentinel */ +}; + + //FIXME: write more doc PyDoc_STRVAR(VectorSexp_Type_doc, "R object that is a vector.\ @@ -883,9 +886,9 @@ 0, /*tp_weaklistoffset*/ 0, /*tp_iter*/ 0, /*tp_iternext*/ - 0, /*tp_methods*/ + 0, /*tp_methods*/ 0, /*tp_members*/ - 0,//Sexp_getset, /*tp_getset*/ + VectorSexp_getsets, /*tp_getset*/ &Sexp_Type, /*tp_base*/ 0, /*tp_dict*/ 0, /*tp_descr_get*/ Added: branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVectorNumeric.py =================================================================== --- branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVectorNumeric.py (rev 0) +++ branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVectorNumeric.py 2008-04-19 14:43:10 UTC (rev 496) @@ -0,0 +1,59 @@ +import unittest +import itertools +import rpy2.rinterface as rinterface +import Numeric + + +try: + #FIXME: can starting and stopping an embedded R be done several times ? + rinterface.initEmbeddedR("foo", "--vanilla", "--no-save", "--quiet") +except: + pass + +def floatEqual(x, y, epsilon = 0.00000001): + return abs(x - y) < epsilon + +class SexpVectorNumericTestCase(unittest.TestCase): + + def testArrayStructInt(self): + px = [1,-2,3] + x = rinterface.SexpVector(px, rinterface.INTSXP) + nx = Numeric.asarray(x) + self.assertEquals('i', nx.typecode()) + for orig, new in itertools.izip(px, nx): + self.assertEquals(orig, new) + self.assertTrue(False) + + def testArrayStructDouble(self): + px = [1.0, -2.0, 3.0] + x = rinterface.SexpVector(px, rinterface.REALSXP) + nx = Numeric.asarray(x) + self.assertEquals('f', nx.typecode()) + for orig, new in itertools.izip(px, nx): + self.assertEquals(orig, new) + self.assertTrue(False) + + def testArrayStructComplex(self): + px = [1+2j, 2+5j, -1+0j] + x = rinterface.SexpVector(px, rinterface.CPLXSXP) + nx = Numeric.asarray(x) + self.assertEquals('D', nx.typecode()) + for orig, new in itertools.izip(px, nx): + self.assertEquals(orig, new) + self.assertTrue(False) + +# def testArrayStructBoolean(self): +# px = [True, False, True] +# x = rinterface.SexpVector(px, rinterface.REALSXP) +# nx = Numeric.asarray(x) +# self.assertEquals('b', nx.typecode()) +# for orig, new in itertools.izip(px, nx): +# self.assertEquals(orig, new) +# self.assertTrue(False) + +def suite(): + suite = unittest.TestLoader().loadTestsFromTestCase(SexpVectorNumericTestCase) + return suite + +if __name__ == '__main__': + unittest.main() Property changes on: branches/rpy_nextgen/rpy/rinterface/tests/test_SexpVectorNumeric.py ___________________________________________________________________ Name: svn:eol-style + native Modified: branches/rpy_nextgen/setup.py =================================================================== --- branches/rpy_nextgen/setup.py 2008-04-18 20:56:30 UTC (rev 495) +++ branches/rpy_nextgen/setup.py 2008-04-19 14:43:10 UTC (rev 496) @@ -4,6 +4,7 @@ from subprocess import Popen, PIPE +#FIXME: still needed ? try: import ctypes except Exception, e: @@ -80,8 +81,8 @@ rinterface = Extension( pack_name + ".rinterface.rinterface", - [os.path.join('rpy', 'rinterface', 'rinterface.c'), - os.path.join('rpy', 'rinterface', 'array.c')], + [os.path.join('rpy', 'rinterface', 'array.c'), + os.path.join('rpy', 'rinterface', 'rinterface.c')], include_dirs=[ os.path.join(RHOME, 'include'), os.path.join('rpy', 'rinterface')], libraries=['R', 'Rlapack', 'Rblas'], This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone _______________________________________________ rpy-list mailing list rpy-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rpy-list