Пример #1
0
def insallRpackages(options):
    """Install R packages that cannot be installed using pip install ..."""
    #from rpy2.robjects.packages import importr
    src = join(sys.prefix, "lib", "R-3.2.3", "lib64", "R", "lib")
    spe = "$"
    cmd = 'export LD_LIBRARY_PATH=%sLD_LIBRARY_PATH:%s' %(spe,src)
    sh(cmd)
   # import rpy2.robjects as robjects
    import rpy2.robjects.packages as rpackages
    #from rpy2.robjects.vectors import StrVector
    packageNames = ('ggplot2')

    if all(rpackages.isinstalled(x) for x in packageNames):

        have_packages = True

    else:

        have_packages = False

    if not have_packages:

         #utils = rpackages.importr('utils')
         #utils.chooseCRANmirror(ind=1, useHTTPS=False)
         packnames_to_install = [x for x in packageNames if not rpackages.isinstalled(x)]
        # if len(packnames_to_install) > 0:
         #    utils.install_packages(StrVector(packnames_to_install))
         if len(packnames_to_install) > 0:
             # install biocondcutor package
            base = rpackages.importr('base')
            base.source("http://www.bioconductor.org/biocLite.R")
            biocinstaller = rpackages.importr("BiocInstaller")
            biocinstaller.biocLite("ggplot2", suppressUpdates=True)
Пример #2
0
def insallRpackages(options):
    """Install R packages that cannot be installed using pip install ..."""
    #from rpy2.robjects.packages import importr
    src = join(sys.prefix, "lib", "R-3.2.3", "lib64", "R", "lib")
    spe = "$"
    cmd = 'export LD_LIBRARY_PATH=%sLD_LIBRARY_PATH:%s' % (spe, src)
    sh(cmd)
    # import rpy2.robjects as robjects
    import rpy2.robjects.packages as rpackages
    #from rpy2.robjects.vectors import StrVector
    packageNames = ('ggplot2')

    if all(rpackages.isinstalled(x) for x in packageNames):

        have_packages = True

    else:

        have_packages = False

    if not have_packages:

        #utils = rpackages.importr('utils')
        #utils.chooseCRANmirror(ind=1, useHTTPS=False)
        packnames_to_install = [
            x for x in packageNames if not rpackages.isinstalled(x)
        ]
        # if len(packnames_to_install) > 0:
        #    utils.install_packages(StrVector(packnames_to_install))
        if len(packnames_to_install) > 0:
            # install biocondcutor package
            base = rpackages.importr('base')
            base.source("http://www.bioconductor.org/biocLite.R")
            biocinstaller = rpackages.importr("BiocInstaller")
            biocinstaller.biocLite("ggplot2", suppressUpdates=True)
Пример #3
0
def cran():
    print("install R packages")
    from rpy2.robjects.packages import importr, isinstalled
    if not os.path.exists(R_REQUIREMENTS):
        print("  {} not found".format(R_REQUIREMENTS))
        return False
    rlib = os.path.abspath(os.path.join(os.path.dirname(sys.executable), RLIB))
    if not os.path.isdir(rlib):
        os.makedirs(rlib)
        print("  {} created".format(rlib))
    with open(R_REQUIREMENTS, 'r') as file:
        data = file.read()
    packages_required = data.split(sep='\n')
    utils = importr('utils')
    utils.chooseCRANmirror(ind=0)
    for package in packages_required:
        if package:
            if not isinstalled(package, lib_loc=rlib):
                print("  install", package)
                utils.install_packages(package,
                                       lib=rlib,
                                       verbose=False,
                                       quiet=True)
            else:
                print("  requirement", package, "already satisfied")
Пример #4
0
    def install_bart(self):
        import rpy2.robjects.packages as rpackages
        from rpy2.robjects.packages import importr
        from rpy2.robjects.vectors import StrVector
        import rpy2.robjects as robjects

        robjects.r.options(download_file_method='curl')

        # install.packages("rJava")
        rj = importr("rJava", robject_translations={'.env': 'rj_env'})
        rj._jinit(parameters="-Xmx16g", force_init=True)
        print(
            "rJava heap size is",
            np.array(rj._jcall(rj._jnew("java/lang/Runtime"), "J",
                               "maxMemory"))[0] / 1e9,
            "GB.",
            file=sys.stderr)

        package_names = ["bartMachine"]
        utils = rpackages.importr('utils')
        utils.chooseCRANmirror(ind=0)
        utils.chooseCRANmirror(ind=0)

        names_to_install = [
            x for x in package_names if not rpackages.isinstalled(x)
        ]
        if len(names_to_install) > 0:
            utils.install_packages(StrVector(names_to_install))

        return importr("bartMachine")
def importFromDashUtils(funcName):
    # Check to ensure that the direct parent of study_management/ is dashboard/
    path = Path(os.getcwd())
    parent = path.parent.parts[-1]
    if parent != "dashboard":
        # raise exception if the direct parent is not dashboard/
        raise Exception(
            "Error: the direct parent of 'study_management/' should be 'dashboard/'."
        )
    # import R's utility package
    utils = rpackages.importr('utils')
    # select a mirror for R packages
    utils.chooseCRANmirror(ind=1)  # select the first mirror in the list
    # R package names, copied from dashboard/dashboard_utils.R
    packnames = ("RSQLite", "plyr", "dplyr", "ggplot2", "zoo", "sqldf",
                 "rjson", "reticulate")
    # Selectively install what needs to be install.
    names_to_install = [x for x in packnames if not rpackages.isinstalled(x)]
    if len(names_to_install) > 0:
        utils.install_packages(StrVector(names_to_install))
    # Defining the R script and loading the instance in Python
    r = robjects.r
    r['source']('../dashboard_utils.R')
    # Loading the function we have defined in R
    r_func = robjects.globalenv[funcName]
    # return the selected function
    return r_func
