/usr/lib/python2.7/dist-packages/rpy2/robjects/tests/testVector.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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | import unittest
import rpy2.robjects as robjects
ri = robjects.rinterface
import array, time, sys
import time
import datetime
import rpy2.rlike.container as rlc
from collections import OrderedDict
if sys.version_info[0] == 2:
range = xrange
rlist = robjects.baseenv["list"]
class VectorTestCase(unittest.TestCase):
def testNew(self):
identical = ri.baseenv["identical"]
py_a = array.array('i', [1,2,3])
ro_v = robjects.Vector(py_a)
self.assertEqual(ro_v.typeof, ri.INTSXP)
ri_v = ri.SexpVector(py_a, ri.INTSXP)
ro_v = robjects.Vector(ri_v)
self.assertTrue(identical(ro_v, ri_v)[0])
del(ri_v)
self.assertEqual(ri.INTSXP, ro_v.typeof)
def testNewStrVector(self):
vec = robjects.StrVector(['abc', 'def'])
self.assertEqual('abc', vec[0])
self.assertEqual('def', vec[1])
self.assertEqual(2, len(vec))
def testNewIntVector(self):
vec = robjects.IntVector([123, 456])
self.assertEqual(123, vec[0])
self.assertEqual(456, vec[1])
self.assertEqual(2, len(vec))
def testNewFloatVector(self):
vec = robjects.FloatVector([123.0, 456.0])
self.assertEqual(123.0, vec[0])
self.assertEqual(456.0, vec[1])
self.assertEqual(2, len(vec))
def testNewBoolVector(self):
vec = robjects.BoolVector([True, False])
self.assertEqual(True, vec[0])
self.assertEqual(False, vec[1])
self.assertEqual(2, len(vec))
def _testNewListVector(self, vec):
self.assertTrue('a' in vec.names)
self.assertTrue('b' in vec.names)
self.assertEqual(2, len(vec))
self.assertEqual(2, len(vec.names))
def testNewListVector(self):
vec = robjects.ListVector({'a': 1, 'b': 2})
self._testNewListVector(vec)
s = (('a', 1), ('b', 2))
vec = robjects.ListVector(s)
self._testNewListVector(vec)
it = iter(s)
vec = robjects.ListVector(s)
self._testNewListVector(vec)
def testAddOperators(self):
seq_R = robjects.r["seq"]
mySeqA = seq_R(0, 3)
mySeqB = seq_R(5, 7)
mySeqAdd = mySeqA + mySeqB
self.assertEqual(len(mySeqA)+len(mySeqB), len(mySeqAdd))
for i, li in enumerate(mySeqA):
self.assertEqual(li, mySeqAdd[i])
for j, li in enumerate(mySeqB):
self.assertEqual(li, mySeqAdd[i+j+1])
def testRAddOperators(self):
seq_R = robjects.r["seq"]
mySeq = seq_R(0, 10)
mySeqAdd = mySeq.ro + 2
for i, li in enumerate(mySeq):
self.assertEqual(li + 2, mySeqAdd[i])
def testRMultOperators(self):
seq_R = robjects.r["seq"]
mySeq = seq_R(0, 10)
mySeqAdd = mySeq.ro + mySeq
for i, li in enumerate(mySeq):
self.assertEqual(li * 2, mySeqAdd[i])
def testRPowerOperator(self):
seq_R = robjects.r["seq"]
mySeq = seq_R(0, 10)
mySeqPow = mySeq.ro ** 2
for i, li in enumerate(mySeq):
self.assertEqual(li ** 2, mySeqPow[i])
def testGetItem(self):
letters = robjects.baseenv["letters"]
self.assertEqual('a', letters[0])
self.assertEqual('z', letters[25])
def testGetItemOutOfBounds(self):
letters = robjects.baseenv["letters"]
self.assertRaises(IndexError, letters.__getitem__, 26)
def testSetItem(self):
vec = robjects.r.seq(1, 10)
vec[0] = 20
self.assertEqual(20, vec[0])
def testSetItemOutOfBounds(self):
vec = robjects.r.seq(1, 10)
self.assertRaises(IndexError, vec.__setitem__, 20, 20)
def getItemList(self):
mylist = rlist(letters, "foo")
idem = robjects.baseenv["identical"]
self.assertTrue(idem(letters, mylist[0]))
self.assertTrue(idem("foo", mylist[1]))
def testGetNames(self):
vec = robjects.Vector(array.array('i', [1,2,3]))
v_names = [robjects.baseenv["letters"][x] for x in (0,1,2)]
#FIXME: simplify this
r_names = robjects.baseenv["c"](*v_names)
vec = robjects.baseenv["names<-"](vec, r_names)
for i in range(len(vec)):
self.assertEqual(v_names[i], vec.names[i])
vec.names[0] = 'x'
def testSetNames(self):
vec = robjects.Vector(array.array('i', [1,2,3]))
names = ['x', 'y', 'z']
vec.names = names
for i in range(len(vec)):
self.assertEqual(names[i], vec.names[i])
def testNAInteger(self):
vec = robjects.IntVector(range(3))
vec[0] = robjects.NA_Integer
self.assertTrue(robjects.baseenv['is.na'](vec)[0])
def testNAReal(self):
vec = robjects.FloatVector((1.0, 2.0, 3.0))
vec[0] = robjects.NA_Real
self.assertTrue(robjects.baseenv['is.na'](vec)[0])
def testNALogical(self):
vec = robjects.BoolVector((True, False, True))
vec[0] = robjects.NA_Logical
self.assertTrue(robjects.baseenv['is.na'](vec)[0])
def testNAComplex(self):
vec = robjects.ComplexVector((1+1j, 2+2j, 3+3j))
vec[0] = robjects.NA_Complex
self.assertTrue(robjects.baseenv['is.na'](vec)[0])
def testNACharacter(self):
vec = robjects.StrVector('abc')
vec[0] = robjects.NA_Character
self.assertTrue(robjects.baseenv['is.na'](vec)[0])
def testRepr(self):
vec = robjects.IntVector((1,2,3))
s = repr(vec).split('\n')
self.assertEqual('[ 1, 2, 3]', s[1])
def testReprNonVectorInList(self):
vec = robjects.ListVector(OrderedDict((('a', 1),
('b', robjects.Formula('y ~ x')),
)))
s = repr(vec).split('\n')
self.assertEqual('[IntVector, Formula]', s[1].strip())
def testItems(self):
vec = robjects.IntVector(range(3))
vec.names = robjects.StrVector('abc')
names = [k for k,v in vec.items()]
self.assertEqual(['a', 'b', 'c'], names)
values = [v for k,v in vec.items()]
self.assertEqual([0, 1, 2], values)
def testItemsNoNames(self):
vec = robjects.IntVector(range(3))
names = [k for k,v in vec.items()]
self.assertEqual([None, None, None], names)
values = [v for k,v in vec.items()]
self.assertEqual([0, 1, 2], values)
class FactorVectorTestCase(unittest.TestCase):
def testNew(self):
vec = robjects.FactorVector(robjects.StrVector('abaabc'))
self.assertEqual(6, len(vec))
def testIsordered(self):
vec = robjects.FactorVector(robjects.StrVector('abaabc'))
self.assertFalse(vec.isordered)
def testNlevels(self):
vec = robjects.FactorVector(robjects.StrVector('abaabc'))
self.assertEqual(3, vec.nlevels)
def testLevels(self):
vec = robjects.FactorVector(robjects.StrVector('abaabc'))
self.assertEqual(3, len(vec.levels))
self.assertEqual(set(('a','b','c')), set(tuple(vec.levels)))
def testIter_labels(self):
values = 'abaabc'
vec = robjects.FactorVector(robjects.StrVector(values))
it = vec.iter_labels()
for a, b in zip(values, it):
self.assertEqual(a, b)
_dateval_tuple = (1984, 1, 6, 6, 22, 0, 1, 6, 0)
class DateTimeVectorTestCase(unittest.TestCase):
def setUp(self):
time.accept2dyear = False
def tearDown(self):
pass
def testPOSIXlt_fromInvalidPythonTime(self):
x = [time.struct_time(_dateval_tuple),
time.struct_time(_dateval_tuple)]
x.append('foo')
self.assertRaises(ValueError, robjects.POSIXlt, x)
def testPOSIXlt_fromPythonTime(self):
x = [time.struct_time(_dateval_tuple),
time.struct_time(_dateval_tuple)]
res = robjects.POSIXlt(x)
self.assertEqual(2, len(x))
def testPOSIXct_fromInvalidPythonTime(self):
x = [time.struct_time(_dateval_tuple),
time.struct_time(_dateval_tuple)]
x.append('foo')
# string 'foo' does not have attribute 'tm_zone'
self.assertRaises(AttributeError, robjects.POSIXct, x)
def testPOSIXct_fromPythonTime(self):
x = [time.struct_time(_dateval_tuple),
time.struct_time(_dateval_tuple)]
res = robjects.POSIXct(x)
self.assertEqual(2, len(x))
def testPOSIXct_fromPythonDatetime(self):
x = [datetime.datetime(*_dateval_tuple[:-2]),
datetime.datetime(*_dateval_tuple[:-2])]
res = robjects.POSIXct(x)
self.assertEqual(2, len(x))
def testPOSIXct_fromSexp(self):
sexp = robjects.r('ISOdate(2013, 12, 11)')
res = robjects.POSIXct(sexp)
self.assertEqual(1, len(res))
class ExtractDelegatorTestCase(unittest.TestCase):
def setUp(self):
self.console = robjects.rinterface.get_writeconsole()
def tearDown(self):
robjects.rinterface.set_writeconsole(self.console)
def testExtractByIndex(self):
seq_R = robjects.baseenv["seq"]
mySeq = seq_R(0, 10)
# R indexing starts at one
myIndex = robjects.Vector(array.array('i', range(1, 11, 2)))
mySubset = mySeq.rx(myIndex)
for i, si in enumerate(myIndex):
self.assertEqual(mySeq[si-1], mySubset[i])
def testExtractByName(self):
seq_R = robjects.baseenv["seq"]
mySeq = seq_R(0, 25)
letters = robjects.baseenv["letters"]
mySeq = robjects.baseenv["names<-"](mySeq,
letters)
# R indexing starts at one
myIndex = robjects.Vector(letters[2])
mySubset = mySeq.rx(myIndex)
for i, si in enumerate(myIndex):
self.assertEqual(2, mySubset[i])
def testExtractIndexError(self):
seq_R = robjects.baseenv["seq"]
mySeq = seq_R(0, 10)
# R indexing starts at one
myIndex = robjects.Vector(['a', 'b', 'c'])
def noconsole(x):
pass
robjects.rinterface.set_writeconsole(noconsole)
self.assertRaises(ri.RRuntimeError, mySeq.rx, myIndex)
def testReplace(self):
vec = robjects.IntVector(range(1, 6))
i = array.array('i', [1, 3])
vec.rx[rlc.TaggedList((i, ))] = 20
self.assertEqual(20, vec[0])
self.assertEqual(2, vec[1])
self.assertEqual(20, vec[2])
self.assertEqual(4, vec[3])
vec = robjects.IntVector(range(1, 6))
i = array.array('i', [1, 5])
vec.rx[rlc.TaggedList((i, ))] = 50
self.assertEqual(50, vec[0])
self.assertEqual(2, vec[1])
self.assertEqual(3, vec[2])
self.assertEqual(4, vec[3])
self.assertEqual(50, vec[4])
vec = robjects.IntVector(range(1, 6))
vec.rx[1] = 70
self.assertEqual(70, vec[0])
self.assertEqual(2, vec[1])
self.assertEqual(3, vec[2])
self.assertEqual(4, vec[3])
self.assertEqual(5, vec[4])
vec = robjects.IntVector(range(1, 6))
vec.rx[robjects.IntVector((1, 3))] = 70
self.assertEqual(70, vec[0])
self.assertEqual(2, vec[1])
self.assertEqual(70, vec[2])
self.assertEqual(4, vec[3])
self.assertEqual(5, vec[4])
m = robjects.r('matrix(1:10, ncol=2)')
m.rx[1, 1] = 9
self.assertEqual(9, m[0])
m = robjects.r('matrix(1:10, ncol=2)')
m.rx[2, robjects.IntVector((1,2))] = 9
self.assertEqual(9, m[1])
self.assertEqual(9, m[6])
def testExtractRecyclingRule(self):
# recycling rule
v = robjects.Vector(array.array('i', range(1, 23)))
m = robjects.r.matrix(v, ncol = 2)
col = m.rx(True, 1)
self.assertEqual(11, len(col))
def testExtractList(self):
# list
letters = robjects.baseenv["letters"]
myList = rlist(l=letters, f="foo")
idem = robjects.baseenv["identical"]
self.assertTrue(idem(letters, myList.rx("l")[0]))
self.assertTrue(idem("foo", myList.rx("f")[0]))
def suite():
suite = unittest.TestLoader().loadTestsFromTestCase(VectorTestCase)
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(FactorVectorTestCase))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(DateTimeVectorTestCase))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ExtractDelegatorTestCase))
return suite
if __name__ == '__main__':
unittest.main()
|