Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _build(self, **kwargs):
from rpy2.robjects import numpy2ri, pandas2ri
match_it = self.install_matchit()
self.num_treatments = kwargs["num_treatments"]
self.batch_size = kwargs["batch_size"]
self.match_it = match_it
numpy2ri.activate()
pandas2ri.activate()
return super(PSM, self)._build(**kwargs)
def fitfn(t):
rpy2.robjects.numpy2ri.activate()
fitfn_r = rpy.r('fitfn_%s' % uuid_label)
val = np.asarray(fitfn_r(t))
rpy2.robjects.numpy2ri.deactivate()
return val
fitfns.append(fitfn)
def activate():
"""Activate!"""
global original_converter
if original_converter is not None:
return
original_converter = conversion.converter
numpy2ri.activate()
new_converter = conversion.Converter("scipy conversion", template=conversion.converter)
numpy2ri.deactivate()
conversion.set_conversion(new_converter)
def normalize_quantiles_r(array):
# install R package
# source('http://bioconductor.org/biocLite.R')
# biocLite('preprocessCore')
import rpy2.robjects as robjects
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()
robjects.r('require("preprocessCore")')
normq = robjects.r('normalize.quantiles')
return np.array(normq(array))
def init_r():
"""
1. Install R 3.4.1 with conda
2. Install XCMS and PITracer in R
3. Install rpy2 in with conda
"""
numpy2ri.activate()
wd = os.path.dirname(os.path.abspath(__file__)).replace('\\','/')
robjects.r(f'''setwd(\'{wd}\')''')
robjects.r('''source('r_functions.R')''')
PIT = robjects.globalenv['PIT']
XC = robjects.globalenv['XC']
return PIT, XC
def estimateQualityModel(self, holdback=0.3, threshold_score=0.7):
"""
Given a table of the "good" trip ids (i.e., the trace has sufficient information and the fit is good)
comes up with a prediction of the quality of that trip id
holdback is the fraction that is held back for out of sample validation
"""
import rpy2.robjects as ro
import rpy2.robjects.numpy2ri
import cPickle
rpy2.robjects.numpy2ri.activate()
df = self.getPostgresData()
self.scatterplots(df)
self.boxplots(df)
# split sample into estimation and reserve
random.seed(1)
ids_reserve = random.sample(df.index.tolist(), int(len(df)*holdback))
dfEst = df.loc[np.logical_not(df.index.isin(ids_reserve))]
dfRes = df.loc[ids_reserve]
models = {}
models['model1'] = ['pingtime_mean', 'pingtime_max', 'frechet_dist', 'gpsMatchRatio', 'matchGpsRatio',
'll_dist_mean', 'll_dist_min', 'll_topol_mean', 'll_topol_min', 'll_distratio_mean',
def init_rpy():
if rpy_initialized:
return
from rpy2 import robjects
from rpy2.robjects import numpy2ri
import os
path = os.path.dirname(__file__)
with open(path + "/Histogram.R", "r") as rfile:
code = ''.join(rfile.readlines())
robjects.r(code)
numpy2ri.activate()
def save_rds(data, filename):
import collections, re
import pandas as pd
import numpy as np
import rpy2.robjects as RO
import rpy2.rinterface as RI
from rpy2.robjects import numpy2ri
numpy2ri.activate()
from rpy2.robjects import pandas2ri
pandas2ri.activate()
# Supported data types:
# int, float, str, tuple, list, numpy array
# numpy matrix and pandas dataframe
int_type = (int, np.int8, np.int16, np.int32, np.int64)
float_type = (float, np.float)
def assign(name, value):
name = re.sub(r'[^\w' + '_.' + ']', '_', name)
if isinstance(value, (tuple, list)):
if all(isinstance(item, int_type) for item in value):
value = np.asarray(value, dtype=int)
elif all(isinstance(item, float_type) for item in value):
value = np.asarray(value, dtype=float)
else:
import functools, hashlib
import numpy as np
from scipy.stats import norm as normal_dbn
import regreg.api as rr
from selection.algorithms.debiased_lasso import pseudoinverse_debiasing_matrix
# load in the X matrix
import rpy2.robjects as rpy
from rpy2.robjects import numpy2ri
rpy.r('library(hdi); data(riboflavin); X = riboflavin$x')
numpy2ri.activate()
X_full = np.asarray(rpy.r('X'))
numpy2ri.deactivate()
from learn_selection.utils import full_model_inference, liu_inference, pivot_plot
from learn_selection.core import split_sampler, keras_fit, repeat_selection, infer_set_target
from learn_selection.Rutils import lasso_glmnet, cv_glmnet_lam
from learn_selection.learners import mixture_learner
def highdim_model_inference(X,
y,
truth,
selection_algorithm,
sampler,
lam_min,
dispersion,
success_params=(1, 1),