def SCCA_r(X,Y, n_components, pen):


	df_X = pd.DataFrame(X)
	df_Y = pd.DataFrame(Y)

	rmat_X = com.convert_to_r_matrix(df_X)
	rmat_Y = com.convert_to_r_matrix(df_Y)

	ri.globalenv['X'] = rmat_X
	ri.globalenv['Y'] = rmat_Y

	com.r(
	    """
	    out <- CCA(x = X, z = Y, K = %i, niter = 100, standardize = FALSE,
	               penaltyx = %f, penaltyz = %f)
	    """ % (n_components, pen[0], pen[1]))

	# convert the results back to dataframes and then to numpy arrays
	df_u = com.convert_robj(com.r('out[1]'))['u']
	df_v = com.convert_robj(com.r('out[2]'))['v']
	cors = com.convert_robj(com.r('out[16]'))['cors']

	x_loadings = df_u.as_matrix()
	y_loadings = df_v.as_matrix()
	cors = np.array(cors)
	
	loadings = (x_loadings, y_loadings)

	return loadings, cors
Пример #2
0
def bench_xts(n=100000, pct_overlap=0.20):
    from pandas.rpy.common import r
    r('a <- 5')

    xrng = '1:%d' % n

    start = n * pct_overlap + 1
    end = n + start - 1
    yrng = '%d:%d' % (start, end)

    r('library(xts)')

    iterations = 500

    kinds = ['left', 'outer', 'inner']
    result = {}
    for kind in kinds:
        r('x <- xts(rnorm(%d), as.POSIXct(Sys.Date()) + %s)' % (n, xrng))
        r('y <- xts(rnorm(%d), as.POSIXct(Sys.Date()) + %s)' % (n, yrng))
        stmt = 'for (i in 1:%d) merge(x, y, join="%s")' % (iterations, kind)
        elapsed = r('as.list(system.time(%s, gcFirst=F))$elapsed' % stmt)[0]
        result[kind] = (elapsed / iterations) * 1000
    return Series(result)
Пример #3
0
def bench_xts(n=100000, pct_overlap=0.20):
    from pandas.rpy.common import r
    r('a <- 5')

    xrng = '1:%d' % n

    start = n * pct_overlap + 1
    end = n + start - 1
    yrng = '%d:%d' % (start, end)

    r('library(xts)')

    iterations = 500

    kinds = ['left', 'outer', 'inner']
    result = {}
    for kind in kinds:
        r('x <- xts(rnorm(%d), as.POSIXct(Sys.Date()) + %s)' % (n, xrng))
        r('y <- xts(rnorm(%d), as.POSIXct(Sys.Date()) + %s)' % (n, yrng))
        stmt = 'for (i in 1:%d) merge(x, y, join="%s")' % (iterations, kind)
        elapsed = r('as.list(system.time(%s, gcFirst=F))$elapsed' % stmt)[0]
        result[kind] = (elapsed / iterations) * 1000
    return Series(result)
Пример #4
0
iters = [1000, 1000, 100, 1]

fancy_2d = []
take_2d = []
cython_2d = []

n = 1000


def _timeit(stmt, size, k=5, iters=1000):
    timer = timeit.Timer(stmt=stmt, setup=setup % (sz, k))
    return timer.timeit(n) / n

for sz, its in zip(sizes, iters):
    print(sz)
    fancy_2d.append(_timeit('arr[indexer]', sz, iters=its))
    take_2d.append(_timeit('arr.take(indexer, axis=0)', sz, iters=its))
    cython_2d.append(_timeit('lib.take_axis0(arr, indexer)', sz, iters=its))

df = DataFrame({'fancy': fancy_2d,
                'take': take_2d,
                'cython': cython_2d})

print(df)

from pandas.rpy.common import r
r('mat <- matrix(rnorm(50000), nrow=10000, ncol=5)')
r('set.seed(12345')
r('indexer <- sample(1:10000)')
r('mat[indexer,]')
Пример #5
0
sizes = [100, 1000, 10000, 100000]
iters = [1000, 1000, 100, 1]

