.g()
.us()
setScienceIfNot("Acc_vs_Anal_2015", load=FALSE)

lib(ggplot, quiet=TRUE)
lib(h2o)


  # loadIfNotExists("DT.OA_and_GL")
  # loadIfNotExists("DT.pre_transform")
  # loadIfNotExists("DT.transform_wide")

  envir = globalenv()

  DT.pre_transform  <- copy(get("DT.iTunes.acc_v_anal.pre_transform",  envir=envir))
  DT.transform_wide <- copy(get("DT.iTunes.acc_v_anal.transform_wide", envir=envir))

  localh2o = h2o.init(nthreads=30, min_mem_size="18g", max_mem_size="40g")       

  ## Folder for plot outputs
  folder.plots <- plots.p(today.string())
  dir.create(folder.plots, recursive=TRUE, showWarnings=FALSE)

  ## Folder for model outputs
  folder.h2o_models <- out.p(timeStamp("h2o_models"))
  dir.create(folder.h2o_models, recursive=TRUE, showWarnings=FALSE)


  epochs = 50
  hidden = c(200, 400, 400, 200)
  idr = 0.1
  hdr = c(0.5, 0.4, 0.6, 0.5)
  l1 = 1e-5
  l2 = 1e-5
  j <- i <- 1
  nfolds <- 10


hdr_types <- c("increasing", "same", "decreasing", "random")
hidden.options  <- list(c(200, 400, 400, 200), c(500, 500, 500), c(200, 50, 200), c(500, 500, 500, 500), c(800, 800, 800, 800, 800))

epoch.optons    <- c(30, 200, 500, 1000, 10000) # 50
regu.options    <- c(1e-10, 1e-7, 1e-5, 1e-2, 1e-1, 0.2, 0.5, 0.8, 1, 0)
nfolds_options  <- c(10, 20)

