Exemplo n.º 1
0
def _maybe_import_r_lib(name: str,
                        raise_exc: bool = False) -> Optional[Tuple[Any, str]]:
    global _r_lib, _r_lib_name

    if name == _r_lib_name and _r_lib is not None:
        return _r_lib, _r_lib_name

    try:
        from logging import ERROR

        from rpy2.robjects import r
        from rpy2.robjects.packages import PackageNotInstalledError, importr
        from rpy2.rinterface_lib.callbacks import logger

        logger.setLevel(ERROR)
        r["options"](warn=-1)

    except ImportError as e:
        raise ImportError(
            "Unable to import `rpy2`, install it first as `pip install rpy2` version `>=3.3.0`."
        ) from e

    try:
        _r_lib = importr(name)
        _r_lib_name = name
        return _r_lib, _r_lib_name
    except PackageNotInstalledError as e:
        if not raise_exc:
            return
        raise RuntimeError(
            f"Install R library `{name!r}` first as `install.packages({name!r}).`"
        ) from e
Exemplo n.º 2
0
def dirichletreg_df(prop_df,
                    covar_df,
                    formula,
                    onevsrest_category=None,
                    return_reg_input=False):
    from rpy2.robjects import r, Formula
    from rpy2.robjects.packages import importr
    from rpy2.rinterface_lib.callbacks import logger as rpy2_logger

    dr = importr('DirichletReg')
    dr_df = pd.concat([prop_df, covar_df], axis=1)

    f = Formula(formula)

    rpy2_logger.setLevel(
        logging.ERROR)  # will display errors, but not warnings
    f.environment['y'] = dr.DR_data(py2r(prop_df))
    rpy2_logger.setLevel(
        logging.WARNING)  # will display errors, but not warnings

    if onevsrest_category is None:
        fit = dr.DirichReg(f, py2r(dr_df))
    else:
        assert onevsrest_category in prop_df.columns
        cat_index = prop_df.columns.tolist().index(onevsrest_category) + 1
        fit = dr.DirichReg(f,
                           py2r(dr_df),
                           model='alternative',
                           **{'sub.comp': cat_index})

    r.sink(file='/dev/null')
    u = r.summary(fit)
    r.sink()
    if r('sink.number')()[0] > 0:
        r.sink()

    if onevsrest_category is None:
        varnames = u.rx2('varnames')
    else:
        varnames = [onevsrest_category] * 2

    coef_mat = u.rx2('coef.mat')
    rows = r2py(r('rownames')(coef_mat))
    coef_df = r2py(r('as.data.frame')(coef_mat)).reset_index(drop=True)
    coef_df.columns = ['coefficient', 'se', 'zval', 'pval']

    coef_df['compartment'] = np.repeat(varnames, r2py(u.rx2('n.vars')))
    coef_df['variable'] = rows
    coef_df['significance'] = bin_pval(coef_df.pval)

    if onevsrest_category is not None:
        coef_df['coef_type'] = np.repeat(['mean', 'precision'],
                                         r2py(u.rx2('n.vars')))

    if return_reg_input:
        return dr_df, coef_df
    else:
        return coef_df
Exemplo n.º 3
0
from rpy2.robjects import r
from rpy2.rinterface_lib.callbacks import logger as rpy2_logger
import logging
rpy2_logger.setLevel(logging.ERROR)
# rpy2_logger.addFilter(lambda record: 'Model is being refit with current smoothing parameters but initial states are being re-estimated' not in record.msg)
r("library(forecast)")

auto_arima = r(
    """
    function(y, xreg, seasonal){
        ts(y) %>% auto.arima(max.p=10, max.q=3, max.order=10, xreg=xreg, seasonal=seasonal, lambda='auto', stepwise=F, approximation=F)
    }
"""
)

get_arima_order = r("function(model){ model %>% arimaorder()}")

get_arima_lambda = r("function(model){ model$lambda}")

fit_arima = r(
    """
            function(order, y_train, X_train){
                ts(y_train) %>%
                    Arima(order=order, xreg=X_train)
            }
        """
)