Пример #6
0
def create_clustering_file(outdir, outfile):
    base = rpackages.importr('base')
    packageNames = ('tidyverse')
    utils = rpackages.importr('utils')
    utils.chooseCRANmirror(ind=1)
    packnames_to_install = [
        x for x in packageNames if not rpackages.isinstalled(x)
    ]
    if len(packnames_to_install) > 0:
        utils.install_packages(StrVector(packnames_to_install))
    tidyverse = rpackages.importr('tidyverse')
    robjects.r['options'](warn=-1)
    create_file = robjects.r('''
		function(results_file,outdir) {
			all_domains_blast_df <- read_tsv(results_file, col_names = F)
			names(all_domains_blast_df) <- c("sseqid", "slen","sstart", "send", "qseqid", "qlen", "qstart", "qend", "qcovs", "pident"," evalue", "Sample", "cohort")
			all_domains_blast_df_count <- all_domains_blast_df %>% group_by(Sample, qseqid) %>% count() %>% ungroup()
			all_domains_blast_df_count_table <- all_domains_blast_df_count %>% spread(., Sample, n, fill =0 )
			abundFile = file.path(outdir, "unique-biosynthetic-reads-abundance-table.txt")
			abundWideFile = file.path(outdir, "unique-biosynthetic-reads-abundance-table-wide.txt")
			write_tsv(all_domains_blast_df_count_table, abundFile, col_names = T)
			write_tsv(all_domains_blast_df_count, abundWideFile, col_names = T)
		}
		''')

    create_file(outfile, outdir)
Пример #7
0
def create_reformat_data(input_df, outdir):
    rpackages.importr('base')
    utils = rpackages.importr('utils')
    packageNames = ('tidyverse')
    packnames_to_install = [
        x for x in packageNames if not rpackages.isinstalled(x)
    ]
    if len(packnames_to_install) > 0:
        utils.install_packages(StrVector(packnames_to_install))
    rpackages.importr('tidyverse')

    robjects.r['options'](warn=-1)
    reformat_df = robjects.r('''
		function(hmmdf,outDir) {
			hmmdfRecoded <- separate(hmmdf, readID, into = c("read","F_R_read_frame"), sep = "_", extra = "merge") %>%
			select(-c(F_R_read_frame))
			hmmdfRecodedDFUnique<-aggregate(HMMScore ~ read + Sample + sampleType + protType , hmmdfRecoded, max)
			colnames(hmmdfRecodedDFUnique)<-c("readID","Sample", "sampleType", "protType","HMMScore")
			write_tsv(hmmdfRecodedDFUnique, file.path(outDir, "spHMM-filtered-results.txt"), col_names = T)
			return(hmmdfRecodedDFUnique)
		}
		''')
    # convert pandas df to R datafame
    with localconverter(ro.default_converter + pandas2ri.converter):
        input_r_df = ro.conversion.py2rpy(input_df)

    data_filter = reformat_df(input_r_df, outdir)
    return (data_filter)
def get_results_from_r(
        processed_data: pd.DataFrame,
        experiment_config: ExperimentConfig) -> ExperimentOutput:
    """
    Get results from the CausalImpact implementation using R
    :type experiment_config.training_period: List[int]
    :type experiment_config.evaluation_period: List[int]
    """

    processed_data.set_index(experiment_config.time_var, inplace=True)
    pre_period = robjects.FloatVector(experiment_config.training_period)
    post_period = robjects.FloatVector(experiment_config.evaluation_period)

    # Load R libraries
    utils = rpackages.importr("utils")
    utils.chooseCRANmirror(ind=1)
    lib_names = ("CausalImpact", "bsts")
    libs_to_install = [x for x in lib_names if not rpackages.isinstalled(x)]

    if len(libs_to_install) > 0:
        utils.install_packages(StrVector(libs_to_install))

    robjects.numpy2ri.activate()
    pandas2ri.activate()

    rdf = robjects.conversion.py2rpy(processed_data)
    causalimpact = importr("CausalImpact")
    ci = causalimpact.CausalImpact(rdf, pre_period, post_period)

    summary = robjects.r("function(x) x$summary")

    results = summary(ci).T
    results.rename(
        index={
            "Actual": "actual",
            "Pred": "predicted",
            "Pred.lower": "predicted_lower",
            "Pred.upper": "predicted_upper",
            "Pred.sd": "predicted_sd",
            "AbsEffect": "abs_effect",
            "AbsEffect.lower": "abs_effect_lower",
            "AbsEffect.upper": "abs_effect_upper",
            "AbsEffect.sd": "abs_effect_sd",
            "RelEffect": "rel_effect",
            "RelEffect.lower": "rel_effect_lower",
            "RelEffect.upper": "rel_effect_upper",
            "RelEffect.sd": "rel_effect_sd",
        },
        inplace=True,
    )

    results.columns = ["average", "cumulative"]

    experiment_output = ExperimentOutput(
        **{
            "results_summary": results,
            "trained_model": ci,
            "experiment_name": experiment_config.experiment_name,
        })
    return experiment_output
