<file_sep>
require(ggplot2)
require(gridExtra)
require(RColorBrewer)
require(pracma)
require(scales)



get.mfARI <- function(Ks, Delta.tau, Phi)
{
  #
  # From regression on bounded mfARI parameters made
  # on mar 06 2015
  #
  # Call:
  # lm(formula = ATARI ~ Ks + Delta.tau + Phi, data = params)
  # 
  # Residuals:
  #      Min       1Q   Median       3Q      Max 
  # -0.24540 -0.08135  0.01454  0.07764  0.19562 
  # 
  # Coefficients:
  #             Estimate Std. Error t value Pr(>|t|)    
  # (Intercept)  1.48740    0.31454   4.729 8.65e-06 ***
  # Ks           3.85688    0.11339  34.014  < 2e-16 ***
  # Delta.tau   -0.12420    0.02910  -4.269 4.99e-05 ***
  # Phi          0.10999    0.00608  18.091  < 2e-16 ***
  # ---
  # Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  # 
  # Residual standard error: 0.1161 on 87 degrees of freedom
  # Multiple R-squared:  0.9981,	Adjusted R-squared:  0.9981 
  # F-statistic: 1.55e+04 on 3 and 87 DF,  p-value: < 2.2e-16
  
  invisible(1.48740 + 3.85688 * Ks - 0.12420 * Delta.tau + 0.10999 * Phi)
}


get.mfARI.no.Phi <- function(Ks, Delta.tau)
{
  # From regression on bounded mfARI parameters (discarding Phi)
  # made on mar 10 2015
  #
  # Call:
  # lm(formula = ATARI ~ Ks + Delta.tau, data = params)
  # 
  # Residuals:
  #      Min       1Q   Median       3Q      Max 
  # -0.56369 -0.15209 -0.03217  0.16018  0.60636 
  # 
  # Coefficients:
  #             Estimate Std. Error t value Pr(>|t|)    
  # (Intercept)  6.75148    0.25915   26.05   <2e-16 ***
  # Ks           2.51501    0.18608   13.52   <2e-16 ***
  # Delta.tau   -0.61895    0.02155  -28.72   <2e-16 ***
  # ---
  # Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  # 
  # Residual standard error: 0.2519 on 88 degrees of freedom
  # Multiple R-squared:  0.9911,	Adjusted R-squared:  0.9909 
  # F-statistic:  4904 on 2 and 88 DF,  p-value: < 2.2e-16
  
  invisible(6.75148 + 2.51501 * Ks - 0.61895 * Delta.tau)
}


#
# Returns a plot with the CBFV parameters for a specific Delta-Tau.
#
get.plot.CBFV.parameters.search <- function(
      time.instants,
      normalised.CBFV.signal,
      min.CBFV.sample,
      min.CBFV.time.instant,
      search.results,
      cbfv.palette = brewer.pal(n = 9, name = "Blues"),
      id = NULL,
      ...
      )
{
  ymin <- min(normalised.CBFV.signal)
  ymax <- max(normalised.CBFV.signal)
  dy <- (ymax - ymin) / 100
  dx <- (tail(time.instants, 1) - time.instants[1]) / 100
  
  # Plots CBFV
  d <- data.frame(Time = time.instants, CBFV = normalised.CBFV.signal)
  p <- ggplot(data = d, mapping = aes(x = Time, y = CBFV))
  p <- p + geom_line(colour = cbfv.palette[4])
  p <- p + geom_point(colour = cbfv.palette[4])
  
  # If a transient and a steady CBFV were found
  if(is.finite(search.results[["CBFV.response.MSE"]]))
  {
    # Plots transient CBFV line
    dtl <- data.frame(
      Time = search.results[["transient.CBFV.time.instants"]],
      CBFV = search.results[["transient.CBFV.line"]]
    )
    p <- p + geom_line(data = dtl, colour = cbfv.palette[7])
    
    # Plots initial and final points of the transient CBFV
    dti <- data.frame(
      Time = search.results[["transient.CBFV.time.instants"]][1],
      CBFV = search.results[["transient.normalised.CBFV.signal"]][1]
    )
    p <- p + geom_point(data = dti, colour = cbfv.palette[7])
    dtf <- data.frame(
      Time = tail(search.results[["transient.CBFV.time.instants"]], 1),
      CBFV = tail(search.results[["transient.normalised.CBFV.signal"]], 1)
    )
    p <- p + geom_point(data = dtf, colour = cbfv.palette[7])
    
    # Adds time annotations
    d <- dti
    d[["CBFV"]] <- d[["CBFV"]] - 2 * dy
    d <- cbind(d, Text = sprintf("%.1f", min.CBFV.time.instant))
    p <- p + geom_text(
      data = d,
      mapping = aes(label = Text),
      colour = cbfv.palette[7],
      size = 2,
      hjust = 0.5,
      vjust = 1
    )
    d <- dtf
    u <- ifelse(dti[["CBFV"]] < dtf[["CBFV"]], -2 * dy, 3 * dy)
    d[["CBFV"]] <- dti[["CBFV"]] + u
    d <- cbind(d, Text = sprintf("%.1f", search.results[["tau"]]))
    p <- p + geom_text(
      data = d,
      mapping = aes(label = Text),
      colour = cbfv.palette[7],
      size = 2,
      hjust = 0.5,
      vjust = 1
    )
    d <- dtf
    d[["Time"]] <- d[["Time"]] - dx
    u <- ifelse(dti[["CBFV"]] < dtf[["CBFV"]], 2 * dy, -3 * dy)
    d[["CBFV"]] <- d[["CBFV"]] + u
    d <- cbind(d, Text = "Tau")
    p <- p + geom_text(
      data = d,
      mapping = aes(label = Text),
      colour = cbfv.palette[7],
      size = 2,
      hjust = 0.5,
      vjust = 0
    )
    
    # Adds lines to annotations
    d <- rbind(dtf, dtf)
    d[["CBFV"]][2] <- dti[["CBFV"]]
    p <- p + geom_line(
      data = d,
      colour = cbfv.palette[7],
      linetype = "dashed"
    )
    
    # Plots steady CBFV
    dsl <- data.frame(
      Time = search.results[["steady.CBFV.time.instants"]],
      CBFV = search.results[["steady.CBFV.line"]]
    )
    p <- p + geom_line(data = dsl, colour = cbfv.palette[7])
    
    # Adds time annotations
    d <- tail(dsl, 1)
    u <- ifelse(dti[["CBFV"]] < dtf[["CBFV"]], -2 * dy, 3 * dy)
    d[["CBFV"]] <- dti[["CBFV"]] + u
    s <- sprintf("%.1f", d[["Time"]])
    d <- cbind(d, Text = s)
    p <- p + geom_text(
      data = d,
      mapping = aes(label = Text),
      colour = cbfv.palette[7],
      size = 2,
      hjust = 0.5,
      vjust = 1
    )
    
    # Adds lines to annotations
    u <- ifelse(dti[["CBFV"]] < dtf[["CBFV"]], 2 * dy, -3 * dy)
    d[["CBFV"]] <- d[["CBFV"]] + u
    d <- rbind(d[, c(1, 2)], tail(dsl, 1))
    p <- p + geom_line(
      data = d,
      colour = cbfv.palette[7],
      linetype = "dashed"
    )
    
    # Adds summary table
    s <- sprintf("%.1fs", search.results[["Delta.tau"]])
    t <- data.frame(l = "Delta Tau", d = s)
    s <- sprintf("%.3f", search.results[["Ks"]])
    t <- rbind(t, data.frame(l = "Ks", d = s))
    s <- sprintf("%.2f°",
      atan(search.results[["transient.CBFV.slope"]]) * 180 / pi
    )
    t <- rbind(t, data.frame(l = "Angle", d = s))
    s <- sprintf("%.4f", search.results[["transient.CBFV.MSE"]])
    t <- rbind(t, data.frame(l = "Trans. MSE", d = s))
    s <- sprintf("%.4f", search.results[["steady.CBFV.MSE"]])
    t <- rbind(t, data.frame(l = "Steady MSE", d = s))
    s <- sprintf("%.4f", search.results[["CBFV.response.MSE"]])
    t <- rbind(t, data.frame(l = "Total MSE", d = s))
    
    gt <- tableGrob(
      d = t,
      show.rownames = FALSE,
      show.colnames = FALSE,
      gpar.coretext = gpar(col = cbfv.palette[7], cex = 0.5),
      padding.v = unit(2, "mm")
    )
    p <- p + annotation_custom(
      grob = gt,
      xmin = time.instants[1],
      xmax = time.instants[1] + 9,
      ymin = ymin,
      ymax = ymin + 25 * dy
    )
  }
  
  if(!is.null(id))
    p <- p + ggtitle(id)
  p <- p + xlab("Time") + ylab("Normalised CBFV")
  
  p
}


#
# Returns a plot with the CBFV parameters for a specific Delta-Tau.
#
get.plots.mfARI.parameters <- function(
      time.instants,
      ABP.signal,
      CBFV.signal,
      parameter.results,
      index.estimation.function = NULL,
      abp.palette = brewer.pal(n = 9, name = "Reds"),
      cbfv.palette = brewer.pal(n = 9, name = "Blues"),
      ann.palette = brewer.pal(n = 9, name = "Greens")[seq(3, 9, 2)],
      time.tol = min(diff(time.instants)) / 100,
      id = NULL,
      ...
      )
{
  #
  # Plot 1 (original signals)
  #
  
  abp.ymin <- min(ABP.signal, na.rm = TRUE)
  abp.ymax <- max(ABP.signal, na.rm = TRUE)
  abp.ry <- abp.ymax - abp.ymin
  abp.dy <- abp.ry / 100
  
  cbfv.ymin <- min(CBFV.signal, na.rm = TRUE)
  cbfv.ymax <- max(CBFV.signal, na.rm = TRUE)
  cbfv.ry <- cbfv.ymax - cbfv.ymin
  cbfv.dy <- cbfv.ry / 100
  
  rx <- tail(time.instants, 1) - time.instants[1]
  dx <- rx / 100
  
  # Plots ABP
  dabp <- data.frame(
    Time = time.instants,
    Signal = ABP.signal,
    Segment = "ABP Signal"
  )
  p1 <- ggplot(
    data = dabp,
    mapping = aes(x = Time, y = Signal, colour = Segment)
  )
  p1 <- p1 + geom_line(linetype = "dashed")
  
  # Plots CBFV
  dcbfv <- data.frame(
    Time = time.instants,
    Signal = CBFV.signal,
    Segment = "CBFV Signal"
  )
  p2 <- ggplot(
    data = dabp,
    mapping = aes(x = Time, y = Signal, colour = Segment)
  )
  p2 <- p2 + geom_line(data = dcbfv, linetype = "dashed")
  
  # Plots ABP baseline
  d <- dabp[parameter.results[["ABP.baseline.samples"]], ]
  d[["Segment"]] <- "Baseline"
  p1 <- p1 + geom_point(data = d)
  d[["Signal"]] <- parameter.results[["ABP.baseline.value"]]
  p1 <- p1 + geom_line(data = d)
  
  # Plots CBFV baseline
  d <- dcbfv[parameter.results[["CBFV.baseline.samples"]], ]
  d[["Segment"]] <- "Baseline"
  p2 <- p2 + geom_point(data = d)
  d[["Signal"]] <- parameter.results[["CBFV.baseline.value"]]
  p2 <- p2 + geom_line(data = d)
    
  # Plots min ABP window
  d <- dabp[parameter.results[["min.ABP.samples"]], ]
  d[["Segment"]] <- "Min. Search Window"
  p1 <- p1 + geom_point(data = d)
  d[["Signal"]] <- parameter.results[["min.ABP.value"]]
  p1 <- p1 + geom_line(data = d, linetype = "dashed")
  
  # Plots min CBFV window
  d <- dcbfv[parameter.results[["min.CBFV.samples"]], ]
  d[["Segment"]] <- "Min. Search Window"
  p2 <- p2 + geom_point(data = d)
  
  # Adds min ABP time annotation
  d <- dabp[parameter.results[["min.ABP.sample"]], ]
  d[["Signal"]] <- d[["Signal"]] - 2 * abp.dy
  d[["Segment"]] <- "Min. Search Window"
  d <- cbind(d, Text = sprintf("%.1f", d[["Time"]]))
  p1 <- p1 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 3,
    hjust = 0.5,
    vjust = 1
  )
  
  # Adds min CBFV time annotation
  d <- dcbfv[parameter.results[["min.CBFV.sample"]], ]
  d[["Signal"]] <- d[["Signal"]] - 2 * cbfv.dy
  d[["Segment"]] <- "Min. Search Window"
  d <- cbind(d, Text = sprintf("%.1f", d[["Time"]]))
  p2 <- p2 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 3,
    hjust = 0.5,
    vjust = 1
  )
  
  # Adds min ABP drop annotation
  d <- dabp[parameter.results[["min.ABP.samples"]][1], ]
  d[["Segment"]] <- "Min. Search Window"
  d[["Signal"]] <- parameter.results[["min.ABP.value"]] - 4 * cbfv.dy
  d <- cbind(
    d,
    Text = sprintf("%.1f%% drop", parameter.results[["min.ABP.drop.pc"]])
  )
  p1 <- p1 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 2,
    hjust = 0.5,
    vjust = 1
  )
  
  p1 <- p1 + xlab("Time") + ylab("ABP")
  p2 <- p2 + xlab("Time") + ylab("CBFV")
  cmatch1 <- c(
    "ABP Signal" = abp.palette[4],
    "CBFV Signal" = cbfv.palette[4],
    "Baseline" = ann.palette[2],
    "Min. Search Window" = ann.palette[3]
  )
  p1 <- p1 + scale_colour_manual(values = cmatch1, breaks = names(cmatch1))
  p2 <- p2 + scale_colour_manual(values = cmatch1, breaks = names(cmatch1))
  p1 <- p1 + theme(
    legend.justification = c(1, 1),
    legend.background = element_rect(fill = alpha('grey90', 0.0)),
    legend.position = c(1, 1),
    legend.key.size =  unit(0.3, "cm"),
    legend.title = element_text(size = 7),
    legend.text = element_text(size = 6)
  )
  p2 <- p2 + theme(
    legend.justification = c(1, 1),
    legend.background = element_rect(fill = alpha('grey90', 0.0)),
    legend.position = c(1, 1),
    legend.key.size =  unit(0.4, "cm"),
    legend.title = element_text(size = 7),
    legend.text = element_text(size = 6)
  )
  
  #
  # Plot 3 (normalised signals)
  #
  
  ABP.time <- time.instants -
              time.instants[parameter.results[["min.ABP.sample"]]]
  ABP.signal <- parameter.results[["normalised.ABP.signal"]]
  
  CBFV.time <- time.instants -
               time.instants[parameter.results[["min.CBFV.sample"]]]
  CBFV.signal <- parameter.results[["normalised.CBFV.signal"]]
  
  xmin <- min(c(ABP.time, CBFV.time))
  xmax <- max(c(ABP.time, CBFV.time))
  rx <- xmax - xmin
  dx <- rx / 100
  ymin <- min(c(ABP.signal, CBFV.signal))
  ymax <- max(c(ABP.signal, CBFV.signal))
  ry <- ymax - ymin
  dy <- ry / 100
  
  # Plots CBFV
  dcbfv <- data.frame(
    Time = CBFV.time,
    Signal = CBFV.signal,
    Segment = "Normalised CBFV"
  )
  p3 <- ggplot(
    data = dcbfv,
    mapping = aes(x = Time, y = Signal, colour = Segment)
  )
  d <- dcbfv[1:(parameter.results[["transient.CBFV.samples"]][1]), ]
  p3 <- p3 + geom_line(data = d, linetype = "dashed")
  r <- tail(parameter.results[["transient.CBFV.samples"]], 1)
  d <- dcbfv[r:nrow(dcbfv), ]
  p3 <- p3 + geom_line(data = d, linetype = "dashed")
  
  # Plots ABP
  dabp <- data.frame(
    Time = ABP.time,
    Signal = ABP.signal,
    Segment = "Normalised ABP"
  )
  d <- dabp[1:(parameter.results[["transient.ABP.samples"]][1]), ]
  p3 <- p3 + geom_line(data = d, linetype = "dashed")
  r <- tail(parameter.results[["transient.ABP.samples"]], 1)
  d <- dabp[r:nrow(dabp), ]
  p3 <- p3 + geom_line(data = d, linetype = "dashed")
  
  # Plots ABP transient segment
  d <- dabp[parameter.results[["transient.ABP.samples"]], ]
  p3 <- p3 + geom_line(data = d, linetype = "11")
  d[["Signal"]] <- parameter.results[["transient.ABP.line"]]
  d[["Segment"]] <- "Transient Line"
  p3 <- p3 + geom_line(data = d)
  
  # Plots CBFV transient segment
  d <- dcbfv[parameter.results[["transient.CBFV.samples"]], ]
  p3 <- p3 + geom_line(data = d, linetype = "11")
  d[["Signal"]] <- parameter.results[["transient.CBFV.line"]]
  d[["Segment"]] <- "Transient Line"
  p3 <- p3 + geom_line(data = d)
  
  # Plots CBFV steady segment
  d <- dcbfv[parameter.results[["steady.CBFV.samples"]], ]
  d[["Signal"]] <- parameter.results[["steady.CBFV.line"]]
  d[["Segment"]] <- "Steady Line"
  p3 <- p3 + geom_line(data = d, linetype = "dashed")
  
  # Plots and annotates Delta Tau
  s2 <- parameter.results[["transient.CBFV.samples"]]
  d2 <- dcbfv[s2, ]
  y <- min(d2[["Signal"]], na.rm = TRUE)
  tini2 <- CBFV.time[s2[1]]
  tfin2 <- CBFV.time[tail(s2, 1)]
  tini1 <- ABP.time > tini2 - time.tol
  tfin1 <- ABP.time < tfin2 + time.tol
  s1 <- which(tini1 & tfin1)
  d1 <- dabp[s1, ]
  y <- min(c(d1[["Signal"]], y), na.rm = TRUE)
  
  d2[["Signal"]] <- y - 2 * dy
  d2[["Segment"]] <- "Transient Line"
  p3 <- p3 + geom_line(data = d2)
  m <- round(nrow(d2) / 2)
  d <- d2[m, ]
  d[["Signal"]] <- d[["Signal"]] - dy
  s <- paste(
    "Delta*tau",
    "==",
    sprintf("%.1f", parameter.results[["Delta.tau"]])
  )
  d <- cbind(d, Text = s)
  p3 <- p3 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 3,
    hjust = 0.4,
    vjust = 1,
    parse = TRUE
  )
  
  # Annotates Ks
  d2 <- dcbfv[parameter.results[["steady.CBFV.samples"]], ]
  i <- d2[["Signal"]] < parameter.results[["Ks"]]
  d2[["Signal"]] <- parameter.results[["Ks"]]
  d2[["Segment"]] <- "Steady Line"
  p3 <- p3 + geom_line(data = d2)
  u <- 2 * dy
  vjust <- 0
  if(sum(i) - sum(!i) < 0)
  {
    u <- -u
    vjust <- 1 - vjust
  }
  m <- round(nrow(d2) / 2)
  d <- d2[m, ]
  d[["Signal"]] <- d[["Signal"]] + u
  s <- paste("K[s]", "==", sprintf("%.4f", parameter.results[["Ks"]]))
  d <- cbind(d, Text = s)
  p3 <- p3 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 3,
    hjust = 0.5,
    vjust = vjust,
    parse = TRUE
  )
  
  # Plots and annotates Phi
  if(!is.null(ABP.signal))
  {
    d1 <- dabp[parameter.results[["transient.ABP.samples"]], ]
    d1[["Signal"]] <- parameter.results[["transient.ABP.line"]]
    d2 <- dcbfv[parameter.results[["transient.CBFV.samples"]], ]
    d2[["Signal"]] <- parameter.results[["transient.CBFV.line"]]
    m <- round(min(nrow(d1), nrow(d2)) * 0.8)
    #d <- rbind(d1[m, ], d2[m, ])
    #d[["Segment"]] <- "Angle"
    #p3 <- p3 + geom_point(data = d)
    r <- d1[["Time"]][m]
    tt <- seq(-90, 90, length.out = 100)
    xx <- r * cos(tt * pi / 180)
    yy <- (sin(tt * pi / 180) + 1) / 2
    yy <- sin(tt * pi / 180)
    #d <- data.frame(Time = xx, Signal = yy, Segment = "Angle")
    #p3 <- p3 + geom_path(data = d, linetype = "dotted")
    .dist <- function(x, y) sqrt((x - xx)^2 + (y - yy)^2)
    .tmp.fun <- function(x, y) min(.dist(x, y))
    s1 <- mapply(.tmp.fun, d1[["Time"]], d1[["Signal"]])
    s1 <- which.min(s1)
    s2 <- mapply(.tmp.fun, d2[["Time"]], d2[["Signal"]])
    s2 <- which.min(s2)
    t1 <- ABP.time[parameter.results[["transient.ABP.samples"]][s1]]
    t2 <- CBFV.time[parameter.results[["transient.CBFV.samples"]][s2]]
    y1 <- parameter.results[["transient.ABP.line"]][s1]
    y2 <- parameter.results[["transient.CBFV.line"]][s2]
    d <- data.frame(Time = c(t1, t2), Signal = c(y1, y2), Segment = "BLA")
    #p3 <- p3 + geom_point(data = d)
    i <- yy < max(y1, y2)
    j <- yy > min(y1, y2)
    if(sum(i & j) > 0)
    {      
      d <- data.frame(
        Time = xx[i & j],
        Signal = yy[i & j],
        Segment = "Angle"
      )
      p3 <- p3 + geom_path(data = d, linetype = "11")
    }
    
    i <- yy < max(y1, y2) & yy < 1
    j <- yy > min(y1, y2) & yy > 0
    if(sum(i & j) > 0)
    {
      xx <- xx[i & j]
      yy <- yy[i & j]
      d <- data.frame(Time = xx, Signal = yy, Segment = "Angle")
      p3 <- p3 + geom_path(data = d)
    }
    m <- round(nrow(d) / 2)
    d <- d[m, ]
    d[["Time"]] <- d[["Time"]] + dx
    d[["Segment"]] <- "Angle"
    s <- paste(
      "varphi",
      "==",
      sprintf("%.2f*degree", parameter.results[["Phi"]])
    )
    d <- cbind(d, Text = s)
    p3 <- p3 + geom_text(
      data = d,
      mapping = aes(label = Text),
      size = 3,
      hjust = 0,
      vjust = 0.5,
      parse = TRUE
    )
  }
  
  p3 <- p3 + xlab("Time") + ylab("Normalised Signals")
  cmatch2 <- c(
    "Normalised ABP" = abp.palette[4],
    "Normalised CBFV" = cbfv.palette[4],
    "Transient Line" = ann.palette[2],
    "Steady Line" = ann.palette[3],
    "Angle" = ann.palette[4]
  )
  p3 <- p3 + scale_colour_manual(
    values = cmatch2,
    breaks = names(cmatch2)
  )
  p3 <- p3 + theme(
    legend.justification = c(0, 0),
    legend.background = element_rect(fill = alpha('grey90', 0.0)),
    legend.position = c(0, 0),
    legend.key.size =  unit(0.4, "cm"),
    legend.title = element_text(size = 7),
    legend.text = element_text(size = 6)
  )
  
  #
  # Joins plots
  #
  
  main <- NULL
  if(!is.null(parameter.results[["mfARI"]]))
    main <- sprintf("mfARI = %.1f", parameter.results[["mfARI"]])
  else
    if(!is.null(index.estimation.function))
    {
      mfari <- index.estimation.function(
        Ks = parameter.results[["Ks"]],
        Delta.tau = parameter.results[["Delta.tau"]],
        Phi = parameter.results[["Phi"]]
      )
      main <- sprintf("estimated mfARI = %.1f", mfari)
    }
  
  if(!is.null(id))
    if(is.null(main))
      main <- id
    else
      main <- paste(id, main, sep = ", ")
  
  p <- arrangeGrob(
    arrangeGrob(p1, p2, nrow = 2),
    p3,
    nrow = 2,
    main = main
  )
  
  p
}
get.plots.mfARI.parameters.fixed.coord <- function(
      time.instants,
      ABP.signal,
      CBFV.signal,
      parameter.results,
      time.until.min,
      time.after.min,
      min.normalised.intensity,
      max.normalised.intensity,
      index.estimation.function = NULL,
      abp.palette = brewer.pal(n = 9, name = "Reds"),
      cbfv.palette = brewer.pal(n = 9, name = "Blues"),
      ann.palette = brewer.pal(n = 9, name = "Greens")[seq(3, 9, 2)],
      time.tol = min(diff(time.instants)) / 100,
      id = NULL,
      ...
      )
{
  #
  # Plot 1 (original signals)
  #
  
  abp.ymin <- min(ABP.signal, na.rm = TRUE)
  abp.ymax <- max(ABP.signal, na.rm = TRUE)
  abp.ry <- abp.ymax - abp.ymin
  abp.dy <- abp.ry / 100
  
  cbfv.ymin <- min(CBFV.signal, na.rm = TRUE)
  cbfv.ymax <- max(CBFV.signal, na.rm = TRUE)
  cbfv.ry <- cbfv.ymax - cbfv.ymin
  cbfv.dy <- cbfv.ry / 100
  
  rx <- tail(time.instants, 1) - time.instants[1]
  dx <- rx / 100
  
  # Plots ABP
  dabp <- data.frame(
    Time = time.instants,
    Signal = ABP.signal,
    Segment = "ABP Signal"
  )
  p1 <- ggplot(
    data = dabp,
    mapping = aes(x = Time, y = Signal, colour = Segment)
  )
  p1 <- p1 + geom_line(linetype = "dashed")
  
  # Plots CBFV
  dcbfv <- data.frame(
    Time = time.instants,
    Signal = CBFV.signal,
    Segment = "CBFV Signal"
  )
  p2 <- ggplot(
    data = dabp,
    mapping = aes(x = Time, y = Signal, colour = Segment)
  )
  p2 <- p2 + geom_line(data = dcbfv, linetype = "dashed")
  
  # Plots ABP baseline
  d <- dabp[parameter.results[["ABP.baseline.samples"]], ]
  d[["Segment"]] <- "Baseline"
  p1 <- p1 + geom_point(data = d)
  d[["Signal"]] <- parameter.results[["ABP.baseline.value"]]
  p1 <- p1 + geom_line(data = d)
  
  # Plots CBFV baseline
  d <- dcbfv[parameter.results[["CBFV.baseline.samples"]], ]
  d[["Segment"]] <- "Baseline"
  p2 <- p2 + geom_point(data = d)
  d[["Signal"]] <- parameter.results[["CBFV.baseline.value"]]
  p2 <- p2 + geom_line(data = d)
    
  # Plots min ABP window
  d <- dabp[parameter.results[["min.ABP.samples"]], ]
  d[["Segment"]] <- "Min. Search Window"
  p1 <- p1 + geom_point(data = d)
  d[["Signal"]] <- parameter.results[["min.ABP.value"]]
  p1 <- p1 + geom_line(data = d, linetype = "dashed")
  
  # Plots min CBFV window
  d <- dcbfv[parameter.results[["min.CBFV.samples"]], ]
  d[["Segment"]] <- "Min. Search Window"
  p2 <- p2 + geom_point(data = d)
  
  # Adds min ABP time annotation
  d <- dabp[parameter.results[["min.ABP.sample"]], ]
  d[["Signal"]] <- d[["Signal"]] - 2 * abp.dy
  d[["Segment"]] <- "Min. Search Window"
  d <- cbind(d, Text = sprintf("%.1f", d[["Time"]]))
  p1 <- p1 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 3,
    hjust = 0.5,
    vjust = 1
  )
  
  # Adds min CBFV time annotation
  d <- dcbfv[parameter.results[["min.CBFV.sample"]], ]
  d[["Signal"]] <- d[["Signal"]] - 2 * cbfv.dy
  d[["Segment"]] <- "Min. Search Window"
  d <- cbind(d, Text = sprintf("%.1f", d[["Time"]]))
  p2 <- p2 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 3,
    hjust = 0.5,
    vjust = 1
  )
  
  # Adds min ABP drop annotation
  d <- dabp[parameter.results[["min.ABP.samples"]][1], ]
  d[["Segment"]] <- "Min. Search Window"
  d[["Signal"]] <- parameter.results[["min.ABP.value"]] - 4 * cbfv.dy
  d <- cbind(
    d,
    Text = sprintf("%.1f%% drop", parameter.results[["min.ABP.drop.pc"]])
  )
  p1 <- p1 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 2,
    hjust = 0.5,
    vjust = 1
  )
  
  p1 <- p1 + xlab("Time") + ylab("ABP")
  p2 <- p2 + xlab("Time") + ylab("CBFV")
  cmatch1 <- c(
    "ABP Signal" = abp.palette[4],
    "CBFV Signal" = cbfv.palette[4],
    "Baseline" = ann.palette[2],
    "Min. Search Window" = ann.palette[3]
  )
  p1 <- p1 + scale_colour_manual(values = cmatch1, breaks = names(cmatch1))
  p2 <- p2 + scale_colour_manual(values = cmatch1, breaks = names(cmatch1))
  p1 <- p1 + theme(
    legend.justification = c(1, 1),
    legend.background = element_rect(fill = alpha('grey90', 0.0)),
    legend.position = c(1, 1),
    legend.key.size =  unit(0.3, "cm"),
    legend.title = element_text(size = 7),
    legend.text = element_text(size = 6)
  )
  p2 <- p2 + theme(
    legend.justification = c(1, 1),
    legend.background = element_rect(fill = alpha('grey90', 0.0)),
    legend.position = c(1, 1),
    legend.key.size =  unit(0.4, "cm"),
    legend.title = element_text(size = 7),
    legend.text = element_text(size = 6)
  )
  
  #
  # Plot 3 (normalised signals)
  #
  
  ABP.time <- time.instants -
              time.instants[parameter.results[["min.ABP.sample"]]]
  ABP.signal <- parameter.results[["normalised.ABP.signal"]]
  
  CBFV.time <- time.instants -
               time.instants[parameter.results[["min.CBFV.sample"]]]
  CBFV.signal <- parameter.results[["normalised.CBFV.signal"]]
  
  time.ini <- 0 - time.until.min - time.tol
  time.fin <- 0 + time.after.min + time.tol
  
  rx <- time.fin - time.ini
  dx <- rx / 100
  ry <- max.normalised.intensity - min.normalised.intensity
  dy <- ry / 100
  
  # Plots CBFV
  dcbfv <- data.frame(
    Time = CBFV.time,
    Signal = CBFV.signal,
    Segment = "Normalised CBFV"
  )
  p3 <- ggplot(
    data = dcbfv,
    mapping = aes(x = Time, y = Signal, colour = Segment)
  )
  d <- dcbfv[1:(parameter.results[["transient.CBFV.samples"]][1]), ]
  p3 <- p3 + geom_line(data = d, linetype = "dashed")
  r <- tail(parameter.results[["transient.CBFV.samples"]], 1):nrow(dcbfv)
  d <- dcbfv[r, ]
  p3 <- p3 + geom_line(data = d, linetype = "dashed")
  
  # Plots ABP, if corresponds
  dabp <- data.frame(
    Time = ABP.time,
    Signal = ABP.signal,
    Segment = "Normalised ABP"
  )
  d <- dabp[1:(parameter.results[["transient.ABP.samples"]][1]), ]
  p3 <- p3 + geom_line(data = d, linetype = "dashed")
  d <-dabp[tail(parameter.results[["transient.ABP.samples"]], 1):nrow(dabp), ]
  p3 <- p3 + geom_line(data = d, linetype = "dashed")
  
  # Plots ABP transient segment
  d <- dabp[parameter.results[["transient.ABP.samples"]], ]
  p3 <- p3 + geom_line(data = d, linetype = "11")
  d[["Signal"]] <- parameter.results[["transient.ABP.line"]]
  d[["Segment"]] <- "Transient Line"
  p3 <- p3 + geom_line(data = d)
  
  # Plots CBFV transient segment
  d <- dcbfv[parameter.results[["transient.CBFV.samples"]], ]
  p3 <- p3 + geom_line(data = d, linetype = "11")
  d[["Signal"]] <- parameter.results[["transient.CBFV.line"]]
  d[["Segment"]] <- "Transient Line"
  p3 <- p3 + geom_line(data = d)
  
  # Plots CBFV steady segment
  d <- dcbfv[parameter.results[["steady.CBFV.samples"]], ]
  d[["Signal"]] <- parameter.results[["steady.CBFV.line"]]
  d[["Segment"]] <- "Steady Line"
  p3 <- p3 + geom_line(data = d, linetype = "dashed")
  
  # Plots and annotates Delta Tau
  s2 <- parameter.results[["transient.CBFV.samples"]]
  d2 <- dcbfv[s2, ]
  y2 <- min(d2[["Signal"]], na.rm = TRUE)
  tini2 <- CBFV.time[s2[1]]
  tfin2 <- CBFV.time[tail(s2, 1)]
  tini1 <- ABP.time > tini2 - time.tol
  tfin1 <- ABP.time < tfin2 + time.tol
  s1 <- which(tini1 & tfin1)
  d1 <- dabp[s1, ]
  y1 <- min(d1[["Signal"]], na.rm = TRUE)
  y <- min(y1, y2)
  d2[["Signal"]] <- y - 2 * dy
  d2[["Segment"]] <- "Transient Line"
  p3 <- p3 + geom_line(data = d2)
  m <- round(nrow(d2) / 2)
  d <- d2[m, ]
  d[["Signal"]] <- d[["Signal"]] - dy
  s <- paste(
    "Delta*tau",
    "==",
    sprintf("%.1f", parameter.results[["Delta.tau"]])
  )
  d <- cbind(d, Text = s)
  p3 <- p3 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 3,
    hjust = 0.4,
    vjust = 1,
    parse = TRUE
  )
  
  # Annotates Ks
  d2 <- dcbfv[parameter.results[["steady.CBFV.samples"]], ]
  i <- d2[["Signal"]] < parameter.results[["Ks"]]
  d2[["Signal"]] <- parameter.results[["Ks"]]
  d2[["Segment"]] <- "Steady Line"
  p3 <- p3 + geom_line(data = d2)
  u <- 2 * dy
  vjust <- 0
  if(sum(i) - sum(!i) < 0)
  {
    u <- -u
    vjust <- 1 - vjust
  }
  m <- round(nrow(d2) / 2)
  d <- d2[m, ]
  d[["Signal"]] <- d[["Signal"]] + u
  s <- paste("K[s]", "==", sprintf("%.4f", parameter.results[["Ks"]]))
  d <- cbind(d, Text = s)
  p3 <- p3 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 3,
    hjust = 0.5,
    vjust = vjust,
    parse = TRUE
  )
  
  # Plots and annotates Phi
  d1 <- dabp[parameter.results[["transient.ABP.samples"]], ]
  d1[["Signal"]] <- parameter.results[["transient.ABP.line"]]
  d2 <- dcbfv[parameter.results[["transient.CBFV.samples"]], ]
  d2[["Signal"]] <- parameter.results[["transient.CBFV.line"]]
  m <- round(min(nrow(d1), nrow(d2)) * 0.8)
  #d <- rbind(d1[m, ], d2[m, ])
  #d[["Segment"]] <- "Angle"
  #p3 <- p3 + geom_point(data = d)
  r <- d1[["Time"]][m]
  tt <- seq(-90, 90, length.out = 100)
  xx <- r * cos(tt * pi / 180)
  yy <- (sin(tt * pi / 180) + 1) / 2
  yy <- sin(tt * pi / 180)
  #d <- data.frame(Time = xx, Signal = yy, Segment = "Angle")
  #p3 <- p3 + geom_path(data = d, linetype = "dotted")
  .dist <- function(x, y) sqrt((x - xx)^2 + (y - yy)^2)
  .tmp.fun <- function(x, y) min(.dist(x, y))
  s1 <- mapply(.tmp.fun, d1[["Time"]], d1[["Signal"]])
  s1 <- which.min(s1)
  s2 <- mapply(.tmp.fun, d2[["Time"]], d2[["Signal"]])
  s2 <- which.min(s2)
  t1 <- ABP.time[parameter.results[["transient.ABP.samples"]][s1]]
  t2 <- CBFV.time[parameter.results[["transient.CBFV.samples"]][s2]]
  y1 <- parameter.results[["transient.ABP.line"]][s1]
  y2 <- parameter.results[["transient.CBFV.line"]][s2]
  #d <- data.frame(Time = c(t1, t2), Signal = c(y1, y2), Segment = "BLA")
  #p3 <- p3 + geom_point(data = d)
  i <- yy < max(y1, y2)
  j <- yy > min(y1, y2)
  d <- data.frame(
    Time = xx[i & j],
    Signal = yy[i & j],
    Segment = "Angle"
  )
  p3 <- p3 + geom_path(data = d, linetype = "11")
  
  i <- yy < max(y1, y2) & yy < 1
  j <- yy > min(y1, y2) & yy > 0
  if(sum(i & j) > 0)
  {
    xx <- xx[i & j]
    yy <- yy[i & j]
    d <- data.frame(Time = xx, Signal = yy, Segment = "Angle")
    p3 <- p3 + geom_path(data = d)
  }
  m <- round(nrow(d) / 2)
  d <- d[m, ]
  d[["Time"]] <- d[["Time"]] + dx
  s <- paste(
    "varphi",
    "==",
    sprintf("%.2f*degree", parameter.results[["Phi"]])
  )
  d <- cbind(d, Text = s)
  p3 <- p3 + geom_text(
    data = d,
    mapping = aes(label = Text),
    size = 3,
    hjust = 0,
    vjust = 0.5,
    parse = TRUE
  )
  
  p3 <- p3 + xlab("Time") + ylab("Normalised Signals")
  cmatch2 <- c(
    "Normalised ABP" = abp.palette[4],
    "Normalised CBFV" = cbfv.palette[4],
    "Transient Line" = ann.palette[2],
    "Steady Line" = ann.palette[3],
    "Angle" = ann.palette[4]
  )
  p3 <- p3 + scale_colour_manual(
    values = cmatch2,
    breaks = names(cmatch2)
  )
  p3 <- p3 + theme(
    legend.justification = c(0, 0),
    legend.background = element_rect(fill = alpha('grey90', 0.0)),
    legend.position = c(0, 0),
    legend.key.size =  unit(0.4, "cm"),
    legend.title = element_text(size = 7),
    legend.text = element_text(size = 6)
  )
  p3 <- p3 +  coord_cartesian(
    xlim = c(time.ini, time.fin),
    ylim = c(min.normalised.intensity, max.normalised.intensity)
  )
  
  #
  # Joins plots
  #
  
  main <- NULL
  if(!is.null(parameter.results[["mfARI"]]))
    main <- sprintf("mfARI = %.1f", parameter.results[["mfARI"]])
  else
    if(!is.null(index.estimation.function))
    {
      mfari <- index.estimation.function(
        Ks = parameter.results[["Ks"]],
        Delta.tau = parameter.results[["Delta.tau"]],
        Phi = parameter.results[["Phi"]]
      )
      main <- sprintf("estimated mfARI = %.1f", mfari)
    }
  
  if(!is.null(id))
    if(is.null(main))
      main <- id
    else
      main <- paste(id, main, sep = ", ")
  
  p <- arrangeGrob(
    arrangeGrob(p1, p2, nrow = 2),
    p3,
    nrow = 2,
    main = main
  )
  
  p
}


#
# Estimates the MSE for specified duration of the transient CBFV signal
# In this version, Ks is the value of the normalised CBFV at the end
# of the transient period.
#
# # Would be better to limit the CBFV angle here?
# # This would affect the slope and straight line,
# # affecting the transient MSE...
#
#
estimate.mfARI.CBFV.parameters.type.2 <- function(
      time.instants,
      normalised.CBFV.signal,
      min.CBFV.sample,
      min.CBFV.time.instant,
      transient.CBFV.duration,
      steady.CBFV.duration,
      time.tol = min(diff(time.instants)) / 100,
      ...
      )
{
  ans <- list()
  
  ans[["Delta.tau"]] <- transient.CBFV.duration
  ans[["tau"]] <- min.CBFV.time.instant + transient.CBFV.duration
  ans[["transient.CBFV.samples"]] <- which(
    time.instants >= min.CBFV.time.instant - time.tol &
    time.instants <= ans[["tau"]] + time.tol
  )
  ans[["transient.CBFV.time.instants"]] <- 
    time.instants[ans[["transient.CBFV.samples"]]]
  ans[["transient.normalised.CBFV.signal"]] <-
    normalised.CBFV.signal[ans[["transient.CBFV.samples"]]]
  ans[["transient.CBFV.slope"]] <- 
    mean(ans[["transient.normalised.CBFV.signal"]]) /
    mean(ans[["transient.CBFV.time.instants"]] - min.CBFV.time.instant)
  ans[["transient.CBFV.line"]] <- 
    ans[["transient.CBFV.slope"]] *
    (time.instants[ans[["transient.CBFV.samples"]]] -
     min.CBFV.time.instant)
  if(length(ans[["transient.normalised.CBFV.signal"]]) > 0)
    ans[["transient.CBFV.MSE"]] <- get.MSE(
      sim = ans[["transient.CBFV.line"]],
      obs = ans[["transient.normalised.CBFV.signal"]]
    )
  else
    ans[["transient.CBFV.MSE"]] <- Inf
  
  ans[["Ks"]] <- tail(ans[["transient.normalised.CBFV.signal"]], 1)
  ans[["nominal.steady.CBFV.duration"]] <- steady.CBFV.duration
  ans[["steady.CBFV.samples"]] <- which(
    time.instants >= ans[["tau"]] - time.tol &
    time.instants <= ans[["tau"]] + steady.CBFV.duration + time.tol
  )
  ans[["steady.CBFV.time.instants"]] <-
    time.instants[ans[["steady.CBFV.samples"]]]
  steady.CBFV.length <- length(ans[["steady.CBFV.samples"]])
  ans[["steady.CBFV.duration"]] <-
    ans[["steady.CBFV.time.instants"]][length(ans[["steady.CBFV.time.instants"]])] -
    ans[["tau"]]
  ans[["steady.normalised.CBFV.signal"]] <-
    normalised.CBFV.signal[ans[["steady.CBFV.samples"]]]
  ans[["steady.CBFV.line"]] <- rep(ans[["Ks"]], steady.CBFV.length)
  if(steady.CBFV.length > 0)
    ans[["steady.CBFV.MSE"]] <- get.MSE(
      sim = ans[["steady.CBFV.line"]],
      obs = ans[["steady.normalised.CBFV.signal"]]
    )
  else
    ans[["steady.CBFV.MSE"]] <- Inf
  
  ans[["CBFV.response.duration"]] <-
    ans[["Delta.tau"]] +
    ans[["steady.CBFV.duration"]]
  if(is.finite(ans[["transient.CBFV.MSE"]]) &&
     is.finite(ans[["steady.CBFV.MSE"]]))
    ans[["CBFV.response.MSE"]] <-
      (ans[["Delta.tau"]] / ans[["CBFV.response.duration"]]) *
      ans[["transient.CBFV.MSE"]] +
      (ans[["steady.CBFV.duration"]] / ans[["CBFV.response.duration"]]) *
      ans[["steady.CBFV.MSE"]]
  else
    ans[["CBFV.response.MSE"]] <- Inf
  
  return(ans)
}


#
# Estimates the parameters and MSE for each possible duration of the
# transient CBFV signal.
#
# # Here, if the signal is too short to cover the specified maximum
# # transient CBFV duration, this maximum is reduced (keeping the
# # specified duration of the steady CBFV signal).
#
# # For equal MSE values, it prefers the one with lower transient CBFV
# # duration.
# 
search.mfARI.CBFV.parameters <- function(
      time.instants,
      normalised.CBFV.signal,
      min.CBFV.sample,
      min.CBFV.time.instant,
      min.transient.CBFV.duration,
      max.transient.CBFV.duration,
      steady.CBFV.duration,
      time.tol = min(diff(time.instants)) / 100,
      estimation.function = estimate.mfARI.CBFV.parameters.type.2,
      keep.search.results = FALSE,
      add.search.plots = FALSE,
      id = NULL,
      ...
      )
{
  min.delta.tau <- min.CBFV.time.instant + min.transient.CBFV.duration
  max.delta.tau <- min.CBFV.time.instant + max.transient.CBFV.duration
  last.time <- max.delta.tau + steady.CBFV.duration
  last.instant <-time.instants[length(time.instants)]
  if(last.time > last.instant + time.tol)
  {
    last.time <- last.instant
    max.delta.tau <- last.time - steady.CBFV.duration
    max.transient.CBFV.duration <- max.delta.tau - min.CBFV.time.instant
    if(is.null(id))
      msg <- "Warning: signal is too short for specified parameters;"
    else
      msg <- sprintf(
        "Warning: signal is too short for specified parameters (%s);",
        id
      ) 
    msg <- paste(
      msg,
      "maximum transient CBFV signal duration was reduced to")
    msg <- paste(
      msg,
      sprintf('%.1f seconds', max.transient.CBFV.duration)
    )
    warning(msg)
  }
  i <- which(
    time.instants >= min.delta.tau - time.tol &
    time.instants <= max.delta.tau + time.tol
  )
  
  ans <- list()
  ans[["Delta.tau.values"]] <- time.instants[i] - min.CBFV.time.instant
  
  plots <- list()
  results <- list()
  best.result <- list(CBFV.response.MSE = Inf)
  
  for(delta.tau in ans[["Delta.tau.values"]])
  {
    current.estimation <- estimation.function(
      time.instants = time.instants,
      normalised.CBFV.signal = normalised.CBFV.signal,
      min.CBFV.sample = min.CBFV.sample,
      min.CBFV.time.instant = min.CBFV.time.instant,
      transient.CBFV.duration = delta.tau,
      steady.CBFV.duration = steady.CBFV.duration,
      time.tol = time.tol,
      ...
    )
    
    if(current.estimation[["CBFV.response.MSE"]] <
       best.result[["CBFV.response.MSE"]])
      best.result <- current.estimation
    
    str.delta.tau <- as.character(delta.tau)
    if(keep.search.results)
    {
      lce <- list(current.estimation)
      names(lce) <- str.delta.tau
      results <- c(results, lce)
    }
    
    if(add.search.plots)
    {
      p <- get.plot.CBFV.parameters.search(
        time.instants,
        normalised.CBFV.signal,
        min.CBFV.sample,
        min.CBFV.time.instant,
        current.estimation,
        id = id,
        ...
      )
      lp <- list(p)
      names(lp) <- str.delta.tau
      plots <- c(plots, lp)
    }
  }
  
  if(keep.search.results)
    ans[["search.results"]] <- results
  
  if(add.search.plots)
    ans[["search.plots"]] <- plots
  
  ans <- c(ans, best.result)
  
  return(ans)
}


normalise.ABP.signal <- function(
      time.instants,
      ABP.signal,
      sample.release,
      sampling.time,
      baseline.initial.time = time.instants[1],
      baseline.final.time = time.instants[sample.release],
      min.ABP.max.delta.time = 5 * 0.8,
      time.tol = sampling.time / 100
      )
{
  ans <- list()
  valid <- !is.na(ABP.signal)
  time.release <- time.instants[sample.release]
  
  # Adds baseline data to answer
  if(baseline.final.time < baseline.initial.time)
    stop("final time for the ABP baseline cannot be ",
         "before its initial time")
  if(baseline.final.time > time.release)
    stop("final time for the ABP baseline cannot be ",
         "after the release of the thigh cuffs")
  
	i <- which(valid & time.instants >= baseline.initial.time - time.tol)
  if(length(i) == 0)
    stop("no time instant for the beginning of the ABP baseline could be determined")
	i <- i[1]
  ans[["ABP.baseline.initial.time"]] <- time.instants[i]
  ans[["ABP.baseline.initial.sample"]] <- i
  
	i <- which(valid & time.instants <= baseline.final.time + time.tol)
  if(length(i) == 0)
    stop("no time instant for the end of the ABP baseline could be determined")
	i <- tail(i, 1)
  ans[["ABP.baseline.final.time"]] <- time.instants[i]
  ans[["ABP.baseline.final.sample"]] <- i
  
	ans[["ABP.baseline.samples"]] <-
    ans[["ABP.baseline.initial.sample"]]:ans[["ABP.baseline.final.sample"]]
	ans[["ABP.baseline.duration"]] <-
    ans[["ABP.baseline.final.time"]] - ans[["ABP.baseline.initial.time"]]
  ans[["ABP.baseline.value"]] <- mean(
    ABP.signal[ans[["ABP.baseline.samples"]]],
    na.rm = TRUE
  )
  
  # Sets min ABP window
  ans[["min.ABP.max.delta.time"]] <- min.ABP.max.delta.time
  i <- time.instants > time.release + time.tol
  j <- time.instants < time.release + ans[["min.ABP.max.delta.time"]] +
       time.tol
  ans[["min.ABP.samples"]] <- which(valid & i & j)
  if(length(ans[["min.ABP.samples"]]) == 0)
    stop("no candidates for min ABP")
    
  # Gets minimum ABP
  ans[["min.ABP.window"]] <- ABP.signal[ans[["min.ABP.samples"]]]
  min.sample.in.window <- which.min(ans[["min.ABP.window"]])
  min.ABP.sample <- min.sample.in.window + ans[["min.ABP.samples"]][1] - 1
  ans[["min.ABP.time.instant"]] <- time.instants[min.ABP.sample]
  ans[["min.ABP.sample"]] <- min.ABP.sample
  ans[["min.ABP.value"]] <- ABP.signal[min.ABP.sample]
  
  # Gets min ABP type
  min.info <- findpeaks(
    -ans[["min.ABP.window"]],
    zero = "-",
    sortstr = TRUE
  )
  ans[["min.ABP.type"]] <- "minimum value in window"
  if(!is.null(min.info) && min.info[1, 2] == min.sample.in.window)
    ans[["min.ABP.type"]] <- "local minimum"
  
  # Measures min ABP drop
  ans[["min.ABP.drop"]] <-
    ans[["ABP.baseline.value"]] - ans[["min.ABP.value"]]
  ans[["min.ABP.drop.pc"]] <-
    ans[["min.ABP.drop"]] / ans[["ABP.baseline.value"]]
  ans[["min.ABP.drop.pc"]] <- round(ans[["min.ABP.drop.pc"]] * 100, 2)
  
  # Normalises the signal
  ans[["normalised.ABP.signal"]] <- normalise.signal(
    signal = ABP.signal,
    signal.baseline.value = ans[["ABP.baseline.value"]],
    signal.min.value = ans[["min.ABP.value"]]
  )
  
  invisible(ans)
}


normalise.CBFV.signal <- function(
      time.instants,
      CBFV.signal,
      sample.release,
      sampling.time,
      baseline.initial.time = time.instants[1],
      baseline.final.time = time.instants[sample.release],
      min.CBFV.max.delta.time = 8 * 0.8,
      time.tol = sampling.time / 100
      )
{
  ans <- list()
  valid <- !is.na(CBFV.signal)
  time.release <- time.instants[sample.release]
  
  # Adds baseline data to answer
  if(baseline.final.time < baseline.initial.time)
    stop("final time for the CBFV baseline cannot be before its initial time")
  if(baseline.final.time > time.release)
    stop("final time for the CBFV baseline cannot be after the release of the thigh cuffs")
  
	i <- which(valid & time.instants >= baseline.initial.time - time.tol)
  if(length(i) == 0)
    stop("no time instant for the beginning of the CBFV baseline could be determined")
	i <- i[1]
  ans[["CBFV.baseline.initial.time"]] <- time.instants[i]
  ans[["CBFV.baseline.initial.sample"]] <- i
  
	i <- which(valid & time.instants <= baseline.final.time + time.tol)
  if(length(i) == 0)
    stop("no time instant for the end of the CBFV baseline could be determined")
	i <- tail(i, 1)
  ans[["CBFV.baseline.final.time"]] <- time.instants[i]
  ans[["CBFV.baseline.final.sample"]] <- i
  
	ans[["CBFV.baseline.samples"]] <- ans[["CBFV.baseline.initial.sample"]]:ans[["CBFV.baseline.final.sample"]]
	ans[["CBFV.baseline.duration"]] <- ans[["CBFV.baseline.final.time"]] - ans[["CBFV.baseline.initial.time"]]
  
  # Gets baseline value
  ans[["CBFV.baseline.value"]] <- mean(CBFV.signal[ans[["CBFV.baseline.samples"]]], na.rm = TRUE)
  
  # Sets min CBFV window
  ans[["min.CBFV.max.delta.time"]] <- min.CBFV.max.delta.time
  i <- time.instants > time.release + time.tol
  j <- time.instants < time.release + ans[["min.CBFV.max.delta.time"]] +
       time.tol
  ans[["min.CBFV.samples"]] <- which(valid & i & j)
  if(length(ans[["min.CBFV.samples"]]) == 0)
    stop("no candidates for min CBFV")
  
  # Gets minimum CBFV
  ans[["min.CBFV.window"]] <- CBFV.signal[ans[["min.CBFV.samples"]]]
  min.sample.in.window <- which.min(ans[["min.CBFV.window"]])
  min.CBFV.sample <- min.sample.in.window + ans[["min.CBFV.samples"]][1] - 1
  ans[["min.CBFV.time.instant"]] <- time.instants[min.CBFV.sample]
  ans[["min.CBFV.sample"]] <- min.CBFV.sample
  ans[["min.CBFV.value"]] <- CBFV.signal[min.CBFV.sample]
  
  # Gets min CBFV type
  min.info <- findpeaks(
    -ans[["min.CBFV.window"]],
    zero = "-",
    sortstr = TRUE
  )
  ans[["min.CBFV.type"]] <- "minimum value in window"
  if(!is.null(min.info) && min.info[1, 2] == min.sample.in.window)
    ans[["min.CBFV.type"]] <- "local minimum"
  
  # Normalises the signal
  ans[["normalised.CBFV.signal"]] <- normalise.signal(
    signal = CBFV.signal,
    signal.baseline.value = ans[["CBFV.baseline.value"]],
    signal.min.value = ans[["min.CBFV.value"]]
  )
  
  invisible(ans)
}


get.mfARI.parameters <- function(
      time.instants,
      ABP.signal,
      CBFV.signal,
      sampling.time,
      time.release,
      baseline.initial.time = time.instants[1],
      baseline.final.time = time.release,
      min.ABP.max.delta.time = 5 * 0.8,
      transient.ABP.duration = 6,
      min.CBFV.max.delta.time = 8 * 0.8, 
      min.transient.CBFV.duration = 1.5,
      max.transient.CBFV.duration = 10,
      steady.CBFV.duration = 6,
      min.Ks = 0.0,
      max.Ks = 1.022095,
      min.ABP.angle = 0.0,
      max.ABP.angle = 90.0,
      min.CBFV.angle = 0.0,
      max.CBFV.angle = 90.0,
      min.Phi = 0.0,
      max.Phi = 35.238932,
      keep.details = TRUE,
      ABP.rounding.digits = 2,
      normalised.ABP.rounding.digits = ABP.rounding.digits * 2,
      CBFV.rounding.digits = 2,
      normalised.CBFV.rounding.digits = CBFV.rounding.digits * 2,
      Ks.rounding.digits = 4,
      Phi.rounding.digits = 2,
      time.tol = sampling.time / 100,
      ... # Passed to search.mfARI.CBFV.parameters
      )
{
  # Simple validations
  if(transient.ABP.duration < sampling.time)
    stop("duration of the transient ABP signal is too short")
  if(steady.CBFV.duration < sampling.time)
    stop("duration of the steady CBFV signal is too short")
  if(max.transient.CBFV.duration < min.transient.CBFV.duration)
    stop("maximum duration of the transient CBFV signal must be equal or higher than the specified minimum duration")
  if(max.Ks < min.Ks)
    stop("maximum value for Ks must be equal or higher than the specified minimum value")
  if(max.ABP.angle < min.ABP.angle)
    stop("maximum value for the ABP recovery angle must be equal or higher than the specified minimum value")
  if(max.CBFV.angle < min.CBFV.angle)
    stop("maximum value for the CBFV recovery angle must be equal or higher than the specified minimum value")
  if(max.Phi < min.Phi)
    stop("maximum value for the difference between ABP and CBFV recovery angles must be equal or higher than the specified minimum value")
    
  # Validates the lengths of the signals
  if(length(ABP.signal) != length(CBFV.signal))
    stop("ABP and CBFV signals must be of the same length")
  if(length(ABP.signal) != length(time.instants))
    stop("number of time instants must coincide with the signals")
  
  # Validates sampling time
  if(sampling.time < 0.1 - time.tol)
    stop("sampling time must be equal or higher than 0.1")
  
  # Defines the detailed answer list
  dans <- list()
  
  # Copies the original signals
	dans[["time.instants"]] <- round(time.instants, 1)
	dans[["ABP.signal"]] <- round(ABP.signal, ABP.rounding.digits)
	dans[["CBFV.signal"]] <- round(CBFV.signal, CBFV.rounding.digits)
  
  # Sets frequency
	dans[["sampling.time"]] <- round(sampling.time, 1)
	dans[["frequency"]] <- 1 / sampling.time
  
  # Sets release sample
	dans[["time.release"]] <- time.release
  i <- which(are.tolerably.equal(time.instants, time.release, time.tol))
  if(length(i) != 1)
    stop("a unique time instant for the thigh-cuff release could not be determined")
  dans[["sample.release"]] <- i
  if(!is.finite(ABP.signal[i]))
    stop("invalid ABP signal value at the time of thigh-cuff release")
  if(!is.finite(CBFV.signal[i]))
    stop("invalid CBFV signal value at the time of thigh-cuff release")
  
  # Normalises ABP
  nabp <- normalise.ABP.signal(
    time.instants = dans[["time.instants"]],
    ABP.signal = dans[["ABP.signal"]],
    sample.release = dans[["sample.release"]],
    sampling.time = dans[["sampling.time"]],
    baseline.initial.time = baseline.initial.time,
    baseline.final.time = baseline.final.time,
    min.ABP.max.delta.time = min.ABP.max.delta.time,
    time.tol = time.tol
  )
  nabp[["normalised.ABP.signal"]] <- round(
    nabp[["normalised.ABP.signal"]],
    normalised.ABP.rounding.digits
  )
  dans <- c(dans, nabp)
  
  # Normalises CBFV
  ncbfv <- normalise.CBFV.signal(
    time.instants = dans[["time.instants"]],
    CBFV.signal = dans[["CBFV.signal"]],
    sample.release = dans[["sample.release"]],
    sampling.time = dans[["sampling.time"]],
    baseline.initial.time = baseline.initial.time,
    baseline.final.time = baseline.final.time,
    min.CBFV.max.delta.time = min.CBFV.max.delta.time,
    time.tol = time.tol
  )
  ncbfv[["normalised.CBFV.signal"]] <- round(
    ncbfv[["normalised.CBFV.signal"]],
    normalised.CBFV.rounding.digits
  )
  dans <- c(dans, ncbfv)
  
  if(any(nabp[["ABP.baseline.samples"]] != ncbfv[["CBFV.baseline.samples"]]))
    warning("baseline samples are different for ABP and CBFV")
  
  # Search for best Ks and delta-tau
  search.results <- search.mfARI.CBFV.parameters(
    time.instants = time.instants,
    normalised.CBFV.signal = dans[["normalised.CBFV.signal"]],
    min.CBFV.sample = dans[["min.CBFV.sample"]],
    min.CBFV.time.instant = dans[["min.CBFV.time.instant"]],
    min.transient.CBFV.duration = min.transient.CBFV.duration,
    max.transient.CBFV.duration = max.transient.CBFV.duration,
    steady.CBFV.duration = steady.CBFV.duration,
    time.tol = time.tol,
    ...)
  dans <- c(dans, search.results)
  
  dans[["Delta.tau"]] <- round(dans[["Delta.tau"]], 1)
  
  # Bounds Ks parameter
  if(dans[["Ks"]] < min.Ks)
    dans[["Ks"]] <- min.Ks
  if(dans[["Ks"]] > max.Ks)
    dans[["Ks"]] <- max.Ks
  dans[["Ks"]] <- round(dans[["Ks"]], Ks.rounding.digits)
    
  # Gets transient ABP representation
  dans[["nominal.transient.ABP.duration"]] <- transient.ABP.duration
  nominal.transient.ABP.end.time <- dans[["min.ABP.time.instant"]] + transient.ABP.duration
  dans[["transient.ABP.samples"]] <- which(time.instants >= dans[["min.ABP.time.instant"]] & time.instants <= nominal.transient.ABP.end.time)
  dans[["transient.ABP.time.instants"]] <- time.instants[dans[["transient.ABP.samples"]]]
  dans[["transient.ABP.duration"]] <- dans[["transient.ABP.time.instants"]][length(dans[["transient.ABP.time.instants"]])] - dans[["min.ABP.time.instant"]]
  dans[["transient.normalised.ABP.signal"]] <- dans[["normalised.ABP.signal"]][dans[["transient.ABP.samples"]]]
  dans[["transient.ABP.slope"]] <- mean(dans[["transient.normalised.ABP.signal"]]) / mean(dans[["transient.ABP.time.instants"]] - dans[["min.ABP.time.instant"]])
  dans[["transient.ABP.line"]] <- dans[["transient.ABP.slope"]] * (time.instants[dans[["transient.ABP.samples"]]] - dans[["min.ABP.time.instant"]])
  
  # Gets transient ABP angle
  dans[["transient.ABP.angle"]] <- atan(dans[["transient.ABP.slope"]]) * 180 / pi
  dans[["bounded.transient.ABP.angle"]] <- dans[["transient.ABP.angle"]]
  if(dans[["bounded.transient.ABP.angle"]] < min.ABP.angle)
    dans[["bounded.transient.ABP.angle"]] <- min.ABP.angle
  if(dans[["bounded.transient.ABP.angle"]] > max.ABP.angle)
    dans[["bounded.transient.ABP.angle"]] <- max.ABP.angle
  
  # Gets transient CBFV angle
  dans[["transient.CBFV.angle"]] <- atan(dans[["transient.CBFV.slope"]]) * 180 / pi
  dans[["bounded.transient.CBFV.angle"]] <- dans[["transient.CBFV.angle"]]
  if(dans[["bounded.transient.CBFV.angle"]] < min.CBFV.angle)
    dans[["bounded.transient.CBFV.angle"]] <- min.CBFV.angle
  if(dans[["bounded.transient.CBFV.angle"]] > max.CBFV.angle)
    dans[["bounded.transient.CBFV.angle"]] <- max.CBFV.angle
  
  # Gets Phi parameter
  dans[["transient.angles.difference"]] <- dans[["transient.CBFV.angle"]] - dans[["transient.ABP.angle"]]
  dans[["bounded.transient.angles.difference"]] <- dans[["bounded.transient.CBFV.angle"]] - dans[["bounded.transient.ABP.angle"]]
  dans[["Phi"]] <- dans[["bounded.transient.angles.difference"]]
  if(dans[["Phi"]] < min.Phi)
    dans[["Phi"]] <- min.Phi
  if(dans[["Phi"]] > max.Phi)
    dans[["Phi"]] <- max.Phi
  dans[["Phi"]] <- round(dans[["Phi"]], Phi.rounding.digits)
  
  if(!keep.details)
  {
    ans <- list()
    
    ans[["ABP.baseline.samples"]] <- dans[["ABP.baseline.samples"]]
    ans[["ABP.baseline.value"]] <- dans[["ABP.baseline.value"]]
    
    ans[["min.ABP.samples"]] <- dans[["min.ABP.samples"]]
    ans[["min.ABP.sample"]] <- dans[["min.ABP.sample"]]
    ans[["min.ABP.value"]] <- dans[["min.ABP.value"]]
    ans[["min.ABP.type"]] <- dans[["min.ABP.type"]]
    ans[["min.ABP.drop.pc"]] <- dans[["min.ABP.drop.pc"]]
    
    ans[["CBFV.baseline.samples"]] <- dans[["CBFV.baseline.samples"]]
    ans[["CBFV.baseline.value"]] <- dans[["CBFV.baseline.value"]]
    
    ans[["min.CBFV.samples"]] <- dans[["min.CBFV.samples"]]
    ans[["min.CBFV.sample"]] <- dans[["min.CBFV.sample"]]
    ans[["min.CBFV.value"]] <- dans[["min.CBFV.value"]]
    ans[["min.CBFV.type"]] <- dans[["min.CBFV.type"]]
    
    ans[["normalised.ABP.signal"]] <- dans[["normalised.ABP.signal"]]
    ans[["normalised.CBFV.signal"]] <- dans[["normalised.CBFV.signal"]]
    
    ans[["transient.ABP.samples"]] <- dans[["transient.ABP.samples"]]
    ans[["transient.ABP.slope"]] <- dans[["transient.ABP.slope"]]
    ans[["transient.ABP.line"]] <- dans[["transient.ABP.line"]]
    
    ans[["transient.CBFV.samples"]] <- dans[["transient.CBFV.samples"]]
    ans[["transient.CBFV.slope"]] <- dans[["transient.CBFV.slope"]]
    ans[["transient.CBFV.line"]] <- dans[["transient.CBFV.line"]]
    ans[["steady.CBFV.samples"]] <- dans[["steady.CBFV.samples"]]
    ans[["steady.CBFV.line"]] <- dans[["steady.CBFV.line"]]
    ans[["steady.CBFV.duration"]] <- dans[["steady.CBFV.duration"]]
    
    ans[["Delta.tau"]] <- dans[["Delta.tau"]]
    ans[["Ks"]] <- dans[["Ks"]]
    ans[["Phi"]] <- dans[["Phi"]]
  }
  else
    ans <- dans
  
  invisible(ans)
}


get.unbounded.mfARI.parameters.for.AT.decimal <- function()
{
  sampling.time <- 0.1
  time.until.release <- 10
  time.after.release <- 20
  smooth.step.stimulus <- FALSE
  filter.order <- 2
  cutoff.frequency <- 0.20
  left.stabilisation.time <- ifelse(smooth.step.stimulus, 30, 0)
  time.rounding.digits <-  1
  stabilisation.time <- 1
  rounding.digits <- 6
  
  min.ABP.max.delta.time <- 5 * 0.8
  transient.ABP.duration <- 6
  min.CBFV.max.delta.time <- 8 * 0.8
  min.transient.CBFV.duration <- 0.1
  max.transient.CBFV.duration <- 20 - 6 - 0.1
  steady.CBFV.duration <- 6
  min.Ks <- -Inf
  max.Ks <- Inf
  min.ABP.angle <- -Inf
  max.ABP.angle <- Inf
  min.CBFV.angle <- -Inf
  max.CBFV.angle <- Inf
  min.Phi <- -Inf
  max.Phi <- Inf
  
  params <- get.AT.decimal.templates.parameters(rounding.digits = rounding.digits)
  curves <- get.AT.decimal.templates(sampling.time = sampling.time,
                                     time.until.release = time.until.release,
                                     time.after.release = time.after.release,
                                     smooth.step.stimulus = smooth.step.stimulus,
                                     filter.order = filter.order,
                                     cutoff.frequency = cutoff.frequency,
                                     left.stabilisation.time = left.stabilisation.time,
                                     time.rounding.digits =  time.rounding.digits,
                                     stabilisation.time = stabilisation.time,
                                     rounding.digits = rounding.digits)
  
  .tmp.fun <- function(i) get.mfARI.parameters(
    time.instants = curves[[i]][["time.instants"]],
    ABP.signal = curves[[i]][["ABP.normalised"]],
    CBFV.signal = curves[[i]][["CBFV.step.response"]],
    sampling.time = curves[[i]][["sampling.time"]],
    time.release = curves[[i]][["time.release"]],
    baseline.initial.time = curves[[i]][["time.instants"]][1],
    baseline.final.time = curves[[i]][["time.release"]],
    min.ABP.max.delta.time = min.ABP.max.delta.time,
    transient.ABP.duration = transient.ABP.duration,
    min.CBFV.max.delta.time = min.CBFV.max.delta.time,
    min.transient.CBFV.duration = min.transient.CBFV.duration,
    max.transient.CBFV.duration = max.transient.CBFV.duration,
    steady.CBFV.duration = steady.CBFV.duration,
    min.Ks = min.Ks,
    max.Ks = max.Ks,
    min.ABP.angle = min.ABP.angle,
    max.ABP.angle = max.ABP.angle,
    min.CBFV.angle = min.CBFV.angle,
    max.CBFV.angle = max.CBFV.angle,
    min.Phi = min.Phi,
    max.Phi = max.Phi,
    rounding.digits = rounding.digits,
    keep.details = FALSE
  )
  mfARI.curves <- lapply(1:length(curves), .tmp.fun)
  
  ATARI <- params[["ARI"]]
  Ks <- sapply(mfARI.curves, function(c) c[["Ks"]])
  Delta.tau <- sapply(mfARI.curves, function(c) c[["Delta.tau"]])
  Phi <- sapply(mfARI.curves, function(c) c[["Phi"]])
  
  data.frame(ATARI, Ks, Delta.tau, Phi)
}


get.bounds.of.mfARI.parameters.for.AT.decimal <- function()
{
  table <- get.unbounded.mfARI.parameters.for.AT.decimal()
  table[["Delta.tau"]][1] <- max(table[["Delta.tau"]][-1])
  
  mins <- c(min(table[["Ks"]]), min(table[["Delta.tau"]]), min(table[["Phi"]]))
  maxs <- c(max(table[["Ks"]]), max(table[["Delta.tau"]]), max(table[["Phi"]]))
  
  ans <- data.frame(matrix(c(mins, maxs), nrow = 3))
  rownames(ans) <- c("Ks", "Delta.tau", "Phi")
  colnames(ans) <- c("Min", "Max")
  
  ans
}


get.bounded.mfARI.parameters.for.AT.decimal <- function()
{
  sampling.time <- 0.1
  time.until.release <- 10
  time.after.release <- 20
  smooth.step.stimulus <- FALSE
  filter.order <- 2
  cutoff.frequency <- 0.20
  left.stabilisation.time <- ifelse(smooth.step.stimulus, 30, 0)
  time.rounding.digits <-  1
  stabilisation.time <- 1
  rounding.digits <- 6
  
  min.ABP.max.delta.time <- 5 * 0.8
  transient.ABP.duration <- 6
  min.CBFV.max.delta.time <- 8 * 0.8
  min.transient.CBFV.duration <-  1.5
  max.transient.CBFV.duration <- 10.0
  steady.CBFV.duration <- 6
  min.Ks <- 0.0
  max.Ks <- 1.022095
  min.ABP.angle <- 0
  max.ABP.angle <- 90
  min.CBFV.angle <- 0
  max.CBFV.angle <- 90
  min.Phi <- 0.0
  max.Phi <- 35.238932
  
  params <- get.AT.decimal.templates.parameters(rounding.digits = rounding.digits)
  curves <- get.AT.decimal.templates(sampling.time = sampling.time,
                                     time.until.release = time.until.release,
                                     time.after.release = time.after.release,
                                     smooth.step.stimulus = smooth.step.stimulus,
                                     filter.order = filter.order,
                                     cutoff.frequency = cutoff.frequency,
                                     left.stabilisation.time = left.stabilisation.time,
                                     time.rounding.digits =  time.rounding.digits,
                                     stabilisation.time = stabilisation.time,
                                     rounding.digits = rounding.digits)
  
  .tmp.fun <- function(i) get.mfARI.parameters(
    time.instants = curves[[i]][["time.instants"]],
    ABP.signal = curves[[i]][["ABP.normalised"]],
    CBFV.signal = curves[[i]][["CBFV.step.response"]],
    sampling.time = curves[[i]][["sampling.time"]],
    time.release = curves[[i]][["time.release"]],
    baseline.initial.time = curves[[i]][["time.instants"]][1],
    baseline.final.time = curves[[i]][["time.release"]],
    min.ABP.max.delta.time = min.ABP.max.delta.time,
    transient.ABP.duration = transient.ABP.duration,
    min.CBFV.max.delta.time = min.CBFV.max.delta.time,
    min.transient.CBFV.duration = min.transient.CBFV.duration,
    max.transient.CBFV.duration = max.transient.CBFV.duration,
    steady.CBFV.duration = steady.CBFV.duration,
    min.Ks = min.Ks,
    max.Ks = max.Ks,
    min.ABP.angle = min.ABP.angle,
    max.ABP.angle = max.ABP.angle,
    min.CBFV.angle = min.CBFV.angle,
    max.CBFV.angle = max.CBFV.angle,
    min.Phi = min.Phi,
    max.Phi = max.Phi,
    rounding.digits = rounding.digits,
    keep.details = FALSE
  )
    
  n <- length(curves)
  mfARI.curves <- lapply(1:n, .tmp.fun)
  
  ATARI <- params[["ARI"]]
  Ks <- sapply(mfARI.curves, function(c) c[["Ks"]])
  Delta.tau <- sapply(mfARI.curves, function(c) c[["Delta.tau"]])
  Delta.tau[1] <- max.transient.CBFV.duration
  Phi <- sapply(mfARI.curves, function(c) c[["Phi"]])
  
  data.frame(ATARI, Ks, Delta.tau, Phi)
}


get.ATARI.mfARI.linear.relationship <- function()
{
  params <<- get.bounded.mfARI.parameters.for.AT.decimal()
  model <- lm(ATARI ~ Ks + Delta.tau + Phi, data = params)
  print(summary(model))
  
  predictions <- stats::predict(model, newdata = params, interval = "confidence")
  colnames(predictions) <- c("mfARI", "lwr", "upr")
  params <- cbind(params, predictions)
  
  p1 <- ggplot(params, aes(x = ATARI, y = Ks))
  p1 <- p1 + geom_line() + geom_point()
  p2 <- ggplot(params, aes(x = ATARI, y = Delta.tau))
  p2 <- p2 + geom_line() + geom_point()
  p3 <- ggplot(params, aes(x = ATARI, y = Phi))
  p3 <- p3 + geom_line() + geom_point()
  
  i <- complete.cases(params)
  print(params[!i])
  print(params)
  
  g <- ggplot(data = params, aes(x=ATARI, y = mfARI))
  g <- g + geom_point()
  g <- g + stat_smooth(method = "lm", colour = "red")
  g <- g + xlim(0, 9) + ylim(0, 9)
  p <- arrangeGrob(p1, p2, p3, g)
  #capture.output(print(summary(m), prmsd=TRUE, digits=1))
  
  list(model, p)
}


get.ATARI.mfARI.linear.relationship.no.Phi <- function()
{
  params <<- get.bounded.mfARI.parameters.for.AT.decimal()
  model <- lm(ATARI ~ Ks + Delta.tau, data = params)
  print(summary(model))
  
  predictions <- stats::predict(model, newdata = params, interval = "confidence")
  colnames(predictions) <- c("mfARI", "lwr", "upr")
  params <- cbind(params, predictions)
  
  p1 <- ggplot(params, aes(x = ATARI, y = Ks))
  p1 <- p1 + geom_line() + geom_point()
  p2 <- ggplot(params, aes(x = ATARI, y = Delta.tau))
  p2 <- p2 + geom_line() + geom_point()
  p3 <- ggplot(params, aes(x = ATARI, y = Phi))
  p3 <- p3 + geom_line() + geom_point()
  
  g <- ggplot(data = params, aes(x=ATARI, y = mfARI))
  g <- g + geom_point()
  g <- g + stat_smooth(method = "lm", colour = "red")
  g <- g + xlim(0, 9) + ylim(0, 9)
  p <- arrangeGrob(p1, p2, p3, g)
  #capture.output(print(summary(m), prmsd=TRUE, digits=1))
  
  list(model, p)
}

<file_sep># Create remote compute target
from azureml.core.compute import BatchAiCompute
from azureml.core.compute import ComputeTarget
import os
import Workspace_Experiment as we 

ws = we.load_workspace
# choose a name for your cluster
batchai_cluster_name = os.environ.get("BATCHAI_CLUSTER_NAME", ws.name + "gpu")
cluster_min_nodes = os.environ.get("BATCHAI_CLUSTER_MIN_NODES", 1)
cluster_max_nodes = os.environ.get("BATCHAI_CLUSTER_MAX_NODES", 3)
vm_size = os.environ.get("BATCHAI_CLUSTER_SKU", "STANDARD_NC6")
autoscale_enabled = os.environ.get("BATCHAI_CLUSTER_AUTOSCALE_ENABLED", True)


if batchai_cluster_name in ws.compute_targets:
    compute_target = ws.compute_targets[batchai_cluster_name]
    if compute_target and type(compute_target) is BatchAiCompute:
        print('found compute target. just use it. ' + batchai_cluster_name)
else:
    print('creating a new compute target...')
    provisioning_config = BatchAiCompute.provisioning_configuration(vm_size = vm_size, # NC6 is GPU-enabled
                                                                vm_priority = 'lowpriority', # optional
                                                                autoscale_enabled = autoscale_enabled,
                                                                cluster_min_nodes = cluster_min_nodes, 
                                                                cluster_max_nodes = cluster_max_nodes)

    # create the cluster
    compute_target = ComputeTarget.create(ws, batchai_cluster_name, provisioning_config)

    # can poll for a minimum number of nodes and for a specific timeout. 
    # if no min node count is provided it will use the scale settings for the cluster
    compute_target.wait_for_completion(show_output=True, min_node_count=None, timeout_in_minutes=20)

     # For a more detailed view of current BatchAI cluster status, use the 'status' property    
    print(compute_target.status.serialize())
<file_sep>

# Change accordingly
VERSION <- "v15.9.10"
WORK.DIR <- file.path("", "research", "Memoristas", VERSION)


CARSIG.SCRIPT.BASENAME <- paste("carSignal", VERSION, sep = "-")
CARSIG.SCRIPT.BASENAME <- paste(CARSIG.SCRIPT.BASENAME, "R", sep = ".")
CARSIG.SCRIPT.NAME <- file.path(WORK.DIR, CARSIG.SCRIPT.BASENAME)
source(CARSIG.SCRIPT.NAME)

METRIC.UTILS.SCRIPT.BASENAME <-
  paste("Metric", "utils", VERSION, sep = "-")
METRIC.UTILS.SCRIPT.BASENAME <-
  paste(METRIC.UTILS.SCRIPT.BASENAME, "R", sep = ".")
METRIC.UTILS.SCRIPT.NAME <-
  file.path(WORK.DIR, METRIC.UTILS.SCRIPT.BASENAME)
source(METRIC.UTILS.SCRIPT.NAME)

MFARI.SCRIPT.BASENAME <- paste("mfARI", VERSION, sep = "-")
MFARI.SCRIPT.BASENAME <- paste(MFARI.SCRIPT.BASENAME, "R", sep = ".")
MFARI.SCRIPT.NAME <- file.path(WORK.DIR, MFARI.SCRIPT.BASENAME)
source(MFARI.SCRIPT.NAME)

ROUND.UTILS.SCRIPT.BASENAME <-
  paste("Rounding", "utils", VERSION, sep = "-")
ROUND.UTILS.SCRIPT.BASENAME <-
  paste(ROUND.UTILS.SCRIPT.BASENAME, "R", sep = ".")
ROUND.UTILS.SCRIPT.NAME <- 
  file.path(WORK.DIR, ROUND.UTILS.SCRIPT.BASENAME)
source(ROUND.UTILS.SCRIPT.NAME)



plot.manoeuvre.signals <- function(
      tgt.plot.filename,
      tgt.plot.id,
      time.instants,
      abp.signal,
      cbfv.signal,
      sampling.time,
      time.release,
      ...
    )
{  
  mfari <- get.mfARI.parameters(
    time.instants = time.instants,
    ABP.signal = abp.signal,
    CBFV.signal = cbfv.signal,
    sampling.time = sampling.time,
    time.release = time.release,
    keep.details = FALSE,
    ...
  )
  mfari[["mfARI"]] <- get.mfARI(
    Ks = mfari[["Ks"]],
    Delta.tau = mfari[["Delta.tau"]],
    Phi = mfari[["Phi"]]
  )
  
  p <- get.plots.mfARI.parameters(
    time.instants = time.instants,
    ABP.signal = abp.signal,
    CBFV.signal = cbfv.signal,
    parameter.results = mfari,
    id = tgt.plot.id,
    ...
  )
  
  ggsave(
    filename = tgt.plot.filename,
    plot = p,
    width = 11, 
    height = 8,
    units = "in"
  )
}


plot.subject.signals <- function(
      subject,
      src.subject.dir,
      src.ext = "txt",
      header = TRUE,
      tgt.subject.dir,
      tgt.suffix,
      tgt.ext = "txt",
      overwrite = FALSE,
      manoeuvres,
      time.col.name = "Time",
      abp.col.name = "ABP",
      left.cbfv.col.name = "LCBFV",
      right.cbfv.col.name = "RCBFV",
      sampling.time = 0.2,
      time.release = 0,
      left.plot.suffix = "Izq",
      right.plot.suffix = "Der",
      indent = "",
      ...
      )
{
  plot.created <- FALSE
  next.indent <- paste0(indent , "  ")
  
  for(mvre in manoeuvres)
  {
    cat(indent, "-- ", mvre, " --\n", sep = "")
    
    # Sets the subject source file
    src.basename <- paste(subject, mvre, sep = "-")
    src.basename <- paste(src.basename, src.ext, sep = ".")
    src.filename <- file.path(src.subject.dir, src.basename)
    
    if(!file.exists(src.filename))
    {
      cat(
        next.indent,
        "Warning: file ",
        src.filename,
        " does not exists\n",
        sep = ""
      )
      next
    }
    
    # Reads the manoeuvre signals
    mvre.data <- read.table(src.filename, header = header)
    
    # Sets plot titles
    mvre.id <- paste(subject, mvre, sep = ", ")
    
    #
    # Creates the plot for the left hemisphere
    #
    
    cat(next.indent, "-- Left hemisphere --\n", sep = "")
    
    # Sets target plot filename
    tgt.basename <-
      paste(subject, mvre, tgt.suffix, left.plot.suffix, sep = "-")
    tgt.basename <- paste(tgt.basename, tgt.ext, sep = ".")
    tgt.filename <- file.path(tgt.subject.dir, tgt.basename)
    
    # If the target plot does not exists or it should not be overwritten
    if(any(!file.exists(tgt.filename), overwrite))
    {
      # Makes sure the target directory exists
      dir.create(
        path = tgt.subject.dir,
        showWarnings = FALSE,
        recursive = TRUE,
        mode = "0711"
      )
      
      plot.manoeuvre.signals(
        tgt.plot.filename = tgt.filename,
        tgt.plot.id = paste(mvre.id, left.plot.suffix, sep = ", "),
        time.instants = mvre.data[[time.col.name]],
        abp.signal = mvre.data[[abp.col.name]],
        cbfv.signal = mvre.data[[left.cbfv.col.name]],
        sampling.time = sampling.time,
        time.release = time.release,
        ...
      )
      plot.created <- TRUE
    }
    else
      cat(
        next.indent,
        "Warning: target plot already exist and not overwritten\n",
        sep = ""
      )
    
    
    #
    # Creates the plot for the right hemisphere
    #
    
    cat(next.indent, "-- Right hemisphere --\n", sep = "")
    
    # Sets target plot filename
    tgt.basename <-
      paste(subject, mvre, tgt.suffix, right.plot.suffix, sep = "-")
    tgt.basename <- paste(tgt.basename, tgt.ext, sep = ".")
    tgt.filename <- file.path(tgt.subject.dir, tgt.basename)
    
    # If the target plot does not exists or it should not be overwritten
    if(any(!file.exists(tgt.filename), overwrite))
    {
      # Makes sure the target directory exists
      dir.create(
        path = tgt.subject.dir,
        showWarnings = FALSE,
        recursive = TRUE,
        mode = "0711"
      )
      
      plot.manoeuvre.signals(
        tgt.plot.filename = tgt.filename,
        tgt.plot.id = paste(mvre.id, right.plot.suffix, sep = ", "),
        time.instants = mvre.data[[time.col.name]],
        abp.signal = mvre.data[[abp.col.name]],
        cbfv.signal = mvre.data[[right.cbfv.col.name]],
        sampling.time = sampling.time,
        time.release = time.release,
        ...
      )
      plot.created <- TRUE
    }
    else
      cat(
        next.indent,
        "Warning: target plot already exist and not overwritten\n",
        sep = ""
      )
  }
  
  plot.created
}


run <- function(
      src.dir = file.path(WORK.DIR, "Data"),
      src.ext = "txt",
      header = TRUE,
      tgt.suffix = paste("mfARI", "plots", sep = "-"),
      tgt.dir = file.path(WORK.DIR, tgt.suffix),
      tgt.ext = "pdf",
      overwrite = FALSE,
      subjects = c("Sujeto1", "Sujeto2", "Sujeto3"),
      manoeuvres = c("maniobra1", "maniobra2", "maniobra3"),
      time.col.name = "Time",
      abp.col.name = "ABP",
      left.cbfv.col.name = "LCBFV",
      right.cbfv.col.name = "RCBFV",
      left.plot.suffix = "Izq",
      right.plot.suffix = "Der",
      sampling.time = 0.2,
      time.release = 0,
      baseline.initial.time = -5,
      baseline.final.time = time.release,
      min.ABP.max.delta.time = 5 * 0.8,
      min.CBFV.max.delta.time = 8 * 0.8,
      time.tol = sampling.time / 100,
      indent = ""
      )
{
  plot.created <- FALSE
  
  for(subject in subjects)
  {
    cat(indent, "-- ", subject, " --\n", sep = "")
    
    # Sets subject directories
    src.subject.dir <- file.path(src.dir, subject)
    tgt.subject.dir <- file.path(tgt.dir, subject)
    
    subject.plot.created <- plot.subject.signals(
      subject = subject,
      src.subject.dir = src.subject.dir,
      src.ext = src.ext,
      tgt.subject.dir = tgt.subject.dir,
      tgt.suffix = tgt.suffix,
      tgt.ext = tgt.ext,
      overwrite = overwrite,
      manoeuvres = manoeuvres,
      time.col.name = time.col.name,
      abp.col.name = abp.col.name,
      left.cbfv.col.name = left.cbfv.col.name,
      right.cbfv.col.name = right.cbfv.col.name,
      left.plot.suffix = left.plot.suffix,
      right.plot.suffix = right.plot.suffix,
      indent = paste0(indent, "  "),
      header = header,
      sampling.time = sampling.time,
      time.release = time.release,
      baseline.initial.time = baseline.initial.time,
      baseline.final.time = baseline.final.time,
      min.ABP.max.delta.time = min.ABP.max.delta.time,
      min.CBFV.max.delta.time = min.CBFV.max.delta.time,
      time.tol = time.tol
    )
    plot.created <- any(plot.created, subject.plot.created)
  }
  
  plot.created
}
<file_sep><<<<<<< HEAD
<<<<<<< HEAD
import pandas
import numpy
import scipy.stats
import seaborn
import matplotlib.pyplot as plt
data = pandas.read_csv(‘addhealth_pds.csv’, low_memory=False)
data['BIO_SEX’]=data['BIO_SEX’].replace(0, numpy.nan)
data['H1GH28’]=data['H1GH28’].replace(0, numpy.nan)
data['BIO_SEX’] = pandas.to_numeric(data['BIO_SEX’], errors='coerce’)
data['H1GH28’] = pandas.to_numeric(data['H1GH28’], errors='coerce’)
#There is a six within the data
data = data[(data['BIO_SEX’] == 1) | (data['BIO_SEX’] == 2)]
ct=pandas.crosstab(data['BIO_SEX’], data['H1GH28’])
print (ct)
# column percentages
colsum=ct.sum(axis=0)
colpct=ct/colsum
print(colpct)
# chi-square
print ('chi-square value, p value, expected counts’)
cs= scipy.stats.chi2_contingency(ct)
print (cs)
# set variable types 
data[“H1GH28”] = data[“H1GH28”].astype('category’)
# new code for setting variables to numeric:
data['BIO_SEX’] = pandas.to_numeric(data['BIO_SEX’], errors='coerce’)
# graph percent with nicotine dependence within each smoking frequency group 
seaborn.factorplot(x=“H1GH28”, y=“BIO_SEX”, data=data, kind=“bar”, ci=None)
plt.xlabel('Thougts of Weight’)
plt.ylabel('Bio Sex’)


###################Post Hoc Analysis
#make a copy of my new subsetted data
sub = data.copy()
for a in [1, 2, 3, 4, 5, 6]:
   for b in range((a + 1),9):
       if b != 7:
           print (’———————–’)
           recode1 = {a: a, b: b}
           sub['COMP1v2’]= data['H1GH28’].map(recode1)
           # contingency table of observed counts
           ct1=pandas.crosstab(sub['BIO_SEX’], sub['COMP1v2’])
           cs1= scipy.stats.chi2_contingency(ct1)
           if (cs1[0] > 3.84) & (cs1[1] < 0.003) :
               print ('chi-square value %f’%cs1[0])
               print ('p value %.2E’%cs1[1])
               print (ct1)
               # column percentages
               colsum=ct1.sum(axis=0)
               colpct=ct1/colsum
               print(colpct)
=======
import pandas
import numpy
import scipy.stats
import seaborn
import matplotlib.pyplot as plt
data = pandas.read_csv(‘addhealth_pds.csv’, low_memory=False)
data['BIO_SEX’]=data['BIO_SEX’].replace(0, numpy.nan)
data['H1GH28’]=data['H1GH28’].replace(0, numpy.nan)
data['BIO_SEX’] = pandas.to_numeric(data['BIO_SEX’], errors='coerce’)
data['H1GH28’] = pandas.to_numeric(data['H1GH28’], errors='coerce’)
#There is a six within the data
data = data[(data['BIO_SEX’] == 1) | (data['BIO_SEX’] == 2)]
ct=pandas.crosstab(data['BIO_SEX’], data['H1GH28’])
print (ct)
# column percentages
colsum=ct.sum(axis=0)
colpct=ct/colsum
print(colpct)
# chi-square
print ('chi-square value, p value, expected counts’)
cs= scipy.stats.chi2_contingency(ct)
print (cs)
# set variable types 
data[“H1GH28”] = data[“H1GH28”].astype('category’)
# new code for setting variables to numeric:
data['BIO_SEX’] = pandas.to_numeric(data['BIO_SEX’], errors='coerce’)
# graph percent with nicotine dependence within each smoking frequency group 
seaborn.factorplot(x=“H1GH28”, y=“BIO_SEX”, data=data, kind=“bar”, ci=None)
plt.xlabel('Thougts of Weight’)
plt.ylabel('Bio Sex’)


###################Post Hoc Analysis
#make a copy of my new subsetted data
sub = data.copy()
for a in [1, 2, 3, 4, 5, 6]:
   for b in range((a + 1),9):
       if b != 7:
           print (’———————–’)
           recode1 = {a: a, b: b}
           sub['COMP1v2’]= data['H1GH28’].map(recode1)
           # contingency table of observed counts
           ct1=pandas.crosstab(sub['BIO_SEX’], sub['COMP1v2’])
           cs1= scipy.stats.chi2_contingency(ct1)
           if (cs1[0] > 3.84) & (cs1[1] < 0.003) :
               print ('chi-square value %f’%cs1[0])
               print ('p value %.2E’%cs1[1])
               print (ct1)
               # column percentages
               colsum=ct1.sum(axis=0)
               colpct=ct1/colsum
               print(colpct)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
import pandas
import numpy
import scipy.stats
import seaborn
import matplotlib.pyplot as plt
data = pandas.read_csv(‘addhealth_pds.csv’, low_memory=False)
data['BIO_SEX’]=data['BIO_SEX’].replace(0, numpy.nan)
data['H1GH28’]=data['H1GH28’].replace(0, numpy.nan)
data['BIO_SEX’] = pandas.to_numeric(data['BIO_SEX’], errors='coerce’)
data['H1GH28’] = pandas.to_numeric(data['H1GH28’], errors='coerce’)
#There is a six within the data
data = data[(data['BIO_SEX’] == 1) | (data['BIO_SEX’] == 2)]
ct=pandas.crosstab(data['BIO_SEX’], data['H1GH28’])
print (ct)
# column percentages
colsum=ct.sum(axis=0)
colpct=ct/colsum
print(colpct)
# chi-square
print ('chi-square value, p value, expected counts’)
cs= scipy.stats.chi2_contingency(ct)
print (cs)
# set variable types 
data[“H1GH28”] = data[“H1GH28”].astype('category’)
# new code for setting variables to numeric:
data['BIO_SEX’] = pandas.to_numeric(data['BIO_SEX’], errors='coerce’)
# graph percent with nicotine dependence within each smoking frequency group 
seaborn.factorplot(x=“H1GH28”, y=“BIO_SEX”, data=data, kind=“bar”, ci=None)
plt.xlabel('Thougts of Weight’)
plt.ylabel('Bio Sex’)


###################Post Hoc Analysis
#make a copy of my new subsetted data
sub = data.copy()
for a in [1, 2, 3, 4, 5, 6]:
   for b in range((a + 1),9):
       if b != 7:
           print (’———————–’)
           recode1 = {a: a, b: b}
           sub['COMP1v2’]= data['H1GH28’].map(recode1)
           # contingency table of observed counts
           ct1=pandas.crosstab(sub['BIO_SEX’], sub['COMP1v2’])
           cs1= scipy.stats.chi2_contingency(ct1)
           if (cs1[0] > 3.84) & (cs1[1] < 0.003) :
               print ('chi-square value %f’%cs1[0])
               print ('p value %.2E’%cs1[1])
               print (ct1)
               # column percentages
               colsum=ct1.sum(axis=0)
               colpct=ct1/colsum
               print(colpct)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep><<<<<<< HEAD
<<<<<<< HEAD
#Ejemplo de entrenmiento:
#https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/

#Elegir cantidad de hidden layesr:
#https://wiki.inf.ed.ac.uk/twiki/pub/CSTR/ListenTerm1201415/sak2.pdf

#Mejores parametros:
#https://arxiv.org/pdf/1709.05206.pdf
##Tuning the Number of Epochs
##Tuning the Batch Size
##Tuning the Number of Neurons
##Tuning the Number of Hidden Layers

from __future__ import print_function
import pandas as pd
import numpy
import matplotlib.pyplot as plt
import math
from keras.models import load_model, Model, Sequential
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.initializers import glorot_uniform
from keras.utils import to_categorical
from keras.optimizers import Adam
from keras import backend as K
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from pandas import DataFrame
from pandas.plotting import table

class Entrenaiento_1:
    # evaluate the model on a dataset, returns RMSE in transformed units
    def evaluate(model, X, Y, batch_size):
        
    	output = model.predict(X, batch_size=batch_size)
    	# invert data transforms on forecast
    	yhat = list()
    	for i in range(len(output)):
    		# store forecast
    		yhat.append(output[i,0])
    	# report performance
    	rmse = math.sqrt(mean_squared_error(Y[:,0], yhat))
    	return rmse
    
    # fit an LSTM network to training data
    def fit_lstm(trainX, trainY, testX, testY, n_batch, n_epochs, n_neurons):
    	# prepare model
    	model = Sequential()
    	model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True, return_sequences=True))
    	model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True))
    	model.add(Dense(1))
    	model.compile(loss='mean_squared_error', optimizer='adam')
    	# fit model
    	train_rmse, test_rmse = list(), list()
    	for i in range(n_epochs):
    		model.fit(trainX, trainY, epochs=1, batch_size=n_batch, verbose=0, shuffle=False)
    		model.reset_states()
    		# evaluate model on train data
    		train_rmse.append(Entrenaiento_1.evaluate(model, trainX, trainY, n_batch))
    		model.reset_states()
    		# evaluate model on test data
    		test_rmse.append(Entrenaiento_1.evaluate(model, testX, testY, n_batch))
    		model.reset_states()
    	history = DataFrame()
    	history['train'], history['test'] = train_rmse, test_rmse
    	return history
    
    def run(trainX, trainY, testX, testY):
        repeats = 10
        n_batch = 2 # (n_batch%trainX.shape[0]) == 0
        n_epochs = 50
        n_neurons = 10
        # run diagnostic tests
        for i in range(repeats):
        	history = Entrenaiento_1.fit_lstm(trainX, trainY, testX, testY, n_batch, n_epochs, n_neurons)
        	plt.plot(history['train'], color='blue')
        	plt.plot(history['test'], color='orange')
        	print('%d) TrainRMSE=%f, TestRMSE=%f' % (i, history['train'].iloc[-1], history['test'].iloc[-1]))
        plt.savefig('epochs_diagnostic.png')

class Entrenaiento_2:
    # fit an LSTM network to training data
    def fit_lstm(trainX, trainY, n_batch, n_epochs, n_neurons):
        
        model = Sequential()
        model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True, return_sequences=True))
        model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True))
        model.add(Dense(1))
        model.compile(loss='mean_squared_error', optimizer='adam')
        for i in range(n_epochs):
            model.fit(trainX, trainY, epochs=1, batch_size=n_batch, verbose=0, shuffle=False)
            model.reset_states()
        return model
    
    #Evaluate the Model
    def evaluate(model, X, Y, batch_size):
        
    	output = model.predict(X, batch_size=batch_size)
    	# invert data transforms on forecast
    	yhat = list()
    	for i in range(len(output)):
    		# store forecast
    		yhat.append(output[i,0])
    	# report performance
    	rmse = math.sqrt(mean_squared_error(Y[:,0], yhat))
    	return rmse
    
    # Guardar en formato PNG los resultados
    def save_results(results, nombre_archivo_resultados):
        
        desc = results.describe()
        #create a subplot without frame
        plot = plt.subplot(111, frame_on=False)
        
        #remove axis
        plot.xaxis.set_visible(False) 
        plot.yaxis.set_visible(False) 
        
        #create the table plot and position it in the upper left corner
        table(plot, desc,loc='upper right')

        #save the plot as a png file
        plt.savefig('desc_%s.png'%(nombre_archivo_resultados))
        plt.close()
        
        results.boxplot()
        plt.savefig('boxplot_%s.png'%(nombre_archivo_resultados))
        plt.close()
    
        # run a repeated experiment
    def experiment(trainX, trainY, testX, testY, repeats, epochs, batch_size, n_neurons):
    	# run experiment
    	error_scores = list()
    	for r in range(repeats):
    		# fit the model
    		lstm_model = Entrenaiento_2.fit_lstm(trainX, trainY, batch_size, epochs, n_neurons)

    		lstm_model.predict(trainX, batch_size=batch_size)
    	 
    		# report performance
    		rmse = Entrenaiento_2.evaluate(lstm_model, testX, testY, batch_size)
    		print('%d) Test RMSE: %.3f' % (r+1, rmse))
    		error_scores.append(rmse)
    	return error_scores
    
    def run_epochs(trainX, trainY, testX, testY, nombre_archivo_resultados):
        nombre_experimento = "_epochs_"
        results = DataFrame()
        repeats = 2
        # experiment
        # vary training epochs
        epochs = [2, 3, 4]
        batch_size = 1
        n_neurons = 1

        for e in epochs:
        	results[str(e)] = Entrenaiento_2.experiment(trainX, trainY, testX, testY, repeats, e, batch_size, n_neurons)
        # summarize results
        print(results.describe())
        # save boxplot
        Entrenaiento_2.save_results(results, nombre_archivo_resultados+nombre_experimento)
        
###########################################################################################################    
nombre_sujeto = "AC"
nombre_postura = "ACOSTADO"
nombre_emisferio = "DERECHO"


PATH = ("C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/%s/%s-%s-VE.csv"%(nombre_sujeto, nombre_sujeto, nombre_postura))
X = pd.read_csv(PATH, sep="	")

# normalize the dataset
scaler = MinMaxScaler(feature_range=(-1, 1))

VFSCd = scaler.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
VFSCi = scaler.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
PAMn = scaler.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
# fix random seed for reproducibility
numpy.random.seed(7)

#Dar formato float a las señales
PAMn, VFSCd = PAMn.astype('float32'), VFSCd.astype('float32')
PAMn, VFSCd = numpy.array(PAMn), numpy.array(VFSCd)

# Validacion Valanceada
train_size = int(len(PAMn) * 0.5)
test_size = len(PAMn) - train_size
train_PAM, train_VFSCd = PAMn[0:train_size], VFSCd[0:train_size]
test_PAM, test_VFSCd = PAMn[train_size:len(PAMn)], VFSCd[train_size:len(VFSCd)]

# Reshape segun el formato que acepta Keras
# reshape input to be [samples, time steps, features]
train_PAM = numpy.reshape(train_PAM, (train_PAM.shape[0], 1, train_PAM.shape[1]))
test_PAM = numpy.reshape(test_PAM, (test_PAM.shape[0], 1, test_PAM.shape[1]))
# create and fit the LSTM network
##############################################################################################################################################################
nombre_archivo_resultados = nombre_sujeto+"_"+nombre_postura+"_"+nombre_emisferio
Entrenaiento_2.run_epochs(train_PAM, train_VFSCd, test_PAM, test_VFSCd, nombre_archivo_resultados+"_MODELO1")
Entrenaiento_2.run_epochs(test_PAM, test_VFSCd, train_PAM, train_VFSCd, nombre_archivo_resultados+"_MODELO2")
##############################################################################################################################################################
'''
model = Sequential()
model.add(LSTM(7, input_shape=(1, 1)))
model.add(Dense(1))#activation='sigmoid'
model.compile(loss='mse', optimizer='adam')
history = model.fit(trainX, trainY, validation_data=(testX, testY), epochs=800, batch_size=50, verbose=0, shuffle=False)
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform(trainY)
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform(testY)
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[:,0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[:,0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(trainPredict)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(testPredict)
testPredictPlot[:, :] = numpy.nan
testPredictPlot = testPredict
#plot loss function
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
'''
# plot baseline and predictions
'''
plt.plot(scaler.inverse_transform(VFSCd))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()
'''
'''
import plotly.plotly as py
import plotly.graph_objs as go
import plotly
plotly.tools.set_credentials_file(username='luis.orellana777', api_key='<KEY>')


trace_high = go.Scatter(
                x=list(range(1, len(VFSCd))),
                y=scaler.inverse_transform(VFSCd),
                name = "VFSCd",
                line = dict(color = '#17BECF'),
                opacity = 0.8)

trace_low = go.Scatter(
                x=list(range(1, len(VFSCd))),
                y=numpy.concatenate((trainPredictPlot, testPredictPlot)),
                name = "VFSCd Predicted",
                line = dict(color = '#7F7F7F'),
                opacity = 0.8)

data = [trace_high,trace_low]

layout = dict(
    title = "LSTM VFSCd",
    xaxis = dict(
        range = [1, len(VFSCd)])
)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename = "LSTM VFSCd")
=======
#Ejemplo de entrenmiento:
#https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/

#Elegir cantidad de hidden layesr:
#https://wiki.inf.ed.ac.uk/twiki/pub/CSTR/ListenTerm1201415/sak2.pdf

#Mejores parametros:
#https://arxiv.org/pdf/1709.05206.pdf
##Tuning the Number of Epochs
##Tuning the Batch Size
##Tuning the Number of Neurons
##Tuning the Number of Hidden Layers

from __future__ import print_function
import pandas as pd
import numpy
import matplotlib.pyplot as plt
import math
from keras.models import load_model, Model, Sequential
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.initializers import glorot_uniform
from keras.utils import to_categorical
from keras.optimizers import Adam
from keras import backend as K
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from pandas import DataFrame
from pandas.plotting import table

class Entrenaiento_1:
    # evaluate the model on a dataset, returns RMSE in transformed units
    def evaluate(model, X, Y, batch_size):
        
    	output = model.predict(X, batch_size=batch_size)
    	# invert data transforms on forecast
    	yhat = list()
    	for i in range(len(output)):
    		# store forecast
    		yhat.append(output[i,0])
    	# report performance
    	rmse = math.sqrt(mean_squared_error(Y[:,0], yhat))
    	return rmse
    
    # fit an LSTM network to training data
    def fit_lstm(trainX, trainY, testX, testY, n_batch, n_epochs, n_neurons):
    	# prepare model
    	model = Sequential()
    	model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True, return_sequences=True))
    	model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True))
    	model.add(Dense(1))
    	model.compile(loss='mean_squared_error', optimizer='adam')
    	# fit model
    	train_rmse, test_rmse = list(), list()
    	for i in range(n_epochs):
    		model.fit(trainX, trainY, epochs=1, batch_size=n_batch, verbose=0, shuffle=False)
    		model.reset_states()
    		# evaluate model on train data
    		train_rmse.append(Entrenaiento_1.evaluate(model, trainX, trainY, n_batch))
    		model.reset_states()
    		# evaluate model on test data
    		test_rmse.append(Entrenaiento_1.evaluate(model, testX, testY, n_batch))
    		model.reset_states()
    	history = DataFrame()
    	history['train'], history['test'] = train_rmse, test_rmse
    	return history
    
    def run(trainX, trainY, testX, testY):
        repeats = 10
        n_batch = 2 # (n_batch%trainX.shape[0]) == 0
        n_epochs = 50
        n_neurons = 10
        # run diagnostic tests
        for i in range(repeats):
        	history = Entrenaiento_1.fit_lstm(trainX, trainY, testX, testY, n_batch, n_epochs, n_neurons)
        	plt.plot(history['train'], color='blue')
        	plt.plot(history['test'], color='orange')
        	print('%d) TrainRMSE=%f, TestRMSE=%f' % (i, history['train'].iloc[-1], history['test'].iloc[-1]))
        plt.savefig('epochs_diagnostic.png')

class Entrenaiento_2:
    # fit an LSTM network to training data
    def fit_lstm(trainX, trainY, n_batch, n_epochs, n_neurons):
        
        model = Sequential()
        model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True, return_sequences=True))
        model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True))
        model.add(Dense(1))
        model.compile(loss='mean_squared_error', optimizer='adam')
        for i in range(n_epochs):
            model.fit(trainX, trainY, epochs=1, batch_size=n_batch, verbose=0, shuffle=False)
            model.reset_states()
        return model
    
    #Evaluate the Model
    def evaluate(model, X, Y, batch_size):
        
    	output = model.predict(X, batch_size=batch_size)
    	# invert data transforms on forecast
    	yhat = list()
    	for i in range(len(output)):
    		# store forecast
    		yhat.append(output[i,0])
    	# report performance
    	rmse = math.sqrt(mean_squared_error(Y[:,0], yhat))
    	return rmse
    
    # Guardar en formato PNG los resultados
    def save_results(results, nombre_archivo_resultados):
        
        desc = results.describe()
        #create a subplot without frame
        plot = plt.subplot(111, frame_on=False)
        
        #remove axis
        plot.xaxis.set_visible(False) 
        plot.yaxis.set_visible(False) 
        
        #create the table plot and position it in the upper left corner
        table(plot, desc,loc='upper right')

        #save the plot as a png file
        plt.savefig('desc_%s.png'%(nombre_archivo_resultados))
        plt.close()
        
        results.boxplot()
        plt.savefig('boxplot_%s.png'%(nombre_archivo_resultados))
        plt.close()
    
        # run a repeated experiment
    def experiment(trainX, trainY, testX, testY, repeats, epochs, batch_size, n_neurons):
    	# run experiment
    	error_scores = list()
    	for r in range(repeats):
    		# fit the model
    		lstm_model = Entrenaiento_2.fit_lstm(trainX, trainY, batch_size, epochs, n_neurons)

    		lstm_model.predict(trainX, batch_size=batch_size)
    	 
    		# report performance
    		rmse = Entrenaiento_2.evaluate(lstm_model, testX, testY, batch_size)
    		print('%d) Test RMSE: %.3f' % (r+1, rmse))
    		error_scores.append(rmse)
    	return error_scores
    
    def run_epochs(trainX, trainY, testX, testY, nombre_archivo_resultados):
        nombre_experimento = "_epochs_"
        results = DataFrame()
        repeats = 2
        # experiment
        # vary training epochs
        epochs = [2, 3, 4]
        batch_size = 1
        n_neurons = 1

        for e in epochs:
        	results[str(e)] = Entrenaiento_2.experiment(trainX, trainY, testX, testY, repeats, e, batch_size, n_neurons)
        # summarize results
        print(results.describe())
        # save boxplot
        Entrenaiento_2.save_results(results, nombre_archivo_resultados+nombre_experimento)
        
###########################################################################################################    
nombre_sujeto = "AC"
nombre_postura = "ACOSTADO"
nombre_emisferio = "DERECHO"


PATH = ("C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/%s/%s-%s-VE.csv"%(nombre_sujeto, nombre_sujeto, nombre_postura))
X = pd.read_csv(PATH, sep="	")

# normalize the dataset
scaler = MinMaxScaler(feature_range=(-1, 1))

VFSCd = scaler.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
VFSCi = scaler.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
PAMn = scaler.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
# fix random seed for reproducibility
numpy.random.seed(7)

#Dar formato float a las señales
PAMn, VFSCd = PAMn.astype('float32'), VFSCd.astype('float32')
PAMn, VFSCd = numpy.array(PAMn), numpy.array(VFSCd)

# Validacion Valanceada
train_size = int(len(PAMn) * 0.5)
test_size = len(PAMn) - train_size
train_PAM, train_VFSCd = PAMn[0:train_size], VFSCd[0:train_size]
test_PAM, test_VFSCd = PAMn[train_size:len(PAMn)], VFSCd[train_size:len(VFSCd)]

# Reshape segun el formato que acepta Keras
# reshape input to be [samples, time steps, features]
train_PAM = numpy.reshape(train_PAM, (train_PAM.shape[0], 1, train_PAM.shape[1]))
test_PAM = numpy.reshape(test_PAM, (test_PAM.shape[0], 1, test_PAM.shape[1]))
# create and fit the LSTM network
##############################################################################################################################################################
nombre_archivo_resultados = nombre_sujeto+"_"+nombre_postura+"_"+nombre_emisferio
Entrenaiento_2.run_epochs(train_PAM, train_VFSCd, test_PAM, test_VFSCd, nombre_archivo_resultados+"_MODELO1")
Entrenaiento_2.run_epochs(test_PAM, test_VFSCd, train_PAM, train_VFSCd, nombre_archivo_resultados+"_MODELO2")
##############################################################################################################################################################
'''
model = Sequential()
model.add(LSTM(7, input_shape=(1, 1)))
model.add(Dense(1))#activation='sigmoid'
model.compile(loss='mse', optimizer='adam')
history = model.fit(trainX, trainY, validation_data=(testX, testY), epochs=800, batch_size=50, verbose=0, shuffle=False)
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform(trainY)
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform(testY)
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[:,0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[:,0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(trainPredict)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(testPredict)
testPredictPlot[:, :] = numpy.nan
testPredictPlot = testPredict
#plot loss function
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
'''
# plot baseline and predictions
'''
plt.plot(scaler.inverse_transform(VFSCd))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()
'''
'''
import plotly.plotly as py
import plotly.graph_objs as go
import plotly
plotly.tools.set_credentials_file(username='luis.orellana777', api_key='<KEY>')


trace_high = go.Scatter(
                x=list(range(1, len(VFSCd))),
                y=scaler.inverse_transform(VFSCd),
                name = "VFSCd",
                line = dict(color = '#17BECF'),
                opacity = 0.8)

trace_low = go.Scatter(
                x=list(range(1, len(VFSCd))),
                y=numpy.concatenate((trainPredictPlot, testPredictPlot)),
                name = "VFSCd Predicted",
                line = dict(color = '#7F7F7F'),
                opacity = 0.8)

data = [trace_high,trace_low]

layout = dict(
    title = "LSTM VFSCd",
    xaxis = dict(
        range = [1, len(VFSCd)])
)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename = "LSTM VFSCd")
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
#Ejemplo de entrenmiento:
#https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/

#Elegir cantidad de hidden layesr:
#https://wiki.inf.ed.ac.uk/twiki/pub/CSTR/ListenTerm1201415/sak2.pdf

#Mejores parametros:
#https://arxiv.org/pdf/1709.05206.pdf
##Tuning the Number of Epochs
##Tuning the Batch Size
##Tuning the Number of Neurons
##Tuning the Number of Hidden Layers

from __future__ import print_function
import pandas as pd
import numpy
import matplotlib.pyplot as plt
import math
from keras.models import load_model, Model, Sequential
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.initializers import glorot_uniform
from keras.utils import to_categorical
from keras.optimizers import Adam
from keras import backend as K
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from pandas import DataFrame
from pandas.plotting import table

class Entrenaiento_1:
    # evaluate the model on a dataset, returns RMSE in transformed units
    def evaluate(model, X, Y, batch_size):
        
    	output = model.predict(X, batch_size=batch_size)
    	# invert data transforms on forecast
    	yhat = list()
    	for i in range(len(output)):
    		# store forecast
    		yhat.append(output[i,0])
    	# report performance
    	rmse = math.sqrt(mean_squared_error(Y[:,0], yhat))
    	return rmse
    
    # fit an LSTM network to training data
    def fit_lstm(trainX, trainY, testX, testY, n_batch, n_epochs, n_neurons):
    	# prepare model
    	model = Sequential()
    	model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True, return_sequences=True))
    	model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True))
    	model.add(Dense(1))
    	model.compile(loss='mean_squared_error', optimizer='adam')
    	# fit model
    	train_rmse, test_rmse = list(), list()
    	for i in range(n_epochs):
    		model.fit(trainX, trainY, epochs=1, batch_size=n_batch, verbose=0, shuffle=False)
    		model.reset_states()
    		# evaluate model on train data
    		train_rmse.append(Entrenaiento_1.evaluate(model, trainX, trainY, n_batch))
    		model.reset_states()
    		# evaluate model on test data
    		test_rmse.append(Entrenaiento_1.evaluate(model, testX, testY, n_batch))
    		model.reset_states()
    	history = DataFrame()
    	history['train'], history['test'] = train_rmse, test_rmse
    	return history
    
    def run(trainX, trainY, testX, testY):
        repeats = 10
        n_batch = 2 # (n_batch%trainX.shape[0]) == 0
        n_epochs = 50
        n_neurons = 10
        # run diagnostic tests
        for i in range(repeats):
        	history = Entrenaiento_1.fit_lstm(trainX, trainY, testX, testY, n_batch, n_epochs, n_neurons)
        	plt.plot(history['train'], color='blue')
        	plt.plot(history['test'], color='orange')
        	print('%d) TrainRMSE=%f, TestRMSE=%f' % (i, history['train'].iloc[-1], history['test'].iloc[-1]))
        plt.savefig('epochs_diagnostic.png')

class Entrenaiento_2:
    # fit an LSTM network to training data
    def fit_lstm(trainX, trainY, n_batch, n_epochs, n_neurons):
        
        model = Sequential()
        model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True, return_sequences=True))
        model.add(LSTM(n_neurons, batch_input_shape=(n_batch, trainX.shape[1], trainX.shape[2]), stateful=True))
        model.add(Dense(1))
        model.compile(loss='mean_squared_error', optimizer='adam')
        for i in range(n_epochs):
            model.fit(trainX, trainY, epochs=1, batch_size=n_batch, verbose=0, shuffle=False)
            model.reset_states()
        return model
    
    #Evaluate the Model
    def evaluate(model, X, Y, batch_size):
        
    	output = model.predict(X, batch_size=batch_size)
    	# invert data transforms on forecast
    	yhat = list()
    	for i in range(len(output)):
    		# store forecast
    		yhat.append(output[i,0])
    	# report performance
    	rmse = math.sqrt(mean_squared_error(Y[:,0], yhat))
    	return rmse
    
    # Guardar en formato PNG los resultados
    def save_results(results, nombre_archivo_resultados):
        
        desc = results.describe()
        #create a subplot without frame
        plot = plt.subplot(111, frame_on=False)
        
        #remove axis
        plot.xaxis.set_visible(False) 
        plot.yaxis.set_visible(False) 
        
        #create the table plot and position it in the upper left corner
        table(plot, desc,loc='upper right')

        #save the plot as a png file
        plt.savefig('desc_%s.png'%(nombre_archivo_resultados))
        plt.close()
        
        results.boxplot()
        plt.savefig('boxplot_%s.png'%(nombre_archivo_resultados))
        plt.close()
    
        # run a repeated experiment
    def experiment(trainX, trainY, testX, testY, repeats, epochs, batch_size, n_neurons):
    	# run experiment
    	error_scores = list()
    	for r in range(repeats):
    		# fit the model
    		lstm_model = Entrenaiento_2.fit_lstm(trainX, trainY, batch_size, epochs, n_neurons)

    		lstm_model.predict(trainX, batch_size=batch_size)
    	 
    		# report performance
    		rmse = Entrenaiento_2.evaluate(lstm_model, testX, testY, batch_size)
    		print('%d) Test RMSE: %.3f' % (r+1, rmse))
    		error_scores.append(rmse)
    	return error_scores
    
    def run_epochs(trainX, trainY, testX, testY, nombre_archivo_resultados):
        nombre_experimento = "_epochs_"
        results = DataFrame()
        repeats = 2
        # experiment
        # vary training epochs
        epochs = [2, 3, 4]
        batch_size = 1
        n_neurons = 1

        for e in epochs:
        	results[str(e)] = Entrenaiento_2.experiment(trainX, trainY, testX, testY, repeats, e, batch_size, n_neurons)
        # summarize results
        print(results.describe())
        # save boxplot
        Entrenaiento_2.save_results(results, nombre_archivo_resultados+nombre_experimento)
        
###########################################################################################################    
nombre_sujeto = "AC"
nombre_postura = "ACOSTADO"
nombre_emisferio = "DERECHO"


PATH = ("C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/%s/%s-%s-VE.csv"%(nombre_sujeto, nombre_sujeto, nombre_postura))
X = pd.read_csv(PATH, sep="	")

# normalize the dataset
scaler = MinMaxScaler(feature_range=(-1, 1))

VFSCd = scaler.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
VFSCi = scaler.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
PAMn = scaler.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
# fix random seed for reproducibility
numpy.random.seed(7)

#Dar formato float a las señales
PAMn, VFSCd = PAMn.astype('float32'), VFSCd.astype('float32')
PAMn, VFSCd = numpy.array(PAMn), numpy.array(VFSCd)

# Validacion Valanceada
train_size = int(len(PAMn) * 0.5)
test_size = len(PAMn) - train_size
train_PAM, train_VFSCd = PAMn[0:train_size], VFSCd[0:train_size]
test_PAM, test_VFSCd = PAMn[train_size:len(PAMn)], VFSCd[train_size:len(VFSCd)]

# Reshape segun el formato que acepta Keras
# reshape input to be [samples, time steps, features]
train_PAM = numpy.reshape(train_PAM, (train_PAM.shape[0], 1, train_PAM.shape[1]))
test_PAM = numpy.reshape(test_PAM, (test_PAM.shape[0], 1, test_PAM.shape[1]))
# create and fit the LSTM network
##############################################################################################################################################################
nombre_archivo_resultados = nombre_sujeto+"_"+nombre_postura+"_"+nombre_emisferio
Entrenaiento_2.run_epochs(train_PAM, train_VFSCd, test_PAM, test_VFSCd, nombre_archivo_resultados+"_MODELO1")
Entrenaiento_2.run_epochs(test_PAM, test_VFSCd, train_PAM, train_VFSCd, nombre_archivo_resultados+"_MODELO2")
##############################################################################################################################################################
'''
model = Sequential()
model.add(LSTM(7, input_shape=(1, 1)))
model.add(Dense(1))#activation='sigmoid'
model.compile(loss='mse', optimizer='adam')
history = model.fit(trainX, trainY, validation_data=(testX, testY), epochs=800, batch_size=50, verbose=0, shuffle=False)
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform(trainY)
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform(testY)
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[:,0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[:,0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(trainPredict)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(testPredict)
testPredictPlot[:, :] = numpy.nan
testPredictPlot = testPredict
#plot loss function
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
'''
# plot baseline and predictions
'''
plt.plot(scaler.inverse_transform(VFSCd))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()
'''
'''
import plotly.plotly as py
import plotly.graph_objs as go
import plotly
plotly.tools.set_credentials_file(username='luis.orellana777', api_key='<KEY>')


trace_high = go.Scatter(
                x=list(range(1, len(VFSCd))),
                y=scaler.inverse_transform(VFSCd),
                name = "VFSCd",
                line = dict(color = '#17BECF'),
                opacity = 0.8)

trace_low = go.Scatter(
                x=list(range(1, len(VFSCd))),
                y=numpy.concatenate((trainPredictPlot, testPredictPlot)),
                name = "VFSCd Predicted",
                line = dict(color = '#7F7F7F'),
                opacity = 0.8)

data = [trace_high,trace_low]

layout = dict(
    title = "LSTM VFSCd",
    xaxis = dict(
        range = [1, len(VFSCd)])
)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename = "LSTM VFSCd")
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
'''<file_sep>import azureml
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from azureml.core import Run, Workspace
from azureml.core import Experiment

# load workspace configuration from the config.json file in the current folder.
ws = Workspace.from_config()

def load_workspace():
    return ws

def load_experiment():
    # create a new experiment
    experiment_name = 'MNIST_Logistic_Regression'
    
    exp = Experiment(workspace=ws, name=experiment_name)

    return exp<file_sep><<<<<<< HEAD
<<<<<<< HEAD
# -*- coding: utf-8 -*-
"""
Editor de Spyder

Este es un archivo temporal

"""

import numpy as np
import pandas as pd
from scipy import stats
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn.metrics import silhouette_score
from sklearn import preprocessing

X = pd.read_csv(r"C:/Users/Luis.O.A/Documents/Analisis Clientes/Dataset2.csv", sep=";")

min_max_scaler = preprocessing.MinMaxScaler()
df_tr = min_max_scaler.fit_transform(X)
df_tr

#Verificar Cantidad de Clusters
Nc = range(2, 15)
kmeans = [KMeans(n_clusters=i) for i in Nc]
score = [kmeans[i].fit(df_tr).score(df_tr) for i in range(len(kmeans))]

plt.plot(Nc,score)
plt.xlabel('Numero de Clusters')
plt.ylabel('Score')
plt.title('Curva del Codo')
plt.show()



silhouette_score_values = [silhouette_score(df_tr,kmeans[i].labels_ ,metric='euclidean', sample_size=None, random_state=None) for i in range(len(kmeans))]

plt.plot(Nc, silhouette_score_values)
plt.title("Silhouette score values vs Numbers of Clusters ")
plt.show()


kmeans = KMeans(n_clusters=3, random_state=0).fit(df_tr)
labels = kmeans.labels_

#Glue back to originaal data
X['clusters'] = labels

clmns = ['TIPO_EMPRESA',
         'FAMILIA',
         'CANTIDAD_EMPLEADOS',
         'CARTERA',
         'REGION',
         'ESTADO_SERVICIOS',
         'Q_ACUERDOS',
         'COMISION',
         'DEUDA',
         'Q_MESES_COMPRA',
         'MES_PRIMERA_COMPRA',
         'MES_ULTIMA_COMPRA',
         'DIAS_CREDITO',
         'CONDICION_ENTREGA',
         'PUNTOS',
         'PORCENTAJE_NO_PERSONALIZADO',
         'MESES_CON_PEDIDOS',
         'VF_PROMEDIO',
         '%_COBERTURA_SUPERMERCADOS',
         '%_COBERTURA_RESTAURANT',
         '%_COBERTURA_BV',
         '%_COBERTURA_MERCHANTS',
         '$_COBERTURA_SUPERMERCADOS']
#Add the column into our list
clmns.extend(['clusters'])

print (X[clmns].groupby(['clusters']).mean())

C = kmeans.cluster_centers_
colores=['red','green','blue','cyan','yellow']
asignar=[]
for row in labels:
    asignar.append(colores[row])

pca = PCA(n_components=2, whiten=True).fit(df_tr)
X_pca = pca.transform(df_tr)

pca_C = PCA(n_components=2, whiten=True).fit(C)
X_pca_C = pca_C.transform(C)

plt.scatter(X_pca[:, 0], X_pca[:, 1], c=asignar, s=5, cmap='viridis')

plt.scatter(X_pca_C[:, 0], X_pca_C[:, 1], c='black', s=20, alpha=0.5);


'''

pca = PCA(n_components=2, whiten=True).fit(X)
X_pca = pca.transform(X)

#Verificar Cantidad de Clusters
Nc = range(1, 10)
kmeans = [KMeans(n_clusters=i) for i in Nc]
score = [kmeans[i].fit(X_pca).score(X_pca) for i in range(len(kmeans))]
score
plt.plot(Nc,score)
plt.xlabel('Numero de Clusters')
plt.ylabel('Score')
plt.title('Curva del Codo')
plt.show()

#Aplicar optimo de Numero de Clusters
kmeans = KMeans(n_clusters=3).fit(X)
labels = kmeans.predict(X)

C = kmeans.cluster_centers_
colores=['red','green','blue','cyan','yellow']
asignar=[]
for row in labels:
    asignar.append(colores[row])

################################
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=asignar, s=10, cmap='viridis')

plt.scatter(C[:, 0], C[:, 1], c='black', s=50, alpha=0.5);
################################
#Mostrar Centroides en sus dimensiones originales
centers = pca.inverse_transform(kmeans.cluster_centers_)
print(centers)

#Instancias mas cercanas a clusters
closest, _ = pairwise_distances_argmin_min(kmeans.cluster_centers_, X)
print(closest)#indices del dataset


cobertura=X['PESOS_COBERTURA_SUPERMERCADOS'].values
for row in closest:
    print(cobertura[row])


#Predecir con el modelo ya entrenado
X_in = np.array([[-1.30357739e-02,2.28489484e-01,5.18036247e-03,5.15420989e-02,3.05902670e-02,9.99349161e-01,2.60555288e-04,1.95107905e-01,2.11808226e-03,4.26917188e-02,1.58504804e-02,6.45018308e-01,6.03258340e-01,6.35717578e-01,1.29403368e-02,2.49905384e-01,5.10030389e-03,1.03674421e-02,3.62927127e-03,1.07621551e-02,9.89237856e-01,8.57144911e-01,2.27023458e-03,9.26154934e-01,8.09486133e-01,8.69697378e-01,8.21334173e-01,3.97226057e-03,6.62009214e-03]])
X_pca_in = pca.transform(X_in)

print(X_pca_in)
label_in = kmeans.predict(X_pca_in)
print(label_in)#Cluster predicho
'''

'''
CON EL DATASET SIN NORMALIZAR (Pre-Procesado Datos con Formulas.csv)
import numpy as np
import pandas as pd
from scipy import stats
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn.metrics import pairwise_distances_argmin_min
from scipy.sparse import csr_matrix
from sklearn import preprocessing


df = pd.read_csv('C:/Users/luis.orellana.ext/Documents/Analisis de Clientes/Datos Pro-Procesados.csv',sep=';')


min_max_scaler = preprocessing.MinMaxScaler()
df_tr = min_max_scaler.fit_transform(df)
df_tr

#Cluster the data
kmeans = KMeans(n_clusters=2, random_state=0).fit(df_tr)
labels = kmeans.labels_

#Glue back to originaal data
df['clusters'] = labels

clmns = ['ESTADO_EMPRESA','TIPO_EMPRESA',	'FAMILIA',	'CANTIDAD_EMPLEADOS','CARTERA']
#Add the column into our list
clmns.extend(['clusters'])

#Lets analyze the clusters
print (df[clmns].groupby(['clusters']).mean())
'''
=======
# -*- coding: utf-8 -*-
"""
Editor de Spyder

Este es un archivo temporal

"""

import numpy as np
import pandas as pd
from scipy import stats
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn.metrics import silhouette_score
from sklearn import preprocessing

X = pd.read_csv(r"C:/Users/Luis.O.A/Documents/Analisis Clientes/Dataset2.csv", sep=";")

min_max_scaler = preprocessing.MinMaxScaler()
df_tr = min_max_scaler.fit_transform(X)
df_tr

#Verificar Cantidad de Clusters
Nc = range(2, 15)
kmeans = [KMeans(n_clusters=i) for i in Nc]
score = [kmeans[i].fit(df_tr).score(df_tr) for i in range(len(kmeans))]

plt.plot(Nc,score)
plt.xlabel('Numero de Clusters')
plt.ylabel('Score')
plt.title('Curva del Codo')
plt.show()



silhouette_score_values = [silhouette_score(df_tr,kmeans[i].labels_ ,metric='euclidean', sample_size=None, random_state=None) for i in range(len(kmeans))]

plt.plot(Nc, silhouette_score_values)
plt.title("Silhouette score values vs Numbers of Clusters ")
plt.show()


kmeans = KMeans(n_clusters=3, random_state=0).fit(df_tr)
labels = kmeans.labels_

#Glue back to originaal data
X['clusters'] = labels

clmns = ['TIPO_EMPRESA',
         'FAMILIA',
         'CANTIDAD_EMPLEADOS',
         'CARTERA',
         'REGION',
         'ESTADO_SERVICIOS',
         'Q_ACUERDOS',
         'COMISION',
         'DEUDA',
         'Q_MESES_COMPRA',
         'MES_PRIMERA_COMPRA',
         'MES_ULTIMA_COMPRA',
         'DIAS_CREDITO',
         'CONDICION_ENTREGA',
         'PUNTOS',
         'PORCENTAJE_NO_PERSONALIZADO',
         'MESES_CON_PEDIDOS',
         'VF_PROMEDIO',
         '%_COBERTURA_SUPERMERCADOS',
         '%_COBERTURA_RESTAURANT',
         '%_COBERTURA_BV',
         '%_COBERTURA_MERCHANTS',
         '$_COBERTURA_SUPERMERCADOS']
#Add the column into our list
clmns.extend(['clusters'])

print (X[clmns].groupby(['clusters']).mean())

C = kmeans.cluster_centers_
colores=['red','green','blue','cyan','yellow']
asignar=[]
for row in labels:
    asignar.append(colores[row])

pca = PCA(n_components=2, whiten=True).fit(df_tr)
X_pca = pca.transform(df_tr)

pca_C = PCA(n_components=2, whiten=True).fit(C)
X_pca_C = pca_C.transform(C)

plt.scatter(X_pca[:, 0], X_pca[:, 1], c=asignar, s=5, cmap='viridis')

plt.scatter(X_pca_C[:, 0], X_pca_C[:, 1], c='black', s=20, alpha=0.5);


'''

pca = PCA(n_components=2, whiten=True).fit(X)
X_pca = pca.transform(X)

#Verificar Cantidad de Clusters
Nc = range(1, 10)
kmeans = [KMeans(n_clusters=i) for i in Nc]
score = [kmeans[i].fit(X_pca).score(X_pca) for i in range(len(kmeans))]
score
plt.plot(Nc,score)
plt.xlabel('Numero de Clusters')
plt.ylabel('Score')
plt.title('Curva del Codo')
plt.show()

#Aplicar optimo de Numero de Clusters
kmeans = KMeans(n_clusters=3).fit(X)
labels = kmeans.predict(X)

C = kmeans.cluster_centers_
colores=['red','green','blue','cyan','yellow']
asignar=[]
for row in labels:
    asignar.append(colores[row])

################################
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=asignar, s=10, cmap='viridis')

plt.scatter(C[:, 0], C[:, 1], c='black', s=50, alpha=0.5);
################################
#Mostrar Centroides en sus dimensiones originales
centers = pca.inverse_transform(kmeans.cluster_centers_)
print(centers)

#Instancias mas cercanas a clusters
closest, _ = pairwise_distances_argmin_min(kmeans.cluster_centers_, X)
print(closest)#indices del dataset


cobertura=X['PESOS_COBERTURA_SUPERMERCADOS'].values
for row in closest:
    print(cobertura[row])


#Predecir con el modelo ya entrenado
X_in = np.array([[-1.30357739e-02,2.28489484e-01,5.18036247e-03,5.15420989e-02,3.05902670e-02,9.99349161e-01,2.60555288e-04,1.95107905e-01,2.11808226e-03,4.26917188e-02,1.58504804e-02,6.45018308e-01,6.03258340e-01,6.35717578e-01,1.29403368e-02,2.49905384e-01,5.10030389e-03,1.03674421e-02,3.62927127e-03,1.07621551e-02,9.89237856e-01,8.57144911e-01,2.27023458e-03,9.26154934e-01,8.09486133e-01,8.69697378e-01,8.21334173e-01,3.97226057e-03,6.62009214e-03]])
X_pca_in = pca.transform(X_in)

print(X_pca_in)
label_in = kmeans.predict(X_pca_in)
print(label_in)#Cluster predicho
'''

'''
CON EL DATASET SIN NORMALIZAR (Pre-Procesado Datos con Formulas.csv)
import numpy as np
import pandas as pd
from scipy import stats
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn.metrics import pairwise_distances_argmin_min
from scipy.sparse import csr_matrix
from sklearn import preprocessing


df = pd.read_csv('C:/Users/luis.orellana.ext/Documents/Analisis de Clientes/Datos Pro-Procesados.csv',sep=';')


min_max_scaler = preprocessing.MinMaxScaler()
df_tr = min_max_scaler.fit_transform(df)
df_tr

#Cluster the data
kmeans = KMeans(n_clusters=2, random_state=0).fit(df_tr)
labels = kmeans.labels_

#Glue back to originaal data
df['clusters'] = labels

clmns = ['ESTADO_EMPRESA','TIPO_EMPRESA',	'FAMILIA',	'CANTIDAD_EMPLEADOS','CARTERA']
#Add the column into our list
clmns.extend(['clusters'])

#Lets analyze the clusters
print (df[clmns].groupby(['clusters']).mean())
'''
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
# -*- coding: utf-8 -*-
"""
Editor de Spyder

Este es un archivo temporal

"""

import numpy as np
import pandas as pd
from scipy import stats
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn.metrics import silhouette_score
from sklearn import preprocessing

X = pd.read_csv(r"C:/Users/Luis.O.A/Documents/Analisis Clientes/Dataset2.csv", sep=";")

min_max_scaler = preprocessing.MinMaxScaler()
df_tr = min_max_scaler.fit_transform(X)
df_tr

#Verificar Cantidad de Clusters
Nc = range(2, 15)
kmeans = [KMeans(n_clusters=i) for i in Nc]
score = [kmeans[i].fit(df_tr).score(df_tr) for i in range(len(kmeans))]

plt.plot(Nc,score)
plt.xlabel('Numero de Clusters')
plt.ylabel('Score')
plt.title('Curva del Codo')
plt.show()



silhouette_score_values = [silhouette_score(df_tr,kmeans[i].labels_ ,metric='euclidean', sample_size=None, random_state=None) for i in range(len(kmeans))]

plt.plot(Nc, silhouette_score_values)
plt.title("Silhouette score values vs Numbers of Clusters ")
plt.show()


kmeans = KMeans(n_clusters=3, random_state=0).fit(df_tr)
labels = kmeans.labels_

#Glue back to originaal data
X['clusters'] = labels

clmns = ['TIPO_EMPRESA',
         'FAMILIA',
         'CANTIDAD_EMPLEADOS',
         'CARTERA',
         'REGION',
         'ESTADO_SERVICIOS',
         'Q_ACUERDOS',
         'COMISION',
         'DEUDA',
         'Q_MESES_COMPRA',
         'MES_PRIMERA_COMPRA',
         'MES_ULTIMA_COMPRA',
         'DIAS_CREDITO',
         'CONDICION_ENTREGA',
         'PUNTOS',
         'PORCENTAJE_NO_PERSONALIZADO',
         'MESES_CON_PEDIDOS',
         'VF_PROMEDIO',
         '%_COBERTURA_SUPERMERCADOS',
         '%_COBERTURA_RESTAURANT',
         '%_COBERTURA_BV',
         '%_COBERTURA_MERCHANTS',
         '$_COBERTURA_SUPERMERCADOS']
#Add the column into our list
clmns.extend(['clusters'])

print (X[clmns].groupby(['clusters']).mean())

C = kmeans.cluster_centers_
colores=['red','green','blue','cyan','yellow']
asignar=[]
for row in labels:
    asignar.append(colores[row])

pca = PCA(n_components=2, whiten=True).fit(df_tr)
X_pca = pca.transform(df_tr)

pca_C = PCA(n_components=2, whiten=True).fit(C)
X_pca_C = pca_C.transform(C)

plt.scatter(X_pca[:, 0], X_pca[:, 1], c=asignar, s=5, cmap='viridis')

plt.scatter(X_pca_C[:, 0], X_pca_C[:, 1], c='black', s=20, alpha=0.5);


'''

pca = PCA(n_components=2, whiten=True).fit(X)
X_pca = pca.transform(X)

#Verificar Cantidad de Clusters
Nc = range(1, 10)
kmeans = [KMeans(n_clusters=i) for i in Nc]
score = [kmeans[i].fit(X_pca).score(X_pca) for i in range(len(kmeans))]
score
plt.plot(Nc,score)
plt.xlabel('Numero de Clusters')
plt.ylabel('Score')
plt.title('Curva del Codo')
plt.show()

#Aplicar optimo de Numero de Clusters
kmeans = KMeans(n_clusters=3).fit(X)
labels = kmeans.predict(X)

C = kmeans.cluster_centers_
colores=['red','green','blue','cyan','yellow']
asignar=[]
for row in labels:
    asignar.append(colores[row])

################################
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=asignar, s=10, cmap='viridis')

plt.scatter(C[:, 0], C[:, 1], c='black', s=50, alpha=0.5);
################################
#Mostrar Centroides en sus dimensiones originales
centers = pca.inverse_transform(kmeans.cluster_centers_)
print(centers)

#Instancias mas cercanas a clusters
closest, _ = pairwise_distances_argmin_min(kmeans.cluster_centers_, X)
print(closest)#indices del dataset


cobertura=X['PESOS_COBERTURA_SUPERMERCADOS'].values
for row in closest:
    print(cobertura[row])


#Predecir con el modelo ya entrenado
X_in = np.array([[-1.30357739e-02,2.28489484e-01,5.18036247e-03,5.15420989e-02,3.05902670e-02,9.99349161e-01,2.60555288e-04,1.95107905e-01,2.11808226e-03,4.26917188e-02,1.58504804e-02,6.45018308e-01,6.03258340e-01,6.35717578e-01,1.29403368e-02,2.49905384e-01,5.10030389e-03,1.03674421e-02,3.62927127e-03,1.07621551e-02,9.89237856e-01,8.57144911e-01,2.27023458e-03,9.26154934e-01,8.09486133e-01,8.69697378e-01,8.21334173e-01,3.97226057e-03,6.62009214e-03]])
X_pca_in = pca.transform(X_in)

print(X_pca_in)
label_in = kmeans.predict(X_pca_in)
print(label_in)#Cluster predicho
'''

'''
CON EL DATASET SIN NORMALIZAR (Pre-Procesado Datos con Formulas.csv)
import numpy as np
import pandas as pd
from scipy import stats
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn.metrics import pairwise_distances_argmin_min
from scipy.sparse import csr_matrix
from sklearn import preprocessing


df = pd.read_csv('C:/Users/luis.orellana.ext/Documents/Analisis de Clientes/Datos Pro-Procesados.csv',sep=';')


min_max_scaler = preprocessing.MinMaxScaler()
df_tr = min_max_scaler.fit_transform(df)
df_tr

#Cluster the data
kmeans = KMeans(n_clusters=2, random_state=0).fit(df_tr)
labels = kmeans.labels_

#Glue back to originaal data
df['clusters'] = labels

clmns = ['ESTADO_EMPRESA','TIPO_EMPRESA',	'FAMILIA',	'CANTIDAD_EMPLEADOS','CARTERA']
#Add the column into our list
clmns.extend(['clusters'])

#Lets analyze the clusters
print (df[clmns].groupby(['clusters']).mean())
'''
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep>#api= https://biopython.org/DIST/docs/api/Bio.Entrez-module.html
#ejemplos url = https://www.ncbi.nlm.nih.gov/books/NBK25499/
from Bio import Entrez
import pandas as pd
import re  # For preprocessing
from time import time  # To time our operations
import spacy  # For preprocessing

Entrez.email = '<EMAIL>'

#Bases de datos = https://www.ncbi.nlm.nih.gov/books/NBK25499/table/chapter4.T._valid_values_of__retmode_and/ 
#Obtener lista de id por tema y periodo de tiempo
#https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=cancer&reldate=99999&datetype=edat&retmax=10&usehistory=y
#Sinonimos: https://pubchem.ncbi.nlm.nih.gov/compound/Oxime-_-methoxy-phenyl#section=Synonyms&fullscreen=true

#crear lista de palabras claves a buscar y agregar todos los ids encontrados a la lista "pmids"

key_words = list()
key_words.append("oxime-methoxy-phenyl")
key_words.append("SCHEMBL8530447")
key_words.append("<KEY>")
key_words.append("Methyl N-hydroxybenzenecarboximidoate")
key_words.append("methyl (z)-N-hydroxybenzenecarboximidate")
key_words.append("Methyl")
key_words.append("hydroxybenzenecarboximidoate")
key_words.append("hydroxybenzenecarboximidate")
key_words.append("methoxy")
key_words.append("Oxime")
key_words.append("phenyl")

#key_words.append("Curcumin")
#key_words.append("Diferuloylmethane")
#key_words.append("Turmeric Yellow")
#key_words.append("Turmeric")

pmids =  list()

for key_word in key_words:
    handle = Entrez.esearch(db="pubmed", retmax=9999999, term=key_word)
    records = Entrez.read(handle)
    # Obtener abstract por id 
    if len(records['IdList']) > 0:
        pmids.extend(records['IdList'])

pmids = (list(pmids))
print(pmids)


handle = Entrez.efetch(db="pubmed", id=','.join(map(str, pmids)), retmode="json", rettype="xml")

records = Entrez.read(handle)

abstracts = list()
pmids =  list()

for pubmed_article in records['PubmedArticle']:
    try:
        abstracts.append(pubmed_article['MedlineCitation']['Article']['Abstract']['AbstractText'][0])
        pmids.append(pubmed_article['MedlineCitation']['PMID'])
    except Exception:
        #abstracts.append("-")
        #pmids.append("-")
        pass

df = pd.DataFrame({"Ids":pmids, "Abstract":abstracts})

df.to_csv (r'D:\Investigacion Word2Vec\Modelo\pubmed_abstracts.csv', index = None, header=True)


print(df)

############LIMPIEZA DE LOS DATOS   

df.isnull().sum()
df = df.dropna().reset_index(drop=True)
df.isnull().sum()

nlp = spacy.load('en', disable=['ner', 'parser']) # disabling Named Entity Recognition for speed

def cleaning(doc):
    # Lemmatizes and removes stopwords
    # doc needs to be a spacy Doc object
    txt = [token.lemma_ for token in doc if not token.is_stop]
    # Word2Vec uses context words to learn the vector representation of a target word,
    # if a sentence is only one or two words long,
    # the benefit for the training is very small
    if len(txt) > 2:
        return ' '.join(txt)

brief_cleaning = (re.sub("<..........>|<.........>|<........>|<.......>|<......>|<.....>|<....>|<...>|<..>|<.>|[^A-Za-z]+", ' ', str(row)).lower() for row in df['Abstract'])

t = time()

txt = [cleaning(doc) for doc in nlp.pipe(brief_cleaning, batch_size=5000, n_threads=-1)]

print('Time to clean up everything: {} mins'.format(round((time() - t) / 60, 2)))


df_clean = pd.DataFrame({'clean': txt})
df_clean = df_clean.dropna().drop_duplicates()
df_clean.shape

df_clean.to_csv (r'D:\Investigacion Word2Vec\Modelo\pubmed_sentences.csv', index = None, header=True)


handle.close()<file_sep># make sure utils.py is in the same directory as this code
import matplotlib.pyplot as plt
import numpy as np
import Load_Dataset as ld



# note we also shrink the intensity values (X) from 0-255 to 0-1. This helps the model converge faster.
X_train, y_train, X_test, y_test = ld.get_data()

# now let's show some randomly chosen images from the traininng set.
count = 0
sample_size = 30
plt.figure(figsize = (16, 6))
for i in np.random.permutation(X_train.shape[0])[:sample_size]:
    count = count + 1
    plt.subplot(1, sample_size, count)
    plt.axhline('')
    plt.axvline('')
    plt.text(x=10, y=-10, s=y_train[i], fontsize=18)
    plt.imshow(X_train[i].reshape(28, 28), cmap=plt.cm.Greys)
plt.show()<file_sep>import pandas as pd
from pandas.io import gbq
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.tools import FigureFactory as FF

project_id = 'titanic-231219'

top10_active_users_query = "SELECT * FROM [titanic-231219.titanic.titanic_passenger] LIMIT 10"


top10_active_users_df = gbq.to_gbq(top10_active_users_query, project_id=project_id)

print (top10_active_users_df)





<file_sep># download dataset
import os
import urllib.request

os.makedirs('./Python/Regresion Logistica - Clasificacion Imagenes/data', exist_ok = True)

urllib.request.urlretrieve('http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz', filename='./Python/Regresion Logistica - Clasificacion Imagenes/data/train-images.gz')
urllib.request.urlretrieve('http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz', filename='./Python/Regresion Logistica - Clasificacion Imagenes/data/train-labels.gz')
urllib.request.urlretrieve('http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz', filename='./Python/Regresion Logistica - Clasificacion Imagenes/data/test-images.gz')
urllib.request.urlretrieve('http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz', filename='./Python/Regresion Logistica - Clasificacion Imagenes/data/test-labels.gz')

<file_sep><<<<<<< HEAD
<<<<<<< HEAD

install.packages('randomForest', dep = TRUE)
library(randomForest)

install.packages('party', dep = TRUE)
library(party)


setwd("C:/Users/Luis.O.A/Documents/USACH/Mineria de Datos/grupo 6 Wine Quality")

wine <- read.csv('winequality-red.csv', sep=';')

##Mostrar variable con mas outliers
source("http://goo.gl/UUyEzD")
outlierKD(wine, wine$residual.sugar)

remove_outliers <- function(x, na.rm = TRUE, ...) {
  qnt <- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
  H <- 1.5 * IQR(x, na.rm = na.rm)
  y <- x
  y[x < (qnt[1] - H)] <- mean(x)#NA
  y[x > (qnt[2] + H)] <- mean(x)#NA
  y
}

wine$alcohol <- remove_outliers(wine$alcohol)
wine$sulphates <- remove_outliers(wine$sulphates)
wine$pH <- remove_outliers(wine$pH)
wine$free.sulfur.dioxid <- remove_outliers(wine$free.sulfur.dioxid)
wine$chlorides <- remove_outliers(wine$chlorides)
wine$residual.sugar <- remove_outliers(wine$residual.sugar)
wine$citric.acid <- remove_outliers(wine$citric.acid)
wine$volatile.acidity <- remove_outliers(wine$volatile.acidity)
wine$fixed.acidity <- remove_outliers(wine$fixed.acidity)


#Normalizacion
dataNorm <- as.data.frame( scale(wine[1:12] ))

#Etiquetar instancias
dataNorm$taste <- ifelse(dataNorm$quality <= -2.02586, 'malo', 'regular')
dataNorm$taste[dataNorm$quality >= 1.68899] <- 'bueno'
dataNorm$taste <- as.factor(dataNorm$taste)

barplot(table(dataNorm$taste))
############ Ramdom Forest ############

set.seed(123)
samp <- sample(nrow(dataNorm), 0.75 * nrow(dataNorm))
train <- dataNorm[samp, ]
test <- dataNorm[-samp, ]

model <- randomForest(taste ~ . - quality, data = train, importance=TRUE, proximity=TRUE, ntree=1000)

model
####Con outliers: OOB estimate of  error rate: 10.93%
####Sin outliers: OOB estimate of  error rate: 11.01%

pred <- predict(model, newdata = test)
table(pred, test$taste)

#################
##Sin Outliers
#Accuracy 
(29 + 0 + 344) / nrow(test)
#0.9109375 = 91%
#P = 29/(29+19) = 0.604
#R = 29/(29+5) = 0.853
#FScore
(2*0.604*0.853)/(0.604 + 0.853)
# 0.7072231
#################
##Con Outliers
#Accuracy 
(29 + 0 + 342) / nrow(test)

#P = 29/(29+19) = 0.604
#R = 29/(29+7) = 0.8055556
#FScore
(2*0.604*0.8055556)/(0.604 + 0.8055556)
#0.6903674
#################
#Configuracion optima
model <- randomForest(taste ~ . - quality, data = train, importance=TRUE, proximity=TRUE, ntree=1000, mtry=8)

# Grafica de error vs arboles
plot(model)
legend("bottomright", colnames(model$err.rate),col=1:4,cex=0.8,fill=1:4)

##############

#Ver calidad de las variables
round(importance(model), 2)

varImpPlot(model)



mds <- cmdscale(1 - model$proximity, eig=TRUE) 


op <- par(pty="s")

pairs(cbind(train[1:11], mds$points), cex=0.6, gap=0,
col=c("red", "green", "blue")[as.numeric(test$taste)],
main="Wine Data: Predictors and MDS of Proximity Based on RandomForest")

par(op)

print(mds$GOF)
MDSplot(model, test$taste)
=======

install.packages('randomForest', dep = TRUE)
library(randomForest)

install.packages('party', dep = TRUE)
library(party)


setwd("C:/Users/Luis.O.A/Documents/USACH/Mineria de Datos/grupo 6 Wine Quality")

wine <- read.csv('winequality-red.csv', sep=';')

##Mostrar variable con mas outliers
source("http://goo.gl/UUyEzD")
outlierKD(wine, wine$residual.sugar)

remove_outliers <- function(x, na.rm = TRUE, ...) {
  qnt <- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
  H <- 1.5 * IQR(x, na.rm = na.rm)
  y <- x
  y[x < (qnt[1] - H)] <- mean(x)#NA
  y[x > (qnt[2] + H)] <- mean(x)#NA
  y
}

wine$alcohol <- remove_outliers(wine$alcohol)
wine$sulphates <- remove_outliers(wine$sulphates)
wine$pH <- remove_outliers(wine$pH)
wine$free.sulfur.dioxid <- remove_outliers(wine$free.sulfur.dioxid)
wine$chlorides <- remove_outliers(wine$chlorides)
wine$residual.sugar <- remove_outliers(wine$residual.sugar)
wine$citric.acid <- remove_outliers(wine$citric.acid)
wine$volatile.acidity <- remove_outliers(wine$volatile.acidity)
wine$fixed.acidity <- remove_outliers(wine$fixed.acidity)


#Normalizacion
dataNorm <- as.data.frame( scale(wine[1:12] ))

#Etiquetar instancias
dataNorm$taste <- ifelse(dataNorm$quality <= -2.02586, 'malo', 'regular')
dataNorm$taste[dataNorm$quality >= 1.68899] <- 'bueno'
dataNorm$taste <- as.factor(dataNorm$taste)

barplot(table(dataNorm$taste))
############ Ramdom Forest ############

set.seed(123)
samp <- sample(nrow(dataNorm), 0.75 * nrow(dataNorm))
train <- dataNorm[samp, ]
test <- dataNorm[-samp, ]

model <- randomForest(taste ~ . - quality, data = train, importance=TRUE, proximity=TRUE, ntree=1000)

model
####Con outliers: OOB estimate of  error rate: 10.93%
####Sin outliers: OOB estimate of  error rate: 11.01%

pred <- predict(model, newdata = test)
table(pred, test$taste)

#################
##Sin Outliers
#Accuracy 
(29 + 0 + 344) / nrow(test)
#0.9109375 = 91%
#P = 29/(29+19) = 0.604
#R = 29/(29+5) = 0.853
#FScore
(2*0.604*0.853)/(0.604 + 0.853)
# 0.7072231
#################
##Con Outliers
#Accuracy 
(29 + 0 + 342) / nrow(test)

#P = 29/(29+19) = 0.604
#R = 29/(29+7) = 0.8055556
#FScore
(2*0.604*0.8055556)/(0.604 + 0.8055556)
#0.6903674
#################
#Configuracion optima
model <- randomForest(taste ~ . - quality, data = train, importance=TRUE, proximity=TRUE, ntree=1000, mtry=8)

# Grafica de error vs arboles
plot(model)
legend("bottomright", colnames(model$err.rate),col=1:4,cex=0.8,fill=1:4)

##############

#Ver calidad de las variables
round(importance(model), 2)

varImpPlot(model)



mds <- cmdscale(1 - model$proximity, eig=TRUE) 


op <- par(pty="s")

pairs(cbind(train[1:11], mds$points), cex=0.6, gap=0,
col=c("red", "green", "blue")[as.numeric(test$taste)],
main="Wine Data: Predictors and MDS of Proximity Based on RandomForest")

par(op)

print(mds$GOF)
MDSplot(model, test$taste)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======

install.packages('randomForest', dep = TRUE)
library(randomForest)

install.packages('party', dep = TRUE)
library(party)


setwd("C:/Users/Luis.O.A/Documents/USACH/Mineria de Datos/grupo 6 Wine Quality")

wine <- read.csv('winequality-red.csv', sep=';')

##Mostrar variable con mas outliers
source("http://goo.gl/UUyEzD")
outlierKD(wine, wine$residual.sugar)

remove_outliers <- function(x, na.rm = TRUE, ...) {
  qnt <- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
  H <- 1.5 * IQR(x, na.rm = na.rm)
  y <- x
  y[x < (qnt[1] - H)] <- mean(x)#NA
  y[x > (qnt[2] + H)] <- mean(x)#NA
  y
}

wine$alcohol <- remove_outliers(wine$alcohol)
wine$sulphates <- remove_outliers(wine$sulphates)
wine$pH <- remove_outliers(wine$pH)
wine$free.sulfur.dioxid <- remove_outliers(wine$free.sulfur.dioxid)
wine$chlorides <- remove_outliers(wine$chlorides)
wine$residual.sugar <- remove_outliers(wine$residual.sugar)
wine$citric.acid <- remove_outliers(wine$citric.acid)
wine$volatile.acidity <- remove_outliers(wine$volatile.acidity)
wine$fixed.acidity <- remove_outliers(wine$fixed.acidity)


#Normalizacion
dataNorm <- as.data.frame( scale(wine[1:12] ))

#Etiquetar instancias
dataNorm$taste <- ifelse(dataNorm$quality <= -2.02586, 'malo', 'regular')
dataNorm$taste[dataNorm$quality >= 1.68899] <- 'bueno'
dataNorm$taste <- as.factor(dataNorm$taste)

barplot(table(dataNorm$taste))
############ Ramdom Forest ############

set.seed(123)
samp <- sample(nrow(dataNorm), 0.75 * nrow(dataNorm))
train <- dataNorm[samp, ]
test <- dataNorm[-samp, ]

model <- randomForest(taste ~ . - quality, data = train, importance=TRUE, proximity=TRUE, ntree=1000)

model
####Con outliers: OOB estimate of  error rate: 10.93%
####Sin outliers: OOB estimate of  error rate: 11.01%

pred <- predict(model, newdata = test)
table(pred, test$taste)

#################
##Sin Outliers
#Accuracy 
(29 + 0 + 344) / nrow(test)
#0.9109375 = 91%
#P = 29/(29+19) = 0.604
#R = 29/(29+5) = 0.853
#FScore
(2*0.604*0.853)/(0.604 + 0.853)
# 0.7072231
#################
##Con Outliers
#Accuracy 
(29 + 0 + 342) / nrow(test)

#P = 29/(29+19) = 0.604
#R = 29/(29+7) = 0.8055556
#FScore
(2*0.604*0.8055556)/(0.604 + 0.8055556)
#0.6903674
#################
#Configuracion optima
model <- randomForest(taste ~ . - quality, data = train, importance=TRUE, proximity=TRUE, ntree=1000, mtry=8)

# Grafica de error vs arboles
plot(model)
legend("bottomright", colnames(model$err.rate),col=1:4,cex=0.8,fill=1:4)

##############

#Ver calidad de las variables
round(importance(model), 2)

varImpPlot(model)



mds <- cmdscale(1 - model$proximity, eig=TRUE) 


op <- par(pty="s")

pairs(cbind(train[1:11], mds$points), cex=0.6, gap=0,
col=c("red", "green", "blue")[as.numeric(test$taste)],
main="Wine Data: Predictors and MDS of Proximity Based on RandomForest")

par(op)

print(mds$GOF)
MDSplot(model, test$taste)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
legend("bottomright", colnames(model$classes),col=1:3,cex=0.8,fill=1:3, legend=model$classes)<file_sep>from utils import load_data

def get_data():
    # note we also shrink the intensity values (X) from 0-255 to 0-1. This helps the model converge faster.
    X_train = load_data('./Python/Regresion Logistica - Clasificacion Imagenes/data/train-images.gz', False) / 255.0
    y_train = load_data('./Python/Regresion Logistica - Clasificacion Imagenes/data/train-labels.gz', True).reshape(-1)

    X_test = load_data('./Python/Regresion Logistica - Clasificacion Imagenes/data/test-images.gz', False) / 255.0
    y_test = load_data('./Python/Regresion Logistica - Clasificacion Imagenes/data/test-labels.gz', True).reshape(-1)

    return X_train, y_train, X_test, y_test

<file_sep><<<<<<< HEAD
<<<<<<< HEAD
#<NAME>
#Carga dataset ZOO
data_zoo  =  read.csv("zoo-tree1.csv") 
#Discretiza datos
data_zoo["animal_name"] <- NULL
data_zoo <- data.frame(sapply(data_zoo,as.factor)) 
# Clasifica los modelos de árbol usando el algoritmo de Quinlan C5.0 
c50_result<-C5.0(class_type~.,data=data_zoo) 
# Despliega resumen detallado para los modelos C.50 
summary(c50_result) 
# calcula y despliega la importancia de la variable para el modelo C5.0
 importanceC5imp(c50_result,metric='usage')

#REGLAS.

# Generar reglas a partir del arcol de decisión de los modelos C.50 
ruleModel <- C5.0(type ~ ., data = data_zoo, rules = TRUE)
ruleModel
# Despliega resumen detallado para las reglas de los modelos C.50
summary(ruleModel)

#VALIDACION CRUZADA.
library(arulesViz)
install.packages('partykit', dep = TRUE)
install.packages('gmodels', dep = TRUE)
library(partykit)
library(C50)
library(gmodels)
 library(plyr)
data_zoo  =  read.csv("zoo-tree1.csv") #Carga dataset
data_zoo["animal_name"] <- NULL
data_zoo <- data.frame(sapply(data_zoo,as.factor)) #Discretiza datos

 form <- "class_type  ~ hair+feathers+eggs+milk+airborne+aquatic+predator+toothed+backbone+breathes+venomous+fins+legs0+legs2+legs4+legs5+legs6+legs8+tail+domestic+catsize" 

folds <- split(data_zoo , cut(sample(1:nrow(data_zoo)),10)) 
length(folds)
folds
errs.c50 <- rep(NA, length(folds))
for (i in 1:length(folds)) { 
test <- ldply(folds[i], data.frame) 
train <- ldply(folds[-i], data.frame) 
tmp.model <- C5.0(as.formula(form), train) 
tmp.predict <- predict(tmp.model, newdata=test) 
conf.mat <- table(test$class_type , tmp.predict) 
errs.c50[i] <- 1 - sum(diag(conf.mat))/sum(conf.mat)
 }
print(sprintf("El promedio de error usando la validación cruzada k-fold y el algoritmo de arbol de decision  C5.0 es: %.3f percent", 100*mean(errs.c50))) 
=======
#ARBOL BINARIO
#Carga dataset ZOO
data_zoo  =  read.csv("zoo-tree1.csv") 
#Discretiza datos
data_zoo["animal_name"] <- NULL
data_zoo <- data.frame(sapply(data_zoo,as.factor)) 
# Clasifica los modelos de árbol usando el algoritmo de Quinlan C5.0 
c50_result<-C5.0(class_type~.,data=data_zoo) 
# Despliega resumen detallado para los modelos C.50 
summary(c50_result) 
# calcula y despliega la importancia de la variable para el modelo C5.0
 importanceC5imp(c50_result,metric='usage')

#REGLAS.

# Generar reglas a partir del arcol de decisión de los modelos C.50 
ruleModel <- C5.0(type ~ ., data = data_zoo, rules = TRUE)
ruleModel
# Despliega resumen detallado para las reglas de los modelos C.50
summary(ruleModel)

#VALIDACION CRUZADA.
library(arulesViz)
install.packages('partykit', dep = TRUE)
install.packages('gmodels', dep = TRUE)
library(partykit)
library(C50)
library(gmodels)
 library(plyr)
data_zoo  =  read.csv("zoo-tree1.csv") #Carga dataset
data_zoo["animal_name"] <- NULL
data_zoo <- data.frame(sapply(data_zoo,as.factor)) #Discretiza datos

 form <- "class_type  ~ hair+feathers+eggs+milk+airborne+aquatic+predator+toothed+backbone+breathes+venomous+fins+legs0+legs2+legs4+legs5+legs6+legs8+tail+domestic+catsize" 

folds <- split(data_zoo , cut(sample(1:nrow(data_zoo)),10)) 
length(folds)
folds
errs.c50 <- rep(NA, length(folds))
for (i in 1:length(folds)) { 
test <- ldply(folds[i], data.frame) 
train <- ldply(folds[-i], data.frame) 
tmp.model <- C5.0(as.formula(form), train) 
tmp.predict <- predict(tmp.model, newdata=test) 
conf.mat <- table(test$class_type , tmp.predict) 
errs.c50[i] <- 1 - sum(diag(conf.mat))/sum(conf.mat)
 }
print(sprintf("El promedio de error usando la validación cruzada k-fold y el algoritmo de arbol de decision  C5.0 es: %.3f percent", 100*mean(errs.c50))) 
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
#ARBOL BINARIO
#Carga dataset ZOO
data_zoo  =  read.csv("zoo-tree1.csv") 
#Discretiza datos
data_zoo["animal_name"] <- NULL
data_zoo <- data.frame(sapply(data_zoo,as.factor)) 
# Clasifica los modelos de árbol usando el algoritmo de Quinlan C5.0 
c50_result<-C5.0(class_type~.,data=data_zoo) 
# Despliega resumen detallado para los modelos C.50 
summary(c50_result) 
# calcula y despliega la importancia de la variable para el modelo C5.0
 importanceC5imp(c50_result,metric='usage')

#REGLAS.

# Generar reglas a partir del arcol de decisión de los modelos C.50 
ruleModel <- C5.0(type ~ ., data = data_zoo, rules = TRUE)
ruleModel
# Despliega resumen detallado para las reglas de los modelos C.50
summary(ruleModel)

#VALIDACION CRUZADA.
library(arulesViz)
install.packages('partykit', dep = TRUE)
install.packages('gmodels', dep = TRUE)
library(partykit)
library(C50)
library(gmodels)
 library(plyr)
data_zoo  =  read.csv("zoo-tree1.csv") #Carga dataset
data_zoo["animal_name"] <- NULL
data_zoo <- data.frame(sapply(data_zoo,as.factor)) #Discretiza datos

 form <- "class_type  ~ hair+feathers+eggs+milk+airborne+aquatic+predator+toothed+backbone+breathes+venomous+fins+legs0+legs2+legs4+legs5+legs6+legs8+tail+domestic+catsize" 

folds <- split(data_zoo , cut(sample(1:nrow(data_zoo)),10)) 
length(folds)
folds
errs.c50 <- rep(NA, length(folds))
for (i in 1:length(folds)) { 
test <- ldply(folds[i], data.frame) 
train <- ldply(folds[-i], data.frame) 
tmp.model <- C5.0(as.formula(form), train) 
tmp.predict <- predict(tmp.model, newdata=test) 
conf.mat <- table(test$class_type , tmp.predict) 
errs.c50[i] <- 1 - sum(diag(conf.mat))/sum(conf.mat)
 }
print(sprintf("El promedio de error usando la validación cruzada k-fold y el algoritmo de arbol de decision  C5.0 es: %.3f percent", 100*mean(errs.c50))) 
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep>from sklearn.linear_model import LogisticRegression
import Load_Dataset as ld
import numpy as np

X_train, y_train, X_test, y_test = ld.get_data()

clf = LogisticRegression()
clf.fit(X_train, y_train)

y_hat = clf.predict(X_test)
print(np.average(y_hat == y_test))<file_sep># Change accordingly
VERSION <- "v15.9.10"
WORK.DIR <- file.path("C:", "Users/Luis/Documents/DataScience/Tesis", "Calculo ARI", VERSION)

CARSIG.SCRIPT.BASENAME <- paste("carSignal", VERSION, sep = "-")
CARSIG.SCRIPT.BASENAME <- paste(CARSIG.SCRIPT.BASENAME, "R", sep = ".")
CARSIG.SCRIPT.NAME <- file.path(WORK.DIR, CARSIG.SCRIPT.BASENAME)
source(CARSIG.SCRIPT.NAME)

DARI.SCRIPT.BASENAME <- paste("atARI", VERSION, sep = "-")
DARI.SCRIPT.BASENAME <- paste(DARI.SCRIPT.BASENAME, "R", sep = ".")
DARI.SCRIPT.NAME <- file.path(WORK.DIR, DARI.SCRIPT.BASENAME)
source(DARI.SCRIPT.NAME)

METRIC.UTILS.SCRIPT.BASENAME <-
  paste("Metric", "utils", VERSION, sep = "-")
METRIC.UTILS.SCRIPT.BASENAME <-
  paste(METRIC.UTILS.SCRIPT.BASENAME, "R", sep = ".")
METRIC.UTILS.SCRIPT.NAME <-
  file.path(WORK.DIR, METRIC.UTILS.SCRIPT.BASENAME)
source(METRIC.UTILS.SCRIPT.NAME)

MFARI.SCRIPT.BASENAME <- paste("mfARI", VERSION, sep = "-")
MFARI.SCRIPT.BASENAME <- paste(MFARI.SCRIPT.BASENAME, "R", sep = ".")
MFARI.SCRIPT.NAME <- file.path(WORK.DIR, MFARI.SCRIPT.BASENAME)
source(MFARI.SCRIPT.NAME)

ROUND.UTILS.SCRIPT.BASENAME <-
  paste("Rounding", "utils", VERSION, sep = "-")
ROUND.UTILS.SCRIPT.BASENAME <-
  paste(ROUND.UTILS.SCRIPT.BASENAME, "R", sep = ".")
ROUND.UTILS.SCRIPT.NAME <- 
  file.path(WORK.DIR, ROUND.UTILS.SCRIPT.BASENAME)
source(ROUND.UTILS.SCRIPT.NAME)


get.normalised.dari.templates <- function(
      time.instants,
      abp.signal,
      sample.release,
      sampling.time,
      baseline.initial.time = time.instants[1],
      baseline.final.time = time.instants[sample.release],
      min.abp.max.delta.time = 5 * 0.8,
      min.cbfv.max.delta.time = 8 * 0.8,
      stabilisation.time = 1,
      at.param.rounding.digits = 6,
      normalised.cbfv.rounding.digits = 4,
      time.tol = sampling.time / 100,
      ... # Not used
      )
{
  # Gets normalised ABP
  normalised.abp.signal <- normalise.ABP.signal(
    time.instants = time.instants,
    ABP.signal = abp.signal,
    sample.release = sample.release,
    sampling.time = sampling.time,
    baseline.initial.time = baseline.initial.time,
    baseline.final.time = baseline.final.time,
    min.ABP.max.delta.time = min.abp.max.delta.time,
    time.tol = time.tol
  )
  
  # Gets 91 Aaslid-Tiecks parameters
  at.params <- get.AT.decimal.templates.parameters(
    rounding.digits = at.param.rounding.digits
  )
  
  .tmp.fun <- function(i)
    get.theoretical.CBFV.response(
      T = at.params[i, 1],
      D = at.params[i, 2],
      K = at.params[i, 3],
      time.instants = time.instants,
      ABP.normalised = normalised.abp.signal[["normalised.ABP.signal"]],
      sampling.time = sampling.time,
      stabilisation.time = stabilisation.time
    )
  r <- 1:nrow(at.params)
  templates <- lapply(r, .tmp.fun)
  
  .tmp.fun <- function(t)
    normalise.CBFV.signal(
      time.instants = time.instants,
      CBFV.signal = t[["CBFV.theoretical.response"]],
      sample.release = sample.release,
      sampling.time = sampling.time,
      baseline.initial.time = baseline.initial.time,
      baseline.final.time = baseline.final.time,
      min.CBFV.max.delta.time = min.cbfv.max.delta.time,
      time.tol = time.tol
    )
  normalised.dari.templates <- lapply(templates, .tmp.fun)
  
  .tmp.fun <- function(t)
    round(t[["normalised.CBFV.signal"]], normalised.cbfv.rounding.digits)
  normalised.dari.templates <- lapply(normalised.dari.templates, .tmp.fun)
  
  names(normalised.dari.templates) <- 
    sapply(r, function(i) sprintf("%.1f", at.params[i, 4]))
  
  normalised.dari.templates
}


get.mfari.stats <- function(
      time.instants,
      abp.signal,
      cbfv.signal,
      sampling.time,
      time.release,
      keep.details = FALSE,
      ... # Passed to get.mfARI.parameters
    )
{
  mfari.params <- get.mfARI.parameters(
    time.instants = time.instants,
    ABP.signal = abp.signal,
    CBFV.signal = cbfv.signal,
    sampling.time = sampling.time,
    time.release = time.release,
    keep.details = keep.details,
    ...
  )
  
  mfari <- get.mfARI(
    Ks = mfari.params[["Ks"]],
    Delta.tau = mfari.params[["Delta.tau"]],
    Phi = mfari.params[["Phi"]]
  )
  
  data.frame(
    Ks = mfari.params[["Ks"]],
    Delta.tau = mfari.params[["Delta.tau"]],
    Phi = mfari.params[["Phi"]],
    mfARI = round(mfari, 1)
  )
}


get.dari.stats <- function(
      time.instants,
      cbfv.signal,
      sample.release,
      sampling.time,
      normalised.dari.templates,
      baseline.initial.time = time.instants[1],
      baseline.final.time = time.instants[sample.release],
      min.cbfv.max.delta.time = 8 * 0.8,
      fitting.value.name = "GoF",
      fitting.value.rounding.digits = 4,
      time.tol = sampling.time / 100,
      ... # Passed to get.best.templates()
      )
{
  normalised.cbfv.signal <- normalise.CBFV.signal(
    time.instants = time.instants,
    CBFV.signal = cbfv.signal,
    sample.release = sample.release,
    sampling.time = sampling.time,
    baseline.initial.time = baseline.initial.time,
    baseline.final.time = baseline.final.time,
    min.CBFV.max.delta.time = min.cbfv.max.delta.time,
    time.tol = time.tol
  )
  
  dari.search <- get.best.templates(
    time.instants = time.instants,
    signal = normalised.cbfv.signal[["normalised.CBFV.signal"]],
    templates = normalised.dari.templates,
    keep.details = FALSE,
    time.tol = time.tol,
    ...
  )
  
  ibest <- dari.search[["best.template.index"]]
  error <- dari.search[["best.fit.value"]]
  error <- round(error, fitting.value.rounding.digits)
  dari <- as.numeric(names(normalised.dari.templates)[ibest])
  
  d <- data.frame(error, dari)
  colnames(d) <- c(fitting.value.name, "dARI")
  
  d
}


get.signal.stats <- function(
      time.instants,
      abp.signal,
      cbfv.signal,
      sampling.time,
      sample.release,
      normalised.dari.templates,
      time.release = time.instants[sample.release],
      ... # Passed to get.mfari.stats & get.ari.stats
    )
{ 
  mfari.stats <- get.mfari.stats(
    time.instants = time.instants,
    abp.signal = abp.signal,
    cbfv.signal = cbfv.signal,
    sampling.time = sampling.time,
    time.release = time.release,
    ...
  )
  
  dari.stats <- get.dari.stats(
    time.instants = time.instants,
    cbfv.signal = cbfv.signal,
    sample.release = sample.release,
    sampling.time = sampling.time,
    normalised.dari.templates = normalised.dari.templates,
    ...
  )
  
  cbind(mfari.stats, dari.stats)
}


get.mvre.stats <- function(
      mvre.data,
      time.col.name = "Time",
      abp.col.name = "ABP",
      left.cbfv.col.name = "LCBFV",
      right.cbfv.col.name = "RCBFV",
      sampling.time = 0.2,
      time.release = 0,
      time.tol = sampling.time / 100,
      indent = "",
      ... # Passed to get.signal.stats
      )
{
  # Gets sample of release
  sample.release <- which(are.tolerably.equal(
    mvre.data[[time.col.name]],
    time.release,
    time.tol
  ))
  
  # Gets dARI templates
  normalised.dari.templates <- get.normalised.dari.templates(
    time.instants = mvre.data[[time.col.name]],
    abp.signal = mvre.data[[abp.col.name]],
    sample.release = sample.release,
    sampling.time = sampling.time,
    time.tol = time.tol,
    ...
  )
  
  # Left hemisphere
  cat(indent, "-- Left hemisphere --\n", sep = "")
  
  left.signal.stats <- get.signal.stats(
    time.instants = mvre.data[[time.col.name]],
    abp.signal = mvre.data[[abp.col.name]],
    cbfv.signal = mvre.data[[left.cbfv.col.name]],
    sampling.time = sampling.time,
    sample.release = sample.release,
    normalised.dari.templates = normalised.dari.templates,
    time.release = time.release,
    time.tol = time.tol,
    ...
  )
  
  left.signal.stats <- cbind(
    data.frame(Side = "Left"),
    left.signal.stats
  )
  
  # Right hemisphere
  cat(indent, "-- Right hemisphere --\n", sep = "")
  
  right.signal.stats <- get.signal.stats(
    time.instants = mvre.data[[time.col.name]],
    abp.signal = mvre.data[[abp.col.name]],
    cbfv.signal = mvre.data[[right.cbfv.col.name]],
    sampling.time = sampling.time,
    sample.release = sample.release,
    normalised.dari.templates = normalised.dari.templates,
    time.release = time.release,
    time.tol = time.tol,
    ...
  )
  
  right.signal.stats <- cbind(
    data.frame(Side = "Right"),
    right.signal.stats
  )
  
  # Joins stats
  rbind(left.signal.stats, right.signal.stats)
}


get.subject.stats.by.mvre <- function(
      subject,
      manoeuvres,
      src.subject.dir,
      src.ext = "txt",
      header = TRUE,
      indent = "",
      ... # Passed to get.mvre.stats
      )
{
  stats <- NULL
  for(mvre in manoeuvres)
  {
    cat(indent, "-- ", mvre, " --\n", sep = "")
    
    # Sets the subject source file
    src.basename <- paste(subject, mvre, sep = "-")
    src.basename <- paste(src.basename, src.ext, sep = ".")
    src.filename <- file.path(src.subject.dir, src.basename)
    
    if(!file.exists(src.filename))
    {
      cat(
        paste(indent, " "),
        "Warning: file ",
        src.filename,
        " does not exists\n",
        sep = ""
      )
      next
    }
    
    # Reads the manoeuvre signals
    mvre.data <- read.table(src.filename, header = header)
    
    # Gets manoeuvre stats
    mvre.stats <- get.mvre.stats(
      mvre.data = mvre.data,
      indent = paste0(indent, "  "),
      ... # Passed to get.signal.stats
    )
    
    mvre.stats <- cbind(
      data.frame(Manoeuvre = mvre),
      mvre.stats
    )
    
    # Joins manoeuvre's stats
    stats <- rbind(stats, mvre.stats)
    
  }
  
  stats
}

get.stats.by.mvre <- function(
      src.dir,
      subjects,
      manoeuvres,
      indent = "",
      ... # Passed to get.subject.stats.by.mvre
      )
{
  stats <- NULL
  for(subject in subjects)
  {
    cat(indent, "-- ", subject, " --\n", sep = "")
    
    # Sets subject source directory
    src.subject.dir <- file.path(src.dir, subject)
    
    # Gets the subject's stats
    subject.stats <- get.subject.stats.by.mvre(
      subject = subject,
      manoeuvres = manoeuvres,
      src.subject.dir = src.subject.dir,
      indent = paste(indent, " "),
      ... # Passed to get.mvre.stats
    )
    
    subject.stats <- cbind(
      data.frame(Subject = subject),
      subject.stats
    )
    
    # Joins subject's stats
    stats <- rbind(stats, subject.stats)
  }
  
  stats
}


get.stats.by.subject <- function(stats.by.mvre, index.name)
{
  col.names <- c("Subject", "Manoeuvre", "Side", index.name)
  table.index <- stats.by.mvre[, col.names]
  table.index <- reshape(
    data = table.index,
    idvar = c("Subject", "Side"),
    timevar = "Manoeuvre",
    direction = "wide"
  )

  col.names <- colnames(table.index)[3:ncol(table.index)]
  col.names <- gsub(paste0(index.name, "."), "", col.names)
  colnames(table.index)[3:ncol(table.index)] <- col.names
  
  means <- apply(table.index[, col.names], 1, mean)
  sds <- apply(table.index[, col.names], 1, sd)
  covs = round(100 * sds / means, 2)
  covs
  table.index[["Mean"]] <- round(means, 1)
  table.index[["SD"]] <- round(sds, 2)
  table.index[["CoV"]] <- round(covs, 2)
  
  table.index
}

run <- function(
  src.dir = file.path(WORK.DIR, "Data"),
  src.ext = "txt",
  header = TRUE,
  tgt.suffix = paste("stats", VERSION, sep = "-"),
  tgt.dir = file.path(WORK.DIR, "stats"),
  tgt.ext = "csv",
  overwrite = TRUE,
  subjects = c("AC","AP","AV","CC","CS","DM","DS","GP","HF","HS","IH","MM","MR","MV","ND","PC","RO", "VT"),
  manoeuvres = c("ACOSTADO", "PIE", "SENTADO"),
  time.col.name = "Time",
  abp.col.name = "ABP",
  left.cbfv.col.name = "LCBFV",
  right.cbfv.col.name = "RCBFV",
  left.plot.suffix = "Izq",
  right.plot.suffix = "Der",
  sampling.time = 0.4,
  time.release = 0,#5 segundos antes de la caida
  baseline.initial.time = -10.0,#5 segundos antes de la caida
  baseline.final.time = time.release,
  min.ABP.max.delta.time = 20 * 0.8,#a partir de time.release busca el minimo
  min.CBFV.max.delta.time = 20 * 0.8,#a partir de time.release busca el minimo
  stabilisation.time = 20,#cuanto dura la señal para que se recupere nuevamente
  referential.time.instant = time.release,
  delta.time.before.ref = 0,
  delta.time.after.ref = round(floor(20 * 0.8 / sampling.time) * sampling.time, 1),
  comparison.function = get.MSE,
  fitting.value.name = "MSE",
  fitting.value.rounding.digits = 4,
  at.param.rounding.digits = 6,
  time.tol = sampling.time / 100,
  indent = ""
      )
{
  # Makes sure the target directory exists
  dir.create(
    path = tgt.dir,
    showWarnings = FALSE,
    recursive = TRUE,
    mode = "0711"
  )
  
  next.indent <- paste0(indent, "  ")
  
  # 
  # Results by manoeuvre (English format)
  # 
  
  cat(
    indent,
    "-- CSV file with stats by manoeuvre (English)\n",
    sep = ""
  )
  tgt.basename <- paste("manoeuvres", tgt.suffix, sep = "-")
  tgt.csv.name <- paste(tgt.basename, tgt.ext, sep = ".")
  tgt.csv.filename <- file.path(tgt.dir, tgt.csv.name)
  
  # If the target CSV file exists and it should not be overwritten
  if(all(file.exists(tgt.csv.filename), !overwrite))
  {
    cat(
      next.indent,
      "-- Target CSV file already exist and not overwritten...\n",
      next.indent,
      "   Using these data for any other missing file.\n",
      sep = ""
    )
    stats.by.mvre <- read.csv(tgt.csv.filename)
  }
  else
  {
    stats.by.mvre <- get.stats.by.mvre(
      src.dir = src.dir,
      src.ext = src.ext,
      header = header,
      subjects = subjects,
      manoeuvres = manoeuvres,
      time.col.name = time.col.name,
      abp.col.name = abp.col.name,
      left.cbfv.col.name = left.cbfv.col.name,
      right.cbfv.col.name = right.cbfv.col.name,
      left.plot.suffix = left.plot.suffix,
      right.plot.suffix = right.plot.suffix,
      sampling.time = sampling.time,
      time.release = time.release,
      baseline.initial.time = baseline.initial.time,
      baseline.final.time = baseline.final.time,
      min.ABP.max.delta.time = min.ABP.max.delta.time,
      min.CBFV.max.delta.time = min.CBFV.max.delta.time,
      stabilisation.time = stabilisation.time,
      referential.time.instant = referential.time.instant,
      delta.time.before.ref = delta.time.before.ref,
      delta.time.after.ref = delta.time.after.ref,
      comparison.function = comparison.function,
      fitting.value.name = fitting.value.name,
      fitting.value.rounding.digits = fitting.value.rounding.digits,
      at.param.rounding.digits = at.param.rounding.digits,
      time.tol = time.tol,
      indent = next.indent
    )
    write.csv(stats.by.mvre, file = tgt.csv.filename, row.names = FALSE)
    cat(next.indent, " CSV file created.\n", sep = "")
  }
  
  # 
  # Results by manoeuvre (Spanish format)
  # 
  
  cat(
    indent,
    "-- CSV file with stats by manoeuvre (Spanish)\n",
    sep = ""
  )
  tgt.basename <- paste("maniobras", tgt.suffix, sep = "-")
  tgt.csv.name <- paste(tgt.basename, tgt.ext, sep = ".")
  tgt.csv.filename <- file.path(tgt.dir, tgt.csv.name)
  
  # If the target CSV file exists and it should not be overwritten
  if(all(file.exists(tgt.csv.filename), !overwrite))
    cat(
      next.indent,
      "-- Target CSV file already exist and not overwritten\n",
      sep = ""
    )
  else
  {
    english <- c("Subject", "Manoeuvre", "Side")
    spanish <- c("Sujeto", "Maniobra", "Hemisferio")
    col.names <- mapply(gsub, english, spanish, colnames(stats.by.mvre))
    stats.by.mvre.es <- stats.by.mvre
    colnames(stats.by.mvre.es) <- col.names
    write.csv2(
      x = stats.by.mvre.es,
      file = tgt.csv.filename,
      row.names = FALSE
    )
    cat(next.indent, " CSV file created.\n", sep = "")
  }

  # 
  # Gets stats by subject
  # 
  
  cat(indent, "-- Getting dARI stats by subject\n", sep = "")
  subject.dari.stats <- get.stats.by.subject(stats.by.mvre, "dARI")
  
  cat(indent, "-- Getting mfARI stats by subject\n", sep = "")
  subject.mfari.stats <- get.stats.by.subject(stats.by.mvre, "mfARI")
  
  # 
  # Results by subject: dARI (English format)
  # 
  
  cat(
    indent,
    "-- CSV file with dARI stats by subject (English)\n",
    sep = ""
  )
  tgt.basename <- paste("subjects", "dARI", tgt.suffix, sep = "-")
  tgt.csv.name <- paste(tgt.basename, tgt.ext, sep = ".")
  tgt.csv.filename <- file.path(tgt.dir, tgt.csv.name)
  
  # If the target CSV file exists and it should not be overwritten
  if(all(file.exists(tgt.csv.filename), !overwrite))
    cat(
      next.indent,
      "-- Target CSV file already exist and not overwritten\n",
      sep = ""
    )
  else
  {
    write.csv(
      x = subject.dari.stats,
      file = tgt.csv.filename,
      row.names = FALSE
    )
    cat(next.indent, " CSV file created.\n", sep = "")
  }
  
  # 
  # Results by subject: mfARI (English format)
  # 
  
  cat(
    indent,
    "-- CSV file with mfARI stats by subject (English)\n",
    sep = ""
  )
  tgt.basename <- paste("subjects", "mfARI", tgt.suffix, sep = "-")
  tgt.csv.name <- paste(tgt.basename, tgt.ext, sep = ".")
  tgt.csv.filename <- file.path(tgt.dir, tgt.csv.name)
  
  # If the target CSV file exists and it should not be overwritten
  if(all(file.exists(tgt.csv.filename), !overwrite))
    cat(
      next.indent,
      "-- Target CSV file already exist and not overwritten\n",
      sep = ""
    )
  else
  {
    write.csv(
      x = subject.mfari.stats,
      file = tgt.csv.filename,
      row.names = FALSE
    )
    cat(next.indent, " CSV file created.\n", sep = "")
  }
  
  # 
  # Results by subject: dARI (Spanish format)
  # 
  
  cat(
    indent,
    "-- CSV file with dARI stats by subject (Spanish)\n",
    sep = ""
  )
  tgt.basename <- paste("sujetos", "dARI", tgt.suffix, sep = "-")
  tgt.csv.name <- paste(tgt.basename, tgt.ext, sep = ".")
  tgt.csv.filename <- file.path(tgt.dir, tgt.csv.name)
  
  # If the target CSV file exists and it should not be overwritten
  if(all(file.exists(tgt.csv.filename), !overwrite))
    cat(
      next.indent,
      "-- Target CSV file already exist and not overwritten\n",
      sep = ""
    )
  else
  {
    english <- c("Subject", "Side")
    spanish <- c("Sujeto", "Hemisferio")
    col.names <- mapply(
      FUN = gsub,
      english,
      spanish,
      colnames(subject.dari.stats)
    )
    subject.dari.stats.es <- subject.dari.stats
    colnames(subject.dari.stats.es) <- col.names
    write.csv2(
      x = subject.dari.stats.es,
      file = tgt.csv.filename,
      row.names = FALSE
    )
    cat(next.indent, " CSV file created.\n", sep = "")
  }
  
    
  stats.by.mvre
}
<file_sep><<<<<<< HEAD
<<<<<<< HEAD
import numpy
import pandas
import statsmodels.formula.api as smf 
import statsmodels.stats.multicomp as multi
import scipy.stats
import seaborn
import matplotlib.pyplot as plt
data = pandas.read_csv(‘addhealth_pds.csv’, low_memory=False)
data['H1FS11’]=data['H1FS11’].replace(0, numpy.nan)
data['H1ED12’]=data['H1ED12’].replace(0, numpy.nan)
data['H1FS11’] = pandas.to_numeric(data['H1FS11’], errors='coerce’)
data['H1ED12’] = pandas.to_numeric(data['H1ED12’], errors='coerce’)
ct=pandas.crosstab(data['H1FS11’], data['H1ED12’])
print (ct)
# column percentages
colsum=ct.sum(axis=0)
colpct=ct/colsum
print(colpct)
# chi-square
cs= scipy.stats.chi2_contingency(ct)
print ('chi-square value %f’%cs[0])
print ('p value %f’%cs[1])
###################Post Hoc Analysis
sub = data.copy()
for a in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
   for b in range((a + 1),10):
       bb = b
       aa = a
       if b == 7:
           bb = 96
       if b == 8:
           bb = 97
       if b == 9:
           bb = 98
       if a == 7:
           aa = 96
       if a == 8:
           aa = 97
       print (’———————–’)
       recode1 = {aa: aa, bb: bb}
       sub['COMP1v2’]= data['H1ED12’].map(recode1)
       # contingency table of observed counts
       ct1=pandas.crosstab(sub['H1FS11’], sub['COMP1v2’])
       cs1= scipy.stats.chi2_contingency(ct1)
       if (cs1[0] > 3.84) & (cs1[1] < 0.003) :
           print ('chi-square value %f’%cs1[0])
           print ('p value %.2E’%cs1[1])
           print (ct1)
           # column percentages
           colsum=ct1.sum(axis=0)
           colpct=ct1/colsum
           print(colpct)
“’ The Best one
———————–
chi-square value 1150.361891
p value 4.31E-249
COMP1v2  1.0   96.0
H1FS11             
1.0       237     1
2.0       605     0
3.0       687     0
6.0         0     3
COMP1v2      1.0   96.0
H1FS11                 
1.0      0.155003  0.25
2.0      0.395683  0.00
3.0      0.449313  0.00
6.0      0.000000  0.75
———————–
”’
################### Moderation Analysis
for a in range(1, 6):
   print ('How close do you feel to your mother? = %d’%a)
   sub2=data[(data['H1WP9’]==a)]
   ct=pandas.crosstab(sub2['H1FS11’], sub2['H1ED12’])
   colsum=ct.sum(axis=0)
   colpct=ct/colsum
   # chi-square
   cs= scipy.stats.chi2_contingency(ct)
   print ('chi-square value %f’%cs[0])
   print ('p value %f’%cs[1])
=======
import numpy
import pandas
import statsmodels.formula.api as smf 
import statsmodels.stats.multicomp as multi
import scipy.stats
import seaborn
import matplotlib.pyplot as plt
data = pandas.read_csv(‘addhealth_pds.csv’, low_memory=False)
data['H1FS11’]=data['H1FS11’].replace(0, numpy.nan)
data['H1ED12’]=data['H1ED12’].replace(0, numpy.nan)
data['H1FS11’] = pandas.to_numeric(data['H1FS11’], errors='coerce’)
data['H1ED12’] = pandas.to_numeric(data['H1ED12’], errors='coerce’)
ct=pandas.crosstab(data['H1FS11’], data['H1ED12’])
print (ct)
# column percentages
colsum=ct.sum(axis=0)
colpct=ct/colsum
print(colpct)
# chi-square
cs= scipy.stats.chi2_contingency(ct)
print ('chi-square value %f’%cs[0])
print ('p value %f’%cs[1])
###################Post Hoc Analysis
sub = data.copy()
for a in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
   for b in range((a + 1),10):
       bb = b
       aa = a
       if b == 7:
           bb = 96
       if b == 8:
           bb = 97
       if b == 9:
           bb = 98
       if a == 7:
           aa = 96
       if a == 8:
           aa = 97
       print (’———————–’)
       recode1 = {aa: aa, bb: bb}
       sub['COMP1v2’]= data['H1ED12’].map(recode1)
       # contingency table of observed counts
       ct1=pandas.crosstab(sub['H1FS11’], sub['COMP1v2’])
       cs1= scipy.stats.chi2_contingency(ct1)
       if (cs1[0] > 3.84) & (cs1[1] < 0.003) :
           print ('chi-square value %f’%cs1[0])
           print ('p value %.2E’%cs1[1])
           print (ct1)
           # column percentages
           colsum=ct1.sum(axis=0)
           colpct=ct1/colsum
           print(colpct)
“’ The Best one
———————–
chi-square value 1150.361891
p value 4.31E-249
COMP1v2  1.0   96.0
H1FS11             
1.0       237     1
2.0       605     0
3.0       687     0
6.0         0     3
COMP1v2      1.0   96.0
H1FS11                 
1.0      0.155003  0.25
2.0      0.395683  0.00
3.0      0.449313  0.00
6.0      0.000000  0.75
———————–
”’
################### Moderation Analysis
for a in range(1, 6):
   print ('How close do you feel to your mother? = %d’%a)
   sub2=data[(data['H1WP9’]==a)]
   ct=pandas.crosstab(sub2['H1FS11’], sub2['H1ED12’])
   colsum=ct.sum(axis=0)
   colpct=ct/colsum
   # chi-square
   cs= scipy.stats.chi2_contingency(ct)
   print ('chi-square value %f’%cs[0])
   print ('p value %f’%cs[1])
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
import numpy
import pandas
import statsmodels.formula.api as smf 
import statsmodels.stats.multicomp as multi
import scipy.stats
import seaborn
import matplotlib.pyplot as plt
data = pandas.read_csv(‘addhealth_pds.csv’, low_memory=False)
data['H1FS11’]=data['H1FS11’].replace(0, numpy.nan)
data['H1ED12’]=data['H1ED12’].replace(0, numpy.nan)
data['H1FS11’] = pandas.to_numeric(data['H1FS11’], errors='coerce’)
data['H1ED12’] = pandas.to_numeric(data['H1ED12’], errors='coerce’)
ct=pandas.crosstab(data['H1FS11’], data['H1ED12’])
print (ct)
# column percentages
colsum=ct.sum(axis=0)
colpct=ct/colsum
print(colpct)
# chi-square
cs= scipy.stats.chi2_contingency(ct)
print ('chi-square value %f’%cs[0])
print ('p value %f’%cs[1])
###################Post Hoc Analysis
sub = data.copy()
for a in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
   for b in range((a + 1),10):
       bb = b
       aa = a
       if b == 7:
           bb = 96
       if b == 8:
           bb = 97
       if b == 9:
           bb = 98
       if a == 7:
           aa = 96
       if a == 8:
           aa = 97
       print (’———————–’)
       recode1 = {aa: aa, bb: bb}
       sub['COMP1v2’]= data['H1ED12’].map(recode1)
       # contingency table of observed counts
       ct1=pandas.crosstab(sub['H1FS11’], sub['COMP1v2’])
       cs1= scipy.stats.chi2_contingency(ct1)
       if (cs1[0] > 3.84) & (cs1[1] < 0.003) :
           print ('chi-square value %f’%cs1[0])
           print ('p value %.2E’%cs1[1])
           print (ct1)
           # column percentages
           colsum=ct1.sum(axis=0)
           colpct=ct1/colsum
           print(colpct)
“’ The Best one
———————–
chi-square value 1150.361891
p value 4.31E-249
COMP1v2  1.0   96.0
H1FS11             
1.0       237     1
2.0       605     0
3.0       687     0
6.0         0     3
COMP1v2      1.0   96.0
H1FS11                 
1.0      0.155003  0.25
2.0      0.395683  0.00
3.0      0.449313  0.00
6.0      0.000000  0.75
———————–
”’
################### Moderation Analysis
for a in range(1, 6):
   print ('How close do you feel to your mother? = %d’%a)
   sub2=data[(data['H1WP9’]==a)]
   ct=pandas.crosstab(sub2['H1FS11’], sub2['H1ED12’])
   colsum=ct.sum(axis=0)
   colpct=ct/colsum
   # chi-square
   cs= scipy.stats.chi2_contingency(ct)
   print ('chi-square value %f’%cs[0])
   print ('p value %f’%cs[1])
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep><<<<<<< HEAD
<<<<<<< HEAD
library(bnlearn)
library(plotly)
packageVersion('plotly')
source('http://bioconductor.org/biocLite.R')
biocLite('Rgraphviz')
library(xlsx)

data.alarm <- data.frame(alarm)
# create and plot the network structure.
modelstring = paste("[HIST|LVF][CVP|LVV][PCWP|LVV][HYP][LVV|HYP:LVF]",
  "[LVF][STKV|HYP:LVF][ERLO][HRBP|ERLO:HR][HREK|ERCA:HR][ERCA]",
  "[HRSA|ERCA:HR][ANES][APL][TPR|APL][ECO2|ACO2:VLNG][KINK]",
  "[MINV|INT:VLNG][FIO2][PVS|FIO2:VALV][SAO2|PVS:SHNT][PAP|PMB][PMB]",
  "[SHNT|INT:PMB][INT][PRSS|INT:KINK:VTUB][DISC][MVS][VMCH|MVS]",
  "[VTUB|DISC:VMCH][VLNG|INT:KINK:VTUB][VALV|INT:VLNG][ACO2|VALV]",
  "[CCHL|ACO2:ANES:SAO2:TPR][HR|CCHL][CO|HR:STKV][BP|CO:TPR]", sep = "")
dag = model2network(modelstring)


#se comparan los 3 metodos
set.seed(6)
res = hc(data.alarm)##MEJOR MODELO
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#22 31 24 
#BIC: -220761.7

set.seed(6)
res = mmhc(data.alarm)
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#16 13 30 
#BIC: -311189.8

set.seed(6)
res = mmpc(data.alarm)
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#0 29 46
#the graph is only partially directed

tp <- c()
Restart <- c()
Seed <- c()

maximoTp = 0
bestRest = 0;
bestSeed = 0;
for(seed in 1:500){
  for(i in 1:100){
	if((seed%%10) == 0 & (i%%10) == 0){
		set.seed(seed)
		res = hc(data.alarm,restart = i)
		tph = unlist(compare(dag, res))["tp"]
		
		tp <- c(tp, tph)
		Restart <- c(Restart, i)
		Seed <- c(Seed, seed)

		if(maximoTp < tph){
		  maximoTp = tph
		  bestRest = i
		  bestSeed = seed
		}
	}
  }
}


##Grafico Curva Arendizaje

datos <- data.frame(Restart, Seed, tp, co = as.numeric(c(1:length(Restart))))
setwd("C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\Trabajos\\4")
write.xlsx(datos, "Ajuste_TP.xlsx", sheetName="Sheet1")

p <- plot_ly(datos, x = ~Restart, y = ~Seed, z = ~tp, type = 'scatter3d', mode = 'lines',
        line = list(color = '#1f77b4', width = 1))
		
p
		


##MEJOR MODELO
set.seed(bestSeed)#Seed=40
res = hc(data.alarm,restart = bestRest)#Restart=90
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#34 17 12 
#BIC: -218690.1

##Grafico de la mejor red
graphviz.plot(res)

###################################################################33CONSULTAS ORACULO
fittedbn <- bn.fit(res, data = data.alarm)

#¿Qué tipo de entubación se le suministra a pacientes con anafilaxis? (PPT 15)
set.seed(6)
cpquery(fittedbn, event = (INT=="NORMAL"), evidence = (APL=="TRUE") )
#0.9194631

#¿Cuál es la probabilidad de que el paciente padezca de embolia pulmonar (PMB) si la catecolamina es alta(CCHL) y el ritmo cardiaco es alto (HR)?
#PPT 16
set.seed(6)
cpquery(fittedbn, event = (HR=="HIGH" & CCHL == "HIGH"), evidence = (PMB=="TRUE") )
#0.6923077

#¿Cuál es la probabilidad de que el paciente padezca de embolia pulmonar (PMB) si la catecolamina es alta(CCHL), si el ritmo cardiaco es alto (HR) y el CO es alto?
#PPT 17
set.seed(6)
cpquery(fittedbn, event = (HR=="HIGH" & CCHL == "HIGH" & CO == "HIGH"), evidence = (PMB=="TRUE") )
#0.5605096

#¿Cuál es la probabilidad de que un paciente con ritmo cardiaco alto, catecolamina alta e irrigación sanguínea pulmonar baja padezca de anafilaxis?
#PPT 18
set.seed(6)
cpquery(fittedbn, event = (HR =="HIGH" & CCHL =="HIGH" & TPR == "LOW"), evidence = (APL=="TRUE") )
#0.8636364




=======
library(bnlearn)
library(plotly)
packageVersion('plotly')
source('http://bioconductor.org/biocLite.R')
biocLite('Rgraphviz')
library(xlsx)

data.alarm <- data.frame(alarm)
# create and plot the network structure.
modelstring = paste("[HIST|LVF][CVP|LVV][PCWP|LVV][HYP][LVV|HYP:LVF]",
  "[LVF][STKV|HYP:LVF][ERLO][HRBP|ERLO:HR][HREK|ERCA:HR][ERCA]",
  "[HRSA|ERCA:HR][ANES][APL][TPR|APL][ECO2|ACO2:VLNG][KINK]",
  "[MINV|INT:VLNG][FIO2][PVS|FIO2:VALV][SAO2|PVS:SHNT][PAP|PMB][PMB]",
  "[SHNT|INT:PMB][INT][PRSS|INT:KINK:VTUB][DISC][MVS][VMCH|MVS]",
  "[VTUB|DISC:VMCH][VLNG|INT:KINK:VTUB][VALV|INT:VLNG][ACO2|VALV]",
  "[CCHL|ACO2:ANES:SAO2:TPR][HR|CCHL][CO|HR:STKV][BP|CO:TPR]", sep = "")
dag = model2network(modelstring)


#se comparan los 3 metodos
set.seed(6)
res = hc(data.alarm)##MEJOR MODELO
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#22 31 24 
#BIC: -220761.7

set.seed(6)
res = mmhc(data.alarm)
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#16 13 30 
#BIC: -311189.8

set.seed(6)
res = mmpc(data.alarm)
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#0 29 46
#the graph is only partially directed

tp <- c()
Restart <- c()
Seed <- c()

maximoTp = 0
bestRest = 0;
bestSeed = 0;
for(seed in 1:500){
  for(i in 1:100){
	if((seed%%10) == 0 & (i%%10) == 0){
		set.seed(seed)
		res = hc(data.alarm,restart = i)
		tph = unlist(compare(dag, res))["tp"]
		
		tp <- c(tp, tph)
		Restart <- c(Restart, i)
		Seed <- c(Seed, seed)

		if(maximoTp < tph){
		  maximoTp = tph
		  bestRest = i
		  bestSeed = seed
		}
	}
  }
}


##Grafico Curva Arendizaje

datos <- data.frame(Restart, Seed, tp, co = as.numeric(c(1:length(Restart))))
setwd("C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\Trabajos\\4")
write.xlsx(datos, "Ajuste_TP.xlsx", sheetName="Sheet1")

p <- plot_ly(datos, x = ~Restart, y = ~Seed, z = ~tp, type = 'scatter3d', mode = 'lines',
        line = list(color = '#1f77b4', width = 1))
		
p
		


##MEJOR MODELO
set.seed(bestSeed)#Seed=40
res = hc(data.alarm,restart = bestRest)#Restart=90
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#34 17 12 
#BIC: -218690.1

##Grafico de la mejor red
graphviz.plot(res)

###################################################################33CONSULTAS ORACULO
fittedbn <- bn.fit(res, data = data.alarm)

#¿Qué tipo de entubación se le suministra a pacientes con anafilaxis? (PPT 15)
set.seed(6)
cpquery(fittedbn, event = (INT=="NORMAL"), evidence = (APL=="TRUE") )
#0.9194631

#¿Cuál es la probabilidad de que el paciente padezca de embolia pulmonar (PMB) si la catecolamina es alta(CCHL) y el ritmo cardiaco es alto (HR)?
#PPT 16
set.seed(6)
cpquery(fittedbn, event = (HR=="HIGH" & CCHL == "HIGH"), evidence = (PMB=="TRUE") )
#0.6923077

#¿Cuál es la probabilidad de que el paciente padezca de embolia pulmonar (PMB) si la catecolamina es alta(CCHL), si el ritmo cardiaco es alto (HR) y el CO es alto?
#PPT 17
set.seed(6)
cpquery(fittedbn, event = (HR=="HIGH" & CCHL == "HIGH" & CO == "HIGH"), evidence = (PMB=="TRUE") )
#0.5605096

#¿Cuál es la probabilidad de que un paciente con ritmo cardiaco alto, catecolamina alta e irrigación sanguínea pulmonar baja padezca de anafilaxis?
#PPT 18
set.seed(6)
cpquery(fittedbn, event = (HR =="HIGH" & CCHL =="HIGH" & TPR == "LOW"), evidence = (APL=="TRUE") )
#0.8636364




>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
library(bnlearn)
library(plotly)
packageVersion('plotly')
source('http://bioconductor.org/biocLite.R')
biocLite('Rgraphviz')
library(xlsx)

data.alarm <- data.frame(alarm)
# create and plot the network structure.
modelstring = paste("[HIST|LVF][CVP|LVV][PCWP|LVV][HYP][LVV|HYP:LVF]",
  "[LVF][STKV|HYP:LVF][ERLO][HRBP|ERLO:HR][HREK|ERCA:HR][ERCA]",
  "[HRSA|ERCA:HR][ANES][APL][TPR|APL][ECO2|ACO2:VLNG][KINK]",
  "[MINV|INT:VLNG][FIO2][PVS|FIO2:VALV][SAO2|PVS:SHNT][PAP|PMB][PMB]",
  "[SHNT|INT:PMB][INT][PRSS|INT:KINK:VTUB][DISC][MVS][VMCH|MVS]",
  "[VTUB|DISC:VMCH][VLNG|INT:KINK:VTUB][VALV|INT:VLNG][ACO2|VALV]",
  "[CCHL|ACO2:ANES:SAO2:TPR][HR|CCHL][CO|HR:STKV][BP|CO:TPR]", sep = "")
dag = model2network(modelstring)


#se comparan los 3 metodos
set.seed(6)
res = hc(data.alarm)##MEJOR MODELO
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#22 31 24 
#BIC: -220761.7

set.seed(6)
res = mmhc(data.alarm)
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#16 13 30 
#BIC: -311189.8

set.seed(6)
res = mmpc(data.alarm)
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#0 29 46
#the graph is only partially directed

tp <- c()
Restart <- c()
Seed <- c()

maximoTp = 0
bestRest = 0;
bestSeed = 0;
for(seed in 1:500){
  for(i in 1:100){
	if((seed%%10) == 0 & (i%%10) == 0){
		set.seed(seed)
		res = hc(data.alarm,restart = i)
		tph = unlist(compare(dag, res))["tp"]
		
		tp <- c(tp, tph)
		Restart <- c(Restart, i)
		Seed <- c(Seed, seed)

		if(maximoTp < tph){
		  maximoTp = tph
		  bestRest = i
		  bestSeed = seed
		}
	}
  }
}


##Grafico Curva Arendizaje

datos <- data.frame(Restart, Seed, tp, co = as.numeric(c(1:length(Restart))))
setwd("C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\Trabajos\\4")
write.xlsx(datos, "Ajuste_TP.xlsx", sheetName="Sheet1")

p <- plot_ly(datos, x = ~Restart, y = ~Seed, z = ~tp, type = 'scatter3d', mode = 'lines',
        line = list(color = '#1f77b4', width = 1))
		
p
		


##MEJOR MODELO
set.seed(bestSeed)#Seed=40
res = hc(data.alarm,restart = bestRest)#Restart=90
unlist(compare(dag, res))
sc<-score(res,data.alarm)
print(sc)
#tp fp fn 
#34 17 12 
#BIC: -218690.1

##Grafico de la mejor red
graphviz.plot(res)

###################################################################33CONSULTAS ORACULO
fittedbn <- bn.fit(res, data = data.alarm)

#¿Qué tipo de entubación se le suministra a pacientes con anafilaxis? (PPT 15)
set.seed(6)
cpquery(fittedbn, event = (INT=="NORMAL"), evidence = (APL=="TRUE") )
#0.9194631

#¿Cuál es la probabilidad de que el paciente padezca de embolia pulmonar (PMB) si la catecolamina es alta(CCHL) y el ritmo cardiaco es alto (HR)?
#PPT 16
set.seed(6)
cpquery(fittedbn, event = (HR=="HIGH" & CCHL == "HIGH"), evidence = (PMB=="TRUE") )
#0.6923077

#¿Cuál es la probabilidad de que el paciente padezca de embolia pulmonar (PMB) si la catecolamina es alta(CCHL), si el ritmo cardiaco es alto (HR) y el CO es alto?
#PPT 17
set.seed(6)
cpquery(fittedbn, event = (HR=="HIGH" & CCHL == "HIGH" & CO == "HIGH"), evidence = (PMB=="TRUE") )
#0.5605096

#¿Cuál es la probabilidad de que un paciente con ritmo cardiaco alto, catecolamina alta e irrigación sanguínea pulmonar baja padezca de anafilaxis?
#PPT 18
set.seed(6)
cpquery(fittedbn, event = (HR =="HIGH" & CCHL =="HIGH" & TPR == "LOW"), evidence = (APL=="TRUE") )
#0.8636364




>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep>
require(ggplot2)
require(gridExtra)
require(pracma)
require(signal)



# This function obtains the CBFV response to an ABP stimulus
# according to the Aaslid-Tiecks (A-T) model with the specified
# parameters.
# The function receives the following arguments:
# 
# T, D and K: the model parameters.
#             Default value: values that generates a response with
#             A-T ARI = 5.
#
# time.instants: the time instants in which the input ABP stimulus was
#                sampled.
#
# ABP.normalised: the normalised ABP stimulus to be used as input to the
#                 A-T model.
#
# sampling.time: the sampling time used in the input ABP stimulus.
#                Default value: the smallest difference between time
#                instants (rounded to 3 decimal places - max. 1000 Hz).
#
# stabilisation.time: the time to be used for stabilisation of the model,
#                     before and after the input stimulus.
#                     Default value: 1 s
# 
# The function's answer is a list with the following:
# .$T, .$D, .$K: the parameters of the model used
# .$time.instants: a copy of the specified time instants.
# .$sampling.time: a copy of the specified sampling time of the signals.
# .$ABP.normalised: the specified normalised ABP stimulus.
# .$CBFV.theoretical.response: the CBFV response given by the A-T model
#                              with the specified parameters to the given
#                              ABP stimulus.
#
get.theoretical.CBFV.response <- function(
      T = 1.9,
      D = 0.75,
      K = 0.9,
      time.instants,
      ABP.normalised,
      sampling.time = min(round(diff(time.instants), 3)),
      stabilisation.time = 1
      )
{
  # Initialises the answer
  ans <- list()
  ans[["T"]] <- T
  ans[["D"]] <- D
  ans[["K"]] <- K
  ans[["time.instants"]] <- time.instants
	ans[["sampling.time"]] <- sampling.time
  ans[["ABP.normalised"]] <- ABP.normalised
  
	frequency <- 1 / ans[["sampling.time"]]
	nsamples <- length(ans[["time.instants"]])
	nsamples.stabilisation <-
    round(stabilisation.time / ans[["sampling.time"]])
  
  P <- c(
    rep(ans[["ABP.normalised"]][1], nsamples.stabilisation),
	  ans[["ABP.normalised"]],
		rep(ans[["ABP.normalised"]][nsamples], nsamples.stabilisation)
  )
	
  # Gets dP
  dP <- P - 1
  
  # Applies Tiecks' equations to obtain the CBFV signal
  X1 <- vector(mode = "numeric", length = length(P))
  X2 <- vector(mode = "numeric", length = length(P))
  CBFV <- vector(mode = "numeric", length = length(P))
  
  divisor <- frequency * ans[["T"]]
  X1[1] <- 0
  X2[1] <- 0
  CBFV[1] <- 1
  for(t in 2:length(P))
  {
    X1[t] <- X1[t-1] + (dP[t] - X2[t-1]) / divisor
    X2[t] <- X2[t-1] + (X1[t] - 2 * ans[["D"]] * X2[t-1]) / divisor
    CBFV[t] <- 1 + dP[t] - ans[["K"]] * X2[t]
  }
	
  if(nsamples.stabilisation > 0)
    CBFV <- CBFV[-(1:nsamples.stabilisation)]
	CBFV <- CBFV[1:nsamples]
	ans[["CBFV.theoretical.response"]] <- CBFV
  
  invisible(ans)
}


# This function creates a normalised ABP step stimulus and obtains the
# CBFV response according to the Aaslid-Tiecks (A-T) model with a
# specific level of efficiency.
# The function receives the following arguments:
# 
# sampling.time,
# time.until.release,
# time.after.release,
# smooth.step.stimulus,
# filter.order,
# cutoff.frequency,
# left.stabilisation.time,
# time.rounding.digits: the parameters required to generate a normalised
#                       ABP stimulus.
#                       Default values: as in function
#                       get.normalised.ABP.stimulus()
#
# T, D, K, stabilisation.time: the parameters necessary to obtain the
#                              CBFV response to the ABP stimulus
#                              generated.
#                              Default values: as in function
#                              get.theoretical.CBFV.response()
# 
# The function's answer is a list with the following:
# .$T, .$D, .$K: the parameters of the model used
# .$time.instants: the specified time instants.
# .$sampling.time: the sampling time of the signals.
# .$ABP.normalised: the specified normalised ABP stimulus.
# .$CBFV.step.response: the CBFV response given by the model to the
#                       specified ABP stimulus.
# .$time.release: the time instant in which the thigh-cuffs are
#                 supposedly released
#
get.AT.curve <- function(
      sampling.time = 0.1,
      time.until.release = 10,
      time.after.release = 20,
      smooth.step.stimulus = FALSE,
      filter.order = 2,
      cutoff.frequency = 0.20,
      left.stabilisation.time = ifelse(smooth.step.stimulus, 30, 0),
      time.rounding.digits =  format.info(sampling.time)[2],
      T = 1.9,
      D = 0.75,
      K = 0.9,
      stabilisation.time = 1
      )
{
	ans.abp <- get.normalised.ABP.stimulus(
    sampling.time = sampling.time,
    time.until.release = time.until.release,
    time.after.release = time.after.release,
    smooth.step.stimulus = smooth.step.stimulus,
    filter.order = filter.order,
    cutoff.frequency = cutoff.frequency,
    left.stabilisation.time = left.stabilisation.time,
    time.rounding.digits = time.rounding.digits
  )
  
	ans.cbfv <- get.theoretical.CBFV.response(
    T = T,
    D = D,
    K = K,
    time.instants = ans.abp$time.instants,
    ABP.normalised = ans.abp$ABP.normalised,
    sampling.time = ans.abp$sampling.time,
    stabilisation.time = stabilisation.time
  )
  
	ans.cbfv[["CBFV.step.response"]] <-
    ans.cbfv[["CBFV.theoretical.response"]]
	ans.cbfv[["CBFV.theoretical.response"]] <- NULL
	ans.cbfv[["time.release"]] <- ans.abp[["time.release"]]
  
  invisible(ans.cbfv)
}


# This function creates a plot of the signals generated with an
# Aaslid-Tiecks (A-T) model.
# The function receives the following arguments:
# curve: a list with the A-T curve, as returned by the function
#        get.AT.curve()
# title: the title for the plot.
#        Default value: NULL
# Specifically:
# - plots curve$ABP.normalised and curve$CBFV.step.response signals using
#   curve$time.instants as x-axis
# - annotates the plot with the values of curve$T, curve$D, curve$K and
#   curve$sampling.time
# - annotates the plot with a vertical grey line marking the time of
#   thigh-cuff release
# - uses the argument title (if specified) as the plot's title
# 
# The function returns the resulting plot
#
get.AT.curve.plot <- function(curve, title = NULL)
{
  tab <- data.frame(
    Parameters = c(
      curve[["T"]],
      curve[["D"]],
      curve[["K"]],
      curve[["sampling.time"]]
  ))
  tab[["Parameters"]] <- format(tab[["Parameters"]])
  g <- tableGrob(tab, gp = gpar(fontsize = 5))
  xrange <- range(curve[["time.instants"]])
  xrange <- (xrange[2] - xrange[1]) / 5
  yrange <- 0.5
  
  df1 <- data.frame(
    Time = curve[["time.instants"]],
    Value = curve[["ABP.normalised"]],
    Signal = "ABP"
  )
  df2 <- data.frame(
    Time = curve[["time.instants"]],
    Value = curve[["CBFV.step.response"]],
    Signal = "CBFV"
  )
  df <- rbind(df1, df2)
  p <- ggplot(data = df, aes(x = Time, y = Value, colour = Signal))
  p <- p + geom_line() + geom_point()
  p <- p + geom_vline(xintercept = curve[["time.release"]], colour = "grey")  
  
  n <- length(curve[["time.instants"]])
  xmax <- curve[["time.instants"]][n]
  y <- curve[["CBFV.step.response"]][n]
  if(y > yrange)
    y <- c(0, yrange)
  else
    y <- c(yrange, 2 * yrange)
  p <- p + annotation_custom(
    g,
    xmin = xmax - xrange,
    xmax = xmax,
    ymin = y[1],
    ymax = y[2]
  )
  
  if(!is.null(title))
    p <- p + labs(title = title, x = "Time [s]", y = "Signals [a.u.]")
  else
    p <- p + labs(x = "Time [s]", y = "Signals [a.u.]")
  
  p
}


get.AT.templates.parameters <- function()
{
  K <- c(0.00, 0.20, 0.40, 0.60, 0.80, 0.90, 0.94, 0.96, 0.97, 0.98)
  D <- c(1.60, 1.60, 1.50, 1.15, 0.90, 0.75, 0.65, 0.55, 0.52, 0.50)
  T <- c(2.00, 2.00, 2.00, 2.00, 2.00, 1.90, 1.60, 1.20, 0.87, 0.65)
  ARI <- 0:9
  
  data.frame(T, D, K, ARI)
}


get.AT.templates <- function(
      sampling.time = 0.1,
      time.until.release = 10,
      time.after.release = 20,
      smooth.step.stimulus = FALSE,
      filter.order = 2,
      cutoff.frequency = 0.20,
      left.stabilisation.time = ifelse(smooth.step.stimulus, 30, 0),
      time.rounding.digits =  format.info(sampling.time)[2],
      stabilisation.time = 1
      )
{
	ans.abp <- get.normalised.ABP.stimulus(
    sampling.time = sampling.time,
    time.until.release = time.until.release,
    time.after.release = time.after.release,
    smooth.step.stimulus = smooth.step.stimulus,
    filter.order = filter.order,
    cutoff.frequency = cutoff.frequency,
    left.stabilisation.time = left.stabilisation.time,
    time.rounding.digits = time.rounding.digits
  )
  params <- get.AT.templates.parameters()
   
  .tmp.fun <- function(row)
    get.theoretical.CBFV.response(
      T = row[["T"]],
      D = row[["D"]],
      K = row[["K"]],
      time.instants = ans.abp$time.instants,
      ABP.normalised = ans.abp$ABP.normalised,
      sampling.time = ans.abp$sampling.time,
      stabilisation.time = stabilisation.time
    )
  templates <- apply(params, 1, .tmp.fun)
  
  .tmp.fun <- function(ans) {
    ans[["CBFV.step.response"]] <- ans[["CBFV.theoretical.response"]]
    ans[["CBFV.theoretical.response"]] <- NULL
    ans[["time.release"]] <- ans.abp[["time.release"]]
    ans
  }
  templates <- lapply(templates, .tmp.fun)
  
  .tmp.fun <- function(i) {
    templates[[i]][["ARI.value"]] <- params[i, "ARI"]
    templates[[i]]
  }
  templates <- lapply(1:length(templates), .tmp.fun)
  
  names(templates) <- paste("ARI", params[["ARI"]], sep = " = ")
  
  templates
}


get.AT.decimal.templates <- function(
      sampling.time = 0.1,
      time.until.release = 10,
      time.after.release = 20,
      smooth.step.stimulus = FALSE,
      filter.order = 2,
      cutoff.frequency = 0.20,
      left.stabilisation.time = ifelse(smooth.step.stimulus, 30, 0),
      time.rounding.digits =  format.info(sampling.time)[2],
      stabilisation.time = 1,
      rounding.digits = 6
      )
{
	ans.abp <- get.normalised.ABP.stimulus(
    sampling.time = sampling.time,
    time.until.release = time.until.release,
    time.after.release = time.after.release,
    smooth.step.stimulus = smooth.step.stimulus,
    filter.order = filter.order,
    cutoff.frequency = cutoff.frequency,
    left.stabilisation.time = left.stabilisation.time,
    time.rounding.digits = time.rounding.digits
  )
  params <- get.AT.decimal.templates.parameters(
    rounding.digits = rounding.digits
  )
  
  .tmp.fun <- function(row)
    get.theoretical.CBFV.response(
      T = row[["T"]],
      D = row[["D"]],
      K = row[["K"]],
      time.instants = ans.abp$time.instants,
      ABP.normalised = ans.abp$ABP.normalised,
      sampling.time = ans.abp$sampling.time,
      stabilisation.time = stabilisation.time
    )
  templates <- apply(params, 1, .tmp.fun)
  
  .tmp.fun <- function(ans) {
    ans[["CBFV.step.response"]] <- ans[["CBFV.theoretical.response"]]
    ans[["CBFV.theoretical.response"]] <- NULL
    ans[["time.release"]] <- ans.abp[["time.release"]]
    ans
  }
  templates <- lapply(templates, .tmp.fun)
  
  .tmp.fun <- function(i) {
    templates[[i]][["ARI.value"]] <- params[i, "ARI"]
    templates[[i]]
  }
  templates <- lapply(1:length(templates), .tmp.fun)
  
  names(templates) <-
    paste("ARI", sprintf("%.1f", params[["ARI"]]), sep = " = ")
  
  templates
}


get.AT.decimal.templates.parameters <- function(rounding.digits = 6)
{
  orig <- get.AT.templates.parameters()
  
  ARI.decimal <- round(seq(0, 9, 0.1), 1)
  K.decimal <-
    pracma::interp1(orig[["ARI"]], orig[["K"]], ARI.decimal, 'spline')
  K.decimal <- round(K.decimal, rounding.digits)
  D.decimal <- 
    pracma::interp1(orig[["ARI"]], orig[["D"]], ARI.decimal, 'spline')
  D.decimal <- round(D.decimal, rounding.digits)
  T.decimal <-
    pracma::interp1(orig[["ARI"]], orig[["T"]], ARI.decimal, 'spline')
  T.decimal <- round(T.decimal, rounding.digits)
  
  data.frame(
    T = T.decimal,
    D = D.decimal,
    K = K.decimal,
    ARI = ARI.decimal
  )
}


# This function creates a plot with CBFV responses generated by
# Aaslid-Tiecks (A-T) models  (normally for the same ABP stimulus).
# The function receives the following argument:
# curves: a list in which each element is a list with an A-T curve as
#         returned by the function get.AT.curve()
# label.name: the name for the legend of the plot. If NULL, no legend is
#             added and curves are plotted in black.
#             Default value: NULL
# labels: a vector of the same length as curves, containing the label for
#         each curve. If label.name is not NULL, these labels are used as
#         the colour aesthetic and for labelling the curves in the legend.
#         Default value: NULL
# title: the title for the plot.
#        Default value: NULL
#
# The function returns the resulting plot
#
get.AT.curves.plot <- function(
      curves,
      label.name = NULL,
      labels = NULL,
      title = NULL
      )
{
  n <- length(curves)
  if(is.null(labels))
    labels <- 1:n
  group.name <- "Response"
  if(!is.null(label.name))
    group.name <- label.name
  
  times <- c(sapply(1:n, function(i) curves[[i]][["time.instants"]]))
  values <- c(sapply(1:n, function(i) curves[[i]][["CBFV.step.response"]]))
  lengths <- c(sapply(1:n, function(i) length(curves[[i]][["time.instants"]])))
  groups <- rep(labels, times = lengths)
  
  df <- data.frame(Time = times, CBFV = values, groups)
  names(df)[ncol(df)] <- group.name
  
  p <- ggplot(df, aes_string(x = "Time", y = "CBFV", group = group.name))
  if(is.null(label.name))
    p <- p + geom_line()
  else
    p <- p + geom_line(aes_string(colour = group.name))
  
  if(!is.null(title))
    p <- p + labs(title = title, x = "Time [s]", y = "CBFV responses [a.u.]")
  else
    p <- p + labs(x = "Time [s]", y = "CBFV responses [a.u.]")
  
  p
}




<file_sep><<<<<<< HEAD
<<<<<<< HEAD
"""
Created on Wed Oct 17 15:08:50 2018

@author: Luis.Orellana.EXT

Dataset: https://www.kaggle.com/dragonheir/logistic-regression
"""

import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.externals import joblib
from azureml.core import Workspace, Experiment, Run
import math, random, pickle
from azureml.core.compute import ComputeTarget


class Scal:
    #Escala de -1 a 1
    scaler = MinMaxScaler(feature_range=(0, 1))
    
    def __init__(self, X):
        self.X = X
        Scal.scaler.fit(X)

    def scaling(self):
        self.X_Scaled = Scal.scaler.transform(self.X)
        return self.X_Scaled
        
    def undo_scaling(self):
        X_un_Scaled = Scal.scaler.inverse_transform(self.X_Scaled)
        return X_un_Scaled

def transform(column):
    
    wcdict = {}
    
    wcdict['Male'] = 0
    wcdict['Female'] = 1
    
    col_num = pd.DataFrame(column).applymap(lambda s: wcdict.get(s) if s in wcdict else s)
    
    return col_num

def init():

    ws = Workspace.from_config()
    experiment = Experiment(workspace = ws, name = "Experiment_RL_VentaOnline")
    run = Run.get_context()
    ds = ws.get_default_datastore()
    
    X = pd.read_csv(r"./Python/Regresion Logistica - Venta Online/Social_Network_Ads.csv", sep=",")
    
    Y = X.Purchased
    X = X.drop('Purchased', axis=1)
    X = X.drop('User ID', axis=1)
    
    X.Gender = transform(X.Gender)

    scal = Scal(X)
    X = scal.scaling()
    #scal.undo_scaling()
    ###########################################################################################
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = .3, random_state=25)
    
    C_param_range = [0.001,0.01,0.1,1.0,10.0,100.0]

    run.log("Parameters",C_param_range)
    
    LogReg

    for i in C_param_range:
        LogReg = LogisticRegression(penalty = 'l2', C = i,random_state = 0)
        LogReg.fit(X_train, y_train)
        
        y_pred = LogReg.predict(X_test)
        
        
        matriz_confu = confusion_matrix(y_test, y_pred)
        print("------------------------------------------------------")
        print("Parametro: ", i)
        print(matriz_confu)
        print(classification_report(y_test, y_pred))
        print(accuracy_score(y_test, y_pred))
        print("------------------------------------------------------")
        run.log("accuracy",accuracy_score(y_test, y_pred))
        run.log("Parametro: ", i)

    joblib.dump(value=LogReg, filename='./Python/Regresion Logistica - Venta Online/outputs/LogReg.pkl')
    print("Run completed")

    run = experiment.submit()
=======
"""
Created on Wed Oct 17 15:08:50 2018

@author: Luis.Orellana.EXT

Dataset: https://www.kaggle.com/dragonheir/logistic-regression
"""

import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.externals import joblib
from azureml.core import Workspace, Experiment, Run
import math, random, pickle
from azureml.core.compute import ComputeTarget


class Scal:
    #Escala de -1 a 1
    scaler = MinMaxScaler(feature_range=(0, 1))
    
    def __init__(self, X):
        self.X = X
        Scal.scaler.fit(X)

    def scaling(self):
        self.X_Scaled = Scal.scaler.transform(self.X)
        return self.X_Scaled
        
    def undo_scaling(self):
        X_un_Scaled = Scal.scaler.inverse_transform(self.X_Scaled)
        return X_un_Scaled

def transform(column):
    
    wcdict = {}
    
    wcdict['Male'] = 0
    wcdict['Female'] = 1
    
    col_num = pd.DataFrame(column).applymap(lambda s: wcdict.get(s) if s in wcdict else s)
    
    return col_num

def init():

    ws = Workspace.from_config()
    experiment = Experiment(workspace = ws, name = "Experiment_RL_VentaOnline")
    run = Run.get_context()
    ds = ws.get_default_datastore()
    
    X = pd.read_csv(r"./Python/Regresion Logistica - Venta Online/Social_Network_Ads.csv", sep=",")
    
    Y = X.Purchased
    X = X.drop('Purchased', axis=1)
    X = X.drop('User ID', axis=1)
    
    X.Gender = transform(X.Gender)

    scal = Scal(X)
    X = scal.scaling()
    #scal.undo_scaling()
    ###########################################################################################
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = .3, random_state=25)
    
    C_param_range = [0.001,0.01,0.1,1.0,10.0,100.0]

    run.log("Parameters",C_param_range)
    
    LogReg

    for i in C_param_range:
        LogReg = LogisticRegression(penalty = 'l2', C = i,random_state = 0)
        LogReg.fit(X_train, y_train)
        
        y_pred = LogReg.predict(X_test)
        
        
        matriz_confu = confusion_matrix(y_test, y_pred)
        print("------------------------------------------------------")
        print("Parametro: ", i)
        print(matriz_confu)
        print(classification_report(y_test, y_pred))
        print(accuracy_score(y_test, y_pred))
        print("------------------------------------------------------")
        run.log("accuracy",accuracy_score(y_test, y_pred))
        run.log("Parametro: ", i)

    joblib.dump(value=LogReg, filename='./Python/Regresion Logistica - Venta Online/outputs/LogReg.pkl')
    print("Run completed")

    run = experiment.submit()
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
"""
Created on Wed Oct 17 15:08:50 2018

@author: Luis.Orellana.EXT

Dataset: https://www.kaggle.com/dragonheir/logistic-regression
"""

import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.externals import joblib
from azureml.core import Workspace, Experiment, Run
import math, random, pickle
from azureml.core.compute import ComputeTarget


class Scal:
    #Escala de -1 a 1
    scaler = MinMaxScaler(feature_range=(0, 1))
    
    def __init__(self, X):
        self.X = X
        Scal.scaler.fit(X)

    def scaling(self):
        self.X_Scaled = Scal.scaler.transform(self.X)
        return self.X_Scaled
        
    def undo_scaling(self):
        X_un_Scaled = Scal.scaler.inverse_transform(self.X_Scaled)
        return X_un_Scaled

def transform(column):
    
    wcdict = {}
    
    wcdict['Male'] = 0
    wcdict['Female'] = 1
    
    col_num = pd.DataFrame(column).applymap(lambda s: wcdict.get(s) if s in wcdict else s)
    
    return col_num

def init():

    ws = Workspace.from_config()
    experiment = Experiment(workspace = ws, name = "Experiment_RL_VentaOnline")
    run = Run.get_context()
    ds = ws.get_default_datastore()
    
    X = pd.read_csv(r"./Python/Regresion Logistica - Venta Online/Social_Network_Ads.csv", sep=",")
    
    Y = X.Purchased
    X = X.drop('Purchased', axis=1)
    X = X.drop('User ID', axis=1)
    
    X.Gender = transform(X.Gender)

    scal = Scal(X)
    X = scal.scaling()
    #scal.undo_scaling()
    ###########################################################################################
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = .3, random_state=25)
    
    C_param_range = [0.001,0.01,0.1,1.0,10.0,100.0]

    run.log("Parameters",C_param_range)
    
    LogReg

    for i in C_param_range:
        LogReg = LogisticRegression(penalty = 'l2', C = i,random_state = 0)
        LogReg.fit(X_train, y_train)
        
        y_pred = LogReg.predict(X_test)
        
        
        matriz_confu = confusion_matrix(y_test, y_pred)
        print("------------------------------------------------------")
        print("Parametro: ", i)
        print(matriz_confu)
        print(classification_report(y_test, y_pred))
        print(accuracy_score(y_test, y_pred))
        print("------------------------------------------------------")
        run.log("accuracy",accuracy_score(y_test, y_pred))
        run.log("Parametro: ", i)

    joblib.dump(value=LogReg, filename='./Python/Regresion Logistica - Venta Online/outputs/LogReg.pkl')
    print("Run completed")

    run = experiment.submit()
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
init()<file_sep>


get.MSE <-function(sim, obs, ...) UseMethod("get.MSE")

get.MSE.default <- function(sim, obs, ...)
{
  valid.classes <- c("integer", "numeric", "ts")
  if(!all(inherits(sim, valid.classes), inherits(obs, valid.classes)))
    stop("Invalid argument type: 'sim' & 'obs' have to be of class ",
         valid.classes)
  
  mse <- mean((sim - obs)^2, ...)
  
  return(mse)
}

get.MSE.matrix <- function(sim, obs, ...)
{
  # Check that 'sim' and 'obs' have the same dimensions
  if(!all.equal(dim(sim), dim(obs)))
    stop(paste0("Invalid argument: dim(sim) != dim(obs) ",
         "(", "[", paste(dim(sim), collapse = " "), "]", " != ",
         "[", paste(dim(obs), collapse = " "), "]", ")"))
  
  mse <- colMeans((sim - obs)^2, ...)
  
  return(mse)
}

get.MSE.data.frame <- function(sim, obs, ...)
{
  sim <- as.matrix(sim)
  obs <- as.matrix(obs)
  
  get.MSE.matrix(sim = sim, obs = obs, ...)
  
}
<file_sep>import os
#FORZAR USO DE CPU
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 

import time
from math import sqrt, log, exp
import math
import pandas as pd
import numpy
import matplotlib.pyplot as plt
from keras import Sequential
from keras import backend as K
from keras.models import Model
from keras.layers import LSTM, Dense
from sklearn.preprocessing import MinMaxScaler
import gc
from scipy import stats
import plotly.plotly as py
import plotly.graph_objs as go
import plotly
from numpy.random import seed
from tensorflow import set_random_seed
import tensorflow as tf
from functools import reduce



CPU_USAGE = 7
GPU_USAGE = 1
PATH_SUJETOS = ("C:/Users/Luis/Documents/DataScience/Tesis/Datos/SUJETOS/%s/%s-%s-VE.csv")
PATH_ESCALON = ("C:/Users/Luis/Documents/DataScience/Tesis/Datos/ESCALON_PRESION/ESCALON.csv")
PATH_RESULTADO = ("C:/Users/Luis/Documents/DataScience/Tesis/Resultados/Escalon/%s")
PATH_RESULTADO_ESCALON = (PATH_RESULTADO%("%s/%s_%s"))
OPTIMUM_NEURONS = 10

def create_dataset(nombre_sujeto, nombre_postura):

    X = pd.read_csv(PATH_SUJETOS%(nombre_sujeto, nombre_sujeto, nombre_postura), sep="	")
    data_escalon = pd.read_csv(PATH_ESCALON)
    
    # normalize the dataset
    scaler_VFSCd = MinMaxScaler(feature_range=(-1, 1))
    scaler_VFSCi = MinMaxScaler(feature_range=(-1, 1))
    scaler_PAMn = MinMaxScaler(feature_range=(-1, 1))
    scaler_escalon = MinMaxScaler(feature_range=(-1, 1))
    
    VFSCd = scaler_VFSCd.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
    VFSCi = scaler_VFSCi.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
    PAMn = scaler_PAMn.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
    Escalon = scaler_escalon.fit_transform(data_escalon.ESCALON.values.reshape((len(data_escalon.ESCALON.values), 1)))
    
    #Dar formato float a las señales
    PAMn, VFSCd, VFSCi, Escalon = PAMn.astype('float32'), VFSCd.astype('float32'), VFSCi.astype('float32'), Escalon.astype('float32')
    PAMn, VFSCd, VFSCi, Escalon = numpy.array(PAMn), numpy.array(VFSCd), numpy.array(VFSCi), numpy.array(Escalon)
    
    # Validacion Valanceada
    train_size = int(len(PAMn) * 0.5)
    train_PAM, train_VFSCd, train_VFSCi = PAMn[0:train_size], VFSCd[0:train_size], VFSCi[0:train_size]
    test_PAM, test_VFSCd, test_VFSCi = PAMn[train_size:len(PAMn)], VFSCd[train_size:len(VFSCd)], VFSCi[train_size:len(VFSCi)]
    
    # Reshape segun el formato que acepta Keras
    # reshape input to be [samples, time steps, features]
    train_PAM = numpy.reshape(train_PAM, (train_PAM.shape[0], 1, train_PAM.shape[1]))
    test_PAM = numpy.reshape(test_PAM, (test_PAM.shape[0], 1, test_PAM.shape[1]))
    Escalon = numpy.reshape(Escalon, (Escalon.shape[0], 1, Escalon.shape[1]))
    
    return train_PAM, train_VFSCd, train_VFSCi, test_PAM, test_VFSCd, test_VFSCi, Escalon, scaler_VFSCd, scaler_VFSCi, scaler_escalon


# fit an LSTM network to training data
def fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout):
    use_cpu_gpu()
    
    ret_seq = False
    model = Sequential()
    model.add(LSTM(64, return_sequences=True, input_shape=(198, 1)))
    model.add(LSTM(128, return_sequences=True))
    model.add(LSTM(256, return_sequences=True))
    model.add(LSTM(128, return_sequences=True))
    model.add(LSTM(64, return_sequences=True))
    model.add(LSTM(2, return_sequences=True)) 

    model.compile(loss='mse', optimizer='adam')
    model.fit(trainX[0:198], trainY[0:198], epochs=1000, batch_size=1, verbose=2, validation_data=(trainX,trainY))  
    return model

def fit_lstm2(trainX, trainY, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout):
    use_cpu_gpu()
    
    ret_seq = False
    if hidden_layers > 1:
        ret_seq = True

    model = Sequential()
    model.add(LSTM(neurons, batch_input_shape=(batch_size, trainX.shape[1], trainX.shape[2]), return_sequences=ret_seq, stateful=True, recurrent_activation=activation, recurrent_dropout=dropout))
    for i in range (hidden_layers-1):
        if i == (hidden_layers-2):
            ret_seq = False
            #neurons = OPTIMUM_NEURONS
        model.add(LSTM(neurons, return_sequences=ret_seq, stateful = True, recurrent_activation=activation, recurrent_dropout=dropout))
    model.add(Dense(trainX.shape[1], activation=activation))

    model.compile(loss='mean_squared_error', optimizer=optimization)
    for i in range(epochs):
        model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
        model.reset_states()
    return model

from keras import backend as K
def correlation_coefficient_loss(y_true, y_pred):
    x = y_true
    y = y_pred
    mx = K.mean(x)
    my = K.mean(y)
    xm, ym = x-mx, y-my
    r_num = K.sum(tf.multiply(xm,ym))
    r_den = K.sqrt(tf.multiply(K.sum(K.square(xm)), K.sum(K.square(ym))))
    r = r_num / r_den

    r = K.maximum(K.minimum(r, 1.0), -1.0)
    return ((1 - K.square(r))*-1)+1

#Evaluate the Model
def evaluate(model, X, Y, batch_size):
    output = model.predict(X)
    
    # invert data transforms on forecast
    # report performance
    rmse = stats.pearsonr(Y[:,0], output[:,0])
    
    return rmse[0]

def evaluate2(model, X, Y, batch_size):
    output = model.predict(X, batch_size=batch_size)
    
    # invert data transforms on forecast
    # report performance
    rmse = stats.pearsonr(Y[:,0], output[:,0])
    
    return rmse[0]

    #Evaluate the Model
def evaluate_stair(model, Escalon, batch_size):
    output = model.predict(Escalon, batch_size=batch_size)
    
    return output

#Transfomracion de Fisher
def fisher_transform(r):
    z = 0.5*log((1+r)/(1-r))
    return z

def inverse_fisher_transform(z):
    r = (exp(2*z)-1)/(exp(2*z)+1)
    return r

# run a repeated experiment
def experiment(trainX, trainY, testX, testY, repeats, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout):
    # run experiment
    lista_z = list()
    repetir_error = 0
    i = 1
    del_level_hyp = False
    while i <= repeats:
        # fit the model
        lstm_model = fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout)
        # report performance
        r = evaluate(lstm_model, testX, testY, batch_size)

        if math.isnan(r) and repetir_error < 3:
            repetir_error = repetir_error + 1
            if repetir_error == 3:
                del_level_hyp = True
                break
        else:
            if repetir_error != 3:
                lista_z.append(fisher_transform(r))
            repetir_error = 0
            i = i + 1

        K.clear_session()
        del lstm_model
        gc.collect()

    if del_level_hyp:
        mean_z = float('nan')
        mean_corr = float('nan')
    else:
        mean_z = reduce(lambda x, y: x + y, lista_z) / len(lista_z)
        mean_corr = inverse_fisher_transform(mean_z)

    print('epochs=%d, dropout=%.1f, activation=%s, optimization=%s neurons=%d, batch_size=%d, hidden_layers=%d:::::::::: RESULT=%.3f' % (epochs, dropout, activation, optimization, neurons, batch_size, hidden_layers, mean_corr))
    return mean_corr

def run_experiment(trainX, trainY, testX, testY, batch_size=[1], epochs=[65], optimization=["RMSprop"], activation=["tanh"], hidden_layers=[3], neurons=[10], dropout=[0.9]):
    
    columnas = ['epochs','dropout','activation','optimization','neurons','batch_size','hidden_layers','RESULT']
    filas = len(batch_size) * len(epochs) * len(optimization) * len(activation) * len(hidden_layers) * len(neurons) * len(dropout)
    results = numpy.chararray((filas,8), itemsize=20, unicode=True)
    row = 0
    repeats = 5
    
    for b in batch_size:
        for e in epochs:
            for o in optimization:
                for a in activation:
                    for h in hidden_layers:
                        for n in neurons:
                            for d in dropout:
                                result = experiment(trainX, trainY, testX, testY, repeats, b, e, o, a, h, n, d)
                                results[row][0] = e
                                results[row][1] = d
                                results[row][2] = a
                                results[row][3] = o
                                results[row][4] = n
                                results[row][5] = b
                                results[row][6] = h
                                results[row][7] = result

                                row = row + 1
    
    df = pd.DataFrame(results, columns=columnas)
    df = df.sort_values(by='RESULT', ascending=False)

    return df

def best_model(df_1, df_2, ruta_archivo):

    balance_extencion = "_1.csv"
    best_balance = 1
    max_df_1 = float(df_1.iat[0,7])
    max_df_2 = float(df_2.iat[0,7])

    df = df_1
    if(max_df_1<max_df_2):
        df = df_2
        best_balance = 2
        balance_extencion = "_2.csv"


    print('++++++++++++++++++++++++++++++++++++++ Mejor Balance: ')
    print(df)
    df.to_csv(ruta_archivo+balance_extencion, index=False)
    #df.to_excel(writer,'Resultados')
    #writer.save()
    print('################################################################################### Archivo |||'+ruta_archivo+'||| Creado')

    return df, best_balance


def plotting(escalon, output, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout, result_r):

    re_escalon = numpy.reshape(escalon, (escalon.shape[0], 1))
    re_output = numpy.reshape(output, (output.shape[0], 1))

    plotly.tools.set_credentials_file(username='luis.orellana777', api_key='pCEXLd20Nqi47WlLGYGk')

    trace_high = go.Scatter(
                    x=list(range(1, len(re_escalon))),
                    y=re_escalon,
                    name = "Escalon",
                    line = dict(color = '#17BECF'),
                    opacity = 0.8)

    trace_low = go.Scatter(
                    x=list(range(1, len(re_output))),
                    y=re_output,
                    name = "VFSC Respuesta Escalon",
                    line = dict(color = '#7F7F7F'),
                    opacity = 0.8)

    data = [trace_high,trace_low]

    layout = dict(

        title = ("LSTM - Respuesta a Escalon"),
        xaxis = dict(
            range = [1, len(re_output)], 
            title="Batch=%d ; Epoch=%d ; Optimization=%s ; Activation=%s ; Layers=%d ; Units=%d ; Dropout=%.2f ; Correlation=%.3f"%(batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout, result_r))
    )

    fig = dict(data=data, layout=layout)
    py.plot(fig, filename = "LSTM VFSC")


def plotting_mathplot(escalon, output, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout, result_r):
    
    re_escalon = numpy.reshape(escalon, (escalon.shape[0]))
    re_output = numpy.reshape(output, (output.shape[0]))
    
    fig, ax = plt.subplots()
    
    # Using set_dashes() to modify dashing of an existing line
    line1, = ax.plot(list(range(1, len(re_escalon)+1)), re_escalon, label='Escalon')
    
    # Using plot(..., dashes=...) to set the dashing when creating a line
    line2, = ax.plot(list(range(1, len(re_output)+1)), re_output, label='VFSC Respuesta Escalon')
    
    title="Batch=%d ; Epoch=%d ; Optimization=%s ; Activation=%s ; Layers=%d ; Units=%d ; Dropout=%.2f ; Correlation=%.3f"%(batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout, result_r)
    ax.set_xlabel(title)
    ax.set_title("LSTM - Respuesta a Escalon")
    
    ax.legend()
    plt.show()
    
def use_cpu_gpu():
    config = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=10,
                            inter_op_parallelism_threads=10, 
                            device_count = {'CPU' : CPU_USAGE,
                                            'GPU' : GPU_USAGE}
                           )
    config.gpu_options.allow_growth = True
    config.gpu_options.per_process_gpu_memory_fraction = 0.9
    #config.gpu_options.allow_growth = True
    session = tf.compat.v1.Session(config=config)
    K.set_session(session)

def apply_stair(df, trainX, trainY, testX, testY, escalon, scaler_VFSC, scaler_escalon, sujeto, postura, hemisferio):

    for row in range(df.shape[0]):#Cantidad de registros en el dataframe resultados
        batch_size = int(df.iat[row,5])
        epochs = int(df.iat[row,0])
        optimization = df.iat[row,3]
        activation = df.iat[row,2]
        hidden_layers = int(df.iat[row,6])
        neurons = int(df.iat[row,4])
        dropout = float(df.iat[row,1])
        result_r = float(df.iat[row,7])
        
        if hemisferio == "Izquierdo":
            pregunta_seguir = "¿Finalizar (3)?\n"
        else:
            pregunta_seguir = "¿Seguir con el Otro Hemisferio (3)?\n"

        for dropouts in [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]:

            for execution in range(1,100):

                opcion = input("Ejecucion %d. \nHemisferio %s. \n¿Deseas continuar? (0) \n¿Deseas Cambiar DropOut? (1) \n¿Deseas Probar Con Otros Hiperparametros (2)?\n%s"%(execution,hemisferio,pregunta_seguir))
            
                if opcion == "1":
                    break
                elif opcion == "2":
                    break
                elif opcion == "3":
                    return

                use_cpu_gpu()

                lstm_model = fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout)

                output = evaluate_stair(lstm_model, escalon, batch_size)

                r = evaluate(lstm_model, testX, testY, batch_size)

                plotting_mathplot(escalon, output, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout, r)

                quedar = input("¿Te quedas con esta? \nSi(1) \nNo(0) \n")
            
                if quedar == "1":
                    save_hidden_states(lstm_model, neurons, layer=0, sujeto=sujeto, postura=postura, hemisferio=hemisferio)
                    save_hidden_states(lstm_model, neurons, layer=1, sujeto=sujeto, postura=postura, hemisferio=hemisferio)
                    save_hidden_states(lstm_model, neurons, layer=2, sujeto=sujeto, postura=postura, hemisferio=hemisferio)
                    save_signal(scaler_VFSC.inverse_transform(output), sujeto=sujeto, postura=postura, hemisferio=hemisferio)
                    save_model(lstm_model, sujeto=sujeto, postura=postura, hemisferio=hemisferio)
                    #save_hidden_output(lstm_model, escalon, sujeto, postura, hemisferio)
                K.clear_session()
                del lstm_model
                gc.collect()

                if opcion == "0":
                    continue

                seguir = input("dropout = %.1f \nSeguir si(0) no(2):"%dropout)
                if seguir == "2":
                    dropout = 1.0
                    break

            if opcion == "1":
                continue
            elif opcion == "2" or seguir == "2":

                break

def save_model(model, sujeto, postura, hemisferio):
    directory = PATH_RESULTADO%(sujeto)+'/models'
    if not os.path.exists(directory):
        os.makedirs(directory)

    model_json = model.to_json()
    with open(directory+"/model_"+str(postura)+'_'+str(hemisferio)+".json", "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights(directory+"/model_"+str(postura)+'_'+str(hemisferio)+".h5")

def save_signal(output, sujeto, postura, hemisferio):

    df = pd.DataFrame(output.tolist(), columns=['Output'])
    df.to_csv(PATH_RESULTADO_ESCALON%(sujeto, postura, hemisferio)+'_output.csv',index=False)

def save_hidden_output(model, escalon, sujeto, postura, hemisferio):
    intermediate_layer_model = Model(inputs=model.layers[0].input,
                                 outputs=model.layers[0].output)
    intermediate_output = intermediate_layer_model.predict(escalon)
    df = pd.DataFrame({'Output': intermediate_output})
    writer = pd.ExcelWriter(PATH_RESULTADO_ESCALON%(sujeto, postura, hemisferio)+'_output_hidden_layer.xlsx')
    df.to_excel(writer, sheet_name='Kernel')
    writer.save()


def save_hidden_states(model, units, layer, sujeto, postura, hemisferio):
    W = model.layers[layer].get_weights()[0]
    U = model.layers[layer].get_weights()[1]
    b = model.layers[layer].get_weights()[2]
    
    W_i = W[:, :units]
    W_f = W[:, units: units * 2]
    W_c = W[:, units * 2: units * 3]
    W_o = W[:, units * 3:]

    U_i = U[:, :units]
    U_f = U[:, units: units * 2]
    U_c = U[:, units * 2: units * 3]
    U_o = U[:, units * 3:]

    b_i = b[:units]
    b_f = b[units: units * 2]
    b_c = b[units * 2: units * 3]
    b_o = b[units * 3:]

    column_W = ['W_i', 'W_f', 'W_c', 'W_o']
    column_U = ['U_i', 'U_f', 'U_c', 'U_o']
    column_b = ['b_i', 'b_f', 'b_c', 'b_o']
    # Create some Pandas dataframes from some data.
    if layer == 0:

        data_W_i = list(zip(W_i.reshape(units,1).tolist(), W_f.reshape(units,1).tolist(), W_c.reshape(units,1).tolist(), W_o.reshape(units,1).tolist()))
        data_U_i = list(zip(U_i.reshape(units*units,1).tolist(), U_f.reshape(units*units,1).tolist(), U_c.reshape(units*units,1).tolist(), U_o.reshape(units*units,1).tolist()))
        data_b_i = list(zip(b_i.reshape(units,1).tolist(), b_f.reshape(units,1).tolist(), b_c.reshape(units,1).tolist(), b_o.reshape(units,1).tolist()))
    else:

        data_W_i = list(zip(W_i.reshape(units*units,1).tolist(), W_f.reshape(units*units,1).tolist(), W_c.reshape(units*units,1).tolist(), W_o.reshape(units*units,1).tolist()))
        data_U_i = list(zip(U_i.reshape(units*units,1).tolist(), U_f.reshape(units*units,1).tolist(), U_c.reshape(units*units,1).tolist(),  U_o.reshape(units*units,1).tolist()))
        data_b_i = list(zip(b_i.reshape(units,1).tolist(), b_f.reshape(units,1).tolist(), b_c.reshape(units,1).tolist(), b_o.reshape(units,1).tolist()))

    df1 = pd.DataFrame(data_W_i, columns=column_W)
    df2 = pd.DataFrame(data_U_i, columns=column_U)
    df3 = pd.DataFrame(data_b_i, columns=column_b)
    # Create a Pandas Excel writer using XlsxWriter as the engine.
    writer = pd.ExcelWriter(PATH_RESULTADO_ESCALON%(sujeto, postura, hemisferio)+'_layer_'+str(layer)+'.xlsx',index=False)

    # Write each dataframe to a different worksheet.
    df1.to_excel(writer, sheet_name='Kernel')
    df2.to_excel(writer, sheet_name='Recurrent Kernel')
    df3.to_excel(writer, sheet_name='Bias')

    # Close the Pandas Excel writer and output the Excel file.
    writer.save()


def run (sujeto, postura, proceso_escalon = False, hemisferio_derecho=True, hemisferio_izquierdo=True):
    print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
    print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
    print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
    
    

    path = PATH_RESULTADO%(sujeto)
    if not os.path.exists(path):
        os.makedirs(path)

    train_PAM, train_VFSCd, train_VFSCi, test_PAM, test_VFSCd, test_VFSCi, Escalon, scaler_VFSCd, scaler_VFSCi, scaler_escalon = create_dataset(sujeto, postura)

    activation = ['softplus','sigmoid', 'hard_sigmoid']
    neurons = [5,10,15,20,30,40]
    hidden_layers = [3, 4, 5]
    dropout = [0.6, 0.7, 0.8, 0.9]
    optimization = ['RMSprop']
    epochs = [10]
    batch_size = [198]
    '''
    batch_size = []
    for i in range(1,train_PAM.shape[0]+1):
        if (train_PAM.shape[0]%i)==0:
            batch_size.append(199)
    '''
    if hemisferio_derecho == True:

        hemisferio = "Derecho"
        PATH_RESULTADO_CONTEXTO = PATH_RESULTADO_ESCALON%(sujeto, postura, hemisferio)

        exists_1 = os.path.isfile(PATH_RESULTADO_CONTEXTO+"_1.csv")
        exists_2 = os.path.isfile(PATH_RESULTADO_CONTEXTO+"_2.csv")

        best_balance = 0
        
        if exists_1 == False and exists_2 == False:
            ################################################################################### Balance 1
            print('++++++++++++++++++++++++++++++++++++++ Sujeto: ' + sujeto + ' Posicion: ' + postura + ' Balance: 1, Emisferio: Derecho')

            df_1 = run_experiment(train_PAM, train_VFSCd, test_PAM, test_VFSCd, batch_size=batch_size, epochs=epochs, optimization=optimization, activation=activation, hidden_layers=hidden_layers, neurons=neurons, dropout=dropout)

            ################################################################################### Balance 2
            print('++++++++++++++++++++++++++++++++++++++ Sujeto: ' + sujeto + ' Posicion: ' + postura + ' Balance: 2, Emisferio: Derecho')

            df_2 = run_experiment(test_PAM, test_VFSCd, train_PAM, train_VFSCd, batch_size=batch_size, epochs=epochs, optimization=optimization, activation=activation, hidden_layers=hidden_layers, neurons=neurons, dropout=dropout)

            df, best_balance = best_model(df_1, df_2, PATH_RESULTADO_CONTEXTO)

        if (best_balance == 1 or exists_1 == True) and proceso_escalon == True:

            df = pd.read_csv(PATH_RESULTADO_CONTEXTO+"_1.csv", dtype='S')

            apply_stair(df, train_PAM, train_VFSCd, test_PAM, test_VFSCd, Escalon, scaler_VFSCd, scaler_escalon, sujeto, postura, hemisferio)

        elif (best_balance == 2 or exists_2 == True) and proceso_escalon == True:

            df = pd.read_csv(PATH_RESULTADO_CONTEXTO+"_2.csv", dtype='S')

            apply_stair(df, test_PAM, test_VFSCd, train_PAM, train_VFSCd, Escalon, scaler_VFSCd, scaler_escalon, sujeto, postura, hemisferio)
    
    ########################################################################################## HEMISFERIO IZQUIERDO
    ################################################################################### Balance 1
    
    if hemisferio_izquierdo == True:

        hemisferio = "Izquierdo"
        PATH_RESULTADO_CONTEXTO = PATH_RESULTADO_ESCALON%(sujeto, postura, hemisferio)

        exists_1 = os.path.isfile(PATH_RESULTADO_CONTEXTO+"_1.csv")
        exists_2 = os.path.isfile(PATH_RESULTADO_CONTEXTO+"_2.csv")
        
        best_balance = 0
        
        print('++++++++++++++++++++++++++++++++++++++ Sujeto: ' + sujeto + ' Posicion: ' + postura + ' Balance: 1, Emisferio: Izquierdo')

        if exists_1 == False and exists_2 == False:
            df_1 = run_experiment(train_PAM, train_VFSCi, test_PAM, test_VFSCi, batch_size=batch_size, epochs=epochs, optimization=optimization, activation=activation, hidden_layers=hidden_layers, neurons=neurons, dropout=dropout)

            ################################################################################### Balance 2
            print('++++++++++++++++++++++++++++++++++++++ Sujeto: ' + sujeto + ' Posicion: ' + postura + ' Balance: 2, Emisferio: Izquierdo')

            df_2 = run_experiment(test_PAM, test_VFSCi, train_PAM, train_VFSCi, batch_size=batch_size, epochs=epochs, optimization=optimization, activation=activation, hidden_layers=hidden_layers, neurons=neurons, dropout=dropout)

            df, best_balance = best_model(df_1, df_2, PATH_RESULTADO_CONTEXTO)

        ########################################################################################## ENTRENAR MODELO A PARTIR DE "df" Y GRAFICAR RESPUESTA  ESCALON
        
        if (best_balance == 1 or exists_1 == True) and proceso_escalon == True:

            df = pd.read_csv(PATH_RESULTADO_CONTEXTO+"_1.csv")

            apply_stair(df, train_PAM, train_VFSCi, test_PAM, test_VFSCi, Escalon, scaler_VFSCi, scaler_escalon, sujeto, postura, hemisferio)

        elif (best_balance == 2 or exists_2 == True) and proceso_escalon == True:

            df = pd.read_csv(PATH_RESULTADO_CONTEXTO+"_2.csv")

            apply_stair(df, test_PAM, test_VFSCi, train_PAM, train_VFSCi, Escalon, scaler_VFSCi, scaler_escalon, sujeto, postura, hemisferio)


#Repitable Experiment
seed(1)
set_random_seed(2)


#run(sujeto='AP', postura='PIE', proceso_escalon=False, hemisferio_derecho=False)


#run(sujeto='AV', postura='ACOSTADO', proceso_escalon = False, hemisferio_izquierdo=False)

#run(sujeto='AV', postura='PIE', proceso_escalon = False, hemisferio_derecho=False)


#run(sujeto='CC', postura='ACOSTADO', proceso_escalon = False, hemisferio_derecho=False)

#run(sujeto='CC', postura='PIE', proceso_escalon = True)#falta emisferio ezquierdo


####################################################

#take time
t0 = time.time()

run(sujeto='AC', postura='ACOSTADO', proceso_escalon = True)

print ('###################################################################################',time.time() - t0, "segundos tardo")<file_sep>


is.wholenumber <- function(x, tol, ...) UseMethod("is.wholenumber")

is.wholenumber.default <- function(x, tol = .Machine$double.eps^0.5, ...)
{
  abs(x - round(x)) < tol
}


is.divisible <- function(x, y, tol, ...) UseMethod("is.divisible")

is.divisible.default <- function(x, y, tol = .Machine$double.eps^0.5, ...)
{
  is.wholenumber( x / y, tol, ...)
}


are.tolerably.equal <- function(x, y, tol, ...) UseMethod("tolerably.equal")

are.tolerably.equal <- function(x, y, tol = .Machine$double.eps^0.5, ...)
{
  abs(x - y) < tol
}

<file_sep>from gensim.models import Word2Vec
import pandas as pd  

w2v_model = Word2Vec.load("w2v_model")



print(w2v_model.wv.most_similar(positive=["methoxy"]))

########################################################################################

import numpy as np
import matplotlib.pyplot as plt
 
import seaborn as sns
sns.set_style("darkgrid")

from sklearn.decomposition import PCA
from sklearn.manifold import TSNE

def tsnescatterplot(model, word, list_names):

    arrays = np.empty((0, 200), dtype='f')
    word_labels = [word]
    color_list  = ['red']

    arrays = np.append(arrays, model.wv.__getitem__([word]), axis=0)
    

    close_words = model.wv.most_similar([word])
    

    for wrd_score in close_words:
        wrd_vector = model.wv.__getitem__([wrd_score[0]])
        word_labels.append(wrd_score[0])
        color_list.append('blue')
        arrays = np.append(arrays, wrd_vector, axis=0)
    

    for wrd in list_names:
        wrd_vector = model.wv.__getitem__([wrd])
        word_labels.append(wrd)
        color_list.append('green')
        arrays = np.append(arrays, wrd_vector, axis=0)
        

    reduc = PCA(n_components=21).fit_transform(arrays)
    

    np.set_printoptions(suppress=True)
    
    Y = TSNE(n_components=2, random_state=0, perplexity=15).fit_transform(reduc)
    

    df = pd.DataFrame({'x': [x for x in Y[:, 0]],
                       'y': [y for y in Y[:, 1]],
                       'words': word_labels,
                       'color': color_list})
    
    fig, _ = plt.subplots()
    fig.set_size_inches(9, 9)
    

    p1 = sns.regplot(data=df,
                     x="x",
                     y="y",
                     fit_reg=False,
                     marker="o",
                     scatter_kws={'s': 40,
                                  'facecolors': df['color']
                                 }
                    )
    

    for line in range(0, df.shape[0]):
         p1.text(df["x"][line],
                 df['y'][line],
                 '  ' + df["words"][line].title(),
                 horizontalalignment='left',
                 verticalalignment='bottom', size='medium',
                 color=df['color'][line],
                 weight='normal'
                ).set_size(15)

    
    plt.xlim(Y[:, 0].min()-50, Y[:, 0].max()+50)
    plt.ylim(Y[:, 1].min()-50, Y[:, 1].max()+50)
            
    plt.title('t-SNE visualization for {}'.format(word.title()))


tsnescatterplot(w2v_model, 'curcumin', [i[0] for i in w2v_model.wv.most_similar(negative=["curcumin"])])

tsnescatterplot(w2v_model, "oxime", [t[0] for t in w2v_model.wv.most_similar(positive=["oxime"], topn=20)][10:])<file_sep><<<<<<< HEAD
<<<<<<< HEAD
setwd(choose.dir())#Entregar ruta de trabajo

#Instala paquete “aruleViz”
install.packages('arulesViz', dep = TRUE)
library(arulesViz)

data  =  read.csv("zoo.csv") #Carga dataset

summary(data) #Resumen estadístico

data <- data.frame(sapply(data,as.factor)) #Discretiza datos

rules <- apriori(data, parameter=list(support=0.37, confidence=0.5, maxlen=50))#5736 Encontradas

inspect(head(sort(rules, by ="lift"),3))#Tres primeras reglas

plot(rules, measure=c("support", "lift"), shading="confidence")#Diagrama de dispersión

inspect(head(sort(rules, by ="lift"),260)) #Visualizar reglas para seleccionar


subrules <- rules[quality(rules)$lift > 1.46]#Visualizar reglas que tengan más de “1.46” lift

subrules #Número de reglas obtenidas: 1928

#Matrices de reglas
plot(subrules, method="matrix", measure="lift", control=list(reorder=TRUE))
plot(subrules, method="matrix3D", measure="lift", control=list(reorder=TRUE))
plot(subrules, method="matrix3D", measure="support", control=list(reorder=TRUE))
plot(subrules, method="matrix", measure=c("lift", "confidence"),control=list(reorder=TRUE))

#Matriz de agrupación (5.736 reglas). Siete agrupadores de reglas en eje “X”.
plot(rules, method="grouped", control=list(k=7))

subrules2 <- head(sort(rules, by="lift"), 10)#Sub Set de 10 reglas.

plot(subrules2, method="graph")#Grafico de vértices para diez reglas.

#Grafico de coordenadas paralelas para diez reglas.
plot(subrules2, method="paracoord", control=list(reorder=TRUE))
=======
setwd(choose.dir())#Entregar ruta de trabajo

#Instala paquete “aruleViz”
install.packages('arulesViz', dep = TRUE)
library(arulesViz)

data  =  read.csv("zoo.csv") #Carga dataset

summary(data) #Resumen estadístico

data <- data.frame(sapply(data,as.factor)) #Discretiza datos

rules <- apriori(data, parameter=list(support=0.37, confidence=0.5, maxlen=50))#5736 Encontradas

inspect(head(sort(rules, by ="lift"),3))#Tres primeras reglas

plot(rules, measure=c("support", "lift"), shading="confidence")#Diagrama de dispersión

inspect(head(sort(rules, by ="lift"),260)) #Visualizar reglas para seleccionar


subrules <- rules[quality(rules)$lift > 1.46]#Visualizar reglas que tengan más de “1.46” lift

subrules #Número de reglas obtenidas: 1928

#Matrices de reglas
plot(subrules, method="matrix", measure="lift", control=list(reorder=TRUE))
plot(subrules, method="matrix3D", measure="lift", control=list(reorder=TRUE))
plot(subrules, method="matrix3D", measure="support", control=list(reorder=TRUE))
plot(subrules, method="matrix", measure=c("lift", "confidence"),control=list(reorder=TRUE))

#Matriz de agrupación (5.736 reglas). Siete agrupadores de reglas en eje “X”.
plot(rules, method="grouped", control=list(k=7))

subrules2 <- head(sort(rules, by="lift"), 10)#Sub Set de 10 reglas.

plot(subrules2, method="graph")#Grafico de vértices para diez reglas.

#Grafico de coordenadas paralelas para diez reglas.
plot(subrules2, method="paracoord", control=list(reorder=TRUE))
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
setwd(choose.dir())#Entregar ruta de trabajo

#Instala paquete “aruleViz”
install.packages('arulesViz', dep = TRUE)
library(arulesViz)

data  =  read.csv("zoo.csv") #Carga dataset

summary(data) #Resumen estadístico

data <- data.frame(sapply(data,as.factor)) #Discretiza datos

rules <- apriori(data, parameter=list(support=0.37, confidence=0.5, maxlen=50))#5736 Encontradas

inspect(head(sort(rules, by ="lift"),3))#Tres primeras reglas

plot(rules, measure=c("support", "lift"), shading="confidence")#Diagrama de dispersión

inspect(head(sort(rules, by ="lift"),260)) #Visualizar reglas para seleccionar


subrules <- rules[quality(rules)$lift > 1.46]#Visualizar reglas que tengan más de “1.46” lift

subrules #Número de reglas obtenidas: 1928

#Matrices de reglas
plot(subrules, method="matrix", measure="lift", control=list(reorder=TRUE))
plot(subrules, method="matrix3D", measure="lift", control=list(reorder=TRUE))
plot(subrules, method="matrix3D", measure="support", control=list(reorder=TRUE))
plot(subrules, method="matrix", measure=c("lift", "confidence"),control=list(reorder=TRUE))

#Matriz de agrupación (5.736 reglas). Siete agrupadores de reglas en eje “X”.
plot(rules, method="grouped", control=list(k=7))

subrules2 <- head(sort(rules, by="lift"), 10)#Sub Set de 10 reglas.

plot(subrules2, method="graph")#Grafico de vértices para diez reglas.

#Grafico de coordenadas paralelas para diez reglas.
plot(subrules2, method="paracoord", control=list(reorder=TRUE))
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep><<<<<<< HEAD
<<<<<<< HEAD
RUTA_DATASET <- "C:/Users/Luis.O.A/Documents/USACH/Mineria de Datos/Trabajos/6"
CANTIDAD_NUCLEOS <- 4

setwd(RUTA_DATASET)

#install.packages('plotly', dep = TRUE)
library(e1071)

#install.packages('doParallel', dep = TRUE)
library(doParallel)

#install.packages('xlsx', dep = TRUE)
library('xlsx')

library(plotly)
library(plyr)

registerDoParallel(cores = CANTIDAD_NUCLEOS)


retardos_multi <- function( signalData, lags){
	signal.uni <- signalData
	max.lag <- max(unlist(lags)) + 1
	indices <- 1:nrow(signal.uni)
	lag.mat <- embed(indices, max.lag)
	col.names <- list("PAMn","VFSCn")
	columns <- NULL
	lagged.columns.names <- c()
	for(colname in col.names){
		lag.order <- lags[[colname]]
		columns[[colname]] <- signal.uni[lag.mat[, 1], colname]
		if(!is.null(lag.order) && lag.order > 0)
		for(i in 1:lag.order){
			new.colname <- paste(colname, paste0("lag", i), sep = ".")
			lagged.columns.names <- c(lagged.columns.names, new.colname)
			columns[[new.colname]] <- signal.uni[lag.mat[, i+1], colname]
		}
	}
	folded.signal <- data.frame(columns)
	sorting <- order(lag.mat[, 1])
	folded.signal <- folded.signal[sorting, ]
	list(folded.signal = folded.signal, lagged.columns.names = lagged.columns.names)
}

entrenar <- function(data.1, data.2, parms, retardos_multi){
	salida <- (c( foreach(i = 1:nrow(parms), combine = rbind, .inorder = FALSE)
	%dopar% {
		c <- parms[i, ]$cost
		n <- parms[i, ]$nu
		g <- parms[i, ]$gamma
		l <- parms[i, ]$lagsList
		
		lag<-list(PAMn = l,VFSCn = 0)
		#Etrenamiento 1
		signal.train.1 <- retardos_multi(data.1, lag)
		retDataset1=signal.train.1$folded.signal
		x.1=subset(retDataset1, select = -VFSCn)
		y.1=retDataset1$VFSCn
		modelo <- e1071::svm(x.1, y.1, type = "nu-regression", kernel = "radial", cost =
		c, nu = n, gamma=g)
		#Test 2
		signal.train.2 <- retardos_multi(data.2, lag)
		retDataset2=signal.train.2$folded.signal
		x.2=subset(retDataset2, select = -VFSCn)
		pred <- predict(modelo, x.2)
		y.2=retDataset2$VFSCn
		corr_pred<-cor(pred,y.2,method = "pearson")
		c(l, c, n, g, corr_pred)
	}))
	output <- matrix(unlist(salida), ncol = 5, byrow = TRUE)
	mejoresModelos<-output[order(output[,5], decreasing = TRUE),]
	return(mejoresModelos)
}

plot.signal <- function(datos, retardos_multi, mejoresModelos, i){

	Ts=0.2
	inverseStep=matrix(1,180/Ts,1)
	inverseStep[(90/Ts):(180/Ts),1]=0

	if(i == 0){
		for (i in 1:length(mejoresModelos[,1])){

			lag<-list(PAMn = mejoresModelos[i,1],VFSCn = 0)
			signal.train <- retardos_multi(datos, lag)
			retDataset1=signal.train$folded.signal

			x=subset(retDataset1, select = -VFSCn)
			y=retDataset1$VFSCn
			
			mejorModelo <- svm(x, y, kernel = "radial",type = "nu-regression", cost = mejoresModelos[i,2],
			nu = mejoresModelos[i,3], gamma=mejoresModelos[i,4])

			#PAMn=inverseStep
			#VFSCn=inverseStep
			
			##################PRUEBA CON LOS ESCALONES
			data.esc = read.table("esc.txt")
			PAMn = matrix(data.esc$V1)
			VFSCn = matrix(1,length(PAMn),1)
			#############
			
			data <- data.frame(PAMn,VFSCn)

			signal.train <- retardos_multi(data, lag)
			retDataset1=signal.train$folded.signal

			x=subset(retDataset1, select = -VFSCn)
			y=retDataset1$VFSCn
			
			stepTime=seq(Ts,(length(retDataset1$PAMn))*Ts,Ts)
			stepResponse <- predict(mejorModelo, x )


			plot(stepTime,retDataset1$PAMn,type="l", col="red")
			lines(stepTime,stepResponse, col = "blue")
			legend("topright", c("Escalon de presión", "respuesta al escalon", paste("i = ",i)), title = "autorregulacion", pch =
			1, col=c("red","blue"),lty=c(1,1),inset = 0.01)
			print(paste("corr=",mejoresModelos[i,5]))
			readline(prompt="Press [enter] to continue")

			
		}
	}else if(i > 0){
		lag<-list(PAMn = mejoresModelos[i,1],VFSCn = 0)
		signal.train <- retardos_multi(datos, lag)
		retDataset1=signal.train$folded.signal

		x=subset(retDataset1, select = -VFSCn)
		y=retDataset1$VFSCn
		
		mejorModelo <- svm(x, y, kernel = "radial",type = "nu-regression", cost = mejoresModelos[i,2],
		nu = mejoresModelos[i,3], gamma=mejoresModelos[i,4])

		#PAMn=inverseStep
		#VFSCn=inverseStep
		
		##################PRUEBA CON LOS ESCALONES
		data.esc = read.table("esc.txt")
		PAMn = matrix(data.esc$V1)
		VFSCn = matrix(1,length(PAMn),1)
		#############
		
		data <- data.frame(PAMn,VFSCn)

		signal.train <- retardos_multi(data, lag)
		retDataset1=signal.train$folded.signal

		x=subset(retDataset1, select = -VFSCn)
		y=retDataset1$VFSCn
		
		stepTime=seq(Ts,(length(retDataset1$PAMn))*Ts,Ts)
		stepResponse <- predict(mejorModelo, x )
		
		data.escalon <- data.frame(Time = stepTime, PAM = retDataset1$PAMn, z = stepResponse)
		
		p <- plot_ly(data.escalon, x = ~Time, y = ~PAM, name = 'Escalon de presión', type = 'scatter', mode = 'lines') %>%
		add_trace(y = ~z, name = 'respuesta al escalon', mode = 'lines')
		p
		
	}
}

cost <- 2^(-4:12)
nu <- seq(0.1, 1.0, 0.1)
gamma<- 2^(-4:12)
lagsList<-(1:8)
Dataset1=read.csv("G5_002.csv")


set.seed(100)
trainingRows <- sample(1:nrow(Dataset1), 0.5*nrow(Dataset1))
A <- Dataset1[trainingRows, ]
B <- Dataset1[-trainingRows, ]


data.1 <- data.frame(PAMn = (A$PAM-min(A$PAM))/(max(A$PAM)-min(A$PAM)),VFSCn = (A$VFSC-min(A$VFSC))/(max(A$VFSC)-min(A$VFSC)))

data.2 <- data.frame(PAMn = (B$PAM-min(B$PAM))/(max(B$PAM)-min(B$PAM)),VFSCn = (B$VFSC-min(B$VFSC))/(max(B$VFSC)-min(B$VFSC)))

parms <- expand.grid(lagsList=lagsList, cost = cost, nu = nu, gamma=gamma)


mejoresModelos.1 <- entrenar(data.1, data.2, parms, retardos_multi)
mejoresModelos.2 <- entrenar(data.2, data.1, parms, retardos_multi)

write.xlsx(data.frame(LagList = mejoresModelos.1[,1], Cost = mejoresModelos.1[,2], Nu = mejoresModelos.1[,3], Gamma = mejoresModelos.1[,4], Correlacion = mejoresModelos.1[,5]), "mejoresModelos_1.xlsx", sheetName="Sheet1", append = FALSE)
write.xlsx(data.frame(LagList = mejoresModelos.2[,1], Cost = mejoresModelos.2[,2], Nu = mejoresModelos.2[,3], Gamma = mejoresModelos.2[,4], Correlacion = mejoresModelos.2[,5]), "mejoresModelos_2.xlsx", sheetName="Sheet1", append = FALSE)
############################CORRER HASTA ESTA LINEA

mm1=read.csv("mm1.csv")
mm1 <- as.matrix(setNames(mm1,NULL)) #sano

mm2=read.csv("mm2.csv")
mm2 <- as.matrix(setNames(mm2,NULL)) #Enfermo


######### Eleccion de modelos. Si el ultimo parametro es 0, se veran todos los modelos generados. Si el parametro es mayor a 0 se visualizara el modelo ingresado
plot.signal(data.1, retardos_multi, mm1, 85)#17-21-25(mejor)-39-40(mejor)-44(mejor)-56-59-85(mejor)
plot.signal(data.2, retardos_multi, mm2, 22)#4-8-22(mejor)

####################GRAFICA SEÑAL
Dataset1=read.csv("G5_002.csv")
largo.d1 = length(Dataset1$PAM)

Dataset2=read.csv("G6_001.csv")[1:largo.d1,]
largo.d2 = length(Dataset2$PAM)

data.escalon <- data.frame(Time = c(1:largo.d2), PAM = Dataset1$PAM, PAM_Sujeto_2 = Dataset2$PAM)
		
p <- plot_ly(data.escalon, x = ~Time, y = ~PAM, name = 'PAM Sujeto 1', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~PAM_Sujeto_2, name = 'PAM Sujeto 2', mode = 'lines')
p
		
		


data.escalon <- data.frame(Time = c(1:largo.d2), VFSC_Sujeto_1 = Dataset1$VFSC, VFSC_Sujeto_2 = Dataset2$VFSC)
		
p <- plot_ly(data.escalon, x = ~Time, y = ~VFSC_Sujeto_1, name = 'VFSC Sujeto 1', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~VFSC_Sujeto_2, name = 'VFSC Sujeto 2', mode = 'lines')
p
		
		
=======
RUTA_DATASET <- "C:/Users/Luis.O.A/Documents/USACH/Mineria de Datos/Trabajos/6"
CANTIDAD_NUCLEOS <- 4

setwd(RUTA_DATASET)

#install.packages('plotly', dep = TRUE)
library(e1071)

#install.packages('doParallel', dep = TRUE)
library(doParallel)

#install.packages('xlsx', dep = TRUE)
library('xlsx')

library(plotly)
library(plyr)

registerDoParallel(cores = CANTIDAD_NUCLEOS)


retardos_multi <- function( signalData, lags){
	signal.uni <- signalData
	max.lag <- max(unlist(lags)) + 1
	indices <- 1:nrow(signal.uni)
	lag.mat <- embed(indices, max.lag)
	col.names <- list("PAMn","VFSCn")
	columns <- NULL
	lagged.columns.names <- c()
	for(colname in col.names){
		lag.order <- lags[[colname]]
		columns[[colname]] <- signal.uni[lag.mat[, 1], colname]
		if(!is.null(lag.order) && lag.order > 0)
		for(i in 1:lag.order){
			new.colname <- paste(colname, paste0("lag", i), sep = ".")
			lagged.columns.names <- c(lagged.columns.names, new.colname)
			columns[[new.colname]] <- signal.uni[lag.mat[, i+1], colname]
		}
	}
	folded.signal <- data.frame(columns)
	sorting <- order(lag.mat[, 1])
	folded.signal <- folded.signal[sorting, ]
	list(folded.signal = folded.signal, lagged.columns.names = lagged.columns.names)
}

entrenar <- function(data.1, data.2, parms, retardos_multi){
	salida <- (c( foreach(i = 1:nrow(parms), combine = rbind, .inorder = FALSE)
	%dopar% {
		c <- parms[i, ]$cost
		n <- parms[i, ]$nu
		g <- parms[i, ]$gamma
		l <- parms[i, ]$lagsList
		
		lag<-list(PAMn = l,VFSCn = 0)
		#Etrenamiento 1
		signal.train.1 <- retardos_multi(data.1, lag)
		retDataset1=signal.train.1$folded.signal
		x.1=subset(retDataset1, select = -VFSCn)
		y.1=retDataset1$VFSCn
		modelo <- e1071::svm(x.1, y.1, type = "nu-regression", kernel = "radial", cost =
		c, nu = n, gamma=g)
		#Test 2
		signal.train.2 <- retardos_multi(data.2, lag)
		retDataset2=signal.train.2$folded.signal
		x.2=subset(retDataset2, select = -VFSCn)
		pred <- predict(modelo, x.2)
		y.2=retDataset2$VFSCn
		corr_pred<-cor(pred,y.2,method = "pearson")
		c(l, c, n, g, corr_pred)
	}))
	output <- matrix(unlist(salida), ncol = 5, byrow = TRUE)
	mejoresModelos<-output[order(output[,5], decreasing = TRUE),]
	return(mejoresModelos)
}

plot.signal <- function(datos, retardos_multi, mejoresModelos, i){

	Ts=0.2
	inverseStep=matrix(1,180/Ts,1)
	inverseStep[(90/Ts):(180/Ts),1]=0

	if(i == 0){
		for (i in 1:length(mejoresModelos[,1])){

			lag<-list(PAMn = mejoresModelos[i,1],VFSCn = 0)
			signal.train <- retardos_multi(datos, lag)
			retDataset1=signal.train$folded.signal

			x=subset(retDataset1, select = -VFSCn)
			y=retDataset1$VFSCn
			
			mejorModelo <- svm(x, y, kernel = "radial",type = "nu-regression", cost = mejoresModelos[i,2],
			nu = mejoresModelos[i,3], gamma=mejoresModelos[i,4])

			#PAMn=inverseStep
			#VFSCn=inverseStep
			
			##################PRUEBA CON LOS ESCALONES
			data.esc = read.table("esc.txt")
			PAMn = matrix(data.esc$V1)
			VFSCn = matrix(1,length(PAMn),1)
			#############
			
			data <- data.frame(PAMn,VFSCn)

			signal.train <- retardos_multi(data, lag)
			retDataset1=signal.train$folded.signal

			x=subset(retDataset1, select = -VFSCn)
			y=retDataset1$VFSCn
			
			stepTime=seq(Ts,(length(retDataset1$PAMn))*Ts,Ts)
			stepResponse <- predict(mejorModelo, x )


			plot(stepTime,retDataset1$PAMn,type="l", col="red")
			lines(stepTime,stepResponse, col = "blue")
			legend("topright", c("Escalon de presión", "respuesta al escalon", paste("i = ",i)), title = "autorregulacion", pch =
			1, col=c("red","blue"),lty=c(1,1),inset = 0.01)
			print(paste("corr=",mejoresModelos[i,5]))
			readline(prompt="Press [enter] to continue")

			
		}
	}else if(i > 0){
		lag<-list(PAMn = mejoresModelos[i,1],VFSCn = 0)
		signal.train <- retardos_multi(datos, lag)
		retDataset1=signal.train$folded.signal

		x=subset(retDataset1, select = -VFSCn)
		y=retDataset1$VFSCn
		
		mejorModelo <- svm(x, y, kernel = "radial",type = "nu-regression", cost = mejoresModelos[i,2],
		nu = mejoresModelos[i,3], gamma=mejoresModelos[i,4])

		#PAMn=inverseStep
		#VFSCn=inverseStep
		
		##################PRUEBA CON LOS ESCALONES
		data.esc = read.table("esc.txt")
		PAMn = matrix(data.esc$V1)
		VFSCn = matrix(1,length(PAMn),1)
		#############
		
		data <- data.frame(PAMn,VFSCn)

		signal.train <- retardos_multi(data, lag)
		retDataset1=signal.train$folded.signal

		x=subset(retDataset1, select = -VFSCn)
		y=retDataset1$VFSCn
		
		stepTime=seq(Ts,(length(retDataset1$PAMn))*Ts,Ts)
		stepResponse <- predict(mejorModelo, x )
		
		data.escalon <- data.frame(Time = stepTime, PAM = retDataset1$PAMn, z = stepResponse)
		
		p <- plot_ly(data.escalon, x = ~Time, y = ~PAM, name = 'Escalon de presión', type = 'scatter', mode = 'lines') %>%
		add_trace(y = ~z, name = 'respuesta al escalon', mode = 'lines')
		p
		
	}
}

cost <- 2^(-4:12)
nu <- seq(0.1, 1.0, 0.1)
gamma<- 2^(-4:12)
lagsList<-(1:8)
Dataset1=read.csv("G5_002.csv")


set.seed(100)
trainingRows <- sample(1:nrow(Dataset1), 0.5*nrow(Dataset1))
A <- Dataset1[trainingRows, ]
B <- Dataset1[-trainingRows, ]


data.1 <- data.frame(PAMn = (A$PAM-min(A$PAM))/(max(A$PAM)-min(A$PAM)),VFSCn = (A$VFSC-min(A$VFSC))/(max(A$VFSC)-min(A$VFSC)))

data.2 <- data.frame(PAMn = (B$PAM-min(B$PAM))/(max(B$PAM)-min(B$PAM)),VFSCn = (B$VFSC-min(B$VFSC))/(max(B$VFSC)-min(B$VFSC)))

parms <- expand.grid(lagsList=lagsList, cost = cost, nu = nu, gamma=gamma)


mejoresModelos.1 <- entrenar(data.1, data.2, parms, retardos_multi)
mejoresModelos.2 <- entrenar(data.2, data.1, parms, retardos_multi)

write.xlsx(data.frame(LagList = mejoresModelos.1[,1], Cost = mejoresModelos.1[,2], Nu = mejoresModelos.1[,3], Gamma = mejoresModelos.1[,4], Correlacion = mejoresModelos.1[,5]), "mejoresModelos_1.xlsx", sheetName="Sheet1", append = FALSE)
write.xlsx(data.frame(LagList = mejoresModelos.2[,1], Cost = mejoresModelos.2[,2], Nu = mejoresModelos.2[,3], Gamma = mejoresModelos.2[,4], Correlacion = mejoresModelos.2[,5]), "mejoresModelos_2.xlsx", sheetName="Sheet1", append = FALSE)
############################CORRER HASTA ESTA LINEA

mm1=read.csv("mm1.csv")
mm1 <- as.matrix(setNames(mm1,NULL)) #sano

mm2=read.csv("mm2.csv")
mm2 <- as.matrix(setNames(mm2,NULL)) #Enfermo


######### Eleccion de modelos. Si el ultimo parametro es 0, se veran todos los modelos generados. Si el parametro es mayor a 0 se visualizara el modelo ingresado
plot.signal(data.1, retardos_multi, mm1, 85)#17-21-25(mejor)-39-40(mejor)-44(mejor)-56-59-85(mejor)
plot.signal(data.2, retardos_multi, mm2, 22)#4-8-22(mejor)

####################GRAFICA SEÑAL
Dataset1=read.csv("G5_002.csv")
largo.d1 = length(Dataset1$PAM)

Dataset2=read.csv("G6_001.csv")[1:largo.d1,]
largo.d2 = length(Dataset2$PAM)

data.escalon <- data.frame(Time = c(1:largo.d2), PAM = Dataset1$PAM, PAM_Sujeto_2 = Dataset2$PAM)
		
p <- plot_ly(data.escalon, x = ~Time, y = ~PAM, name = 'PAM Sujeto 1', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~PAM_Sujeto_2, name = 'PAM Sujeto 2', mode = 'lines')
p
		
		


data.escalon <- data.frame(Time = c(1:largo.d2), VFSC_Sujeto_1 = Dataset1$VFSC, VFSC_Sujeto_2 = Dataset2$VFSC)
		
p <- plot_ly(data.escalon, x = ~Time, y = ~VFSC_Sujeto_1, name = 'VFSC Sujeto 1', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~VFSC_Sujeto_2, name = 'VFSC Sujeto 2', mode = 'lines')
p
		
		
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
RUTA_DATASET <- "C:/Users/Luis.O.A/Documents/USACH/Mineria de Datos/Trabajos/6"
CANTIDAD_NUCLEOS <- 4

setwd(RUTA_DATASET)

#install.packages('plotly', dep = TRUE)
library(e1071)

#install.packages('doParallel', dep = TRUE)
library(doParallel)

#install.packages('xlsx', dep = TRUE)
library('xlsx')

library(plotly)
library(plyr)

registerDoParallel(cores = CANTIDAD_NUCLEOS)


retardos_multi <- function( signalData, lags){
	signal.uni <- signalData
	max.lag <- max(unlist(lags)) + 1
	indices <- 1:nrow(signal.uni)
	lag.mat <- embed(indices, max.lag)
	col.names <- list("PAMn","VFSCn")
	columns <- NULL
	lagged.columns.names <- c()
	for(colname in col.names){
		lag.order <- lags[[colname]]
		columns[[colname]] <- signal.uni[lag.mat[, 1], colname]
		if(!is.null(lag.order) && lag.order > 0)
		for(i in 1:lag.order){
			new.colname <- paste(colname, paste0("lag", i), sep = ".")
			lagged.columns.names <- c(lagged.columns.names, new.colname)
			columns[[new.colname]] <- signal.uni[lag.mat[, i+1], colname]
		}
	}
	folded.signal <- data.frame(columns)
	sorting <- order(lag.mat[, 1])
	folded.signal <- folded.signal[sorting, ]
	list(folded.signal = folded.signal, lagged.columns.names = lagged.columns.names)
}

entrenar <- function(data.1, data.2, parms, retardos_multi){
	salida <- (c( foreach(i = 1:nrow(parms), combine = rbind, .inorder = FALSE)
	%dopar% {
		c <- parms[i, ]$cost
		n <- parms[i, ]$nu
		g <- parms[i, ]$gamma
		l <- parms[i, ]$lagsList
		
		lag<-list(PAMn = l,VFSCn = 0)
		#Etrenamiento 1
		signal.train.1 <- retardos_multi(data.1, lag)
		retDataset1=signal.train.1$folded.signal
		x.1=subset(retDataset1, select = -VFSCn)
		y.1=retDataset1$VFSCn
		modelo <- e1071::svm(x.1, y.1, type = "nu-regression", kernel = "radial", cost =
		c, nu = n, gamma=g)
		#Test 2
		signal.train.2 <- retardos_multi(data.2, lag)
		retDataset2=signal.train.2$folded.signal
		x.2=subset(retDataset2, select = -VFSCn)
		pred <- predict(modelo, x.2)
		y.2=retDataset2$VFSCn
		corr_pred<-cor(pred,y.2,method = "pearson")
		c(l, c, n, g, corr_pred)
	}))
	output <- matrix(unlist(salida), ncol = 5, byrow = TRUE)
	mejoresModelos<-output[order(output[,5], decreasing = TRUE),]
	return(mejoresModelos)
}

plot.signal <- function(datos, retardos_multi, mejoresModelos, i){

	Ts=0.2
	inverseStep=matrix(1,180/Ts,1)
	inverseStep[(90/Ts):(180/Ts),1]=0

	if(i == 0){
		for (i in 1:length(mejoresModelos[,1])){

			lag<-list(PAMn = mejoresModelos[i,1],VFSCn = 0)
			signal.train <- retardos_multi(datos, lag)
			retDataset1=signal.train$folded.signal

			x=subset(retDataset1, select = -VFSCn)
			y=retDataset1$VFSCn
			
			mejorModelo <- svm(x, y, kernel = "radial",type = "nu-regression", cost = mejoresModelos[i,2],
			nu = mejoresModelos[i,3], gamma=mejoresModelos[i,4])

			#PAMn=inverseStep
			#VFSCn=inverseStep
			
			##################PRUEBA CON LOS ESCALONES
			data.esc = read.table("esc.txt")
			PAMn = matrix(data.esc$V1)
			VFSCn = matrix(1,length(PAMn),1)
			#############
			
			data <- data.frame(PAMn,VFSCn)

			signal.train <- retardos_multi(data, lag)
			retDataset1=signal.train$folded.signal

			x=subset(retDataset1, select = -VFSCn)
			y=retDataset1$VFSCn
			
			stepTime=seq(Ts,(length(retDataset1$PAMn))*Ts,Ts)
			stepResponse <- predict(mejorModelo, x )


			plot(stepTime,retDataset1$PAMn,type="l", col="red")
			lines(stepTime,stepResponse, col = "blue")
			legend("topright", c("Escalon de presión", "respuesta al escalon", paste("i = ",i)), title = "autorregulacion", pch =
			1, col=c("red","blue"),lty=c(1,1),inset = 0.01)
			print(paste("corr=",mejoresModelos[i,5]))
			readline(prompt="Press [enter] to continue")

			
		}
	}else if(i > 0){
		lag<-list(PAMn = mejoresModelos[i,1],VFSCn = 0)
		signal.train <- retardos_multi(datos, lag)
		retDataset1=signal.train$folded.signal

		x=subset(retDataset1, select = -VFSCn)
		y=retDataset1$VFSCn
		
		mejorModelo <- svm(x, y, kernel = "radial",type = "nu-regression", cost = mejoresModelos[i,2],
		nu = mejoresModelos[i,3], gamma=mejoresModelos[i,4])

		#PAMn=inverseStep
		#VFSCn=inverseStep
		
		##################PRUEBA CON LOS ESCALONES
		data.esc = read.table("esc.txt")
		PAMn = matrix(data.esc$V1)
		VFSCn = matrix(1,length(PAMn),1)
		#############
		
		data <- data.frame(PAMn,VFSCn)

		signal.train <- retardos_multi(data, lag)
		retDataset1=signal.train$folded.signal

		x=subset(retDataset1, select = -VFSCn)
		y=retDataset1$VFSCn
		
		stepTime=seq(Ts,(length(retDataset1$PAMn))*Ts,Ts)
		stepResponse <- predict(mejorModelo, x )
		
		data.escalon <- data.frame(Time = stepTime, PAM = retDataset1$PAMn, z = stepResponse)
		
		p <- plot_ly(data.escalon, x = ~Time, y = ~PAM, name = 'Escalon de presión', type = 'scatter', mode = 'lines') %>%
		add_trace(y = ~z, name = 'respuesta al escalon', mode = 'lines')
		p
		
	}
}

cost <- 2^(-4:12)
nu <- seq(0.1, 1.0, 0.1)
gamma<- 2^(-4:12)
lagsList<-(1:8)
Dataset1=read.csv("G5_002.csv")


set.seed(100)
trainingRows <- sample(1:nrow(Dataset1), 0.5*nrow(Dataset1))
A <- Dataset1[trainingRows, ]
B <- Dataset1[-trainingRows, ]


data.1 <- data.frame(PAMn = (A$PAM-min(A$PAM))/(max(A$PAM)-min(A$PAM)),VFSCn = (A$VFSC-min(A$VFSC))/(max(A$VFSC)-min(A$VFSC)))

data.2 <- data.frame(PAMn = (B$PAM-min(B$PAM))/(max(B$PAM)-min(B$PAM)),VFSCn = (B$VFSC-min(B$VFSC))/(max(B$VFSC)-min(B$VFSC)))

parms <- expand.grid(lagsList=lagsList, cost = cost, nu = nu, gamma=gamma)


mejoresModelos.1 <- entrenar(data.1, data.2, parms, retardos_multi)
mejoresModelos.2 <- entrenar(data.2, data.1, parms, retardos_multi)

write.xlsx(data.frame(LagList = mejoresModelos.1[,1], Cost = mejoresModelos.1[,2], Nu = mejoresModelos.1[,3], Gamma = mejoresModelos.1[,4], Correlacion = mejoresModelos.1[,5]), "mejoresModelos_1.xlsx", sheetName="Sheet1", append = FALSE)
write.xlsx(data.frame(LagList = mejoresModelos.2[,1], Cost = mejoresModelos.2[,2], Nu = mejoresModelos.2[,3], Gamma = mejoresModelos.2[,4], Correlacion = mejoresModelos.2[,5]), "mejoresModelos_2.xlsx", sheetName="Sheet1", append = FALSE)
############################CORRER HASTA ESTA LINEA

mm1=read.csv("mm1.csv")
mm1 <- as.matrix(setNames(mm1,NULL)) #sano

mm2=read.csv("mm2.csv")
mm2 <- as.matrix(setNames(mm2,NULL)) #Enfermo


######### Eleccion de modelos. Si el ultimo parametro es 0, se veran todos los modelos generados. Si el parametro es mayor a 0 se visualizara el modelo ingresado
plot.signal(data.1, retardos_multi, mm1, 85)#17-21-25(mejor)-39-40(mejor)-44(mejor)-56-59-85(mejor)
plot.signal(data.2, retardos_multi, mm2, 22)#4-8-22(mejor)

####################GRAFICA SEÑAL
Dataset1=read.csv("G5_002.csv")
largo.d1 = length(Dataset1$PAM)

Dataset2=read.csv("G6_001.csv")[1:largo.d1,]
largo.d2 = length(Dataset2$PAM)

data.escalon <- data.frame(Time = c(1:largo.d2), PAM = Dataset1$PAM, PAM_Sujeto_2 = Dataset2$PAM)
		
p <- plot_ly(data.escalon, x = ~Time, y = ~PAM, name = 'PAM Sujeto 1', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~PAM_Sujeto_2, name = 'PAM Sujeto 2', mode = 'lines')
p
		
		


data.escalon <- data.frame(Time = c(1:largo.d2), VFSC_Sujeto_1 = Dataset1$VFSC, VFSC_Sujeto_2 = Dataset2$VFSC)
		
p <- plot_ly(data.escalon, x = ~Time, y = ~VFSC_Sujeto_1, name = 'VFSC Sujeto 1', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~VFSC_Sujeto_2, name = 'VFSC Sujeto 2', mode = 'lines')
p
		
		
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep><<<<<<< HEAD
<<<<<<< HEAD
install.packages('lime', dep = TRUE)
install.packages('maxent', dep = TRUE)
install.packages('tm', dep = TRUE)
install.packages('SnowballC', dep = TRUE)
install.packages('xlsx', dep = TRUE)
install.packages('wordcloud', dep = TRUE)

rm(list=ls(all=TRUE))
library(lime)
library(maxent)
library(tm)
library(SnowballC)
library(xlsx)
library(wordcloud)

data(train_sentences)

data(test_sentences)

train_corpus <- Corpus(VectorSource(train_sentences$text))
test_corpus <- Corpus(VectorSource(test_sentences$text))

for (i in 1:10) print (train_corpus[[i]]$content) 

#Remover Puntuación xx
train_corpus <- tm_map(train_corpus, content_transformer(removePunctuation))
test_corpus <- tm_map(test_corpus, content_transformer(removePunctuation))

#Remover Stop Words xx
train_corpus <- tm_map(train_corpus, content_transformer(removeWords), stopwords("english"))
test_corpus <- tm_map(test_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case xx
train_corpus <- tm_map(train_corpus, content_transformer(tolower))
train_corpus <- tm_map(train_corpus, content_transformer(removeWords), stopwords("english"))
test_corpus <- tm_map(test_corpus, content_transformer(tolower))
test_corpus <- tm_map(test_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming xx
train_corpus <- tm_map(train_corpus, stemDocument)
test_corpus <- tm_map(test_corpus, stemDocument)

#Remover White Space xx
train_corpus <- tm_map(train_corpus, stripWhitespace)
test_corpus <- tm_map(test_corpus, stripWhitespace)

#Remover Números xx
train_corpus <- tm_map(train_corpus, content_transformer(removeNumbers))
test_corpus <- tm_map(test_corpus, content_transformer(removeNumbers))

train_matrix <- DocumentTermMatrix(train_corpus)
test_matrix <- DocumentTermMatrix(test_corpus)

train_sparse <- as.compressed.matrix(train_matrix) 
test_sparse <- as.compressed.matrix(test_matrix) 

f <- tune.maxent(train_sparse,train_sentences$class.text,nfold=3,showall=TRUE, verbose=TRUE) 

print(f)

model<-maxent(train_sparse,train_sentences$class.text, l1_regularizer=0.0,
l2_regularizer=0.8, use_sgd=FALSE, set_heldout=0, verbose=TRUE)

results <- predict(model,test_sparse) 

out = matrix(, nrow = length(results[,1]), ncol = 2)

for(i in 1:length(results[,1])){
	value <- results[i,2]
	mejorJ <- 2
	for(j in 3:16){
		if(value > results[i,j]){
			#results[i,j] <- FALSE
		}else{
			value <- results[i,j]
			mejorJ <- j
			#results[i,2] <- FALSE
		}
	}
	
	out[i, 1] <- results[i,1]
	if(results[i,1] == substr(names(results[i,mejorJ]), 1, 4)){
		out[i, 2] <- 1
	}else{
		out[i, 2] <- 0
	}
	#results[i,mejorJ] <- TRUE
}

out_charts_correcto = matrix(unique(substr(names(results[1,])[2:16], 1, 4)),, nrow = length(unique(substr(names(results[1,])[2:16], 1, 4))), ncol = 2)
out_charts_noCorrecto = matrix(unique(substr(names(results[1,])[2:16], 1, 4)),, nrow = length(unique(substr(names(results[1,])[2:16], 1, 4))), ncol = 2)

out_charts_correcto[,2] <- 0
out_charts_noCorrecto[,2] <- 0

cantDatosCorrectos = 0
cantDatosIncorrectos = 0
for(i in 1:length(out_charts_correcto[,1])){
	cantUnos = 0
	cantCeros = 0
	for(j in 1:length(results[,1])){
		if(substr(out_charts_correcto[i,1], 1, 4) == substr(out[j,1], 1, 4) && out[j,2] == 1){
			cantUnos = cantUnos + 1
			out_charts_correcto[i, 2] <- cantUnos
		}else if(substr(out_charts_correcto[i,1], 1, 4) == substr(out[j,1], 1, 4) && out[j,2] == 0){
			cantCeros = cantCeros + 1
			out_charts_noCorrecto[i, 2] <- cantCeros
		}	
	}
	cantDatosCorrectos <- cantDatosCorrectos + as.numeric(out_charts_correcto[i, 2])
	cantDatosIncorrectos <- cantDatosIncorrectos + as.numeric(out_charts_noCorrecto[i, 2])
}

out_charts_noCorrecto[1:5,]
out_charts_correcto[1:5,]

##grafico de distribucion de etiquetas - TRAIN
distribucion_etiquetas_train <- matrix(names(results[1,])[2:6],, nrow = length(names(results[1,])[2:6]), ncol = 2)
distribucion_etiquetas_train[,2] <- 0
for(i in 1:length(distribucion_etiquetas_train[,1])){
	for(j in 1:length(train_sentences$class.text)){
		if(distribucion_etiquetas_train[i,1] == substr(train_sentences$class.text[j], 1, 4)){
			distribucion_etiquetas_train[i,2] = as.numeric(distribucion_etiquetas_train[i,2]) + 1
		}
	}
}
barplot(as.numeric(distribucion_etiquetas_train[1:5,2]), main=paste("Distribución Train", toString(length(train_sentences$class.text)), sep=" - "), 
  	xlab="Etiquetas",names.arg = distribucion_etiquetas_train[1:5,1])


##grafico de distribucion de etiquetas - TEST
distribucion_etiquetas_test = matrix(names(results[1,])[2:6],, nrow = length(names(results[1,])[2:6]), ncol = 2)
distribucion_etiquetas_test[,2] <- 0
for(i in 1:length(results[1:5,1])){
	distribucion_etiquetas_test[i,2] <- as.numeric(out_charts_correcto[i,2]) + as.numeric(out_charts_noCorrecto[i,2])
	
}
barplot(as.numeric(distribucion_etiquetas_test[1:5,2]), main=paste("Distribución Test", toString(length(test_sentences$class.text)), sep=" - "), 
  	xlab="Etiquetas",names.arg = distribucion_etiquetas_test[1:5,1])
	
	
##grafico de frecuencia de palabras por etiqueta
etiquetas <- c("OWNX", "MISC", "CONT", "AIMX", "BASE")

n_Text = 1
#_--------------OWNX
m_OWNX = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[1]){
		m_OWNX[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
OWNX_corpus <- Corpus(VectorSource(m_OWNX))
#Remover Puntuación
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removePunctuation))

#Remover Stop Words
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(tolower))
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
OWNX_corpus <- tm_map(OWNX_corpus, stemDocument)

#Remover White Space
OWNX_corpus <- tm_map(OWNX_corpus, stripWhitespace)

#Remover Números
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removeNumbers))

tdm_OWNX <- TermDocumentMatrix(OWNX_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_OWNX)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
		  
		  
n_Text = 1  
#_--------------MISC
m_MISC = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[2]){
		m_MISC[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
MISC_corpus <- Corpus(VectorSource(m_MISC))

#Remover Puntuación
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removePunctuation))

#Remover Stop Words
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
MISC_corpus <- tm_map(MISC_corpus, content_transformer(tolower))
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
MISC_corpus <- tm_map(MISC_corpus, stemDocument)

#Remover White Space
MISC_corpus <- tm_map(MISC_corpus, stripWhitespace)

#Remover Números
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removeNumbers))

tdm_MISC <- TermDocumentMatrix(MISC_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_MISC)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
		  
		  
n_Text = 1  
#_--------------CONT
m_CONT = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[3]){
		m_CONT[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
CONT_corpus <- Corpus(VectorSource(m_CONT))

#Remover Puntuación
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removePunctuation))

#Remover Stop Words
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
CONT_corpus <- tm_map(CONT_corpus, content_transformer(tolower))
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
CONT_corpus <- tm_map(CONT_corpus, stemDocument)

#Remover White Space
CONT_corpus <- tm_map(CONT_corpus, stripWhitespace)

#Remover Números
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removeNumbers))

tdm_CONT <- TermDocumentMatrix(CONT_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_CONT)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
	
	
n_Text = 1  
#_--------------AIMX
m_AIMX = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[4]){
		m_AIMX[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
AIMX_corpus <- Corpus(VectorSource(m_AIMX))

#Remover Puntuación
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removePunctuation))

#Remover Stop Words
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(tolower))
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
AIMX_corpus <- tm_map(AIMX_corpus, stemDocument)

#Remover White Space
AIMX_corpus <- tm_map(AIMX_corpus, stripWhitespace)

#Remover Números
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removeNumbers))

tdm_AIMX <- TermDocumentMatrix(AIMX_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_AIMX)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
		  
		  
n_Text = 1  
#_--------------BASE
m_BASE = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[5]){
		m_BASE[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
BASE_corpus <- Corpus(VectorSource(m_BASE))

#Remover Puntuación
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removePunctuation))

#Remover Stop Words
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
BASE_corpus <- tm_map(BASE_corpus, content_transformer(tolower))
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
BASE_corpus <- tm_map(BASE_corpus, stemDocument)

#Remover White Space
BASE_corpus <- tm_map(BASE_corpus, stripWhitespace)

#Remover Números
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removeNumbers))

tdm_BASE <- TermDocumentMatrix(BASE_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_BASE)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)

####################
##################
##grafico de torta Incorectos
pie(as.numeric(out_charts_noCorrecto[1:5,2]), labels = out_charts_noCorrecto[1:5,2], main=paste("Pie Chart Predicción Incorrecta", toString(cantDatosIncorrectos), sep=" - "),col = rainbow(length(out_charts_noCorrecto[1:5,1])))
legend("topright", out_charts_noCorrecto[1:5,1], cex = 0.8,
   fill = rainbow(length(out_charts_noCorrecto[1:5,1])))

##grafio de lineas entre correctos e incorrectos   
plot(as.numeric(out_charts_correcto[1:5,2]),type = "o",col = "blue", xlab = "Etiquetas", ylab = "Cantidad Predicciones", 
   main = "Clasificación")
lines(out_charts_noCorrecto[1:5,2], type = "o", col = "red")
legend("topright", out_charts_correcto[1:5,1], cex = 0.8)

##grafico de barra entre correctos e incorrectos 
set.union <- function(a, b) {
  u <- a
  for (i in 1:length(b)) {
    u <- append(u, b[i])
    
  }
  return(u)
}
Values <- matrix(set.union(as.numeric(out_charts_correcto[1:5,2]), as.numeric(out_charts_noCorrecto[1:5,2])),nrow = 2,ncol = 5,byrow = TRUE)
colors <- c("green","red")
et <- c(paste("Correctas", toString(cantDatosCorrectos), sep=" "),paste("Incorrectos", toString(cantDatosIncorrectos), sep=" "))

barplot(Values, main="Predicción", 
  	ylab="Número de Predicciones",names.arg = out_charts_correcto[1:5,1], 
	xlab = "Etiquetas",col = colors)
  
legend("topright", et, cex = 1.3, fill = colors)
############# MATRIZ DE CONFUCION  


Labels <- c("MISC", "OWNX", "AIMX", "BASE", "CONT");

for(l in 1:length(Labels)){

	m_confucion = matrix(nrow = 3, ncol = 3)
	m_confucion[,] <- 0
	m_confucion[1,1] <- ""
	m_confucion[1,2] <- "Relevantes"
	m_confucion[1,3] <- "No Relevantes"   
	m_confucion[2,1] <- "Recuperados"
	m_confucion[3,1] <- "No Recuperados" 

	#Recuperados
	for(i in 1:length(out_charts_correcto[1:5,1])){
		#Relevante#
		if(out_charts_correcto[i,1] == Labels[l]){
			if(as.numeric(out_charts_correcto[i,2])>0){
				#Verdadero Positivo
				m_confucion[2,2] <- as.numeric(m_confucion[2,2]) + as.numeric(out_charts_correcto[i,2])
			}
		}else{
			#No Relevante#
			if(as.numeric(out_charts_correcto[i,2])>0){
				#Falso Positivo
				m_confucion[2,3] <- as.numeric(m_confucion[2,3]) + as.numeric(out_charts_correcto[i,2])
			}
		}
	}

	#No Recuperados
	for(i in 1:length(out_charts_noCorrecto[1:5,1])){
		#Relevante#
		if(out_charts_noCorrecto[i,1] == Labels[l]){
			if(as.numeric(out_charts_noCorrecto[i,2])>0){
				#Falso Negativo
				m_confucion[3,2] <- as.numeric(m_confucion[3,2]) + as.numeric(out_charts_noCorrecto[i,2])
			}
		}else{
			#No Relevante#
			if(as.numeric(out_charts_noCorrecto[i,2])>0){
				#Verdadero Negativo
				m_confucion[3,3] <- as.numeric(m_confucion[3,3]) + as.numeric(out_charts_noCorrecto[i,2])
			}
		}
	}
	print(Labels[l])
	print(m_confucion)  

	#Precision
	Precision <- as.numeric(m_confucion[2,2])/(as.numeric(m_confucion[2,2]) + as.numeric(m_confucion[2,3]))

	#Recall
	Recall <- as.numeric(m_confucion[2,2])/(as.numeric(m_confucion[2,2]) + as.numeric(m_confucion[3,2]))

	##Fscore
	Fscore = (2*Precision*Recall)/(Precision+Recall)
	print("Fscore")
	print(Fscore)
	print("Precision")
	print(Precision)
	print("Recall")
	print(Recall)
	
	print("-----------------------------------------");
	
}



###########################################################
##Grafico Resultados Fscore
fScore <- c(0.0685155,0.06896552,0.096,0.07830343,0.09888357, 0.09888357, 0.1032258, 0.08064516)
Etiquetas <- c("1- Sin Pre-Pro", "2- Remover Puntuación", "3- Remover Stop Words","4- To Lower Case","5- Stremming","6- Remover White Space","7- Remover Números", "8- Sin To Lower Case")

plot(fScore,type = "o", col = "red", xlab = "Procesamiento", ylab = "Fscore",
   main = "Procesamiento")
legend("bottomright", Etiquetas, cex = 0.8)
   
   
setwd("C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\Trabajos\\5")
write.xlsx(as.data.frame(results), "Prediccion.xlsx", sheetName="Sheet1")

##Grafico de Lineas Nuevo
 library(plotly)

 MISC <- c(0.7673216, 0.7983952, 0.7549531, 0.7431579, 0.7214514, 0.7241747, 0.7983952)
 OWNX <- c(0.3541076, 0.3239437, 0.4717949, 0.498094, 0.4627451, 0.4728682, 0.3638889)
 AIMX <- c(0.1855346, 0.17737, 0.06168831, 0.05280528, 0.1064516, 0.09983897, 0.08780488)
 BASE <- c(0.003460208, 0.01666667, 0.01666667, 0.01013514, 0.02356902, 0.02013423, 0.03672788)
 CONT <- c(0.07023411, 0.06188925, 0.08064516, 0.06885246, 0.1033926, 0.09677419, 0.06896552)




x <- c(1:7)

data <- data.frame(x, MISC, OWNX, AIMX, BASE, CONT)

p <- plot_ly(data, x = ~x, y = ~MISC, name = 'MISC', type = 'scatter', mode = 'lines+markers') %>%
  add_trace(y = ~OWNX, name = 'OWNX', mode = 'lines+markers') %>%
  add_trace(y = ~AIMX, name = 'AIMX', mode = 'lines+markers') %>%
  add_trace(y = ~BASE, name = 'BASE', mode = 'lines+markers') %>%
  add_trace(y = ~CONT, name = 'CONT', mode = 'lines+markers') %>%
  layout(title = "Fscore Segun Pre-Procesamiento",
         xaxis = list(title = "Pre-Procesamiento"),
         yaxis = list (title = "Fscore"))


p


## 7 - Remover Números
[1] "MISC"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "398"        "190"          
[3,] "No Recuperados" "11"         "1"            
[1] "Fscore"
[1] 0.7983952
[1] "Precision"
[1] 0.6768707
[1] "Recall"
[1] 0.9731051
[1] "-----------------------------------------"
[1] "OWNX"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "131"        "457"          
[3,] "No Recuperados" "1"          "11"           
[1] "Fscore"
[1] 0.3638889
[1] "Precision"
[1] 0.2227891
[1] "Recall"
[1] 0.9924242
[1] "-----------------------------------------"
[1] "AIMX"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "27"         "561"          
[3,] "No Recuperados" "0"          "12"           
[1] "Fscore"
[1] 0.08780488
[1] "Precision"
[1] 0.04591837
[1] "Recall"
[1] 1
[1] "-----------------------------------------"
[1] "BASE"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "11"         "577"          
[3,] "No Recuperados" "0"          "12"           
[1] "Fscore"
[1] 0.03672788
[1] "Precision"
[1] 0.01870748
[1] "Recall"
[1] 1
[1] "-----------------------------------------"
[1] "CONT"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "21"         "567"          
[3,] "No Recuperados" "0"          "12"           
[1] "Fscore"
[1] 0.06896552
[1] "Precision"
[1] 0.03571429
[1] "Recall"
[1] 1
[1] "-----------------------------------------"

=======
install.packages('lime', dep = TRUE)
install.packages('maxent', dep = TRUE)
install.packages('tm', dep = TRUE)
install.packages('SnowballC', dep = TRUE)
install.packages('xlsx', dep = TRUE)
install.packages('wordcloud', dep = TRUE)

rm(list=ls(all=TRUE))
library(lime)
library(maxent)
library(tm)
library(SnowballC)
library(xlsx)
library(wordcloud)

data(train_sentences)

data(test_sentences)

train_corpus <- Corpus(VectorSource(train_sentences$text))
test_corpus <- Corpus(VectorSource(test_sentences$text))

for (i in 1:10) print (train_corpus[[i]]$content) 

#Remover Puntuación xx
train_corpus <- tm_map(train_corpus, content_transformer(removePunctuation))
test_corpus <- tm_map(test_corpus, content_transformer(removePunctuation))

#Remover Stop Words xx
train_corpus <- tm_map(train_corpus, content_transformer(removeWords), stopwords("english"))
test_corpus <- tm_map(test_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case xx
train_corpus <- tm_map(train_corpus, content_transformer(tolower))
train_corpus <- tm_map(train_corpus, content_transformer(removeWords), stopwords("english"))
test_corpus <- tm_map(test_corpus, content_transformer(tolower))
test_corpus <- tm_map(test_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming xx
train_corpus <- tm_map(train_corpus, stemDocument)
test_corpus <- tm_map(test_corpus, stemDocument)

#Remover White Space xx
train_corpus <- tm_map(train_corpus, stripWhitespace)
test_corpus <- tm_map(test_corpus, stripWhitespace)

#Remover Números xx
train_corpus <- tm_map(train_corpus, content_transformer(removeNumbers))
test_corpus <- tm_map(test_corpus, content_transformer(removeNumbers))

train_matrix <- DocumentTermMatrix(train_corpus)
test_matrix <- DocumentTermMatrix(test_corpus)

train_sparse <- as.compressed.matrix(train_matrix) 
test_sparse <- as.compressed.matrix(test_matrix) 

f <- tune.maxent(train_sparse,train_sentences$class.text,nfold=3,showall=TRUE, verbose=TRUE) 

print(f)

model<-maxent(train_sparse,train_sentences$class.text, l1_regularizer=0.0,
l2_regularizer=0.8, use_sgd=FALSE, set_heldout=0, verbose=TRUE)

results <- predict(model,test_sparse) 

out = matrix(, nrow = length(results[,1]), ncol = 2)

for(i in 1:length(results[,1])){
	value <- results[i,2]
	mejorJ <- 2
	for(j in 3:16){
		if(value > results[i,j]){
			#results[i,j] <- FALSE
		}else{
			value <- results[i,j]
			mejorJ <- j
			#results[i,2] <- FALSE
		}
	}
	
	out[i, 1] <- results[i,1]
	if(results[i,1] == substr(names(results[i,mejorJ]), 1, 4)){
		out[i, 2] <- 1
	}else{
		out[i, 2] <- 0
	}
	#results[i,mejorJ] <- TRUE
}

out_charts_correcto = matrix(unique(substr(names(results[1,])[2:16], 1, 4)),, nrow = length(unique(substr(names(results[1,])[2:16], 1, 4))), ncol = 2)
out_charts_noCorrecto = matrix(unique(substr(names(results[1,])[2:16], 1, 4)),, nrow = length(unique(substr(names(results[1,])[2:16], 1, 4))), ncol = 2)

out_charts_correcto[,2] <- 0
out_charts_noCorrecto[,2] <- 0

cantDatosCorrectos = 0
cantDatosIncorrectos = 0
for(i in 1:length(out_charts_correcto[,1])){
	cantUnos = 0
	cantCeros = 0
	for(j in 1:length(results[,1])){
		if(substr(out_charts_correcto[i,1], 1, 4) == substr(out[j,1], 1, 4) && out[j,2] == 1){
			cantUnos = cantUnos + 1
			out_charts_correcto[i, 2] <- cantUnos
		}else if(substr(out_charts_correcto[i,1], 1, 4) == substr(out[j,1], 1, 4) && out[j,2] == 0){
			cantCeros = cantCeros + 1
			out_charts_noCorrecto[i, 2] <- cantCeros
		}	
	}
	cantDatosCorrectos <- cantDatosCorrectos + as.numeric(out_charts_correcto[i, 2])
	cantDatosIncorrectos <- cantDatosIncorrectos + as.numeric(out_charts_noCorrecto[i, 2])
}

out_charts_noCorrecto[1:5,]
out_charts_correcto[1:5,]

##grafico de distribucion de etiquetas - TRAIN
distribucion_etiquetas_train <- matrix(names(results[1,])[2:6],, nrow = length(names(results[1,])[2:6]), ncol = 2)
distribucion_etiquetas_train[,2] <- 0
for(i in 1:length(distribucion_etiquetas_train[,1])){
	for(j in 1:length(train_sentences$class.text)){
		if(distribucion_etiquetas_train[i,1] == substr(train_sentences$class.text[j], 1, 4)){
			distribucion_etiquetas_train[i,2] = as.numeric(distribucion_etiquetas_train[i,2]) + 1
		}
	}
}
barplot(as.numeric(distribucion_etiquetas_train[1:5,2]), main=paste("Distribución Train", toString(length(train_sentences$class.text)), sep=" - "), 
  	xlab="Etiquetas",names.arg = distribucion_etiquetas_train[1:5,1])


##grafico de distribucion de etiquetas - TEST
distribucion_etiquetas_test = matrix(names(results[1,])[2:6],, nrow = length(names(results[1,])[2:6]), ncol = 2)
distribucion_etiquetas_test[,2] <- 0
for(i in 1:length(results[1:5,1])){
	distribucion_etiquetas_test[i,2] <- as.numeric(out_charts_correcto[i,2]) + as.numeric(out_charts_noCorrecto[i,2])
	
}
barplot(as.numeric(distribucion_etiquetas_test[1:5,2]), main=paste("Distribución Test", toString(length(test_sentences$class.text)), sep=" - "), 
  	xlab="Etiquetas",names.arg = distribucion_etiquetas_test[1:5,1])
	
	
##grafico de frecuencia de palabras por etiqueta
etiquetas <- c("OWNX", "MISC", "CONT", "AIMX", "BASE")

n_Text = 1
#_--------------OWNX
m_OWNX = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[1]){
		m_OWNX[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
OWNX_corpus <- Corpus(VectorSource(m_OWNX))
#Remover Puntuación
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removePunctuation))

#Remover Stop Words
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(tolower))
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
OWNX_corpus <- tm_map(OWNX_corpus, stemDocument)

#Remover White Space
OWNX_corpus <- tm_map(OWNX_corpus, stripWhitespace)

#Remover Números
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removeNumbers))

tdm_OWNX <- TermDocumentMatrix(OWNX_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_OWNX)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
		  
		  
n_Text = 1  
#_--------------MISC
m_MISC = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[2]){
		m_MISC[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
MISC_corpus <- Corpus(VectorSource(m_MISC))

#Remover Puntuación
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removePunctuation))

#Remover Stop Words
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
MISC_corpus <- tm_map(MISC_corpus, content_transformer(tolower))
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
MISC_corpus <- tm_map(MISC_corpus, stemDocument)

#Remover White Space
MISC_corpus <- tm_map(MISC_corpus, stripWhitespace)

#Remover Números
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removeNumbers))

tdm_MISC <- TermDocumentMatrix(MISC_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_MISC)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
		  
		  
n_Text = 1  
#_--------------CONT
m_CONT = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[3]){
		m_CONT[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
CONT_corpus <- Corpus(VectorSource(m_CONT))

#Remover Puntuación
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removePunctuation))

#Remover Stop Words
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
CONT_corpus <- tm_map(CONT_corpus, content_transformer(tolower))
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
CONT_corpus <- tm_map(CONT_corpus, stemDocument)

#Remover White Space
CONT_corpus <- tm_map(CONT_corpus, stripWhitespace)

#Remover Números
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removeNumbers))

tdm_CONT <- TermDocumentMatrix(CONT_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_CONT)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
	
	
n_Text = 1  
#_--------------AIMX
m_AIMX = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[4]){
		m_AIMX[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
AIMX_corpus <- Corpus(VectorSource(m_AIMX))

#Remover Puntuación
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removePunctuation))

#Remover Stop Words
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(tolower))
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
AIMX_corpus <- tm_map(AIMX_corpus, stemDocument)

#Remover White Space
AIMX_corpus <- tm_map(AIMX_corpus, stripWhitespace)

#Remover Números
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removeNumbers))

tdm_AIMX <- TermDocumentMatrix(AIMX_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_AIMX)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
		  
		  
n_Text = 1  
#_--------------BASE
m_BASE = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[5]){
		m_BASE[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
BASE_corpus <- Corpus(VectorSource(m_BASE))

#Remover Puntuación
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removePunctuation))

#Remover Stop Words
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
BASE_corpus <- tm_map(BASE_corpus, content_transformer(tolower))
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
BASE_corpus <- tm_map(BASE_corpus, stemDocument)

#Remover White Space
BASE_corpus <- tm_map(BASE_corpus, stripWhitespace)

#Remover Números
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removeNumbers))

tdm_BASE <- TermDocumentMatrix(BASE_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_BASE)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)

####################
##################
##grafico de torta Incorectos
pie(as.numeric(out_charts_noCorrecto[1:5,2]), labels = out_charts_noCorrecto[1:5,2], main=paste("Pie Chart Predicción Incorrecta", toString(cantDatosIncorrectos), sep=" - "),col = rainbow(length(out_charts_noCorrecto[1:5,1])))
legend("topright", out_charts_noCorrecto[1:5,1], cex = 0.8,
   fill = rainbow(length(out_charts_noCorrecto[1:5,1])))

##grafio de lineas entre correctos e incorrectos   
plot(as.numeric(out_charts_correcto[1:5,2]),type = "o",col = "blue", xlab = "Etiquetas", ylab = "Cantidad Predicciones", 
   main = "Clasificación")
lines(out_charts_noCorrecto[1:5,2], type = "o", col = "red")
legend("topright", out_charts_correcto[1:5,1], cex = 0.8)

##grafico de barra entre correctos e incorrectos 
set.union <- function(a, b) {
  u <- a
  for (i in 1:length(b)) {
    u <- append(u, b[i])
    
  }
  return(u)
}
Values <- matrix(set.union(as.numeric(out_charts_correcto[1:5,2]), as.numeric(out_charts_noCorrecto[1:5,2])),nrow = 2,ncol = 5,byrow = TRUE)
colors <- c("green","red")
et <- c(paste("Correctas", toString(cantDatosCorrectos), sep=" "),paste("Incorrectos", toString(cantDatosIncorrectos), sep=" "))

barplot(Values, main="Predicción", 
  	ylab="Número de Predicciones",names.arg = out_charts_correcto[1:5,1], 
	xlab = "Etiquetas",col = colors)
  
legend("topright", et, cex = 1.3, fill = colors)
############# MATRIZ DE CONFUCION  


Labels <- c("MISC", "OWNX", "AIMX", "BASE", "CONT");

for(l in 1:length(Labels)){

	m_confucion = matrix(nrow = 3, ncol = 3)
	m_confucion[,] <- 0
	m_confucion[1,1] <- ""
	m_confucion[1,2] <- "Relevantes"
	m_confucion[1,3] <- "No Relevantes"   
	m_confucion[2,1] <- "Recuperados"
	m_confucion[3,1] <- "No Recuperados" 

	#Recuperados
	for(i in 1:length(out_charts_correcto[1:5,1])){
		#Relevante#
		if(out_charts_correcto[i,1] == Labels[l]){
			if(as.numeric(out_charts_correcto[i,2])>0){
				#Verdadero Positivo
				m_confucion[2,2] <- as.numeric(m_confucion[2,2]) + as.numeric(out_charts_correcto[i,2])
			}
		}else{
			#No Relevante#
			if(as.numeric(out_charts_correcto[i,2])>0){
				#Falso Positivo
				m_confucion[2,3] <- as.numeric(m_confucion[2,3]) + as.numeric(out_charts_correcto[i,2])
			}
		}
	}

	#No Recuperados
	for(i in 1:length(out_charts_noCorrecto[1:5,1])){
		#Relevante#
		if(out_charts_noCorrecto[i,1] == Labels[l]){
			if(as.numeric(out_charts_noCorrecto[i,2])>0){
				#Falso Negativo
				m_confucion[3,2] <- as.numeric(m_confucion[3,2]) + as.numeric(out_charts_noCorrecto[i,2])
			}
		}else{
			#No Relevante#
			if(as.numeric(out_charts_noCorrecto[i,2])>0){
				#Verdadero Negativo
				m_confucion[3,3] <- as.numeric(m_confucion[3,3]) + as.numeric(out_charts_noCorrecto[i,2])
			}
		}
	}
	print(Labels[l])
	print(m_confucion)  

	#Precision
	Precision <- as.numeric(m_confucion[2,2])/(as.numeric(m_confucion[2,2]) + as.numeric(m_confucion[2,3]))

	#Recall
	Recall <- as.numeric(m_confucion[2,2])/(as.numeric(m_confucion[2,2]) + as.numeric(m_confucion[3,2]))

	##Fscore
	Fscore = (2*Precision*Recall)/(Precision+Recall)
	print("Fscore")
	print(Fscore)
	print("Precision")
	print(Precision)
	print("Recall")
	print(Recall)
	
	print("-----------------------------------------");
	
}



###########################################################
##Grafico Resultados Fscore
fScore <- c(0.0685155,0.06896552,0.096,0.07830343,0.09888357, 0.09888357, 0.1032258, 0.08064516)
Etiquetas <- c("1- Sin Pre-Pro", "2- Remover Puntuación", "3- Remover Stop Words","4- To Lower Case","5- Stremming","6- Remover White Space","7- Remover Números", "8- Sin To Lower Case")

plot(fScore,type = "o", col = "red", xlab = "Procesamiento", ylab = "Fscore",
   main = "Procesamiento")
legend("bottomright", Etiquetas, cex = 0.8)
   
   
setwd("C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\Trabajos\\5")
write.xlsx(as.data.frame(results), "Prediccion.xlsx", sheetName="Sheet1")

##Grafico de Lineas Nuevo
 library(plotly)

 MISC <- c(0.7673216, 0.7983952, 0.7549531, 0.7431579, 0.7214514, 0.7241747, 0.7983952)
 OWNX <- c(0.3541076, 0.3239437, 0.4717949, 0.498094, 0.4627451, 0.4728682, 0.3638889)
 AIMX <- c(0.1855346, 0.17737, 0.06168831, 0.05280528, 0.1064516, 0.09983897, 0.08780488)
 BASE <- c(0.003460208, 0.01666667, 0.01666667, 0.01013514, 0.02356902, 0.02013423, 0.03672788)
 CONT <- c(0.07023411, 0.06188925, 0.08064516, 0.06885246, 0.1033926, 0.09677419, 0.06896552)




x <- c(1:7)

data <- data.frame(x, MISC, OWNX, AIMX, BASE, CONT)

p <- plot_ly(data, x = ~x, y = ~MISC, name = 'MISC', type = 'scatter', mode = 'lines+markers') %>%
  add_trace(y = ~OWNX, name = 'OWNX', mode = 'lines+markers') %>%
  add_trace(y = ~AIMX, name = 'AIMX', mode = 'lines+markers') %>%
  add_trace(y = ~BASE, name = 'BASE', mode = 'lines+markers') %>%
  add_trace(y = ~CONT, name = 'CONT', mode = 'lines+markers') %>%
  layout(title = "Fscore Segun Pre-Procesamiento",
         xaxis = list(title = "Pre-Procesamiento"),
         yaxis = list (title = "Fscore"))


p


## 7 - Remover Números
[1] "MISC"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "398"        "190"          
[3,] "No Recuperados" "11"         "1"            
[1] "Fscore"
[1] 0.7983952
[1] "Precision"
[1] 0.6768707
[1] "Recall"
[1] 0.9731051
[1] "-----------------------------------------"
[1] "OWNX"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "131"        "457"          
[3,] "No Recuperados" "1"          "11"           
[1] "Fscore"
[1] 0.3638889
[1] "Precision"
[1] 0.2227891
[1] "Recall"
[1] 0.9924242
[1] "-----------------------------------------"
[1] "AIMX"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "27"         "561"          
[3,] "No Recuperados" "0"          "12"           
[1] "Fscore"
[1] 0.08780488
[1] "Precision"
[1] 0.04591837
[1] "Recall"
[1] 1
[1] "-----------------------------------------"
[1] "BASE"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "11"         "577"          
[3,] "No Recuperados" "0"          "12"           
[1] "Fscore"
[1] 0.03672788
[1] "Precision"
[1] 0.01870748
[1] "Recall"
[1] 1
[1] "-----------------------------------------"
[1] "CONT"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "21"         "567"          
[3,] "No Recuperados" "0"          "12"           
[1] "Fscore"
[1] 0.06896552
[1] "Precision"
[1] 0.03571429
[1] "Recall"
[1] 1
[1] "-----------------------------------------"

>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
install.packages('lime', dep = TRUE)
install.packages('maxent', dep = TRUE)
install.packages('tm', dep = TRUE)
install.packages('SnowballC', dep = TRUE)
install.packages('xlsx', dep = TRUE)
install.packages('wordcloud', dep = TRUE)

rm(list=ls(all=TRUE))
library(lime)
library(maxent)
library(tm)
library(SnowballC)
library(xlsx)
library(wordcloud)

data(train_sentences)

data(test_sentences)

train_corpus <- Corpus(VectorSource(train_sentences$text))
test_corpus <- Corpus(VectorSource(test_sentences$text))

for (i in 1:10) print (train_corpus[[i]]$content) 

#Remover Puntuación xx
train_corpus <- tm_map(train_corpus, content_transformer(removePunctuation))
test_corpus <- tm_map(test_corpus, content_transformer(removePunctuation))

#Remover Stop Words xx
train_corpus <- tm_map(train_corpus, content_transformer(removeWords), stopwords("english"))
test_corpus <- tm_map(test_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case xx
train_corpus <- tm_map(train_corpus, content_transformer(tolower))
train_corpus <- tm_map(train_corpus, content_transformer(removeWords), stopwords("english"))
test_corpus <- tm_map(test_corpus, content_transformer(tolower))
test_corpus <- tm_map(test_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming xx
train_corpus <- tm_map(train_corpus, stemDocument)
test_corpus <- tm_map(test_corpus, stemDocument)

#Remover White Space xx
train_corpus <- tm_map(train_corpus, stripWhitespace)
test_corpus <- tm_map(test_corpus, stripWhitespace)

#Remover Números xx
train_corpus <- tm_map(train_corpus, content_transformer(removeNumbers))
test_corpus <- tm_map(test_corpus, content_transformer(removeNumbers))

train_matrix <- DocumentTermMatrix(train_corpus)
test_matrix <- DocumentTermMatrix(test_corpus)

train_sparse <- as.compressed.matrix(train_matrix) 
test_sparse <- as.compressed.matrix(test_matrix) 

f <- tune.maxent(train_sparse,train_sentences$class.text,nfold=3,showall=TRUE, verbose=TRUE) 

print(f)

model<-maxent(train_sparse,train_sentences$class.text, l1_regularizer=0.0,
l2_regularizer=0.8, use_sgd=FALSE, set_heldout=0, verbose=TRUE)

results <- predict(model,test_sparse) 

out = matrix(, nrow = length(results[,1]), ncol = 2)

for(i in 1:length(results[,1])){
	value <- results[i,2]
	mejorJ <- 2
	for(j in 3:16){
		if(value > results[i,j]){
			#results[i,j] <- FALSE
		}else{
			value <- results[i,j]
			mejorJ <- j
			#results[i,2] <- FALSE
		}
	}
	
	out[i, 1] <- results[i,1]
	if(results[i,1] == substr(names(results[i,mejorJ]), 1, 4)){
		out[i, 2] <- 1
	}else{
		out[i, 2] <- 0
	}
	#results[i,mejorJ] <- TRUE
}

out_charts_correcto = matrix(unique(substr(names(results[1,])[2:16], 1, 4)),, nrow = length(unique(substr(names(results[1,])[2:16], 1, 4))), ncol = 2)
out_charts_noCorrecto = matrix(unique(substr(names(results[1,])[2:16], 1, 4)),, nrow = length(unique(substr(names(results[1,])[2:16], 1, 4))), ncol = 2)

out_charts_correcto[,2] <- 0
out_charts_noCorrecto[,2] <- 0

cantDatosCorrectos = 0
cantDatosIncorrectos = 0
for(i in 1:length(out_charts_correcto[,1])){
	cantUnos = 0
	cantCeros = 0
	for(j in 1:length(results[,1])){
		if(substr(out_charts_correcto[i,1], 1, 4) == substr(out[j,1], 1, 4) && out[j,2] == 1){
			cantUnos = cantUnos + 1
			out_charts_correcto[i, 2] <- cantUnos
		}else if(substr(out_charts_correcto[i,1], 1, 4) == substr(out[j,1], 1, 4) && out[j,2] == 0){
			cantCeros = cantCeros + 1
			out_charts_noCorrecto[i, 2] <- cantCeros
		}	
	}
	cantDatosCorrectos <- cantDatosCorrectos + as.numeric(out_charts_correcto[i, 2])
	cantDatosIncorrectos <- cantDatosIncorrectos + as.numeric(out_charts_noCorrecto[i, 2])
}

out_charts_noCorrecto[1:5,]
out_charts_correcto[1:5,]

##grafico de distribucion de etiquetas - TRAIN
distribucion_etiquetas_train <- matrix(names(results[1,])[2:6],, nrow = length(names(results[1,])[2:6]), ncol = 2)
distribucion_etiquetas_train[,2] <- 0
for(i in 1:length(distribucion_etiquetas_train[,1])){
	for(j in 1:length(train_sentences$class.text)){
		if(distribucion_etiquetas_train[i,1] == substr(train_sentences$class.text[j], 1, 4)){
			distribucion_etiquetas_train[i,2] = as.numeric(distribucion_etiquetas_train[i,2]) + 1
		}
	}
}
barplot(as.numeric(distribucion_etiquetas_train[1:5,2]), main=paste("Distribución Train", toString(length(train_sentences$class.text)), sep=" - "), 
  	xlab="Etiquetas",names.arg = distribucion_etiquetas_train[1:5,1])


##grafico de distribucion de etiquetas - TEST
distribucion_etiquetas_test = matrix(names(results[1,])[2:6],, nrow = length(names(results[1,])[2:6]), ncol = 2)
distribucion_etiquetas_test[,2] <- 0
for(i in 1:length(results[1:5,1])){
	distribucion_etiquetas_test[i,2] <- as.numeric(out_charts_correcto[i,2]) + as.numeric(out_charts_noCorrecto[i,2])
	
}
barplot(as.numeric(distribucion_etiquetas_test[1:5,2]), main=paste("Distribución Test", toString(length(test_sentences$class.text)), sep=" - "), 
  	xlab="Etiquetas",names.arg = distribucion_etiquetas_test[1:5,1])
	
	
##grafico de frecuencia de palabras por etiqueta
etiquetas <- c("OWNX", "MISC", "CONT", "AIMX", "BASE")

n_Text = 1
#_--------------OWNX
m_OWNX = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[1]){
		m_OWNX[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
OWNX_corpus <- Corpus(VectorSource(m_OWNX))
#Remover Puntuación
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removePunctuation))

#Remover Stop Words
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(tolower))
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
OWNX_corpus <- tm_map(OWNX_corpus, stemDocument)

#Remover White Space
OWNX_corpus <- tm_map(OWNX_corpus, stripWhitespace)

#Remover Números
OWNX_corpus <- tm_map(OWNX_corpus, content_transformer(removeNumbers))

tdm_OWNX <- TermDocumentMatrix(OWNX_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_OWNX)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
		  
		  
n_Text = 1  
#_--------------MISC
m_MISC = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[2]){
		m_MISC[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
MISC_corpus <- Corpus(VectorSource(m_MISC))

#Remover Puntuación
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removePunctuation))

#Remover Stop Words
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
MISC_corpus <- tm_map(MISC_corpus, content_transformer(tolower))
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
MISC_corpus <- tm_map(MISC_corpus, stemDocument)

#Remover White Space
MISC_corpus <- tm_map(MISC_corpus, stripWhitespace)

#Remover Números
MISC_corpus <- tm_map(MISC_corpus, content_transformer(removeNumbers))

tdm_MISC <- TermDocumentMatrix(MISC_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_MISC)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
		  
		  
n_Text = 1  
#_--------------CONT
m_CONT = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[3]){
		m_CONT[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
CONT_corpus <- Corpus(VectorSource(m_CONT))

#Remover Puntuación
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removePunctuation))

#Remover Stop Words
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
CONT_corpus <- tm_map(CONT_corpus, content_transformer(tolower))
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
CONT_corpus <- tm_map(CONT_corpus, stemDocument)

#Remover White Space
CONT_corpus <- tm_map(CONT_corpus, stripWhitespace)

#Remover Números
CONT_corpus <- tm_map(CONT_corpus, content_transformer(removeNumbers))

tdm_CONT <- TermDocumentMatrix(CONT_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_CONT)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
	
	
n_Text = 1  
#_--------------AIMX
m_AIMX = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[4]){
		m_AIMX[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
AIMX_corpus <- Corpus(VectorSource(m_AIMX))

#Remover Puntuación
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removePunctuation))

#Remover Stop Words
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(tolower))
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
AIMX_corpus <- tm_map(AIMX_corpus, stemDocument)

#Remover White Space
AIMX_corpus <- tm_map(AIMX_corpus, stripWhitespace)

#Remover Números
AIMX_corpus <- tm_map(AIMX_corpus, content_transformer(removeNumbers))

tdm_AIMX <- TermDocumentMatrix(AIMX_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_AIMX)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)
		  
		  
n_Text = 1  
#_--------------BASE
m_BASE = matrix()
for(i in 1:length(test_sentences$class.text)){
	if(substr(test_sentences$class.text[i], 1, 4) == etiquetas[5]){
		m_BASE[n_Text] <- test_sentences$text[i]
		n_Text = n_Text + 1
	}
}
BASE_corpus <- Corpus(VectorSource(m_BASE))

#Remover Puntuación
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removePunctuation))

#Remover Stop Words
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removeWords), stopwords("english"))

#To Lower Case
BASE_corpus <- tm_map(BASE_corpus, content_transformer(tolower))
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removeWords), stopwords("english"))

#Stremming
BASE_corpus <- tm_map(BASE_corpus, stemDocument)

#Remover White Space
BASE_corpus <- tm_map(BASE_corpus, stripWhitespace)

#Remover Números
BASE_corpus <- tm_map(BASE_corpus, content_transformer(removeNumbers))

tdm_BASE <- TermDocumentMatrix(BASE_corpus, control = list(wordLengths = c(1, Inf)))
m <- as.matrix(tdm_BASE)
# calculate the frequency of words and sort it by frequency
word.freq <- sort(rowSums(m), decreasing = T)
wordcloud(main = "Clasificación", words = names(word.freq), freq = word.freq, min.freq = 3,
          random.order = F)

####################
##################
##grafico de torta Incorectos
pie(as.numeric(out_charts_noCorrecto[1:5,2]), labels = out_charts_noCorrecto[1:5,2], main=paste("Pie Chart Predicción Incorrecta", toString(cantDatosIncorrectos), sep=" - "),col = rainbow(length(out_charts_noCorrecto[1:5,1])))
legend("topright", out_charts_noCorrecto[1:5,1], cex = 0.8,
   fill = rainbow(length(out_charts_noCorrecto[1:5,1])))

##grafio de lineas entre correctos e incorrectos   
plot(as.numeric(out_charts_correcto[1:5,2]),type = "o",col = "blue", xlab = "Etiquetas", ylab = "Cantidad Predicciones", 
   main = "Clasificación")
lines(out_charts_noCorrecto[1:5,2], type = "o", col = "red")
legend("topright", out_charts_correcto[1:5,1], cex = 0.8)

##grafico de barra entre correctos e incorrectos 
set.union <- function(a, b) {
  u <- a
  for (i in 1:length(b)) {
    u <- append(u, b[i])
    
  }
  return(u)
}
Values <- matrix(set.union(as.numeric(out_charts_correcto[1:5,2]), as.numeric(out_charts_noCorrecto[1:5,2])),nrow = 2,ncol = 5,byrow = TRUE)
colors <- c("green","red")
et <- c(paste("Correctas", toString(cantDatosCorrectos), sep=" "),paste("Incorrectos", toString(cantDatosIncorrectos), sep=" "))

barplot(Values, main="Predicción", 
  	ylab="Número de Predicciones",names.arg = out_charts_correcto[1:5,1], 
	xlab = "Etiquetas",col = colors)
  
legend("topright", et, cex = 1.3, fill = colors)
############# MATRIZ DE CONFUCION  


Labels <- c("MISC", "OWNX", "AIMX", "BASE", "CONT");

for(l in 1:length(Labels)){

	m_confucion = matrix(nrow = 3, ncol = 3)
	m_confucion[,] <- 0
	m_confucion[1,1] <- ""
	m_confucion[1,2] <- "Relevantes"
	m_confucion[1,3] <- "No Relevantes"   
	m_confucion[2,1] <- "Recuperados"
	m_confucion[3,1] <- "No Recuperados" 

	#Recuperados
	for(i in 1:length(out_charts_correcto[1:5,1])){
		#Relevante#
		if(out_charts_correcto[i,1] == Labels[l]){
			if(as.numeric(out_charts_correcto[i,2])>0){
				#Verdadero Positivo
				m_confucion[2,2] <- as.numeric(m_confucion[2,2]) + as.numeric(out_charts_correcto[i,2])
			}
		}else{
			#No Relevante#
			if(as.numeric(out_charts_correcto[i,2])>0){
				#Falso Positivo
				m_confucion[2,3] <- as.numeric(m_confucion[2,3]) + as.numeric(out_charts_correcto[i,2])
			}
		}
	}

	#No Recuperados
	for(i in 1:length(out_charts_noCorrecto[1:5,1])){
		#Relevante#
		if(out_charts_noCorrecto[i,1] == Labels[l]){
			if(as.numeric(out_charts_noCorrecto[i,2])>0){
				#Falso Negativo
				m_confucion[3,2] <- as.numeric(m_confucion[3,2]) + as.numeric(out_charts_noCorrecto[i,2])
			}
		}else{
			#No Relevante#
			if(as.numeric(out_charts_noCorrecto[i,2])>0){
				#Verdadero Negativo
				m_confucion[3,3] <- as.numeric(m_confucion[3,3]) + as.numeric(out_charts_noCorrecto[i,2])
			}
		}
	}
	print(Labels[l])
	print(m_confucion)  

	#Precision
	Precision <- as.numeric(m_confucion[2,2])/(as.numeric(m_confucion[2,2]) + as.numeric(m_confucion[2,3]))

	#Recall
	Recall <- as.numeric(m_confucion[2,2])/(as.numeric(m_confucion[2,2]) + as.numeric(m_confucion[3,2]))

	##Fscore
	Fscore = (2*Precision*Recall)/(Precision+Recall)
	print("Fscore")
	print(Fscore)
	print("Precision")
	print(Precision)
	print("Recall")
	print(Recall)
	
	print("-----------------------------------------");
	
}



###########################################################
##Grafico Resultados Fscore
fScore <- c(0.0685155,0.06896552,0.096,0.07830343,0.09888357, 0.09888357, 0.1032258, 0.08064516)
Etiquetas <- c("1- Sin Pre-Pro", "2- Remover Puntuación", "3- Remover Stop Words","4- To Lower Case","5- Stremming","6- Remover White Space","7- Remover Números", "8- Sin To Lower Case")

plot(fScore,type = "o", col = "red", xlab = "Procesamiento", ylab = "Fscore",
   main = "Procesamiento")
legend("bottomright", Etiquetas, cex = 0.8)
   
   
setwd("C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\Trabajos\\5")
write.xlsx(as.data.frame(results), "Prediccion.xlsx", sheetName="Sheet1")

##Grafico de Lineas Nuevo
 library(plotly)

 MISC <- c(0.7673216, 0.7983952, 0.7549531, 0.7431579, 0.7214514, 0.7241747, 0.7983952)
 OWNX <- c(0.3541076, 0.3239437, 0.4717949, 0.498094, 0.4627451, 0.4728682, 0.3638889)
 AIMX <- c(0.1855346, 0.17737, 0.06168831, 0.05280528, 0.1064516, 0.09983897, 0.08780488)
 BASE <- c(0.003460208, 0.01666667, 0.01666667, 0.01013514, 0.02356902, 0.02013423, 0.03672788)
 CONT <- c(0.07023411, 0.06188925, 0.08064516, 0.06885246, 0.1033926, 0.09677419, 0.06896552)




x <- c(1:7)

data <- data.frame(x, MISC, OWNX, AIMX, BASE, CONT)

p <- plot_ly(data, x = ~x, y = ~MISC, name = 'MISC', type = 'scatter', mode = 'lines+markers') %>%
  add_trace(y = ~OWNX, name = 'OWNX', mode = 'lines+markers') %>%
  add_trace(y = ~AIMX, name = 'AIMX', mode = 'lines+markers') %>%
  add_trace(y = ~BASE, name = 'BASE', mode = 'lines+markers') %>%
  add_trace(y = ~CONT, name = 'CONT', mode = 'lines+markers') %>%
  layout(title = "Fscore Segun Pre-Procesamiento",
         xaxis = list(title = "Pre-Procesamiento"),
         yaxis = list (title = "Fscore"))


p


## 7 - Remover Números
[1] "MISC"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "398"        "190"          
[3,] "No Recuperados" "11"         "1"            
[1] "Fscore"
[1] 0.7983952
[1] "Precision"
[1] 0.6768707
[1] "Recall"
[1] 0.9731051
[1] "-----------------------------------------"
[1] "OWNX"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "131"        "457"          
[3,] "No Recuperados" "1"          "11"           
[1] "Fscore"
[1] 0.3638889
[1] "Precision"
[1] 0.2227891
[1] "Recall"
[1] 0.9924242
[1] "-----------------------------------------"
[1] "AIMX"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "27"         "561"          
[3,] "No Recuperados" "0"          "12"           
[1] "Fscore"
[1] 0.08780488
[1] "Precision"
[1] 0.04591837
[1] "Recall"
[1] 1
[1] "-----------------------------------------"
[1] "BASE"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "11"         "577"          
[3,] "No Recuperados" "0"          "12"           
[1] "Fscore"
[1] 0.03672788
[1] "Precision"
[1] 0.01870748
[1] "Recall"
[1] 1
[1] "-----------------------------------------"
[1] "CONT"
     [,1]             [,2]         [,3]           
[1,] ""               "Relevantes" "No Relevantes"
[2,] "Recuperados"    "21"         "567"          
[3,] "No Recuperados" "0"          "12"           
[1] "Fscore"
[1] 0.06896552
[1] "Precision"
[1] 0.03571429
[1] "Recall"
[1] 1
[1] "-----------------------------------------"

>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
   <file_sep><<<<<<< HEAD
<<<<<<< HEAD
setwd(choose.dir())

install.packages('cluster', dep = TRUE)
library(cluster)

install.packages('factoextra', dep = TRUE)
library(factoextra)

install.packages('FactoMineR', dep = TRUE)
library(FactoMineR)

install.packages('ggplot2', dep = TRUE)
library(ggplot2)

data_norm  =  read.csv("zoo-cluster - class_type.csv")
data  =  read.csv("zoo-cluster - class_type.csv")

data_norm["animal_name"] <- NULL

#--------------------------------------------------------------
#modelo con 7 clusters
#Distancia Hamming
data_cluster <- dist(data_norm, method = "binary", diag = FALSE, upper = FALSE, p = 2)

data_matrix <- as.matrix(data_cluster)

data_kmeans <- pam(data_matrix, 7)

data$class_type[data_kmeans$id.med] # centroides con distancia hamming
#--------------------------------------------------------------
#Distancia manhattan o euclidea
# datos dicotomicos a variables cuantitativas por medio de "multiple correspondence analysis"
for (i in 1:ncol(data_norm)) data_norm[,i]=as.factor(data_norm[,i])

mca1 = MCA(data_norm, graph = FALSE)

data_matrix <- as.matrix(mca1)
#hacer tabla que explique a que especie corresponde cada cluster (para las dos distancias ("hamming" y "manhattan"))
data_kmeans <- pam(mca1$ind$cos2, 7, metric = "manhattan") 
data$class_type[data_kmeans$id.med]
#--------------------------------------------------------------
#Desaprobar 7 clusters para clasificar dataset
fviz_silhouette(silhouette(data_kmeans))
sil <- silhouette(data_kmeans)
# se seleccionan los animales con altas probabilidades de  pertenecer a otro cluster
neg_sil_index <- which(sil[, 'sil_width'] < 0.2)
sil[neg_sil_index, , drop = FALSE]
#--------------------------------------------------------------
#modelo con 6 clusters
data_cluster <- dist(data_norm, method = "binary", diag = FALSE, upper = FALSE, p = 2)

data_matrix <- as.matrix(data_cluster)

data_kmeans <- pam(data_matrix, 6)#se ha elegido 6 clusters
data$class_type[data_kmeans$id.med] # centroides con distancia hamming
data[data_kmeans$id.med,23:29]#informacion centroides

#Grafica de siluetas
fviz_silhouette(silhouette(data_kmeans)) 

#Agrupaciones sin nombres
fviz_cluster(data_kmeans, stand = FALSE, geom = "point", ellipse.type = "norm", show.clust.cent = TRUE)
# Agrupaciones de animales en particular
fviz_cluster(data_kmeans, stand = FALSE, data = data_norm, ellipse.type = "norm", show.clust.cent = TRUE)
#Graficas de agrupacion y silueta 
plot(data$class_type, col = data_kmeans$cluster)
plot(data[data_kmeans$id.med,23:29], col = data_kmeans$cluster)
#Informacion global de clusters
summary(data_kmeans)
plot(data_kmeans)
names(data_kmeans)
table(data_kmeans$clustering)

#BONUS-Algoritmo jerarquico
data  =  read.csv("zoo-cluster.csv")
data_norm["animal_name"] <- NULL
data_cluster <- dist(data_norm, method = "manhattan", diag = FALSE, upper = FALSE, p = 2)
 
clusters <- hclust(data_cluster)
plot(clusters)

clusterCut <- cutree(clusters, 6) # a distancia 0.78 en el dendrograma
table(clusterCut, data$class_type)
plot(clusterCut, data$class_type)
=======
setwd(choose.dir())

install.packages('cluster', dep = TRUE)
library(cluster)

install.packages('factoextra', dep = TRUE)
library(factoextra)

install.packages('FactoMineR', dep = TRUE)
library(FactoMineR)

install.packages('ggplot2', dep = TRUE)
library(ggplot2)

data_norm  =  read.csv("zoo-cluster - class_type.csv")
data  =  read.csv("zoo-cluster - class_type.csv")

data_norm["animal_name"] <- NULL

#--------------------------------------------------------------
#modelo con 7 clusters
#Distancia Hamming
data_cluster <- dist(data_norm, method = "binary", diag = FALSE, upper = FALSE, p = 2)

data_matrix <- as.matrix(data_cluster)

data_kmeans <- pam(data_matrix, 7)

data$class_type[data_kmeans$id.med] # centroides con distancia hamming
#--------------------------------------------------------------
#Distancia manhattan o euclidea
# datos dicotomicos a variables cuantitativas por medio de "multiple correspondence analysis"
for (i in 1:ncol(data_norm)) data_norm[,i]=as.factor(data_norm[,i])

mca1 = MCA(data_norm, graph = FALSE)

data_matrix <- as.matrix(mca1)
#hacer tabla que explique a que especie corresponde cada cluster (para las dos distancias ("hamming" y "manhattan"))
data_kmeans <- pam(mca1$ind$cos2, 7, metric = "manhattan") 
data$class_type[data_kmeans$id.med]
#--------------------------------------------------------------
#Desaprobar 7 clusters para clasificar dataset
fviz_silhouette(silhouette(data_kmeans))
sil <- silhouette(data_kmeans)
# se seleccionan los animales con altas probabilidades de  pertenecer a otro cluster
neg_sil_index <- which(sil[, 'sil_width'] < 0.2)
sil[neg_sil_index, , drop = FALSE]
#--------------------------------------------------------------
#modelo con 6 clusters
data_cluster <- dist(data_norm, method = "binary", diag = FALSE, upper = FALSE, p = 2)

data_matrix <- as.matrix(data_cluster)

data_kmeans <- pam(data_matrix, 6)#se ha elegido 6 clusters
data$class_type[data_kmeans$id.med] # centroides con distancia hamming
data[data_kmeans$id.med,23:29]#informacion centroides

#Grafica de siluetas
fviz_silhouette(silhouette(data_kmeans)) 

#Agrupaciones sin nombres
fviz_cluster(data_kmeans, stand = FALSE, geom = "point", ellipse.type = "norm", show.clust.cent = TRUE)
# Agrupaciones de animales en particular
fviz_cluster(data_kmeans, stand = FALSE, data = data_norm, ellipse.type = "norm", show.clust.cent = TRUE)
#Graficas de agrupacion y silueta 
plot(data$class_type, col = data_kmeans$cluster)
plot(data[data_kmeans$id.med,23:29], col = data_kmeans$cluster)
#Informacion global de clusters
summary(data_kmeans)
plot(data_kmeans)
names(data_kmeans)
table(data_kmeans$clustering)

#BONUS-Algoritmo jerarquico
data  =  read.csv("zoo-cluster.csv")
data_norm["animal_name"] <- NULL
data_cluster <- dist(data_norm, method = "manhattan", diag = FALSE, upper = FALSE, p = 2)
 
clusters <- hclust(data_cluster)
plot(clusters)

clusterCut <- cutree(clusters, 6) # a distancia 0.78 en el dendrograma
table(clusterCut, data$class_type)
plot(clusterCut, data$class_type)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
setwd(choose.dir())

install.packages('cluster', dep = TRUE)
library(cluster)

install.packages('factoextra', dep = TRUE)
library(factoextra)

install.packages('FactoMineR', dep = TRUE)
library(FactoMineR)

install.packages('ggplot2', dep = TRUE)
library(ggplot2)

data_norm  =  read.csv("zoo-cluster - class_type.csv")
data  =  read.csv("zoo-cluster - class_type.csv")

data_norm["animal_name"] <- NULL

#--------------------------------------------------------------
#modelo con 7 clusters
#Distancia Hamming
data_cluster <- dist(data_norm, method = "binary", diag = FALSE, upper = FALSE, p = 2)

data_matrix <- as.matrix(data_cluster)

data_kmeans <- pam(data_matrix, 7)

data$class_type[data_kmeans$id.med] # centroides con distancia hamming
#--------------------------------------------------------------
#Distancia manhattan o euclidea
# datos dicotomicos a variables cuantitativas por medio de "multiple correspondence analysis"
for (i in 1:ncol(data_norm)) data_norm[,i]=as.factor(data_norm[,i])

mca1 = MCA(data_norm, graph = FALSE)

data_matrix <- as.matrix(mca1)
#hacer tabla que explique a que especie corresponde cada cluster (para las dos distancias ("hamming" y "manhattan"))
data_kmeans <- pam(mca1$ind$cos2, 7, metric = "manhattan") 
data$class_type[data_kmeans$id.med]
#--------------------------------------------------------------
#Desaprobar 7 clusters para clasificar dataset
fviz_silhouette(silhouette(data_kmeans))
sil <- silhouette(data_kmeans)
# se seleccionan los animales con altas probabilidades de  pertenecer a otro cluster
neg_sil_index <- which(sil[, 'sil_width'] < 0.2)
sil[neg_sil_index, , drop = FALSE]
#--------------------------------------------------------------
#modelo con 6 clusters
data_cluster <- dist(data_norm, method = "binary", diag = FALSE, upper = FALSE, p = 2)

data_matrix <- as.matrix(data_cluster)

data_kmeans <- pam(data_matrix, 6)#se ha elegido 6 clusters
data$class_type[data_kmeans$id.med] # centroides con distancia hamming
data[data_kmeans$id.med,23:29]#informacion centroides

#Grafica de siluetas
fviz_silhouette(silhouette(data_kmeans)) 

#Agrupaciones sin nombres
fviz_cluster(data_kmeans, stand = FALSE, geom = "point", ellipse.type = "norm", show.clust.cent = TRUE)
# Agrupaciones de animales en particular
fviz_cluster(data_kmeans, stand = FALSE, data = data_norm, ellipse.type = "norm", show.clust.cent = TRUE)
#Graficas de agrupacion y silueta 
plot(data$class_type, col = data_kmeans$cluster)
plot(data[data_kmeans$id.med,23:29], col = data_kmeans$cluster)
#Informacion global de clusters
summary(data_kmeans)
plot(data_kmeans)
names(data_kmeans)
table(data_kmeans$clustering)

#BONUS-Algoritmo jerarquico
data  =  read.csv("zoo-cluster.csv")
data_norm["animal_name"] <- NULL
data_cluster <- dist(data_norm, method = "manhattan", diag = FALSE, upper = FALSE, p = 2)
 
clusters <- hclust(data_cluster)
plot(clusters)

clusterCut <- cutree(clusters, 6) # a distancia 0.78 en el dendrograma
table(clusterCut, data$class_type)
plot(clusterCut, data$class_type)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep>

# Change accordingly
VERSION <- "v15.9.10"
WORK.DIR <- file.path("", "research", "Memoristas", VERSION)


CARSIG.SCRIPT.BASENAME <- paste("carSignal", VERSION, sep = "-")
CARSIG.SCRIPT.BASENAME <- paste(CARSIG.SCRIPT.BASENAME, "R", sep = ".")
CARSIG.SCRIPT.NAME <- file.path(WORK.DIR, CARSIG.SCRIPT.BASENAME)
source(CARSIG.SCRIPT.NAME)

DARI.SCRIPT.BASENAME <- paste("atARI", VERSION, sep = "-")
DARI.SCRIPT.BASENAME <- paste(DARI.SCRIPT.BASENAME, "R", sep = ".")
DARI.SCRIPT.NAME <- file.path(WORK.DIR, DARI.SCRIPT.BASENAME)
source(DARI.SCRIPT.NAME)

METRIC.UTILS.SCRIPT.BASENAME <-
  paste("Metric", "utils", VERSION, sep = "-")
METRIC.UTILS.SCRIPT.BASENAME <-
  paste(METRIC.UTILS.SCRIPT.BASENAME, "R", sep = ".")
METRIC.UTILS.SCRIPT.NAME <-
  file.path(WORK.DIR, METRIC.UTILS.SCRIPT.BASENAME)
source(METRIC.UTILS.SCRIPT.NAME)

MFARI.SCRIPT.BASENAME <- paste("mfARI", VERSION, sep = "-")
MFARI.SCRIPT.BASENAME <- paste(MFARI.SCRIPT.BASENAME, "R", sep = ".")
MFARI.SCRIPT.NAME <- file.path(WORK.DIR, MFARI.SCRIPT.BASENAME)
source(MFARI.SCRIPT.NAME)

ROUND.UTILS.SCRIPT.BASENAME <-
  paste("Rounding", "utils", VERSION, sep = "-")
ROUND.UTILS.SCRIPT.BASENAME <-
  paste(ROUND.UTILS.SCRIPT.BASENAME, "R", sep = ".")
ROUND.UTILS.SCRIPT.NAME <- 
  file.path(WORK.DIR, ROUND.UTILS.SCRIPT.BASENAME)
source(ROUND.UTILS.SCRIPT.NAME)



get.normalised.dari.templates <- function(
      time.instants,
      normalised.abp.signal,
      sampling.time,
      sample.release,
      baseline.initial.time = time.instants[1],
      baseline.final.time = time.instants[sample.release],
      min.cbfv.max.delta.time = 8 * 0.8,
      stabilisation.time = 30,
      at.param.rounding.digits = 6,
      cbfv.rounding.digits = 2,
      time.tol = sampling.time / 100,
      ... # Not used
      )
{
  at.params <- get.AT.decimal.templates.parameters(
    rounding.digits = at.param.rounding.digits
  )
  
  .tmp.fun <- function(i)
    get.theoretical.CBFV.response(
      T = at.params[i, 1],
      D = at.params[i, 2],
      K = at.params[i, 3],
      time.instants = time.instants,
      ABP.normalised = normalised.abp.signal,
      sampling.time = sampling.time,
      stabilisation.time = stabilisation.time
    )
  r <- 1:nrow(at.params)
  templates <- lapply(r, .tmp.fun)
  
  .tmp.fun <- function(t)
    normalise.CBFV.signal(
      time.instants = time.instants,
      CBFV.signal = t[["CBFV.theoretical.response"]],
      sample.release = sample.release,
      sampling.time = sampling.time,
      baseline.initial.time = baseline.initial.time,
      baseline.final.time = baseline.final.time,
      min.CBFV.max.delta.time = min.cbfv.max.delta.time,
      time.tol = time.tol
    )
  normalised.templates <- lapply(templates, .tmp.fun)
  
  .tmp.fun <- function(t) t[["normalised.CBFV.signal"]]
  normalised.templates <- lapply(normalised.templates, .tmp.fun)
  
  names(normalised.templates) <- 
    sapply(r, function(i) sprintf("%.1f", at.params[i, 4]))
  
  normalised.templates
}


get.plot.dARI <- function(
      time.instants,
      normalised.abp.signal,
      normalised.cbfv.signal,
      template.search.results,
      abp.palette = brewer.pal(n = 9, name = "Reds"),
      cbfv.palette = brewer.pal(n = 9, name = "Blues"),
      template.palette = brewer.pal(n = 9, name = "Greens"),
      time.tol = min(diff(time.instants)) / 100,
      plot.id = NULL,
      fiting.value.name = "Fitting",
      fiting.value.rounding.digits = 3,
      ... # Not used
      )
{
  # Rebuilds the original signals
  abp.baseline <- attr(normalised.abp.signal, "baseline.value")
  abp.min <- attr(normalised.abp.signal, "min.value")
  abp <- normalised.abp.signal * (abp.baseline - abp.min) + abp.min
  
  cbfv.baseline <- attr(normalised.cbfv.signal, "baseline.value")
  cbfv.min <- attr(normalised.cbfv.signal, "min.value")
  .tmp.fun <- function(x) x * (cbfv.baseline - cbfv.min) + cbfv.min
  cbfv <- .tmp.fun(normalised.cbfv.signal)
  
  # Plots CBFV
  dcbfv <- 
    data.frame(Time = time.instants, Signal = cbfv, Segment = "CBFV")
  p <- ggplot(dcbfv, aes(x = Time, y = Signal, colour = Segment))
  p <- p + geom_line(linetype = "dashed")
  d <- dcbfv[template.search.results[["relevant.samples"]], ]
  p <- p + geom_line(data = d)
  
  # Plots ABP
  dabp <- data.frame(Time = time.instants, Signal = abp, Segment = "ABP")
  p <- p + geom_line(data = dabp, linetype = "dashed")
  
  # Gets relevant time instants
  relevant.time.instants <- 
    time.instants[template.search.results[["relevant.samples"]]]

  # Plots representative templates
  i <- seq(11, 91, 20)
  temps <- template.search.results[["relevant.template.segments"]][i]
  values <- c(sapply(temps, .tmp.fun))
  lengths <- c(sapply(temps, function(t) length(t)))
  aris <- paste("ARI", "=", names(temps))
  aris <- rep(aris, times = lengths)
  times <- rep(relevant.time.instants, times = length(i))
  
  d <- data.frame(Time = times, Signal = values, Segment = aris)
  p <- p + geom_line(data = d, linetype = "dashed")
  
  # Plots best template
  ibest <- template.search.results[["ranking"]][1]
  best <- template.search.results[["relevant.template.segments"]][[ibest]]
  d <- data.frame(
    Time = relevant.time.instants,
    Signal = .tmp.fun(best),
    Segment = "Best template"
  )
  p <- p + geom_line(data = d)
  
  p <- p + xlab("Time") + ylab("Signals")
  cmatch <- c("ABP" = abp.palette[4], "CBFV" = cbfv.palette[4])
  cmatch <- c(cmatch, "Best template" = template.palette[9])
  subpal <- template.palette[3:7]
  aris <- paste("ARI", "=", names(temps))
  names(subpal) <- aris
  cmatch <- c(cmatch, subpal)
  p <- p + scale_colour_manual(values = cmatch, breaks = names(cmatch))
  p <- p + theme(
    legend.justification = c(0, 0),
    legend.background = element_rect(fill = alpha('grey90', 0.0)),
    legend.position = c(0, 0),
    legend.key.size =  unit(0.4, "cm"),
    legend.title = element_blank(),
    legend.text = element_text(size = 8)
  )
  
  # Adds annotation table
  l1 <- "Time of release"
  v1 <- sprintf(
    "t = %.1f",
    template.search.results[["referential.time.instant"]]
  )
  l2 <- "Compared window"
  v2 <- sprintf(
    "t in [%.1f, %.1f]",
    template.search.results[["initial.time.instant"]],
    template.search.results[["final.time.instant"]]
  )
  l3 <- paste("Best", fiting.value.name)
  v3 <- sprintf(
    "%.*f",
    fiting.value.rounding.digits,
    template.search.results[["fit.values"]][ibest]
  )
  ann.table <- data.frame(label = c(l1, l2, l3), values = c(v1, v2, v3))
  gt <- tableGrob(
    d = ann.table,
    show.rownames = FALSE,
    show.colnames = FALSE,
    gpar.coretext = gpar(col = cbfv.palette[7], cex = 0.6),
    padding.v = unit(2, "mm")
  )
  
  xmin <- time.instants[1]
  xmax <- tail(time.instants, 1)
  rx <- xmax - xmin
  dx <- rx / 100
  ymin <- min(abp, cbfv)
  ymax <- max(abp, cbfv)
  ry <- ymax - ymin
  dy <- ry / 100

  p <- p + annotation_custom(
    grob = gt,
    xmin = xmax - 25 * dx,
    xmax = xmax,
    ymin = ymin,
    ymax = ymin + 10 * dy
  )
  
  # Sets the plot title
  best.ari <- paste(
    "dARI",
    names(template.search.results[["relevant.template.segments"]])[ibest],
    sep = " = "
  )

  if(is.null(plot.id))
    p <- p + ggtitle(best.ari)
  else
    p <- p + ggtitle(paste(plot.id, best.ari, sep = ", "))
  
  p
}


plot.manoeuvre.signals <- function(
      time.instants,
      normalised.abp.signal,
      normalised.cbfv.signal,
      normalised.cbfv.templates,
      tgt.plot.filename,
      tgt.plot.id,
      ...
    )
{
  dari.search <- get.best.templates(
    time.instants = time.instants,
    signal = normalised.cbfv.signal,
    templates = normalised.cbfv.templates,
    keep.details = TRUE,
    ...
  )
    
  p <- get.plot.dARI(
    time.instants = time.instants,
    normalised.abp.signal = normalised.abp.signal,
    normalised.cbfv.signal = normalised.cbfv.signal,
    template.search.results = dari.search,
    time.tol = time.tol,
    plot.id = tgt.plot.id,
    ...
  )
  
  ggsave(
    filename = tgt.plot.filename,
    plot = p,
    width = 11, 
    height = 8,
    units = "in"
  )
}


get.manoeuvre.normalised.abp <- function(
      time.instants,
      abp.signal,
      sampling.time,
      sample.release,
      baseline.initial.time = time.instants[1],
      baseline.final.time = time.instants[sample.release],
      min.abp.max.delta.time = 5 * 0.8,
      time.tol = sampling.time / 100,
      ... # Not used
      )
{
  normalised.signal <- normalise.ABP.signal(
    time.instants = time.instants,
    ABP.signal = abp.signal,
    sample.release = sample.release,
    sampling.time = sampling.time,
    baseline.initial.time = baseline.initial.time,
    baseline.final.time = baseline.final.time,
    min.ABP.max.delta.time = min.abp.max.delta.time,
    time.tol = time.tol
  )
  
  normalised.abp.signal <- normalised.signal[["normalised.ABP.signal"]]
  attr(normalised.abp.signal, "baseline.value") <-
    normalised.signal[["ABP.baseline.value"]]
  attr(normalised.abp.signal, "min.value") <-
    normalised.signal[["min.ABP.value"]]
  
  normalised.abp.signal
}


get.manoeuvre.normalised.cbfv <- function(
      time.instants,
      cbfv.signal,
      sampling.time,
      sample.release,
      baseline.initial.time = time.instants[1],
      baseline.final.time = time.instants[sample.release],
      min.cbfv.max.delta.time = 8 * 0.8,
      time.tol = sampling.time / 100,
      ... # Not used
      )
{
  normalised.signal <- normalise.CBFV.signal(
    time.instants = time.instants,
    CBFV.signal = cbfv.signal,
    sample.release = sample.release,
    sampling.time = sampling.time,
    baseline.initial.time = baseline.initial.time,
    baseline.final.time = baseline.final.time,
    min.CBFV.max.delta.time = min.cbfv.max.delta.time,
    time.tol = time.tol
  )
  
  normalised.cbfv.signal <- normalised.signal[["normalised.CBFV.signal"]]
  attr(normalised.cbfv.signal, "baseline.value") <-
    normalised.signal[["CBFV.baseline.value"]]
  attr(normalised.cbfv.signal, "min.value") <-
    normalised.signal[["min.CBFV.value"]]
  
  normalised.cbfv.signal
}


plot.subject.signals <- function(
      subject,
      src.subject.dir,
      src.ext = "txt",
      header = TRUE,
      tgt.subject.dir,
      tgt.suffix,
      tgt.ext = "txt",
      overwrite = FALSE,
      manoeuvres,
      time.col.name = "Time",
      abp.col.name = "ABP",
      left.cbfv.col.name = "LCBFV",
      right.cbfv.col.name = "RCBFV",
      sampling.time = 0.2,
      time.release = 0,
      left.plot.suffix = "Izq",
      right.plot.suffix = "Der",
      indent = "",
      time.tol = sampling.time / 100,
      ...
      )
{
  plot.created <- FALSE
  next.indent <- paste0(indent , "  ")
  
  for(mvre in manoeuvres)
  {
    cat(indent, "-- ", mvre, " --\n", sep = "")
    
    # Sets the subject source file
    src.basename <- paste(subject, mvre, sep = "-")
    src.basename <- paste(src.basename, src.ext, sep = ".")
    src.filename <- file.path(src.subject.dir, src.basename)
    
    if(!file.exists(src.filename))
    {
      cat(
        next.indent,
        "Warning: file ",
        src.filename,
        " does not exists\n",
        sep = ""
      )
      next
    }
    
    # Reads the manoeuvre signals
    mvre.data <- read.table(src.filename, header = header)
    
    # Sets subject and manoeuvre as initial plot titles
    mvre.id <- paste(subject, mvre, sep = ", ")
    
    # Gets sample of release
    sample.release <- which(are.tolerably.equal(
      mvre.data[[time.col.name]],
      time.release,
      time.tol
    ))
    
    # Gets normalised ABP
    normalised.abp.signal <- get.manoeuvre.normalised.abp(
      time.instants = mvre.data[[time.col.name]],
      abp.signal = mvre.data[[abp.col.name]],
      sampling.time = sampling.time,
      sample.release = sample.release,
      time.tol = time.tol,
      ...
    )
    
    # Gets dARI templates
    normalised.dari.templates <- get.normalised.dari.templates(
      time.instants = mvre.data[[time.col.name]],
      normalised.abp.signal = normalised.abp.signal,
      sampling.time = sampling.time,
      sample.release = sample.release,
      time.tol = time.tol,
      ...
    )
    
    #
    # Creates the plot for the left hemisphere
    #
    
    cat(next.indent, "-- Left hemisphere --\n", sep = "")
    
    # Sets target plot filename
    tgt.basename <-
      paste(subject, mvre, tgt.suffix, left.plot.suffix, sep = "-")
    tgt.basename <- paste(tgt.basename, tgt.ext, sep = ".")
    tgt.filename <- file.path(tgt.subject.dir, tgt.basename)
    
    # If the target plot does not exists or it should not be overwritten
    if(any(!file.exists(tgt.filename), overwrite))
    {
      # Makes sure the target directory exists
      dir.create(
        path = tgt.subject.dir,
        showWarnings = FALSE,
        recursive = TRUE,
        mode = "0711"
      )
    
      normalised.cbfv.signal <- get.manoeuvre.normalised.cbfv(
        time.instants = mvre.data[[time.col.name]],
        cbfv.signal = mvre.data[[left.cbfv.col.name]],
        sampling.time = sampling.time,
        sample.release = sample.release,
        time.tol = time.tol,
        ...
      )
      
      plot.manoeuvre.signals(
        time.instants = mvre.data[[time.col.name]],
        normalised.abp.signal = normalised.abp.signal,
        normalised.cbfv.signal = normalised.cbfv.signal,
        normalised.cbfv.templates = normalised.dari.templates,
        tgt.plot.filename = tgt.filename,
        tgt.plot.id = paste(mvre.id, left.plot.suffix, sep = ", "),
        ...
      )
      
      plot.created <- TRUE
    }
    else
      cat(
        paste(next.indent, "  "),
        "Warning: target plot already exist and not overwritten\n",
        sep = ""
      )
    
    
    #
    # Creates the plot for the right hemisphere
    #
    
    cat(next.indent, "-- Right hemisphere --\n", sep = "")
    
    # Sets target plot filename
    tgt.basename <-
      paste(subject, mvre, tgt.suffix, right.plot.suffix, sep = "-")
    tgt.basename <- paste(tgt.basename, tgt.ext, sep = ".")
    tgt.filename <- file.path(tgt.subject.dir, tgt.basename)
    
    # If the target plot does not exists or it should not be overwritten
    if(any(!file.exists(tgt.filename), overwrite))
    {
      # Makes sure the target directory exists
      dir.create(
        path = tgt.subject.dir,
        showWarnings = FALSE,
        recursive = TRUE,
        mode = "0711"
      )
      
      normalised.cbfv.signal <- get.manoeuvre.normalised.cbfv(
        time.instants = mvre.data[[time.col.name]],
        cbfv.signal = mvre.data[[right.cbfv.col.name]],
        sampling.time = sampling.time,
        sample.release = sample.release,
        time.tol = time.tol,
        ...
      )
      
      plot.manoeuvre.signals(
        time.instants = mvre.data[[time.col.name]],
        normalised.abp.signal = normalised.abp.signal,
        normalised.cbfv.signal = normalised.cbfv.signal,
        normalised.cbfv.templates = normalised.dari.templates,
        tgt.plot.filename = tgt.filename,
        tgt.plot.id = paste(mvre.id, right.plot.suffix, sep = ", "),
        ...
      )
      
      plot.created <- TRUE
    }
    else
      cat(
        paste(next.indent, "  "),
        "Warning: target plot already exist and not overwritten\n",
        sep = ""
      )
  }
  
  plot.created
}


run <- function(
      src.dir = file.path(WORK.DIR, "Data"),
      src.ext = "txt",
      header = TRUE,
      tgt.suffix = paste("dARI", "search", "plots", sep = "-"),
      tgt.dir = file.path(WORK.DIR, tgt.suffix),
      tgt.ext = "pdf",
      overwrite = FALSE,
      subjects = c("Sujeto1", "Sujeto2", "Sujeto3"),
      manoeuvres = c("maniobra1", "maniobra2", "maniobra3"),
      time.col.name = "Time",
      abp.col.name = "ABP",
      left.cbfv.col.name = "LCBFV",
      right.cbfv.col.name = "RCBFV",
      left.plot.suffix = "Izq",
      right.plot.suffix = "Der",
      sampling.time = 0.2,
      time.release = 0,
      baseline.initial.time = -5,
      baseline.final.time = time.release,
      min.abp.max.delta.time = 5 * 0.8,
      min.cbfv.max.delta.time = 8 * 0.8,
      stabilisation.time = 30,
      referential.time.instant = time.release,
      delta.time.before.ref = 0,
      delta.time.after.ref = round(floor(20 * 0.8 / sampling.time) * sampling.time, 1),
      comparison.function = get.MSE,
      fiting.value.name = "MSE",
      fiting.value.rounding.digits = 4,
      at.param.rounding.digits = 6,
      time.tol = sampling.time / 100,
      indent = ""
      )
{
  plot.created <- FALSE
  
  for(subject in subjects)
  {
    cat(indent, "-- ", subject, " --\n", sep = "")
    
    # Sets subject directories
    src.subject.dir <- file.path(src.dir, subject)
    tgt.subject.dir <- file.path(tgt.dir, subject)
    
    subject.plot.created <- plot.subject.signals(
      subject = subject,
      src.subject.dir = src.subject.dir,
      src.ext = src.ext,
      header = header,
      tgt.subject.dir = tgt.subject.dir,
      tgt.suffix = tgt.suffix,
      tgt.ext = tgt.ext,
      overwrite = overwrite,
      manoeuvres = manoeuvres,
      time.col.name = time.col.name,
      abp.col.name = abp.col.name,
      left.cbfv.col.name = left.cbfv.col.name,
      right.cbfv.col.name = right.cbfv.col.name,
      left.plot.suffix = left.plot.suffix,
      right.plot.suffix = right.plot.suffix,
      sampling.time = sampling.time,
      time.release = time.release,
      baseline.initial.time = baseline.initial.time,
      baseline.final.time = baseline.final.time,
      min.abp.max.delta.time = min.abp.max.delta.time,
      min.cbfv.max.delta.time = min.cbfv.max.delta.time,
      stabilisation.time = stabilisation.time,
      referential.time.instant = referential.time.instant,
      delta.time.before.ref = delta.time.before.ref,
      delta.time.after.ref = delta.time.after.ref,
      comparison.function = comparison.function,
      fiting.value.name = fiting.value.name,
      fiting.value.rounding.digits = fiting.value.rounding.digits,
      at.param.rounding.digits = at.param.rounding.digits,
      time.tol = time.tol,
      indent = paste0(indent, "  ")
    )
    plot.created <- any(plot.created, subject.plot.created)
  }
  
  plot.created
}
<file_sep>setwd("C:/Users/Luis/Documents/DataScience/Tesis/Calculo ARI/v15.9.10/stats")

data <- read.csv('maniobras-stats-v15.9.10.csv', sep=';')


left <- data[data$Hemisferio=="Left",]$mfARI
right <- data[data$Hemisferio=="Right",]$mfARI

#Kolmogorov-Smirnov ya que son 54 datos pero tiene valores repetidos
#ks.test(left, "pnorm")#no normal
#ks.test(right, "pnorm")#no normal
#no normal
#wilcox.test(left, right, alternative = "two.sided")#Los hemisferios son similares

shapiro.test(left)#normal
shapiro.test(right)#normal

hist(left)
hist(right)

#normal
t.test(left, right, alternative = "two.sided") #Los hemisferios son similares por quep-value > 0.05

##############################################
#mean mfARI
mfARI <- rowMeans(data.frame(left, right))
dat = data.frame("Maniobra" = data[data$Hemisferio=="Left",]$Maniobra, "mfARI" = mfARI)

fm = aov( dat$mfARI ~ dat$Maniobra, data = dat )

summary(fm)

tuk <- TukeyHSD(fm)

plot (tuk)

boxplot(dat$mfARI ~ dat$Maniobra, 
        main="Comparacion de Posiciones mfARI", 
        col= rainbow(3), 
        horizontal = TRUE)

#Conclusion
##La mayor diferencia de posiciones van en este orde:
### PIE-ACOSTADO = 0.7775675 
### SENTADO-ACOSTADO = 0.8830272
### SENTADO-PIE = 0.9778065

###############################mejor esto que el calculo anterior del post hoc -> es mejor pairwise
tyres.aov<- aov(mfARI~Maniobra, dat)
class(tyres.aov)

summary(tyres.aov)

pairwise.t.test(dat$mfARI,dat$Maniobra,p.adjust.method = "none")

##
model = lm(mfARI ~ Maniobra, 
           data=dat)

Anova(model, type="III")

summary(model)

boxplot(mfARI ~ Maniobra,
        data = dat,
        ylab="aam / height",
        xlab="Location")

<file_sep><<<<<<< HEAD
<<<<<<< HEAD
import pandas
import numpy
import seaborn
import scipy
import matplotlib.pyplot as plt
import seaborn as sns
data = pandas.read_csv(‘marscrater_pds.csv’, low_memory=False)
data['DIAM_CIRCLE_IMAGE’] = data['DIAM_CIRCLE_IMAGE’].convert_objects(convert_numeric=True)
data['DEPTH_RIMFLOOR_TOPOG’] = data['DEPTH_RIMFLOOR_TOPOG’].convert_objects(convert_numeric=True)
data['DIAM_CIRCLE_IMAGE’]=data['DIAM_CIRCLE_IMAGE’].replace(’ ’, numpy.nan)
data['DEPTH_RIMFLOOR_TOPOG’]=data['DEPTH_RIMFLOOR_TOPOG’].replace(’ ’, numpy.nan)
data_clean=data.dropna()
print ('association between urbanrate and internetuserate’)
print (scipy.stats.pearsonr(data_clean['DIAM_CIRCLE_IMAGE’], data_clean['DEPTH_RIMFLOOR_TOPOG’]))
scat1 = seaborn.regplot(x=“DIAM_CIRCLE_IMAGE”, y=“DEPTH_RIMFLOOR_TOPOG”, fit_reg=True, data=data_clean)
plt.xlabel('Crater Diameter (km)’)
plt.ylabel('Crater Elevation (km)’)
plt.title('Scatterplot for the Association Between Diameter and Elevation’)
corr = data_clean.corr()
sns.heatmap(corr, 
       xticklabels=corr.columns,
       yticklabels=corr.columns)
=======
import pandas
import numpy
import seaborn
import scipy
import matplotlib.pyplot as plt
import seaborn as sns
data = pandas.read_csv(‘marscrater_pds.csv’, low_memory=False)
data['DIAM_CIRCLE_IMAGE’] = data['DIAM_CIRCLE_IMAGE’].convert_objects(convert_numeric=True)
data['DEPTH_RIMFLOOR_TOPOG’] = data['DEPTH_RIMFLOOR_TOPOG’].convert_objects(convert_numeric=True)
data['DIAM_CIRCLE_IMAGE’]=data['DIAM_CIRCLE_IMAGE’].replace(’ ’, numpy.nan)
data['DEPTH_RIMFLOOR_TOPOG’]=data['DEPTH_RIMFLOOR_TOPOG’].replace(’ ’, numpy.nan)
data_clean=data.dropna()
print ('association between urbanrate and internetuserate’)
print (scipy.stats.pearsonr(data_clean['DIAM_CIRCLE_IMAGE’], data_clean['DEPTH_RIMFLOOR_TOPOG’]))
scat1 = seaborn.regplot(x=“DIAM_CIRCLE_IMAGE”, y=“DEPTH_RIMFLOOR_TOPOG”, fit_reg=True, data=data_clean)
plt.xlabel('Crater Diameter (km)’)
plt.ylabel('Crater Elevation (km)’)
plt.title('Scatterplot for the Association Between Diameter and Elevation’)
corr = data_clean.corr()
sns.heatmap(corr, 
       xticklabels=corr.columns,
       yticklabels=corr.columns)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
import pandas
import numpy
import seaborn
import scipy
import matplotlib.pyplot as plt
import seaborn as sns
data = pandas.read_csv(‘marscrater_pds.csv’, low_memory=False)
data['DIAM_CIRCLE_IMAGE’] = data['DIAM_CIRCLE_IMAGE’].convert_objects(convert_numeric=True)
data['DEPTH_RIMFLOOR_TOPOG’] = data['DEPTH_RIMFLOOR_TOPOG’].convert_objects(convert_numeric=True)
data['DIAM_CIRCLE_IMAGE’]=data['DIAM_CIRCLE_IMAGE’].replace(’ ’, numpy.nan)
data['DEPTH_RIMFLOOR_TOPOG’]=data['DEPTH_RIMFLOOR_TOPOG’].replace(’ ’, numpy.nan)
data_clean=data.dropna()
print ('association between urbanrate and internetuserate’)
print (scipy.stats.pearsonr(data_clean['DIAM_CIRCLE_IMAGE’], data_clean['DEPTH_RIMFLOOR_TOPOG’]))
scat1 = seaborn.regplot(x=“DIAM_CIRCLE_IMAGE”, y=“DEPTH_RIMFLOOR_TOPOG”, fit_reg=True, data=data_clean)
plt.xlabel('Crater Diameter (km)’)
plt.ylabel('Crater Elevation (km)’)
plt.title('Scatterplot for the Association Between Diameter and Elevation’)
corr = data_clean.corr()
sns.heatmap(corr, 
       xticklabels=corr.columns,
       yticklabels=corr.columns)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep>
require(ggplot2)
require(gridExtra)
require(signal)
require(pracma)



# This function creates an normalised ABP step stimulus.
# The function receives the following arguments:
# 
# sampling.time: sampling time for the ABP stimulus (in seconds).
#                Default value: 0.1 s
# 
# time.until.release: time included in the resulting signal until the
#                     time of cuff release (in seconds).
#                     Default value: 10 s
# 
# time.after.release: time included in the resulting signals after the
#                     time of cuff release.
#                     Default value: 20 s
#
# smooth.step.stimulus: logical, whether the ABP step stimulus should
#                       be smoothed (filtered).
#                       Default value: FALSE
# 
# filter.order: the order of the low-pass Butterworth filter used to
#               smooth the ABP step stimulus.
#               Default value: 2
# 
# cutoff.frequency: the cutoff frequency for the low-pass Butterworth
#                   filter used to smooth the ABP step stimulus (in Hz).
#                   Default value: 0.20 Hz
# 
# left.stabilisation.time: the time to be used for stabilisation of the
#                          step. This time is included in the step when
#                          a filter is applied, but removed after that.
#                          Default value: 30 s when a filter will be
#                          applied, 0 s otherwise.
#
# time.rounding.digits: the number of decimal when time instants are
#                       used.
#                       Default value: the number of decimals in the
#                       sampling time
#
# The function's answer is a list with the following:
# .$time.instants: the time instants in which the signal is sampled.
# .$ABP.normalised: the normalised ABP stimulus generated
# .$sampling.time: the sampling time used
# .$time.release: the time instant in which the thigh cuffs are
#                 supposedly released. Currently at the second 0.0.
#
get.normalised.ABP.stimulus <- function(
      sampling.time = 0.1,
      time.until.release = 10,
      time.after.release = 20,
      smooth.step.stimulus = FALSE,
      filter.order = 2,
      cutoff.frequency = 0.20,
      left.stabilisation.time = ifelse(smooth.step.stimulus, 30, 0),
      time.rounding.digits = format.info(sampling.time)[2],
      time.tol = sampling.time / 100
      )
{
  if(!is.divisible(time.until.release, sampling.time, time.tol))
    stop("time until release must be a multiple of the target sampling time")
  if(!is.divisible(time.after.release, sampling.time, time.tol))
    stop("time after release must be a multiple of the target sampling time")
	
	frequency <- 1 / sampling.time
  nsamples.stabilisation.left <-
    round(left.stabilisation.time / sampling.time)
  nsamples.until.release <-
    round(time.until.release / sampling.time) + 1
  nsamples.left <- nsamples.stabilisation.left + nsamples.until.release
  nsamples.after.release <- round(time.after.release / sampling.time)
  nsamples <- nsamples.until.release + nsamples.after.release
  
  # ABP step stimulus
  P <- c(rep(1, nsamples.left), rep(0, nsamples.after.release))
  
  # Smooths ABP step stimulus if corresponds
  if(smooth.step.stimulus)
  {
     wn <- cutoff.frequency / (frequency / 2)
     b <- butter(filter.order, wn)
     P <- as.numeric(filter(b, P))
  }
	
  if(nsamples.stabilisation.left > 0)
    P <- P[-(1:nsamples.stabilisation.left)]
  
  tini <- -time.until.release
  time <- seq(tini, length.out = nsamples, by = sampling.time)
	
  # Creates the answer
  ans <- list()
  ans[["time.instants"]] <- round(time, time.rounding.digits)
  ans[["ABP.normalised"]] <- P
  ans[["sampling.time"]] <- sampling.time
  ans[["time.release"]] <- ans[["time.instants"]][nsamples.until.release]
	
  invisible(ans)
}


normalise.signal <- function(
      signal,
      signal.baseline.value,
      signal.min.value
      )
{
  (signal - signal.min.value) / abs(signal.baseline.value - signal.min.value)
}


get.best.templates <- function(
      time.instants,
      signal,
      templates,
      referential.time.instant = 0,
      delta.time.before.ref = 0,
      delta.time.after.ref = 20 * 0.8,
      comparison.function = get.MSE,
      keep.details = TRUE,
      time.tol = min(diff(time.instants)) / 100,
      ... # Pass over to comparison.function()
      )
{
  # Validates lengths
  lengths <- c(length(time.instants), length(signal))
  lengths <- c(lengths, sapply(templates, length))
  lengths <- unique(lengths)
  if(length(lengths) != 1)
    stop("time, signal and templates must have the same length")
  
  # Initialises detailed answer
  ans <- list()
  ans[["time.instants"]] <- time.instants
  ans[["signal"]] <- signal
  ans[["templates"]] <- templates
  ans[["referential.time.instant"]] <- referential.time.instant
  
  # Finds referential sample
  i <- which(are.tolerably.equal(
    time.instants,
    referential.time.instant,
    time.tol
  ))
  if(length(i) != 1)
    stop("a unique referential time instant could not be determined")
  ans[["referential.sample"]] <- i
  
  # Finds initial sample
  ans[["delta.time.before.ref"]] <- delta.time.before.ref
  ans[["initial.time.instant"]] <- referential.time.instant - delta.time.before.ref
  i <- which(are.tolerably.equal(
    time.instants,
    ans[["initial.time.instant"]],
    time.tol
  ))
  if(length(i) != 1)
    stop("initial time instant could not be found in specified time instants")
  ans[["initial.sample"]] <- i
  
  # Finds final sample
  ans[["delta.time.after.ref"]] <- delta.time.after.ref
  ans[["final.time.instant"]] <-
    referential.time.instant + delta.time.after.ref
  i <- which(are.tolerably.equal(
    time.instants,
    ans[["final.time.instant"]],
    time.tol
  ))
  if(length(i) != 1)
    stop("final time instant could not be found in specified time instants")
  ans[["final.sample"]] <- i
  
  # Sets relevant interval
  ans[["relevant.samples"]] <-
    ans[["initial.sample"]]:ans[["final.sample"]]
  
  # Gets relevant segments
  .tmp.fun <- function(s) s[ans[["relevant.samples"]]]
  ans[["relevant.signal.segment"]] <- .tmp.fun(ans[["signal"]])
  ans[["relevant.template.segments"]] <-
    lapply(ans[["templates"]], .tmp.fun)
  
  # Gets fit values
  .tmp.fun <- function(t) 
    comparison.function(ans[["relevant.signal.segment"]], t, ...)
  ans[["fit.values"]] <-
    sapply(ans[["relevant.template.segments"]], .tmp.fun)
  
  # Orders templates and determines the best one
  ans[["ranking"]] <- order(ans[["fit.values"]])
  
  # Deletes details if corresponds
  if(!keep.details)
  {
    i <- ans[["ranking"]][1]
    e <- ans[["fit.values"]][i]
    ans <- list(best.template.index = i, best.fit.value = e)
  }
  
  ans
}



<file_sep><<<<<<< HEAD
<<<<<<< HEAD

install.packages('modeest', dep = TRUE)
library(modeest)

install.packages('ggplot2', dep = TRUE)
library(ggplot2)

install.packages('corrplot', dep = TRUE)
library('corrplot')

install.packages('mclust', dep = TRUE)
library(mclust)

library('corrplot')
library(ggplot2)
library(modeest)
library(mclust)

setwd("C:/Users/Luis.O.A/Documents/USACH/Mineria de Datos/grupo 6 Wine Quality")

data <- read.csv('winequality-red.csv', sep=';')

############ Estadistica Descriptiva ############

######## Medida de Tendencias Centrales

## Media ##

mean(data$fixed.acidity)
8.319637
mean(data$volatile.acidity)
0.5278205
mean(data$citric.acid)
0.2709756
mean(data$residual.sugar)
2.538806
mean(data$chlorides)
0.08746654
mean(data$free.sulfur.dioxide)
15.87492
mean(data$total.sulfur.dioxide)
46.46779
mean(data$density)
0.9967467
mean(data$pH)
3.311113
mean(data$sulphates)
0.6581488
mean(data$alcohol)
10.42298

## Moda ##

mlv(data$fixed.acidity, method = "mfv")[1]
7.2
mlv(data$volatile.acidity, method = "mfv")[1]
0.6
mlv(data$citric.acid, method = "mfv")[1]
0
mlv(data$residual.sugar, method = "mfv")[1]
2
mlv(data$chlorides, method = "mfv")[1]
0.08
mlv(data$free.sulfur.dioxide, method = "mfv")[1]
6
mlv(data$density, method = "mfv")[1]
0.9972
mlv(data$pH, method = "mfv")[1]
3.3
mlv(data$sulphates, method = "mfv")[1]
0.6
mlv(data$alcohol, method = "mfv")[1]
9.5

## Mediana ##

median(data$fixed.acidity)
7.9
median(data$volatile.acidity)
0.52
median(data$citric.acid)
0.26
median(data$residual.sugar)
2.2
median(data$chlorides)
0.079
median(data$free.sulfur.dioxide)
14
median(data$density)
0.99675
median(data$pH)
3.31
median(data$sulphates)
0.62
median(data$alcohol)
10.2

######## Medidas de Dispersion

## Rango ##

range(data$fixed.acidity)
4.6 15.9
range(data$volatile.acidity)
0.12 1.58
range(data$citric.acid)
0 1
range(data$residual.sugar)
0.9 15.5
range(data$chlorides)
0.012 0.611
range(data$free.sulfur.dioxide)
1 72
range(data$density)
0.99007 1.00369
range(data$pH)
 2.74 4.01
range(data$sulphates)
 0.33 2.00
range(data$alcohol)
8.4 14.9

summary(data)# Vista resumida de lo anteriormente visto. Seria util presentarlo en una tabla
boxplot(data$nombre_variable)# grafico de caja por variable

## Cuartiles ##

#El siguiente grafico sera muy util para explicr que incidencia tiene cada una de las variables e la calidad del vino.
boxplot(data$nombre_variable ~ data$quality, xlab = "Calidad", ylab = "Acides Fijada", col = c("red"))

## Varianza ##

var(data$fixed.acidity)
3.031416
var(data$volatile.acidity)
0.03206238
var(data$citric.acid)
0.03794748
var(data$residual.sugar)
1.987897
var(data$chlorides)
0.002215143
var(data$free.sulfur.dioxide)
109.4149
var(data$density)
3.562029e-06
var(data$pH)
0.02383518
var(data$sulphates)
0.02873262
var(data$alcohol)
1.135647

## Distribucion ##

#Nos servira para saber de manera mas grafica el rango y la mayor dispercion de cada una de las variables
hist(data$Nombre_variable, main = "Nombre_variable", xlab = "Acides Fijada", col = "grey", breaks = 40, 
     xlim = c(0,300))
	 
	 
############ Estadistica Inferencial ############

## Prueba de chi-cuadrado ## 

#NOTA: 
# "p-value" = 0 Alta dependencia entre variables

# "p-value" = 1 Nula dependencia entre variables

# "p-value" < 0.05 Alta dependencia entre variables (95% de dependencia)

summary(table(data$fixed.acidity,data$quality))
p-value = 1.416e-13
summary(table(data$volatile.acidity,data$quality))
p-value = 3.429e-69
summary(table(data$citric.acid,data$quality))
p-value = 6.425e-19
summary(table(data$residual.sugar,data$quality))
p-value = 1.649e-28
summary(table(data$chlorides,data$quality))
p-value = 1.181e-34
summary(table(data$free.sulfur.dioxide,data$quality))
p-value = 0.2399 #Esta es la unica variable que tiene una baja dependencia
summary(table(data$density,data$quality))
p-value = 7.547e-33
summary(table(data$pH,data$quality))
p-value = 8.719e-10
summary(table(data$sulphates,data$quality))
p-value = 2.382e-31
summary(table(data$alcohol,data$quality))
p-value = 5.345e-90

## Correlacion ##

M <- cor(data)
corrplot(M, method="circle")


################################### Noralizacion de los datos ################################### 

# NOTA:
# Cuando los rangos de las variables son muy dispares (ver "Rango") se aplica el siguiente modelo:
# X1 = (X1 - Media X1) / (max X1 - min X1) 
dataNorm <- as.data.frame( scale(data[1:11] )) # no se normaliza la clase

##https://www.analyticsvidhya.com/blog/2015/10/inferential-descriptive-statistics-beginners-r/


BIC = mclustBIC(dataNorm)
plot(BIC)

Mclust(dataNorm, modelNames ="VVV", G = 3)$BIC
Mclust(dataNorm, modelNames ="VVV", G = 4)$BIC
Mclust(dataNorm, modelNames ="VVV", G = 5)$BIC 
Mclust(dataNorm, modelNames ="VVV", G = 6)$BIC 
Mclust(dataNorm, modelNames ="VVV", G = 7)$BIC 

Mclust(dataNorm, modelNames ="VEV", G = 3)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 4)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 5)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 6)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 7)$BIC 

Mclust(dataNorm, modelNames ="VVE", G = 3)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 4)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 5)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 6)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 7)$BIC

# Mclust(dataNorm, prior = priorControl(functionName="defaultPrior", shrinkage=0.1), G = 4, modelNames ="VEV")$BIC

# Comprabar el error de clasificacion
m = Mclust(dataNorm, modelNames ="VVE", G = 7)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 7)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 6)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 5)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 4)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VVV", G = 4)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VVV", G = 7)
classError(data$quality, m$classification)$errorRate


################################### Sin Noralizacion de los datos ################################### 

dataS = data[1:11]

BIC = mclustBIC(dataS)
plot(BIC)

m1 = Mclust(dataS, modelNames ="VVE", G = 3)
m2 = Mclust(dataS, modelNames ="VVE", G = 4)
m3 = Mclust(dataS, modelNames ="VVE", G = 5)
m4 = Mclust(dataS, modelNames ="VVE", G = 6)

m5 = Mclust(dataS, modelNames ="EEV", G = 3)
m6 = Mclust(dataS, modelNames ="EEV", G = 4)
m7 = Mclust(dataS, modelNames ="EEV", G = 5)
m8 = Mclust(dataS, modelNames ="EEV", G = 6)

m9 = Mclust(dataS, modelNames ="VEV", G = 3)
m10 = Mclust(dataS, modelNames ="VEV", G = 4)
m11 = Mclust(dataS, modelNames ="VEV", G = 5)
m12 = Mclust(dataS, modelNames ="VEV", G = 6)

m13 = Mclust(dataS, modelNames ="EVV", G = 3)
m14 = Mclust(dataS, modelNames ="EVV", G = 4)
m15 = Mclust(dataS, modelNames ="EVV", G = 5)
m16 = Mclust(dataS, modelNames ="EVV", G = 6)

m1$BIC
m2$BIC
m3$BIC
m4$BIC
m5$BIC
m6$BIC
m7$BIC
m8$BIC
m9$BIC
m10$BIC
m11$BIC
m12$BIC
m13$BIC
m14$BIC
m15$BIC
m16$BIC

# Comprabar el error de clasificacion

classError(data$quality, m4$classification)$errorRate

classError(data$quality, m3$classification)$errorRate

classError(data$quality, m12$classification)$errorRate

classError(data$quality, m2$classification)$errorRate

classError(data$quality, m11$classification)$errorRate

classError(data$quality, m15$classification)$errorRate

classError(data$quality, m14$classification)$errorRate

classError(data$quality, m10$classification)$errorRate

# Mejor Cluster -> m14

# Grafico de clusters
drmod <- MclustDR(m14, lambda = 1)

plot(drmod, what = "contour")

plot(drmod, what = "boundaries", ngrid = 200)

#Grafica de densidad
plot(drmod, what = "density", type = "persp", theta = -25, phi = 20,
      border = adjustcolor(grey(0.1), alpha.f = 0.3))


table(data$quality, m14$classification) ##comprobar ERRORES segun quality


########################################

install.packages('plotly', dep = TRUE)
# segun calidad
data$quality.cut <- cut(data$quality, breaks = c(0,4,6,10))
table(data$quality.cut, m14$classification)
# segun pH
table(cut(data$pH, breaks = c(2.5, 3, 3.5, 4)), m14$classification)




library(plotly)

p <- plot_ly(
  x = data$quality.cut,
  y = data$pH,
  name = "pH",
  type = "bar"
)
# segun acides citrica
table(cut(data$citric.acid, breaks = c(0, 0.25, 0.5, 0.75, 1)), m14$classification)




library(plotly)

p <- plot_ly(
  x = data$quality.cut,
  y = data$citric.acid,
  name = "citric.acid",
  type = "bar"
)

# segun alcohol
table(cut(data$alcohol, breaks = c(8, 9, 10, 11, 12, 13 ,14, 15)), m14$classification)




library(plotly)

p <- plot_ly(
  x = data$quality.cut,
  y = data$alcohol,
  name = "alcohol",
  type = "bar"
)

###########################################

source("http://goo.gl/UUyEzD")
outlierKD(dataS, dataS$fixed.acidity)
outlierKD(dataS, dataS$volatile.acidity)
outlierKD(dataNorm, dataNorm$citric.acid)
outlierKD(dataNorm, dataNorm$residual.sugar)
outlierKD(dataNorm, dataNorm$chlorides)
outlierKD(dataNorm, dataNorm$free.sulfur.dioxide)
outlierKD(dataNorm, dataNorm$pH)
outlierKD(dataNorm, dataNorm$sulphates)
outlierKD(dataNorm, dataNorm$alcohol)
##


install.packages('outliers', dep = TRUE)
library(outliers)

dataS = data[1:11]

remove_outliers <- function(x, na.rm = TRUE, ...) {
  qnt <- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
  H <- 1.5 * IQR(x, na.rm = na.rm)
  y <- x
  y[x < (qnt[1] - H)] <- mean(x)#NA
  y[x > (qnt[2] + H)] <- mean(x)#NA
  y
}

dataS$alcohol <- remove_outliers(dataS$alcohol)
dataS$sulphates <- remove_outliers(dataS$sulphates)
dataS$pH <- remove_outliers(dataS$pH)
dataS$free.sulfur.dioxid <- remove_outliers(dataS$free.sulfur.dioxid)
dataS$chlorides <- remove_outliers(dataS$chlorides)
dataS$residual.sugar <- remove_outliers(dataS$residual.sugar)
dataS$citric.acid <- remove_outliers(dataS$citric.acid)
dataS$volatile.acidity <- remove_outliers(dataS$volatile.acidity)
dataS$fixed.acidity <- remove_outliers(dataS$fixed.acidity)
=======

install.packages('modeest', dep = TRUE)
library(modeest)

install.packages('ggplot2', dep = TRUE)
library(ggplot2)

install.packages('corrplot', dep = TRUE)
library('corrplot')

install.packages('mclust', dep = TRUE)
library(mclust)

library('corrplot')
library(ggplot2)
library(modeest)
library(mclust)

setwd("C:/Users/Luis.O.A/Documents/USACH/Mineria de Datos/grupo 6 Wine Quality")

data <- read.csv('winequality-red.csv', sep=';')

############ Estadistica Descriptiva ############

######## Medida de Tendencias Centrales

## Media ##

mean(data$fixed.acidity)
8.319637
mean(data$volatile.acidity)
0.5278205
mean(data$citric.acid)
0.2709756
mean(data$residual.sugar)
2.538806
mean(data$chlorides)
0.08746654
mean(data$free.sulfur.dioxide)
15.87492
mean(data$total.sulfur.dioxide)
46.46779
mean(data$density)
0.9967467
mean(data$pH)
3.311113
mean(data$sulphates)
0.6581488
mean(data$alcohol)
10.42298

## Moda ##

mlv(data$fixed.acidity, method = "mfv")[1]
7.2
mlv(data$volatile.acidity, method = "mfv")[1]
0.6
mlv(data$citric.acid, method = "mfv")[1]
0
mlv(data$residual.sugar, method = "mfv")[1]
2
mlv(data$chlorides, method = "mfv")[1]
0.08
mlv(data$free.sulfur.dioxide, method = "mfv")[1]
6
mlv(data$density, method = "mfv")[1]
0.9972
mlv(data$pH, method = "mfv")[1]
3.3
mlv(data$sulphates, method = "mfv")[1]
0.6
mlv(data$alcohol, method = "mfv")[1]
9.5

## Mediana ##

median(data$fixed.acidity)
7.9
median(data$volatile.acidity)
0.52
median(data$citric.acid)
0.26
median(data$residual.sugar)
2.2
median(data$chlorides)
0.079
median(data$free.sulfur.dioxide)
14
median(data$density)
0.99675
median(data$pH)
3.31
median(data$sulphates)
0.62
median(data$alcohol)
10.2

######## Medidas de Dispersion

## Rango ##

range(data$fixed.acidity)
4.6 15.9
range(data$volatile.acidity)
0.12 1.58
range(data$citric.acid)
0 1
range(data$residual.sugar)
0.9 15.5
range(data$chlorides)
0.012 0.611
range(data$free.sulfur.dioxide)
1 72
range(data$density)
0.99007 1.00369
range(data$pH)
 2.74 4.01
range(data$sulphates)
 0.33 2.00
range(data$alcohol)
8.4 14.9

summary(data)# Vista resumida de lo anteriormente visto. Seria util presentarlo en una tabla
boxplot(data$nombre_variable)# grafico de caja por variable

## Cuartiles ##

#El siguiente grafico sera muy util para explicr que incidencia tiene cada una de las variables e la calidad del vino.
boxplot(data$nombre_variable ~ data$quality, xlab = "Calidad", ylab = "Acides Fijada", col = c("red"))

## Varianza ##

var(data$fixed.acidity)
3.031416
var(data$volatile.acidity)
0.03206238
var(data$citric.acid)
0.03794748
var(data$residual.sugar)
1.987897
var(data$chlorides)
0.002215143
var(data$free.sulfur.dioxide)
109.4149
var(data$density)
3.562029e-06
var(data$pH)
0.02383518
var(data$sulphates)
0.02873262
var(data$alcohol)
1.135647

## Distribucion ##

#Nos servira para saber de manera mas grafica el rango y la mayor dispercion de cada una de las variables
hist(data$Nombre_variable, main = "Nombre_variable", xlab = "Acides Fijada", col = "grey", breaks = 40, 
     xlim = c(0,300))
	 
	 
############ Estadistica Inferencial ############

## Prueba de chi-cuadrado ## 

#NOTA: 
# "p-value" = 0 Alta dependencia entre variables

# "p-value" = 1 Nula dependencia entre variables

# "p-value" < 0.05 Alta dependencia entre variables (95% de dependencia)

summary(table(data$fixed.acidity,data$quality))
p-value = 1.416e-13
summary(table(data$volatile.acidity,data$quality))
p-value = 3.429e-69
summary(table(data$citric.acid,data$quality))
p-value = 6.425e-19
summary(table(data$residual.sugar,data$quality))
p-value = 1.649e-28
summary(table(data$chlorides,data$quality))
p-value = 1.181e-34
summary(table(data$free.sulfur.dioxide,data$quality))
p-value = 0.2399 #Esta es la unica variable que tiene una baja dependencia
summary(table(data$density,data$quality))
p-value = 7.547e-33
summary(table(data$pH,data$quality))
p-value = 8.719e-10
summary(table(data$sulphates,data$quality))
p-value = 2.382e-31
summary(table(data$alcohol,data$quality))
p-value = 5.345e-90

## Correlacion ##

M <- cor(data)
corrplot(M, method="circle")


################################### Noralizacion de los datos ################################### 

# NOTA:
# Cuando los rangos de las variables son muy dispares (ver "Rango") se aplica el siguiente modelo:
# X1 = (X1 - Media X1) / (max X1 - min X1) 
dataNorm <- as.data.frame( scale(data[1:11] )) # no se normaliza la clase

##https://www.analyticsvidhya.com/blog/2015/10/inferential-descriptive-statistics-beginners-r/


BIC = mclustBIC(dataNorm)
plot(BIC)

Mclust(dataNorm, modelNames ="VVV", G = 3)$BIC
Mclust(dataNorm, modelNames ="VVV", G = 4)$BIC
Mclust(dataNorm, modelNames ="VVV", G = 5)$BIC 
Mclust(dataNorm, modelNames ="VVV", G = 6)$BIC 
Mclust(dataNorm, modelNames ="VVV", G = 7)$BIC 

Mclust(dataNorm, modelNames ="VEV", G = 3)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 4)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 5)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 6)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 7)$BIC 

Mclust(dataNorm, modelNames ="VVE", G = 3)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 4)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 5)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 6)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 7)$BIC

# Mclust(dataNorm, prior = priorControl(functionName="defaultPrior", shrinkage=0.1), G = 4, modelNames ="VEV")$BIC

# Comprabar el error de clasificacion
m = Mclust(dataNorm, modelNames ="VVE", G = 7)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 7)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 6)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 5)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 4)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VVV", G = 4)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VVV", G = 7)
classError(data$quality, m$classification)$errorRate


################################### Sin Noralizacion de los datos ################################### 

dataS = data[1:11]

BIC = mclustBIC(dataS)
plot(BIC)

m1 = Mclust(dataS, modelNames ="VVE", G = 3)
m2 = Mclust(dataS, modelNames ="VVE", G = 4)
m3 = Mclust(dataS, modelNames ="VVE", G = 5)
m4 = Mclust(dataS, modelNames ="VVE", G = 6)

m5 = Mclust(dataS, modelNames ="EEV", G = 3)
m6 = Mclust(dataS, modelNames ="EEV", G = 4)
m7 = Mclust(dataS, modelNames ="EEV", G = 5)
m8 = Mclust(dataS, modelNames ="EEV", G = 6)

m9 = Mclust(dataS, modelNames ="VEV", G = 3)
m10 = Mclust(dataS, modelNames ="VEV", G = 4)
m11 = Mclust(dataS, modelNames ="VEV", G = 5)
m12 = Mclust(dataS, modelNames ="VEV", G = 6)

m13 = Mclust(dataS, modelNames ="EVV", G = 3)
m14 = Mclust(dataS, modelNames ="EVV", G = 4)
m15 = Mclust(dataS, modelNames ="EVV", G = 5)
m16 = Mclust(dataS, modelNames ="EVV", G = 6)

m1$BIC
m2$BIC
m3$BIC
m4$BIC
m5$BIC
m6$BIC
m7$BIC
m8$BIC
m9$BIC
m10$BIC
m11$BIC
m12$BIC
m13$BIC
m14$BIC
m15$BIC
m16$BIC

# Comprabar el error de clasificacion

classError(data$quality, m4$classification)$errorRate

classError(data$quality, m3$classification)$errorRate

classError(data$quality, m12$classification)$errorRate

classError(data$quality, m2$classification)$errorRate

classError(data$quality, m11$classification)$errorRate

classError(data$quality, m15$classification)$errorRate

classError(data$quality, m14$classification)$errorRate

classError(data$quality, m10$classification)$errorRate

# Mejor Cluster -> m14

# Grafico de clusters
drmod <- MclustDR(m14, lambda = 1)

plot(drmod, what = "contour")

plot(drmod, what = "boundaries", ngrid = 200)

#Grafica de densidad
plot(drmod, what = "density", type = "persp", theta = -25, phi = 20,
      border = adjustcolor(grey(0.1), alpha.f = 0.3))


table(data$quality, m14$classification) ##comprobar ERRORES segun quality


########################################

install.packages('plotly', dep = TRUE)
# segun calidad
data$quality.cut <- cut(data$quality, breaks = c(0,4,6,10))
table(data$quality.cut, m14$classification)
# segun pH
table(cut(data$pH, breaks = c(2.5, 3, 3.5, 4)), m14$classification)




library(plotly)

p <- plot_ly(
  x = data$quality.cut,
  y = data$pH,
  name = "pH",
  type = "bar"
)
# segun acides citrica
table(cut(data$citric.acid, breaks = c(0, 0.25, 0.5, 0.75, 1)), m14$classification)




library(plotly)

p <- plot_ly(
  x = data$quality.cut,
  y = data$citric.acid,
  name = "citric.acid",
  type = "bar"
)

# segun alcohol
table(cut(data$alcohol, breaks = c(8, 9, 10, 11, 12, 13 ,14, 15)), m14$classification)




library(plotly)

p <- plot_ly(
  x = data$quality.cut,
  y = data$alcohol,
  name = "alcohol",
  type = "bar"
)

###########################################

source("http://goo.gl/UUyEzD")
outlierKD(dataS, dataS$fixed.acidity)
outlierKD(dataS, dataS$volatile.acidity)
outlierKD(dataNorm, dataNorm$citric.acid)
outlierKD(dataNorm, dataNorm$residual.sugar)
outlierKD(dataNorm, dataNorm$chlorides)
outlierKD(dataNorm, dataNorm$free.sulfur.dioxide)
outlierKD(dataNorm, dataNorm$pH)
outlierKD(dataNorm, dataNorm$sulphates)
outlierKD(dataNorm, dataNorm$alcohol)
##


install.packages('outliers', dep = TRUE)
library(outliers)

dataS = data[1:11]

remove_outliers <- function(x, na.rm = TRUE, ...) {
  qnt <- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
  H <- 1.5 * IQR(x, na.rm = na.rm)
  y <- x
  y[x < (qnt[1] - H)] <- mean(x)#NA
  y[x > (qnt[2] + H)] <- mean(x)#NA
  y
}

dataS$alcohol <- remove_outliers(dataS$alcohol)
dataS$sulphates <- remove_outliers(dataS$sulphates)
dataS$pH <- remove_outliers(dataS$pH)
dataS$free.sulfur.dioxid <- remove_outliers(dataS$free.sulfur.dioxid)
dataS$chlorides <- remove_outliers(dataS$chlorides)
dataS$residual.sugar <- remove_outliers(dataS$residual.sugar)
dataS$citric.acid <- remove_outliers(dataS$citric.acid)
dataS$volatile.acidity <- remove_outliers(dataS$volatile.acidity)
dataS$fixed.acidity <- remove_outliers(dataS$fixed.acidity)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======

install.packages('modeest', dep = TRUE)
library(modeest)

install.packages('ggplot2', dep = TRUE)
library(ggplot2)

install.packages('corrplot', dep = TRUE)
library('corrplot')

install.packages('mclust', dep = TRUE)
library(mclust)

library('corrplot')
library(ggplot2)
library(modeest)
library(mclust)

setwd("C:/Users/Luis.O.A/Documents/USACH/Mineria de Datos/grupo 6 Wine Quality")

data <- read.csv('winequality-red.csv', sep=';')

############ Estadistica Descriptiva ############

######## Medida de Tendencias Centrales

## Media ##

mean(data$fixed.acidity)
8.319637
mean(data$volatile.acidity)
0.5278205
mean(data$citric.acid)
0.2709756
mean(data$residual.sugar)
2.538806
mean(data$chlorides)
0.08746654
mean(data$free.sulfur.dioxide)
15.87492
mean(data$total.sulfur.dioxide)
46.46779
mean(data$density)
0.9967467
mean(data$pH)
3.311113
mean(data$sulphates)
0.6581488
mean(data$alcohol)
10.42298

## Moda ##

mlv(data$fixed.acidity, method = "mfv")[1]
7.2
mlv(data$volatile.acidity, method = "mfv")[1]
0.6
mlv(data$citric.acid, method = "mfv")[1]
0
mlv(data$residual.sugar, method = "mfv")[1]
2
mlv(data$chlorides, method = "mfv")[1]
0.08
mlv(data$free.sulfur.dioxide, method = "mfv")[1]
6
mlv(data$density, method = "mfv")[1]
0.9972
mlv(data$pH, method = "mfv")[1]
3.3
mlv(data$sulphates, method = "mfv")[1]
0.6
mlv(data$alcohol, method = "mfv")[1]
9.5

## Mediana ##

median(data$fixed.acidity)
7.9
median(data$volatile.acidity)
0.52
median(data$citric.acid)
0.26
median(data$residual.sugar)
2.2
median(data$chlorides)
0.079
median(data$free.sulfur.dioxide)
14
median(data$density)
0.99675
median(data$pH)
3.31
median(data$sulphates)
0.62
median(data$alcohol)
10.2

######## Medidas de Dispersion

## Rango ##

range(data$fixed.acidity)
4.6 15.9
range(data$volatile.acidity)
0.12 1.58
range(data$citric.acid)
0 1
range(data$residual.sugar)
0.9 15.5
range(data$chlorides)
0.012 0.611
range(data$free.sulfur.dioxide)
1 72
range(data$density)
0.99007 1.00369
range(data$pH)
 2.74 4.01
range(data$sulphates)
 0.33 2.00
range(data$alcohol)
8.4 14.9

summary(data)# Vista resumida de lo anteriormente visto. Seria util presentarlo en una tabla
boxplot(data$nombre_variable)# grafico de caja por variable

## Cuartiles ##

#El siguiente grafico sera muy util para explicr que incidencia tiene cada una de las variables e la calidad del vino.
boxplot(data$nombre_variable ~ data$quality, xlab = "Calidad", ylab = "Acides Fijada", col = c("red"))

## Varianza ##

var(data$fixed.acidity)
3.031416
var(data$volatile.acidity)
0.03206238
var(data$citric.acid)
0.03794748
var(data$residual.sugar)
1.987897
var(data$chlorides)
0.002215143
var(data$free.sulfur.dioxide)
109.4149
var(data$density)
3.562029e-06
var(data$pH)
0.02383518
var(data$sulphates)
0.02873262
var(data$alcohol)
1.135647

## Distribucion ##

#Nos servira para saber de manera mas grafica el rango y la mayor dispercion de cada una de las variables
hist(data$Nombre_variable, main = "Nombre_variable", xlab = "Acides Fijada", col = "grey", breaks = 40, 
     xlim = c(0,300))
	 
	 
############ Estadistica Inferencial ############

## Prueba de chi-cuadrado ## 

#NOTA: 
# "p-value" = 0 Alta dependencia entre variables

# "p-value" = 1 Nula dependencia entre variables

# "p-value" < 0.05 Alta dependencia entre variables (95% de dependencia)

summary(table(data$fixed.acidity,data$quality))
p-value = 1.416e-13
summary(table(data$volatile.acidity,data$quality))
p-value = 3.429e-69
summary(table(data$citric.acid,data$quality))
p-value = 6.425e-19
summary(table(data$residual.sugar,data$quality))
p-value = 1.649e-28
summary(table(data$chlorides,data$quality))
p-value = 1.181e-34
summary(table(data$free.sulfur.dioxide,data$quality))
p-value = 0.2399 #Esta es la unica variable que tiene una baja dependencia
summary(table(data$density,data$quality))
p-value = 7.547e-33
summary(table(data$pH,data$quality))
p-value = 8.719e-10
summary(table(data$sulphates,data$quality))
p-value = 2.382e-31
summary(table(data$alcohol,data$quality))
p-value = 5.345e-90

## Correlacion ##

M <- cor(data)
corrplot(M, method="circle")


################################### Noralizacion de los datos ################################### 

# NOTA:
# Cuando los rangos de las variables son muy dispares (ver "Rango") se aplica el siguiente modelo:
# X1 = (X1 - Media X1) / (max X1 - min X1) 
dataNorm <- as.data.frame( scale(data[1:11] )) # no se normaliza la clase

##https://www.analyticsvidhya.com/blog/2015/10/inferential-descriptive-statistics-beginners-r/


BIC = mclustBIC(dataNorm)
plot(BIC)

Mclust(dataNorm, modelNames ="VVV", G = 3)$BIC
Mclust(dataNorm, modelNames ="VVV", G = 4)$BIC
Mclust(dataNorm, modelNames ="VVV", G = 5)$BIC 
Mclust(dataNorm, modelNames ="VVV", G = 6)$BIC 
Mclust(dataNorm, modelNames ="VVV", G = 7)$BIC 

Mclust(dataNorm, modelNames ="VEV", G = 3)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 4)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 5)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 6)$BIC 
Mclust(dataNorm, modelNames ="VEV", G = 7)$BIC 

Mclust(dataNorm, modelNames ="VVE", G = 3)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 4)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 5)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 6)$BIC 
Mclust(dataNorm, modelNames ="VVE", G = 7)$BIC

# Mclust(dataNorm, prior = priorControl(functionName="defaultPrior", shrinkage=0.1), G = 4, modelNames ="VEV")$BIC

# Comprabar el error de clasificacion
m = Mclust(dataNorm, modelNames ="VVE", G = 7)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 7)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 6)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 5)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VEV", G = 4)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VVV", G = 4)
classError(data$quality, m$classification)$errorRate

m = Mclust(dataNorm, modelNames ="VVV", G = 7)
classError(data$quality, m$classification)$errorRate


################################### Sin Noralizacion de los datos ################################### 

dataS = data[1:11]

BIC = mclustBIC(dataS)
plot(BIC)

m1 = Mclust(dataS, modelNames ="VVE", G = 3)
m2 = Mclust(dataS, modelNames ="VVE", G = 4)
m3 = Mclust(dataS, modelNames ="VVE", G = 5)
m4 = Mclust(dataS, modelNames ="VVE", G = 6)

m5 = Mclust(dataS, modelNames ="EEV", G = 3)
m6 = Mclust(dataS, modelNames ="EEV", G = 4)
m7 = Mclust(dataS, modelNames ="EEV", G = 5)
m8 = Mclust(dataS, modelNames ="EEV", G = 6)

m9 = Mclust(dataS, modelNames ="VEV", G = 3)
m10 = Mclust(dataS, modelNames ="VEV", G = 4)
m11 = Mclust(dataS, modelNames ="VEV", G = 5)
m12 = Mclust(dataS, modelNames ="VEV", G = 6)

m13 = Mclust(dataS, modelNames ="EVV", G = 3)
m14 = Mclust(dataS, modelNames ="EVV", G = 4)
m15 = Mclust(dataS, modelNames ="EVV", G = 5)
m16 = Mclust(dataS, modelNames ="EVV", G = 6)

m1$BIC
m2$BIC
m3$BIC
m4$BIC
m5$BIC
m6$BIC
m7$BIC
m8$BIC
m9$BIC
m10$BIC
m11$BIC
m12$BIC
m13$BIC
m14$BIC
m15$BIC
m16$BIC

# Comprabar el error de clasificacion

classError(data$quality, m4$classification)$errorRate

classError(data$quality, m3$classification)$errorRate

classError(data$quality, m12$classification)$errorRate

classError(data$quality, m2$classification)$errorRate

classError(data$quality, m11$classification)$errorRate

classError(data$quality, m15$classification)$errorRate

classError(data$quality, m14$classification)$errorRate

classError(data$quality, m10$classification)$errorRate

# Mejor Cluster -> m14

# Grafico de clusters
drmod <- MclustDR(m14, lambda = 1)

plot(drmod, what = "contour")

plot(drmod, what = "boundaries", ngrid = 200)

#Grafica de densidad
plot(drmod, what = "density", type = "persp", theta = -25, phi = 20,
      border = adjustcolor(grey(0.1), alpha.f = 0.3))


table(data$quality, m14$classification) ##comprobar ERRORES segun quality


########################################

install.packages('plotly', dep = TRUE)
# segun calidad
data$quality.cut <- cut(data$quality, breaks = c(0,4,6,10))
table(data$quality.cut, m14$classification)
# segun pH
table(cut(data$pH, breaks = c(2.5, 3, 3.5, 4)), m14$classification)




library(plotly)

p <- plot_ly(
  x = data$quality.cut,
  y = data$pH,
  name = "pH",
  type = "bar"
)
# segun acides citrica
table(cut(data$citric.acid, breaks = c(0, 0.25, 0.5, 0.75, 1)), m14$classification)




library(plotly)

p <- plot_ly(
  x = data$quality.cut,
  y = data$citric.acid,
  name = "citric.acid",
  type = "bar"
)

# segun alcohol
table(cut(data$alcohol, breaks = c(8, 9, 10, 11, 12, 13 ,14, 15)), m14$classification)




library(plotly)

p <- plot_ly(
  x = data$quality.cut,
  y = data$alcohol,
  name = "alcohol",
  type = "bar"
)

###########################################

source("http://goo.gl/UUyEzD")
outlierKD(dataS, dataS$fixed.acidity)
outlierKD(dataS, dataS$volatile.acidity)
outlierKD(dataNorm, dataNorm$citric.acid)
outlierKD(dataNorm, dataNorm$residual.sugar)
outlierKD(dataNorm, dataNorm$chlorides)
outlierKD(dataNorm, dataNorm$free.sulfur.dioxide)
outlierKD(dataNorm, dataNorm$pH)
outlierKD(dataNorm, dataNorm$sulphates)
outlierKD(dataNorm, dataNorm$alcohol)
##


install.packages('outliers', dep = TRUE)
library(outliers)

dataS = data[1:11]

remove_outliers <- function(x, na.rm = TRUE, ...) {
  qnt <- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
  H <- 1.5 * IQR(x, na.rm = na.rm)
  y <- x
  y[x < (qnt[1] - H)] <- mean(x)#NA
  y[x > (qnt[2] + H)] <- mean(x)#NA
  y
}

dataS$alcohol <- remove_outliers(dataS$alcohol)
dataS$sulphates <- remove_outliers(dataS$sulphates)
dataS$pH <- remove_outliers(dataS$pH)
dataS$free.sulfur.dioxid <- remove_outliers(dataS$free.sulfur.dioxid)
dataS$chlorides <- remove_outliers(dataS$chlorides)
dataS$residual.sugar <- remove_outliers(dataS$residual.sugar)
dataS$citric.acid <- remove_outliers(dataS$citric.acid)
dataS$volatile.acidity <- remove_outliers(dataS$volatile.acidity)
dataS$fixed.acidity <- remove_outliers(dataS$fixed.acidity)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep>import os
#FORZAR USO DE CPU
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 

import time
import pandas as pd
import numpy
import matplotlib.pyplot as plt
from keras import Sequential
from keras.models import load_model, Model
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.initializers import glorot_uniform
from keras.utils import to_categorical
from keras.optimizers import Adam
from keras import backend as K
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from pandas import DataFrame
from pandas.plotting import table
from math import sqrt, log, exp
import math
from joblib import Parallel, delayed
import tensorflow as tf
import gc
from statistics import mean,stdev
from functools import reduce
from scipy import stats
from numpy.random import seed
from tensorflow import set_random_seed

CPU_USAGE = 7
GPU_USAGE = 1

def create_dataset(nombre_sujeto, nombre_postura):
    PATH = ("C:/Users/Luis/Documents/DataScience/Tesis/Datos/SUJETOS/%s/%s-%s-VE.csv"%(nombre_sujeto, nombre_sujeto, nombre_postura))
    X = pd.read_csv(PATH, sep="	")
    
    # normalize the dataset
    scaler = MinMaxScaler(feature_range=(0, 1))
    
    VFSCd = scaler.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
    VFSCi = scaler.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
    PAMn = scaler.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
    
    #Dar formato float a las señales
    PAMn, VFSCd = PAMn.astype('float32'), VFSCd.astype('float32')
    PAMn, VFSCd = numpy.array(PAMn), numpy.array(VFSCd)
    
    # Validacion Valanceada
    train_size = int(len(PAMn) * 0.5)
    test_size = len(PAMn) - train_size
    train_PAM, train_VFSCd = PAMn[0:train_size], VFSCd[0:train_size]
    test_PAM, test_VFSCd = PAMn[train_size:len(PAMn)], VFSCd[train_size:len(VFSCd)]
    
    # Reshape segun el formato que acepta Keras
    # reshape input to be [samples, time steps, features]
    train_PAM = numpy.reshape(train_PAM, (train_PAM.shape[0], 1, train_PAM.shape[1]))
    test_PAM = numpy.reshape(test_PAM, (test_PAM.shape[0], 1, test_PAM.shape[1]))
    
    return train_PAM, train_VFSCd, test_PAM, test_VFSCd
    

def correlation_coefficient_loss_metric(y_true, y_pred):
    x = y_true
    y = y_pred
    mx = K.mean(x)
    my = K.mean(y)
    xm, ym = x-mx, y-my
    r_num = K.sum(tf.multiply(xm,ym))
    r_den = K.sqrt(tf.multiply(K.sum(K.square(xm)), K.sum(K.square(ym))))
    r = r_num / r_den

    r = K.maximum(K.minimum(r, 1.0), -1.0)
    return 1 - K.square(r)


# fit an LSTM network to training data
def fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout):
    
    ret_seq = False
    if hidden_layers > 1:
        ret_seq = True

    model = Sequential()
    model.add(LSTM(neurons, batch_input_shape=(batch_size, trainX.shape[1], trainX.shape[2]), return_sequences=ret_seq, stateful=True, recurrent_activation=activation, recurrent_dropout=dropout))
    for i in range (hidden_layers-1):
        if i == (hidden_layers-2):
            ret_seq = False
        model.add(LSTM(neurons, return_sequences=ret_seq, stateful = True, recurrent_activation=activation, recurrent_dropout=dropout))
    model.add(Dense(trainX.shape[1], activation=activation))

    model.compile(loss='mean_squared_error', optimizer=optimization)
    for i in range(epochs):
        model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
        model.reset_states()
    return model


#Evaluate the Model
def evaluate(model, X, Y, batch_size):

    output = model.predict(X, batch_size=batch_size)
    
    # invert data transforms on forecast
    # report performance
    rmse = stats.pearsonr(Y[:,0], output[:,0])
    
    return rmse

# Guardar en formato PNG los resultados
def save_results(results, nombre_archivo_resultados):

    desc = results.describe()
    #create a subplot without frame
    plot = plt.subplot(111, frame_on=False)
    
    #remove axis
    plot.xaxis.set_visible(False) 
    plot.yaxis.set_visible(False) 
    
    #create the table plot and position it in the upper left corner
    table(plot, desc,loc='upper right')
    
    #save the plot as a png file
    plt.savefig('desc_%s.png'%(nombre_archivo_resultados))
    plt.close()
    
    results.boxplot()
    plt.savefig('boxplot_%s.png'%(nombre_archivo_resultados))
    plt.close()

# run a repeated experiment
def experiment(trainX, trainY, testX, testY, repeats, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout):
    # run experiment

    lista_z = list()
    repetir_error = 0
    i = 1
    del_level_hyp = False
    while i <= repeats:
        # fit the model
        use_cpu_gpu()
        lstm_model = fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, hidden_layers, neurons, dropout)
        # report performance
        r = evaluate(lstm_model, testX, testY, batch_size)[0]
        
        if math.isnan(r) and repetir_error < 3:
            repetir_error = repetir_error + 1
            if repetir_error == 3:
                del_level_hyp = True
                break
        else:
            if repetir_error != 3:
                lista_z.append(fisher_transform(r))
            repetir_error = 0
            i = i + 1

        
        K.clear_session()
        del lstm_model
        gc.collect()

    if del_level_hyp:
        mean_z = float('nan')
        mean_corr = float('nan')
    else:
        mean_z = reduce(lambda x, y: x + y, lista_z) / len(lista_z)
        mean_corr = inverse_fisher_transform(mean_z)

    print('batch_size=%d, neurons=%d, hidden_layers=%d, dropout=%.1f, epochs=%d, optimization=%s, activation=%s:::::::::: RESULT CORR=%.3f, RESULT Z=%.3f' % (batch_size, neurons, hidden_layers, dropout, epochs, optimization, activation, mean_corr, mean_z))
    
    return mean_corr, mean_z

#Transfomracion de Fisher
def fisher_transform(r):
    z = 0.5*log((1+r)/(1-r))
    return z

def inverse_fisher_transform(z):
    r = (exp(2*z)-1)/(exp(2*z)+1)
    return r

def use_cpu_gpu():
    config = tf.ConfigProto(intra_op_parallelism_threads=10,
                            inter_op_parallelism_threads=10, 
                            allow_soft_placement=True,
                            device_count = {'CPU' : CPU_USAGE,
                                            'GPU' : GPU_USAGE}
                           )
    #config.gpu_options.allow_growth = True
    session = tf.Session(config=config)
    K.set_session(session)

def run_experiment(experimento, sujeto, postura, balance, trainX, trainY, testX, testY, hyperparameter, batch_size=[2], epochs=[10], optimization=["RMSprop"], activation=["tanh"], hidden_layers=[2], neurons=[10], dropout=[0.9]):
    #take time
    t0 = time.time()
    print("################################################################################### " + hyperparameter)
    
    columnas = ['batch_size','neurons','hidden_layers','dropout','epochs','optimization','activation','CORRELATION','FISHER']
    filas = len(batch_size) * len(epochs) * len(optimization) * len(activation) * len(hidden_layers) * len(neurons) * len(dropout)
    results = numpy.chararray((filas,9), itemsize=40)
    row = 0
    repeats = 10
    best_result = -1
    best_row = 0
    
    for b in batch_size:
        for e in epochs:
            for o in optimization:
                for a in activation:
                    for h in hidden_layers:
                        for n in neurons:
                            for d in dropout:

                                mean_corr, mean_z = experiment(trainX, trainY, testX, testY, repeats, b, e, o, a, h, n, d)
                                results[row][0] = b
                                results[row][1] = n
                                results[row][2] = h
                                results[row][3] = d
                                results[row][4] = e
                                results[row][5] = o
                                results[row][6] = a
                                results[row][7] = mean_corr
                                results[row][8] = mean_z
                            
                                if best_result <= mean_corr:
                                    best_result = mean_corr
                                    best_row = row

                                row = row + 1
    
    df = pd.DataFrame(results, columns=columnas)
    df = df.sort_values(by='CORRELATION', ascending=False)
    print(df)
    nombre_archivo = sujeto+'_'+postura+'/Sujeto_Posicion_'+hyperparameter+'_'+str(balance)+'.xlsx'
    writer = pd.ExcelWriter('C:/Users/Luis/Documents/DataScience/Tesis/Resultados/Fundamentacion Ajuste Modelo/Resultados_'+experimento+'/'+nombre_archivo)
    df.to_excel(writer,'Resultados')
    writer.save()
    print('################################################################################### Archivo |||'+nombre_archivo+'||| Creado')
    print ('###################################################################################',time.time() - t0, "segundos tardo")

    if hyperparameter == "epochs":
        return results[best_row][4]
    if hyperparameter == "dropout":
        return results[best_row][3]
    if hyperparameter == "activation":
        return results[best_row][6].decode("utf-8") 
    if hyperparameter == "optimization":
        return results[best_row][5].decode("utf-8")
    if hyperparameter == "neurons":
        return results[best_row][1] 
    if hyperparameter == "batch_size":
        return results[best_row][0]
    if hyperparameter == "hidden_layers":
        return results[best_row][2]
    if hyperparameter == "deep_network":
        return [int(results[best_row][2])],[int(results[best_row][1])],[str(results[best_row][6].decode("utf-8"))]
        


def run_rango (sujeto, postura, balance):
    print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
    print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
    print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
    print('++++++++++++++++++++++++++++++++++++++ Sujeto: ' + sujeto + ' Posicion: ' + postura + ' Balance: ' + str(balance))
    # load dataset
    # split into input (X) and output (Y) variables
    
    train_PAM, train_VFSCd, test_PAM, test_VFSCd = create_dataset(sujeto, postura)
    if balance == 2:
        train_PAM_temp = train_PAM
        train_PAM = test_PAM
        test_PAM = train_PAM_temp
        del train_PAM_temp
        train_VFSCd_temp = train_VFSCd
        train_VFSCd = test_VFSCd
        test_VFSCd = train_VFSCd_temp
        del train_VFSCd_temp
    
    ################################################################################### batch_size
    batch_size = []
    for i in range(1,train_PAM.shape[0]+1):
        if (train_PAM.shape[0]%i)==0 and i>1:
            batch_size.append(i)
    batch_size_result = [int(run_experiment('Rango', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="batch_size", batch_size=batch_size))]
    batch_size_result = [198]
    ################################################################################### neurons
    #neurons = [10,20,30,40,50,60,70,80,90,100]
    #neurons_result = [int(run_experiment('Rango', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="neurons", neurons=neurons, batch_size=batch_size_result))]
    
    ################################################################################### dropout
    dropout = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
    dropout_result = [float(run_experiment('Rango', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="dropout", dropout=dropout, batch_size=batch_size_result))]

    ################################################################################### deep network (hidden_layers, neurons, activation)
    hidden_layers = [2, 3, 4, 5]
    neurons = [10,20,30,40,50,60,70,80,90,100]
    activation = ['softplus', 'softsign', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear']
    hidden_layers_result, neurons_result, activation_result = run_experiment('Rango', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="deep_network", hidden_layers=hidden_layers, neurons=neurons, activation=activation, dropout=dropout_result, batch_size=batch_size_result)

    ################################################################################### activation
    #activation = ['softplus', 'softsign', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear']
    #activation_result = [str(run_experiment('Rango', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="activation", activation=activation, dropout=dropout_result, neurons=neurons_result, batch_size=batch_size_result))]
    #activation_result = ['tanh']#Es el unico que puede modelar respuesta a escalones (de 2 capas)

    ################################################################################### optimization
    optimization = ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
    optimization_result = [str(run_experiment('Rango', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="optimization", optimization=optimization, hidden_layers=hidden_layers_result, activation=activation_result, dropout=dropout_result, neurons=neurons_result, batch_size=batch_size_result))]

    ################################################################################### epochs
    epochs = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    epochs_result = [int(run_experiment('Rango', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="epochs", epochs=epochs, optimization=optimization_result, hidden_layers=hidden_layers_result, activation=activation_result, dropout=dropout_result, neurons=neurons_result, batch_size=batch_size_result))]

def run_hiperparametro (sujeto, postura, balance):
    print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
    print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
    print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
    print('++++++++++++++++++++++++++++++++++++++ Sujeto: ' + sujeto + ' Posicion: ' + postura + ' Balance: ' + str(balance))

    # load dataset
    # split into input (X) and output (Y) variables
    train_PAM, train_VFSCd, test_PAM, test_VFSCd = create_dataset(sujeto, postura)
    if balance == 2:
        train_PAM_temp = train_PAM
        train_PAM = test_PAM
        test_PAM = train_PAM_temp
        del train_PAM_temp
        train_VFSCd_temp = train_VFSCd
        train_VFSCd = test_VFSCd
        test_VFSCd = train_VFSCd_temp
        del train_VFSCd_temp
    
    ################################################################################### Base
    run_experiment('Hiperparametro', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="Base")
    
    ################################################################################### epochs
    epochs = [10, 20, 30, 40, 50]
    run_experiment('Hiperparametro', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="epochs", epochs=epochs)

    ################################################################################### activation
    activation = ['softplus', 'softsign', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear']
    run_experiment('Hiperparametro', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="activation", activation=activation)
    
    ################################################################################### optimization
    optimization = ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
    run_experiment('Hiperparametro', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="optimization", optimization=optimization)
    
    ################################################################################### neurons
    neurons = [10,20,30,40,50,60,70,80,90,100]
    run_experiment('Hiperparametro', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="neurons", neurons=neurons)

    ################################################################################### batch_size
    batch_size = []
    for i in range(1,train_PAM.shape[0]+1):
        if (train_PAM.shape[0]%i)==0:
            batch_size.append(i)

    run_experiment('Hiperparametro', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="batch_size", batch_size=batch_size)

    ################################################################################### hidden_layers
    hidden_layers = [2, 3, 4, 5]
    run_experiment('Hiperparametro', sujeto, postura, balance, train_PAM, train_VFSCd, test_PAM, test_VFSCd, hyperparameter="hidden_layers", hidden_layers=hidden_layers)

#Repitable Experiment
seed(1)
set_random_seed(2)

run_rango(sujeto='AC', postura='ACOSTADO', balance=1) 
#run_rango(sujeto='AC', postura='ACOSTADO', balance=2) 

#run_rango(sujeto='DM', postura='PIE', balance=1) 
#run_rango(sujeto='DM', postura='PIE', balance=2) 

#run_rango(sujeto='PC', postura='SENTADO', balance=1) 
#run_rango(sujeto='PC', postura='SENTADO', balance=2) 

#run_rango(sujeto='AV', postura='ACOSTADO', balance=1) 
#run_rango(sujeto='AV', postura='ACOSTADO', balance=2) 

#run_rango(sujeto='CC', postura='PIE', balance=1) 
#run_rango(sujeto='CC', postura='PIE', balance=2) 

#run_rango(sujeto='CS', postura='SENTADO', balance=1) 
#run_rango(sujeto='CS', postura='SENTADO', balance=2) <file_sep><<<<<<< HEAD
<<<<<<< HEAD
library(nnet)
#https://www.kaggle.com/vmalyi/run-or-walk/data
setwd("C:/Users/Luis.O.A/Documents/Charla ML/DataScience")

wr <- read.csv('dataset.csv', sep=',')

wr <- wr[,-c(1,2,3)]

normalize <- function(x) {
	x = (x-min(x))/(max(x)-min(x))
	x
}
#hist(wine$fixed.acidity,breaks=10, xlab="wine",col="lightblue", main="")

wr$activity <- factor(wr$activity, levels=sort(unique(wr$activity)))
wr$wrist <- normalize(wr$wrist)
wr$acceleration_x <- normalize(wr$acceleration_x)
wr$acceleration_y <- normalize(wr$acceleration_y)
wr$acceleration_z <- normalize(wr$acceleration_z)
wr$gyro_x <- normalize(wr$gyro_x)
wr$gyro_y <- normalize(wr$gyro_y)
wr$gyro_z <- normalize(wr$gyro_z)

set.seed(100)
trainingRows <- sample(1:nrow(wr), 0.7*nrow(wr))
training <- wr[trainingRows, ]
test <- wr[-trainingRows, -2]
test.activity <- wr[-trainingRows, -c(1,3,4,5,6,7, 8)]#Solo la clase


model <- multinom(activity ~ ., data = training)

##Prueba de clasificacion con las 10 primeras instancias
predicted=predict(model,wr[750:760,], 'probs')

predicted
##Verificar las clases
wr[750:760,]

#############################################

predicted=predict(model,test)

result <- table(predicted, test.activity)

#Correctos
(result[1,1] + result[2,2]) * 100 / (result[1,1] + result[1,2] + result[2,1] + result[2,2])
#86.12334

#Error
mean(as.character(predicted) != as.character(test.activity)) * 100
#13.87666

Precision <- as.numeric(result[1,1])/(as.numeric(result[1,1]) + as.numeric(result[1,2]))

#Recall
Recall <- as.numeric(result[1,1])/(as.numeric(result[1,1]) + as.numeric(result[2,1]))

##Fscore
Fscore = (2*Precision*Recall)/(Precision+Recall)
print("Fscore")
print(Fscore)
print("Precision")
print(Precision)
print("Recall")
print(Recall)
	


=======
library(nnet)
#https://www.kaggle.com/vmalyi/run-or-walk/data
setwd("C:/Users/Luis.O.A/Documents/Charla ML/DataScience")

wr <- read.csv('dataset.csv', sep=',')

wr <- wr[,-c(1,2,3)]

normalize <- function(x) {
	x = (x-min(x))/(max(x)-min(x))
	x
}
#hist(wine$fixed.acidity,breaks=10, xlab="wine",col="lightblue", main="")

wr$activity <- factor(wr$activity, levels=sort(unique(wr$activity)))
wr$wrist <- normalize(wr$wrist)
wr$acceleration_x <- normalize(wr$acceleration_x)
wr$acceleration_y <- normalize(wr$acceleration_y)
wr$acceleration_z <- normalize(wr$acceleration_z)
wr$gyro_x <- normalize(wr$gyro_x)
wr$gyro_y <- normalize(wr$gyro_y)
wr$gyro_z <- normalize(wr$gyro_z)

set.seed(100)
trainingRows <- sample(1:nrow(wr), 0.7*nrow(wr))
training <- wr[trainingRows, ]
test <- wr[-trainingRows, -2]
test.activity <- wr[-trainingRows, -c(1,3,4,5,6,7, 8)]#Solo la clase


model <- multinom(activity ~ ., data = training)

##Prueba de clasificacion con las 10 primeras instancias
predicted=predict(model,wr[750:760,], 'probs')

predicted
##Verificar las clases
wr[750:760,]

#############################################

predicted=predict(model,test)

result <- table(predicted, test.activity)

#Correctos
(result[1,1] + result[2,2]) * 100 / (result[1,1] + result[1,2] + result[2,1] + result[2,2])
#86.12334

#Error
mean(as.character(predicted) != as.character(test.activity)) * 100
#13.87666

Precision <- as.numeric(result[1,1])/(as.numeric(result[1,1]) + as.numeric(result[1,2]))

#Recall
Recall <- as.numeric(result[1,1])/(as.numeric(result[1,1]) + as.numeric(result[2,1]))

##Fscore
Fscore = (2*Precision*Recall)/(Precision+Recall)
print("Fscore")
print(Fscore)
print("Precision")
print(Precision)
print("Recall")
print(Recall)
	


>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
library(nnet)
#https://www.kaggle.com/vmalyi/run-or-walk/data
setwd("C:/Users/Luis.O.A/Documents/Charla ML/DataScience")

wr <- read.csv('dataset.csv', sep=',')

wr <- wr[,-c(1,2,3)]

normalize <- function(x) {
	x = (x-min(x))/(max(x)-min(x))
	x
}
#hist(wine$fixed.acidity,breaks=10, xlab="wine",col="lightblue", main="")

wr$activity <- factor(wr$activity, levels=sort(unique(wr$activity)))
wr$wrist <- normalize(wr$wrist)
wr$acceleration_x <- normalize(wr$acceleration_x)
wr$acceleration_y <- normalize(wr$acceleration_y)
wr$acceleration_z <- normalize(wr$acceleration_z)
wr$gyro_x <- normalize(wr$gyro_x)
wr$gyro_y <- normalize(wr$gyro_y)
wr$gyro_z <- normalize(wr$gyro_z)

set.seed(100)
trainingRows <- sample(1:nrow(wr), 0.7*nrow(wr))
training <- wr[trainingRows, ]
test <- wr[-trainingRows, -2]
test.activity <- wr[-trainingRows, -c(1,3,4,5,6,7, 8)]#Solo la clase


model <- multinom(activity ~ ., data = training)

##Prueba de clasificacion con las 10 primeras instancias
predicted=predict(model,wr[750:760,], 'probs')

predicted
##Verificar las clases
wr[750:760,]

#############################################

predicted=predict(model,test)

result <- table(predicted, test.activity)

#Correctos
(result[1,1] + result[2,2]) * 100 / (result[1,1] + result[1,2] + result[2,1] + result[2,2])
#86.12334

#Error
mean(as.character(predicted) != as.character(test.activity)) * 100
#13.87666

Precision <- as.numeric(result[1,1])/(as.numeric(result[1,1]) + as.numeric(result[1,2]))

#Recall
Recall <- as.numeric(result[1,1])/(as.numeric(result[1,1]) + as.numeric(result[2,1]))

##Fscore
Fscore = (2*Precision*Recall)/(Precision+Recall)
print("Fscore")
print(Fscore)
print("Precision")
print(Precision)
print("Recall")
print(Recall)
	


>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep><<<<<<< HEAD
<<<<<<< HEAD
import os
import csv
import math
import numpy as np
import pandas as pd
from scipy import stats
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn.metrics import silhouette_score
from sklearn import preprocessing
from matplotlib import pyplot as plt
from scipy import signal

class Signal:
    PATH = "C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/CO2_HIPERCAPNIA"
    
    def __init__(self, filePath):
        self.filePath = filePath
        
    def reesampling(self, var):
        x_resampled = signal.resample(var, math.ceil(var.shape[0]*0.26675603217158176943699731903485))
        return x_resampled
     
    def process(self):
        X = pd.read_csv(self.filePath, sep="	")
        
        VFSCd = self.reesampling(X['VFSCd'])
        VFSCi = self.reesampling(X['VFSCi'])
        PAMn = self.reesampling(X['PAMn'])
        
        
        reesampled_data = zip(VFSCd,VFSCi, PAMn)
        
        with open(self.filePath[:(len(self.filePath)-3)] + "csv", 'w', newline='') as f:
            writer = csv.writer(f, delimiter='\t')
            writer.writerow(["VFSCd", "VFSCi", "PAMn"])
            writer.writerows(reesampled_data)
        
def run_signal():    
    listOfFolders = os.listdir(Signal.PATH)

    folderPath = Signal.PATH

    for file in listOfFolders:
        filePath = folderPath+"/"+file
        signal = Signal(filePath)
        signal.process()


class Stair:
        
    def __init__(self, file_stair):
        self.file_stair = file_stair

    def reesampling(self, var):
        x_resampled = signal.resample(var, math.ceil(var.shape[0]*0.26533333333333333333333333333333))#199 ==> 50%
        return x_resampled
     
    def process(self):
        X = pd.read_csv(self.file_stair, sep="	")
        
        escalon = self.reesampling(X['ESCALON'])

        reesampled_data = zip(escalon)
        
        with open(self.file_stair[:(len(self.file_stair)-4)] + "_04.csv", 'w', newline='') as f:
            writer = csv.writer(f, delimiter='\t')
            writer.writerow(["ESCALON"])
            writer.writerows(reesampled_data)

def run_stair():   
    file_stair = "C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/esc.csv"     
    stair = Stair(file_stair)
    stair.process()

=======
import os
import csv
import math
import numpy as np
import pandas as pd
from scipy import stats
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn.metrics import silhouette_score
from sklearn import preprocessing
from matplotlib import pyplot as plt
from scipy import signal

class Signal:
    PATH = "C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/CO2_HIPERCAPNIA"
    
    def __init__(self, filePath):
        self.filePath = filePath
        
    def reesampling(self, var):
        x_resampled = signal.resample(var, math.ceil(var.shape[0]*0.26675603217158176943699731903485))
        return x_resampled
     
    def process(self):
        X = pd.read_csv(self.filePath, sep="	")
        
        VFSCd = self.reesampling(X['VFSCd'])
        VFSCi = self.reesampling(X['VFSCi'])
        PAMn = self.reesampling(X['PAMn'])
        
        
        reesampled_data = zip(VFSCd,VFSCi, PAMn)
        
        with open(self.filePath[:(len(self.filePath)-3)] + "csv", 'w', newline='') as f:
            writer = csv.writer(f, delimiter='\t')
            writer.writerow(["VFSCd", "VFSCi", "PAMn"])
            writer.writerows(reesampled_data)
        
def run_signal():    
    listOfFolders = os.listdir(Signal.PATH)

    folderPath = Signal.PATH

    for file in listOfFolders:
        filePath = folderPath+"/"+file
        signal = Signal(filePath)
        signal.process()


class Stair:
        
    def __init__(self, file_stair):
        self.file_stair = file_stair

    def reesampling(self, var):
        x_resampled = signal.resample(var, math.ceil(var.shape[0]*0.26533333333333333333333333333333))#199 ==> 50%
        return x_resampled
     
    def process(self):
        X = pd.read_csv(self.file_stair, sep="	")
        
        escalon = self.reesampling(X['ESCALON'])

        reesampled_data = zip(escalon)
        
        with open(self.file_stair[:(len(self.file_stair)-4)] + "_04.csv", 'w', newline='') as f:
            writer = csv.writer(f, delimiter='\t')
            writer.writerow(["ESCALON"])
            writer.writerows(reesampled_data)

def run_stair():   
    file_stair = "C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/esc.csv"     
    stair = Stair(file_stair)
    stair.process()

>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
import os
import csv
import math
import numpy as np
import pandas as pd
from scipy import stats
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn.metrics import silhouette_score
from sklearn import preprocessing
from matplotlib import pyplot as plt
from scipy import signal

class Signal:
    PATH = "C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/CO2_HIPERCAPNIA"
    
    def __init__(self, filePath):
        self.filePath = filePath
        
    def reesampling(self, var):
        x_resampled = signal.resample(var, math.ceil(var.shape[0]*0.26675603217158176943699731903485))
        return x_resampled
     
    def process(self):
        X = pd.read_csv(self.filePath, sep="	")
        
        VFSCd = self.reesampling(X['VFSCd'])
        VFSCi = self.reesampling(X['VFSCi'])
        PAMn = self.reesampling(X['PAMn'])
        
        
        reesampled_data = zip(VFSCd,VFSCi, PAMn)
        
        with open(self.filePath[:(len(self.filePath)-3)] + "csv", 'w', newline='') as f:
            writer = csv.writer(f, delimiter='\t')
            writer.writerow(["VFSCd", "VFSCi", "PAMn"])
            writer.writerows(reesampled_data)
        
def run_signal():    
    listOfFolders = os.listdir(Signal.PATH)

    folderPath = Signal.PATH

    for file in listOfFolders:
        filePath = folderPath+"/"+file
        signal = Signal(filePath)
        signal.process()


class Stair:
        
    def __init__(self, file_stair):
        self.file_stair = file_stair

    def reesampling(self, var):
        x_resampled = signal.resample(var, math.ceil(var.shape[0]*0.26533333333333333333333333333333))#199 ==> 50%
        return x_resampled
     
    def process(self):
        X = pd.read_csv(self.file_stair, sep="	")
        
        escalon = self.reesampling(X['ESCALON'])

        reesampled_data = zip(escalon)
        
        with open(self.file_stair[:(len(self.file_stair)-4)] + "_04.csv", 'w', newline='') as f:
            writer = csv.writer(f, delimiter='\t')
            writer.writerow(["ESCALON"])
            writer.writerows(reesampled_data)

def run_stair():   
    file_stair = "C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/esc.csv"     
    stair = Stair(file_stair)
    stair.process()

>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
run_signal()<file_sep><<<<<<< HEAD
<<<<<<< HEAD
from __future__ import print_function
import pandas as pd
import numpy
import matplotlib.pyplot as plt
import math
from keras.models import load_model, Model, Sequential
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.initializers import glorot_uniform
from keras.utils import to_categorical
from keras.optimizers import Adam
from keras import backend as K
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from pandas import DataFrame
from pandas.plotting import table
from math import sqrt
from joblib import Parallel, delayed
import tensorflow as tf
from scipy import stats


class Entrenamiento:
    '''    
    #Por batch
    def correlation_coefficient_loss(y_true, y_pred):
        fsp = y_pred - K.mean(y_pred) #being K.mean a scalar here, it will be automatically subtracted from all elements in y_pred
        fst = y_true - K.mean(y_true)
        
        devP = K.std(y_pred)
        devT = K.std(y_true)
        
        return K.mean(fsp*fst)/(devP*devT)
        
    #Por cada ejemplo
    def correlation_coefficient_loss(y_true, y_pred):
        #original shapes: (batch, 10)
        
        fsp = y_pred - K.mean(y_pred,axis=0) #you take the mean over the batch, keeping the features separate.   
        fst = y_true - K.mean(y_true,axis=0) 
        #mean shape: (1,10)
        #fst shape keeps (batch,10)
        
        devP = K.std(y_pred,axis=0)  
        devt = K.std(y_true,axis=0)
        #dev shape: (1,10)
        #mean shape: (1,10), making all tensors in the expression be (1,10). 
        #sum is only necessary because we need a single loss value
        return K.sum(K.mean(fsp*fst,axis=0)/(devP*devT))
    '''       
    
    def correlation_coefficient_loss(y_true, y_pred):
        x = y_true
        y = y_pred
        mx = K.mean(x)
        my = K.mean(y)
        xm, ym = x-mx, y-my
        r_num = K.sum(tf.multiply(xm,ym))
        r_den = K.sqrt(tf.multiply(K.sum(K.square(xm)), K.sum(K.square(ym))))
        r = r_num / r_den
    
        r = K.maximum(K.minimum(r, 1.0), -1.0)
        return 1 - K.square(r)
    # fit an LSTM network to training data
    def fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, dropout, hidden_layers, neurons):
        
        model = Sequential()
        model.add(LSTM(neurons, batch_input_shape=(batch_size, trainX.shape[1], trainX.shape[2]), stateful=True, return_sequences=True))
        for i in range (hidden_layers-1):
            model.add(LSTM(neurons, batch_input_shape=(batch_size, trainX.shape[1], trainX.shape[2]), stateful=True))
        model.add(Dropout(dropout))
        model.add(Dense(1, activation=activation))
        model.compile(loss=Entrenamiento.correlation_coefficient_loss, optimizer=optimization)
        
        for i in range(epochs):
            model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False)
            model.reset_states()
    
        return model
    
    #Evaluate the Model
    def evaluate(model, X, Y, batch_size):
        
        output = model.predict(X, batch_size=batch_size)
        # invert data transforms on forecast
        yhat = list()
        for i in range(len(output)):
            # store forecast
            yhat.append(output[i,0])
        # report performance
        rmse = stats.pearsonr(Y[:,0], yhat)
        
        return rmse
    
    # Guardar en formato PNG los resultados
    def save_results(results, nombre_archivo_resultados):
        
        desc = results.describe()
        #create a subplot without frame
        plot = plt.subplot(111, frame_on=False)
        
        #remove axis
        plot.xaxis.set_visible(False) 
        plot.yaxis.set_visible(False) 
        
        #create the table plot and position it in the upper left corner
        table(plot, desc,loc='upper right')
        
        #save the plot as a png file
        plt.savefig('desc_%s.png'%(nombre_archivo_resultados))
        plt.close()
        
        results.boxplot()
        plt.savefig('boxplot_%s.png'%(nombre_archivo_resultados))
        plt.close()
    
        # run a repeated experiment
    def experiment(trainX, trainY, testX, testY, repeats, batch_size, epochs, optimization, activation, dropout, hidden_layers, neurons):
        # run experiment
        error_scores = list()
        for r in range(repeats):
            # fit the model
            lstm_model = Entrenamiento.fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, dropout, hidden_layers, neurons)
            
            lstm_model.predict(trainX, batch_size=batch_size)
            
        # report performance
        rmse = Entrenamiento.evaluate(lstm_model, testX, testY, batch_size)
        print('%d) Test RMSE: %.3f' % (r+1, rmse))
        error_scores.append(neurons)
        return error_scores
        
#QUITAR PARALEL DE ESTE METODO PARA CORRER LAS PRUEBAS

    def run_experiment(trainX, trainY, testX, testY, batch_size=[1], epochs=[1], optimization=["adam"], activation=["sigmoid"], dropout=[0], hidden_layers=[1], neurons=[1]):
        
        results = []
        results_data_frame = DataFrame()
        repeats = 1
        
        if (len(batch_size) > 1):
            for b in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, b, epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0])
        
        if (len(epochs) > 1):
            for e in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], e, optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0])

        if (len(optimization) > 1):
            for o in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], o, activation[0], dropout[0], hidden_layers[0], neurons[0])
                
        if (len(activation) > 1):
            for a in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], a, dropout[0], hidden_layers[0], neurons[0])
                
        if (len(dropout) > 1):
            for d in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], d, hidden_layers[0], neurons[0])
                
        if (len(hidden_layers) > 1):
            for h in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], h, neurons[0])
                
        if (len(neurons) > 1):
            for n in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], n)
        '''
        if (len(batch_size) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, b, epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0]) for b in batch_size)
            
        elif (len(epochs) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], e, optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0]) for e in epochs)
            
        elif (len(optimization) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], o, activation[0], dropout[0], hidden_layers[0], neurons[0]) for o in optimization)
            
        elif (len(activation) > 1):
            results = Parallel(n_jobs=4)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], a, dropout[0], hidden_layers[0], neurons[0]) for a in activation)
            
        elif (len(dropout) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], d, hidden_layers[0], neurons[0]) for d in dropout)
            
        elif (len(hidden_layers) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], h, neurons[0]) for h in hidden_layers)
            
        elif (len(neurons) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], n) for n in neurons)
        '''
        for i in range(len(results)):
            results_data_frame[str(i)] = results[i]
        
        # summarize results
        print(results);
        #print(results_data_frame.describe())
        # save boxplot
        #Entrenamiento.save_results(results_data_frame, "nombre_archivo")

###########################################################################################################    
nombre_sujeto = "AC"
nombre_postura = "ACOSTADO"
nombre_emisferio = "DERECHO"


PATH = ("C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/%s/%s-%s-VE.csv"%(nombre_sujeto, nombre_sujeto, nombre_postura))
X = pd.read_csv(PATH, sep="	")

# normalize the dataset
scaler = MinMaxScaler(feature_range=(-1, 1))

VFSCd = scaler.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
VFSCi = scaler.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
PAMn = scaler.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
# fix random seed for reproducibility
numpy.random.seed(7)

#Dar formato float a las señales
PAMn, VFSCd = PAMn.astype('float32'), VFSCd.astype('float32')
PAMn, VFSCd = numpy.array(PAMn), numpy.array(VFSCd)

# Validacion Valanceada
train_size = int(len(PAMn) * 0.75)
test_size = len(PAMn) - train_size
train_PAM, train_VFSCd = PAMn[0:train_size], VFSCd[0:train_size]
test_PAM, test_VFSCd = PAMn[train_size:len(PAMn)], VFSCd[train_size:len(VFSCd)]

# Reshape segun el formato que acepta Keras
# reshape input to be [samples, time steps, features]
train_PAM = numpy.reshape(train_PAM, (train_PAM.shape[0], 1, train_PAM.shape[1]))
test_PAM = numpy.reshape(test_PAM, (test_PAM.shape[0], 1, test_PAM.shape[1]))
# create and fit the LSTM network
##############################################################################################################################################################
##Hyperparametros
numpy.random.uniform(low=0.5, high=13.3, size=(5,))
#################
nombre_archivo_resultados = nombre_sujeto+"_"+nombre_postura+"_"+nombre_emisferio
Entrenamiento.run_experiment(train_PAM, train_VFSCd, test_PAM, test_VFSCd, epochs = [2,3])
##############################################################################################################################################################

        
###########################################################################################################################
#random search
#https://github.com/llSourcell/hyperparameter_optimization_strategies/blob/master/random_search.py


Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))

#################








# Use scikit-learn to grid search the batch size and epochs
import pandas as pd
import numpy
import keras.backend as K
import tensorflow as tf
import math
from scipy import stats
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.wrappers.scikit_learn import KerasRegressor

    
def correlation_coefficient_loss(y_true, y_pred):
    fsp = y_pred - K.mean(y_pred,axis=0) #you take the mean over the batch, keeping the features separate.   
    fst = y_true - K.mean(y_true,axis=0) 
        #mean shape: (1,10)
        #fst shape keeps (batch,10)
    
    devP = K.std(y_pred,axis=0)  
    devT = K.std(y_true,axis=0)
        #dev shape: (1,10)
    
    return K.sum(K.mean(fsp*fst,axis=0)/(devP*devT))

# Function to create model, required for KerasClassifier
def create_model(neurons=1):
    # create model
    model = Sequential()
    #model.add(LSTM(1, return_sequences=True))
    model.add(LSTM(neurons))
    model.add(Dense(1))
    # Compile model
    model.compile(loss=correlation_coefficient_loss, optimizer='adam')
    return model

def create_dataset():
    nombre_sujeto = "AC"
    nombre_postura = "ACOSTADO"
    nombre_emisferio = "DERECHO"
    
    PATH = ("C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/%s/%s-%s-VE.csv"%(nombre_sujeto, nombre_sujeto, nombre_postura))
    X = pd.read_csv(PATH, sep="	")
    
    # normalize the dataset
    scaler = MinMaxScaler(feature_range=(-1, 1))
    
    VFSCd = scaler.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
    VFSCi = scaler.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
    PAMn = scaler.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
    # fix random seed for reproducibility
    numpy.random.seed(7)
    
    #Dar formato float a las señales
    PAMn, VFSCd, VFSCi = PAMn.astype('float32'), VFSCd.astype('float32'), VFSCi.astype('float32')
    PAMn, VFSCd, VFSCi = numpy.array(PAMn), numpy.array(VFSCd), numpy.array(VFSCi)
    
    
    # Reshape segun el formato que acepta Keras
    # reshape input to be [samples, time steps, features]
    PAMn = numpy.reshape(PAMn, (PAMn.shape[0], 1, PAMn.shape[1]))
    return PAMn, VFSCd, VFSCi

def run_experiment(hyperparam_grid):
    # create model
    model = KerasRegressor(build_fn=create_model, verbose=0)

    grid = GridSearchCV(estimator=model, param_grid=hyperparam_grid, cv=5, n_jobs=1)
    grid_result = grid.fit(X, Y1)
    # summarize results
    print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
    means = grid_result.cv_results_['mean_test_score']
    stds = grid_result.cv_results_['std_test_score']
    params = grid_result.cv_results_['params']
    for mean, stdev, param in zip(means, stds, params):
        print("%f (%f) with: %r" % (mean, stdev, param))
    

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
# split into input (X) and output (Y) variables
X, Y1, Y2 = create_dataset()

# define the grid search parameters
##Tuning the Number of Epochs
##Tuning the Batch Size
##Tuning the Number of Neurons
##Tuning the Number of Hidden Layers
#################################################################### batch_size
batch_size = []
for i in range(1,500):
    if (math.floor(X.shape[0] - (X.shape[0]/5))%i)==0:
        batch_size.append(i)

hyperparam_grid = dict(batch_size=batch_size)
run_experiment(hyperparam_grid)
        
######################################################################## epochs
#epochs = numpy.random.randint(low=1, high=10000, size=(10,))
epochs = [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]

hyperparam_grid = dict(epochs=epochs)
run_experiment(hyperparam_grid)

####################################################################### neurons
neurons = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

hyperparam_grid = dict(neurons=neurons)
run_experiment(hyperparam_grid)

################################################################## dropout_rate
dropout_rate = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
hyperparam_grid = dict(dropout_rate=dropout_rate)
run_experiment(hyperparam_grid)





batch_size = [1]

epochs = [100]

neurons = [10]

hyperparam_grid = dict(batch_size=batch_size, epochs=epochs, neurons=neurons)
=======
from __future__ import print_function
import pandas as pd
import numpy
import matplotlib.pyplot as plt
import math
from keras.models import load_model, Model, Sequential
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.initializers import glorot_uniform
from keras.utils import to_categorical
from keras.optimizers import Adam
from keras import backend as K
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from pandas import DataFrame
from pandas.plotting import table
from math import sqrt
from joblib import Parallel, delayed
import tensorflow as tf
from scipy import stats


class Entrenamiento:
    '''    
    #Por batch
    def correlation_coefficient_loss(y_true, y_pred):
        fsp = y_pred - K.mean(y_pred) #being K.mean a scalar here, it will be automatically subtracted from all elements in y_pred
        fst = y_true - K.mean(y_true)
        
        devP = K.std(y_pred)
        devT = K.std(y_true)
        
        return K.mean(fsp*fst)/(devP*devT)
        
    #Por cada ejemplo
    def correlation_coefficient_loss(y_true, y_pred):
        #original shapes: (batch, 10)
        
        fsp = y_pred - K.mean(y_pred,axis=0) #you take the mean over the batch, keeping the features separate.   
        fst = y_true - K.mean(y_true,axis=0) 
        #mean shape: (1,10)
        #fst shape keeps (batch,10)
        
        devP = K.std(y_pred,axis=0)  
        devt = K.std(y_true,axis=0)
        #dev shape: (1,10)
        #mean shape: (1,10), making all tensors in the expression be (1,10). 
        #sum is only necessary because we need a single loss value
        return K.sum(K.mean(fsp*fst,axis=0)/(devP*devT))
    '''       
    
    def correlation_coefficient_loss(y_true, y_pred):
        x = y_true
        y = y_pred
        mx = K.mean(x)
        my = K.mean(y)
        xm, ym = x-mx, y-my
        r_num = K.sum(tf.multiply(xm,ym))
        r_den = K.sqrt(tf.multiply(K.sum(K.square(xm)), K.sum(K.square(ym))))
        r = r_num / r_den
    
        r = K.maximum(K.minimum(r, 1.0), -1.0)
        return 1 - K.square(r)
    # fit an LSTM network to training data
    def fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, dropout, hidden_layers, neurons):
        
        model = Sequential()
        model.add(LSTM(neurons, batch_input_shape=(batch_size, trainX.shape[1], trainX.shape[2]), stateful=True, return_sequences=True))
        for i in range (hidden_layers-1):
            model.add(LSTM(neurons, batch_input_shape=(batch_size, trainX.shape[1], trainX.shape[2]), stateful=True))
        model.add(Dropout(dropout))
        model.add(Dense(1, activation=activation))
        model.compile(loss=Entrenamiento.correlation_coefficient_loss, optimizer=optimization)
        
        for i in range(epochs):
            model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False)
            model.reset_states()
    
        return model
    
    #Evaluate the Model
    def evaluate(model, X, Y, batch_size):
        
        output = model.predict(X, batch_size=batch_size)
        # invert data transforms on forecast
        yhat = list()
        for i in range(len(output)):
            # store forecast
            yhat.append(output[i,0])
        # report performance
        rmse = stats.pearsonr(Y[:,0], yhat)
        
        return rmse
    
    # Guardar en formato PNG los resultados
    def save_results(results, nombre_archivo_resultados):
        
        desc = results.describe()
        #create a subplot without frame
        plot = plt.subplot(111, frame_on=False)
        
        #remove axis
        plot.xaxis.set_visible(False) 
        plot.yaxis.set_visible(False) 
        
        #create the table plot and position it in the upper left corner
        table(plot, desc,loc='upper right')
        
        #save the plot as a png file
        plt.savefig('desc_%s.png'%(nombre_archivo_resultados))
        plt.close()
        
        results.boxplot()
        plt.savefig('boxplot_%s.png'%(nombre_archivo_resultados))
        plt.close()
    
        # run a repeated experiment
    def experiment(trainX, trainY, testX, testY, repeats, batch_size, epochs, optimization, activation, dropout, hidden_layers, neurons):
        # run experiment
        error_scores = list()
        for r in range(repeats):
            # fit the model
            lstm_model = Entrenamiento.fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, dropout, hidden_layers, neurons)
            
            lstm_model.predict(trainX, batch_size=batch_size)
            
        # report performance
        rmse = Entrenamiento.evaluate(lstm_model, testX, testY, batch_size)
        print('%d) Test RMSE: %.3f' % (r+1, rmse))
        error_scores.append(neurons)
        return error_scores
        
#QUITAR PARALEL DE ESTE METODO PARA CORRER LAS PRUEBAS

    def run_experiment(trainX, trainY, testX, testY, batch_size=[1], epochs=[1], optimization=["adam"], activation=["sigmoid"], dropout=[0], hidden_layers=[1], neurons=[1]):
        
        results = []
        results_data_frame = DataFrame()
        repeats = 1
        
        if (len(batch_size) > 1):
            for b in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, b, epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0])
        
        if (len(epochs) > 1):
            for e in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], e, optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0])

        if (len(optimization) > 1):
            for o in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], o, activation[0], dropout[0], hidden_layers[0], neurons[0])
                
        if (len(activation) > 1):
            for a in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], a, dropout[0], hidden_layers[0], neurons[0])
                
        if (len(dropout) > 1):
            for d in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], d, hidden_layers[0], neurons[0])
                
        if (len(hidden_layers) > 1):
            for h in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], h, neurons[0])
                
        if (len(neurons) > 1):
            for n in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], n)
        '''
        if (len(batch_size) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, b, epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0]) for b in batch_size)
            
        elif (len(epochs) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], e, optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0]) for e in epochs)
            
        elif (len(optimization) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], o, activation[0], dropout[0], hidden_layers[0], neurons[0]) for o in optimization)
            
        elif (len(activation) > 1):
            results = Parallel(n_jobs=4)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], a, dropout[0], hidden_layers[0], neurons[0]) for a in activation)
            
        elif (len(dropout) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], d, hidden_layers[0], neurons[0]) for d in dropout)
            
        elif (len(hidden_layers) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], h, neurons[0]) for h in hidden_layers)
            
        elif (len(neurons) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], n) for n in neurons)
        '''
        for i in range(len(results)):
            results_data_frame[str(i)] = results[i]
        
        # summarize results
        print(results);
        #print(results_data_frame.describe())
        # save boxplot
        #Entrenamiento.save_results(results_data_frame, "nombre_archivo")

###########################################################################################################    
nombre_sujeto = "AC"
nombre_postura = "ACOSTADO"
nombre_emisferio = "DERECHO"


PATH = ("C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/%s/%s-%s-VE.csv"%(nombre_sujeto, nombre_sujeto, nombre_postura))
X = pd.read_csv(PATH, sep="	")

# normalize the dataset
scaler = MinMaxScaler(feature_range=(-1, 1))

VFSCd = scaler.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
VFSCi = scaler.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
PAMn = scaler.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
# fix random seed for reproducibility
numpy.random.seed(7)

#Dar formato float a las señales
PAMn, VFSCd = PAMn.astype('float32'), VFSCd.astype('float32')
PAMn, VFSCd = numpy.array(PAMn), numpy.array(VFSCd)

# Validacion Valanceada
train_size = int(len(PAMn) * 0.75)
test_size = len(PAMn) - train_size
train_PAM, train_VFSCd = PAMn[0:train_size], VFSCd[0:train_size]
test_PAM, test_VFSCd = PAMn[train_size:len(PAMn)], VFSCd[train_size:len(VFSCd)]

# Reshape segun el formato que acepta Keras
# reshape input to be [samples, time steps, features]
train_PAM = numpy.reshape(train_PAM, (train_PAM.shape[0], 1, train_PAM.shape[1]))
test_PAM = numpy.reshape(test_PAM, (test_PAM.shape[0], 1, test_PAM.shape[1]))
# create and fit the LSTM network
##############################################################################################################################################################
##Hyperparametros
numpy.random.uniform(low=0.5, high=13.3, size=(5,))
#################
nombre_archivo_resultados = nombre_sujeto+"_"+nombre_postura+"_"+nombre_emisferio
Entrenamiento.run_experiment(train_PAM, train_VFSCd, test_PAM, test_VFSCd, epochs = [2,3])
##############################################################################################################################################################

        
###########################################################################################################################
#random search
#https://github.com/llSourcell/hyperparameter_optimization_strategies/blob/master/random_search.py


Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))

#################








# Use scikit-learn to grid search the batch size and epochs
import pandas as pd
import numpy
import keras.backend as K
import tensorflow as tf
import math
from scipy import stats
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.wrappers.scikit_learn import KerasRegressor

    
def correlation_coefficient_loss(y_true, y_pred):
    fsp = y_pred - K.mean(y_pred,axis=0) #you take the mean over the batch, keeping the features separate.   
    fst = y_true - K.mean(y_true,axis=0) 
        #mean shape: (1,10)
        #fst shape keeps (batch,10)
    
    devP = K.std(y_pred,axis=0)  
    devT = K.std(y_true,axis=0)
        #dev shape: (1,10)
    
    return K.sum(K.mean(fsp*fst,axis=0)/(devP*devT))

# Function to create model, required for KerasClassifier
def create_model(neurons=1):
    # create model
    model = Sequential()
    #model.add(LSTM(1, return_sequences=True))
    model.add(LSTM(neurons))
    model.add(Dense(1))
    # Compile model
    model.compile(loss=correlation_coefficient_loss, optimizer='adam')
    return model

def create_dataset():
    nombre_sujeto = "AC"
    nombre_postura = "ACOSTADO"
    nombre_emisferio = "DERECHO"
    
    PATH = ("C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/%s/%s-%s-VE.csv"%(nombre_sujeto, nombre_sujeto, nombre_postura))
    X = pd.read_csv(PATH, sep="	")
    
    # normalize the dataset
    scaler = MinMaxScaler(feature_range=(-1, 1))
    
    VFSCd = scaler.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
    VFSCi = scaler.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
    PAMn = scaler.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
    # fix random seed for reproducibility
    numpy.random.seed(7)
    
    #Dar formato float a las señales
    PAMn, VFSCd, VFSCi = PAMn.astype('float32'), VFSCd.astype('float32'), VFSCi.astype('float32')
    PAMn, VFSCd, VFSCi = numpy.array(PAMn), numpy.array(VFSCd), numpy.array(VFSCi)
    
    
    # Reshape segun el formato que acepta Keras
    # reshape input to be [samples, time steps, features]
    PAMn = numpy.reshape(PAMn, (PAMn.shape[0], 1, PAMn.shape[1]))
    return PAMn, VFSCd, VFSCi

def run_experiment(hyperparam_grid):
    # create model
    model = KerasRegressor(build_fn=create_model, verbose=0)

    grid = GridSearchCV(estimator=model, param_grid=hyperparam_grid, cv=5, n_jobs=1)
    grid_result = grid.fit(X, Y1)
    # summarize results
    print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
    means = grid_result.cv_results_['mean_test_score']
    stds = grid_result.cv_results_['std_test_score']
    params = grid_result.cv_results_['params']
    for mean, stdev, param in zip(means, stds, params):
        print("%f (%f) with: %r" % (mean, stdev, param))
    

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
# split into input (X) and output (Y) variables
X, Y1, Y2 = create_dataset()

# define the grid search parameters
##Tuning the Number of Epochs
##Tuning the Batch Size
##Tuning the Number of Neurons
##Tuning the Number of Hidden Layers
#################################################################### batch_size
batch_size = []
for i in range(1,500):
    if (math.floor(X.shape[0] - (X.shape[0]/5))%i)==0:
        batch_size.append(i)

hyperparam_grid = dict(batch_size=batch_size)
run_experiment(hyperparam_grid)
        
######################################################################## epochs
#epochs = numpy.random.randint(low=1, high=10000, size=(10,))
epochs = [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]

hyperparam_grid = dict(epochs=epochs)
run_experiment(hyperparam_grid)

####################################################################### neurons
neurons = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

hyperparam_grid = dict(neurons=neurons)
run_experiment(hyperparam_grid)

################################################################## dropout_rate
dropout_rate = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
hyperparam_grid = dict(dropout_rate=dropout_rate)
run_experiment(hyperparam_grid)





batch_size = [1]

epochs = [100]

neurons = [10]

hyperparam_grid = dict(batch_size=batch_size, epochs=epochs, neurons=neurons)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
from __future__ import print_function
import pandas as pd
import numpy
import matplotlib.pyplot as plt
import math
from keras.models import load_model, Model, Sequential
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.initializers import glorot_uniform
from keras.utils import to_categorical
from keras.optimizers import Adam
from keras import backend as K
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from pandas import DataFrame
from pandas.plotting import table
from math import sqrt
from joblib import Parallel, delayed
import tensorflow as tf
from scipy import stats


class Entrenamiento:
    '''    
    #Por batch
    def correlation_coefficient_loss(y_true, y_pred):
        fsp = y_pred - K.mean(y_pred) #being K.mean a scalar here, it will be automatically subtracted from all elements in y_pred
        fst = y_true - K.mean(y_true)
        
        devP = K.std(y_pred)
        devT = K.std(y_true)
        
        return K.mean(fsp*fst)/(devP*devT)
        
    #Por cada ejemplo
    def correlation_coefficient_loss(y_true, y_pred):
        #original shapes: (batch, 10)
        
        fsp = y_pred - K.mean(y_pred,axis=0) #you take the mean over the batch, keeping the features separate.   
        fst = y_true - K.mean(y_true,axis=0) 
        #mean shape: (1,10)
        #fst shape keeps (batch,10)
        
        devP = K.std(y_pred,axis=0)  
        devt = K.std(y_true,axis=0)
        #dev shape: (1,10)
        #mean shape: (1,10), making all tensors in the expression be (1,10). 
        #sum is only necessary because we need a single loss value
        return K.sum(K.mean(fsp*fst,axis=0)/(devP*devT))
    '''       
    
    def correlation_coefficient_loss(y_true, y_pred):
        x = y_true
        y = y_pred
        mx = K.mean(x)
        my = K.mean(y)
        xm, ym = x-mx, y-my
        r_num = K.sum(tf.multiply(xm,ym))
        r_den = K.sqrt(tf.multiply(K.sum(K.square(xm)), K.sum(K.square(ym))))
        r = r_num / r_den
    
        r = K.maximum(K.minimum(r, 1.0), -1.0)
        return 1 - K.square(r)
    # fit an LSTM network to training data
    def fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, dropout, hidden_layers, neurons):
        
        model = Sequential()
        model.add(LSTM(neurons, batch_input_shape=(batch_size, trainX.shape[1], trainX.shape[2]), stateful=True, return_sequences=True))
        for i in range (hidden_layers-1):
            model.add(LSTM(neurons, batch_input_shape=(batch_size, trainX.shape[1], trainX.shape[2]), stateful=True))
        model.add(Dropout(dropout))
        model.add(Dense(1, activation=activation))
        model.compile(loss=Entrenamiento.correlation_coefficient_loss, optimizer=optimization)
        
        for i in range(epochs):
            model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False)
            model.reset_states()
    
        return model
    
    #Evaluate the Model
    def evaluate(model, X, Y, batch_size):
        
        output = model.predict(X, batch_size=batch_size)
        # invert data transforms on forecast
        yhat = list()
        for i in range(len(output)):
            # store forecast
            yhat.append(output[i,0])
        # report performance
        rmse = stats.pearsonr(Y[:,0], yhat)
        
        return rmse
    
    # Guardar en formato PNG los resultados
    def save_results(results, nombre_archivo_resultados):
        
        desc = results.describe()
        #create a subplot without frame
        plot = plt.subplot(111, frame_on=False)
        
        #remove axis
        plot.xaxis.set_visible(False) 
        plot.yaxis.set_visible(False) 
        
        #create the table plot and position it in the upper left corner
        table(plot, desc,loc='upper right')
        
        #save the plot as a png file
        plt.savefig('desc_%s.png'%(nombre_archivo_resultados))
        plt.close()
        
        results.boxplot()
        plt.savefig('boxplot_%s.png'%(nombre_archivo_resultados))
        plt.close()
    
        # run a repeated experiment
    def experiment(trainX, trainY, testX, testY, repeats, batch_size, epochs, optimization, activation, dropout, hidden_layers, neurons):
        # run experiment
        error_scores = list()
        for r in range(repeats):
            # fit the model
            lstm_model = Entrenamiento.fit_lstm(trainX, trainY, batch_size, epochs, optimization, activation, dropout, hidden_layers, neurons)
            
            lstm_model.predict(trainX, batch_size=batch_size)
            
        # report performance
        rmse = Entrenamiento.evaluate(lstm_model, testX, testY, batch_size)
        print('%d) Test RMSE: %.3f' % (r+1, rmse))
        error_scores.append(neurons)
        return error_scores
        
#QUITAR PARALEL DE ESTE METODO PARA CORRER LAS PRUEBAS

    def run_experiment(trainX, trainY, testX, testY, batch_size=[1], epochs=[1], optimization=["adam"], activation=["sigmoid"], dropout=[0], hidden_layers=[1], neurons=[1]):
        
        results = []
        results_data_frame = DataFrame()
        repeats = 1
        
        if (len(batch_size) > 1):
            for b in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, b, epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0])
        
        if (len(epochs) > 1):
            for e in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], e, optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0])

        if (len(optimization) > 1):
            for o in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], o, activation[0], dropout[0], hidden_layers[0], neurons[0])
                
        if (len(activation) > 1):
            for a in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], a, dropout[0], hidden_layers[0], neurons[0])
                
        if (len(dropout) > 1):
            for d in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], d, hidden_layers[0], neurons[0])
                
        if (len(hidden_layers) > 1):
            for h in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], h, neurons[0])
                
        if (len(neurons) > 1):
            for n in batch_size:
                Entrenamiento.experiment(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], n)
        '''
        if (len(batch_size) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, b, epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0]) for b in batch_size)
            
        elif (len(epochs) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], e, optimization[0], activation[0], dropout[0], hidden_layers[0], neurons[0]) for e in epochs)
            
        elif (len(optimization) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], o, activation[0], dropout[0], hidden_layers[0], neurons[0]) for o in optimization)
            
        elif (len(activation) > 1):
            results = Parallel(n_jobs=4)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], a, dropout[0], hidden_layers[0], neurons[0]) for a in activation)
            
        elif (len(dropout) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], d, hidden_layers[0], neurons[0]) for d in dropout)
            
        elif (len(hidden_layers) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], h, neurons[0]) for h in hidden_layers)
            
        elif (len(neurons) > 1):
            results = Parallel(n_jobs=3)(delayed (Entrenamiento.experiment)(trainX, trainY, testX, testY, repeats, batch_size[0], epochs[0], optimization[0], activation[0], dropout[0], hidden_layers[0], n) for n in neurons)
        '''
        for i in range(len(results)):
            results_data_frame[str(i)] = results[i]
        
        # summarize results
        print(results);
        #print(results_data_frame.describe())
        # save boxplot
        #Entrenamiento.save_results(results_data_frame, "nombre_archivo")

###########################################################################################################    
nombre_sujeto = "AC"
nombre_postura = "ACOSTADO"
nombre_emisferio = "DERECHO"


PATH = ("C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/%s/%s-%s-VE.csv"%(nombre_sujeto, nombre_sujeto, nombre_postura))
X = pd.read_csv(PATH, sep="	")

# normalize the dataset
scaler = MinMaxScaler(feature_range=(-1, 1))

VFSCd = scaler.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
VFSCi = scaler.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
PAMn = scaler.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
# fix random seed for reproducibility
numpy.random.seed(7)

#Dar formato float a las señales
PAMn, VFSCd = PAMn.astype('float32'), VFSCd.astype('float32')
PAMn, VFSCd = numpy.array(PAMn), numpy.array(VFSCd)

# Validacion Valanceada
train_size = int(len(PAMn) * 0.75)
test_size = len(PAMn) - train_size
train_PAM, train_VFSCd = PAMn[0:train_size], VFSCd[0:train_size]
test_PAM, test_VFSCd = PAMn[train_size:len(PAMn)], VFSCd[train_size:len(VFSCd)]

# Reshape segun el formato que acepta Keras
# reshape input to be [samples, time steps, features]
train_PAM = numpy.reshape(train_PAM, (train_PAM.shape[0], 1, train_PAM.shape[1]))
test_PAM = numpy.reshape(test_PAM, (test_PAM.shape[0], 1, test_PAM.shape[1]))
# create and fit the LSTM network
##############################################################################################################################################################
##Hyperparametros
numpy.random.uniform(low=0.5, high=13.3, size=(5,))
#################
nombre_archivo_resultados = nombre_sujeto+"_"+nombre_postura+"_"+nombre_emisferio
Entrenamiento.run_experiment(train_PAM, train_VFSCd, test_PAM, test_VFSCd, epochs = [2,3])
##############################################################################################################################################################

        
###########################################################################################################################
#random search
#https://github.com/llSourcell/hyperparameter_optimization_strategies/blob/master/random_search.py


Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))

#################








# Use scikit-learn to grid search the batch size and epochs
import pandas as pd
import numpy
import keras.backend as K
import tensorflow as tf
import math
from scipy import stats
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.wrappers.scikit_learn import KerasRegressor

    
def correlation_coefficient_loss(y_true, y_pred):
    fsp = y_pred - K.mean(y_pred,axis=0) #you take the mean over the batch, keeping the features separate.   
    fst = y_true - K.mean(y_true,axis=0) 
        #mean shape: (1,10)
        #fst shape keeps (batch,10)
    
    devP = K.std(y_pred,axis=0)  
    devT = K.std(y_true,axis=0)
        #dev shape: (1,10)
    
    return K.sum(K.mean(fsp*fst,axis=0)/(devP*devT))

# Function to create model, required for KerasClassifier
def create_model(neurons=1):
    # create model
    model = Sequential()
    #model.add(LSTM(1, return_sequences=True))
    model.add(LSTM(neurons))
    model.add(Dense(1))
    # Compile model
    model.compile(loss=correlation_coefficient_loss, optimizer='adam')
    return model

def create_dataset():
    nombre_sujeto = "AC"
    nombre_postura = "ACOSTADO"
    nombre_emisferio = "DERECHO"
    
    PATH = ("C:/Users/Luis.O.A/Documents/USACH/Tesis/Dataset/Sujetos/Muestreo 0.4/%s/%s-%s-VE.csv"%(nombre_sujeto, nombre_sujeto, nombre_postura))
    X = pd.read_csv(PATH, sep="	")
    
    # normalize the dataset
    scaler = MinMaxScaler(feature_range=(-1, 1))
    
    VFSCd = scaler.fit_transform(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
    VFSCi = scaler.fit_transform(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
    PAMn = scaler.fit_transform(X.PAMn.values.reshape((len(X.PAMn.values), 1)))
    # fix random seed for reproducibility
    numpy.random.seed(7)
    
    #Dar formato float a las señales
    PAMn, VFSCd, VFSCi = PAMn.astype('float32'), VFSCd.astype('float32'), VFSCi.astype('float32')
    PAMn, VFSCd, VFSCi = numpy.array(PAMn), numpy.array(VFSCd), numpy.array(VFSCi)
    
    
    # Reshape segun el formato que acepta Keras
    # reshape input to be [samples, time steps, features]
    PAMn = numpy.reshape(PAMn, (PAMn.shape[0], 1, PAMn.shape[1]))
    return PAMn, VFSCd, VFSCi

def run_experiment(hyperparam_grid):
    # create model
    model = KerasRegressor(build_fn=create_model, verbose=0)

    grid = GridSearchCV(estimator=model, param_grid=hyperparam_grid, cv=5, n_jobs=1)
    grid_result = grid.fit(X, Y1)
    # summarize results
    print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
    means = grid_result.cv_results_['mean_test_score']
    stds = grid_result.cv_results_['std_test_score']
    params = grid_result.cv_results_['params']
    for mean, stdev, param in zip(means, stds, params):
        print("%f (%f) with: %r" % (mean, stdev, param))
    

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
# split into input (X) and output (Y) variables
X, Y1, Y2 = create_dataset()

# define the grid search parameters
##Tuning the Number of Epochs
##Tuning the Batch Size
##Tuning the Number of Neurons
##Tuning the Number of Hidden Layers
#################################################################### batch_size
batch_size = []
for i in range(1,500):
    if (math.floor(X.shape[0] - (X.shape[0]/5))%i)==0:
        batch_size.append(i)

hyperparam_grid = dict(batch_size=batch_size)
run_experiment(hyperparam_grid)
        
######################################################################## epochs
#epochs = numpy.random.randint(low=1, high=10000, size=(10,))
epochs = [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]

hyperparam_grid = dict(epochs=epochs)
run_experiment(hyperparam_grid)

####################################################################### neurons
neurons = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

hyperparam_grid = dict(neurons=neurons)
run_experiment(hyperparam_grid)

################################################################## dropout_rate
dropout_rate = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
hyperparam_grid = dict(dropout_rate=dropout_rate)
run_experiment(hyperparam_grid)





batch_size = [1]

epochs = [100]

neurons = [10]

hyperparam_grid = dict(batch_size=batch_size, epochs=epochs, neurons=neurons)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
run_experiment(hyperparam_grid)<file_sep>import os
script_folder = './Python/Regresion Logistica - Clasificacion Imagenes/sklearn-mnist'
os.makedirs(script_folder, exist_ok=True)<file_sep>import Workspace_Experiment as we 

ds = we.load_workspace.get_default_datastore()
print(ds.datastore_type, ds.account_name, ds.container_name)

ds.upload(src_dir='./Python/Regresion Logistica - Venta Online/Social_Network_Ads.csv', target_path='mnist', overwrite=True, show_progress=True)<file_sep><<<<<<< HEAD
<<<<<<< HEAD
#Ruta del Dataset
RUTA_DATASET <- "C:/Users/Luis.O.A/Documents/Analisis Cervezas"
setwd(RUTA_DATASET)

#Leer Dataset
data=read.csv("beer_reviews.csv")

#Eliminar datos NA
data= na.omit(data)

#Resumen medidas de tendencia 
summary(data)

#Desviacion estandar para observar desviacion con respecto a la media
sd(data$review_appearance)
sd(data$review_aroma)
sd(data$review_palate)
sd(data$review_taste)


#¿Qué cervecería produce la cerveza más fuerte según ABV? 
unique(data$brewery_name[data$beer_abv == max(data$beer_abv)])

#¿Si tuviera que elegir 3 cervezas para recomendar usando sólo estos datos, cuáles elegiría?
#El siguente sumatoria de los campos "review" es equivalente al campo "review_overall"
new_data <- data.frame(nom = data$beer_name, suma = data$review_aroma + data$review_appearance + data$review_palate + data$review_taste)
grupo <- aggregate(new_data$suma, by=list(nom=new_data$nom), FUN=sum)

head(grupo[order(grupo$x, decreasing = TRUE),], 3)

#¿Cual de los factores (aroma, taste, aperance, palete) es ams importante para 
#determinar la calidad general de una cerveza?

hist(data$review_aroma, xlab="review_aroma", main="review_aroma")
hist(data$review_taste, xlab="review_taste", main="review_taste")
hist(data$review_appearance, xlab="review_appearance", main="review_appearance")
hist(data$review_palate, xlab="review_palate", main="review_palate")

hist(data$review_overall, xlab="review_overall", main="review_overall")

d <- data.frame(data$review_aroma, 
data$review_taste, 
data$review_appearance, 
data$review_palate)

cor(d, data$review_overall, method = "pearson")

#Si yo tipicamente disfruto una cerveza debido a su 
#aroma y apariencia, ¿que estilo de cerveza deberia probar?
new_ar_ap <- data.frame(nom = data$beer_style, suma = data$review_aroma + data$review_appearance)
    
grupo <- aggregate(new_ar_ap$suma, by=list(nom=new_ar_ap$nom), FUN=sum)
head(grupo[order(grupo$x, decreasing = TRUE),], 1)
=======
#Ruta del Dataset
RUTA_DATASET <- "C:/Users/Luis.O.A/Documents/Analisis Cervezas"
setwd(RUTA_DATASET)

#Leer Dataset
data=read.csv("beer_reviews.csv")

#Eliminar datos NA
data= na.omit(data)

#Resumen medidas de tendencia 
summary(data)

#Desviacion estandar para observar desviacion con respecto a la media
sd(data$review_appearance)
sd(data$review_aroma)
sd(data$review_palate)
sd(data$review_taste)


#¿Qué cervecería produce la cerveza más fuerte según ABV? 
unique(data$brewery_name[data$beer_abv == max(data$beer_abv)])

#¿Si tuviera que elegir 3 cervezas para recomendar usando sólo estos datos, cuáles elegiría?
#El siguente sumatoria de los campos "review" es equivalente al campo "review_overall"
new_data <- data.frame(nom = data$beer_name, suma = data$review_aroma + data$review_appearance + data$review_palate + data$review_taste)
grupo <- aggregate(new_data$suma, by=list(nom=new_data$nom), FUN=sum)

head(grupo[order(grupo$x, decreasing = TRUE),], 3)

#¿Cual de los factores (aroma, taste, aperance, palete) es ams importante para 
#determinar la calidad general de una cerveza?

hist(data$review_aroma, xlab="review_aroma", main="review_aroma")
hist(data$review_taste, xlab="review_taste", main="review_taste")
hist(data$review_appearance, xlab="review_appearance", main="review_appearance")
hist(data$review_palate, xlab="review_palate", main="review_palate")

hist(data$review_overall, xlab="review_overall", main="review_overall")

d <- data.frame(data$review_aroma, 
data$review_taste, 
data$review_appearance, 
data$review_palate)

cor(d, data$review_overall, method = "pearson")

#Si yo tipicamente disfruto una cerveza debido a su 
#aroma y apariencia, ¿que estilo de cerveza deberia probar?
new_ar_ap <- data.frame(nom = data$beer_style, suma = data$review_aroma + data$review_appearance)
    
grupo <- aggregate(new_ar_ap$suma, by=list(nom=new_ar_ap$nom), FUN=sum)
head(grupo[order(grupo$x, decreasing = TRUE),], 1)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
#Ruta del Dataset
RUTA_DATASET <- "C:/Users/Luis.O.A/Documents/Analisis Cervezas"
setwd(RUTA_DATASET)

#Leer Dataset
data=read.csv("beer_reviews.csv")

#Eliminar datos NA
data= na.omit(data)

#Resumen medidas de tendencia 
summary(data)

#Desviacion estandar para observar desviacion con respecto a la media
sd(data$review_appearance)
sd(data$review_aroma)
sd(data$review_palate)
sd(data$review_taste)


#¿Qué cervecería produce la cerveza más fuerte según ABV? 
unique(data$brewery_name[data$beer_abv == max(data$beer_abv)])

#¿Si tuviera que elegir 3 cervezas para recomendar usando sólo estos datos, cuáles elegiría?
#El siguente sumatoria de los campos "review" es equivalente al campo "review_overall"
new_data <- data.frame(nom = data$beer_name, suma = data$review_aroma + data$review_appearance + data$review_palate + data$review_taste)
grupo <- aggregate(new_data$suma, by=list(nom=new_data$nom), FUN=sum)

head(grupo[order(grupo$x, decreasing = TRUE),], 3)

#¿Cual de los factores (aroma, taste, aperance, palete) es ams importante para 
#determinar la calidad general de una cerveza?

hist(data$review_aroma, xlab="review_aroma", main="review_aroma")
hist(data$review_taste, xlab="review_taste", main="review_taste")
hist(data$review_appearance, xlab="review_appearance", main="review_appearance")
hist(data$review_palate, xlab="review_palate", main="review_palate")

hist(data$review_overall, xlab="review_overall", main="review_overall")

d <- data.frame(data$review_aroma, 
data$review_taste, 
data$review_appearance, 
data$review_palate)

cor(d, data$review_overall, method = "pearson")

#Si yo tipicamente disfruto una cerveza debido a su 
#aroma y apariencia, ¿que estilo de cerveza deberia probar?
new_ar_ap <- data.frame(nom = data$beer_style, suma = data$review_aroma + data$review_appearance)
    
grupo <- aggregate(new_ar_ap$suma, by=list(nom=new_ar_ap$nom), FUN=sum)
head(grupo[order(grupo$x, decreasing = TRUE),], 1)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
  <file_sep><<<<<<< HEAD
<<<<<<< HEAD
import numpy
import pandas
import statsmodels.formula.api as smf
import statsmodels.stats.multicomp as multi 
import matplotlib.pyplot as plt
import seaborn as sns
data = pandas.read_csv(‘addhealth_pds.csv’, low_memory=False)
data = data[['IYEAR’, 'H1GI1Y’, 'H1FS15’]].dropna()
data['AGE’]=data['IYEAR’] - data['H1GI1Y’]
data['AGE’]= data['AGE’].convert_objects(convert_numeric=True)
sub1 = data[['AGE’, 'H1FS15’]].dropna()
sub1=sub1[(sub1['AGE’] >= 13)&(sub1['AGE’] <= 17)]
print ('means for AGE by feeling tired status’)
m1= sub1.groupby('H1FS15’).mean()
print (m1)
print ('standard deviations for AGE by feeling tired status’)
sd1 = sub1.groupby('H1FS15’).std()
print (sd1)
mc1 = multi.MultiComparison(sub1['AGE’], sub1['H1FS15’])
res1 = mc1.tukeyhsd()
print(res1.summary())
sns.distplot(sub1['AGE’]);
=======
import numpy
import pandas
import statsmodels.formula.api as smf
import statsmodels.stats.multicomp as multi 
import matplotlib.pyplot as plt
import seaborn as sns
data = pandas.read_csv(‘addhealth_pds.csv’, low_memory=False)
data = data[['IYEAR’, 'H1GI1Y’, 'H1FS15’]].dropna()
data['AGE’]=data['IYEAR’] - data['H1GI1Y’]
data['AGE’]= data['AGE’].convert_objects(convert_numeric=True)
sub1 = data[['AGE’, 'H1FS15’]].dropna()
sub1=sub1[(sub1['AGE’] >= 13)&(sub1['AGE’] <= 17)]
print ('means for AGE by feeling tired status’)
m1= sub1.groupby('H1FS15’).mean()
print (m1)
print ('standard deviations for AGE by feeling tired status’)
sd1 = sub1.groupby('H1FS15’).std()
print (sd1)
mc1 = multi.MultiComparison(sub1['AGE’], sub1['H1FS15’])
res1 = mc1.tukeyhsd()
print(res1.summary())
sns.distplot(sub1['AGE’]);
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======
import numpy
import pandas
import statsmodels.formula.api as smf
import statsmodels.stats.multicomp as multi 
import matplotlib.pyplot as plt
import seaborn as sns
data = pandas.read_csv(‘addhealth_pds.csv’, low_memory=False)
data = data[['IYEAR’, 'H1GI1Y’, 'H1FS15’]].dropna()
data['AGE’]=data['IYEAR’] - data['H1GI1Y’]
data['AGE’]= data['AGE’].convert_objects(convert_numeric=True)
sub1 = data[['AGE’, 'H1FS15’]].dropna()
sub1=sub1[(sub1['AGE’] >= 13)&(sub1['AGE’] <= 17)]
print ('means for AGE by feeling tired status’)
m1= sub1.groupby('H1FS15’).mean()
print (m1)
print ('standard deviations for AGE by feeling tired status’)
sd1 = sub1.groupby('H1FS15’).std()
print (sd1)
mc1 = multi.MultiComparison(sub1['AGE’], sub1['H1FS15’])
res1 = mc1.tukeyhsd()
print(res1.summary())
sns.distplot(sub1['AGE’]);
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep>#https://www.kaggle.com/pierremegret/gensim-word2vec-tutorial

import re  # For preprocessing
import pandas as pd  # For data handling
from time import time  # To time our operations
from collections import defaultdict  # For word frequency

import spacy  # For preprocessing

import logging  # Setting up the loggings to monitor gensim
logging.basicConfig(format="%(levelname)s - %(asctime)s: %(message)s", datefmt= '%H:%M:%S', level=logging.INFO)

df_clean = pd.read_csv(r'D:\Investigacion Word2Vec\Modelo\pubmed_sentences.csv')

#Bigrams

from gensim.models.phrases import Phrases, Phraser
sent = [row.split() for row in df_clean['clean']]
phrases = Phrases(sent, min_count=30, progress_per=10000)

bigram = Phraser(phrases)
sentences = bigram[sent]

#Most Frequent Words:
word_freq = defaultdict(int)
for sent in sentences:
    for i in sent:
        word_freq[i] += 1
len(word_freq)

print(sorted(word_freq, key=word_freq.get, reverse=True)[:10])

#Training the model
import multiprocessing

from gensim.models import Word2Vec

cores = multiprocessing.cpu_count() # Count the number of cores in a computer

w2v_model = Word2Vec(min_count=20,
                     window=8,
                     size=200,
                     sample=6e-5, 
                     alpha=0.01, 
                     min_alpha=0.0001, 
                     negative=20,
                     workers=cores-1,
                     sg=1)

#Building the Vocabulary Table
t = time()

w2v_model.build_vocab(sentences, progress_per=10000)

print('Time to build vocab: {} mins'.format(round((time() - t) / 60, 2)))

#Training of the model:
t = time()

w2v_model.train(sentences, total_examples=w2v_model.corpus_count, epochs=30, report_delay=1)

print('Time to train the model: {} mins'.format(round((time() - t) / 60, 2)))

w2v_model.init_sims(replace=True)

#Save the Model
w2v_model.save("w2v_model")
model = Word2Vec.load("w2v_model")

#Exploring the model
#print(w2v_model.wv.most_similar(positive=["homer"]))


<file_sep><<<<<<< HEAD
<<<<<<< HEAD

install.packages('e1071', dep = TRUE)
install.packages('xlsx', dep = TRUE)
install.packages('caret', dep = TRUE)
install.packages('caTools', dep = TRUE)
install.packages('plotly', dep = TRUE)

library(e1071)
library(xlsx)
library(caret)
library(caTools)
library(plotly)

#Modifica estas rutas segun tu ambiente de desarrollo (la variable RUTA_TXT_XLS esta explicada en el correo)
RUTA_DATASET <- "C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\grupo 6 Wine Quality"
RUTA_TXT_XLS <- "C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\Trabajos\\5"

setwd(RUTA_DATASET)

wine <- read.csv('winequality-red.csv', sep=';')

#Funcion que permite normalizar los datos entre 0 a 1
normalize <- function(x) {
	x = (x-min(x))/(max(x)-min(x))
	x
}

#Se normalizan los datos menos la clase (wine quality)
wine$fixed.acidity <- NULL
wine$volatile.acidity = normalize(wine$volatile.acidity)
wine$citric.acid = normalize(wine$citric.acid)
wine$residual.sugar = normalize(wine$residual.sugar)
wine$chlorides = normalize(wine$chlorides)
wine$free.sulfur.dioxide = normalize(wine$free.sulfur.dioxide)
wine$total.sulfur.dioxide = normalize(wine$total.sulfur.dioxide)
wine$density = normalize(wine$density)
wine$pH = normalize(wine$pH)
wine$sulphates = normalize(wine$sulphates)
wine$alcohol = normalize(wine$alcohol)

#hist(wine$fixed.acidity,breaks=10, xlab="wine",col="lightblue", main="")
#la clase es transformada a factor.
wine$quality = factor(wine$quality,labels=c("III", "IV", "V", "VI", "VII", "VIII"))

#Se comprobara el error con cada uno de los tipos de kernel
#Los rangos de los parametros de cada kernel estan sugeridos aqui: (rstudio)
#http://rstudio-pubs-static.s3.amazonaws.com/252840_176bda2bbab2428cbf042f8282d717e1.html
modelo <- function(wine, k){
	#Semilla para no obtener casos destintos en cada ejecucion
	set.seed(123)
	
	#No es necesario separar los datos en test y training porque el metodo ya provee validacion cruzada
	if(k == "radial"){
		# Kernel radial
		obj <- tune(svm, quality~., data = wine, kernel = k, ranges = list(gamma = 2^(-7:12), cost = 2^(-7:14) ), tunecontrol = tune.control(sampling = "cross", cross = 2))
		
	}else if(k == "polynomial"){
		# Kernel polynomial
		obj <- tune(svm, quality~., data = wine, kernel = k, degree = 2, ranges = list(cost = 2^(-2:9)), tunecontrol = tune.control(sampling = "cross", cross = 2))
		#obj <- tune(svm, quality~., data = wine, kernel = k, degree = 2, ranges = list(cost=10^seq(-2, 1, by=0.25)), tunecontrol = tune.control(sampling = "cross", cross = 2))
		
	}else if(k == "linear"){
		# Kernel linear
		obj <- tune(svm, quality~., data = wine, kernel = k, ranges = list(cost = 2^(-3:6)), tunecontrol = tune.control(sampling = "cross", cross = 2))
	}else{
		# Kernel sigmoid
		obj <- tune(svm, quality~., data = wine, kernel = k, ranges=list(gamma = 2^(-7:12), cost = 2^(-7:14)), tunecontrol = tune.control(sampling = "cross", cross = 2))
	}
	summary(obj)

	x <- subset(wine, select = -quality)
	y <- wine$quality

	pred <- predict(obj$best.model, x)
	tabla.resultados <- table(pred, y)
	
	#Almacena los resultados en un excel para luego analizarlos
	setwd(RUTA_TXT_XLS)
	write.xlsx(obj$performances, paste(k, "xlsx", sep = "."), sheetName="Sheet1", append = FALSE)
	
	escribir.tabla <- function(x,row.names=FALSE,col.names=TRUE,...) {
		write.table(x,paste(k, "txt", sep = "."),sep="\t",row.names=row.names,col.names=col.names,...)
	}

	#Almacena la tabla de resultados de clasificacion en txt para luego analizarlos
	escribir.tabla(tabla.resultados)

	return(obj)
}

#Grafica el error de cada uno de los modelos segun su kernel
graficar <- function(model, k){
	#Ordeno los errores de mayor a menor paraque el grafico no sea tan feo
	Error <- sort(model$performances$error, decreasing = TRUE)
	x <- c(1:length(model$performances$error))
	data <- data.frame(x, sort(model$performances$error, decreasing = TRUE))
	plot_ly(data, x = ~x, y = ~Error, name = k, type = 'scatter', mode = 'lines+markers') 


}

#Mejor modelo = radial; gamma = 0.5; cost = 2
model.r <- modelo(wine, "radial")
#Porcentaje de Error en la clasificacion de wine
error.r <- mean(model.r$best.model$fitted!=wine$quality)*100 #Error = 8.380238
graficar(model.r, "radial");								 #Al eliminar "" 10.69418

model.p <- modelo(wine, "polynomial")
#Porcentaje de Error en la clasificacion de wine
error.p <- mean(model.p$best.model$fitted!=wine$quality)*100#Error = 39.27455
graficar(model.p, "polynomial");

model.l <- modelo(wine, "linear")
#Porcentaje de Error en la clasificacion de wine
error.l <- mean(model.l$best.model$fitted!=wine$quality)*100#Error = 41.2758
graficar(model.l, "linear");

model.s <- modelo(wine, "sigmoid")
#Porcentaje de Error en la clasificacion de wine
error.s <- mean(model.s$best.model$fitted!=wine$quality)*100#Error = 43.15197
graficar(model.s, "sigmoid");

#Grafico de los porcentajes de error
plot_ly(
  x = c("radial","polynomial","linear","sigmoid"),
  y = c(error.r, error.p, error.l, error.s),
  name = "% Error Segun Kernel",
  type = "bar"
)
=======

install.packages('e1071', dep = TRUE)
install.packages('xlsx', dep = TRUE)
install.packages('caret', dep = TRUE)
install.packages('caTools', dep = TRUE)
install.packages('plotly', dep = TRUE)

library(e1071)
library(xlsx)
library(caret)
library(caTools)
library(plotly)

#Modifica estas rutas segun tu ambiente de desarrollo (la variable RUTA_TXT_XLS esta explicada en el correo)
RUTA_DATASET <- "C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\grupo 6 Wine Quality"
RUTA_TXT_XLS <- "C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\Trabajos\\5"

setwd(RUTA_DATASET)

wine <- read.csv('winequality-red.csv', sep=';')

#Funcion que permite normalizar los datos entre 0 a 1
normalize <- function(x) {
	x = (x-min(x))/(max(x)-min(x))
	x
}

#Se normalizan los datos menos la clase (wine quality)
wine$fixed.acidity <- NULL
wine$volatile.acidity = normalize(wine$volatile.acidity)
wine$citric.acid = normalize(wine$citric.acid)
wine$residual.sugar = normalize(wine$residual.sugar)
wine$chlorides = normalize(wine$chlorides)
wine$free.sulfur.dioxide = normalize(wine$free.sulfur.dioxide)
wine$total.sulfur.dioxide = normalize(wine$total.sulfur.dioxide)
wine$density = normalize(wine$density)
wine$pH = normalize(wine$pH)
wine$sulphates = normalize(wine$sulphates)
wine$alcohol = normalize(wine$alcohol)

#hist(wine$fixed.acidity,breaks=10, xlab="wine",col="lightblue", main="")
#la clase es transformada a factor.
wine$quality = factor(wine$quality,labels=c("III", "IV", "V", "VI", "VII", "VIII"))

#Se comprobara el error con cada uno de los tipos de kernel
#Los rangos de los parametros de cada kernel estan sugeridos aqui: (rstudio)
#http://rstudio-pubs-static.s3.amazonaws.com/252840_176bda2bbab2428cbf042f8282d717e1.html
modelo <- function(wine, k){
	#Semilla para no obtener casos destintos en cada ejecucion
	set.seed(123)
	
	#No es necesario separar los datos en test y training porque el metodo ya provee validacion cruzada
	if(k == "radial"){
		# Kernel radial
		obj <- tune(svm, quality~., data = wine, kernel = k, ranges = list(gamma = 2^(-7:12), cost = 2^(-7:14) ), tunecontrol = tune.control(sampling = "cross", cross = 2))
		
	}else if(k == "polynomial"){
		# Kernel polynomial
		obj <- tune(svm, quality~., data = wine, kernel = k, degree = 2, ranges = list(cost = 2^(-2:9)), tunecontrol = tune.control(sampling = "cross", cross = 2))
		#obj <- tune(svm, quality~., data = wine, kernel = k, degree = 2, ranges = list(cost=10^seq(-2, 1, by=0.25)), tunecontrol = tune.control(sampling = "cross", cross = 2))
		
	}else if(k == "linear"){
		# Kernel linear
		obj <- tune(svm, quality~., data = wine, kernel = k, ranges = list(cost = 2^(-3:6)), tunecontrol = tune.control(sampling = "cross", cross = 2))
	}else{
		# Kernel sigmoid
		obj <- tune(svm, quality~., data = wine, kernel = k, ranges=list(gamma = 2^(-7:12), cost = 2^(-7:14)), tunecontrol = tune.control(sampling = "cross", cross = 2))
	}
	summary(obj)

	x <- subset(wine, select = -quality)
	y <- wine$quality

	pred <- predict(obj$best.model, x)
	tabla.resultados <- table(pred, y)
	
	#Almacena los resultados en un excel para luego analizarlos
	setwd(RUTA_TXT_XLS)
	write.xlsx(obj$performances, paste(k, "xlsx", sep = "."), sheetName="Sheet1", append = FALSE)
	
	escribir.tabla <- function(x,row.names=FALSE,col.names=TRUE,...) {
		write.table(x,paste(k, "txt", sep = "."),sep="\t",row.names=row.names,col.names=col.names,...)
	}

	#Almacena la tabla de resultados de clasificacion en txt para luego analizarlos
	escribir.tabla(tabla.resultados)

	return(obj)
}

#Grafica el error de cada uno de los modelos segun su kernel
graficar <- function(model, k){
	#Ordeno los errores de mayor a menor paraque el grafico no sea tan feo
	Error <- sort(model$performances$error, decreasing = TRUE)
	x <- c(1:length(model$performances$error))
	data <- data.frame(x, sort(model$performances$error, decreasing = TRUE))
	plot_ly(data, x = ~x, y = ~Error, name = k, type = 'scatter', mode = 'lines+markers') 


}

#Mejor modelo = radial; gamma = 0.5; cost = 2
model.r <- modelo(wine, "radial")
#Porcentaje de Error en la clasificacion de wine
error.r <- mean(model.r$best.model$fitted!=wine$quality)*100 #Error = 8.380238
graficar(model.r, "radial");								 #Al eliminar "" 10.69418

model.p <- modelo(wine, "polynomial")
#Porcentaje de Error en la clasificacion de wine
error.p <- mean(model.p$best.model$fitted!=wine$quality)*100#Error = 39.27455
graficar(model.p, "polynomial");

model.l <- modelo(wine, "linear")
#Porcentaje de Error en la clasificacion de wine
error.l <- mean(model.l$best.model$fitted!=wine$quality)*100#Error = 41.2758
graficar(model.l, "linear");

model.s <- modelo(wine, "sigmoid")
#Porcentaje de Error en la clasificacion de wine
error.s <- mean(model.s$best.model$fitted!=wine$quality)*100#Error = 43.15197
graficar(model.s, "sigmoid");

#Grafico de los porcentajes de error
plot_ly(
  x = c("radial","polynomial","linear","sigmoid"),
  y = c(error.r, error.p, error.l, error.s),
  name = "% Error Segun Kernel",
  type = "bar"
)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
=======

install.packages('e1071', dep = TRUE)
install.packages('xlsx', dep = TRUE)
install.packages('caret', dep = TRUE)
install.packages('caTools', dep = TRUE)
install.packages('plotly', dep = TRUE)

library(e1071)
library(xlsx)
library(caret)
library(caTools)
library(plotly)

#Modifica estas rutas segun tu ambiente de desarrollo (la variable RUTA_TXT_XLS esta explicada en el correo)
RUTA_DATASET <- "C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\grupo 6 Wine Quality"
RUTA_TXT_XLS <- "C:\\Users\\Luis.O.A\\Documents\\USACH\\Mineria de Datos\\Trabajos\\5"

setwd(RUTA_DATASET)

wine <- read.csv('winequality-red.csv', sep=';')

#Funcion que permite normalizar los datos entre 0 a 1
normalize <- function(x) {
	x = (x-min(x))/(max(x)-min(x))
	x
}

#Se normalizan los datos menos la clase (wine quality)
wine$fixed.acidity <- NULL
wine$volatile.acidity = normalize(wine$volatile.acidity)
wine$citric.acid = normalize(wine$citric.acid)
wine$residual.sugar = normalize(wine$residual.sugar)
wine$chlorides = normalize(wine$chlorides)
wine$free.sulfur.dioxide = normalize(wine$free.sulfur.dioxide)
wine$total.sulfur.dioxide = normalize(wine$total.sulfur.dioxide)
wine$density = normalize(wine$density)
wine$pH = normalize(wine$pH)
wine$sulphates = normalize(wine$sulphates)
wine$alcohol = normalize(wine$alcohol)

#hist(wine$fixed.acidity,breaks=10, xlab="wine",col="lightblue", main="")
#la clase es transformada a factor.
wine$quality = factor(wine$quality,labels=c("III", "IV", "V", "VI", "VII", "VIII"))

#Se comprobara el error con cada uno de los tipos de kernel
#Los rangos de los parametros de cada kernel estan sugeridos aqui: (rstudio)
#http://rstudio-pubs-static.s3.amazonaws.com/252840_176bda2bbab2428cbf042f8282d717e1.html
modelo <- function(wine, k){
	#Semilla para no obtener casos destintos en cada ejecucion
	set.seed(123)
	
	#No es necesario separar los datos en test y training porque el metodo ya provee validacion cruzada
	if(k == "radial"){
		# Kernel radial
		obj <- tune(svm, quality~., data = wine, kernel = k, ranges = list(gamma = 2^(-7:12), cost = 2^(-7:14) ), tunecontrol = tune.control(sampling = "cross", cross = 2))
		
	}else if(k == "polynomial"){
		# Kernel polynomial
		obj <- tune(svm, quality~., data = wine, kernel = k, degree = 2, ranges = list(cost = 2^(-2:9)), tunecontrol = tune.control(sampling = "cross", cross = 2))
		#obj <- tune(svm, quality~., data = wine, kernel = k, degree = 2, ranges = list(cost=10^seq(-2, 1, by=0.25)), tunecontrol = tune.control(sampling = "cross", cross = 2))
		
	}else if(k == "linear"){
		# Kernel linear
		obj <- tune(svm, quality~., data = wine, kernel = k, ranges = list(cost = 2^(-3:6)), tunecontrol = tune.control(sampling = "cross", cross = 2))
	}else{
		# Kernel sigmoid
		obj <- tune(svm, quality~., data = wine, kernel = k, ranges=list(gamma = 2^(-7:12), cost = 2^(-7:14)), tunecontrol = tune.control(sampling = "cross", cross = 2))
	}
	summary(obj)

	x <- subset(wine, select = -quality)
	y <- wine$quality

	pred <- predict(obj$best.model, x)
	tabla.resultados <- table(pred, y)
	
	#Almacena los resultados en un excel para luego analizarlos
	setwd(RUTA_TXT_XLS)
	write.xlsx(obj$performances, paste(k, "xlsx", sep = "."), sheetName="Sheet1", append = FALSE)
	
	escribir.tabla <- function(x,row.names=FALSE,col.names=TRUE,...) {
		write.table(x,paste(k, "txt", sep = "."),sep="\t",row.names=row.names,col.names=col.names,...)
	}

	#Almacena la tabla de resultados de clasificacion en txt para luego analizarlos
	escribir.tabla(tabla.resultados)

	return(obj)
}

#Grafica el error de cada uno de los modelos segun su kernel
graficar <- function(model, k){
	#Ordeno los errores de mayor a menor paraque el grafico no sea tan feo
	Error <- sort(model$performances$error, decreasing = TRUE)
	x <- c(1:length(model$performances$error))
	data <- data.frame(x, sort(model$performances$error, decreasing = TRUE))
	plot_ly(data, x = ~x, y = ~Error, name = k, type = 'scatter', mode = 'lines+markers') 


}

#Mejor modelo = radial; gamma = 0.5; cost = 2
model.r <- modelo(wine, "radial")
#Porcentaje de Error en la clasificacion de wine
error.r <- mean(model.r$best.model$fitted!=wine$quality)*100 #Error = 8.380238
graficar(model.r, "radial");								 #Al eliminar "" 10.69418

model.p <- modelo(wine, "polynomial")
#Porcentaje de Error en la clasificacion de wine
error.p <- mean(model.p$best.model$fitted!=wine$quality)*100#Error = 39.27455
graficar(model.p, "polynomial");

model.l <- modelo(wine, "linear")
#Porcentaje de Error en la clasificacion de wine
error.l <- mean(model.l$best.model$fitted!=wine$quality)*100#Error = 41.2758
graficar(model.l, "linear");

model.s <- modelo(wine, "sigmoid")
#Porcentaje de Error en la clasificacion de wine
error.s <- mean(model.s$best.model$fitted!=wine$quality)*100#Error = 43.15197
graficar(model.s, "sigmoid");

#Grafico de los porcentajes de error
plot_ly(
  x = c("radial","polynomial","linear","sigmoid"),
  y = c(error.r, error.p, error.l, error.s),
  name = "% Error Segun Kernel",
  type = "bar"
)
>>>>>>> 4966aae922150d281e7f47d431009d8054258bc0
<file_sep>import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import numpy
import matplotlib.pyplot as plt
import numpy as np
import csv
import os

PATH_SUJETOS = ("C:/Users/Luis/Documents/DataScience/Tesis/Datos/SUJETOS/%s/%s-%s-VE.csv")
PATH_ESCALON = ("C:/Users/Luis/Documents/DataScience/Tesis/Datos/ESCALON_PRESION/ESCALON.csv")
PATH_RESULTADO = ("C:/Users/Luis/Documents/DataScience/Tesis/Resultados/Escalon/%s")
PATH_RESULTADO_MFARI = ("C:/Users/Luis/Documents/DataScience/Tesis/Calculo ARI/v15.9.10/Data/%s")
PATH_RESULTADO_FORMATEADO = ("C:/Users/Luis/Documents/DataScience/Tesis/Calculo ARI/v15.9.10/Data/%s/%s-%s.txt")
PATH_RESULTADO_IMAGE = ("C:/Users/Luis/Documents/DataScience/Tesis/Calculo ARI/v15.9.10/Data/%s/%s-%s-%s.png")

PATH_RESULTADO_ESCALON = (PATH_RESULTADO%("%s/%s_%s"))

def create_scalers(nombre_sujeto, nombre_postura):

    X = pd.read_csv(PATH_SUJETOS%(nombre_sujeto, nombre_sujeto, nombre_postura), sep="	")
    data_escalon = pd.read_csv(PATH_ESCALON)
    
    # normalize the dataset
    scaler_VFSCd = MinMaxScaler(feature_range=(0, 1))
    scaler_VFSCi = MinMaxScaler(feature_range=(0, 1))
    scaler_escalon = MinMaxScaler(feature_range=(0, 1))
    
    scaler_VFSCd.fit(X.VFSCd.values.reshape((len(X.VFSCd.values), 1)))
    scaler_VFSCi.fit(X.VFSCi.values.reshape((len(X.VFSCi.values), 1)))
    Escalon = scaler_escalon.fit_transform(data_escalon.ESCALON.values.reshape((len(data_escalon.ESCALON.values), 1)))
    
    return scaler_VFSCd, scaler_VFSCi, Escalon


def read_output(nombre_sujeto, nombre_postura, hemisferio, scaler_VFSCd, scaler_VFSCi):

    X = pd.read_csv(PATH_RESULTADO_ESCALON%(nombre_sujeto, nombre_postura, hemisferio)+'_output.csv', sep="	")
    if hemisferio == "Izquierdo":
        out = scaler_VFSCi.transform(X.Output.values.reshape((len(X.Output.values), 1)))
        return out
    elif hemisferio == "Derecho":
        out = scaler_VFSCd.transform(X.Output.values.reshape((len(X.Output.values), 1)))
        return out

def plotting_mathplot(hemisferio, sujeto, postura, escalon, output):
    
    time = np.round(np.arange(-78.0, 81.2, 0.4),1)

    re_escalon = numpy.reshape(escalon, (escalon.shape[0]))
    re_output = numpy.reshape(output, (output.shape[0]))
    
    fig, ax = plt.subplots()
    
    # Using set_dashes() to modify dashing of an existing line
    line1, = ax.plot(time, re_escalon, label='Escalon')
    
    # Using plot(..., dashes=...) to set the dashing when creating a line
    line2, = ax.plot(time, re_output, label='VFSC Respuesta Escalon')
    
    title=""
    ax.set_xlabel(title)
    ax.set_title("LSTM - Respuesta a Escalon")
    ax.margins(x=-0.35, y=0)
    ax.legend()
    #plt.show()
    plt.tight_layout()
    plt.savefig(PATH_RESULTADO_IMAGE%(sujeto, sujeto, postura, hemisferio), 
               quality  = 95, pad_inches =0, bbox_inches= 'tight')

def save_signal(sujeto, postura, escalon, output_izquierdo, output_derecho):

    time = np.round(np.arange(-78.0, 81.2, 0.4),1)

    df = list(zip(time.tolist(), escalon[:,0].tolist(), output_izquierdo[:,0].tolist(), output_derecho[:,0].tolist()))

    df1 = pd.DataFrame(df, columns=['Time', 'ABP', 'LCBFV', 'RCBFV'])

    if not os.path.exists(PATH_RESULTADO_MFARI%(sujeto)):
        os.makedirs(PATH_RESULTADO_MFARI%(sujeto))

    df1.to_csv(PATH_RESULTADO_FORMATEADO%(sujeto, sujeto, postura),index=False, sep="	", quoting=csv.QUOTE_NONE)
    
def run(sujeto, postura):

    scaler_VFSCd, scaler_VFSCi, escalon = create_scalers(sujeto, postura)
    
    output_izquierdo = read_output(sujeto, postura, "Izquierdo", scaler_VFSCd, scaler_VFSCi)
    output_derecho = read_output(sujeto, postura, "Derecho", scaler_VFSCd, scaler_VFSCi)

    save_signal(sujeto, postura, escalon, output_izquierdo, output_derecho)

    plotting_mathplot("izquiero", sujeto, postura, escalon, output_izquierdo)
    plotting_mathplot("derecho", sujeto, postura, escalon, output_derecho)

run("AC", "ACOSTADO")
run("AC", "PIE")
run("AC", "SENTADO")

run(sujeto='AP', postura='ACOSTADO')
run(sujeto='AP', postura='PIE')
run(sujeto='AP', postura='SENTADO')

run(sujeto='AV', postura='ACOSTADO')
run(sujeto='AV', postura='PIE')
run(sujeto='AV', postura='SENTADO')

run(sujeto='CC', postura='ACOSTADO')
run(sujeto='CC', postura='PIE')
run(sujeto='CC', postura='SENTADO')

run(sujeto='CS', postura='ACOSTADO')
run(sujeto='CS', postura='PIE')
run(sujeto='CS', postura='SENTADO')

run(sujeto='DM', postura='ACOSTADO')
run(sujeto='DM', postura='PIE')
run(sujeto='DM', postura='SENTADO')

run(sujeto='DS', postura='ACOSTADO')
run(sujeto='DS', postura='PIE')
run(sujeto='DS', postura='SENTADO')

run(sujeto='GP', postura='ACOSTADO')
run(sujeto='GP', postura='PIE')
run(sujeto='GP', postura='SENTADO')
             
run(sujeto='HF', postura='ACOSTADO')
run(sujeto='HF', postura='PIE')
run(sujeto='HF', postura='SENTADO')

run(sujeto='HS', postura='ACOSTADO')
run(sujeto='HS', postura='PIE')
run(sujeto='HS', postura='SENTADO')

run(sujeto='IH', postura='ACOSTADO')
run(sujeto='IH', postura='PIE')
run(sujeto='IH', postura='SENTADO')

run(sujeto='MM', postura='ACOSTADO')
run(sujeto='MM', postura='PIE')
run(sujeto='MM', postura='SENTADO')

run(sujeto='MR', postura='ACOSTADO')
run(sujeto='MR', postura='PIE')
run(sujeto='MR', postura='SENTADO')

run(sujeto='MV', postura='ACOSTADO')
run(sujeto='MV', postura='PIE')
run(sujeto='MV', postura='SENTADO')

run(sujeto='ND', postura='ACOSTADO')
run(sujeto='ND', postura='PIE')
run(sujeto='ND', postura='SENTADO')

run(sujeto='PC', postura='ACOSTADO')
run(sujeto='PC', postura='PIE')
run(sujeto='PC', postura='SENTADO')

run(sujeto='RO', postura='ACOSTADO')
run(sujeto='RO', postura='PIE')
run(sujeto='RO', postura='SENTADO')

run(sujeto='VT', postura='ACOSTADO')
run(sujeto='VT', postura='PIE')
run(sujeto='VT', postura='SENTADO')