[Numpy-discussion] [Rpy] RPy with NumPy

Sasha ndarray at mac.com
Fri Jan 27 22:45:04 EST 2006


I have succesfully (according to testall.py :-) converted RPy to use
numpy instead of Numeric.  Attached patch was tested on Linux only. I
did not attempt to create a package that would support both numpy and
Numeric, but it is easy to do. Please let me know if you are
interested.

For numpy-discussion readers: "RPy is a very simple, yet robust,
Python interface to the R Programming Language."
<http://rpy.sourceforge.net/>

For rpy-list readers: "Development for Numeric has ceased, and users
should transisition to NumPy as quickly as possible."
<http://numeric.scipy.org/>

For readers of both lists: Sorry for cross-posting.

-- sasha
-------------- next part --------------
diff -Naur rpy-0.4.6/rpy.py rpy-0.4.6-numpy/rpy.py
--- rpy-0.4.6/rpy.py	2005-07-27 13:56:54.000000000 -0400
+++ rpy-0.4.6-numpy/rpy.py	2006-01-27 12:06:13.628796000 -0500
@@ -30,7 +30,7 @@
 # installation time and RPy should have been compiled properly. So,
 # don't complain.
 try:
-    from Numeric import *
+    from numpy import *
     HAS_NUMERIC = 1
 except ImportError:
     HAS_NUMERIC = 0
diff -Naur rpy-0.4.6/setup.py rpy-0.4.6-numpy/setup.py
--- rpy-0.4.6/setup.py	2005-07-28 00:52:29.000000000 -0400
+++ rpy-0.4.6-numpy/setup.py	2006-01-28 01:18:46.148833000 -0500
@@ -34,7 +34,6 @@
 from distutils.errors import *
 import rpy_tools
 
-
 try:
     if sys.platform=="win32":
         RHOMES = os.environ['RHOMES'].split(';')
@@ -72,7 +71,7 @@
     if sys.platform=="win32":
         DEFINE.append(('R_HOME', '\\"%s\\"' % RHOME.replace("\\","/") ))
     else:
-        DEFINE.append(('R_HOME', '"%s"' % RHOME))
+        DEFINE.append(('R_HOME', r'\"%s\"' % RHOME))
 
     r_libs = [
               os.path.join(RHOME, 'bin'), 
@@ -132,8 +131,10 @@
 
     # check whether Numeric is present
     try:
-        import Numeric
+        import numpy
         DEFINE.append(('WITH_NUMERIC', None))
+        DEFINE.append(('PY_ARRAY_TYPES_PREFIX', 'PyArray_'))
+        include_dirs.append(numpy.get_numpy_include())
     except ImportError:
         UNDEF.append('WITH_NUMERIC')
 
@@ -148,12 +149,11 @@
     # use R version specific shared library name
     shlib_name = "_rpy%s" % RVER
     if sys.platform=="win32":
-        DEFINE.append( ('RPY_SHNAME', '\\"_rpy%s\\"' % RVER ) )
-    else:
         DEFINE.append( ('RPY_SHNAME', '"_rpy%s"' % RVER ) )
+    else:
+        DEFINE.append( ('RPY_SHNAME', r'\"_rpy%s\"' % RVER ) )
     DEFINE.append( ('INIT_RPY', 'init_rpy%s' % RVER ) )
 
-
     modules.append( Extension(
         shlib_name,
         source_files,
diff -Naur rpy-0.4.6/src/RPy.h rpy-0.4.6-numpy/src/RPy.h
--- rpy-0.4.6/src/RPy.h	2005-07-26 23:05:29.000000000 -0400
+++ rpy-0.4.6-numpy/src/RPy.h	2006-01-26 17:18:37.750157000 -0500
@@ -68,12 +68,13 @@
 
 #include <signal.h>
 
+#ifdef WITH_NUMERIC
+#include "numpy/arrayobject.h"
+#endif
+
 #include "robjobject.h"
 #include "setenv.h"
 
-#ifdef WITH_NUMERIC
-#include "Numeric/arrayobject.h"
-#endif
 
 #define MAXIDSIZE 256
 
diff -Naur rpy-0.4.6/src/rpymodule.c rpy-0.4.6-numpy/src/rpymodule.c
--- rpy-0.4.6/src/rpymodule.c	2005-07-28 10:23:33.000000000 -0400
+++ rpy-0.4.6-numpy/src/rpymodule.c	2006-01-28 01:19:29.281899000 -0500
@@ -1517,7 +1517,7 @@
   if(use_numeric)
     {
       import_array();
-      multiarray = PyImport_ImportModule("multiarray");
+      multiarray = PyImport_ImportModule("numpy");
       if (multiarray) {
         dict = PyModule_GetDict(multiarray);
         if (dict)
diff -Naur rpy-0.4.6/tests/test_array.py rpy-0.4.6-numpy/tests/test_array.py
--- rpy-0.4.6/tests/test_array.py	2005-07-18 14:54:54.000000000 -0400
+++ rpy-0.4.6-numpy/tests/test_array.py	2006-01-28 00:58:41.100628000 -0500
@@ -28,7 +28,7 @@
             r.array.autoconvert(BASIC_CONVERSION)
 
     def testConversionToPy(self):
-        self.failUnless(self.py == self.ra.as_py(),
+        self.failUnless((self.py == self.ra.as_py()).all(),
                         'wrong conversion to Python')
 
     def testConversionToR(self):
diff -Naur rpy-0.4.6/tests/test_numeric.py rpy-0.4.6-numpy/tests/test_numeric.py
--- rpy-0.4.6/tests/test_numeric.py	2005-07-26 23:08:49.000000000 -0400
+++ rpy-0.4.6-numpy/tests/test_numeric.py	2006-01-27 12:07:38.309828000 -0500
@@ -2,7 +2,7 @@
 
 import unittest
 try:
-    from Numeric import *
+    from numpy import *
 except ImportError:
     print 'Numeric not available. Skipping.\n'
     import sys
diff -Naur rpy-0.4.6/tests/test_topy.py rpy-0.4.6-numpy/tests/test_topy.py
--- rpy-0.4.6/tests/test_topy.py	2005-07-19 16:20:08.000000000 -0400
+++ rpy-0.4.6-numpy/tests/test_topy.py	2006-01-28 01:05:57.007083000 -0500
@@ -27,7 +27,7 @@
         assert( r(-2147483648) == r.NA )
 
     def testNAreal(self):
-        assert(r.NAN == r("as.numeric(NA)") )
+        assert(repr(r.NAN) == repr(r("as.numeric(NA)")) )
 
     def testNAstring(self):
         assert(r("as.character(NA)") == 'NA')
@@ -48,14 +48,14 @@
     def testNAList(self):
         xi = [1,2,r.NA,r.NAN,4]
         assert(r.as_character(xi) == ['1', '2', 'NA', 'NA', '4'])
-        assert(r.as_numeric(xi) == [1.0, 2.0, r.NAN, r.NAN, 4.0])
+        assert(repr(r.as_numeric(xi)) == repr([1.0, 2.0, r.NAN, r.NAN, 4.0]))
         assert(r.as_integer(xi) == [1, 2, r.NA, r.NA, 4])
         assert(r.as_factor(xi) == ['1', '2', 'NA', 'NA', '4'])
         assert(r.is_na(xi) == [False, False, True, True, False] )
 
         xd = [1.01,2.02,r.NA,r.NAN,4.04]
         assert(r.as_character(xd) == ['1.01', '2.02', 'NA', 'NA', '4.04'])
-        assert(r.as_numeric(xd) == [1.01, 2.02, r.NAN, r.NAN, 4.04])
+        assert(repr(r.as_numeric(xd)) == repr([1.01, 2.02, r.NAN, r.NAN, 4.04]))
         assert(r.as_integer(xd) == [1, 2, r.NA, r.NA, 4])
         assert(r.as_factor(xd) == ['1.01', '2.02', 'NA', 'NA', '4.04'])
         assert(r.is_na(xd) == [False, False, True, True, False] )






More information about the NumPy-Discussion mailing list