library(Rcpp)

#remove.packages(fastJT)
#install.packages("./fastJT_1.3.tar.gz", source = TRUR, repos = NULL)

library(microbenchmark)
library(fastJT)
repTime =100


x = 2.5
##  cpu time vs number of SNPs
n = 1000
K = 50
j = c(1000, 100000, 1000000)
resSNP = rep(0,4)

for(irep in 1:10){
  cat(irep)
  i = 1
  for (J in j)
  {	
    Y=matrix(rbinom(n*J,2,0.5),n,J)
    X=matrix(rnorm(n*K),n,K)
    resSNP[i] = resSNP[i] + system.time(fastJT(X, Y, outTopN=5, numThreads=8))[3]
    i = i + 1
  }
}
resSNP = resSNP/10

## cpu time vs number of markers
n = 1000
k = c(100, 500, 1000, 2000)
J = 1000
resMarker = rep(0,4)

for(irep in 1:repTime){
  cat(irep)
  i = 1
  for (K in k)
  {
    Y=matrix(rbinom(n*J,2,0.5),n,J)
    X=matrix(rnorm(n*K),n,K)
    resMarker[i] <- resMarker[i]+ system.time(fastJT(X, Y, outTopN=5))[3]
    i = i + 1
  }
}
resMarker = resMarker/repTime

## cpu time vs number of patient
N = c(100, 500, 2500, 5000)
K = 1000
J = 1000
resPat = rep(0,4)

for(irep in 1:repTime){
  cat(irep)
  i = 1
  for (n in N)
  {
    Y=matrix(rbinom(n*J,2,0.5),n,J)
    X=matrix(rnorm(n*K),n,K)
    resPat[i] <- resPat[i] + system.time(fastJT(X, Y, outTopN=5))[3]
    i = i + 1
  }
}
resPat = resPat/repTime


## clock eclipes time vs number of cores
n = 1000
K = 1000
J = 1000
resCores = rep(0,16)

for(irep in 1:repTime){
  cat(irep)
  i = 1
  for (cores in 1:16)
  {
    Y=matrix(rbinom(n*J,2,0.5),n,J)
    X=matrix(rnorm(n*K),n,K)
    resCores[i] <- resCores[i] + system.time(fastJT(X, Y, outTopN=5, numThreads = cores))[3]
    i = i + 1
  }
}
resCores = resCores/repTime


### benchmark for crossvalidation
library(fastJT)
set.seed(1234)

##  cpu time vs number of SNPs
n = 1000 ## number of patient
K = 2
J = 1000000

Y=matrix(rbinom(n*J,2,0.5),n,J)
X=matrix(rnorm(n*K),n,K)

## make sure there is sample names are list as the colume name of the marker data 
rownames(X) <- seq(1:n)
colnames(Y) <- paste0("fea:", seq(1:J))
p <-  0.05
mu <- 20
n2 <- n*p
y2 <- rnorm(n2,mu)

outlierIdx <- sample.int(n, size = n2, replace = FALSE)
 X[outlierIdx,1] <- y2


#devtools::load_all("../fastJT_1.0.1")

cross <- NULL
res <- fastJT(X,Y,outTopN=15L, numThreads=32)
library(microbenchmark)
t1 <- mean(microbenchmark(resCross <- fastJT.select(X, Y, kFold = 5, outTopN=15L, numThreads=2),times=100L)$time)
t2 <- mean(microbenchmark(resCross <- fastJT.select(X, Y, kFold = 10, outTopN=15L, numThreads=2),times=100L)$time)
t3 <- mean(microbenchmark(resCross <- fastJT.select(X, Y, kFold = 15, outTopN=15L, numThreads=2),times=100L)$time)


save(t1, t2, t3, resSNP, resMarker, resPat, resCores, file = "../Results/JL-bench.RData")
