Пример #9
0
def import_R_library(rpacknames):
    from rpy2.robjects.vectors import StrVector
    utils = rpackages.importr('utils')
    utils.chooseCRANmirror(ind=1)
    names_to_install = [x for x in rpacknames if not rpackages.isinstalled(x)]
    if len(names_to_install) > 0:
        utils.install_packages(StrVector(names_to_install))
Пример #10
0
def uninstall_grf():
    """ Ensures the grf packages is not installed before the test runs"""
    if rpackages.isinstalled("grf"):
        robjects.r.options(download_file_method="curl")
        utils = rpackages.importr("utils")
        utils.chooseCRANmirror(ind=0)

        utils.remove_packages(StrVector(["grf"]))
Пример #11
0
def fart_ass(gene_counts, metadata, output_dir, output_name):

    # imports
    utils = importr('utils')
    utils.chooseBioCmirror(ind=1)  # select the first mirror in the list

    if (not rpackages.isinstalled('kBET')):
        print('kBET not installed, installing kBET')
        utils.install_packages('../source_packages/kBET-master.zip',
                               type="source")
    else:
        print('kBET alraedy installed')
    if (not rpackages.isinstalled('devtools')):
        print('devtools not installed, installing devtools')
        utils.install_packages('devtools')
    else:
        print('devtools alraedy installed')

    #devtools = importr('devtools')
    #devtools.install_github('theislab/kBET')

    kBET = importr('kBET')

    batch = metadata['batch']

    #
    ###
    #
    #
    #r_gene_counts = ro.conversion.py2rpy(gene_counts)
    results = kBET(gene_counts, batch, plot=False)

    #utils.install_packages('gutenbergr', repos='https://cloud.r-project.org')

    print('we made it this far')
    #packages = ['devtools', 'kBET', 'bapred', 'ggplot2']
    #packages_to_install = [p for p in packages if not rpackages.isinstalled(p)]

    #for package in packages_to_install:
    #    utils.install_packages(package, repos='https://cloud.r-project.org'

    # load imports
    #devtools = importr('devtools')
    #kBet = importr('kBET'))

    rstring = """
Пример #12
0
def install_r_package(name):
    from rpy2.robjects.vectors import StrVector
    from rpy2.robjects.packages import importr, isinstalled

    if not isinstalled(name):
        utils = importr("utils")
        utils.chooseCRANmirror(ind=1)
        utils.install_packages(StrVector([name]))
Пример #13
0
def RimportLibrary(lib_name):
    """ Helper function to import R libraries
    using the rpy2 binder
    """
    if not rpackages.isinstalled(lib_name):
        base.source("http://www.bioconductor.org/biocLite.R")
        biocinstaller = rpackages.importr("BiocInstaller")
        biocinstaller.biocLite(lib_name)
    return rpackages.importr(lib_name)
Пример #14
0
 def import_packages(self):
     for im in self.imports:
         if not rpackages.isinstalled(im):
             utils = rpackages.importr('utils')
             utils.chooseCRANmirror(ind=1)
             ## FIXME: this returns rpy2.rinterface.NULL if package is not found
             utils.install_packages(im)
         rpackages.importr(im)
     self.ready = True
Пример #15
0
def activate_r_packages(packnames):
    if all(rpackages.isinstalled(x) for x in packnames):
        have_tutorial_packages = True
    else:
        have_tutorial_packages = False
        if not have_tutorial_packages:
            # import R's utility package
            utils = rpackages.importr('utils')
            # select a mirror for R packages
            utils.chooseCRANmirror(ind=1) # select the first mirror in the list
    #We are now ready to install packages using R's own function install.package:
    if not have_tutorial_packages:
        # R vector of strings
        from rpy2.robjects.vectors import StrVector
        # file
        packnames_to_install = [x for x in packnames if not rpackages.isinstalled(x)]
        if len(packnames_to_install) > 0:
            utils.install_packages(StrVector(packnames_to_install))
Пример #16
0
def RimportLibrary(lib_name):
    """ Helper function to import R libraries
    using the rpy2 binder
    """
    if not rpackages.isinstalled(lib_name):
        base.source("http://www.bioconductor.org/biocLite.R")
        biocinstaller = rpackages.importr("BiocInstaller")
        biocinstaller.biocLite(lib_name)
    return rpackages.importr(lib_name)
Пример #17
0
def r_importer(modules, install_only=[], log=False):
  """
  Import and install R packages. If the desired packages are not installed it will
  automatically install them. Note that this function will act as a one time
  delay in running time, if modules need to be installed. Import R packages
  manually as e.g. <stargazer =  rpy2.robjects.packages.importr('stargazer')>.
  So, the same name used for installing, should be used to import the functions.
  Important to note, this function imports the following modules from rpy2:
  "rpy2.robjects.packages" and "rpy2.robjects.vectors".
 
  Args:
      modules: list of the desired packages. The packages to be included should
      be as a string. E.g. modules = ['stargazer', 'tidyverse'].
 
      install_only: default=None. list or string of packages to be installed 
      only. Note, combinations are possible.
 
      log: default=False. Prints a log message if true, of the packages that are
      (succesfully) installed.
 
  Returns:
      None
  """
  if not isinstance(modules, list):
      modules = [modules]
  
  if not isinstance(install_only, list):
      install_only = [install_only]
      
  # import R's utility package:
  utils = rpackages.importr('utils')
 
  # R package names:
  packnames = tuple(modules)
 
  # Selectively install what needs to be install. Use CRAN cloud server:
  names_to_install = [x for x in packnames if not rpackages.isinstalled(x)]
  if len(names_to_install) > 0:
    print('Installing:', names_to_install)
    with walpy.suppress():
        utils.install_packages(StrVector(names_to_install),
                            repos='https://cloud.r-project.org/')
    print('Successfully installed:', names_to_install)
  
  # Make modules non-overlapping with install_only:
  modules = set(modules) - set(install_only)
 
  # Import modules to be automatically imported
  for module in modules:
    rpackages.importr(module)
  
  # Print log message if true:
  if log == True:
    print('Successfully imported:', [i for i in modules])
 
  return
Пример #18
0
def fit_glm(data, family='binomial', glmnet_kwargs=None):
    """
    :param data: the data dictionary
    :param family: response type
    :param glmnet_kwargs: dictionary of keyword arguments to pass the glmnet function in R.
    :return: pandas dataframe containing the fit model parameters. Each row corresponds to a unique value for lambda.
    """
    if not packages.isinstalled(name='glmnet'):
        utils = packages.importr('utils')
        utils.chooseCRANmirror(ind=1)
        utils.install_packages('glmnet')

    if not glmnet_kwargs:
        glmnet_kwargs = {}

    # intercept should be added as a constant 1 feature, not via glmnet. Thus, always set to 'False'.
    if 'intercept' in glmnet_kwargs.keys():
        assert glmnet_kwargs['intercept'] is False, \
            "Do not add intercept in glmnet. Please add an intercept feature to the dataset instead."
    glmnet_kwargs['intercept'] = False

    # set default parameters
    if 'alpha' not in glmnet_kwargs.keys():
        glmnet_kwargs['alpha'] = DEFAULT_ALPHA
    if 'nlambda' not in glmnet_kwargs.keys():
        glmnet_kwargs['nlambda'] = DEFAULT_NLAMBDA

    # R set-up
    numpy2ri.activate()
    glmnet = importr('glmnet')

    n_row, n_col = data['X'].shape

    # transfer to R objects
    x_ = r.matrix(data['X'], nrow=n_row)
    y_ = r.matrix(data['Y'], nrow=n_row)
    weights = FloatVector(data['sample_weights'])

    output = glmnet.glmnet(x=x_,
                           y=y_,
                           family=family,
                           weights=weights,
                           **glmnet_kwargs)
    coefs = r.t(sparse_to_full_matrix(output.rx2('beta')))
    coefs = np.array(coefs)  # drop added intercept column which is fixed at 0
    lambda_ = output.rx('lambda')
    lambda_ = np.array(r.matrix(lambda_))[0]

    coef_names = data['variable_names']
    df = pd.DataFrame(coefs, columns=coef_names)

    df['lambda_'] = lambda_
    df['alpha'] = glmnet_kwargs['alpha']

    return df
Пример #19
0
def grf():
    """ Ensures grf is installed before running tests with it

    This is required as usually the user is requested to install the package manually

    """
    if not rpackages.isinstalled("grf"):
        install_r_packages(["grf"])
        return 0

    return 1
Пример #20
0
def install_r_dependencies(r_package_names, r_bioconducter_package_names):
    from rpy2.robjects.packages import importr
    import rpy2.robjects.packages as rpackages

    r_packages_uninstalled = [
        x for x in r_package_names if not rpackages.isinstalled(x)
    ]
    r_bioconducter_packages_uninstalled = [
        x for x in r_bioconducter_package_names if not rpackages.isinstalled(x)
    ]
    if r_packages_uninstalled:
        utils = importr('utils')
        utils.chooseCRANmirror(ind=1)
        for p in r_packages_uninstalled:
            utils.install_packages(p)

    if r_bioconducter_packages_uninstalled:
        biocm = importr("BiocManager")
        for p in r_bioconducter_packages_uninstalled:
            biocm.install(p)
Пример #21
0
def install_package():
    #you must run this as root
    packnames = ('readxl', 'BiocManager', 'ggplot2', 'gridExtra')
    if all(rpackages.isinstalled(x) for x in packnames):
        have_packages = True

    else:
        have_packages = False

    if not have_packages:
        utils = rpackages.importr('utils')
        utils.chooseCRANmirror(ind=1)

        packnames_to_install = [
            x for x in packnames if not rpackages.isinstalled(x)
        ]

        if len(packnames_to_install) > 0:
            utils.install_packages(StrVector(packnames_to_install))

    r('BiocManager::install("ropls")')
def setupCluster():
    # import R's utility package
    utils = rpackages.importr('utils')
    # select a mirror for R packages
    utils.chooseCRANmirror(ind=1) # select the first mirror in the list

    for x in packnames:
        if not rpackages.isinstalled(x):
            utils.install_packages(x)
            print ( "installing ", x )

    print( "In case lgfortran is missing make sure these have same used version of gcc, g++ and gfortran" )
Пример #23
0
def install_r_packages(rlib):
    from rpy2.robjects.packages import importr, isinstalled
    r_requirements = "r.txt"
    if os.path.exists(r_requirements):
        with open(r_requirements, 'r') as file:
            data = file.read()
    else:
        data = ""
    packages_required = data.split(sep='\n')
    for default in R_DEFAULT:
        if default not in packages_required:
            packages_required.append(default)
    utils = importr('utils')
    utils.chooseCRANmirror(ind=1)
    for package in packages_required:
        if package:
            output('Checking package: {}', package)
            output('Installed?: {}', isinstalled(package, lib_loc=rlib))
            if not (isinstalled(package, lib_loc=rlib)):
                utils.install_packages(package, lib=rlib, verbose=False,
                                       quiet=True)
Пример #24
0
def install_r_packages():
    import rpy2.robjects.packages as rpackages
    from rpy2.robjects.vectors import StrVector
    utils = rpackages.importr('utils')
    utils.chooseCRANmirror(ind=1)

    packnames = ['zoo', 'caTools', 'gets']
    names_to_install = [
        pkg for pkg in packnames if not rpackages.isinstalled(pkg)
    ]
    if len(names_to_install) > 0:
        utils.install_packages(StrVector(names_to_install))
        print('Installed ' + str(names_to_install))
Пример #25
0
def install_packages():
    packnames = ('base', 'ggplot2', 'smoof', 'mlr', 'mlrMBO', 'DiceKriging',
                 'randomForest', 'ParamHelpers', 'stats', 'rgenoud', 'lhs',
                 'methods', 'emoa')

    if all(isinstalled(x) for x in packnames):
        have_tutorial_packages = True
    else:
        have_tutorial_packages = False

    if not have_tutorial_packages:
        # import R's utility package
        utils = importr('utils')
        # select a mirror for R packages
        utils.chooseCRANmirror(ind=1)  # select the first mirror in the list

        # R vector of strings
        from rpy2.robjects.vectors import StrVector
        # file
        packnames_to_install = [x for x in packnames if not isinstalled(x)]
        if len(packnames_to_install) > 0:
            utils.install_packages(StrVector(packnames_to_install))
Пример #26
0
def gen_match(Tr, X, estimand='ATT'):
    # Install the Matching and rgenoud R packages, if not already installed
    to_install = [
        x for x in [MATCHING, RGENOUD] if not rpackages.isinstalled(x)
    ]
    if len(to_install) > 0:
        utils = importr('utils')
        utils.chooseCRANmirror(ind=1)  # select the first mirror in the list
        utils.install_packages(StrVector(to_install))
    matching = importr(MATCHING)  # import Matching R package
    importr(RGENOUD)

    return matching.GenMatch(Tr=Tr, X=X, estimand=estimand)
Пример #27
0
def check_r_packages(packnames):
    """ Check R packages in list `packnames` are installed

    Return empty string if all packages are installed, otherwise, informative
    error message.
    """
    missing_packnames = []
    for packname in packnames:
        if not rpackages.isinstalled(packname):
            missing_packnames.append(packname)
    if len(missing_packnames) == 0:
        return ''
    raise RuntimeError("Please install R package(s) " +
                       ', '.join(missing_packnames))
Пример #28
0
def rpy2_init() -> None:
    import rpy2.robjects.packages as rpackages
    from rpy2.robjects.vectors import StrVector
    global bnlearn, gRain, utils
    # Initialize RPY2
    utils = rpackages.importr('utils')
    utils.chooseCRANmirror(ind=1)
    # Install packages
    packages = ('bnlearn', 'gRain')
    packages_to_install = [x for x in packages if not rpackages.isinstalled(x)]
    if len(packages_to_install) > 0:
        utils.install_packages(StrVector(packages_to_install))
    # Import packages
    bnlearn = rpackages.importr('bnlearn')
    gRain = rpackages.importr('gRain')
Пример #29
0
def get_package(package):
    """
        Exposes all R objects in package as Python objects after
        checking that it is installed.

        Parameters:
            package (str): the name of an R package

        Returns:
            Exposed R package
    """
    if isinstalled(package):
        return importr(package)
    else:
        raise ProcessError(f"R package, {package}, is not installed")
	def __init__(self, packnames):
		"""Initialise the object through checking the required packages and install them if needed.

		Input ->
		packnames :    tuple of required r packages
		"""
		# import rpy2's package module
		import rpy2.robjects.packages as rpackages
		if all(rpackages.isinstalled(x) for x in packnames):
			have_tutorial_packages = True
		else:
			have_tutorial_packages = False
		if not have_tutorial_packages:
			# import R's utility package
			utils = rpackages.importr('utils')
			# select a mirror for R packages
			utils.chooseCRANmirror(ind=1) # select the first mirror in the list
		if not have_tutorial_packages:
			# R vector of strings
			from rpy2.robjects.vectors import StrVector
			# file
			packnames_to_install = [x for x in packnames if not rpackages.isinstalled(x)]
			if len(packnames_to_install) > 0:
				utils.install_packages(StrVector(packnames_to_install))
Пример #31
0
    def install_package(self, pname):
        print('Check package {}...'.format(pname))
        # import R's utility package
        utils = rpackages.importr('utils')

        # select a mirror for R packages
        utils.chooseCRANmirror(ind=1)  # select the first mirror in the list

        # Selectively install what needs to be install.
        # We are fancy, just because we can.
        if not rpackages.isinstalled(pname):
            print('Package {} not installed, install it...'.format(pname))
            utils.install_packages(pname)
            print('Package {} installed.'.format(pname))
        else:
            print('Package {} already installed.'.format(pname))
Пример #32
0
def check_r_packages():
    """Ensures required R packages are installed."""
    # import R's utility package
    utils = rpackages.importr('utils')
    # select a mirror for R packages
    utils.chooseCRANmirror(ind=113)  # Indiana
    # R package names
    packnames = ('classInt', 'minerva', 'randomForest', 'MASS', 'ggplot2',
                 'gplots')

    # R vector of strings
    packnames_to_install = [
        x for x in packnames if not rpackages.isinstalled(x)
    ]
    if len(packnames_to_install) > 0:
        utils.install_packages(StrVector(packnames_to_install))
Пример #33
0
def install():
    # Install BART
    robjects.r.options(download_file_method="curl")
    numpy2ri.activate()
    rj = importr("rJava", robject_translations={".env": "rj_env"})
    rj._jinit(parameters="-Xmx16g", force_init=True)
    package_names = ["dbarts"]
    utils = rpackages.importr("utils")
    utils.chooseCRANmirror(ind=0)
    utils.chooseCRANmirror(ind=0)
    names_to_install = [
        x for x in package_names if not rpackages.isinstalled(x)
    ]
    if len(names_to_install) > 0:
        utils.install_packages(StrVector(names_to_install),
                               repos="http://cran.us.r-project.org")
Пример #34
0
    def __init__(self, parent):   
        super(QWidget, self).__init__(parent)
        utils = rpackages.importr('utils')
        utils.chooseCRANmirror(ind=1)
        packnames=('plotrix', 'psd', 'fractal', 'squash', 'doParallel')
        for x in packnames:
            if (not rpackages.isinstalled(x)):
                utils.install_packages(x)
            
        #self.layout es una ventana contenedora
        self.layout = QVBoxLayout(self)
 
        # Initialize tab screen
        #self.tabs es un elemento que soporta la contención de pestañas
        self.tabs = QTabWidget()
        
        #Pestañas
        self.tabVis = QWidget()
        self.tabEst = QWidget()
        self.tabEsp = QWidget()


        # Content Tabs 
        self.tabVisLayout = Visualizacion()
        self.tabEstLayout = Estacionariedad()
        self.tabEspLayout = Espectro()


        # Add tabs
        self.tabs.addTab(self.tabVis,"Visualization")
        self.tabs.addTab(self.tabEst,"Estacionariedad")
        self.tabs.addTab(self.tabEsp,"Espectro")
 
        # Set asing layout
        self.tabVis.layout = self.tabVisLayout
        self.tabEst.layout = self.tabEstLayout
        self.tabEsp.layout = self.tabEspLayout
        
        # SetLayout Tabs 
        self.tabVis.setLayout(self.tabVis.layout)
        self.tabEst.setLayout(self.tabEst.layout)
        self.tabEsp.setLayout(self.tabEsp.layout)
 
        # Add tabs to Main
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)
Пример #35
0
    def install_matchit(self):
        from rpy2.robjects.packages import importr
        import rpy2.robjects.packages as rpackages
        from rpy2.robjects.vectors import StrVector
        import rpy2.robjects as robjects

        package_names = ["MatchIt"]

        names_to_install = [x for x in package_names if not rpackages.isinstalled(x)]
        if len(names_to_install) > 0:
            robjects.r.options(download_file_method='curl')
            utils = rpackages.importr('utils')
            utils.chooseCRANmirror(ind=0)
            utils.chooseCRANmirror(ind=0)
            utils.install_packages(StrVector(names_to_install))

        return importr("MatchIt")
Пример #36
0
def RCheckPackage(Packages):
    RPacksUsed = Packages
    RPacksToInstall = [Pack for Pack in RPacksUsed 
                       if not RPackages.isinstalled(Pack)]
    if len(RPacksToInstall) > 0:
        print(str(RPacksToInstall), 'not installed. Install now?')
        Ans = input('[y/N]: ')
        
        if Ans in ['y', 'yes', 'Yes', 'YES']:
            from rpy2.robjects.vectors import StrVector as RStrVector
            
            RUtils = RPackages.importr('utils')
            RUtils.chooseCRANmirror(ind=1)
            
            RUtils.install_packages(RStrVector(RPacksToInstall))
        
        else: print('Aborted.')
    
    else: print('Packages', str(Packages), 'installed.')
    
    return(None)
Пример #37
0
    def set_R_plotting_device(self, device):
        """
        Set which device R should use to produce plots.
        If device == 'svg' then the package 'Cairo' 
        must be installed. Because Cairo forces "onefile=TRUE",
        it is not posible to include multiple plots per cell.

        Parameters
        ----------

        device : ['png', 'X11', 'svg']
            Device to be used for plotting. 
            Currently only "png" and "X11" are supported,
            with 'png' and 'svg' being most useful in the notebook,
            and 'X11' allowing interactive plots in the terminal.

        """
        device = device.strip()
        if device not in ['png', 'X11', 'svg']:
            raise ValueError("device must be one of ['png', 'X11' 'svg'], got '%s'", device)
        if device == 'svg':
            try:
                self.cairo = rpacks.importr('Cairo')
            except ri.RRuntimeError as rre:
                if rpacks.isinstalled('Cairo'):
                    msg = "An error occurred when trying to load the R package Cairo'\n%s" % str(rre)
                else:
                    msg = """
