/usr/lib/python2.7/dist-packages/rpy2/robjects/tests/testNumpyConversions.py is in python-rpy2 2.4.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | import unittest
import sys
import rpy2.robjects as robjects
import rpy2.robjects.conversion
r = robjects.r
has_numpy = True
try:
import numpy
import rpy2.robjects.numpy2ri as rpyn
except:
has_numpy = False
@unittest.skipUnless(has_numpy, 'numpy is not available in python')
class NumpyConversionsTestCase(unittest.TestCase):
def setUp(self):
#self._py2ri = robjects.conversion.py2ri
#self._ri2py = robjects.conversion.ri2py
rpyn.activate()
def tearDown(self):
rpyn.deactivate()
def testActivate(self):
# setUp method has already activated numpy converter
self.assertEqual(rpyn.numpy2ri, robjects.conversion.py2ri)
rpyn.deactivate()
self.assertNotEqual(rpyn.numpy2ri, robjects.conversion.py2ri)
def testActivateTwice(self):
# setUp method has already activated numpy converter
self.assertEqual(rpyn.numpy2ri, robjects.conversion.py2ri)
rpyn.activate()
self.assertEqual(rpyn.numpy2ri, robjects.conversion.py2ri)
rpyn.deactivate()
self.assertNotEqual(rpyn.numpy2ri, robjects.conversion.py2ri)
rpyn.deactivate()
self.assertNotEqual(rpyn.numpy2ri, robjects.conversion.py2ri)
def checkHomogeneous(self, obj, mode, storage_mode):
converted = robjects.conversion.py2ri(obj)
self.assertEqual(r["mode"](converted)[0], mode)
self.assertEqual(r["storage.mode"](converted)[0], storage_mode)
self.assertEqual(list(obj), list(converted))
self.assertTrue(r["is.array"](converted)[0])
def testVectorBoolean(self):
b = numpy.array([True, False, True], dtype=numpy.bool_)
self.checkHomogeneous(b, "logical", "logical")
def testVectorInteger(self):
i = numpy.array([1, 2, 3], dtype="i")
self.checkHomogeneous(i, "numeric", "integer")
def testVectorFloat(self):
f = numpy.array([1, 2, 3], dtype="f")
self.checkHomogeneous(f, "numeric", "double")
def testVectorComplex(self):
c = numpy.array([1j, 2j, 3j], dtype=numpy.complex_)
self.checkHomogeneous(c, "complex", "complex")
def testVectorCharacter(self):
if sys.version_info[0] == 3:
# bail out - strings are unicode and this is tested next
# test below
return
s = numpy.array(["a", "b", "c"], dtype="S")
self.checkHomogeneous(s, "character", "character")
def testVectorUnicodeCharacter(self):
u = numpy.array([u"a", u"b", u"c"], dtype="U")
self.checkHomogeneous(u, "character", "character")
def testArray(self):
i2d = numpy.array([[1, 2, 3], [4, 5, 6]], dtype="i")
i2d_r = robjects.conversion.py2ri(i2d)
self.assertEqual(r["storage.mode"](i2d_r)[0], "integer")
self.assertEqual(tuple(r["dim"](i2d_r)), (2, 3))
# Make sure we got the row/column swap right:
self.assertEqual(r["["](i2d_r, 1, 2)[0], i2d[0, 1])
f3d = numpy.arange(24, dtype="f").reshape((2, 3, 4))
f3d_r = robjects.conversion.py2ri(f3d)
self.assertEqual(r["storage.mode"](f3d_r)[0], "double")
self.assertEqual(tuple(r["dim"](f3d_r)), (2, 3, 4))
# Make sure we got the row/column swap right:
self.assertEqual(r["["](f3d_r, 1, 2, 3)[0], f3d[0, 1, 2])
def testObjectArray(self):
o = numpy.array([1, "a", 3.2], dtype=numpy.object_)
o_r = robjects.conversion.py2ri(o)
self.assertEqual(r["mode"](o_r)[0], "list")
self.assertEqual(r["[["](o_r, 1)[0], 1)
self.assertEqual(r["[["](o_r, 2)[0], "a")
self.assertEqual(r["[["](o_r, 3)[0], 3.2)
def testRecordArray(self):
rec = numpy.array([(1, 2.3), (2, -0.7), (3, 12.1)],
dtype=[("count", "i"), ("value", numpy.double)])
rec_r = robjects.conversion.py2ri(rec)
self.assertTrue(r["is.data.frame"](rec_r)[0])
self.assertEqual(tuple(r["names"](rec_r)), ("count", "value"))
count_r = r["$"](rec_r, "count")
value_r = r["$"](rec_r, "value")
self.assertEqual(r["storage.mode"](count_r)[0], "integer")
self.assertEqual(r["storage.mode"](value_r)[0], "double")
self.assertEqual(count_r[1], 2)
self.assertEqual(value_r[2], 12.1)
def testBadArray(self):
u = numpy.array([1, 2, 3], dtype=numpy.uint32)
self.assertRaises(ValueError, robjects.conversion.py2ri, u)
def testAssignNumpyObject(self):
x = numpy.arange(-10., 10., 1)
env = robjects.Environment()
env["x"] = x
self.assertEqual(1, len(env))
self.assertEqual(type(x), type(env["x"]))
def testDataFrameToNumpy(self):
df = robjects.vectors.DataFrame(dict((('a', 1), ('b', 2))))
rec = robjects.conversion.ri2ro(df)
self.assertEqual(numpy.recarray, type(rec))
self.assertEqual(1, rec.a[0])
self.assertEqual(2, rec.b[0])
def testAtomicVectorToNumpy(self):
v = robjects.vectors.IntVector((1,2,3))
a = rpyn.ri2numpy(v)
self.assertTrue(isinstance(a, numpy.ndarray))
self.assertEqual(1, v[0])
def testRx2(self):
df = robjects.vectors.DataFrame({
"A": robjects.vectors.IntVector([1,2,3]),
"B": robjects.vectors.IntVector([1,2,3])})
b = df.rx2('B')
self.assertEquals(tuple((1,2,3)), tuple(b))
def suite():
if has_numpy:
return unittest.TestLoader().loadTestsFromTestCase(NumpyConversionsTestCase)
else:
return unittest.TestLoader().loadTestsFromTestCase(MissingNumpyDummyTestCase)
if __name__ == '__main__':
unittest.main()
|