def numpy2ri(o): """ Augmented conversion function, converting numpy arrays into rpy2.rinterface-level R structures. """ if not o.dtype.isnative: raise(ValueError("Cannot pass numpy arrays with non-native byte orders at the moment.")) # Most types map onto R arrays: if o.dtype.kind in _kinds: # "F" means "use column-major order" vec = SexpVector(o.ravel("F"), _kinds[o.dtype.kind]) dim = SexpVector(o.shape, INTSXP) #FIXME: no dimnames ? #FIXME: optimize what is below needed/possible ? (other ways to create R arrays ?) res = rinterface.baseenv['array'](vec, dim=dim) # R does not support unsigned types: elif o.dtype.kind == "u": raise(ValueError("Cannot convert numpy array of unsigned values -- R does not have unsigned integers.")) # Array-of-PyObject is treated like a Python list: elif o.dtype.kind == "O": res = conversion.py2ri(list(o)) # Record arrays map onto R data frames: elif o.dtype.kind == "V": if o.dtype.names is None: raise(ValueError("Nothing can be done for this numpy array type %s at the moment." % (o.dtype,))) df_args = [] for field_name in o.dtype.names: df_args.append((field_name, conversion.py2ri(o[field_name]))) res = ro.baseenv["data.frame"].rcall(tuple(df_args), ro.globalenv) # It should be impossible to get here: else: raise(ValueError("Unknown numpy array type.")) return res
def __call__(self, *args, **kwargs): new_args = [conversion.py2ri(a) for a in args] new_kwargs = {} for k, v in kwargs.iteritems(): new_kwargs[k] = conversion.py2ri(v) res = super(RFunction, self).__call__(*new_args, **new_kwargs) res = conversion.ri2py(res) return res
def testScalar(self): i32 = numpy.int32(100) i32_r = conversion.py2ri(i32) i32_test = numpy.array(i32_r)[0] self.assertEqual(i32, i32_test) i64 = numpy.int64(100) i64_r = conversion.py2ri(i64) i64_test = numpy.array(i64_r)[0] self.assertEqual(i64, i64_test)
def testScalar(self): i32 = numpy.int32(100) i32_r = conversion.py2ri(i32) i32_test = numpy.array(i32_r)[0] self.assertEqual(i32, i32_test) i64 = numpy.int64(100) i64_r = conversion.py2ri(i64) i64_test = numpy.array(i64_r)[0] self.assertEqual(i64, i64_test) f128 = numpy.float128(100.000000003) f128_r = conversion.py2ri(f128) f128_test = numpy.array(f128_r)[0] self.assertEqual(f128, f128_test)
def checkHomogeneous(self, obj, mode, storage_mode): converted = 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]) return converted
def testObjectArray(self): o = numpy.array([1, "a", 3.2], dtype=numpy.object_) o_r = 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 py2ri_pandasseries(obj): if obj.dtype.name == 'category': res = py2ri_categoryseries(obj) res = FactorVector(res) elif obj.dtype == dt_datetime64ns_type: # time series d = [ IntVector([x.year for x in obj]), IntVector([x.month for x in obj]), IntVector([x.day for x in obj]), IntVector([x.hour for x in obj]), IntVector([x.minute for x in obj]), IntVector([x.second for x in obj]) ] res = ISOdatetime(*d) #FIXME: can the POSIXct be created from the POSIXct constructor ? # (is '<M8[ns]' mapping to Python datetime.datetime ?) res = POSIXct(res) else: # converted as a numpy array func = numpy2ri.converter.py2ri.registry[numpy.ndarray] # current conversion as performed by numpy res = func(obj) if len(obj.shape) == 1: if (obj.dtype != dt_O_type): # force into an R vector res = as_vector(res) # "index" is equivalent to "names" in R if obj.ndim == 1: res.do_slot_assign('names', StrVector(tuple(str(x) for x in obj.index))) else: res.do_slot_assign('dimnames', SexpVector(conversion.py2ri(obj.index))) return res
def fit(cls, data, x0=None, conditioning_method=ConditioningMethod.no_conditioning, **fixed_values): init_parms = {} for k in cls.params_names: if k in fixed_values: v = fixed_values[k] if isinstance(v, Parametrized): init_parms[k] = v else: init_parms[k] = ConstantParameter(v) init = cls(**init_parms) x0 = x0 if x0 is not None else init.params if len(x0) != len(init.params): raise ValueError( f"Expected {len(init.params)} values in x0, got {len(x0)}") def to_minimize(x): o = init.with_params(x) return o.opposite_log_likelihood( data, conditioning_method=conditioning_method) try: data = conversion.py2ri(data) results = list( nm_get(fminsearch(to_minimize, x0=np.array(x0)), "xopt")) except: results = minimize(to_minimize, np.array(x0), bounds=[(None, None), (0, None), (None, None)], method="Nelder-Mead").x return init.with_params(results)
def numpy_O_py2ri(o): if all((isinstance(x, str) or isinstance(x, bytes) or isinstance(x, unicode)) for x in o): res = StrSexpVector(o) else: res = conversion.py2ri(list(o)) return res
def py2ri_pandasseries(obj): if obj.dtype == '<M8[ns]': # time series d = [ IntVector([x.year for x in obj]), IntVector([x.month for x in obj]), IntVector([x.day for x in obj]), IntVector([x.hour for x in obj]), IntVector([x.minute for x in obj]), IntVector([x.second for x in obj]) ] res = ISOdatetime(*d) #FIXME: can the POSIXct be created from the POSIXct constructor ? # (is '<M8[ns]' mapping to Python datetime.datetime ?) res = POSIXct(res) else: # converted as a numpy array res = numpy2ri.numpy2ri(obj.values) # "index" is equivalent to "names" in R if obj.ndim == 1: res.do_slot_assign('names', StrVector(tuple(str(x) for x in obj.index))) else: res.do_slot_assign('dimnames', SexpVector(conversion.py2ri(obj.index))) return res
def numpy2ri(o): if isinstance(o, numpy.ndarray): if not o.dtype.isnative: raise(ValueError("Cannot pass numpy arrays with non-native byte orders at the moment.")) # The possible kind codes are listed at # http://numpy.scipy.org/array_interface.shtml kinds = { # "t" -> not really supported by numpy "b": rinterface.LGLSXP, "i": rinterface.INTSXP, # "u" -> special-cased below "f": rinterface.REALSXP, "c": rinterface.CPLXSXP, # "O" -> special-cased below "S": rinterface.STRSXP, "U": rinterface.STRSXP, # "V" -> special-cased below } # Most types map onto R arrays: if o.dtype.kind in kinds: # "F" means "use column-major order" vec = rinterface.SexpVector(o.ravel("F"), kinds[o.dtype.kind]) dim = rinterface.SexpVector(o.shape, rinterface.INTSXP) res = ro.r.array(vec, dim=dim) # R does not support unsigned types: elif o.dtype.kind == "u": raise(ValueError("Cannot convert numpy array of unsigned values -- R does not have unsigned integers.")) # Array-of-PyObject is treated like a Python list: elif o.dtype.kind == "O": res = conversion.py2ri(list(o)) # Record arrays map onto R data frames: elif o.dtype.kind == "V": if o.dtype.names is None: raise(ValueError("Nothing can be done for this numpy array type %s at the moment." % (o.dtype,))) df_args = [] for field_name in o.dtype.names: df_args.append((field_name, conversion.py2ri(o[field_name]))) res = ro.baseenv["data.frame"].rcall(tuple(df_args), ro.globalenv) # It should be impossible to get here: else: raise(ValueError("Unknown numpy array type.")) else: res = ro.default_py2ri(o) return res
def py2ri_pandasdataframe(obj): od = OrderedDict() for name, values in obj.iteritems(): if values.dtype.kind == 'O': od[name] = StrVector(values) else: od[name] = conversion.py2ri(values) return DataFrame(od)
def numpy_O_py2ri(o): if all(isinstance(x, str) for x in o): res = StrSexpVector(o) elif all(isinstance(x, bytes) for x in o): res = ByteSexpVector(o) else: res = conversion.py2ri(list(o)) return res
def testArray(self): i2d = numpy.array([[1, 2, 3], [4, 5, 6]], dtype="i") i2d_r = 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 = 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 testRecordArray(self): rec = numpy.array([(1, 2.3), (2, -0.7), (3, 12.1)], dtype=[("count", "i"), ("value", numpy.double)]) rec_r = 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 py2ri_pandasdataframe(obj): od = OrderedDict() for name, values in obj.iteritems(): try: od[name] = conversion.py2ri(values) except Exception as e: warnings.warn('Error while trying to convert ' 'the column "%s". Fall back to string conversion. ' 'The error is: %s' %\ (name, str(e))) od[name] = StrVector(values) return DataFrame(od)
def py2ri_pandasseries(obj): if obj.dtype == '<M8[ns]': # time series d = [IntVector([x.year for x in obj]), IntVector([x.month for x in obj]), IntVector([x.day for x in obj]), IntVector([x.hour for x in obj]), IntVector([x.minute for x in obj]), IntVector([x.second for x in obj])] res = ISOdatetime(*d) #FIXME: can the POSIXct be created from the POSIXct constructor ? # (is '<M8[ns]' mapping to Python datetime.datetime ?) res = POSIXct(res) else: # converted as a numpy array res = numpy2ri.numpy2ri(obj.values) # "index" is equivalent to "names" in R if obj.ndim == 1: res.do_slot_assign('names', ListVector({'x': conversion.py2ri(obj.index)})) else: res.do_slot_assign('dimnames', ListVector(conversion.py2ri(obj.index))) return res
def numpy2ri(o): """ Augmented conversion function, converting numpy arrays into rpy2.rinterface-level R structures. """ # allow array-likes to also function with this module. if not isinstance(o, numpy.ndarray) and hasattr(o, '__array__'): o = o.__array__() if isinstance(o, numpy.ndarray): if not o.dtype.isnative: raise(ValueError("Cannot pass numpy arrays with non-native byte orders at the moment.")) # Most types map onto R arrays: if o.dtype.kind in _kinds: # "F" means "use column-major order" vec = SexpVector(o.ravel("F"), _kinds[o.dtype.kind]) dim = SexpVector(o.shape, INTSXP) res = ro.r.array(vec, dim=dim) # R does not support unsigned types: elif o.dtype.kind == "u": raise(ValueError("Cannot convert numpy array of unsigned values -- R does not have unsigned integers.")) # Array-of-PyObject is treated like a Python list: elif o.dtype.kind == "O": res = conversion.py2ri(list(o)) # Record arrays map onto R data frames: elif o.dtype.kind == "V": if o.dtype.names is None: raise(ValueError("Nothing can be done for this numpy array type %s at the moment." % (o.dtype,))) df_args = [] for field_name in o.dtype.names: df_args.append((field_name, conversion.py2ri(o[field_name]))) res = ro.baseenv["data.frame"].rcall(tuple(df_args), ro.globalenv) # It should be impossible to get here: else: raise(ValueError("Unknown numpy array type.")) else: res = ro.default_py2ri(o) return res
def default_py2ri(o): """ Convert arbitrary Python object to :class:`rpy2.rinterface.Sexp` to objects, creating an R object with the content of the Python object in the process (wichi means data copying). :param o: object :rtype: :class:`rpy2.rinterface.Sexp` (and subclasses) """ if isinstance(o, RObject): res = rinterface.Sexp(o) if isinstance(o, rinterface.Sexp): res = o elif isinstance(o, array.array): if o.typecode in ('h', 'H', 'i', 'I'): res = rinterface.SexpVector(o, rinterface.INTSXP) elif o.typecode in ('f', 'd'): res = rinterface.SexpVector(o, rinterface.REALSXP) else: raise(ValueError("Nothing can be done for this array type at the moment.")) elif isinstance(o, bool): res = rinterface.SexpVector([o, ], rinterface.LGLSXP) elif isinstance(o, int): res = rinterface.SexpVector([o, ], rinterface.INTSXP) elif isinstance(o, float): res = rinterface.SexpVector([o, ], rinterface.REALSXP) elif isinstance(o, str): res = rinterface.SexpVector([o, ], rinterface.STRSXP) elif isinstance(o, unicode): res = rinterface.SexpVector([o, ], rinterface.STRSXP) elif isinstance(o, list): res = r.list(*[conversion.ri2py(conversion.py2ri(x)) for x in o]) elif isinstance(o, complex): res = rinterface.SexpVector([o, ], rinterface.CPLXSXP) else: raise(ValueError("Nothing can be done for the type %s at the moment." %(type(o)))) return res
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
def __init__(self, o): if not isinstance(o, rinterface.SexpVector): o = conversion.py2ri(o) super(RVector, self).__init__(o) self.r = RVectorDelegator(self)
def testScalar_f128(self): f128 = numpy.float128(100.000000003) f128_r = conversion.py2ri(f128) f128_test = numpy.array(f128_r)[0] self.assertEqual(f128, f128_test)
def __setitem__(self, i, value): value = conversion.py2ri(value) res = super(RVector, self).__setitem__(i, value)
def new(cls, data): """ Constructor for the class GGplot. """ data = conversion.py2ri(data) res = cls(cls._constructor(data)) return res
def py2ro_pandasdataframe(obj): ri_dataf = conversion.py2ri(obj) # cast down to an R list (goes through a different code path # in the DataFrame constructor, avoiding `str(k)`) ri_list = rinterface.SexpVector(ri_dataf) return RDataFrame(ri_list)
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)
def validobject(self, test = False, complete = False): """ Return whether the instance is 'valid' for its class. """ test = conversion.py2ri(test) complete = conversion.py2ri(complete) return methods_env['validObject'](self, test = test, complete = complete)[0]
def isclass(name): """ Return whether the given name is a defined class. """ name = conversion.py2ri(name) return methods_env['isClass'](name)[0]
def _2(obj): return conversion.py2ri(list(obj))