The R package 'Cairo' is required but it does not appear to be installed/available. Try:

import rpy2.robjects.packages as rpacks
if not rpacks.isinstalled('Cairo'):
    utils = rpacks.importr('utils')
    utils.chooseCRANmirror(ind=1)
    from rpy2.robjects.vectors import StrVector
    utils.install_packages(StrVector(['Cairo']))

"""
                raise RInterpreterError(msg)
        self.device = device
Пример #38
0
import os
# import rpy2's package module
import rpy2.robjects.packages as rpackages
# R vector of strings
from rpy2.robjects.vectors import StrVector
# import R's utility package
utils = rpackages.importr('utils')
base = rpackages.importr('base')

# select a mirror for R packages
utils.chooseCRANmirror(ind=1) # select the first mirror in the listls')

# R package names
packnames = ('WeightedCluster', 'TraMineR', 'seqdist2')

# Selectively install what needs to be install.
names_to_install = [x for x in packnames if not rpackages.isinstalled(x)]
if len(names_to_install) > 0:
    utils.install_packages(StrVector(names_to_install))

directory = os.path.dirname(__file__)
r_cluster_function_path = os.path.join(directory, 'clustering_sequences.R')
cluster_function = base.dget(r_cluster_function_path)

df_list = '/home/oscar/Documents/tropical_earm/earm_dataframes_consumption/data_frame37.csv'
sm_df = '/home/oscar/Documents/tropical_earm/subs_matrix_consumption/sm_37.csv'

cluster_function(df_list, sm_df, clusterMethod='PAM', clustered_pars_path=directory)
Пример #39
0
def RunWDModel(recent):
    pandas2ri.activate()
    # R package names
    packnames = ('forecast')
    if all(rpackages.isinstalled(x) for x in packnames):
        have_tutorial_packages = True
    else:
        have_tutorial_packages = False
    if not have_tutorial_packages:
        # import R's utility package
        utils = rpackages.importr('utils')
        # select a mirror for R packages
        utils.chooseCRANmirror(ind=1)  # select the first mirror in the list
    if not have_tutorial_packages:
        # R vector of strings
        from rpy2.robjects.vectors import StrVector
        # file
        packnames_to_install = [x for x in packnames
                                if not rpackages.isinstalled(x)]
        if len(packnames_to_install) > 0:
            utils.install_packages(StrVector(packnames_to_install))
    # Import R packages
    forecast = importr('forecast')
    base = importr('base')
    # Model subset of data for particular station
    jump = 3  # skip weekend
    output = pd.DataFrame(columns=['count_diff', 'DateTime', 'Type', 'ID'])
    absent = []
    errors = []
    for x in station_range:
        SepModel = Model(recent, x)
        if SepModel.valid is False:
            absent.append(x)
            continue
        SepModel.PreProcess(separate=True)
        SepModel.WD = SepModel.WD[:480]
        if(len(SepModel.WD) > 0):
            if(x == 1):  # Only needs to be run once, same for all stations
                WD_dates = SepModel.WD.index
                y = np.asarray(WD_dates[-1].year,
                               dtype='datetime64[Y]') - 1970
                doy = np.asarray((WD_dates[-1].dayofyear + jump),
                                 dtype='timedelta64[D]') - 1
                new = pd.to_datetime(y + doy)
                new_dates = pd.DatetimeIndex(start=new,
                                             freq='30Min', periods=48 * 4)
            SepModel.WD.reset_index(inplace=True, drop=True)
            gc.collect()
            robjects.r('o = c(2,0,3)')
            robjects.r('sorder = c(1,1,2)')
            robjects.r('s = list(order=sorder, period=48)')
            DF = pandas2ri.py2ri(SepModel.WD)
            robjects.r.assign('df', DF)
            try:
                robjects.r('fit = Arima(df,order=o, seasonal=s, method="CSS")')
            except:
                errors.append(x)
                continue
            f_cast = robjects.r('f_cast = forecast(fit, h=4*48)')
            arima_mean = np.array(f_cast.rx('mean'))
            robjects.r('rm(list = ls(all = TRUE))')
            robjects.r('gc()')
            results = pd.DataFrame({'count_diff':
                                    arima_mean.flatten()}).round()
            results.count_diff = results.count_diff.astype(int)
            results['DateTime'] = new_dates
            results['Type'] = 'Forecast'
            results['ID'] = x
            SepModel.WD['DateTime'] = WD_dates
            SepModel.WD['Type'] = 'Historic'
            SepModel.WD['ID'] = x
            out = SepModel.WD.append(results)
            output = output.append(out)
            del f_cast
            del DF
            del SepModel
            gc.collect()
    output.ID = output.ID.astype(int)
    output.count_diff = output.count_diff.astype(int)
    output.reset_index(inplace=True, drop=True)
    path = wd + '\Model'
    if not os.path.exists(path):
        os.mkdir(path)
    output.to_csv(path + '\\' 'ModelOutput.csv')
    return output, absent, errors
Пример #40
0
    def Run(self):
        self.transit_message("Starting ZINB analysis")
        start_time = time.time()
        packnames = ("MASS", "pscl")
        r_packages_needed = [x for x in packnames if not rpackages.isinstalled(x)]
        if (len(r_packages_needed) > 0):
            self.transit_error(
                    "Error: Following R packages are required: %(0)s. From R console, You can install them using install.packages(c(%(0)s))"
                    % ({'0': '"{0}"'.format('", "'.join(r_packages_needed))}))
            sys.exit(1)


        self.transit_message("Getting Data")
        (sites, data, filenamesInCombWig) = tnseq_tools.read_combined_wig(self.combined_wig)

        self.transit_message("Normalizing using: %s" % self.normalization)
        (data, factors) = norm_tools.normalize_data(data, self.normalization)

        condition_name = self.condition
        conditionsByFile, covariatesByFileList, interactionsByFileList, orderingMetadata = tnseq_tools.read_samples_metadata(self.metadata, self.covars, self.interactions, condition_name=condition_name)

        ## [Condition] in the order of files in combined wig
        conditions = self.wigs_to_conditions(
            conditionsByFile,
            filenamesInCombWig)
        ## [Covariate] in the order of files in combined wig
        covariates = self.wigs_to_covariates(
            covariatesByFileList,
            filenamesInCombWig)
        ## [Interaction] in the order of files in combined wig
        interactions = self.wigs_to_interactions(
            interactionsByFileList,
            filenamesInCombWig)
        data, conditions, covariates, interactions = self.filter_wigs_by_conditions(
                data,
                conditions,
                covariates = covariates,
                interactions = interactions,
                ignored_conditions = self.ignored_conditions,
                included_conditions = self.included_conditions)

        genes = tnseq_tools.read_genes(self.annotation_path)

        TASiteindexMap = {TA: i for i, TA in enumerate(sites)}
        RvSiteindexesMap = tnseq_tools.rv_siteindexes_map(genes, TASiteindexMap, nterm=self.NTerminus, cterm=self.CTerminus)
        statsByRv, statGroupNames = self.stats_by_rv(data, RvSiteindexesMap, genes, conditions, interactions)
        LogZPercByRep, NZMeanByRep = self.global_stats_for_rep(data)

        self.transit_message("Running ZINB")
        pvals, qvals, run_status = self.run_zinb(data, genes, NZMeanByRep, LogZPercByRep, RvSiteindexesMap, conditions, covariates, interactions)

        def orderStats(x, y):
            ic1 = x.split("_")
            ic2 = y.split("_")
            c1, i1 = (ic1[0], ic1[1]) if len(ic1) > 1 else (ic1[0], None)
            c2, i2 = (ic2[0], ic2[1]) if len(ic2) > 1 else (ic2[0], None)

            if len(self.included_conditions) > 0:
                condDiff = (self.included_conditions.index(c1) - self.included_conditions.index(c2))
                ## Order by interaction, if stat belongs to same condition
                if condDiff == 0 and i1 is not None and i2 is not None:
                    return (orderingMetadata['interaction'].index(i1) - orderingMetadata['interaction'].index(i2))
                return condDiff

            ## Order by samples metadata, if include flag not provided.
            condDiff = (orderingMetadata['condition'].index(c1) - orderingMetadata['condition'].index(c2))
            if condDiff == 0 and i1 is not None and i2 is not None:
                return (orderingMetadata['interaction'].index(i1) - orderingMetadata['interaction'].index(i2))
            return condDiff

        orderedStatGroupNames = sorted(statGroupNames, orderStats)

        self.transit_message("Adding File: %s" % (self.output))
        file = open(self.output,"w")
        head = ("Rv Gene TAs".split() +
                map(lambda v: "Mean_" + v, orderedStatGroupNames) +
                map(lambda v: "NZmean_" + v, orderedStatGroupNames) +
                map(lambda v: "NZperc_" + v, orderedStatGroupNames) +
                "pval padj".split() + ["status"])

        file.write("#Console: python %s\n" % " ".join(sys.argv))
        file.write('\t'.join(head)+EOL)
        for gene in genes:
            Rv = gene["rv"]
            vals = ([Rv, gene["gene"], str(len(RvSiteindexesMap[Rv]))] +
                    ["%0.2f" % statsByRv[Rv]['mean'][group] for group in orderedStatGroupNames] +
                    ["%0.2f" % statsByRv[Rv]['nz_mean'][group] for group in orderedStatGroupNames] +
                    ["%0.2f" % statsByRv[Rv]['nz_perc'][group] for group in orderedStatGroupNames] +
                    ["%f" % x for x in [pvals[Rv], qvals[Rv]]]) + [run_status[Rv]]
            file.write('\t'.join(vals)+EOL)
        file.close()
        self.transit_message("Finished Zinb analysis")
        self.transit_message("Time: %0.1fs\n" % (time.time() - start_time))
Пример #41
0
def check_r_packages(packagenames: list):
	results = dict()
	for package in packagenames:
		results[package] = rpackages.isinstalled(package)
	return results