Exemplo n.º 1
0
def setupdata(tabdata, doscale, group, error, dofit=None):
    if doscale:
        doscale = rpy2.robjects.vectors.FloatVector(tuple(doscale))

    if group in ('', 'NA', '1'):
        group = rinterface.NACharacterType()

    elif isinstance(group, tuple) or isinstance(group, list):
        group = rpy2.robjects.vectors.StrVector(list(group))
        tabdata, group = fusegroupcolumns(tabdata, group)

    if isinstance(dofit, tuple):
        dofit = rpy2.robjects.vectors.StrVector(tuple(dofit))
    else:
        dofit = rpy2.robjects.vectors.StrVector(tuple((0, 0)))

    if error in ('', 'NA'):
        error = rinterface.NACharacterType()

    dataframe = pyobj2dataframe(tabdata)

    return dataframe, doscale, group, error, dofit
Exemplo n.º 2
0
    def __value_py_to_r(value, ri):
        """Returns the R equivalent of a python value"""
        val = value.value
        if not isinstance(val, (list, tuple)):
            # Is this an iterable ?
            if hasattr(val,
                       '__iter__') and not isinstance(val, (str, unicode)):
                val = [v for v in val]
            else:
                # In R scalar values are vectors with one element
                # So here we put the scalar value into a list
                val = [val]

        na_value = None
        if value.type == 'bool':
            na_value = ri.NALogicalType()
        elif value.type == 'byte':
            na_value = ri.NAIntegerType(
            )  # I guess that's correct ? That should probably be tested though
        elif value.type == 'float':
            na_value = ri.NARealType()
        elif value.type == 'int':
            na_value = ri.NAIntegerType()
        elif value.type == 'string':
            na_value = ri.NACharacterType()

        # Scan the elements to replace Nones by NA
        val = [(na_value if v is None else v) for v in val]

        if value.type == 'bool':
            return ri.BoolSexpVector(val)
        if value.type == 'byte':
            return ri.ByteSexpVector(val)
        if value.type == 'float':
            return ri.FloatSexpVector(val)
        if value.type == 'int':
            return ri.IntSexpVector(val)
        if value.type == 'string':
            return ri.StrSexpVector(val)

        raise PredictionValueTypeException(value.type)
Exemplo n.º 3
0
 def testNACharacterRepr(self):
     na_str = ri.NACharacterType()
     self.assertEqual("NA_character_", repr(na_str))
Exemplo n.º 4
0
 def testNACharacterInVector(self):
     na_str = ri.NACharacterType()
     x = ri.StrSexpVector(("ab", na_str, "cd"))
     self.assertTrue(x[1] is na_str)
     self.assertEqual("ab", x[0])
     self.assertEqual("cd", x[2])
Exemplo n.º 5
0
 def testNACharactertoR(self):
     na_character = ri.NACharacterType()
     self.assertEqual(True, ri.baseenv["is.na"](ri.StrSexpVector((na_character, )))[0])
Exemplo n.º 6
0
 def testRtoNACharacter(self):
     na_character = ri.NACharacterType()
     r_na_character = evalr("NA_character_")[0]
     self.assertTrue(r_na_character is na_character)