Python rpy2.robjects.globalenv() Examples
The following are 17
code examples of rpy2.robjects.globalenv().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
rpy2.robjects
, or try the search function
.
Example #1
Source File: dplyr.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def _make_pipe(rfunc, cls, env=robjects.globalenv): """ :param rfunc: An R function. :param cls: The class to use wrap the result of `rfunc`. :param env: A R environment. :rtype: A function.""" def inner(obj, *args, **kwargs): args_inenv = _fix_args_inenv(args, env) kwargs_inenv = _fix_kwargs_inenv(kwargs, env) res = rfunc(obj, *args_inenv, **kwargs_inenv) return cls(res) return inner
Example #2
Source File: benchmarks.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def setup_func(kind): #-- setup_sum-begin n = 20000 x_list = [random.random() for i in range(n)] module = None if kind == "array.array": import array as module res = module.array('f', x_list) elif kind == "numpy.array": import numpy as module res = module.array(x_list, 'f') elif kind == "FloatVector": import rpy2.robjects as module res = module.FloatVector(x_list) elif kind == "FloatSexpVector": import rpy2.rinterface as module module.initr() res = module.FloatSexpVector(x_list) elif kind == "FloatSexpVector-memoryview-array": import rpy2.rinterface as module module.initr() tmp = module.FloatSexpVector(x_list) mv = tmp.memoryview() res = array.array(mv.format, mv) elif kind == "list": res = x_list elif kind == "R": import rpy2.robjects as module res = module.rinterface.FloatSexpVector(x_list) module.globalenv['x'] = res res = None #-- setup_sum-end else: raise ValueError("Unknown kind '%s'" %kind) return (res, module)
Example #3
Source File: test_language.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def test_eval(clean_globalenv): code = """ x <- 1+2 y <- (x+1) / 2 """ res = lg.eval(code) assert 'x' in robjects.globalenv.keys() assert robjects.globalenv['x'][0] == 3 assert 'y' in robjects.globalenv.keys() assert robjects.globalenv['y'][0] == 2
Example #4
Source File: test_language.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def clean_globalenv(): yield for name in robjects.globalenv.keys(): del robjects.globalenv[name]
Example #5
Source File: test_rmagic.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def test_cell_magic_localconverter(ipython_with_magic, clean_globalenv): x = (1,2,3) from rpy2.rinterface import IntSexpVector def tuple_str(tpl): res = IntSexpVector(tpl) return res from rpy2.robjects.conversion import Converter my_converter = Converter('my converter') my_converter.py2rpy.register(tuple, tuple_str) from rpy2.robjects import default_converter foo = default_converter + my_converter snippet = textwrap.dedent(""" x """) # Missing converter/object with the specified name. ipython_with_magic.push({'x':x}) with pytest.raises(NameError): ipython_with_magic.run_cell_magic('R', '-i x -c foo', snippet) # Converter/object is not a converter. ipython_with_magic.push({'x':x, 'foo': 123}) with pytest.raises(TypeError): ipython_with_magic.run_cell_magic('R', '-i x -c foo', snippet) ipython_with_magic.push({'x':x, 'foo': foo}) with pytest.raises(NotImplementedError): ipython_with_magic.run_cell_magic('R', '-i x', snippet) ipython_with_magic.run_cell_magic('R', '-i x -c foo', snippet) assert isinstance(globalenv['x'], vectors.IntVector)
Example #6
Source File: test_rmagic.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def clean_globalenv(): yield for name in rinterface.globalenv.keys(): del rinterface.globalenv[name]
Example #7
Source File: dplyr.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def _make_pipe2(rfunc, cls, env=robjects.globalenv): """ :param rfunc: An R function. :param cls: The class to use wrap the result of `rfunc`. :param env: A R environment. :rtype: A function.""" def inner(obj_a, obj_b, *args, **kwargs): res = rfunc(obj_a, obj_b, *args, **kwargs) return cls(res) return inner
Example #8
Source File: dplyr.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def _wrap2(rfunc, cls, env=robjects.globalenv): def func(dataf_a, dataf_b, *args, **kwargs): res = rfunc(dataf_a, dataf_b, *args, **kwargs) if cls is None: return type(dataf_a)(res) else: return cls(res) return func
Example #9
Source File: dplyr.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def _wrap(rfunc, cls, env=robjects.globalenv): def func(dataf, *args, **kwargs): args_inenv = _fix_args_inenv(args, env) kwargs_inenv = _fix_kwargs_inenv(kwargs, env) res = rfunc(dataf, *args_inenv, **kwargs_inenv) if cls is None: return type(dataf)(res) else: return cls(res) return func
Example #10
Source File: grid.py From rpy2 with GNU General Public License v2.0 | 5 votes |
def __init__(self, *args, **kwargs): od = OrdDict() for item in args: od[None] = conversion.py2rpy(item) for k, v in kwargs.items(): od[k] = conversion.py2rpy(v) res = self._constructor.rcall(tuple(od.items()), robjects.globalenv) super().__init__(res.__sexp__)
Example #11
Source File: r_random_forest_clf.py From 2020plus with Apache License 2.0 | 5 votes |
def set_cv_fold(self, df): """Send which genes are valid test sets for each CV fold.""" if new_pandas_flag: r_df = pandas2ri.py2ri(df) else: r_df = com.convert_to_r_dataframe(df) ro.globalenv['cvFoldDf'] = r_df
Example #12
Source File: hypothesis_test.py From fairtest with Apache License 2.0 | 4 votes |
def permutation_test_ct(data, num_samples=100000): """ Monte-Carlo permutation test for a contingency table. Uses the chi square statistic. Parameters ---------- data : the contingency table num_samples : the number of random permutations to perform Returns ------- pval : the p-value Notes ----- Uses the R 'coin' package that can directly handle contingency tables instead of having to convert into arrays x,y References ---------- https://en.wikipedia.org/wiki/Resampling_(statistics) """ if not isinstance(data, pd.DataFrame): data = pd.DataFrame(data) data = data[data.columns[(data != 0).any()]] data = data[(data.T != 0).any()] # print 'permutation test of size {}'.format(data.sum()) data = np.array(data, dtype='int') if len(data.shape) != 2: return 1 if data.shape[0] < 2 or data.shape[1] < 2: return 1 ro.globalenv['ct'] = data pval = ro.r('chisq.test(ct, simulate.p.value = TRUE, B = {})$p.value'. format(num_samples))[0] return max(pval, 1.0/num_samples)
Example #13
Source File: test_rmagic.py From rpy2 with GNU General Public License v2.0 | 4 votes |
def test_Rconverter(ipython_with_magic, clean_globalenv): # If we get to dropping numpy requirement, we might use something # like the following: # assert tuple(buffer(a).buffer_info()) == tuple(buffer(b).buffer_info()) # numpy recarray (numpy's version of a data frame) dataf_np= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')], dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|%s1' % np_string_type)]) # store it in the notebook's user namespace ipython_with_magic.user_ns['dataf_np'] = dataf_np # equivalent to: # %Rpush dataf_np # that is send Python object 'dataf_np' into R's globalenv # as 'dataf_np'. The current conversion rules will make it an # R data frame. ipython_with_magic.run_line_magic('Rpush', 'dataf_np') # Now retreive 'dataf_np' from R's globalenv. Twice because # we want to test whether copies are made fromr_dataf_np = ipython_with_magic.run_line_magic('Rget', 'dataf_np') fromr_dataf_np_again = ipython_with_magic.run_line_magic('Rget', 'dataf_np') # check whether the data frame retrieved has the same content # as the original recarray assert len(dataf_np) == len(fromr_dataf_np) for col_i, col_n in enumerate(('x', 'y')): if has_pandas: assert isinstance(fromr_dataf_np, pd.DataFrame) assert tuple(dataf_np[col_i]) == tuple(fromr_dataf_np.iloc[col_i].values) else: # has_numpy then assert tuple(dataf_np[col_i]) == tuple(fromr_dataf_np[col_i]) # pandas2ri is currently making copies # # modify the data frame retrieved to check whether # # a copy was made # fromr_dataf_np['x'].values[0] = 11 # assert fromr_dataf_np_again['x'][0] == 11 # fromr_dataf_np['x'].values[0] = 1 # retrieve `dataf_np` from R into `fromr_dataf_np` in the notebook. ipython_with_magic.run_cell_magic('R', '-o dataf_np', 'dataf_np') dataf_np_roundtrip = ipython_with_magic.user_ns['dataf_np'] assert tuple(fromr_dataf_np['x']) == tuple(dataf_np_roundtrip['x']) assert tuple(fromr_dataf_np['y']) == tuple(dataf_np_roundtrip['y'])
Example #14
Source File: run.py From StackedDAE with Apache License 2.0 | 4 votes |
def analyze(sdae, datafile_norm,\ labels, mapped_labels=None,\ bias_node=False, prefix=None): """ Speeks to R, and submits it analysis jobs. """ # Get some R functions on the Python environment def_colors = robjects.globalenv['def_colors'] do_analysis = robjects.globalenv['do_analysis'] # labels.reset_index(level=0, inplace=True) def_colors(labels) act = np.float32(datafile_norm) try: do_analysis(act, sdae.get_weights, sdae.get_biases,\ pjoin(FLAGS.output_dir, "{}_R_Layer_".format(prefix)),\ bias_node=bias_node) except RRuntimeError as e: pass # for layer in sdae.get_layers: # fixed = False if layer.which > sdae.nHLayers - 1 else True # # try: # act = sdae.get_activation(act, layer.which, use_fixed=fixed) # print("Analysis for layer {}:".format(layer.which + 1)) # temp = pd.DataFrame(data=act) # do_analysis(temp, pjoin(FLAGS.output_dir,\ # "{}_Layer_{}"\ # .format(prefix, layer.which))) # # # if not fixed: # # weights = sdae.get_weights[layer.which] # # for node in weights.transpose(): # # sns.distplot(node, kde=False,\ # fit=stats.gamma, rug=True); # # sns.plt.show() # try: # plot_tSNE(act, mapped_labels,\ # plot_name="Pyhton_{}_tSNE_layer_{}"\ # .format(prefix, layer.which)) # except IndexError as e: # pass # except FailedPreconditionError as e: # break
Example #15
Source File: baseline_runner.py From SnowAlert with Apache License 2.0 | 4 votes |
def run_baseline(name, comment): from rpy2 import robjects as ro try: metadata = yaml.load(comment) assert type(metadata) is dict source = metadata['log source'] required_values = metadata['required values'] code_location = metadata['module name'] time_filter = metadata['filter'] time_column = metadata['history'] except Exception as e: log.error(e, f"{name} has invalid metadata: >{metadata}<, skipping") return os.mkdir(FORMATTED_CODE_DIRECTORY) files = os.listdir(f'../baseline_modules/{code_location}') shutil.copyfile( "../baseline_modules/run_module.R", f"{FORMATTED_CODE_DIRECTORY}/run_module.R" ) for file in files: print(file) if not file.startswith('.'): with open(f"../baseline_modules/{code_location}/{file}") as f: r_code = f.read() r_code = format_code(r_code, required_values) with open(f"{FORMATTED_CODE_DIRECTORY}/{file}", 'w+') as ff: ff.write(r_code) with open(f"{FORMATTED_CODE_DIRECTORY}/run_module.R") as fr: r_code = fr.read() frame = query_log_source(source, time_filter, time_column) ro.globalenv['input_table'] = frame ro.r(f"setwd('./{FORMATTED_CODE_DIRECTORY}')") output = ro.r(r_code) output = output.to_dict() results = unpack(output) # Get the columns of the baseline table; find the timestamp column and pop it from the list columns = [row['name'] for row in db.fetch(f'desc table {DATA_SCHEMA}.{name}')] columns.remove('EXPORT_TIME') try: log.info(f"{name} generated {len(results)} rows") db.insert(f"{DATA_SCHEMA}.{name}", results, columns=columns, overwrite=True) except Exception as e: log.error("Failed to insert the results into the target table", e) finally: shutil.rmtree(f"../{FORMATTED_CODE_DIRECTORY}")
Example #16
Source File: radmin.py From rpy2 with GNU General Public License v2.0 | 4 votes |
def __init__(self): window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_title("R") window.connect("delete_event", self.delete_event) window.connect("destroy", self.destroy) window.set_size_request(450, 500) notebook = gtk.Notebook() notebook.set_tab_pos(gtk.POS_LEFT) notebook.set_show_tabs(True) notebook.show() #vbox = gtk.VBox(homogeneous=False, spacing=0) #vbox.show() consolePanel = ConsolePanel() #tmp = robjects.baseenv["fifo"]("") #robjects.baseenv["sink"](tmp) #s = r.readLines(tmp) #r.close(tmp) #s = str.join(os.linesep, s._sexp) consolePanel.show() notebook.append_page(consolePanel, gtk.Label("Console")) codePanel = CodePanel() codePanel.show() notebook.append_page(codePanel, gtk.Label("Code")) # global env globalEnvPanel = EnvExplorer(robjects.globalenv) globalEnvPanel.show() notebook.append_page(globalEnvPanel, gtk.Label("globalEnv")) # global env grDevPanel = GraphicalDeviceExplorer() grDevPanel.show() notebook.append_page(grDevPanel, gtk.Label("Graphics")) # libraries/packages libPanel = LibraryPanel(console=consolePanel) libPanel.show() notebook.append_page(libPanel, gtk.Label("Libraries")) # vignettes vigPanel = VignetteExplorer() vigPanel.show() notebook.append_page(vigPanel, gtk.Label("Vignettes")) # doc docPanel = HelpExplorer() docPanel.show() notebook.append_page(docPanel, gtk.Label("Documentation")) window.add(notebook) window.show()
Example #17
Source File: r_random_forest_clf.py From 2020plus with Apache License 2.0 | 4 votes |
def fit(self, xtrain, ytrain): """The fit method trains R's random forest classifier. NOTE: the method name ("fit") and method signature were choosen to be consistent with scikit learn's fit method. Parameters ---------- xtrain : pd.DataFrame features for training set ytrain : pd.DataFrame true class labels (as integers) for training set """ label_counts = ytrain.value_counts() if self.is_onco_pred and self.is_tsg_pred: sampsize = [label_counts[self.other_num], label_counts[self.onco_num], label_counts[self.tsg_num]] elif self.is_onco_pred: sampsize = [label_counts[self.other_num], label_counts[self.onco_num]] elif self.is_tsg_pred: sampsize = [label_counts[self.other_num], label_counts[self.tsg_num]] self.set_sample_size(sampsize) ytrain.index = xtrain.index # ensure indexes match xtrain['true_class'] = ytrain # convert if new_pandas_flag: r_xtrain = pandas2ri.py2ri(xtrain) else: r_xtrain = com.convert_to_r_dataframe(xtrain) #ro.globalenv['trainData'] = r_xtrain self.rf = self.rf_fit(r_xtrain, self.ntrees, self.sample_size) r_imp = self.rf_imp(self.rf) # importance dataframe in R if new_pandas_flag: self.feature_importances_ = pandas2ri.ri2py(r_imp) else: self.feature_importances_ = com.convert_robj(r_imp) #self.feature_importances_ = pandas2ri.ri2py(r_imp)