fancy_2d = []
take_2d = []
cython_2d = []

n = 1000


def _timeit(stmt, size, k=5, iters=1000):
    timer = timeit.Timer(stmt=stmt, setup=setup % (sz, k))
    return timer.timeit(n) / n


for sz, its in zip(sizes, iters):
    print(sz)
    fancy_2d.append(_timeit('arr[indexer]', sz, iters=its))
    take_2d.append(_timeit('arr.take(indexer, axis=0)', sz, iters=its))
    cython_2d.append(_timeit('lib.take_axis0(arr, indexer)', sz, iters=its))

df = DataFrame({'fancy': fancy_2d, 'take': take_2d, 'cython': cython_2d})

print(df)

from pandas.rpy.common import r
r('mat <- matrix(rnorm(50000), nrow=10000, ncol=5)')
r('set.seed(12345')
r('indexer <- sample(1:10000)')
r('mat[indexer,]')
penalty = (pen_brain,pen_behave)
data = (X, Y)

'''
define CCA
'''
import readline
import rpy2.rinterface as ri
import pandas as pd
import pandas.rpy.common as com
# load the Stanford package for SCCA, it should load dependent packages as well
# please change the path.
com.r(
'''
library(plyr)
library(impute)
library(Rcpp)
library(PMA)
''')

def SCCA_r(X,Y, n_components, pen):


	df_X = pd.DataFrame(X)
	df_Y = pd.DataFrame(Y)

	rmat_X = com.convert_to_r_matrix(df_X)
	rmat_Y = com.convert_to_r_matrix(df_Y)

	ri.globalenv['X'] = rmat_X
	ri.globalenv['Y'] = rmat_Y
pen_behave = 0.5
n_areas = 14

WD = 'U:\\PhDProjects\\Project_CCA'
behavefn = 'select_data_MWQ_sessionMean.npy' 
beh_keysfn = 'select_keys_MWQ.npy'
rscorrfn = 'cs_cross_corr_Bzdok_DMN14.npy'
exp_var_fn = 'BzdokDMN14_MWQ_exp_var_penBrain%1.1f_penBehav%1.1f.pdf' %(pen_brain, pen_behave)

import pandas.rpy.common as com
# load the Stanford package for SCCA, it should load dependent packages as well
# please change the path.
com.r(
    '''
    library(plyr, lib.loc='U:/My Documents/R/win-library/3.2')
    library(impute, lib.loc='U:/My Documents/R/win-library/3.2')
    library(Rcpp, lib.loc='U:/My Documents/R/win-library/3.2')
    library(PMA, lib.loc='U:/My Documents/R/win-library/3.2')
    ''')


###################################################################################################
from os.path import expanduser

from joblib import load
import numpy as np
import matplotlib.pyplot as plt
import os
import pandas as pd
#set cd here
pen_behave = 0.5
n_areas = 14

WD = 'U:\\PhDProjects\\Project_CCA'
behavefn = 'select_data_MWQ_sessionMean.npy'
beh_keysfn = 'select_keys_MWQ.npy'
rscorrfn = 'cs_cross_corr_Bzdok_DMN14.npy'
exp_var_fn = 'BzdokDMN14_MWQ_exp_var_penBrain%1.1f_penBehav%1.1f.pdf' % (
    pen_brain, pen_behave)

import pandas.rpy.common as com
# load the Stanford package for SCCA, it should load dependent packages as well
# please change the path.
com.r('''
    library(plyr, lib.loc='U:/My Documents/R/win-library/3.2')
    library(impute, lib.loc='U:/My Documents/R/win-library/3.2')
    library(Rcpp, lib.loc='U:/My Documents/R/win-library/3.2')
    library(PMA, lib.loc='U:/My Documents/R/win-library/3.2')
    ''')

###################################################################################################
from os.path import expanduser

from joblib import load
import numpy as np
import matplotlib.pyplot as plt
import os
import pandas as pd
#set cd here

os.chdir(WD)