Revision: 342
http://svn.sourceforge.net/rpy/?rev=342&view=rev
Author: warnes
Date: 2007-02-20 08:41:47 -0800 (Tue, 20 Feb 2007)
Log Message:
-----------
Initial attempt to apply Numpy support patch provided by Alexander Belopolsky
Modified Paths:
--------------
trunk/rpy/rpy.py
trunk/rpy/setup.py
trunk/rpy/src/RPy.h
trunk/rpy/src/rpymodule.c
trunk/rpy/tests/test_array.py
trunk/rpy/tests/test_numeric.py
trunk/rpy/tests/test_topy.py
Modified: trunk/rpy/rpy.py
===================================================================
--- trunk/rpy/rpy.py 2006-12-11 18:59:37 UTC (rev 341)
+++ trunk/rpy/rpy.py 2007-02-20 16:41:47 UTC (rev 342)
@@ -32,12 +32,17 @@
# installation time and RPy should have been compiled properly. So,
# don't complain.
try:
- from Numeric import *
- HAS_NUMERIC = 1
+ from numpy import *
+ HAS_NUMERIC = 3
except ImportError:
- HAS_NUMERIC = 0
- pass
+ try:
+ from Numeric import *
+ HAS_NUMERIC = 1
+ except ImportError:
+ HAS_NUMERIC = 0
+ pass
+
# Get necessary paths and version information
RHOME=os.environ.get('RHOME',None)
Modified: trunk/rpy/setup.py
===================================================================
--- trunk/rpy/setup.py 2006-12-11 18:59:37 UTC (rev 341)
+++ trunk/rpy/setup.py 2007-02-20 16:41:47 UTC (rev 342)
@@ -137,12 +137,18 @@
extra_compile_args=["-shared"]
source_files = source_files + ["src/setenv.c"]
- # check whether Numeric is present
+ # check whether NumPy is present
try:
- import Numeric
- DEFINE.append(('WITH_NUMERIC', None))
+ import numpy
+ DEFINE.append(('WITH_NUMERIC', '3'))
+ DEFINE.append(('PY_ARRAY_TYPES_PREFIX', 'PyArray_'))
+ include_dirs.append(numpy.get_numpy_include())
except ImportError:
- UNDEF.append('WITH_NUMERIC')
+ # fall back to Numeric
+ try:
+ DEFINE.append(('WITH_NUMERIC', '1'))
+ except ImportError:
+ UNDEF.append('WITH_NUMERIC')
# get the RPy version
from rpy_version import rpy_version
@@ -154,13 +160,9 @@
# 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 ) )
+ DEFINE.append( ('RPY_SHNAME', shlib_name))
DEFINE.append( ('INIT_RPY', 'init_rpy%s' % RVER ) )
-
modules.append( Extension(
shlib_name,
source_files,
Modified: trunk/rpy/src/RPy.h
===================================================================
--- trunk/rpy/src/RPy.h 2006-12-11 18:59:37 UTC (rev 341)
+++ trunk/rpy/src/RPy.h 2007-02-20 16:41:47 UTC (rev 342)
@@ -87,12 +87,23 @@
#include <signal.h>
+#ifdef WITH_NUMERIC
+# if WITH_NUMERIC == 3
+# include "numpy/arrayobject.h"
+# define PY_ARRAY_MODULE_NAME "numpy"
+# elif WITH_NUMERIC == 1
+# include "Numeric/arrayobject.h"
+# define PY_ARRAY_MODULE_NAME "multiarray"
+# else
+# error "unknown array variant"
+# endif
+#endif
+#define xstr(s) str(s)
+#define str(s) #s
+
#include "robjobject.h"
#include "setenv.h"
-#ifdef WITH_NUMERIC
-#include "Numeric/arrayobject.h"
-#endif
#define MAXIDSIZE 256
Modified: trunk/rpy/src/rpymodule.c
===================================================================
--- trunk/rpy/src/rpymodule.c 2006-12-11 18:59:37 UTC (rev 341)
+++ trunk/rpy/src/rpymodule.c 2007-02-20 16:41:47 UTC (rev 342)
@@ -1556,7 +1556,7 @@
if(use_numeric)
{
import_array();
- multiarray = PyImport_ImportModule("multiarray");
+ multiarray = PyImport_ImportModule(PY_ARRAY_MODULE_NAME);
if (multiarray) {
dict = PyModule_GetDict(multiarray);
if (dict)
@@ -1652,7 +1652,7 @@
R_DefParams(Rp);
/* set R_HOME */
- Rp->rhome = RHOME;
+ Rp->rhome = xstr(RHOME);
index = strlen(RUSER) - 1;
@@ -1733,7 +1733,7 @@
#endif
//m = Py_InitModule(MacroQuote(RPY_SHNAME), rpy_methods);
- m = Py_InitModule(RPY_SHNAME, rpy_methods);
+ m = Py_InitModule(xstr(RPY_SHNAME), rpy_methods);
d = PyModule_GetDict(m);
/* Save this interpreter */
Modified: trunk/rpy/tests/test_array.py
===================================================================
--- trunk/rpy/tests/test_array.py 2006-12-11 18:59:37 UTC (rev 341)
+++ trunk/rpy/tests/test_array.py 2007-02-20 16:41:47 UTC (rev 342)
@@ -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):
Modified: trunk/rpy/tests/test_numeric.py
===================================================================
--- trunk/rpy/tests/test_numeric.py 2006-12-11 18:59:37 UTC (rev 341)
+++ trunk/rpy/tests/test_numeric.py 2007-02-20 16:41:47 UTC (rev 342)
@@ -2,11 +2,14 @@
import unittest
try:
- from Numeric import *
+ from numpy import *
except ImportError:
- print 'Numeric not available. Skipping.\n'
- import sys
- sys.exit(0)
+ try:
+ from Numeric import *
+ except ImportError:
+ print 'Numeric not available. Skipping.\n'
+ import sys
+ sys.exit(0)
import sys
sys.path.insert(1, "..")
Modified: trunk/rpy/tests/test_topy.py
===================================================================
--- trunk/rpy/tests/test_topy.py 2006-12-11 18:59:37 UTC (rev 341)
+++ trunk/rpy/tests/test_topy.py 2007-02-20 16:41:47 UTC (rev 342)
@@ -26,9 +26,8 @@
assert(r.is_na(r.NA))
assert( r(-2147483648) == r.NA )
-# The following test fails on most architectures.
-# def testNAreal(self):
-# assert(r.is_nan( r("as.numeric(NA)")) )
+ def testNAreal(self):
+ assert(repr(r.NAN) == repr(r("as.numeric(NA)")) )
def testNAstring(self):
assert(r("as.character(NA)") == 'NA')
@@ -49,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(r.as_integer(xi) == [1, 2, r.NA, r.NA, 4])
+ assert(repr(r.as_numeric(xi)) == repr([1.0, 2.0, r.NAN, r.NAN, 4.0]))
+ assert(repr(r.as_integer(xi)) == repr([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] )
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
rpy-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rpy-list