refit_arima = r(
    """
            function(model, y_train, X_train){
Exemplo n.º 4
0
def read_rFCS(file_path):
    from rpy2.robjects import globalenv, pandas2ri, r
    from rpy2.robjects.packages import importr
    from rpy2.rinterface_lib.callbacks import logger
    import logging
    logger.setLevel(logging.ERROR)
    
    pandas2ri.activate()
    flowcore = importr("flowCore")
    base = importr("base")
    arg_transf = "transformation=FALSE"
    raw_FCS = flowcore.read_FCS(str(file_path), transformation=False,)
    r('''
        marker_names<-function(FF){
            return(flowCore::markernames(FF))
        }
        FF2dframe<-function(FF){
        if(class(FF) == "flowFrame"){
            return(as.data.frame(exprs(FF)))}
        else {
            stop("Object is not of type flowFrame")}
        }    
    ''')
    fcs_columns = globalenv["marker_names"](raw_FCS)
    df_file = globalenv["FF2dframe"](raw_FCS)
    if not isinstance(fcs_columns,np.ndarray): #Error checking for StrVector output from rpy2
        #This particular issue appeared with an update to a dependency in 20Q4/21Q1
        columns_list = []
        for i in fcs_columns:
            columns_list.append(i)
        fcs_columns = columns_list
    else:
        fcs_columns = fcs_columns.tolist()
    if not isinstance(df_file,pd.core.frame.DataFrame): #Error if the above can't be solved
        print("CALLUM'S ERROR. CONTACT @FerranC96")
        print(type(df_file),": ", df_file)
        for i in df_file:
            print (i)
    original_columns = df_file.columns.values.tolist()
    filtered_columns = []
    try:
        reg_markers = re.compile("(\d+Di$)")#|_dist$)") #All isotopes + debarcoder info
        reg_filter = re.compile("^\d+[A-Za-z]+")
        match_list=[]
        for i in original_columns:
            if reg_markers.search(i):
                match_list.append(i)
        for i in fcs_columns:
            if reg_filter.search(i):
                filtered_columns.append(i)
        if len(filtered_columns)==len(match_list) and len(filtered_columns)!=0:
            for new_n,old_n in zip(filtered_columns,match_list):
                df_file.rename({old_n: new_n}, axis="columns", inplace=True)
            no_filter=False
        elif len(match_list)==0 and len(filtered_columns)!=0: #Columns already have PnS?
            preprocessed_cols = []
            for i in original_columns:
                preprocessed_cols.append(i)
            if preprocessed_cols==filtered_columns: #Indeed, PnS and "PnN" match!
                print("WARNING: FCS was already processed. Keeping original columns")
                no_filter=False
            else:
                raise ValueError
        else:
            print("WARNING: $PnS (desc in flowCore) and $PnN channels don't match")
            print("marker PnS",fcs_columns)
            print("marker PnN (original column names)", match_list)
            raise ValueError
    except ValueError:
        print("ERROR: Couldn't read $PnS channel names")
        print("No filtering will be performed. Please manually rename your channels")
        no_filter=True #Not applying filtering to avoid losing unwated cols

    return df_file, no_filter
Exemplo n.º 5
0
from rpy2.rinterface_lib.callbacks import logger as rpy2_logger
import logging
rpy2_logger.setLevel(logging.ERROR)
'''Beginning with 3.0 version, rpy2 does not use Python's warnings module anymore. 
It now relies on logging instead. The new solution would be:

from rpy2.rinterface_lib.callbacks import logger as rpy2_logger
import logging
rpy2_logger.setLevel(logging.ERROR)   # will display errors, but not warnings

If you want to filter specific warnings only, use:

rpy2_logger.addFilter(lambda record: 'notch went outside hinges' not in record.msg)
See LogRecord class specification for available fields for advanced filtering.
'''

#import warnings
#from rpy2.rinterface import RRuntimeWarning
#warnings.filterwarnings("ignore", category=RRuntimeWarning)

import rpy2.robjects as robjects
from rpy2.robjects import r
import rpy2.robjects.numpy2ri
robjects.numpy2ri.activate()
robjects.r("library(baseline)")
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

t = np.linspace(0, 100, 101)
rate = 10 * stats.norm(50, 1).pdf(t) + np.random.rand(101)
Exemplo n.º 6
0
from statsmodels.tsa.stattools import adfuller
from rpy2.robjects.packages import importr
import rpy2.robjects as robjects

from rpy2.rinterface_lib.callbacks import logger as rpy2_logger
import logging

rpy2_logger.setLevel(logging.ERROR)  # will display errors, but not warnings

import bayesian_changepoint_detection.online_changepoint_detection as oncd
from functools import partial
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
import copy
import pandas as pd

from arch.unitroot import PhillipsPerron, KPSS

# # for use with cox stuart test in R
# webr = importr('webr')
# cox_stuart_test = robjects.r('cox.stuart.test')

# for use with findfrequency in R
forecast = importr("forecast")
findfrequency = robjects.r('findfrequency')


def concept_drift(vals, plot=False, verbose=False):
    # https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.pcolor.html#matplotlib.pyplot.pcolor
    # https://stackoverflow.com/questions/42687454/pcolor-data-plot-in-python