### START HERE 
{
# model.transform_wide <- emptylist(model_name)
model.transform_wide <- list()
start_time <- now()
iter <- 0
total_iterations <- length(hidden.options) *  length(hdr_types)
cat("Iterations to do : ", total_iterations, "\n")

Oct14 <- as.Date("2014-10-01")
# DT.transform_wide[date == Oct14]
yCols <- "Rasrp_0"




## CREATE   h2o.transform_wide -----------------------------------------
cat("\t\t            --={   L O A D I N G    I N T O    D A T A    H 2 O   }=--\n")
h2o.transform_wide <- as.h2o(localh2o, DT.transform_wide, key="transform_wide" )

{
for (nfolds in c(nfolds_options)) {
for (epochs in c(epoch.optons)) {
for (hdr_type in hdr_types) {
for (hidden in hidden.options) {

used.l2 <- c()
for (l2 in sample(setdiff(regu.options, used.l2))) {
  used.l2 <- c(used.l2, l2)

used.l1 <- c()
for (l1 in sample(setdiff(regu.options, used.l1))) {
  used.l1 <- c(used.l1, l1)

# {
#   total_iterations <- 4; iter <- 0
#   hidden <- c(800, 800, 800, 800, 800)
#   hdr <- .5
#   for (epochs in c(30, 100, 250)){

  cat(sprintf("Iterations %i of %i \n", iter <- iter + 1, total_iterations))

  ## setup hdr based on type
  hdr_from <- ifelse(hdr_type == "same", 0.5, 0.38)
  hdr_to   <- ifelse(hdr_type == "same", 0.5, 0.62)
  hdr <- round(seq(from=hdr_from, to=hdr_to, length.out=length(hidden)), 2)
  if (hdr_type == "decreasing")
    hdr <- rev(hdr)
  else if (hdr_type == "random")
    hdr <- removeNA(hdr[c(3, 10, 5, 1, 9, 4, 2, 6, 8, 7, 11, sample(setdiff(seq_along(hidden), 1:11)) )])

  j.i.l.l.    <- sprintf("j_%02i.i_%02i.l1_%s.l2_%s.idr_%s.hdr_%s.epochs_%s.hidden_%s", j, i, as.character(l1), as.character(l2), idr, pasteC(hdr, C="x"), epochs, pasteC(hidden, C="x"))
  model_name  <- sprintf("%smodel__%s", "", j.i.l.l.)


  # DT.transform_wide

  TargetVar   <- yCols
  Explanitory <- setdiff(names(DT.transform_wide), yCols)


  cat("\t\t             --={   C R E A T I N G    M O D E L  '", model_name, "''   }=--\n", sep="")

  model.transform_wide[[model_name]] <- list(model=NULL, prediction=NULL, args=list())

  model.transform_wide[[model_name]][["model"]] <- {
    h2o.deeplearning(
        x = Explanitory
      , y = TargetVar[[1]]
      , classification = FALSE
      , data = h2o.transform_wide
      , nfolds = nfolds
      # , validation =  # must remain empty for CV
      , activation = "RectifierWithDropout"
      , use_all_factor_levels = TRUE
      , hidden = hidden
      , input_dropout_ratio = idr
      , hidden_dropout_ratios = if (length(hdr) == length(hidden)) hdr else rep(hdr[[1]], length(hidden))
      , l1 = l1
      , l2 = l2
      , epochs = epochs)
  }


  ## SAVE TO DISK
  try(h2o.saveModel(object=model.transform_wide[[model_name]][["model"]], dir=folder.h2o_models, save_cv=TRUE, force=TRUE))


  model.transform_wide[[model_name]][["prediction"]] <- {
    unlist(as.data.frame(h2o.predict(model.transform_wide[[model_name]][["model"]], newdata=h2o.transform_wide)), use.names=FALSE)
  }

  argsToCollect <- c("epochs", "hidden", "idr", "hdr", "l1", "l2", "nfolds")
  selfname_(argsToCollect)
  gapply(argsToCollect)

} # // hidden
} # // hdr_type
}
}
}
} # // end outter for loop

end_time <- now()
message(sprintf("Took %s to run", fwTDiff(start_time, end_time)))
jesusForData(model.transform_wide)
} # // end START HERE


## The target value, ie the as reported value
## to untransform, we multiply by the Racct value
nms.rows <- DT.pre_transform[, sprintf("%3s:%s", storeid, date)]
AS_REPORTED <- cbind(DT.pre_transform$Rasrp_0)
REV_Acc     <- cbind(DT.pre_transform$Racct)
dimnames(AS_REPORTED) <- list(nms.rows, "Rasrp_0")
dimnames(REV_Acc)     <- list(nms.rows, "Rasrp_0")

## The predicted value
PREDS <- sapply(model.transform_wide, "[[", "prediction")
c4(cbind(AS_REPORTED, PREDS * c(REV_Acc)), -20, 4)
## 

## Depict which is most accurate 
order(colSums(((PREDS * DT.pre_transform$Racct) - DT.pre_transform$Rasrp_0) ^ 2, na.rm=TRUE))
order(abs(colSums(((PREDS * DT.pre_transform$Racct) - DT.pre_transform$Rasrp_0), na.rm=TRUE)) / sumn(DT.pre_transform$Rasrp_0))

print(sort(setNames(nm=seq(model.transform_wide), unname(fwp(abs(colSums(((PREDS * DT.pre_transform$Racct) - DT.pre_transform$Rasrp_0), na.rm=TRUE)) / sumn(DT.pre_transform$Rasrp_0))))))
print(sort(setNames(nm=seq(model.transform_wide), unname((colSums(((PREDS * DT.pre_transform$Racct) - DT.pre_transform$Rasrp_0) ^ 2, na.rm=TRUE ) / sumn(DT.pre_transform$Rasrp_0))))))
order()


unname(PREDS)
p <- PREDS[, 1]

DT.pre_transform[, list(date, storeid, Rasrp_0, prediction=(p*Racct))]

~~~~~~~~~~~~~
~~~~~~~~~~~~~

REMINDER TO RICK:   
(1) run the itunes new.r  where we create the DTs needed to create the models
(2) Run the code above to create the models  (Except, maybe take out the prediction part?)
(3) Re run part 1, but with dont_crop_maxdate flagged to TRUE
(4) Run the predictions  (filtering out the bad predictors)
(5) Test and plot them 
Run the models the first time with the stuff above
Then 

## DROP ANY PREDICTIONS THAT ARE ALL THE SAME VALUE
models_to_drop <- which(apply(PREDS, 2, lunique) == 1)
PREDS <- PREDS[, !models_to_drop]
dim(PREDS)

## PREDICTIONS
DT.predictions <- DT.pre_transform[, cbind(.SD, (PREDS*Racct)), .SDcols=c(key(DT.pre_transform), "Rasrp_0", "Racct")]

## Confirm that the columns were applied correctly, and not say, by row or some other error
for (nm in colnames(PREDS))
  stopifnot(all(DT.predictions[[nm]] == PREDS[, nm] * DT.pre_transform$Racct, na.rm=TRUE))


## DROP ANY PREDICTIONS THAT ARE ALL THE SAME VALUE
DT.predictions[, ]

~~~~~~~~~~~~~
CandidateModels <- c(7, 4, 12, 30, 24, 29)
names(model.transform_wide[[1]])

h2o.full_predict <- as.h2o(localh2o, DT.transform_wide, key="transform_wide" )

PREDS.full.list <- emptylist(model.transform_wide[CandidateModels])
for (m in CandidateModels) {
  PREDS.full.list[[names(model.transform_wide)[[m]]]] <-
    unlist(as.data.frame(h2o.predict(model.transform_wide[[m]][["model"]], newdata=h2o.transform_wide)), use.names=FALSE)
}

## Drop any models that simply are predicting a single number across the board
PREDS.full.list <- PREDS.full.list[sapply(PREDS.full.list, lunique) != 1]

## Multiply the predicted percentage by the Racct to get the forecasted value
PREDS.full.values <- lapply(PREDS.full.list, "*", DT.pre_transform$Racct)

jesusForData(PREDS.full.values)
jesusForData(DT.pre_transform)


lib(ggplot2)
PLOTS.iTunes_predictions <- emptylist(PREDS.full.values)
for (nm in names(PREDS.full.values)) {
  DF <- cbind(Actual=DT.pre_transform$Racct, Predicted=PREDS.full.values[[nm]])
  DF <- as.data.frame(DF)
  PLOTS.iTunes_predictions[[nm]] <- ggplot(data=DF, aes(x=Actual, y=Predicted)) + geom_point() + ggtitle(nm) + gg_xyline(size=.25)
}

sapply(PLOTS.iTunes_predictions, is)

printToPDF(PLOTS.iTunes_predictions)

## ATTEMPT TO SAVE
for (model_name in names(model.transform_wide)) {
  if (!is.null(model.transform_wide[[model_name]][["model"]]))
    h2o.saveModel(object=model.transform_wide[[model_name]][["model"]], dir=folder.h2o_models, save_cv=TRUE, force=TRUE)
}




