def test_setslice():
    seq = (ri.FloatSexpVector([1.0]), ri.IntSexpVector([2, 3]),
           ri.StrSexpVector(['foo', 'bar']))
    vec = ri.ListSexpVector(seq)
    vec[0:2] = ri.ListSexpVector(
        [ri.FloatSexpVector([10.0]),
         ri.IntSexpVector([20, 30])])
    assert len(vec) == 3
    utils.assert_equal_sequence(vec[0], ri.FloatSexpVector([10.0]))
    utils.assert_equal_sequence(vec[1], ri.IntSexpVector([20, 30]))
    utils.assert_equal_sequence(vec[2], ri.StrSexpVector(['foo', 'bar']))
Exemple #2
0
 def testNewList(self):
     vec = ri.ListSexpVector([1,'b',3,'d',5])
     ok = ri.baseenv["is.list"](vec)[0]
     self.assertTrue(ok)
     self.assertEqual(5, len(vec))
     self.assertEqual(1, vec[0][0])
     self.assertEqual('b', vec[1][0])
def test_init_from_seq():
    seq = (ri.FloatSexpVector([1.0]), ri.IntSexpVector([2, 3]),
           ri.StrSexpVector(['foo', 'bar']))
    v = ri.ListSexpVector(seq)
    assert len(v) == 3
    for x, y in zip(seq, v):
        utils.assert_equal_sequence(x, y)
Exemple #4
0
def ri2numpy(o):
    if isinstance(o, ListSexpVector):
        if 'data.frame' in o.rclass:
            # R "factor" vectors will not convert well by default
            # (will become integers), so we build a temporary list o2
            # with the factors as strings.
            o2 = list()
            # An added complication is that the conversion defined
            # in this module will make __getitem__ at the robjects
            # level return numpy arrays
            for column in rinterface.ListSexpVector(o):
                if 'factor' in column.rclass:
                    levels = tuple(column.do_slot("levels"))
                    column = tuple(levels[x - 1] for x in column)
                o2.append(column)
            names = o.do_slot('names')
            if names is NULL:
                res = numpy.rec.fromarrays(o2)
            else:
                res = numpy.rec.fromarrays(o2, names=tuple(names))
        else:
            # not a data.frame, yet is it still possible to convert it
            res = ro.default_ri2ro(o)
    elif (o.typeof in _vectortypes) and (o.typeof != VECSXP):
        res = numpy.asarray(o)
    else:
        res = ro.default_ri2ro(o)
    return res
def test_getitem():
    seq = (ri.FloatSexpVector([1.0]), ri.IntSexpVector([2, 3]),
           ri.StrSexpVector(['foo', 'bar']))
    vec = ri.ListSexpVector(seq)
    utils.assert_equal_sequence(vec[1], ri.IntSexpVector([2, 3]))
    with pytest.raises(TypeError):
        vec[(2, 3)]
Exemple #6
0
def test_from_list():
    seq = (ri.FloatSexpVector([1.0]), ri.IntSexpVector([2, 3]),
           ri.StrSexpVector(['foo', 'bar']))
    sexp = ri.ListSexpVector(seq)
    isList = ri.globalenv.find('is.list')
    assert isList(sexp)[0]
    assert len(sexp) == 3
def test_getslice():
    seq = (ri.FloatSexpVector([1.0]), ri.IntSexpVector([2, 3]),
           ri.StrSexpVector(['foo', 'bar']))
    vec = ri.ListSexpVector(seq)
    vec_s = vec[0:2]
    assert len(vec_s) == 2
    utils.assert_equal_sequence(vec_s[0], ri.FloatSexpVector([1.0]))
    utils.assert_equal_sequence(vec_s[1], ri.IntSexpVector([2, 3]))
Exemple #8
0
def rpy2py_data_frame(obj):
    # R "factor" vectors will not convert well by default
    # (will become integers), so we build a temporary list o2
    # with the factors as strings.
    o2 = list()
    # An added complication is that the conversion defined
    # in this module will make __getitem__ at the robjects
    # level return numpy arrays
    for column in rinterface.ListSexpVector(obj):
        if 'factor' in column.rclass:
            levels = tuple(column.do_slot("levels"))
            column = tuple(levels[x-1] for x in column)
        o2.append(column)
    names = obj.do_slot('names')
    if names == rinterface.NULL:
        res = numpy.rec.fromarrays(o2)
    else:
        res = numpy.rec.fromarrays(o2, names=tuple(names))
    return res
def test_init_From_seq_invalid_elt():
    seq = (ri.FloatSexpVector([1.0]), lambda x: x,
           ri.StrSexpVector(['foo', 'bar']))
    with pytest.raises(Exception):
        ri.ListSexpVector(seq)
Exemple #10
0
def _py2rpy_list(obj):
    return vectors.ListVector(
        rinterface.ListSexpVector([conversion.py2rpy(x) for x in obj]))
Exemple #11
0
def _(obj):
    res = rinterface.ListSexpVector([conversion.py2ri(x) for x in obj])
    res.do_slot_assign('names', rinterface.StrSexpVector(obj.tags))
    return res
Exemple #12
0
def _(obj):
    return rinterface.ListSexpVector([conversion.py2ri(x) for x in obj])
Exemple #13
0
 def _3(obj):
     # return sequence_to_vector(obj)
     obj = rinterface.ListSexpVector([conversion.py2ri(x) for x in obj])
     return robjects.r.unlist(obj, recursive=False)
Exemple #14
0
 def _1(obj):
     keys = list(obj.keys())
     res = rinterface.ListSexpVector(
         [conversion.py2ri(obj[x]) for x in keys])
     res.do_slot_assign('names', rinterface.StrSexpVector(keys))
     return res
Exemple #15
0
 def testGetSliceList(self):
     vec = ri.ListSexpVector([1,'b',True])
     vec = vec[0:2]
     self.assertEqual(2, len(vec))
     self.assertEqual(1, vec[0][0])
     self.assertEqual('b', vec[1][0])
Exemple #16
0
 def testSetSliceList(self):
     vec = ri.ListSexpVector([1,'b',True])
     vec[0:2] = ri.ListSexpVector([False, 2])
     self.assertEqual(3, len(vec))
     self.assertEqual(False, vec[0][0])
     self.assertEqual(2, vec[1][0])
Exemple #17
0
def _py2rpy_taggedlist(obj):
    res = vectors.ListVector(
        rinterface.ListSexpVector([conversion.py2rpy(x) for x in obj]))
    res.do_slot_assign('names', rinterface.StrSexpVector(obj.tags))
    return res