本文整理汇总了Python中rpy2.robjects.globalenv方法的典型用法代码示例。如果您正苦于以下问题:Python robjects.globalenv方法的具体用法?Python robjects.globalenv怎么用?Python robjects.globalenv使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rpy2.robjects
的用法示例。
在下文中一共展示了robjects.globalenv方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_cv_fold
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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
示例2: __init__
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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__)
示例3: _wrap
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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
示例4: _wrap2
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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
示例5: _make_pipe
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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
示例6: _make_pipe2
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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
示例7: clean_globalenv
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
def clean_globalenv():
yield
for name in rinterface.globalenv.keys():
del rinterface.globalenv[name]
示例8: test_cell_magic_localconverter
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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)
示例9: clean_globalenv
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
def clean_globalenv():
yield
for name in robjects.globalenv.keys():
del robjects.globalenv[name]
示例10: test_eval
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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
示例11: setup_func
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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)
示例12: permutation_test_ct
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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)
示例13: fit
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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)
示例14: run_baseline
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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}")
示例15: analyze
# 需要导入模块: from rpy2 import robjects [as 别名]
# 或者: from rpy2.robjects import globalenv [as 别名]
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